@@ -245,6 +245,9 @@ public class UserVmManagerImplTest {
245245 @ Mock
246246 ServiceOfferingJoinDao serviceOfferingJoinDao ;
247247
248+ @ Mock
249+ private VMInstanceVO vmInstanceMock ;
250+
248251 private static final long vmId = 1l ;
249252 private static final long zoneId = 2L ;
250253 private static final long accountId = 3L ;
@@ -255,6 +258,8 @@ public class UserVmManagerImplTest {
255258
256259 private Map <String , String > customParameters = new HashMap <>();
257260
261+ String [] detailsConstants = {VmDetailConstants .MEMORY , VmDetailConstants .CPU_NUMBER , VmDetailConstants .CPU_SPEED };
262+
258263 private DiskOfferingVO smallerDisdkOffering = prepareDiskOffering (5l * GiB_TO_BYTES , 1l , 1L , 2L );
259264 private DiskOfferingVO largerDisdkOffering = prepareDiskOffering (10l * GiB_TO_BYTES , 2l , 10L , 20L );
260265
@@ -271,6 +276,10 @@ public void beforeTest() {
271276 CallContext .register (callerUser , callerAccount );
272277
273278 customParameters .put (VmDetailConstants .ROOT_DISK_SIZE , "123" );
279+ customParameters .put (VmDetailConstants .MEMORY , "2048" );
280+ customParameters .put (VmDetailConstants .CPU_NUMBER , "4" );
281+ customParameters .put (VmDetailConstants .CPU_SPEED , "1000" );
282+
274283 lenient ().doNothing ().when (resourceLimitMgr ).incrementResourceCount (anyLong (), any (Resource .ResourceType .class ));
275284 lenient ().doNothing ().when (resourceLimitMgr ).decrementResourceCount (anyLong (), any (Resource .ResourceType .class ), anyLong ());
276285
@@ -1221,4 +1230,71 @@ public void testSetVmRequiredFieldsForImportFromLastHost() {
12211230 Mockito .verify (userVmVoMock ).setLastHostId (2L );
12221231 Mockito .verify (userVmVoMock ).setState (VirtualMachine .State .Running );
12231232 }
1233+
1234+ @ Test
1235+ public void updateInstanceDetailsWithCurrentValueTestDetailsConstantIsNotNullDoNothing () {
1236+ int currentValue = 123 ;
1237+
1238+ for (String detailsConstant : detailsConstants ) {
1239+ userVmManagerImpl .updateInstanceDetailsWithCurrentValue (null , customParameters , detailsConstant , currentValue );
1240+ }
1241+
1242+ Assert .assertEquals (customParameters .get (VmDetailConstants .MEMORY ), "2048" );
1243+ Assert .assertEquals (customParameters .get (VmDetailConstants .CPU_NUMBER ), "4" );
1244+ Assert .assertEquals (customParameters .get (VmDetailConstants .CPU_SPEED ), "1000" );
1245+ }
1246+
1247+ @ Test
1248+ public void updateInstanceDetailsWithCurrentValueTestNewValueIsNotNullDoNothing () {
1249+ Map <String , String > details = new HashMap <>();
1250+ int currentValue = 123 ;
1251+
1252+ for (String detailsConstant : detailsConstants ) {
1253+ userVmManagerImpl .updateInstanceDetailsWithCurrentValue (321 , details , detailsConstant , currentValue );
1254+ }
1255+
1256+ Assert .assertNull (details .get (VmDetailConstants .MEMORY ));
1257+ Assert .assertNull (details .get (VmDetailConstants .CPU_NUMBER ));
1258+ Assert .assertNull (details .get (VmDetailConstants .CPU_SPEED ));
1259+ }
1260+
1261+ @ Test
1262+ public void updateInstanceDetailsWithCurrentValueTestBothValuesAreNullKeepCurrentValue () {
1263+ Map <String , String > details = new HashMap <>();
1264+ int currentValue = 123 ;
1265+
1266+ for (String detailsConstant : detailsConstants ) {
1267+ userVmManagerImpl .updateInstanceDetailsWithCurrentValue (null , details , detailsConstant , currentValue );
1268+ }
1269+
1270+ Assert .assertEquals (details .get (VmDetailConstants .MEMORY ), String .valueOf (currentValue ));
1271+ Assert .assertEquals (details .get (VmDetailConstants .CPU_NUMBER ), String .valueOf (currentValue ));
1272+ Assert .assertEquals (details .get (VmDetailConstants .CPU_SPEED ),String .valueOf (currentValue ));
1273+ }
1274+
1275+ @ Test
1276+ public void updateInstanceDetailsWithCurrentValueTestNeitherValueIsNullDoNothing () {
1277+ int currentValue = 123 ;
1278+
1279+ for (String detailsConstant : detailsConstants ) {
1280+ userVmManagerImpl .updateInstanceDetailsWithCurrentValue (321 , customParameters , detailsConstant , currentValue );
1281+ }
1282+
1283+ Assert .assertEquals (customParameters .get (VmDetailConstants .MEMORY ), "2048" );
1284+ Assert .assertEquals (customParameters .get (VmDetailConstants .CPU_NUMBER ), "4" );
1285+ Assert .assertEquals (customParameters .get (VmDetailConstants .CPU_SPEED ),"1000" );
1286+ }
1287+
1288+ @ Test
1289+ public void updateInstanceDetailsTestAllConstantsAreUpdated () {
1290+ Mockito .doReturn (serviceOffering ).when (_serviceOfferingDao ).findById (Mockito .anyLong ());
1291+ Mockito .doReturn (1L ).when (vmInstanceMock ).getId ();
1292+ Mockito .doReturn (1L ).when (vmInstanceMock ).getServiceOfferingId ();
1293+ Mockito .doReturn (serviceOffering ).when (_serviceOfferingDao ).findByIdIncludingRemoved (Mockito .anyLong (), Mockito .anyLong ());
1294+ userVmManagerImpl .updateInstanceDetails (null , vmInstanceMock , 0l );
1295+
1296+ Mockito .verify (userVmManagerImpl ).updateInstanceDetailsWithCurrentValue (Mockito .any (), Mockito .any (), Mockito .eq (VmDetailConstants .CPU_SPEED ), Mockito .any ());
1297+ Mockito .verify (userVmManagerImpl ).updateInstanceDetailsWithCurrentValue (Mockito .any (), Mockito .any (), Mockito .eq (VmDetailConstants .MEMORY ), Mockito .any ());
1298+ Mockito .verify (userVmManagerImpl ).updateInstanceDetailsWithCurrentValue (Mockito .any (), Mockito .any (), Mockito .eq (VmDetailConstants .CPU_NUMBER ), Mockito .any ());
1299+ }
12241300}
0 commit comments