diff options
Diffstat (limited to 'catalog-be-plugins/etsi-nfv-nsd-csar-plugin/src/test')
6 files changed, 710 insertions, 0 deletions
diff --git a/catalog-be-plugins/etsi-nfv-nsd-csar-plugin/src/test/java/org/openecomp/sdc/be/plugins/etsi/nfv/nsd/generator/EtsiNfvNsCsarEntryGeneratorTest.java b/catalog-be-plugins/etsi-nfv-nsd-csar-plugin/src/test/java/org/openecomp/sdc/be/plugins/etsi/nfv/nsd/generator/EtsiNfvNsCsarEntryGeneratorTest.java new file mode 100644 index 0000000000..e02e17034e --- /dev/null +++ b/catalog-be-plugins/etsi-nfv-nsd-csar-plugin/src/test/java/org/openecomp/sdc/be/plugins/etsi/nfv/nsd/generator/EtsiNfvNsCsarEntryGeneratorTest.java @@ -0,0 +1,125 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.anEmptyMap; +import static org.hamcrest.Matchers.hasEntry; +import static org.hamcrest.core.Is.is; +import static org.mockito.Mockito.when; +import static org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator.EtsiNfvNsCsarEntryGenerator.ETSI_NS_COMPONENT_CATEGORY; +import static org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator.EtsiNfvNsCsarEntryGenerator.NSD_FILE_PATH_FORMAT; +import static org.openecomp.sdc.common.api.ArtifactTypeEnum.ETSI_PACKAGE; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +import org.openecomp.sdc.be.model.Service; +import org.openecomp.sdc.be.model.category.CategoryDefinition; +import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.exception.NsdException; + +class EtsiNfvNsCsarEntryGeneratorTest { + + + @Mock + private EtsiNfvNsdCsarGenerator etsiNfvNsdCsarGenerator; + @Mock + private Service service; + @InjectMocks + private EtsiNfvNsCsarEntryGenerator etsiNfvNsCsarEntryGenerator; + + private static final String SERVICE_NORMALIZED_NAME = "normalizedName"; + private static final String CSAR_ENTRY_EMPTY_ASSERT = "Csar Entries should be empty"; + + @BeforeEach + void setUp() { + MockitoAnnotations.initMocks(this); + } + + @Test + void successfullyEntryGenerationTest() throws NsdException { + mockServiceComponent(); + final byte[] expectedNsdCsar = new byte[5]; + when(etsiNfvNsdCsarGenerator.generateNsdCsar(service)).thenReturn(expectedNsdCsar); + + final Map<String, byte[]> entryMap = etsiNfvNsCsarEntryGenerator.generateCsarEntries(service); + + assertThat("Csar Entries should contain only one entry", entryMap.size(), is(1)); + assertThat("Csar Entries should contain the expected entry", entryMap, + hasEntry(String.format(NSD_FILE_PATH_FORMAT, ETSI_PACKAGE, SERVICE_NORMALIZED_NAME), expectedNsdCsar)); + } + + @Test + void knownNsdGenerationErrorTest() throws NsdException { + mockServiceComponent(); + when(etsiNfvNsdCsarGenerator.generateNsdCsar(service)).thenThrow(new NsdException("")); + final Map<String, byte[]> entryMap = etsiNfvNsCsarEntryGenerator.generateCsarEntries(service); + + assertThat(CSAR_ENTRY_EMPTY_ASSERT, entryMap, is(anEmptyMap())); + } + + @Test + void unknownNsdGenerationErrorTest() throws NsdException { + mockServiceComponent(); + when(etsiNfvNsdCsarGenerator.generateNsdCsar(service)).thenThrow(new RuntimeException()); + final Map<String, byte[]> entryMap = etsiNfvNsCsarEntryGenerator.generateCsarEntries(service); + + assertThat(CSAR_ENTRY_EMPTY_ASSERT, entryMap, is(anEmptyMap())); + } + + @Test + void componentNullOrNotAServiceTest() { + Map<String, byte[]> entryMap = etsiNfvNsCsarEntryGenerator.generateCsarEntries(service); + assertThat(CSAR_ENTRY_EMPTY_ASSERT, entryMap, is(anEmptyMap())); + + entryMap = etsiNfvNsCsarEntryGenerator.generateCsarEntries(null); + assertThat(CSAR_ENTRY_EMPTY_ASSERT, entryMap, is(anEmptyMap())); + } + + @Test + void componentNotExpectedCategoryTest() { + when(service.getComponentType()).thenReturn(ComponentTypeEnum.SERVICE); + final List<CategoryDefinition> categoryDefinitionList = new ArrayList<>(); + final CategoryDefinition nsComponentCategoryDefinition = new CategoryDefinition(); + nsComponentCategoryDefinition.setName("notExpectedCategory"); + categoryDefinitionList.add(nsComponentCategoryDefinition); + when(service.getCategories()).thenReturn(categoryDefinitionList); + final Map<String, byte[]> entryMap = etsiNfvNsCsarEntryGenerator.generateCsarEntries(service); + assertThat(CSAR_ENTRY_EMPTY_ASSERT, entryMap, is(anEmptyMap())); + } + + private void mockServiceComponent() { + when(service.getName()).thenReturn("anyName"); + when(service.getComponentType()).thenReturn(ComponentTypeEnum.SERVICE); + when(service.getNormalizedName()).thenReturn(SERVICE_NORMALIZED_NAME); + + final List<CategoryDefinition> categoryDefinitionList = new ArrayList<>(); + final CategoryDefinition nsComponentCategoryDefinition = new CategoryDefinition(); + nsComponentCategoryDefinition.setName(ETSI_NS_COMPONENT_CATEGORY); + categoryDefinitionList.add(nsComponentCategoryDefinition); + when(service.getCategories()).thenReturn(categoryDefinitionList); + } +}
\ No newline at end of file diff --git a/catalog-be-plugins/etsi-nfv-nsd-csar-plugin/src/test/java/org/openecomp/sdc/be/plugins/etsi/nfv/nsd/generator/EtsiNfvNsdCsarGeneratorImplTest.java b/catalog-be-plugins/etsi-nfv-nsd-csar-plugin/src/test/java/org/openecomp/sdc/be/plugins/etsi/nfv/nsd/generator/EtsiNfvNsdCsarGeneratorImplTest.java new file mode 100644 index 0000000000..9144140f9a --- /dev/null +++ b/catalog-be-plugins/etsi-nfv-nsd-csar-plugin/src/test/java/org/openecomp/sdc/be/plugins/etsi/nfv/nsd/generator/EtsiNfvNsdCsarGeneratorImplTest.java @@ -0,0 +1,164 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsNull.notNullValue; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator.EtsiNfvNsCsarEntryGenerator.ETSI_NS_COMPONENT_CATEGORY; +import static org.openecomp.sdc.common.api.ArtifactTypeEnum.ONBOARDED_PACKAGE; + +import fj.data.Either; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao; +import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; +import org.openecomp.sdc.be.model.ArtifactDefinition; +import org.openecomp.sdc.be.model.ComponentInstance; +import org.openecomp.sdc.be.model.Service; +import org.openecomp.sdc.be.model.category.CategoryDefinition; +import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.exception.NsdException; +import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.exception.VnfDescriptorException; +import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.model.Nsd; +import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.model.VnfDescriptor; +import org.openecomp.sdc.be.resources.data.DAOArtifactData; + +class EtsiNfvNsdCsarGeneratorImplTest { + + @Mock + private VnfDescriptorGenerator vnfDescriptorGenerator; + @Mock + private NsDescriptorGenerator nsDescriptorGeneratorImpl; + @Mock + private ArtifactCassandraDao artifactCassandraDao; + @InjectMocks + private EtsiNfvNsdCsarGeneratorImpl etsiNfvNsdCsarGenerator; + @Mock + private Service service; + + private static final String SERVICE_NORMALIZED_NAME = "normalizedName"; + + @BeforeEach + void setUp() { + MockitoAnnotations.initMocks(this); + } + + @Test + void generateNsdCsarSuccessfulTest() throws VnfDescriptorException, NsdException { + mockServiceComponent(); + mockServiceComponentArtifacts(); + final byte[] nsdCsar = etsiNfvNsdCsarGenerator.generateNsdCsar(service); + assertThat("", nsdCsar, is(notNullValue())); + } + + @Test() + void invalidComponentTest() { + assertThrows(NsdException.class, () -> etsiNfvNsdCsarGenerator.generateNsdCsar(null)); + } + + private void mockServiceComponent() throws VnfDescriptorException, NsdException { + when(service.getNormalizedName()).thenReturn(SERVICE_NORMALIZED_NAME); + when(service.getComponentType()).thenReturn(ComponentTypeEnum.SERVICE); + final String componentInstance1Name = "componentInstance1"; + final ComponentInstance componentInstance1 = mockServiceComponentInstance(componentInstance1Name); + final ArtifactDefinition instanceArtifact1 = mockComponentInstanceArtifact(componentInstance1, + "instanceArtifact1"); + + final VnfDescriptor vnfDescriptor1 = new VnfDescriptor(); + final List<VnfDescriptor> vnfDescriptorList = Collections.singletonList(vnfDescriptor1); + final Nsd nsd = new Nsd(); + when(vnfDescriptorGenerator.generate(componentInstance1Name, instanceArtifact1)) + .thenReturn(Optional.of(vnfDescriptor1)); + when(nsDescriptorGeneratorImpl.generate(service, vnfDescriptorList)).thenReturn(Optional.of(nsd)); + + final List<CategoryDefinition> categoryDefinitionList = new ArrayList<>(); + final CategoryDefinition nsComponentCategoryDefinition = new CategoryDefinition(); + nsComponentCategoryDefinition.setName(ETSI_NS_COMPONENT_CATEGORY); + categoryDefinitionList.add(nsComponentCategoryDefinition); + when(service.getCategories()).thenReturn(categoryDefinitionList); + } + + private void mockServiceComponentArtifacts() { + final Map<String, ArtifactDefinition> allArtifactsMap = new HashMap<>(); + final String artifact1Id = "artifact1"; + final ArtifactDefinition artifact1 = mockArtifactDefinition(artifact1Id); + final byte[] artifact1Bytes = new byte[1]; + allArtifactsMap.put(artifact1Id, artifact1); + when(service.getAllArtifacts()).thenReturn(allArtifactsMap); + final DAOArtifactData artifact1Data = new DAOArtifactData(); + artifact1Data.setDataAsArray(artifact1Bytes); + when(artifactCassandraDao.getArtifact(artifact1Id)).thenReturn(Either.left(artifact1Data)); + } + + private ComponentInstance mockServiceComponentInstance(final String componentInstanceName) { + final Map<String, ArtifactDefinition> deploymentArtifactMap = new HashMap<>(); + final String instanceArtifact1Id = "instanceArtifact1"; + final ArtifactDefinition instanceArtifact1 = mockArtifactDefinition(instanceArtifact1Id); + instanceArtifact1.setToscaPresentationValue(JsonPresentationFields.ARTIFACT_TYPE, ONBOARDED_PACKAGE.getType()); + deploymentArtifactMap.put(instanceArtifact1Id, instanceArtifact1); + DAOArtifactData instanceArtifact1Data = new DAOArtifactData(); + final byte[] instanceArtifact1Bytes = new byte[1]; + instanceArtifact1Data.setDataAsArray(instanceArtifact1Bytes); + when(artifactCassandraDao.getArtifact(instanceArtifact1Id)).thenReturn(Either.left(instanceArtifact1Data)); + final ComponentInstance componentInstance = mock(ComponentInstance.class); + when(componentInstance.getDeploymentArtifacts()).thenReturn(deploymentArtifactMap); + when(componentInstance.getName()).thenReturn(componentInstanceName); + final List<ComponentInstance> componentInstanceList = new ArrayList<>(); + componentInstanceList.add(componentInstance); + when(service.getComponentInstances()).thenReturn(componentInstanceList); + + return componentInstance; + } + + private ArtifactDefinition mockComponentInstanceArtifact(final ComponentInstance componentInstance, + final String instanceArtifactId) { + final Map<String, ArtifactDefinition> deploymentArtifactMap = new HashMap<>(); + when(componentInstance.getDeploymentArtifacts()).thenReturn(deploymentArtifactMap); + + final ArtifactDefinition instanceArtifact1 = mockArtifactDefinition(instanceArtifactId); + instanceArtifact1.setToscaPresentationValue(JsonPresentationFields.ARTIFACT_TYPE, ONBOARDED_PACKAGE.getType()); + deploymentArtifactMap.put(instanceArtifactId, instanceArtifact1); + DAOArtifactData instanceArtifact1Data = new DAOArtifactData(); + final byte[] instanceArtifact1Bytes = new byte[1]; + instanceArtifact1Data.setDataAsArray(instanceArtifact1Bytes); + when(artifactCassandraDao.getArtifact(instanceArtifactId)).thenReturn(Either.left(instanceArtifact1Data)); + return instanceArtifact1; + } + + private ArtifactDefinition mockArtifactDefinition(final String artifactId) { + final ArtifactDefinition artifact = new ArtifactDefinition(); + artifact.setEsId(artifactId); + + return artifact; + } +}
\ No newline at end of file diff --git a/catalog-be-plugins/etsi-nfv-nsd-csar-plugin/src/test/java/org/openecomp/sdc/be/plugins/etsi/nfv/nsd/generator/NsDescriptorGeneratorImplTest.java b/catalog-be-plugins/etsi-nfv-nsd-csar-plugin/src/test/java/org/openecomp/sdc/be/plugins/etsi/nfv/nsd/generator/NsDescriptorGeneratorImplTest.java new file mode 100644 index 0000000000..7ed7b7d357 --- /dev/null +++ b/catalog-be-plugins/etsi-nfv-nsd-csar-plugin/src/test/java/org/openecomp/sdc/be/plugins/etsi/nfv/nsd/generator/NsDescriptorGeneratorImplTest.java @@ -0,0 +1,240 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.anEmptyMap; +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsNull.notNullValue; +import static org.hamcrest.core.IsNull.nullValue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import fj.data.Either; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.openecomp.sdc.be.config.Configuration; +import org.openecomp.sdc.be.config.ConfigurationManager; +import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.exception.NsdException; +import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.model.Nsd; +import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.model.VnfDescriptor; +import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.tosca.yaml.ToscaTemplateYamlGenerator; +import org.openecomp.sdc.be.tosca.ToscaExportHandler; +import org.openecomp.sdc.be.tosca.model.SubstitutionMapping; +import org.openecomp.sdc.be.tosca.model.ToscaNodeTemplate; +import org.openecomp.sdc.be.tosca.model.ToscaNodeType; +import org.openecomp.sdc.be.tosca.model.ToscaProperty; +import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraint; +import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintValidValues; +import org.openecomp.sdc.be.tosca.model.ToscaTemplate; +import org.openecomp.sdc.be.tosca.model.ToscaTemplateCapability; +import org.openecomp.sdc.be.tosca.model.ToscaTopolgyTemplate; +import org.openecomp.sdc.common.api.ConfigurationSource; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.ObjectProvider; +import org.yaml.snakeyaml.Yaml; + +class NsDescriptorGeneratorImplTest { + + private static final String VNFD_AMF_NODE_NAME = "vnfd_amf"; + private static final String VIRTUAL_LINK_REQUIREMENT_NAME = "virtual_link"; + + @Mock + private ToscaExportHandler toscaExportHandler; + + private final ObjectProvider<ToscaTemplateYamlGenerator> toscaTemplateYamlGeneratorProvider = new ObjectProvider<ToscaTemplateYamlGenerator>() { + @Override + public ToscaTemplateYamlGenerator getObject(Object... args) { + return new ToscaTemplateYamlGenerator((ToscaTemplate) args[0]); + } + + @Override + public ToscaTemplateYamlGenerator getIfAvailable() { + return null; + } + + @Override + public ToscaTemplateYamlGenerator getIfUnique() { + return null; + } + + @Override + public ToscaTemplateYamlGenerator getObject() { + return null; + } + }; + + private NsDescriptorGeneratorImpl nsDescriptorGenerator; + + @BeforeEach + void setUp() { + setUpConfigurationMock(); + MockitoAnnotations.initMocks(this); + nsDescriptorGenerator = new NsDescriptorGeneratorImpl(toscaExportHandler, toscaTemplateYamlGeneratorProvider); + } + + private void setUpConfigurationMock() { + final List<Map<String, Map<String, String>>> defaultImports = new ArrayList<>(); + final Map<String, Map<String, String>> importMap = new HashMap<>(); + final Map<String, String> nodeImportEntry = new HashMap<>(); + nodeImportEntry.put("file", "nodes.yml"); + importMap.put("nodes", nodeImportEntry); + defaultImports.add(importMap); + + final ConfigurationSource configurationSource = mock(ConfigurationSource.class); + final Configuration configuration = new Configuration(); + configuration.setDefaultImports(defaultImports); + configuration.setHeatEnvArtifactHeader(""); + configuration.setHeatEnvArtifactFooter(""); + when(configurationSource.getAndWatchConfiguration(any(), any())).thenReturn(configuration); + new ConfigurationManager(configurationSource); + } + + @Test + void testGenerate() throws IOException, NsdException { + //given + final Component component = mock(Component.class); + when(component.getComponentType()).thenReturn(ComponentTypeEnum.SERVICE); + final ToscaTemplate componentToscaTemplate = new ToscaTemplate(""); + final ToscaTopolgyTemplate componentToscaTopologyTemplate = new ToscaTopolgyTemplate(); + componentToscaTemplate.setTopology_template(componentToscaTopologyTemplate); + + final HashMap<String, ToscaNodeTemplate> nodeTemplateMap = new HashMap<>(); + final ToscaNodeTemplate vnfAmfNodeTemplate = new ToscaNodeTemplate(); + vnfAmfNodeTemplate.setType("com.ericsson.resource.abstract.Ericsson.AMF"); + //a property to be excluded + vnfAmfNodeTemplate.setProperties(ImmutableMap.of("nf_naming_code", new ToscaProperty())); + //a property that wont be excluded + vnfAmfNodeTemplate.setProperties(ImmutableMap.of("will_not_be_excluded", new ToscaProperty())); + nodeTemplateMap.put(VNFD_AMF_NODE_NAME, vnfAmfNodeTemplate); + + final Map<String, ToscaTemplateCapability> vnfAmfCapabilities = new HashMap<>(); + vnfAmfCapabilities.put("myCapability", new ToscaTemplateCapability()); + vnfAmfNodeTemplate.setCapabilities(vnfAmfCapabilities); + componentToscaTopologyTemplate.setNode_templates(nodeTemplateMap); + + final SubstitutionMapping substitutionMapping = mock(SubstitutionMapping.class); + Map<String, String[]> requirements = new HashMap<>(); + String[] requirementAssignment = {"VNF1", VIRTUAL_LINK_REQUIREMENT_NAME}; + requirements.put(VIRTUAL_LINK_REQUIREMENT_NAME, requirementAssignment); + when(substitutionMapping.getRequirements()).thenReturn(requirements); + Map<String, String[]> capabilities = new HashMap<>(); + String[] capabilitiesAssignment = {"VNF1", "capability1"}; + capabilities.put("capability", capabilitiesAssignment); + when(substitutionMapping.getCapabilities()).thenReturn(capabilities); + componentToscaTopologyTemplate.setSubstitution_mappings(substitutionMapping); + + final ToscaTemplate componentInterfaceToscaTemplate = new ToscaTemplate(""); + final ToscaNodeType interfaceToscaNodeType = new ToscaNodeType(); + interfaceToscaNodeType.setProperties( + ImmutableMap.of("designer", createToscaProperty("designerValue"), + "version", createToscaProperty("versionValue"), + "name", createToscaProperty("nameValue"), + "invariant_id", createToscaProperty("invariantIdValue")) + ); + final String nsNodeTypeName = "nsNodeTypeName"; + componentInterfaceToscaTemplate.setNode_types(ImmutableMap.of(nsNodeTypeName, interfaceToscaNodeType)); + + + when(toscaExportHandler.convertToToscaTemplate(component)).thenReturn(Either.left(componentToscaTemplate)); + when(toscaExportHandler.convertInterfaceNodeType(any(), any(), any(), any(), anyBoolean())) + .thenReturn(Either.left(componentInterfaceToscaTemplate)); + + final List<VnfDescriptor> vnfDescriptorList = new ArrayList<>(); + VnfDescriptor vnfDescriptor1 = new VnfDescriptor(); + vnfDescriptor1.setName(VNFD_AMF_NODE_NAME); + vnfDescriptor1.setVnfdFileName("vnfd_amf.yaml"); + vnfDescriptor1.setNodeType("com.ericsson.resource.abstract.Ericsson.AMF"); + + vnfDescriptorList.add(vnfDescriptor1); + + //when + final Nsd nsd = nsDescriptorGenerator.generate(component, vnfDescriptorList).orElse(null); + //then + assertThat("Nsd should not be null", nsd, is(notNullValue())); + assertThat("Nsd designer should be as expected", nsd.getDesigner(), is("designerValue")); + assertThat("Nsd version should be as expected", nsd.getVersion(), is("versionValue")); + assertThat("Nsd name should be as expected", nsd.getName(), is("nameValue")); + assertThat("Nsd invariantId should be as expected", nsd.getInvariantId(), is("invariantIdValue")); + assertThat("Nsd content should not be empty", nsd.getContents(), is(notNullValue())); + assertThat("Nsd content should not be empty", nsd.getContents().length, is(greaterThan(0))); + + final Map<String, Object> toscaTemplateYaml = readYamlAsMap(nsd.getContents()); + @SuppressWarnings("unchecked") + final Map<String, Object> topologyTemplate = (Map<String, Object>) toscaTemplateYaml.get("topology_template"); + assertThat("topology_template should not be empty", topologyTemplate, is(not(anEmptyMap()))); + @SuppressWarnings("unchecked") + final Map<String, Object> substitutionMappings = + (Map<String, Object>) topologyTemplate.get("substitution_mappings"); + assertThat("substitution_mappings should not be empty", substitutionMappings, is(not(anEmptyMap()))); + assertThat("substitution_mappings->node_type should not be null", + substitutionMappings.get("node_type"), is(notNullValue())); + assertThat("substitution_mappings->node_type should be as expected", + substitutionMappings.get("node_type"), is(nsNodeTypeName)); + + final Map<String, List<String>> subMappingRequirements = (Map<String, List<String>>) substitutionMappings.get("requirements"); + assertThat(subMappingRequirements.get(VIRTUAL_LINK_REQUIREMENT_NAME).get(0), is("VNF1")); + assertThat(subMappingRequirements.get(VIRTUAL_LINK_REQUIREMENT_NAME).get(1), is(VIRTUAL_LINK_REQUIREMENT_NAME)); + final Map<String, List<String>> subMappingCapabilities = (Map<String, List<String>>) substitutionMappings.get("capabilities"); + assertThat(subMappingCapabilities.get("capability").get(0), is("VNF1")); + assertThat(subMappingCapabilities.get("capability").get(1), is("capability1")); + + @SuppressWarnings("unchecked") + final Map<String, Object> nodeTemplates = + (Map<String, Object>) topologyTemplate.get("node_templates"); + @SuppressWarnings("unchecked") + final Map<String, Object> nodeTemplate = + (Map<String, Object>) nodeTemplates.get(VNFD_AMF_NODE_NAME); + assertThat("capabilities should be null", + nodeTemplate.get("capabilities"), is(nullValue())); + } + + private ToscaProperty createToscaProperty(final String value) { + final ToscaProperty toscaProperty = new ToscaProperty(); + final ToscaPropertyConstraint toscaPropertyConstraint = + new ToscaPropertyConstraintValidValues(ImmutableList.of(value)); + toscaProperty.setConstraints(ImmutableList.of(toscaPropertyConstraint)); + return toscaProperty; + } + + @SuppressWarnings("unchecked") + private Map<String, Object> readYamlAsMap(final byte[] yamlContents) throws IOException { + final Yaml yaml = new Yaml(); + try (final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(yamlContents)) { + return (Map<String, Object>) yaml.load(byteArrayInputStream); + } + } +}
\ No newline at end of file diff --git a/catalog-be-plugins/etsi-nfv-nsd-csar-plugin/src/test/java/org/openecomp/sdc/be/plugins/etsi/nfv/nsd/generator/VnfDescriptorGeneratorImplTest.java b/catalog-be-plugins/etsi-nfv-nsd-csar-plugin/src/test/java/org/openecomp/sdc/be/plugins/etsi/nfv/nsd/generator/VnfDescriptorGeneratorImplTest.java new file mode 100644 index 0000000000..e223fda962 --- /dev/null +++ b/catalog-be-plugins/etsi-nfv-nsd-csar-plugin/src/test/java/org/openecomp/sdc/be/plugins/etsi/nfv/nsd/generator/VnfDescriptorGeneratorImplTest.java @@ -0,0 +1,83 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsNull.notNullValue; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Path; +import java.nio.file.Paths; +import org.apache.commons.io.IOUtils; +import org.junit.jupiter.api.Test; +import org.openecomp.sdc.be.model.ArtifactDefinition; +import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.exception.VnfDescriptorException; +import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.model.VnfDescriptor; + +class VnfDescriptorGeneratorImplTest { + + private final VnfDescriptorGeneratorImpl vnfDescriptorGenerator = new VnfDescriptorGeneratorImpl(); + private final Path testResourcesPath = Paths.get("src", "test", "resources", "vnf-onboarded-csar"); + + @Test + void testGenerate() throws IOException, VnfDescriptorException { + final byte[] onboardedPackage = getResourceAsByteArray("VnfPackage_AMF_v2.csar"); + final ArtifactDefinition artifactDefinition = new ArtifactDefinition(); + artifactDefinition.setPayload(onboardedPackage); + artifactDefinition.setArtifactName("vnf-onboarded-csar.csar"); + final String vnfDescriptorName = "vnf-onboarded-csar"; + final VnfDescriptor vnfDescriptor = vnfDescriptorGenerator + .generate(vnfDescriptorName, artifactDefinition).orElse(null); + final String expectedNodeType = "com.ericsson.resource.abstract.Ericsson.AMF"; + final String expectedVnfdFileName = "vnfd_amf.yaml"; + assertThat("Vnf Descriptor should be present", vnfDescriptor, is(notNullValue())); + assertThat("Vnf Descriptor should have the expected name", vnfDescriptor.getName(), + is(vnfDescriptorName)); + assertThat("Vnf Descriptor should have the expected node type", vnfDescriptor.getNodeType(), + is(expectedNodeType)); + assertThat("Vnf Descriptor should have the expected vnfd file name", vnfDescriptor.getVnfdFileName(), + is(expectedVnfdFileName)); + assertThat("Vnf Descriptor should contain the expected definition files count", + vnfDescriptor.getDefinitionFiles().size(), is(2)); + assertThat("Vnf Descriptor should contain the expected definition entries", + vnfDescriptor.getDefinitionFiles().keySet(), contains("Definitions/vnfd_amf.yaml", + "Definitions/etsi_nfv_sol001_vnfd_2_5_1_types.yaml")); + } + + private byte[] getResourceAsByteArray(final String filename) throws IOException { + try (final InputStream inputStream = readFileAsStream(filename)) { + return IOUtils.toByteArray(inputStream); + } catch (final IOException ex) { + throw new IOException( + String.format("Could not read the file \"%s\"", filename), ex); + } + } + + private FileInputStream readFileAsStream(final String fileName) throws FileNotFoundException { + final Path path = Paths.get(testResourcesPath.toString(), fileName); + return new FileInputStream(path.toFile()); + } + +}
\ No newline at end of file diff --git a/catalog-be-plugins/etsi-nfv-nsd-csar-plugin/src/test/java/org/openecomp/sdc/be/plugins/etsi/nfv/nsd/tosca/yaml/ToscaTemplateYamlGeneratorTest.java b/catalog-be-plugins/etsi-nfv-nsd-csar-plugin/src/test/java/org/openecomp/sdc/be/plugins/etsi/nfv/nsd/tosca/yaml/ToscaTemplateYamlGeneratorTest.java new file mode 100644 index 0000000000..5341598ad1 --- /dev/null +++ b/catalog-be-plugins/etsi-nfv-nsd-csar-plugin/src/test/java/org/openecomp/sdc/be/plugins/etsi/nfv/nsd/tosca/yaml/ToscaTemplateYamlGeneratorTest.java @@ -0,0 +1,98 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.plugins.etsi.nfv.nsd.tosca.yaml; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.Test; +import org.openecomp.sdc.be.tosca.model.ToscaNodeType; +import org.openecomp.sdc.be.tosca.model.ToscaProperty; +import org.openecomp.sdc.be.tosca.model.ToscaPropertyConstraintValidValues; +import org.openecomp.sdc.be.tosca.model.ToscaTemplate; + +class ToscaTemplateYamlGeneratorTest { + + @Test + void testGenerateYamlWithImportsKey() { + //given + final ToscaTemplate toscaTemplate = new ToscaTemplate("tosca_simple_yaml_1_1"); + final List<Map<String, Map<String, String>>> importList = + ImmutableList.of( + ImmutableMap.of("etsi_nfv_sol001_nsd_2_7_1_types", + ImmutableMap.of("file", "etsi_nfv_sol001_nsd_2_7_1_types.yaml") + ), + ImmutableMap.of("anotherImport", + ImmutableMap.of("file", "anotherImport.yaml") + ) + ); + toscaTemplate.setImports(importList); + final ToscaTemplateYamlGenerator toscaTemplateYamlGenerator = new ToscaTemplateYamlGenerator(toscaTemplate); + //when + final String toscaTemplateYamlString = toscaTemplateYamlGenerator.parseToYamlString(); + + //then + final String expectedImports = "imports:\n" + + "- file: etsi_nfv_sol001_nsd_2_7_1_types.yaml\n" + + "- file: anotherImport.yaml"; + assertThat("Imports format should be as expected", toscaTemplateYamlString.contains(expectedImports), is(true)); + } + + @Test + void testGenerateYamlWithToscaProperty() { + //given + final ToscaTemplate toscaTemplate = new ToscaTemplate("tosca_simple_yaml_1_1"); + + final Map<String, ToscaProperty> toscaPropertyMap = new HashMap<>(); + final ToscaProperty toscaProperty = new ToscaProperty(); + final String defaultpValue = "defaultpValue"; + toscaProperty.setDefaultp(defaultpValue); + ToscaPropertyConstraintValidValues toscaPropertyConstraintValidValues = + new ToscaPropertyConstraintValidValues(Collections.singletonList(defaultpValue)); + toscaProperty.setConstraints(Collections.singletonList(toscaPropertyConstraintValidValues)); + final String propertyName = "aProperty"; + toscaPropertyMap.put(propertyName, toscaProperty); + + final Map<String, ToscaNodeType> toscaNodeMap = new HashMap<>(); + final ToscaNodeType toscaNodeType = new ToscaNodeType(); + toscaNodeType.setProperties(toscaPropertyMap); + toscaNodeMap.put("aNode", toscaNodeType); + toscaTemplate.setNode_types(toscaNodeMap); + final ToscaTemplateYamlGenerator toscaTemplateYamlGenerator = new ToscaTemplateYamlGenerator(toscaTemplate); + //when + final String toscaTemplateYamlString = toscaTemplateYamlGenerator.parseToYamlString(); + + final String expectedProperty = String.format("%s:\n" + + " default: %s\n" + + " constraints:\n" + + " - valid_values:\n" + + " - %s", + propertyName, defaultpValue, defaultpValue); + //then + assertThat("Property format should be as expected", + toscaTemplateYamlString.contains(expectedProperty), is(true)); + } +}
\ No newline at end of file diff --git a/catalog-be-plugins/etsi-nfv-nsd-csar-plugin/src/test/resources/vnf-onboarded-csar/VnfPackage_AMF_v2.csar b/catalog-be-plugins/etsi-nfv-nsd-csar-plugin/src/test/resources/vnf-onboarded-csar/VnfPackage_AMF_v2.csar Binary files differnew file mode 100644 index 0000000000..1663f97399 --- /dev/null +++ b/catalog-be-plugins/etsi-nfv-nsd-csar-plugin/src/test/resources/vnf-onboarded-csar/VnfPackage_AMF_v2.csar |