From fa2c7f888495ed0969ce9178b9f770ac088a5f07 Mon Sep 17 00:00:00 2001 From: talig Date: Sun, 27 May 2018 14:47:32 +0300 Subject: Refactor candidate heat dao As a result OrchestrationTemplateCandidate element will be added if not exists instead of failing. Change-Id: I1a1c9afed65018ed01be65ff07dabe1c2081bea7 Issue-ID: SDC-1371 Signed-off-by: talig --- .../OrchestrationTemplateCandidateManager.java | 2 +- .../OrchestrationTemplateCandidateManagerImpl.java | 32 +++-------- .../impl/VendorSoftwareProductManagerImpl.java | 40 +++++++------ ...hestrationTemplateCandidateManagerImplTest.java | 8 +-- .../impl/VendorSoftwareProductManagerImplTest.java | 65 +++++++++++----------- 5 files changed, 64 insertions(+), 83 deletions(-) (limited to 'openecomp-be/backend') diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/OrchestrationTemplateCandidateManager.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/OrchestrationTemplateCandidateManager.java index a28e2c8144..238dbc08d0 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/OrchestrationTemplateCandidateManager.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/OrchestrationTemplateCandidateManager.java @@ -41,7 +41,7 @@ public interface OrchestrationTemplateCandidateManager { Optional> get(String vspId, Version version) throws IOException; - OrchestrationTemplateCandidateData getInfo(String vspId, Version version); + Optional getInfo(String vspId, Version version); void abort(String vspId, Version version); } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/OrchestrationTemplateCandidateManagerImpl.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/OrchestrationTemplateCandidateManagerImpl.java index e5b953ffda..57f6b672be 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/OrchestrationTemplateCandidateManagerImpl.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/OrchestrationTemplateCandidateManagerImpl.java @@ -45,7 +45,6 @@ import java.io.InputStream; import java.util.Collections; import java.util.List; import java.util.Map; -import java.util.Objects; import java.util.Optional; public class OrchestrationTemplateCandidateManagerImpl @@ -78,9 +77,10 @@ public class OrchestrationTemplateCandidateManagerImpl @Override public OrchestrationTemplateActionResponse process(String vspId, Version version) { - OrchestrationTemplateCandidateData candidate = fetchCandidateDataEntity(vspId, version) - .orElseThrow( - () -> new CoreException(new OrchestrationTemplateNotFoundErrorBuilder(vspId).build())); + OrchestrationTemplateCandidateData candidate = + candidateService.getOrchestrationTemplateCandidate(vspId, version) + .orElseThrow(() -> new CoreException( + new OrchestrationTemplateNotFoundErrorBuilder(vspId).build())); return OrchestrationProcessFactory.getInstance(candidate.getFileSuffix()) .map(processor -> processor.process(getVspDetails(vspId, version), candidate)) @@ -117,27 +117,22 @@ public class OrchestrationTemplateCandidateManagerImpl VspDetails vspDetails = getVspDetails(vspId, version); Optional candidateDataEntity = - fetchCandidateDataEntity(vspId, version); + candidateService.getOrchestrationTemplateCandidate(vspId, version); if (!candidateDataEntity.isPresent()) { return Optional.empty(); } - if(Objects.isNull(candidateDataEntity.get().getFileSuffix())) { - return Optional.empty(); - } - - OnboardingTypesEnum type = - OnboardingTypesEnum.getOnboardingTypesEnum(candidateDataEntity.get().getFileSuffix()); - if (CommonUtil.isFileOriginFromZip(candidateDataEntity.get().getFileSuffix())) { FilesDataStructure structure = JsonUtil .json2Object(candidateDataEntity.get().getFilesDataStructure(), FilesDataStructure.class); String manifest = candidateService.createManifest(vspDetails, structure); + OnboardingTypesEnum type = + OnboardingTypesEnum.getOnboardingTypesEnum(candidateDataEntity.get().getFileSuffix()); return Optional.of( new ImmutablePair<>(OnboardingTypesEnum.ZIP.toString(), candidateService .replaceManifestInZip(candidateDataEntity.get().getContentData(), - manifest, vspId, type))); + manifest, type))); } return Optional.of( @@ -146,7 +141,7 @@ public class OrchestrationTemplateCandidateManagerImpl } @Override - public OrchestrationTemplateCandidateData getInfo(String vspId, Version version) { + public Optional getInfo(String vspId, Version version) { return candidateService.getOrchestrationTemplateCandidateInfo(vspId, version); } @@ -155,16 +150,7 @@ public class OrchestrationTemplateCandidateManagerImpl candidateService.deleteOrchestrationTemplateCandidate(vspId, version); } - private Optional fetchCandidateDataEntity( - String vspId, Version version) { - return Optional - .ofNullable(candidateService.getOrchestrationTemplateCandidate(vspId, version)); - } - private VspDetails getVspDetails(String vspId, Version version) { - return vspInfoDao.get(new VspDetails(vspId, version)); } - - } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/VendorSoftwareProductManagerImpl.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/VendorSoftwareProductManagerImpl.java index f116abef16..5874394e96 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/VendorSoftwareProductManagerImpl.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/VendorSoftwareProductManagerImpl.java @@ -68,7 +68,6 @@ import org.openecomp.sdc.vendorsoftwareproduct.dao.type.DeploymentFlavorEntity; import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ImageEntity; import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity; import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OnboardingMethod; -import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData; import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateEntity; import org.openecomp.sdc.vendorsoftwareproduct.dao.type.PackageInfo; import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails; @@ -127,6 +126,10 @@ import java.util.Set; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; +import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductInvalidErrorBuilder.candidateDataNotProcessedOrAbortedErrorBuilder; +import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductInvalidErrorBuilder.invalidProcessedCandidate; +import static org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductInvalidErrorBuilder.vspMissingDeploymentFlavorErrorBuilder; + public class VendorSoftwareProductManagerImpl implements VendorSoftwareProductManager { private final VspMergeDao vspMergeDao; @@ -258,19 +261,14 @@ public class VendorSoftwareProductManagerImpl implements VendorSoftwareProductMa private void validateOrchestrationTemplateCandidate(ValidationResponse validationResponse, List vspErrors, String vspId, Version version) { - OrchestrationTemplateCandidateData orchestrationTemplateCandidateData = - orchestrationTemplateCandidateManager.getInfo(vspId, version); - String validationData = orchestrationTemplateCandidateData.getValidationData(); - String fileName = orchestrationTemplateCandidateData.getFileName(); - if (Objects.nonNull(orchestrationTemplateCandidateData.getFileSuffix())) { - if (validationData.isEmpty()) { - vspErrors.add(VendorSoftwareProductInvalidErrorBuilder - .candidateDataNotProcessedOrAbortedErrorBuilder(fileName)); - } else { - vspErrors.add(VendorSoftwareProductInvalidErrorBuilder.invalidProcessedCandidate(fileName)); - } - validationResponse.setVspErrors(vspErrors); - } + orchestrationTemplateCandidateManager.getInfo(vspId, version) + .ifPresent(candidateInfo -> { + String fileName = candidateInfo.getFileName(); + vspErrors.add(candidateInfo.getValidationData().isEmpty() + ? candidateDataNotProcessedOrAbortedErrorBuilder(fileName) + : invalidProcessedCandidate(fileName)); + validationResponse.setVspErrors(vspErrors); + }); } private void validateManualOnboardingMethod(VspDetails vspDetails, @@ -281,8 +279,7 @@ public class VendorSoftwareProductManagerImpl implements VendorSoftwareProductMa Collection deploymentFlavors = deploymentFlavorDao .list(new DeploymentFlavorEntity(vspDetails.getId(), vspDetails.getVersion(), null)); if (CollectionUtils.isEmpty(deploymentFlavors)) { - vspErrors - .add(VendorSoftwareProductInvalidErrorBuilder.vspMissingDeploymentFlavorErrorBuilder()); + vspErrors.add(vspMissingDeploymentFlavorErrorBuilder()); } vspErrors.addAll(validateDeploymentFlavors(deploymentFlavors)); @@ -675,7 +672,7 @@ public class VendorSoftwareProductManagerImpl implements VendorSoftwareProductMa return packageInfo; } - protected void populateVersionsForVlm(String vlmId, Version vlmVersion) { + void populateVersionsForVlm(String vlmId, Version vlmVersion) { VersioningManager versioningManager = VersioningManagerFactory.getInstance().createInterface(); versioningManager.list(vlmId).stream() .filter(version -> version.getId().equalsIgnoreCase(vlmVersion.getId())) @@ -852,11 +849,12 @@ public class VendorSoftwareProductManagerImpl implements VendorSoftwareProductMa return computeDao.listByVsp(vspId, version); } - private boolean isOrchestrationTemplateMissing(OrchestrationTemplateEntity orchestrationTemplate) { + private boolean isOrchestrationTemplateMissing( + OrchestrationTemplateEntity orchestrationTemplate) { return orchestrationTemplate == null - || orchestrationTemplate.getContentData() == null - || orchestrationTemplate.getFileSuffix() == null - || orchestrationTemplate.getFileName() == null; + || orchestrationTemplate.getContentData() == null + || orchestrationTemplate.getFileSuffix() == null + || orchestrationTemplate.getFileName() == null; } private boolean isServiceModelMissing(ToscaServiceModel serviceModel) { diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/OrchestrationTemplateCandidateManagerImplTest.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/OrchestrationTemplateCandidateManagerImplTest.java index a86b1ef0c5..2c97937eab 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/OrchestrationTemplateCandidateManagerImplTest.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/OrchestrationTemplateCandidateManagerImplTest.java @@ -95,7 +95,7 @@ public class OrchestrationTemplateCandidateManagerImplTest { private OrchestrationUtil orchestrationUtil; @BeforeClass - public void setUp() throws Exception { + public void setUp() { MockitoAnnotations.initMocks(this); } @@ -129,7 +129,7 @@ public class OrchestrationTemplateCandidateManagerImplTest { " }\n" + " ]\n" + "}"); - doReturn(orchTemplate) + doReturn(Optional.of(orchTemplate)) .when(candidateServiceMock).getOrchestrationTemplateCandidate(any(), any()); doReturn(new VspDetails(VSP_ID, VERSION01)) @@ -260,7 +260,7 @@ public class OrchestrationTemplateCandidateManagerImplTest { @Test public void testGetFileDataStructure() { - Optional jsonFileDataStructure = Optional.of(new String("{\n" + + Optional jsonFileDataStructure = Optional.of("{\n" + " \"modules\": [\n" + " {\n" + " \"yaml\": \"hot-mog-0108-bs1271.yml\",\n" + @@ -270,7 +270,7 @@ public class OrchestrationTemplateCandidateManagerImplTest { " \"unassigned\": [],\n" + " \"artifacts\": [],\n" + " \"nested\": []\n" + - "}")); + "}"); Optional filesDataStructureOptional = Optional.of(JsonUtil.json2Object (jsonFileDataStructure.get(), FilesDataStructure.class)); doReturn(filesDataStructureOptional).when(candidateServiceMock) diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/VendorSoftwareProductManagerImplTest.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/VendorSoftwareProductManagerImplTest.java index f5c7fb29e8..9149fcfb3f 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/VendorSoftwareProductManagerImplTest.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/impl/VendorSoftwareProductManagerImplTest.java @@ -16,14 +16,6 @@ package org.openecomp.sdc.vendorsoftwareproduct.impl; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyObject; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; - import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.InjectMocks; @@ -67,7 +59,6 @@ import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse; import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse; import org.openecomp.sdc.vendorsoftwareproduct.types.composition.DeploymentFlavor; import org.openecomp.sdc.versioning.ActionVersioningManager; -import org.openecomp.sdc.versioning.VersioningManager; import org.openecomp.sdc.versioning.dao.types.Version; import org.openecomp.sdc.versioning.dao.types.VersionStatus; import org.openecomp.sdc.versioning.types.VersionInfo; @@ -88,22 +79,26 @@ import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; +import java.util.Optional; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; + public class VendorSoftwareProductManagerImplTest { - private static final String INVALID_VERSION_MSG = "Invalid requested version."; - private static String VSP_ID = "vspId"; - private static String VERSION_ID = "versionId"; - public static final Version VERSION01 = new Version(0, 1); - private static final Version VERSION10 = new Version(1, 0); + private static final String VSP_ID = "vspId"; + private static final Version VERSION01 = new Version("0, 1"); + private static final Version VERSION10 = new Version("1, 0"); private static final String USER1 = "vspTestUser1"; private static final String USER2 = "vspTestUser2"; - private static final String USER3 = "vspTestUser3"; - private static String id006 = null; - private static String id007 = null; @Mock private ActionVersioningManager versioningManagerMock; @@ -148,12 +143,12 @@ public class VendorSoftwareProductManagerImplTest { private ArgumentCaptor activityLogEntityArg; @BeforeMethod - public void setUp() throws Exception { + public void setUp() { MockitoAnnotations.initMocks(this); } @AfterMethod - public void tearDown(){ + public void tearDown() { vendorSoftwareProductManager = null; } @@ -232,7 +227,7 @@ public class VendorSoftwareProductManagerImplTest { doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo( VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE, VSP_ID, USER1, VersionableEntityAction.Write); - List fgs = new ArrayList(); + List fgs = new ArrayList<>(); fgs.add("fg1"); fgs.add("fg2"); VspDetails existingVsp = @@ -240,7 +235,7 @@ public class VendorSoftwareProductManagerImplTest { "category", "subCategory", "456", fgs); - List updFgs = new ArrayList(); + List updFgs = new ArrayList<>(); //updFgs.add("fg2"); VspDetails updatedVsp = createVspDetails(VSP_ID, VERSION01, "VSP1_updated", null, "vendorName", "vlm1Id", "icon", @@ -256,7 +251,7 @@ public class VendorSoftwareProductManagerImplTest { flavor.setFeatureGroupId("fg1"); dfEntity.setDeploymentFlavorCompositionData(flavor); - List dfList = new ArrayList(); + List dfList = new ArrayList<>(); dfList.add(dfEntity); doReturn(dfList).when(deploymentFlavorDaoMock).list(anyObject()); @@ -271,7 +266,7 @@ public class VendorSoftwareProductManagerImplTest { @Test(expectedExceptions = CoreException.class) public void testGetNonExistingVersion_negative() { - Version notExistversion = new Version(43, 8); + Version notExistversion = new Version("43, 8"); doReturn(null).when(vspInfoDaoMock).get(any(VspDetails.class)); vendorSoftwareProductManager.getVsp(VSP_ID, notExistversion); } @@ -301,8 +296,8 @@ public class VendorSoftwareProductManagerImplTest { @Test public void testGetOldVersion() { VersionInfo versionInfo = new VersionInfo(); - versionInfo.setActiveVersion(new Version(0, 2)); - versionInfo.setViewableVersions(Arrays.asList(VERSION01, new Version(0, 2))); + versionInfo.setActiveVersion(new Version("0, 2")); + versionInfo.setViewableVersions(Arrays.asList(VERSION01, new Version("0, 2"))); versionInfo.setStatus(VersionStatus.Locked); versionInfo.setLockingUser(USER2); doReturn(versionInfo).when(versioningManagerMock).getEntityVersionInfo( @@ -440,7 +435,7 @@ public class VendorSoftwareProductManagerImplTest { } @Test - public void testCreatePackage() throws IOException { + public void testCreatePackage() { /*VspDetails vspDetailsMock = new VspDetails("vspId", new Version(1, 0)); doReturn(vspDetailsMock).when(vspInfoDaoMock).get(anyObject());*/ VersionInfo versionInfo = new VersionInfo(); @@ -472,7 +467,7 @@ public class VendorSoftwareProductManagerImplTest { try (InputStream zis = getFileInputStream("/vspmanager/zips/missingYml.zip")) { UploadFileResponse uploadFileResponse = - candidateManager.upload(VSP_ID, VERSION01, zis, "zip", "file"); + candidateManager.upload(VSP_ID, VERSION01, zis, "zip", "file"); Assert.assertEquals(uploadFileResponse.getErrors().size(), 0); } @@ -480,7 +475,7 @@ public class VendorSoftwareProductManagerImplTest { // TODO: 3/15/2017 fix and enable //@Test(dependsOnMethods = {"testUploadFileMissingFile"}) - public void testUploadNotZipFile() throws IOException { + public void testUploadNotZipFile() { URL url = this.getClass().getResource("/notZipFile"); try { @@ -596,7 +591,8 @@ public class VendorSoftwareProductManagerImplTest { orchestrationTemplateCandidateData.setFileSuffix("zip"); orchestrationTemplateCandidateData.setFilesDataStructure("testdata"); orchestrationTemplateCandidateData.setValidationData(""); - doReturn(orchestrationTemplateCandidateData).when(orchestrationTemplateCandidateManagerMock) + doReturn(Optional.of(orchestrationTemplateCandidateData)) + .when(orchestrationTemplateCandidateManagerMock) .getInfo(VSP_ID, VERSION01); ValidationResponse validationResponse = vendorSoftwareProductManager.validate(VSP_ID, VERSION01); @@ -619,7 +615,8 @@ public class VendorSoftwareProductManagerImplTest { OrchestrationTemplateCandidateData(); orchestrationTemplateCandidateData.setFileSuffix("zip"); orchestrationTemplateCandidateData.setValidationData("Invalid processed data"); - doReturn(orchestrationTemplateCandidateData).when(orchestrationTemplateCandidateManagerMock) + doReturn(Optional.of(orchestrationTemplateCandidateData)) + .when(orchestrationTemplateCandidateManagerMock) .getInfo(VSP_ID, VERSION01); ValidationResponse validationResponse = vendorSoftwareProductManager.validate(VSP_ID, VERSION01); @@ -639,7 +636,7 @@ public class VendorSoftwareProductManagerImplTest { } - public InputStream getFileInputStream(String fileName) { + private InputStream getFileInputStream(String fileName) { URL url = this.getClass().getResource(fileName); try { return url.openStream(); @@ -649,7 +646,7 @@ public class VendorSoftwareProductManagerImplTest { } } - static VspDetails createVspDetails(String id, Version version, String name, String desc, + private static VspDetails createVspDetails(String id, Version version, String name, String desc, String vendorName, String vlm, String icon, String category, String subCategory, String licenseAgreement, List featureGroups) { @@ -661,14 +658,14 @@ public class VendorSoftwareProductManagerImplTest { vspDetails.setSubCategory(subCategory); vspDetails.setVendorName(vendorName); vspDetails.setVendorId(vlm); - vspDetails.setVlmVersion(new Version(1, 0)); + vspDetails.setVlmVersion(new Version("1, 0")); vspDetails.setLicenseAgreement(licenseAgreement); vspDetails.setFeatureGroups(featureGroups); vspDetails.setOnboardingMethod("HEAT"); return vspDetails; } - static void assertVspsEquals(VspDetails actual, VspDetails expected) { + private static void assertVspsEquals(VspDetails actual, VspDetails expected) { Assert.assertEquals(actual.getId(), expected.getId()); Assert.assertEquals(actual.getVersion(), expected.getVersion()); Assert.assertEquals(actual.getName(), expected.getName()); -- cgit 1.2.3-korg