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