@@ -267,6 +267,9 @@ public class UserVmManagerImplTest {
267267 @ Mock
268268 ServiceOfferingJoinDao serviceOfferingJoinDao ;
269269
270+ @ Mock
271+ private VMInstanceVO vmInstanceMock ;
272+
270273 private static final long vmId = 1l ;
271274 private static final long zoneId = 2L ;
272275 private static final long accountId = 3L ;
@@ -277,6 +280,8 @@ public class UserVmManagerImplTest {
277280
278281 private Map <String , String > customParameters = new HashMap <>();
279282
283+ String [] detailsConstants = {VmDetailConstants .MEMORY , VmDetailConstants .CPU_NUMBER , VmDetailConstants .CPU_SPEED };
284+
280285 private DiskOfferingVO smallerDisdkOffering = prepareDiskOffering (5l * GiB_TO_BYTES , 1l , 1L , 2L );
281286 private DiskOfferingVO largerDisdkOffering = prepareDiskOffering (10l * GiB_TO_BYTES , 2l , 10L , 20L );
282287
@@ -293,6 +298,10 @@ public void beforeTest() {
293298 CallContext .register (callerUser , callerAccount );
294299
295300 customParameters .put (VmDetailConstants .ROOT_DISK_SIZE , "123" );
301+ customParameters .put (VmDetailConstants .MEMORY , "2048" );
302+ customParameters .put (VmDetailConstants .CPU_NUMBER , "4" );
303+ customParameters .put (VmDetailConstants .CPU_SPEED , "1000" );
304+
296305 lenient ().doNothing ().when (resourceLimitMgr ).incrementResourceCount (anyLong (), any (Resource .ResourceType .class ));
297306 lenient ().doNothing ().when (resourceLimitMgr ).decrementResourceCount (anyLong (), any (Resource .ResourceType .class ), anyLong ());
298307
@@ -1443,4 +1452,71 @@ public void testRestoreVirtualMachineWithVMSnapshots() throws ResourceUnavailabl
14431452
14441453 userVmManagerImpl .restoreVirtualMachine (accountMock , vmId , newTemplateId );
14451454 }
1455+
1456+ @ Test
1457+ public void updateInstanceDetailsKeepCurrentValueIfNullTestDetailsConstantIsNotNullDoNothing () {
1458+ int currentValue = 123 ;
1459+
1460+ for (String detailsConstant : detailsConstants ) {
1461+ userVmManagerImpl .updateInstanceDetailsKeepCurrentValueIfNull (null , customParameters , detailsConstant , currentValue );
1462+ }
1463+
1464+ Assert .assertEquals (customParameters .get (VmDetailConstants .MEMORY ), "2048" );
1465+ Assert .assertEquals (customParameters .get (VmDetailConstants .CPU_NUMBER ), "4" );
1466+ Assert .assertEquals (customParameters .get (VmDetailConstants .CPU_SPEED ), "1000" );
1467+ }
1468+
1469+ @ Test
1470+ public void updateInstanceDetailsKeepCurrentValueIfNullTestNewValueIsNotNullDoNothing () {
1471+ Map <String , String > details = new HashMap <>();
1472+ int currentValue = 123 ;
1473+
1474+ for (String detailsConstant : detailsConstants ) {
1475+ userVmManagerImpl .updateInstanceDetailsKeepCurrentValueIfNull (321 , details , detailsConstant , currentValue );
1476+ }
1477+
1478+ Assert .assertNull (details .get (VmDetailConstants .MEMORY ));
1479+ Assert .assertNull (details .get (VmDetailConstants .CPU_NUMBER ));
1480+ Assert .assertNull (details .get (VmDetailConstants .CPU_SPEED ));
1481+ }
1482+
1483+ @ Test
1484+ public void updateInstanceDetailsKeepCurrentValueIfNullTestBothValuesAreNullKeepCurrentValue () {
1485+ Map <String , String > details = new HashMap <>();
1486+ int currentValue = 123 ;
1487+
1488+ for (String detailsConstant : detailsConstants ) {
1489+ userVmManagerImpl .updateInstanceDetailsKeepCurrentValueIfNull (null , details , detailsConstant , currentValue );
1490+ }
1491+
1492+ Assert .assertEquals (details .get (VmDetailConstants .MEMORY ), String .valueOf (currentValue ));
1493+ Assert .assertEquals (details .get (VmDetailConstants .CPU_NUMBER ), String .valueOf (currentValue ));
1494+ Assert .assertEquals (details .get (VmDetailConstants .CPU_SPEED ),String .valueOf (currentValue ));
1495+ }
1496+
1497+ @ Test
1498+ public void updateInstanceDetailsKeepCurrentValueIfNullTestNeitherValueIsNullDoNothing () {
1499+ int currentValue = 123 ;
1500+
1501+ for (String detailsConstant : detailsConstants ) {
1502+ userVmManagerImpl .updateInstanceDetailsKeepCurrentValueIfNull (321 , customParameters , detailsConstant , currentValue );
1503+ }
1504+
1505+ Assert .assertEquals (customParameters .get (VmDetailConstants .MEMORY ), "2048" );
1506+ Assert .assertEquals (customParameters .get (VmDetailConstants .CPU_NUMBER ), "4" );
1507+ Assert .assertEquals (customParameters .get (VmDetailConstants .CPU_SPEED ),"1000" );
1508+ }
1509+
1510+ @ Test
1511+ public void updateInstanceDetailsTestAllConstantsAreUpdated () {
1512+ Mockito .doReturn (serviceOffering ).when (_serviceOfferingDao ).findById (Mockito .anyLong ());
1513+ Mockito .doReturn (1L ).when (vmInstanceMock ).getId ();
1514+ Mockito .doReturn (1L ).when (vmInstanceMock ).getServiceOfferingId ();
1515+ Mockito .doReturn (serviceOffering ).when (_serviceOfferingDao ).findByIdIncludingRemoved (Mockito .anyLong (), Mockito .anyLong ());
1516+ userVmManagerImpl .updateInstanceDetails (null , vmInstanceMock , 0l );
1517+
1518+ Mockito .verify (userVmManagerImpl ).updateInstanceDetailsKeepCurrentValueIfNull (Mockito .any (), Mockito .any (), Mockito .eq (VmDetailConstants .CPU_SPEED ), Mockito .any ());
1519+ Mockito .verify (userVmManagerImpl ).updateInstanceDetailsKeepCurrentValueIfNull (Mockito .any (), Mockito .any (), Mockito .eq (VmDetailConstants .MEMORY ), Mockito .any ());
1520+ Mockito .verify (userVmManagerImpl ).updateInstanceDetailsKeepCurrentValueIfNull (Mockito .any (), Mockito .any (), Mockito .eq (VmDetailConstants .CPU_NUMBER ), Mockito .any ());
1521+ }
14461522}
0 commit comments