diff --git a/server/src/main/java/com/cloud/template/HypervisorTemplateAdapter.java b/server/src/main/java/com/cloud/template/HypervisorTemplateAdapter.java index 56d7382e84c..365f0202c87 100644 --- a/server/src/main/java/com/cloud/template/HypervisorTemplateAdapter.java +++ b/server/src/main/java/com/cloud/template/HypervisorTemplateAdapter.java @@ -304,12 +304,12 @@ public class HypervisorTemplateAdapter extends TemplateAdapterBase { zonesIds = _dcDao.listAllZones().stream().map(DataCenterVO::getId).collect(Collectors.toList()); } - List imageStores = getImageStoresThrowsExceptionIfNotFound(zonesIds, profile); for (long zoneId : zonesIds) { DataStore imageStore = verifyHeuristicRulesForZone(template, zoneId); if (imageStore == null) { + List imageStores = getImageStoresThrowsExceptionIfNotFound(zoneId, profile); standardImageStoreAllocation(imageStores, template); } else { validateSecondaryStorageAndCreateTemplate(List.of(imageStore), template, null); @@ -317,8 +317,8 @@ public class HypervisorTemplateAdapter extends TemplateAdapterBase { } } - protected List getImageStoresThrowsExceptionIfNotFound(List zonesIds, TemplateProfile profile) { - List imageStores = storeMgr.getImageStoresByZoneIds(zonesIds.toArray(new Long[0])); + protected List getImageStoresThrowsExceptionIfNotFound(long zoneId, TemplateProfile profile) { + List imageStores = storeMgr.getImageStoresByZoneIds(zoneId); if (imageStores == null || imageStores.size() == 0) { throw new CloudRuntimeException(String.format("Unable to find image store to download the template [%s].", profile.getTemplate())); } @@ -424,7 +424,7 @@ public class HypervisorTemplateAdapter extends TemplateAdapterBase { List payloads = new LinkedList<>(); if (imageStore == null) { - List imageStores = getImageStoresThrowsExceptionIfNotFound(List.of(zoneId), profile); + List imageStores = getImageStoresThrowsExceptionIfNotFound(zoneId, profile); postUploadAllocation(imageStores, template, payloads); } else { postUploadAllocation(List.of(imageStore), template, payloads); diff --git a/server/src/test/java/com/cloud/template/HypervisorTemplateAdapterTest.java b/server/src/test/java/com/cloud/template/HypervisorTemplateAdapterTest.java index 6e028af895f..d90ae5a2f03 100644 --- a/server/src/test/java/com/cloud/template/HypervisorTemplateAdapterTest.java +++ b/server/src/test/java/com/cloud/template/HypervisorTemplateAdapterTest.java @@ -153,6 +153,8 @@ public class HypervisorTemplateAdapterTest { private AutoCloseable closeable; + private static final long zoneId = 1L; + @Before public void before() { closeable = MockitoAnnotations.openMocks(this); @@ -325,7 +327,6 @@ public class HypervisorTemplateAdapterTest { VMTemplateVO vmTemplateVOMock = Mockito.mock(VMTemplateVO.class); Mockito.when(templateProfileMock.getZoneIdList()).thenReturn(null); - Mockito.doReturn(null).when(_adapter).getImageStoresThrowsExceptionIfNotFound(Mockito.any(List.class), Mockito.any(TemplateProfile.class)); _adapter.createTemplateWithinZones(templateProfileMock, vmTemplateVOMock); @@ -339,7 +340,7 @@ public class HypervisorTemplateAdapterTest { List zoneIds = List.of(1L); Mockito.when(templateProfileMock.getZoneIdList()).thenReturn(zoneIds); - Mockito.doReturn(null).when(_adapter).getImageStoresThrowsExceptionIfNotFound(Mockito.any(List.class), Mockito.any(TemplateProfile.class)); + Mockito.doReturn(null).when(_adapter).getImageStoresThrowsExceptionIfNotFound(Mockito.any(Long.class), Mockito.any(TemplateProfile.class)); Mockito.doReturn(null).when(_adapter).verifyHeuristicRulesForZone(Mockito.any(VMTemplateVO.class), Mockito.anyLong()); Mockito.doNothing().when(_adapter).standardImageStoreAllocation(Mockito.isNull(), Mockito.any(VMTemplateVO.class)); @@ -352,10 +353,10 @@ public class HypervisorTemplateAdapterTest { public void createTemplateWithinZonesTestZoneDoesNotHaveActiveHeuristicRulesShouldCallStandardImageStoreAllocation() { TemplateProfile templateProfileMock = Mockito.mock(TemplateProfile.class); VMTemplateVO vmTemplateVOMock = Mockito.mock(VMTemplateVO.class); - List zoneIds = List.of(1L); + List zoneIds = List.of(zoneId); Mockito.when(templateProfileMock.getZoneIdList()).thenReturn(zoneIds); - Mockito.doReturn(null).when(_adapter).getImageStoresThrowsExceptionIfNotFound(Mockito.any(List.class), Mockito.any(TemplateProfile.class)); + Mockito.doReturn(null).when(_adapter).getImageStoresThrowsExceptionIfNotFound(Mockito.any(Long.class), Mockito.any(TemplateProfile.class)); Mockito.doReturn(null).when(_adapter).verifyHeuristicRulesForZone(Mockito.any(VMTemplateVO.class), Mockito.anyLong()); Mockito.doNothing().when(_adapter).standardImageStoreAllocation(Mockito.isNull(), Mockito.any(VMTemplateVO.class)); @@ -372,7 +373,6 @@ public class HypervisorTemplateAdapterTest { List zoneIds = List.of(1L); Mockito.when(templateProfileMock.getZoneIdList()).thenReturn(zoneIds); - Mockito.doReturn(null).when(_adapter).getImageStoresThrowsExceptionIfNotFound(Mockito.any(List.class), Mockito.any(TemplateProfile.class)); Mockito.doReturn(dataStoreMock).when(_adapter).verifyHeuristicRulesForZone(Mockito.any(VMTemplateVO.class), Mockito.anyLong()); Mockito.doNothing().when(_adapter).validateSecondaryStorageAndCreateTemplate(Mockito.any(List.class), Mockito.any(VMTemplateVO.class), Mockito.isNull()); @@ -384,34 +384,31 @@ public class HypervisorTemplateAdapterTest { @Test(expected = CloudRuntimeException.class) public void getImageStoresThrowsExceptionIfNotFoundTestNullImageStoreShouldThrowCloudRuntimeException() { TemplateProfile templateProfileMock = Mockito.mock(TemplateProfile.class); - List zoneIds = List.of(1L); Mockito.when(dataStoreManagerMock.getImageStoresByZoneIds(Mockito.anyLong())).thenReturn(null); - _adapter.getImageStoresThrowsExceptionIfNotFound(zoneIds, templateProfileMock); + _adapter.getImageStoresThrowsExceptionIfNotFound(zoneId, templateProfileMock); } @Test(expected = CloudRuntimeException.class) public void getImageStoresThrowsExceptionIfNotFoundTestEmptyImageStoreShouldThrowCloudRuntimeException() { TemplateProfile templateProfileMock = Mockito.mock(TemplateProfile.class); - List zoneIds = List.of(1L); List imageStoresList = new ArrayList<>(); Mockito.when(dataStoreManagerMock.getImageStoresByZoneIds(Mockito.anyLong())).thenReturn(imageStoresList); - _adapter.getImageStoresThrowsExceptionIfNotFound(zoneIds, templateProfileMock); + _adapter.getImageStoresThrowsExceptionIfNotFound(zoneId, templateProfileMock); } @Test public void getImageStoresThrowsExceptionIfNotFoundTestNonEmptyImageStoreShouldNotThrowCloudRuntimeException() { TemplateProfile templateProfileMock = Mockito.mock(TemplateProfile.class); - List zoneIds = List.of(1L); DataStore dataStoreMock = Mockito.mock(DataStore.class); List imageStoresList = List.of(dataStoreMock); Mockito.when(dataStoreManagerMock.getImageStoresByZoneIds(Mockito.anyLong())).thenReturn(imageStoresList); - _adapter.getImageStoresThrowsExceptionIfNotFound(zoneIds, templateProfileMock); + _adapter.getImageStoresThrowsExceptionIfNotFound(zoneId, templateProfileMock); } @Test