From 280f8015d06af1f41a3ef12e8300801c7a5e0d54 Mon Sep 17 00:00:00 2001 From: AviZi Date: Fri, 9 Jun 2017 02:39:56 +0300 Subject: [SDC-29] Amdocs OnBoard 1707 initial commit. Change-Id: Ie4d12a3f574008b792899b368a0902a8b46b5370 Signed-off-by: AviZi --- .../artifact/MonitoringMibEnricherTest.java | 167 +++++++++++++++++ .../artifact/ProcessArtifactEnricherTest.java | 128 +++++++++++++ .../tosca/AbstractSubstituteToscaEnricherTest.java | 162 ++++++++++++++++ .../impl/tosca/BaseToscaEnrichmentTest.java | 206 +++++++++++++++++++++ .../impl/tosca/ComponentQuestionnaireDataTest.java | 115 ++++++++++++ 5 files changed, 778 insertions(+) create mode 100644 openecomp-be/lib/openecomp-sdc-enrichment-lib/openecomp-sdc-enrichment-impl/src/test/java/org/openecomp/sdc/enrichment/impl/external/artifact/MonitoringMibEnricherTest.java create mode 100644 openecomp-be/lib/openecomp-sdc-enrichment-lib/openecomp-sdc-enrichment-impl/src/test/java/org/openecomp/sdc/enrichment/impl/external/artifact/ProcessArtifactEnricherTest.java create mode 100644 openecomp-be/lib/openecomp-sdc-enrichment-lib/openecomp-sdc-enrichment-impl/src/test/java/org/openecomp/sdc/enrichment/impl/tosca/AbstractSubstituteToscaEnricherTest.java create mode 100644 openecomp-be/lib/openecomp-sdc-enrichment-lib/openecomp-sdc-enrichment-impl/src/test/java/org/openecomp/sdc/enrichment/impl/tosca/BaseToscaEnrichmentTest.java create mode 100644 openecomp-be/lib/openecomp-sdc-enrichment-lib/openecomp-sdc-enrichment-impl/src/test/java/org/openecomp/sdc/enrichment/impl/tosca/ComponentQuestionnaireDataTest.java (limited to 'openecomp-be/lib/openecomp-sdc-enrichment-lib/openecomp-sdc-enrichment-impl/src/test/java') diff --git a/openecomp-be/lib/openecomp-sdc-enrichment-lib/openecomp-sdc-enrichment-impl/src/test/java/org/openecomp/sdc/enrichment/impl/external/artifact/MonitoringMibEnricherTest.java b/openecomp-be/lib/openecomp-sdc-enrichment-lib/openecomp-sdc-enrichment-impl/src/test/java/org/openecomp/sdc/enrichment/impl/external/artifact/MonitoringMibEnricherTest.java new file mode 100644 index 0000000000..7a16b6945d --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-enrichment-lib/openecomp-sdc-enrichment-impl/src/test/java/org/openecomp/sdc/enrichment/impl/external/artifact/MonitoringMibEnricherTest.java @@ -0,0 +1,167 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.enrichment.impl.external.artifact; + +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.openecomp.core.enrichment.types.ArtifactCategory; +import org.openecomp.core.enrichment.types.ArtifactType; +import org.openecomp.core.model.dao.EnrichedServiceModelDao; +import org.openecomp.core.model.types.ServiceArtifact; +import org.openecomp.core.utilities.file.FileUtils; +import org.openecomp.sdc.enrichment.EnrichmentInfo; +import org.openecomp.sdc.vendorsoftwareproduct.dao.MibDao; +import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao; +import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.MibEntity; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity; +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.File; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Optional; + +import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.times; + + +/** + * @author shiria + * @since November 06, 2016. + */ + +public class MonitoringMibEnricherTest { + @Mock + private MibDao mibDaoMock; + @Mock + private EnrichedServiceModelDao enrichedServiceModelDaoMock; + @Mock + private VendorSoftwareProductDao vendorSoftwareProductDaoMock; + @Mock + private ComponentDao componentDaoMock; + + @InjectMocks + private MonitoringMibEnricher monitoringMibEnricher; + + + @BeforeMethod(alwaysRun = true) + public void injectDoubles() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testEnrichComponent() throws Exception { + String vspId = "123"; + String componentId = "1111111111"; + Version version = new Version(); + version.setMajor(1); + version.setMinor(0); + + ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId); + setMockToEnrichComponent(vspId, componentId, version); + monitoringMibEnricher.enrichComponent(componentEntity, vspId, version); + + String componentName = componentEntity.getComponentCompositionData().getName(); + + ArgumentCaptor expectedServiceArtifact = + ArgumentCaptor.forClass(ServiceArtifact.class); + Mockito.verify(enrichedServiceModelDaoMock, atLeastOnce()) + .storeExternalArtifact(expectedServiceArtifact.capture()); + Assert + .assertEquals(expectedServiceArtifact.getValue().getName().startsWith(componentName), true); + Assert.assertEquals(expectedServiceArtifact.getValue().getName(), + componentName + File.separator + ArtifactCategory.DEPLOYMENT.getDisplayName() + + File.separator + ArtifactType.SNMP_POLL + File.separator + "mib1.yml"); + + } + + @Test + public void testEnrich() throws Exception { + EnrichmentInfo enrichmentInfo = new EnrichmentInfo(); + Version version = new Version(); + version.setMajor(1); + version.setMinor(0); + String vspId = "123"; + enrichmentInfo.setKey(vspId); + enrichmentInfo.setVersion(version); + String componentId1 = "1111111111"; + String componentId2 = "2222222222"; + + + Collection returnedComponents = new ArrayList<>(); + returnedComponents.add(getComponentEntity(vspId, version, componentId1)); + returnedComponents.add(getComponentEntity(vspId, version, componentId2)); + + Mockito.when(componentDaoMock.list(anyObject())) + .thenReturn(returnedComponents); + setMockToEnrichComponent(vspId, componentId1, version); + + monitoringMibEnricher.enrich(enrichmentInfo); + Mockito.verify(enrichedServiceModelDaoMock, times(8)).storeExternalArtifact(anyObject()); + + } + + private void setMockToEnrichComponent(String vspId, String componentId, Version version) { + MibEntity returnedArtifact = new MibEntity(); + returnedArtifact.setVspId(vspId); + returnedArtifact.setVersion(version); + returnedArtifact.setComponentId(componentId); + returnedArtifact.setType(ArtifactType.SNMP_POLL); + returnedArtifact.setArtifactName("mib.zip"); + returnedArtifact.setArtifact(getMibByteBuffer("/mock/enrichMib/MIB.zip")); + + Mockito.when(mibDaoMock.getByType(anyObject())) + .thenReturn(Optional.of(returnedArtifact)); + Mockito.doNothing().when(enrichedServiceModelDaoMock).storeExternalArtifact(anyObject()); + } + + private ComponentEntity getComponentEntity(String vspId, Version version, String componentId) { + ComponentEntity componentEntity = new ComponentEntity(); + componentEntity.setId(componentId); + componentEntity.setVspId(vspId); + componentEntity.setVersion(version); + + String componentName = vspId + "enrichMib_server"; + String compositionData = "{\n" + + " \"name\": \"org.openecomp.resource.vfc.nodes.heat." + componentName + "\",\n" + + " \"displayName\": \"" + componentName + "\"\n" + + "}"; + componentEntity.setCompositionData(compositionData); + return componentEntity; + } + + private ByteBuffer getMibByteBuffer(String fileName) { + InputStream mibFile = FileUtils.getFileInputStream(this.getClass().getResource(fileName)); + byte[] mibBytes = FileUtils.toByteArray(mibFile); + return ByteBuffer.wrap(mibBytes); + } + +} diff --git a/openecomp-be/lib/openecomp-sdc-enrichment-lib/openecomp-sdc-enrichment-impl/src/test/java/org/openecomp/sdc/enrichment/impl/external/artifact/ProcessArtifactEnricherTest.java b/openecomp-be/lib/openecomp-sdc-enrichment-lib/openecomp-sdc-enrichment-impl/src/test/java/org/openecomp/sdc/enrichment/impl/external/artifact/ProcessArtifactEnricherTest.java new file mode 100644 index 0000000000..cfb241483a --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-enrichment-lib/openecomp-sdc-enrichment-impl/src/test/java/org/openecomp/sdc/enrichment/impl/external/artifact/ProcessArtifactEnricherTest.java @@ -0,0 +1,128 @@ +package org.openecomp.sdc.enrichment.impl.external.artifact; + +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.openecomp.core.enrichment.types.ArtifactCategory; +import org.openecomp.core.model.dao.EnrichedServiceModelDao; +import org.openecomp.core.model.types.ServiceArtifact; +import org.openecomp.core.utilities.file.FileUtils; +import org.openecomp.sdc.enrichment.EnrichmentInfo; +import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDao; +import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessType; +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.File; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Collection; + +import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.when; + +public class ProcessArtifactEnricherTest { + @Mock + ProcessDao processDaoMock; + @Mock + EnrichedServiceModelDao enrichedServiceModelDaoMock; + @Mock + VendorSoftwareProductDao vendorSoftwareProductDaoMock; + + @InjectMocks + ProcessArtifactEnricher processArtifactEnricher; + + + @BeforeMethod(alwaysRun = true) + public void injectDoubles() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testEnrichComponent() throws Exception { + String vspId = "123"; + String componentId = "1111111111"; + Version version = new Version(); + version.setMajor(1); + version.setMinor(0); + + ComponentEntity componentEntity = getComponentEntity(vspId, version, componentId); + setMockToEnrichComponent(vspId, componentId, version); + + ProcessEntity entity = new ProcessEntity(vspId, version, componentId, null); + ProcessEntity processEntity = new ProcessEntity(); + processEntity.setType(ProcessType.Lifecycle_Operations); + processEntity.setVspId(vspId); + processEntity.setVersion(version); + processEntity.setComponentId(componentId); + + Collection componentList = new ArrayList(); + componentList.add(componentEntity); + when(vendorSoftwareProductDaoMock.listComponents(vspId, version)).thenReturn(componentList); + + Collection list = new ArrayList(); + list.add(processEntity); + when(processDaoMock.list(entity)).thenReturn(list); + + EnrichmentInfo info = new EnrichmentInfo(); + info.setVersion(version); + info.setKey(vspId); + processArtifactEnricher.enrich(info); + + String componentName = componentEntity.getComponentCompositionData().getName(); + + ArgumentCaptor expectedServiceArtifact = + ArgumentCaptor.forClass(ServiceArtifact.class); + Mockito.verify(enrichedServiceModelDaoMock, atLeastOnce()) + .storeExternalArtifact(expectedServiceArtifact.capture()); + Assert + .assertEquals(expectedServiceArtifact.getValue().getName().startsWith(componentName), true); + Assert.assertEquals(expectedServiceArtifact.getValue().getName(), + componentName + File.separator + ArtifactCategory.DEPLOYMENT.getDisplayName() + + File.separator + "Lifecycle Operations" + File.separator + "artifact_1kb.txt"); + + } + + private void setMockToEnrichComponent(String vspId, String componentId, Version version) { + ProcessEntity returnedArtifact = new ProcessEntity(); + returnedArtifact.setVspId(vspId); + returnedArtifact.setVersion(version); + returnedArtifact.setComponentId(componentId); + returnedArtifact.setArtifactName("artifact_1kb.txt"); + returnedArtifact.setArtifact(getMibByteBuffer("/mock/enrichProcess/artifact_1kb.txt")); + + Mockito.when(processDaoMock.get(anyObject())) + .thenReturn(returnedArtifact); + Mockito.doNothing().when(enrichedServiceModelDaoMock).storeExternalArtifact(anyObject()); + } + + private ComponentEntity getComponentEntity(String vspId, Version version, String componentId) { + ComponentEntity componentEntity = new ComponentEntity(); + componentEntity.setId(componentId); + componentEntity.setVspId(vspId); + componentEntity.setVersion(version); + + String componentName = vspId + "enrichMib_server"; + String compositionData = "{\n" + + " \"name\": \"org.openecomp.resource.vfc.nodes.heat." + componentName + "\",\n" + + " \"displayName\": \"" + componentName + "\"\n" + + "}"; + componentEntity.setCompositionData(compositionData); + return componentEntity; + } + + private ByteBuffer getMibByteBuffer(String fileName) { + InputStream mibFile = FileUtils.getFileInputStream(this.getClass().getResource(fileName)); + byte[] mibBytes = FileUtils.toByteArray(mibFile); + return ByteBuffer.wrap(mibBytes); + } +} diff --git a/openecomp-be/lib/openecomp-sdc-enrichment-lib/openecomp-sdc-enrichment-impl/src/test/java/org/openecomp/sdc/enrichment/impl/tosca/AbstractSubstituteToscaEnricherTest.java b/openecomp-be/lib/openecomp-sdc-enrichment-lib/openecomp-sdc-enrichment-impl/src/test/java/org/openecomp/sdc/enrichment/impl/tosca/AbstractSubstituteToscaEnricherTest.java new file mode 100644 index 0000000000..ae1b613f75 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-enrichment-lib/openecomp-sdc-enrichment-impl/src/test/java/org/openecomp/sdc/enrichment/impl/tosca/AbstractSubstituteToscaEnricherTest.java @@ -0,0 +1,162 @@ +package org.openecomp.sdc.enrichment.impl.tosca; + + +import static org.mockito.Mockito.when; +import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.HIGH_AVAIL_MODE; +import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.MANDATORY; +import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.MAX_INSTANCES; +import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.MIN_INSTANCES; +import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.VFC_NAMING_CODE; + +import org.apache.commons.collections.map.HashedMap; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.openecomp.sdc.datatypes.error.ErrorMessage; +import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; +import org.openecomp.sdc.versioning.dao.types.Version; +import org.testng.Assert; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + + +public class AbstractSubstituteToscaEnricherTest extends BaseToscaEnrichmentTest { + @Mock + ComponentQuestionnaireData utilMock; + + @InjectMocks + AbstractSubstituteToscaEnricher toscaEnricher; + + String vspId = null; + Version version = new Version(); + + @BeforeMethod(alwaysRun = true) + public void injectDoubles() { + MockitoAnnotations.initMocks(this); + vspId = "123"; + version.setMajor(1); + version.setMinor(0); + } + + @Test + public void testEnrich() throws Exception { + outputFilesPath = "/mock/enrichHA/out/"; + + ToscaServiceModel toscaServiceModel = + loadToscaServiceModel("/mock/enrichHA/in/", "/mock/toscaGlobalServiceTemplates/", + "MainServiceTemplate.yaml"); + + Map> componentTypetoParams = new HashMap(); + Map innerProps = new HashedMap(); + innerProps.put(MANDATORY, "YES"); + innerProps.put(HIGH_AVAIL_MODE, "geo-activestandby"); + innerProps.put(VFC_NAMING_CODE, "Code1"); + innerProps.put(MIN_INSTANCES, 1); + innerProps.put(MAX_INSTANCES, 2); + + componentTypetoParams.put("pd_server", innerProps); + + when(utilMock.getPropertiesfromCompQuestionnaire(vspId,version)).thenReturn + (componentTypetoParams); + + Map map = new HashMap(); + Map> sourceToTargetDependencies = new HashMap>(); + List targets = new ArrayList(); + targets.add("fe"); targets.add("be"); + sourceToTargetDependencies.put("pd_server", targets); + + when(utilMock.getSourceToTargetComponent()).thenReturn(map); + + when(utilMock.populateDependencies(vspId,version,map)).thenReturn(sourceToTargetDependencies); + + Map> errors = + toscaEnricher.enrich(toscaServiceModel, vspId, version ); + + compareActualAndExpectedModel(toscaServiceModel); + + Assert.assertEquals(errors.size(), 0); + } + + @Test + public void testEnrichWithoutServiceTemplateFilter() throws Exception { + outputFilesPath = "/mock/enrichHANoServiceTemplateFilter/out"; + + ToscaServiceModel toscaServiceModel = + loadToscaServiceModel("/mock/enrichHANoServiceTemplateFilter/in", + "/mock/toscaGlobalServiceTemplates/", + "MainServiceTemplate.yaml"); + + Map> componentTypetoParams = new HashMap(); + Map innerProps = new HashedMap(); + innerProps.put(MANDATORY, "NO"); + innerProps.put(HIGH_AVAIL_MODE, ""); + innerProps.put(VFC_NAMING_CODE, "pd_server_code1"); + innerProps.put(MIN_INSTANCES, null); + innerProps.put(MAX_INSTANCES, null); + + componentTypetoParams.put("pd_server", innerProps); + + when(utilMock.getPropertiesfromCompQuestionnaire(vspId,version)).thenReturn + (componentTypetoParams); + + Map map = new HashMap(); + Map> sourceToTargetDependencies = new HashMap>(); + + when(utilMock.getSourceToTargetComponent()).thenReturn(map); + when(utilMock.populateDependencies(vspId,version,map)).thenReturn(sourceToTargetDependencies); + + Map> errors = + toscaEnricher.enrich(toscaServiceModel, vspId, version ); + + compareActualAndExpectedModel(toscaServiceModel); + + Assert.assertEquals(errors.size(), 0); + } + + @Test + public void testEnrichNotMandatory() throws Exception { + outputFilesPath = "/mock/enrichHANotMandatory/out"; + + ToscaServiceModel toscaServiceModel = + loadToscaServiceModel("/mock/enrichHANotMandatory/in", + "/mock/toscaGlobalServiceTemplates/", + "MainServiceTemplate.yaml"); + + Map> componentTypetoParams = new HashMap(); + Map innerProps = new HashedMap(); + + innerProps.put(MANDATORY, ""); + innerProps.put(MIN_INSTANCES, 1); + innerProps.put(MAX_INSTANCES, 5); + + componentTypetoParams.put("pd_server_vm", innerProps); + + when(utilMock.getPropertiesfromCompQuestionnaire(vspId,version)).thenReturn + (componentTypetoParams); + + Map map = new HashMap(); + Map> sourceToTargetDependencies = new HashMap>(); + List targets = new ArrayList(); + targets.add("fe"); + sourceToTargetDependencies.put("pd_server_vm", targets); + + when(utilMock.getSourceToTargetComponent()).thenReturn(map); + + when(utilMock.populateDependencies(vspId,version,map)).thenReturn(sourceToTargetDependencies); + + when(utilMock.getSourceToTargetComponent()).thenReturn(map); + when(utilMock.populateDependencies(vspId,version,map)).thenReturn(sourceToTargetDependencies); + + Map> errors = + toscaEnricher.enrich(toscaServiceModel, vspId, version ); + + compareActualAndExpectedModel(toscaServiceModel); + + Assert.assertEquals(errors.size(), 0); + } +} diff --git a/openecomp-be/lib/openecomp-sdc-enrichment-lib/openecomp-sdc-enrichment-impl/src/test/java/org/openecomp/sdc/enrichment/impl/tosca/BaseToscaEnrichmentTest.java b/openecomp-be/lib/openecomp-sdc-enrichment-lib/openecomp-sdc-enrichment-impl/src/test/java/org/openecomp/sdc/enrichment/impl/tosca/BaseToscaEnrichmentTest.java new file mode 100644 index 0000000000..366bd82eab --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-enrichment-lib/openecomp-sdc-enrichment-impl/src/test/java/org/openecomp/sdc/enrichment/impl/tosca/BaseToscaEnrichmentTest.java @@ -0,0 +1,206 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.enrichment.impl.tosca; + +import org.openecomp.core.utilities.file.FileUtils; +import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; +import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate; +import org.openecomp.sdc.tosca.services.ToscaFileOutputService; +import org.openecomp.sdc.tosca.services.ToscaUtil; +import org.openecomp.sdc.tosca.services.impl.ToscaFileOutputServiceCsarImpl; +import org.openecomp.sdc.tosca.services.yamlutil.ToscaExtensionYamlUtil; + +import java.io.*; +import java.net.URL; +import java.nio.file.NotDirectoryException; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +import static org.junit.Assert.assertEquals; + +public class BaseToscaEnrichmentTest { + + protected String outputFilesPath; + + public static ToscaServiceModel loadToscaServiceModel(String serviceTemplatesPath, + String globalServiceTemplatesPath, + String entryDefinitionServiceTemplate) + throws IOException { + ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); + Map serviceTemplates = new HashMap<>(); + if (entryDefinitionServiceTemplate == null) { + entryDefinitionServiceTemplate = "MainServiceTemplate.yaml"; + } + + loadServiceTemplates(serviceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates); + if (globalServiceTemplatesPath != null) { + loadServiceTemplates(globalServiceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates); + } + + return new ToscaServiceModel(null, serviceTemplates, entryDefinitionServiceTemplate); + } + + private static void loadServiceTemplates(String serviceTemplatesPath, + ToscaExtensionYamlUtil toscaExtensionYamlUtil, + Map serviceTemplates) + throws IOException { + URL urlFile = BaseToscaEnrichmentTest.class.getResource(serviceTemplatesPath); + if (urlFile != null) { + File pathFile = new File(urlFile.getFile()); + Collection files = org.apache.commons.io.FileUtils.listFiles(pathFile, null, true); + if (files != null) { + addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil); + } else { + throw new NotDirectoryException(serviceTemplatesPath); + } + } else { + throw new NotDirectoryException(serviceTemplatesPath); + } + } + + private static void addServiceTemplateFiles(Map serviceTemplates, + Collection files, + ToscaExtensionYamlUtil toscaExtensionYamlUtil) + throws IOException { + for (File file : files) { + try (InputStream yamlFile = new FileInputStream(file)) { + ServiceTemplate serviceTemplateFromYaml = + toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); + serviceTemplates.put(ToscaUtil.getServiceTemplateFileName(serviceTemplateFromYaml), serviceTemplateFromYaml); + try { + yamlFile.close(); + } catch (IOException ignore) { + } + } catch (FileNotFoundException exception) { + throw exception; + } catch (IOException exception) { + throw exception; + } + } + } + + + /*public static ToscaServiceModel loadToscaServiceModel(String serviceTemplatesPath, + String globalServiceTemplatesPath, + String entryDefinitionServiceTemplate) + throws IOException { + ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil(); + Map serviceTemplates = new HashMap<>(); + if (entryDefinitionServiceTemplate == null) { + entryDefinitionServiceTemplate = "MainServiceTemplate.yaml"; + } + + loadServiceTemplates(serviceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates); + if (globalServiceTemplatesPath != null) { + loadServiceTemplates(globalServiceTemplatesPath, toscaExtensionYamlUtil, serviceTemplates); + } + + return new ToscaServiceModel(null, serviceTemplates, entryDefinitionServiceTemplate); + } + + private static void loadServiceTemplates(String serviceTemplatesPath, + ToscaExtensionYamlUtil toscaExtensionYamlUtil, + Map serviceTemplates) + throws IOException { + URL urlFile = BaseToscaEnrichmentTest.class.getResource(serviceTemplatesPath); + if (urlFile != null) { + File pathFile = new File(urlFile.getFile()); + File[] files = pathFile.listFiles(); + if (files != null) { + addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil); + } else { + throw new NotDirectoryException(serviceTemplatesPath); + } + } else { + throw new NotDirectoryException(serviceTemplatesPath); + } + } + + private static void addServiceTemplateFiles(Map serviceTemplates, + File[] files, + ToscaExtensionYamlUtil toscaExtensionYamlUtil) + throws IOException { + for (File file : files) { + try (InputStream yamlFile = new FileInputStream(file)) { + ServiceTemplate serviceTemplateFromYaml = + toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class); + serviceTemplates.put(file.getName(), serviceTemplateFromYaml); + try { + yamlFile.close(); + } catch (IOException ignore) { + } + } catch (FileNotFoundException e) { + throw e; + } catch (IOException e) { + throw e; + } + } + }*/ + + void compareActualAndExpectedModel(ToscaServiceModel toscaServiceModel) throws IOException { + + ToscaFileOutputService toscaFileOutputService = new ToscaFileOutputServiceCsarImpl(); + byte[] toscaActualFile = toscaFileOutputService.createOutputFile(toscaServiceModel, null); + + URL url = BaseToscaEnrichmentTest.class.getResource(outputFilesPath); + Set expectedResultFileNameSet = new HashSet<>(); + Map expectedResultMap = new HashMap<>(); + String path = url.getPath(); + File pathFile = new File(path); + File[] files = pathFile.listFiles(); + org.junit.Assert.assertNotNull("model is empty", files); + for (File expectedFile : files) { + expectedResultFileNameSet.add(expectedFile.getName()); + try (FileInputStream input = new FileInputStream(expectedFile)) { + expectedResultMap.put(expectedFile.getName(), FileUtils.toByteArray(input)); + } + } + + try (InputStream fis = new ByteArrayInputStream(toscaActualFile); + ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis))) { + ZipEntry entry; + String name; + String expected; + String actual; + + while ((entry = zis.getNextEntry()) != null) { + name = entry.getName() + .substring(entry.getName().lastIndexOf(File.separator) + 1, entry.getName().length()); + if (expectedResultFileNameSet.contains(name)) { + expected = new String(expectedResultMap.get(name)).trim().replace("\r", ""); + actual = new String(FileUtils.toByteArray(zis)).trim().replace("\r", ""); + assertEquals("difference in file: " + name, expected, actual); + + expectedResultFileNameSet.remove(name); + } + } + if (expectedResultFileNameSet.isEmpty()) { + expectedResultFileNameSet.forEach(System.out::println); + } + } + assertEquals(0, expectedResultFileNameSet.size()); + } +} diff --git a/openecomp-be/lib/openecomp-sdc-enrichment-lib/openecomp-sdc-enrichment-impl/src/test/java/org/openecomp/sdc/enrichment/impl/tosca/ComponentQuestionnaireDataTest.java b/openecomp-be/lib/openecomp-sdc-enrichment-lib/openecomp-sdc-enrichment-impl/src/test/java/org/openecomp/sdc/enrichment/impl/tosca/ComponentQuestionnaireDataTest.java new file mode 100644 index 0000000000..d281604227 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-enrichment-lib/openecomp-sdc-enrichment-impl/src/test/java/org/openecomp/sdc/enrichment/impl/tosca/ComponentQuestionnaireDataTest.java @@ -0,0 +1,115 @@ +package org.openecomp.sdc.enrichment.impl.tosca; + +import static org.mockito.Mockito.doReturn; +import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.HIGH_AVAIL_MODE; +import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.MANDATORY; +import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.MAX_INSTANCES; +import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.MIN_INSTANCES; +import static org.openecomp.sdc.enrichment.impl.util.EnrichmentConstants.VFC_NAMING_CODE; + +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao; +import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDao; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity; +import org.openecomp.sdc.versioning.VersioningManager; +import org.openecomp.sdc.versioning.dao.types.Version; +import org.testng.Assert; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ComponentQuestionnaireDataTest { + private static String VSP_ID = "vspId"; + public static final Version VERSION01 = new Version(0, 1); + private static final Version VERSION10 = new Version(1, 0); + + @Mock + private ComponentDao componentDaoMock; + + @Mock + private ComponentDependencyModelDao componentDependencyDaoMock; + + @InjectMocks + private static ComponentQuestionnaireData componentQuestionnaireData; + + @BeforeMethod + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testGetData() { + ComponentEntity componentEntity = new ComponentEntity(VSP_ID, VERSION01,"ID1" ); + componentEntity.setCompositionData("{\"name\": \"org.openecomp.resource.vfc.nodes.heat.be\"," + + "\"displayName\": \"be\", \"vfcCode\": \"be_1\"}"); + componentEntity.setQuestionnaireData + ("{\"highAvailabilityAndLoadBalancing\":{\"isComponentMandatory\" : \"NO\"," + + "\"highAvailabilityMode\":\"geo-activeactive\"},\"compute\":{\"numOfVMs\" " + + ":{\"maximum\" : 5, \"minimum\" : 0}}}"); + + List entitites = new ArrayList(); + entitites.add(componentEntity); + + doReturn(entitites).when(componentDaoMock).listCompositionAndQuestionnaire(VSP_ID, VERSION01); + + final Map> propertiesfromCompQuestionnaire = + componentQuestionnaireData.getPropertiesfromCompQuestionnaire(VSP_ID, VERSION01); + + final Map be = propertiesfromCompQuestionnaire.get("be"); + Assert.assertEquals(be.get(VFC_NAMING_CODE) , "be_1"); + Assert.assertEquals(be.get(MANDATORY) ,"NO"); + Assert.assertEquals(be.get(HIGH_AVAIL_MODE) ,"geo-activeactive"); + Assert.assertEquals(be.get(MIN_INSTANCES) ,null); + Assert.assertEquals(be.get(MAX_INSTANCES) ,5); + + final Map sourceToTargetComponent = + componentQuestionnaireData.getSourceToTargetComponent(); + + Assert.assertEquals("be", sourceToTargetComponent.get("ID1")); + } + + + @Test + public void testPopulateDepnendency() { + ComponentDependencyModelEntity sourceComponent = new ComponentDependencyModelEntity(VSP_ID, VERSION01,"ID1" ); + sourceComponent.setSourceComponentId("Comp1"); + sourceComponent.setTargetComponentId("Comp2"); + sourceComponent.setRelation("dependsOn"); + + ComponentDependencyModelEntity targetComponent = new ComponentDependencyModelEntity(VSP_ID, + VERSION01,"ID2" ); + targetComponent.setSourceComponentId("Comp1"); + targetComponent.setTargetComponentId("Comp3"); + targetComponent.setRelation("dependsOn"); + + List entitites = new ArrayList(); + entitites.add(sourceComponent); + entitites.add(targetComponent); + + doReturn(entitites).when(componentDependencyDaoMock).list(new ComponentDependencyModelEntity + (VSP_ID, VERSION01, null)); + + final Map sourceToTargetComponent = new HashMap(); + sourceToTargetComponent.put("Comp1", "fe"); + sourceToTargetComponent.put("Comp2", "be"); + sourceToTargetComponent.put("Comp3", "smp"); + final Map> dependencies = + componentQuestionnaireData.populateDependencies(VSP_ID, VERSION01, sourceToTargetComponent); + + List expectedTargets = new ArrayList(); + expectedTargets.add("be"); expectedTargets.add("smp"); + + Assert.assertEquals(dependencies.get("fe"), expectedTargets); + + + + } + +} -- cgit 1.2.3-korg