From e6834bd54f7d37c78d3c8fcab176dbe5128287f2 Mon Sep 17 00:00:00 2001 From: ojasdubey Date: Tue, 14 Nov 2017 12:55:54 +0530 Subject: Fixing update HEAT 1. Update component dependencies based on new Heat 2. Retain VSP data on Heat re-upload 3. Unit tests 4. Code reformatting and refactoring for compliance Issue ID: SDC-655 Change-Id: Ib4822d3e7ec69293d2d544044909f0a22ab224ac Signed-off-by: ojasdubey --- .../impl/orchestration/OrchestrationUtil.java | 553 +++++++++++++-------- .../OrchestrationTemplateProcessCsarHandler.java | 10 +- .../OrchestrationTemplateProcessZipHandler.java | 34 +- ...hestrationTemplateCandidateManagerImplTest.java | 315 +++++++++--- 4 files changed, 614 insertions(+), 298 deletions(-) (limited to 'openecomp-be/backend/openecomp-sdc-vendor-software-product-manager') diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationUtil.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationUtil.java index 9d7421f336..2ee7b263b9 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationUtil.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/OrchestrationUtil.java @@ -1,6 +1,10 @@ package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration; +import static org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants.GENERAL_COMPONENT_ID; +import static org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME; + import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.collections4.MapUtils; import org.openecomp.core.model.dao.ServiceModelDao; import org.openecomp.core.model.dao.ServiceModelDaoFactory; import org.openecomp.core.util.UniqueValueUtil; @@ -22,6 +26,8 @@ import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentArtifactDao; import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao; import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDaoFactory; +import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDao; +import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDaoFactory; import org.openecomp.sdc.vendorsoftwareproduct.dao.MonitoringUploadDaoFactory; import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDao; import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDaoFactory; @@ -31,6 +37,7 @@ import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDao; import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDaoFactory; import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao; import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDaoFactory; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity; import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity; import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity; import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity; @@ -52,231 +59,371 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Optional; - -import static org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants.GENERAL_COMPONENT_ID; -import static org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME; +import java.util.Set; +import java.util.stream.Collectors; public class OrchestrationUtil { - public static final String ORCHESTRATION_CONFIG_NAMESPACE = "orchestration"; - public static final String ORCHESTRATION_IMPL_KEY = "orchestration_impl"; - - - private static VendorSoftwareProductDao vendorSoftwareProductDao = - VendorSoftwareProductDaoFactory.getInstance().createInterface(); - private static NicDao nicDao = NicDaoFactory.getInstance().createInterface(); - private static ComponentArtifactDao componentArtifactDao = MonitoringUploadDaoFactory.getInstance().createInterface(); - private static ProcessDao processDao = ProcessDaoFactory.getInstance().createInterface(); - private static OrchestrationTemplateDao orchestrationTemplateDataDao = - OrchestrationTemplateDaoFactory.getInstance().createInterface(); - private static ComponentDao componentDao = - ComponentDaoFactory.getInstance().createInterface(); - private static ServiceModelDao serviceModelDao = ServiceModelDaoFactory.getInstance() - .createInterface(); - private static CompositionEntityDataManager compositionEntityDataManager = - CompositionEntityDataManagerFactory.getInstance().createInterface(); - private static CompositionDataExtractor compositionDataExtractor = - CompositionDataExtractorFactory.getInstance().createInterface(); - - public static Optional getFileContentMap(OnboardingTypesEnum type, - UploadFileResponse uploadFileResponse, - byte[] uploadedFileData) { - FileContentHandler contentMap = null; - try { - contentMap = CommonUtil.validateAndUploadFileContent(type, uploadedFileData); - } catch (IOException exception) { - uploadFileResponse.addStructureError( - SdcCommon.UPLOAD_FILE, - new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_ZIP_FILE.getErrorMessage())); - } catch (CoreException coreException) { - uploadFileResponse.addStructureError( - SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, coreException.getMessage())); - } - return Optional.ofNullable(contentMap); + public static final String ORCHESTRATION_CONFIG_NAMESPACE = "orchestration"; + public static final String ORCHESTRATION_IMPL_KEY = "orchestration_impl"; + + + private VendorSoftwareProductDao vendorSoftwareProductDao; + private NicDao nicDao; + private ComponentArtifactDao componentArtifactDao; + private ProcessDao processDao; + private OrchestrationTemplateDao orchestrationTemplateDataDao; + private ComponentDao componentDao; + private ServiceModelDao serviceModelDao; + private ComponentDependencyModelDao componentDependencyModelDao; + private CompositionEntityDataManager compositionEntityDataManager; + private CompositionDataExtractor compositionDataExtractor; + + public OrchestrationUtil() { + this (VendorSoftwareProductDaoFactory.getInstance().createInterface(), + NicDaoFactory.getInstance().createInterface(), + MonitoringUploadDaoFactory.getInstance().createInterface(), + ProcessDaoFactory.getInstance().createInterface(), + OrchestrationTemplateDaoFactory.getInstance().createInterface(), + ComponentDaoFactory.getInstance().createInterface(), + ServiceModelDaoFactory.getInstance().createInterface(), + ComponentDependencyModelDaoFactory.getInstance().createInterface(), + CompositionEntityDataManagerFactory.getInstance().createInterface(), + CompositionDataExtractorFactory.getInstance().createInterface()); + } + + public OrchestrationUtil( + VendorSoftwareProductDao vendorSoftwareProductDao, + NicDao nicDao, + ComponentArtifactDao componentArtifactDao, + ProcessDao processDao, + OrchestrationTemplateDao orchestrationTemplateDataDao, + ComponentDao componentDao, + ServiceModelDao serviceModelDao, + ComponentDependencyModelDao componentDependencyModelDao, + CompositionEntityDataManager compositionEntityDataManager, + CompositionDataExtractor compositionDataExtractor) { + this.vendorSoftwareProductDao = vendorSoftwareProductDao; + this.nicDao = nicDao; + this.componentArtifactDao = componentArtifactDao; + this.processDao = processDao; + this.orchestrationTemplateDataDao = orchestrationTemplateDataDao; + this.componentDao = componentDao; + this.serviceModelDao = serviceModelDao; + this.componentDependencyModelDao = componentDependencyModelDao; + this.compositionEntityDataManager = compositionEntityDataManager; + this.compositionDataExtractor = compositionDataExtractor; + } + + public static Optional getFileContentMap(OnboardingTypesEnum type, + UploadFileResponse + uploadFileResponse, + byte[] uploadedFileData) { + FileContentHandler contentMap = null; + try { + contentMap = CommonUtil.validateAndUploadFileContent(type, uploadedFileData); + } catch (IOException exception) { + uploadFileResponse.addStructureError( + SdcCommon.UPLOAD_FILE, + new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_ZIP_FILE.getErrorMessage())); + } catch (CoreException coreException) { + uploadFileResponse.addStructureError( + SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, coreException.getMessage())); + } + return Optional.ofNullable(contentMap); + } + + public void backupComponentsQuestionnaireBeforeDelete(String vspId, Version activeVersion, + Map componentsQustanniare, + Map> + componentNicsQustanniare, + Map> + componentMibList, + Map> + componentProcesses, + Map processArtifact) { + //backup VSP processes + backupProcess(vspId, activeVersion, GENERAL_COMPONENT_ID, GENERAL_COMPONENT_ID, + componentProcesses, processArtifact); + Collection componentsCompositionAndQuestionnaire = vendorSoftwareProductDao + .listComponentsCompositionAndQuestionnaire(vspId, + activeVersion); + componentsCompositionAndQuestionnaire.forEach(componentEntity -> + backupComponentQuestionnaire(vspId, activeVersion, componentEntity, componentsQustanniare, + componentNicsQustanniare, componentMibList, componentProcesses, processArtifact)); + } + + private void backupComponentQuestionnaire(String vspId, Version activeVersion, + ComponentEntity componentEntity, + Map componentsQustanniare, + Map> + componentNicsQustanniare, + Map> + componentMibList, + Map> + componentProcesses, + Map processArtifact) { + String componentName = componentEntity.getComponentCompositionData().getName(); + backupMibData(componentsQustanniare, componentMibList, componentName, componentEntity, vspId, + activeVersion); + backupComponentProcessData(componentNicsQustanniare, vspId, activeVersion, componentName, + componentEntity, componentProcesses, processArtifact); + } + + private void backupMibData(Map componentsQustanniare, + Map> + componentMibList, + String componentName, ComponentEntity componentEntity, + String vspId, Version activeVersion) { + componentsQustanniare.put(componentName, componentEntity.getQuestionnaireData()); + //backup mib + Collection componentMib = + componentArtifactDao.listArtifacts(new + ComponentMonitoringUploadEntity(vspId, activeVersion, componentEntity.getId(), null)); + if (CollectionUtils.isNotEmpty(componentMib)) { + componentMibList.put(componentName, componentMib); } + } - public static void backupComponentsQuestionnaireBeforeDelete(String vspId, Version activeVersion, - Map componentsQustanniare, - Map> - componentNicsQustanniare, - Map> - componentMibList, - Map> - componentProcesses, - Map processArtifact) { - //backup VSP processes - backupProcess(vspId, activeVersion, GENERAL_COMPONENT_ID, GENERAL_COMPONENT_ID, - componentProcesses, processArtifact); - Collection componentsCompositionAndQuestionnaire = vendorSoftwareProductDao - .listComponentsCompositionAndQuestionnaire(vspId, - activeVersion); - componentsCompositionAndQuestionnaire.forEach(componentEntity -> { - String componentName = componentEntity.getComponentCompositionData().getName(); - componentsQustanniare.put(componentName, componentEntity - .getQuestionnaireData()); - Collection - nics = nicDao.list(new NicEntity(vspId, activeVersion, componentEntity.getId(), null)); - //backup mib - Collection componentMib = - componentArtifactDao.listArtifacts(new - ComponentMonitoringUploadEntity(vspId, activeVersion, componentEntity.getId(), - null)); - if (CollectionUtils.isNotEmpty(componentMib)) { - componentMibList.put(componentName,componentMib); - } - - //backup component processes - backupProcess(vspId, activeVersion, componentEntity.getId(), componentName, - componentProcesses, processArtifact); - if (CollectionUtils.isNotEmpty(nics)) { - Map nicsQustanniare = new HashMap<>(); - nics.forEach(nicEntity -> { - NicEntity nic = nicDao.get(new NicEntity(vspId, activeVersion, componentEntity.getId(), - nicEntity.getId())); - NicEntity nicQuestionnaire = nicDao.getQuestionnaireData(vspId, activeVersion, - componentEntity.getId(), nicEntity.getId()); - - nicsQustanniare - .put(nicEntity.getNicCompositionData().getName(), - nicQuestionnaire.getQuestionnaireData()); - }); - componentNicsQustanniare.put(componentName, nicsQustanniare); - } - }); + private void backupComponentProcessData(Map> componentNicsQustanniare, + String vspId, Version activeVersion, String componentName, + ComponentEntity componentEntity, + Map> componentProcesses, + Map processArtifact) { + Collection + nics = nicDao.list(new NicEntity(vspId, activeVersion, componentEntity.getId(), null)); + //backup component processes + backupProcess(vspId, activeVersion, componentEntity.getId(), componentName, + componentProcesses, processArtifact); + if (CollectionUtils.isNotEmpty(nics)) { + Map nicsQustanniare = new HashMap<>(); + nics.forEach(nicEntity -> { + NicEntity nic = nicDao.get(new NicEntity(vspId, activeVersion, componentEntity.getId(), + nicEntity.getId())); + NicEntity nicQuestionnaire = nicDao.getQuestionnaireData(vspId, activeVersion, + componentEntity.getId(), nicEntity.getId()); + + nicsQustanniare + .put(nicEntity.getNicCompositionData().getName(), + nicQuestionnaire.getQuestionnaireData()); + }); + componentNicsQustanniare.put(componentName, nicsQustanniare); } + } - private static void backupProcess(String vspId, Version activeVersion, String componentId, - String componentName, Map> processes, - Map processArtifact) { - Collection processList = vendorSoftwareProductDao.listProcesses(vspId, - activeVersion, componentId); - if (!processList.isEmpty()) { - processes.put(componentName, processList); - processList.forEach(process -> { - //ProcessArtifactEntity artifact = vendorSoftwareProductDao.getProcessArtifact(vspId, - // activeVersion, componentId, process.getId()); - ProcessEntity artifact = - processDao.get(new ProcessEntity(vspId, activeVersion, componentId, process.getId())); - if (artifact.getArtifact() != null) { - processArtifact.put(process.getId(), artifact); - } - }); + private void backupProcess(String vspId, Version activeVersion, String componentId, + String componentName, Map> processes, + Map processArtifact) { + Collection processList = vendorSoftwareProductDao.listProcesses(vspId, + activeVersion, componentId); + if (!processList.isEmpty()) { + processes.put(componentName, processList); + processList.forEach(process -> { + ProcessEntity artifact = + processDao.get(new ProcessEntity(vspId, activeVersion, componentId, process.getId())); + if (artifact.getArtifact() != null) { + processArtifact.put(process.getId(), artifact); } + }); } + } - public static void retainComponentQuestionnaireData(String vspId, Version activeVersion, - Map componentsQustanniare, - Map> - componentNicsQustanniare, - Map> componentMibList, - Map> processes, - Map processArtifact) { - //VSP processes - restoreProcess(vspId, activeVersion, GENERAL_COMPONENT_ID, GENERAL_COMPONENT_ID, processes, + public void retainComponentQuestionnaireData(String vspId, Version activeVersion, + Map componentsQustanniare, + Map> + componentNicsQustanniare, + Map> componentMibList, + Map> processes, + Map processArtifact) { + //VSP processes + restoreProcess(vspId, activeVersion, GENERAL_COMPONENT_ID, GENERAL_COMPONENT_ID, processes, + processArtifact); + Collection + components = vendorSoftwareProductDao.listComponents(vspId, activeVersion); + components.forEach(componentEntity -> { + String componentName = componentEntity.getComponentCompositionData().getName(); + if (componentsQustanniare.containsKey(componentName)) { + //Restore component questionnaire + componentDao.updateQuestionnaireData(vspId, activeVersion, + componentEntity.getId(), + componentsQustanniare.get(componentEntity.getComponentCompositionData() + .getName())); + //Restore component nic questionnaire + if (componentNicsQustanniare.containsKey(componentName)) { + restoreComponentNicQuestionnaire(vspId, activeVersion, componentName, componentEntity, + componentNicsQustanniare); + } + //MIB //todo add for VES_EVENTS + if (componentMibList.containsKey(componentName)) { + restoreComponentMibData(componentName, componentEntity, componentMibList); + } + //VFC processes + restoreProcess(vspId, activeVersion, componentEntity.getId(), componentName, processes, processArtifact); - Collection - components = vendorSoftwareProductDao.listComponents(vspId, activeVersion); - components.forEach(componentEntity -> { - String componentName = componentEntity.getComponentCompositionData().getName(); - if (componentsQustanniare.containsKey(componentName)) { - componentDao.updateQuestionnaireData(vspId, activeVersion, - componentEntity.getId(), - componentsQustanniare.get(componentEntity.getComponentCompositionData() - .getName())); - if (componentNicsQustanniare.containsKey(componentName)) { - Map nicsQustanniare = componentNicsQustanniare.get(componentName); - Collection - nics = - nicDao.list(new NicEntity(vspId, activeVersion, componentEntity.getId(), null)); - nics.forEach(nicEntity -> { - if (nicsQustanniare.containsKey(nicEntity.getNicCompositionData().getName())) { - nicDao.updateQuestionnaireData(vspId, activeVersion, - componentEntity.getId(), nicEntity.getId(), - nicsQustanniare.get(nicEntity.getNicCompositionData().getName())); - } - }); - } - //MIB //todo add for VES_EVENTS - if (componentMibList.containsKey(componentName)) { - Collection mibList = - componentMibList.get(componentName); - mibList.forEach(mib -> { - mib.setComponentId(componentEntity.getId()); - componentArtifactDao.create(mib); - }); - } - //VFC processes - restoreProcess(vspId, activeVersion, componentEntity.getId(), componentName, processes, - processArtifact); - } - }); - } + } + }); + } - private static void restoreProcess(String vspId, Version activeVersion, String componentId, - String componentName, - Map> processes, - Map processArtifact) { - if (processes.containsKey(componentName)) { - Collection processList = processes.get(componentName); - processList.forEach(process -> { - //Reatin VFC process - if (!GENERAL_COMPONENT_ID.equals(componentId) && processArtifact.containsKey(process.getId - ())) { - ProcessEntity artifact = processArtifact.get(process.getId()); - artifact.setComponentId(componentId); - UniqueValueUtil.createUniqueValue(PROCESS_NAME, vspId, activeVersion.toString(), - componentId, process.getName()); - vendorSoftwareProductDao.createProcess(artifact); - } - }); + private void restoreComponentNicQuestionnaire(String vspId, Version activeVersion, + String componentName, + ComponentEntity componentEntity, + Map> + componentNicsQustanniare) { + Map nicsQustanniare = componentNicsQustanniare.get(componentName); + Collection nics = + nicDao.list(new NicEntity(vspId, activeVersion, componentEntity.getId(), null)); + nics.forEach(nicEntity -> { + if (nicsQustanniare.containsKey(nicEntity.getNicCompositionData().getName())) { + nicDao.updateQuestionnaireData(vspId, activeVersion, + componentEntity.getId(), nicEntity.getId(), + nicsQustanniare.get(nicEntity.getNicCompositionData().getName())); + } + }); + } + + private void restoreComponentMibData(String componentName, + ComponentEntity componentEntity, + Map> + componentMibList) { + Collection mibList = componentMibList.get(componentName); + mibList.forEach(mib -> { + mib.setComponentId(componentEntity.getId()); + componentArtifactDao.create(mib); + }); + } + + private void restoreProcess(String vspId, Version activeVersion, String componentId, + String componentName, + Map> processes, + Map processArtifact) { + if (processes.containsKey(componentName)) { + Collection processList = processes.get(componentName); + processList.forEach(process -> { + //Reatin VFC process + if (!GENERAL_COMPONENT_ID.equals(componentId) + && processArtifact.containsKey(process.getId())) { + ProcessEntity artifact = processArtifact.get(process.getId()); + artifact.setComponentId(componentId); + UniqueValueUtil.createUniqueValue(PROCESS_NAME, vspId, activeVersion.toString(), + componentId, process.getName()); + vendorSoftwareProductDao.createProcess(artifact); } + }); } + } - public static void deleteUploadDataAndContent(String vspId, Version version) { - //fixme change this when more tables are zusammenized - vendorSoftwareProductDao.deleteUploadData(vspId, version); - } + public void deleteUploadDataAndContent(String vspId, Version version) { + //fixme change this when more tables are zusammenized + vendorSoftwareProductDao.deleteUploadData(vspId, version); + } - public static void saveUploadData(String vspId, Version activeVersion, InputStream uploadedFileData, - FileContentHandler fileContentMap, HeatStructureTree tree) { - Map manifestAsMap = - fileContentMap.containsFile(SdcCommon.MANIFEST_NAME) ? - (Map) JsonUtil.json2Object(fileContentMap.getFileContent( - SdcCommon.MANIFEST_NAME), Map.class) + public void saveUploadData(String vspId, Version activeVersion, + InputStream uploadedFileData, + FileContentHandler fileContentMap, HeatStructureTree tree) { + Map manifestAsMap = + fileContentMap.containsFile(SdcCommon.MANIFEST_NAME) + ? (Map) JsonUtil.json2Object(fileContentMap.getFileContent( + SdcCommon.MANIFEST_NAME), Map.class) : new HashMap<>(); - UploadData uploadData = new UploadData(); - uploadData.setContentData(ByteBuffer.wrap(FileUtils.toByteArray(uploadedFileData))); - uploadData.setValidationDataStructure(new ValidationStructureList(tree)); - uploadData.setPackageName(Objects.isNull(manifestAsMap.get("name")) ? null : - (String) manifestAsMap.get("name")); - uploadData.setPackageVersion(Objects.isNull(manifestAsMap.get("version")) ? null : - (String) manifestAsMap.get("version")); - orchestrationTemplateDataDao.updateOrchestrationTemplateData(vspId, uploadData); + UploadData uploadData = new UploadData(); + uploadData.setContentData(ByteBuffer.wrap(FileUtils.toByteArray(uploadedFileData))); + uploadData.setValidationDataStructure(new ValidationStructureList(tree)); + uploadData.setPackageName(Objects.isNull(manifestAsMap.get("name")) ? null : + (String) manifestAsMap.get("name")); + uploadData.setPackageVersion(Objects.isNull(manifestAsMap.get("version")) ? null : + (String) manifestAsMap.get("version")); + orchestrationTemplateDataDao.updateOrchestrationTemplateData(vspId, uploadData); + } + + public void saveServiceModel(String vspId, + Version version, + ToscaServiceModel serviceModelToExtract, + ToscaServiceModel serviceModelToStore) { + if (serviceModelToExtract != null) { + serviceModelDao.storeServiceModel(vspId, version, serviceModelToStore); + //Extracting the compostion data from the output service model of the first phase of + // translation + compositionEntityDataManager.saveCompositionData(vspId, version, + compositionDataExtractor.extractServiceCompositionData(serviceModelToExtract)); } + } + + public static HeatStructureTree createHeatTree(FileContentHandler fileContentMap, + Map> validationErrors) { + HeatTreeManager heatTreeManager = HeatTreeManagerUtil.initHeatTreeManager(fileContentMap); + heatTreeManager.createTree(); + heatTreeManager.addErrors(validationErrors); + return heatTreeManager.getTree(); + } + + public void updateVspComponentDependencies(String vspId, Version activeVersion, + Map + vspComponentIdNameInfoBeforeProcess) { + Map updatedVspComponentNameIdInfo = getVspComponentNameIdInfo(vspId, + activeVersion); + if (MapUtils.isNotEmpty(updatedVspComponentNameIdInfo)) { + Set updatedVspComponentNames = updatedVspComponentNameIdInfo.keySet(); + Collection componentDependencies = + vendorSoftwareProductDao.listComponentDependencies(vspId, activeVersion); + if (CollectionUtils.isNotEmpty(componentDependencies)) { + updateComponentDependency(vspComponentIdNameInfoBeforeProcess, componentDependencies, + updatedVspComponentNames, updatedVspComponentNameIdInfo); + } + } + } + + + private void updateComponentDependency(Map vspComponentIdNameInfoBeforeProcess, + Collection + componentDependencies, + Set updatedVspComponentNames, + Map updatedVspComponentNameIdInfo) { + for (ComponentDependencyModelEntity componentDependency : componentDependencies) { + String sourceComponentName = vspComponentIdNameInfoBeforeProcess.get(componentDependency + .getSourceComponentId()); + String targetComponentName = vspComponentIdNameInfoBeforeProcess.get(componentDependency + .getTargetComponentId()); + if (updatedVspComponentNames.contains(sourceComponentName) + && (updatedVspComponentNames.contains(targetComponentName))) { + String newSourceComponentId = updatedVspComponentNameIdInfo.get(sourceComponentName); + componentDependency.setSourceComponentId(newSourceComponentId); + String newTargetComponentId = updatedVspComponentNameIdInfo.get(targetComponentName); + componentDependency.setTargetComponentId(newTargetComponentId); + componentDependencyModelDao.update(componentDependency); + } else { + componentDependencyModelDao.delete(componentDependency); + } + } + } + + public Map getVspComponentIdNameInfo(String vspId, Version activeVersion) { + Collection updatedVspComponents = + vendorSoftwareProductDao.listComponents(vspId, activeVersion); + Map vspComponentIdNameMap = new HashMap<>(); + if (CollectionUtils.isNotEmpty(updatedVspComponents)) { + vspComponentIdNameMap = updatedVspComponents.stream() + .filter(componentEntity -> componentEntity.getComponentCompositionData() != null) + .collect(Collectors.toMap(componentEntity -> componentEntity.getId(), + componentEntity -> componentEntity.getComponentCompositionData().getName())); - public static void saveServiceModel(String vspId, - Version version, - ToscaServiceModel serviceModelToExtract, - ToscaServiceModel serviceModelToStore){ - if (serviceModelToExtract != null) { - serviceModelDao.storeServiceModel(vspId, version, serviceModelToStore); - //Extracting the compostion data from the output service model of the first phase of - // translation - compositionEntityDataManager.saveCompositionData(vspId, version, - compositionDataExtractor.extractServiceCompositionData(serviceModelToExtract)); -// OrchestrationUtil.retainComponentQuestionnaireData(vspId, version, componentsQuestionnaire, -// componentNicsQuestionnaire, componentMibList, processes, processArtifact); - } } + return vspComponentIdNameMap; + } - public static HeatStructureTree createHeatTree(FileContentHandler fileContentMap, - Map> validationErrors){ - HeatTreeManager heatTreeManager = HeatTreeManagerUtil.initHeatTreeManager(fileContentMap); - heatTreeManager.createTree(); - heatTreeManager.addErrors(validationErrors); - return heatTreeManager.getTree(); + private Map getVspComponentNameIdInfo(String vspId, + Version activeVersion) { + Collection updatedVspComponents = + vendorSoftwareProductDao.listComponents(vspId, activeVersion); + Map vspComponentNameIdMap = new HashMap<>(); + if (CollectionUtils.isNotEmpty(updatedVspComponents)) { + vspComponentNameIdMap = updatedVspComponents.stream() + .filter(componentEntity -> componentEntity.getComponentCompositionData() != null) + .collect(Collectors.toMap(componentEntity -> componentEntity + .getComponentCompositionData().getName(), componentEntity -> componentEntity.getId())); } + return vspComponentNameIdMap; + } } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/process/OrchestrationTemplateProcessCsarHandler.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/process/OrchestrationTemplateProcessCsarHandler.java index 4866d93411..3dd118e8ec 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/process/OrchestrationTemplateProcessCsarHandler.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/process/OrchestrationTemplateProcessCsarHandler.java @@ -92,20 +92,20 @@ public class OrchestrationTemplateProcessCsarHandler implements OrchestrationTem Map> componentMibList = new HashMap<>(); Map> processes = new HashMap<>(); Map processArtifact = new HashMap<>(); - - OrchestrationUtil.backupComponentsQuestionnaireBeforeDelete(vspId, + OrchestrationUtil orchestrationUtil = new OrchestrationUtil(); + orchestrationUtil.backupComponentsQuestionnaireBeforeDelete(vspId, version, componentsQuestionnaire, componentNicsQuestionnaire, componentMibList, processes, processArtifact); Optional zipByteArrayInputStream = candidateService .fetchZipFileByteArrayInputStream(vspId, candidateData, null, OnboardingTypesEnum.CSAR, errors); - OrchestrationUtil.deleteUploadDataAndContent(vspId, version); - OrchestrationUtil.saveUploadData( + orchestrationUtil.deleteUploadDataAndContent(vspId, version); + orchestrationUtil.saveUploadData( vspId, version, zipByteArrayInputStream.get(), fileContentHandler, tree); ToscaServiceModel toscaServiceModel = toscaConverter.convert(fileContentHandler); - OrchestrationUtil.saveServiceModel(vspId, version, toscaServiceModel, toscaServiceModel); + orchestrationUtil.saveServiceModel(vspId, version, toscaServiceModel, toscaServiceModel); } diff --git a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/process/OrchestrationTemplateProcessZipHandler.java b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/process/OrchestrationTemplateProcessZipHandler.java index cceacd47b3..85ac26223f 100644 --- a/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/process/OrchestrationTemplateProcessZipHandler.java +++ b/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/process/OrchestrationTemplateProcessZipHandler.java @@ -1,5 +1,7 @@ package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.process; +import static org.openecomp.sdc.logging.messages.AuditMessages.HEAT_VALIDATION_ERROR; + import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; import org.openecomp.core.model.dao.ServiceModelDao; @@ -49,8 +51,6 @@ import java.util.List; import java.util.Map; import java.util.Optional; -import static org.openecomp.sdc.logging.messages.AuditMessages.HEAT_VALIDATION_ERROR; - public class OrchestrationTemplateProcessZipHandler implements OrchestrationTemplateProcessHandler { Logger logger = LoggerFactory.getLogger(OrchestrationTemplateProcessZipHandler.class); private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage(); @@ -68,18 +68,20 @@ public class OrchestrationTemplateProcessZipHandler implements OrchestrationTemp public OrchestrationTemplateProcessZipHandler(){} public OrchestrationTemplateActionResponse process(VspDetails vspDetails, - OrchestrationTemplateCandidateData candidateData, + OrchestrationTemplateCandidateData + candidateData, String user) { String vspId = vspDetails.getId(); Version version = vspDetails.getVersion(); - logger.audit(AuditMessages.AUDIT_MSG + AuditMessages.HEAT_VALIDATION_STARTED + - vspId); + logger.audit(AuditMessages.AUDIT_MSG + AuditMessages.HEAT_VALIDATION_STARTED + + vspId); OrchestrationTemplateActionResponse response = new OrchestrationTemplateActionResponse(); UploadFileResponse uploadFileResponse = new UploadFileResponse(); Optional fileContent = OrchestrationUtil .getFileContentMap( - OnboardingTypesEnum.ZIP, uploadFileResponse, candidateData.getContentData().array()); + OnboardingTypesEnum.ZIP, uploadFileResponse, + candidateData.getContentData().array()); if (!fileContent.isPresent()) { response.addStructureErrors(uploadFileResponse.getErrors()); mdcDataDebugMessage.debugExitMessage("VSP id", vspId); @@ -124,18 +126,21 @@ public class OrchestrationTemplateProcessZipHandler implements OrchestrationTemp Map> processes = new HashMap<>(); Map processArtifact = new HashMap<>(); - OrchestrationUtil.backupComponentsQuestionnaireBeforeDelete(vspId, + OrchestrationUtil orchestrationUtil = new OrchestrationUtil(); + Map vspComponentIdNameInfoBeforeProcess = + orchestrationUtil.getVspComponentIdNameInfo(vspId, version); + orchestrationUtil.backupComponentsQuestionnaireBeforeDelete(vspId, version, componentsQuestionnaire, componentNicsQuestionnaire, componentMibList, processes, processArtifact); - OrchestrationUtil.deleteUploadDataAndContent(vspId, version); - OrchestrationUtil.saveUploadData(vspId, version, zipByteArrayInputStream.get(), fileContentMap, + orchestrationUtil.deleteUploadDataAndContent(vspId, version); + orchestrationUtil.saveUploadData(vspId, version, zipByteArrayInputStream.get(), fileContentMap, tree); response.getErrors().values().forEach(errorList -> printAuditForErrors(errorList,vspId, HEAT_VALIDATION_ERROR)); - if ( MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, response.getErrors - ()))) { + if ( MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, + response.getErrors()))) { logger.audit(AuditMessages.AUDIT_MSG + AuditMessages.HEAT_VALIDATION_COMPLETED + vspId); } @@ -145,11 +150,12 @@ public class OrchestrationTemplateProcessZipHandler implements OrchestrationTemp HeatToToscaUtil.loadAndTranslateTemplateData(fileContentMap); ToscaServiceModel toscaServiceModel = translatorOutput.getToscaServiceModel(); - OrchestrationUtil.saveServiceModel(vspId, version, translatorOutput + orchestrationUtil.saveServiceModel(vspId, version, translatorOutput .getNonUnifiedToscaServiceModel(), toscaServiceModel); - OrchestrationUtil.retainComponentQuestionnaireData(vspId, version, componentsQuestionnaire, + orchestrationUtil.retainComponentQuestionnaireData(vspId, version, componentsQuestionnaire, componentNicsQuestionnaire, componentMibList, processes, processArtifact); - + orchestrationUtil.updateVspComponentDependencies(vspId, version, + vspComponentIdNameInfoBeforeProcess); logger.audit(AuditMessages.AUDIT_MSG + AuditMessages.HEAT_TRANSLATION_COMPLETED + vspId); uploadFileResponse.addStructureErrors(uploadErrors); 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 252e152740..d1cba65182 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 @@ -1,46 +1,62 @@ package org.openecomp.sdc.vendorsoftwareproduct.impl; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.doReturn; + import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.MockitoAnnotations; -import org.openecomp.core.model.dao.ServiceModelDao; -import org.openecomp.core.model.types.ServiceElement; import org.openecomp.core.utilities.file.FileUtils; import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum; -import org.openecomp.sdc.datatypes.error.ErrorLevel; -import org.openecomp.sdc.healing.api.HealingManager; -import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; +import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDao; +import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao; import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao; import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao; -import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateDataEntity; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData; import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails; -import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionDataExtractor; -import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager; +import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUtil; import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService; import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse; -import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse; -import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileStatus; +import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData; import org.openecomp.sdc.vendorsoftwareproduct.utils.ZipFileUtils; import org.openecomp.sdc.versioning.dao.types.Version; import org.testng.Assert; import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; import java.io.IOException; import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; import java.util.Optional; -import static org.mockito.Matchers.anyObject; -import static org.mockito.Mockito.doReturn; - -/** - * @author Avrahamg - * @since November 08, 2016 - */ public class OrchestrationTemplateCandidateManagerImplTest { private static final String USER1 = "vspTestUser1"; private static final String VSP_ID = "vspId"; private static final Version VERSION01 = new Version(0, 1); + private static final String COMPONENT_ORIG_ID_1 = "Component_Pd_Server_Id_Orig"; + private static final String COMPONENT_ORIG_ID_2 = "Component_Sm_Server_Id_Orig"; + private static final String COMPONENT_ORIG_ID_3 = "Component_Oam_Server_Id_Orig"; + private static final String COMPONENT_ORIG_ID_4 = "Component_Ps_Server_Id_Orig"; + + private static final String COMPONENT_NEW_ID_1 = "Component_Pd_Server_Id_New"; + private static final String COMPONENT_NEW_ID_2 = "Component_Sm_Server_Id_New"; + private static final String COMPONENT_NEW_ID_3 = "Component_Oam_Server_Id_New"; + private static final String COMPONENT_NEW_ID_4 = "Component_Ps_Server_Id_New"; + + private static final String COMPONENT_NAME_1 = "pd_server"; + private static final String COMPONENT_NAME_2 = "sm_server"; + private static final String COMPONENT_NAME_3 = "oam_server"; + private static final String COMPONENT_NAME_4 = "ps_server"; + @Mock private VendorSoftwareProductDao vendorSoftwareProductDaoMock; @Mock @@ -48,29 +64,25 @@ public class OrchestrationTemplateCandidateManagerImplTest { @Mock private CandidateService candidateServiceMock; @Mock - private HealingManager healingManagerMock; + private OrchestrationTemplateDao orchestrationTemplateDaoMock; @Mock - private CompositionDataExtractor compositionDataExtractorMock; - @Mock - private ServiceModelDao serviceModelDaoMock; - @Mock - private CompositionEntityDataManager compositionEntityDataManagerMock; + private ComponentDependencyModelDao componentDependencyModelDaoMock; + @InjectMocks private OrchestrationTemplateCandidateManagerImpl candidateManager; + @InjectMocks + private OrchestrationUtil orchestrationUtil; + @BeforeMethod public void setUp() throws Exception { MockitoAnnotations.initMocks(this); } - // TODO: 3/15/2017 fix and enable - //@Test + @Test public void testProcessEmptyUpload() throws IOException { -/* testLegalUpload(VSP_ID, activeVersion002, - new ZipFileUtils().getZipInputStream("/vspmanager/zips/emptyComposition.zip"), USER1);*/ - - OrchestrationTemplateCandidateDataEntity orchTemplate = - new OrchestrationTemplateCandidateDataEntity(VSP_ID, VERSION01); + OrchestrationTemplateCandidateData orchTemplate = + new OrchestrationTemplateCandidateData(); orchTemplate .setContentData(ByteBuffer.wrap(FileUtils.toByteArray(new ZipFileUtils().getZipInputStream ("/vspmanager/zips/emptyComposition.zip")))); @@ -83,14 +95,17 @@ public class OrchestrationTemplateCandidateManagerImplTest { " ]\n" + "}"); doReturn(orchTemplate) - .when(candidateServiceMock).getOrchestrationTemplateCandidate(VSP_ID, VERSION01); + .when(candidateServiceMock).getOrchestrationTemplateCandidate(anyObject(), anyObject()); doReturn(new VspDetails(VSP_ID, VERSION01)) .when(vspInfoDaoMock).get(anyObject()); + doReturn(null) + .when(orchestrationTemplateDaoMock).getValidationData(anyObject(), anyObject()); doReturn("{}").when(candidateServiceMock).createManifest(anyObject(), anyObject()); doReturn(Optional.empty()).when(candidateServiceMock) - .fetchZipFileByteArrayInputStream(anyObject(), anyObject(), anyObject(), OnboardingTypesEnum.ZIP, anyObject()); + .fetchZipFileByteArrayInputStream(anyObject(), anyObject(), anyObject(), + eq(OnboardingTypesEnum.ZIP), anyObject()); OrchestrationTemplateActionResponse response = @@ -98,51 +113,199 @@ public class OrchestrationTemplateCandidateManagerImplTest { Assert.assertNotNull(response); } - /* - @Test(dependsOnMethods = {"testUploadFile"}) - public void testUploadNotExistingFile() throws IOException { - URL url = this.getClass().getResource("notExist.zip"); - testLegalUpload(VSP_ID, activeVersion002, url == null ? null : url.openStream(), USER1); - } - - private void testLegalUpload(String vspId, Version version, InputStream upload, String user) { - candidateManager.upload(vspId, VERSION01, upload, USER1); - candidateManager.process(vspId, VERSION01, user); - - UploadDataEntity - uploadData = - vendorSoftwareProductDaoMock.getUploadData(new UploadDataEntity(vspId, version)); - Assert.assertNotNull(uploadData); - }*/ - - // TODO: 3/15/2017 fix and enable - //@Test - public void testUploadWith2VolsIn1HeatInManifest() { - doReturn(Optional.empty()).when(candidateServiceMock).validateNonEmptyFileToUpload(anyObject()); - doReturn(Optional.empty()).when(candidateServiceMock).validateRawZipData(anyObject()); - - UploadFileResponse uploadFileResponse = candidateManager - .upload(VSP_ID, VERSION01, new ZipFileUtils().getZipInputStream("/legalUploadWithWarning"), - USER1, "zip", "legalUploadWithWarning"); - Assert.assertTrue(uploadFileResponse.getStatus() == UploadFileStatus.Success); - Assert.assertTrue( - uploadFileResponse.getErrors().get("uploadFile").get(0).getLevel() == ErrorLevel.WARNING); - Assert.assertTrue(uploadFileResponse.getErrors().get("uploadFile").get(0).getMessage() - .equals("heat contains more then one vol. selecting only first vol")); - } - - // TODO: 3/15/2017 fix and enable - //@Test - public void testUploadWithManifest() { - UploadFileResponse uploadFileResponse = candidateManager - .upload(VSP_ID, VERSION01, new ZipFileUtils().getZipInputStream("/legalUploadWithWarning"), - USER1, "zip", "legalUploadWithWarning"); - Assert.assertTrue(uploadFileResponse.getStatus() == UploadFileStatus.Success); - Assert.assertTrue( - uploadFileResponse.getErrors().get("uploadFile").get(0).getLevel() == ErrorLevel.WARNING); - Assert.assertTrue(uploadFileResponse.getErrors().get("uploadFile").get(0).getMessage() - .equals("heat contains more then one vol. selecting only first vol")); + + @Test + public void testUpdateVspComponentDependenciesHeatReuploadMoreComponents() { + Collection existingComponentsDependencies = + getExistingComponentDependencies(); + doReturn(existingComponentsDependencies). + when(vendorSoftwareProductDaoMock).listComponentDependencies(anyObject(), anyObject()); + Collection componentListWithMoreComponentsInHeat = + getComponentListWithMoreComponentsInHeat(); + doReturn(componentListWithMoreComponentsInHeat).when(vendorSoftwareProductDaoMock) + .listComponents(anyObject(), anyObject()); + Map componentIdNameInfoBeforeProcess = getVspInitComponentIdNameInfo(); + orchestrationUtil.updateVspComponentDependencies(anyObject(), anyObject(), + componentIdNameInfoBeforeProcess); + Mockito.verify(componentDependencyModelDaoMock, Mockito.times(2)).update(anyObject()); + Mockito.verify(componentDependencyModelDaoMock, Mockito.times(0)).delete(anyObject()); + } + + @Test + public void testUpdateVspComponentDependenciesHeatReuploadLessComponents() { + Collection existingComponentsDependencies = + getExistingComponentDependencies(); + doReturn(existingComponentsDependencies). + when(vendorSoftwareProductDaoMock).listComponentDependencies(anyObject(), anyObject()); + Collection componentListWithLessComponentsInHeat = + getComponentListWithLessComponentsInHeat(); + doReturn(componentListWithLessComponentsInHeat).when(vendorSoftwareProductDaoMock) + .listComponents(anyObject(), anyObject()); + Map componentIdNameInfoBeforeProcess = getVspInitComponentIdNameInfo(); + orchestrationUtil.updateVspComponentDependencies(anyObject(), anyObject(), + componentIdNameInfoBeforeProcess); + Mockito.verify(componentDependencyModelDaoMock, Mockito.times(1)).update(anyObject()); + Mockito.verify(componentDependencyModelDaoMock, Mockito.times(1)).delete(anyObject()); } + @Test + public void testUpdateVspComponentDependenciesHeatReuploadSameComponents() { + Collection existingComponentsDependencies = + getExistingComponentDependencies(); + doReturn(existingComponentsDependencies). + when(vendorSoftwareProductDaoMock).listComponentDependencies(anyObject(), anyObject()); + Collection componentListWithSameComponentsInHeat = + getComponentListWithSameComponentsInHeat(); + doReturn(componentListWithSameComponentsInHeat).when(vendorSoftwareProductDaoMock) + .listComponents(anyObject(), anyObject()); + Map componentIdNameInfoBeforeProcess = getVspInitComponentIdNameInfo(); + orchestrationUtil.updateVspComponentDependencies(anyObject(), anyObject(), + componentIdNameInfoBeforeProcess); + Mockito.verify(componentDependencyModelDaoMock, Mockito.times(2)).update(anyObject()); + Mockito.verify(componentDependencyModelDaoMock, Mockito.times(0)).delete(anyObject()); + } + + @Test + public void testUpdateVspComponentDependenciesHeatReuploadNoComponents() { + Collection existingComponentsDependencies = + getExistingComponentDependencies(); + doReturn(existingComponentsDependencies). + when(vendorSoftwareProductDaoMock).listComponentDependencies(anyObject(), anyObject()); + Collection componentListWithMoreComponentsInHeat = + new ArrayList<>(); + doReturn(componentListWithMoreComponentsInHeat).when(vendorSoftwareProductDaoMock) + .listComponents(anyObject(), anyObject()); + Map componentIdNameInfoBeforeProcess = getVspInitComponentIdNameInfo(); + orchestrationUtil.updateVspComponentDependencies(anyObject(), anyObject(), + componentIdNameInfoBeforeProcess); + Mockito.verify(componentDependencyModelDaoMock, Mockito.times(0)).update(anyObject()); + Mockito.verify(componentDependencyModelDaoMock, Mockito.times(0)).delete(anyObject()); + } + + @Test + public void testVspComponentIdNameInfoNoComponents() { + Collection initialVspComponents = new ArrayList<>(); + Map vspComponentIdNameInfo = + orchestrationUtil.getVspComponentIdNameInfo(anyObject(), anyObject()); + Assert.assertEquals(vspComponentIdNameInfo.size(), 0); + } + @Test + public void testVspComponentIdNameInfo() { + Collection initialVspComponents = getInitialVspComponents(); + doReturn(initialVspComponents).when(vendorSoftwareProductDaoMock) + .listComponents(anyObject(), anyObject()); + Map vspComponentIdNameInfo = + orchestrationUtil.getVspComponentIdNameInfo(anyObject(), anyObject()); + Assert.assertEquals(vspComponentIdNameInfo.size(), 3); + Assert.assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_1)); + Assert.assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_2)); + Assert.assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_3)); + } + + @Test + public void testVspComponentIdNameInfoEmptyCompositionData() { + Collection initialVspComponents = getInitialVspComponents(); + ComponentEntity componentEntity = new ComponentEntity(); + componentEntity.setId(COMPONENT_ORIG_ID_4); + initialVspComponents.add(componentEntity); + + doReturn(initialVspComponents).when(vendorSoftwareProductDaoMock) + .listComponents(anyObject(), anyObject()); + Map vspComponentIdNameInfo = + orchestrationUtil.getVspComponentIdNameInfo(anyObject(), anyObject()); + Assert.assertEquals(vspComponentIdNameInfo.size(), 3); + Assert.assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_1)); + Assert.assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_2)); + Assert.assertNotNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_3)); + Assert.assertNull(vspComponentIdNameInfo.get(COMPONENT_ORIG_ID_4)); + } + + private Map getVspInitComponentIdNameInfo() { + Map componentIdNameInfoBeforeProcess = new HashMap<>(); + componentIdNameInfoBeforeProcess.put(COMPONENT_ORIG_ID_1, COMPONENT_NAME_1); + componentIdNameInfoBeforeProcess.put(COMPONENT_ORIG_ID_2, COMPONENT_NAME_2); + componentIdNameInfoBeforeProcess.put(COMPONENT_ORIG_ID_3, COMPONENT_NAME_3); + return componentIdNameInfoBeforeProcess; + } + + private Collection getInitialVspComponents() { + Collection vspComponents = new ArrayList<>(); + ComponentEntity component1 = createComponentEntity(COMPONENT_ORIG_ID_1, COMPONENT_NAME_1); + ComponentEntity component2 = createComponentEntity(COMPONENT_ORIG_ID_2, COMPONENT_NAME_2); + ComponentEntity component3 = createComponentEntity(COMPONENT_ORIG_ID_3, COMPONENT_NAME_3); + vspComponents.add(component1); + vspComponents.add(component2); + vspComponents.add(component3); + return vspComponents; + } + + private Collection getComponentListWithMoreComponentsInHeat() { + Collection vspComponents = getInitialVspComponents(); + createInitialComponentDependencies(vspComponents); + ComponentEntity newComponent = createComponentEntity(COMPONENT_NEW_ID_4, COMPONENT_NAME_4); + vspComponents.add(newComponent); + return vspComponents; + } + + private Collection getComponentListWithLessComponentsInHeat() { + Collection vspComponents = getInitialVspComponents(); + for (Iterator iterator = vspComponents.iterator(); iterator.hasNext();) { + ComponentEntity componentEntity = iterator.next(); + if (componentEntity.getComponentCompositionData().getName().equals(COMPONENT_NAME_1)) { + iterator.remove(); + } else if (componentEntity.getComponentCompositionData().getName().equals(COMPONENT_NAME_2)) { + componentEntity.setId(COMPONENT_NEW_ID_2); + } else if (componentEntity.getComponentCompositionData().getName().equals(COMPONENT_NAME_3)) { + componentEntity.setId(COMPONENT_NEW_ID_3); + } + } + return vspComponents; + } + + private Collection getComponentListWithSameComponentsInHeat() { + Collection vspComponents = getInitialVspComponents(); + createInitialComponentDependencies(vspComponents); + return vspComponents; + } + + private Collection getExistingComponentDependencies() { + Collection newComponents = new ArrayList<>(); + ComponentDependencyModelEntity entity = + createComponentDependencyEntity(COMPONENT_ORIG_ID_1, COMPONENT_ORIG_ID_2); + ComponentDependencyModelEntity entity2 = + createComponentDependencyEntity(COMPONENT_ORIG_ID_2, COMPONENT_ORIG_ID_3); + newComponents.add(entity); + newComponents.add(entity2); + return newComponents; + } + + private ComponentEntity createComponentEntity(String componentId, String componentName) { + ComponentEntity componentEntity = new ComponentEntity(); + componentEntity.setId(componentId); + ComponentData data = new ComponentData(); + data.setName(componentName); + componentEntity.setComponentCompositionData(data); + return componentEntity; + } + + private ComponentDependencyModelEntity createComponentDependencyEntity(String sourceComponentId, + String targetComponentId) { + ComponentDependencyModelEntity componentDependency = new ComponentDependencyModelEntity(); + componentDependency.setSourceComponentId(sourceComponentId); + componentDependency.setTargetComponentId(targetComponentId); + return componentDependency; + } + + private void createInitialComponentDependencies(Collection vspComponents) { + for (ComponentEntity componentEntity : vspComponents) { + if (componentEntity.getComponentCompositionData().getName().equals(COMPONENT_NAME_1)) { + componentEntity.setId(COMPONENT_NEW_ID_1); + } else if (componentEntity.getComponentCompositionData().getName().equals(COMPONENT_NAME_2)) { + componentEntity.setId(COMPONENT_NEW_ID_2); + } else if (componentEntity.getComponentCompositionData().getName().equals(COMPONENT_NAME_3)) { + componentEntity.setId(COMPONENT_NEW_ID_3); + } + } + } } \ No newline at end of file -- cgit 1.2.3-korg