1919import static org .junit .Assert .assertEquals ;
2020import static org .junit .Assert .assertFalse ;
2121import static org .junit .Assert .assertTrue ;
22- import static org .mockito .Matchers .eq ;
22+ import static org .mockito .ArgumentMatchers .eq ;
2323import static org .mockito .Mockito .any ;
2424import static org .mockito .Mockito .doReturn ;
2525import static org .mockito .Mockito .never ;
2626import static org .mockito .Mockito .times ;
2727import static org .mockito .Mockito .verify ;
2828import static org .mockito .Mockito .when ;
29- import static org .powermock .api .mockito .PowerMockito .whenNew ;
3029
3130import java .util .ArrayList ;
3231import java .util .Arrays ;
3736
3837import org .apache .cloudstack .storage .command .CopyCommand ;
3938import org .apache .cloudstack .storage .to .PrimaryDataStoreTO ;
39+ import org .junit .After ;
4040import org .junit .Before ;
4141import org .junit .Test ;
4242import org .junit .runner .RunWith ;
4343import org .mockito .InOrder ;
4444import org .mockito .InjectMocks ;
45- import org .mockito .Matchers ;
4645import org .mockito .Mock ;
46+ import org .mockito .MockedConstruction ;
4747import org .mockito .Mockito ;
4848import org .mockito .MockitoAnnotations ;
4949import org .mockito .Spy ;
50- import org .powermock .api .mockito .PowerMockito ;
51- import org .powermock .core .classloader .annotations .PrepareForTest ;
52- import org .powermock .modules .junit4 .PowerMockRunner ;
50+ import org .mockito .junit .MockitoJUnitRunner ;
5351
5452import com .cloud .agent .api .Answer ;
5553import com .cloud .agent .api .Command ;
9290import com .vmware .vim25 .VirtualMachineConfigSpec ;
9391import com .vmware .vim25 .VirtualMachineVideoCard ;
9492
95- @ RunWith (PowerMockRunner .class )
96- @ PrepareForTest ({CopyCommand .class })
93+ @ RunWith (MockitoJUnitRunner .class )
9794public class VmwareResourceTest {
9895
9996 private static final String VOLUME_PATH = "XXXXXXXXXXXX" ;
@@ -187,10 +184,12 @@ public VmwareHypervisorHost getHyperHost(VmwareContext context, Command cmd) {
187184
188185 private Map <String ,String > specsArray = new HashMap <String ,String >();
189186
187+ AutoCloseable closeable ;
188+
190189 @ Before
191190 public void setup () throws Exception {
192- MockitoAnnotations .initMocks (this );
193- storageCmd = PowerMockito .mock (CopyCommand .class );
191+ closeable = MockitoAnnotations .openMocks (this );
192+ storageCmd = Mockito .mock (CopyCommand .class );
194193 doReturn (context ).when (_resource ).getServiceContext (null );
195194 when (cmd .getVirtualMachine ()).thenReturn (vmSpec );
196195
@@ -218,19 +217,17 @@ public void setup() throws Exception {
218217 when (hostCapability .isNestedHVSupported ()).thenReturn (true );
219218 }
220219
220+ @ After
221+ public void tearDown () throws Exception {
222+ closeable .close ();
223+ }
224+
221225 //Test successful scaling up the vm
222226 @ Test
223227 public void testScaleVMF1 () throws Exception {
224228 when (_resource .getHyperHost (context , null )).thenReturn (hyperHost );
225229 doReturn ("i-2-3-VM" ).when (cmd ).getVmName ();
226230 when (hyperHost .findVmOnHyperHost ("i-2-3-VM" )).thenReturn (vmMo );
227- doReturn (536870912L ).when (vmSpec ).getMinRam ();
228- doReturn (1 ).when (vmSpec ).getCpus ();
229- doReturn (1000 ).when (vmSpec ).getMinSpeed ();
230- doReturn (1000 ).when (vmSpec ).getMaxSpeed ();
231- doReturn (536870912L ).when (vmSpec ).getMaxRam ();
232- doReturn (false ).when (vmSpec ).getLimitCpuUse ();
233- when (vmMo .configureVm (vmConfigSpec )).thenReturn (true );
234231
235232 _resource .execute (cmd );
236233 verify (_resource ).execute (cmd );
@@ -251,7 +248,7 @@ public void testGenerateMacSequence() {
251248 final NicTO [] nics = new NicTO [] {nicTo1 , nicTo2 };
252249
253250 String macSequence = _resource .generateMacSequence (nics );
254- assertEquals (macSequence , "02:00:65:b5:00:03|01:23:45:67:89:AB" );
251+ assertEquals ("02:00:65:b5:00:03|01:23:45:67:89:AB" , macSequence );
255252 }
256253
257254 @ Test
@@ -404,8 +401,8 @@ public void testExamineStorageSubSystemCommandFullCloneFlagForVmwareNull(){
404401 @ Test
405402 public void checkStorageProcessorAndHandlerNfsVersionAttributeVersionNotSet (){
406403 _resource .checkStorageProcessorAndHandlerNfsVersionAttribute (storageCmd );
407- verify (_resource ).examineStorageSubSystemCommandNfsVersion (Matchers . eq (storageCmd ), any (EnumMap .class ));
408- verify (_resource ).examineStorageSubSystemCommandFullCloneFlagForVmware (Matchers . eq (storageCmd ), any (EnumMap .class ));
404+ verify (_resource ).examineStorageSubSystemCommandNfsVersion (eq (storageCmd ), any (EnumMap .class ));
405+ verify (_resource ).examineStorageSubSystemCommandFullCloneFlagForVmware (eq (storageCmd ), any (EnumMap .class ));
409406 verify (_resource ).reconfigureProcessorByHandler (any (EnumMap .class ));
410407 assertEquals (NFS_VERSION , _resource .storageNfsVersion );
411408 }
@@ -415,24 +412,26 @@ public void checkStorageProcessorAndHandlerNfsVersionAttributeVersionNotSet(){
415412 public void checkStorageProcessorAndHandlerNfsVersionAttributeVersionSet (){
416413 _resource .storageNfsVersion = NFS_VERSION ;
417414 _resource .checkStorageProcessorAndHandlerNfsVersionAttribute (storageCmd );
418- verify (_resource , never ()).examineStorageSubSystemCommandNfsVersion (Matchers . eq (storageCmd ), any (EnumMap .class ));
415+ verify (_resource , never ()).examineStorageSubSystemCommandNfsVersion (eq (storageCmd ), any (EnumMap .class ));
419416 }
420417
421- @ Test (expected = CloudRuntimeException .class )
418+ @ Test (expected =CloudRuntimeException .class )
422419 public void testFindVmOnDatacenterNullHyperHostReference () throws Exception {
423- when (hyperHost .getMor ()).thenReturn (null );
424- _resource .findVmOnDatacenter (context , hyperHost , volume );
420+ try (MockedConstruction <DatacenterMO > ignored = Mockito .mockConstruction (DatacenterMO .class )) {
421+ _resource .findVmOnDatacenter (context , hyperHost , volume );
422+ }
425423 }
426424
427425 @ Test
428- @ PrepareForTest ({DatacenterMO .class , VmwareResource .class })
429426 public void testFindVmOnDatacenter () throws Exception {
430427 when (hyperHost .getHyperHostDatacenter ()).thenReturn (mor );
431- when (datacenter .getMor ()).thenReturn (mor );
432- when (datacenter .findVm (VOLUME_PATH )).thenReturn (vmMo );
433- whenNew (DatacenterMO .class ).withArguments (context , mor ).thenReturn (datacenter );
434- VirtualMachineMO result = _resource .findVmOnDatacenter (context , hyperHost , volume );
435- assertEquals (vmMo , result );
428+ try (MockedConstruction <DatacenterMO > ignored = Mockito .mockConstruction (DatacenterMO .class , (mock , context ) -> {
429+ when (mock .findVm (VOLUME_PATH )).thenReturn (vmMo );
430+ when (mock .getMor ()).thenReturn (mor );
431+ })) {
432+ VirtualMachineMO result = _resource .findVmOnDatacenter (context , hyperHost , volume );
433+ assertEquals (vmMo , result );
434+ }
436435 }
437436
438437 @ Test
0 commit comments