From 49a1e7a2d020b9413e08ba34d2af83c1c6604d54 Mon Sep 17 00:00:00 2001 From: "mark.j.leonard" Date: Thu, 6 Sep 2018 13:12:23 +0100 Subject: [PATCH] Additional tests for VNF image extraction New tests for extracting software versions. Change-Id: I26a9d654d03154a1d026741e623c8f953946a5f4 Issue-ID: AAI-1250 Signed-off-by: mark.j.leonard --- .../csar/vnfcatalog/VnfVendorImageExtractor.java | 3 +- .../aai/babel/csar/vnfcatalog/SdcToscaHelper.java | 123 +++++++++++++++++++++ .../vnfcatalog/VnfVendorImageExtractorTest.java | 53 +++++++++ .../service/TestGenerateArtifactsServiceImpl.java | 8 ++ .../java/org/onap/aai/babel/testdata/CsarTest.java | 1 - .../aai/babel/xml/generator/model/TestModel.java | 21 +++- 6 files changed, 201 insertions(+), 8 deletions(-) create mode 100644 src/test/java/org/onap/aai/babel/csar/vnfcatalog/SdcToscaHelper.java diff --git a/src/main/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractor.java b/src/main/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractor.java index d98c622..247dfee 100644 --- a/src/main/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractor.java +++ b/src/main/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractor.java @@ -30,7 +30,6 @@ import java.util.Map.Entry; import java.util.Objects; import java.util.function.Consumer; import java.util.stream.Collectors; - import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.time.StopWatch; import org.apache.commons.lang3.tuple.ImmutablePair; @@ -239,7 +238,7 @@ public class VnfVendorImageExtractor { } @SuppressWarnings("unchecked") - private List extractSoftwareVersions(SubstitutionMappings sm) throws ToscaToCatalogException { + List extractSoftwareVersions(SubstitutionMappings sm) throws ToscaToCatalogException { applicationLogger.debug("Trying to extract the software versions for the vnf configuration"); List imagesNodes = sm.getNodeTemplates().stream() diff --git a/src/test/java/org/onap/aai/babel/csar/vnfcatalog/SdcToscaHelper.java b/src/test/java/org/onap/aai/babel/csar/vnfcatalog/SdcToscaHelper.java new file mode 100644 index 0000000..9fcc5a4 --- /dev/null +++ b/src/test/java/org/onap/aai/babel/csar/vnfcatalog/SdcToscaHelper.java @@ -0,0 +1,123 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 European Software Marketing Ltd. + * ================================================================================ + * 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.onap.aai.babel.csar.vnfcatalog; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import org.onap.sdc.toscaparser.api.NodeTemplate; +import org.onap.sdc.toscaparser.api.SubstitutionMappings; + +public class SdcToscaHelper { + + private ArrayList smnodetemplates = new ArrayList<>(); + + /** + * @return + */ + public SubstitutionMappings buildMappings() { + LinkedHashMap defProps = getImagesDefProps(); + + LinkedHashMap defs = buildNodeTemplateTypeInfo(defProps); + LinkedHashMap caps = new LinkedHashMap<>(); + LinkedHashMap reqs = new LinkedHashMap<>(); + + String type = "tosca.nodes.custom"; + + LinkedHashMap smsubMappingDef = new LinkedHashMap<>(); + smsubMappingDef.put("node_type", type); + smsubMappingDef.put("capabilities", caps); + smsubMappingDef.put("requirements", reqs); + + LinkedHashMap smcustomDefs = buildCustomTypeDefinitions(type, defs); + + return new SubstitutionMappings(smsubMappingDef, smnodetemplates, null, null, null, null, smcustomDefs); + } + + private LinkedHashMap getImagesDefProps() { + LinkedHashMap imagesDef = new LinkedHashMap<>(); + imagesDef.put("type", "map"); + imagesDef.put("required", false); + imagesDef.put("entry_schema", "{type=org.openecomp.datatypes.ImageInfo}"); + + LinkedHashMap defProps = new LinkedHashMap<>(); + defProps.put("images", imagesDef); + return defProps; + } + + private LinkedHashMap buildCustomTypeDefinitions(String type, + LinkedHashMap typeInfo) { + LinkedHashMap customDefs = new LinkedHashMap<>(); + customDefs.put(type, typeInfo); + return customDefs; + } + + private LinkedHashMap buildNodeTemplateTypeInfo(LinkedHashMap props) { + LinkedHashMap typeInfo = new LinkedHashMap<>(); + typeInfo.put("derived_from", "tosca.nodes.Root"); + typeInfo.put("properties", props); + return typeInfo; + } + + /** + * + */ + public void addNodeTemplate() { + String name = "node name"; + String type = "tosca.nodes.custom"; + + LinkedHashMap nodeTemplate = new LinkedHashMap<>(); + nodeTemplate.put("type", type); + nodeTemplate.put("properties", null); + + LinkedHashMap ntnodeTemplates = buildCustomTypeDefinitions(name, nodeTemplate); + ntnodeTemplates.put("derived_from", null); + ntnodeTemplates.put("properties", getImagesDefProps()); + + LinkedHashMap typeInfo = buildNodeTemplateTypeInfo(getImagesDefProps()); + LinkedHashMap customDefs = buildCustomTypeDefinitions(type, typeInfo); + smnodetemplates.add(new NodeTemplate(name, ntnodeTemplates, customDefs, null, null)); + } + + /** + * @param images + */ + public void addNodeTemplate(Object images) { + LinkedHashMap properties = new LinkedHashMap<>(); + properties.put("images", images); + + String type = "tosca.nodes.custom"; + LinkedHashMap nodeTemplate = new LinkedHashMap<>(); + nodeTemplate.put("type", type); + nodeTemplate.put("properties", properties); + + String name = "node name"; + LinkedHashMap ntnodeTemplates = buildCustomTypeDefinitions(name, nodeTemplate); + ntnodeTemplates.put("derived_from", null); + ntnodeTemplates.put("properties", getImagesDefProps()); + + LinkedHashMap typeInfo = buildNodeTemplateTypeInfo(getImagesDefProps()); + LinkedHashMap customDefs = buildCustomTypeDefinitions(type, typeInfo); + + smnodetemplates.add(new NodeTemplate(name, ntnodeTemplates, customDefs, null, null)); + } +} + diff --git a/src/test/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractorTest.java b/src/test/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractorTest.java index b1229b4..a6f98b8 100644 --- a/src/test/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractorTest.java +++ b/src/test/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractorTest.java @@ -21,6 +21,7 @@ package org.onap.aai.babel.csar.vnfcatalog; +import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; @@ -28,6 +29,9 @@ import static org.hamcrest.CoreMatchers.nullValue; import static org.junit.Assert.assertThat; import java.io.IOException; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; import org.junit.Test; import org.onap.aai.babel.service.data.BabelArtifact; import org.onap.aai.babel.service.data.BabelArtifact.ArtifactType; @@ -84,4 +88,53 @@ public class VnfVendorImageExtractorTest { assertThat(artifact.getPayload(), is(equalTo(new ArtifactTestUtils().getRequestJson("vnfVendorImageConfigurations.json")))); } + + @Test + public void testSoftwareVersions() throws ToscaToCatalogException { + VnfVendorImageExtractor extractor = new VnfVendorImageExtractor(); + SdcToscaHelper helper = new SdcToscaHelper(); + + List versions; + try { + versions = extractor.extractSoftwareVersions(helper.buildMappings()); + assertThat(versions.size(), is(0)); + } catch (ToscaToCatalogException e) { + assertThat(e.getMessage(), containsString("No software versions")); + } + + helper.addNodeTemplate(); + try { + versions = extractor.extractSoftwareVersions(helper.buildMappings()); + assertThat(versions.size(), is(0)); + } catch (ToscaToCatalogException e) { + assertThat(e.getMessage(), containsString("No software versions")); + } + + helper.addNodeTemplate("string"); + try { + versions = extractor.extractSoftwareVersions(helper.buildMappings()); + assertThat(versions.size(), is(0)); + } catch (ClassCastException e) { + assertThat(e.getMessage(), containsString("java.lang.String")); + } + + HashMap images = new LinkedHashMap<>(); + images.put("image", "string"); + helper.addNodeTemplate(images); + try { + versions = extractor.extractSoftwareVersions(helper.buildMappings()); + assertThat(versions.size(), is(1)); + } catch (ClassCastException e) { + assertThat(e.getMessage(), containsString("java.lang.String")); + } + + HashMap image = new LinkedHashMap<>(); + image.put("software_version", "1.2.3"); + images.put("image", image); + helper = new SdcToscaHelper(); + helper.addNodeTemplate(images); + versions = extractor.extractSoftwareVersions(helper.buildMappings()); + assertThat(versions.size(), is(1)); + assertThat(versions.get(0), is("1.2.3")); + } } diff --git a/src/test/java/org/onap/aai/babel/service/TestGenerateArtifactsServiceImpl.java b/src/test/java/org/onap/aai/babel/service/TestGenerateArtifactsServiceImpl.java index 5bd5484..599b3ff 100644 --- a/src/test/java/org/onap/aai/babel/service/TestGenerateArtifactsServiceImpl.java +++ b/src/test/java/org/onap/aai/babel/service/TestGenerateArtifactsServiceImpl.java @@ -155,6 +155,14 @@ public class TestGenerateArtifactsServiceImpl { String jsonString = csar.getJsonRequest(); return invokeService(jsonString); } + + /** + * Create a (mocked) HTTPS request and invoke the Babel generate artifacts API. + * + * @param jsonString the JSON request + * @return the Response from the HTTP API + * @throws URISyntaxException if the URI cannot be created + */ private Response invokeService(String jsonString) throws URISyntaxException { UriInfo mockUriInfo = Mockito.mock(UriInfo.class); Mockito.when(mockUriInfo.getRequestUri()).thenReturn(new URI("/validate")); // NOSONAR (mocked) diff --git a/src/test/java/org/onap/aai/babel/testdata/CsarTest.java b/src/test/java/org/onap/aai/babel/testdata/CsarTest.java index d884e4f..d922e8e 100644 --- a/src/test/java/org/onap/aai/babel/testdata/CsarTest.java +++ b/src/test/java/org/onap/aai/babel/testdata/CsarTest.java @@ -42,7 +42,6 @@ public enum CsarTest { MISSING_METADATA_CSAR("service-MissingMetadataTest.csar"), NO_YAML_FILES("noYmlFilesArchive.zip"), PORT_MIRROR_CSAR("service_PortMirror.csar"), - VNFOD_SERVICE("service-Dev2devnfodservice17July-csar.csar"), MULTIPLE_VNF_CSAR("catalog_csar_too_many_vnfConfigurations.csar"); // @formatter:on diff --git a/src/test/java/org/onap/aai/babel/xml/generator/model/TestModel.java b/src/test/java/org/onap/aai/babel/xml/generator/model/TestModel.java index 8d6fe3e..2bd6fc7 100644 --- a/src/test/java/org/onap/aai/babel/xml/generator/model/TestModel.java +++ b/src/test/java/org/onap/aai/babel/xml/generator/model/TestModel.java @@ -29,6 +29,8 @@ import static org.junit.Assert.assertThat; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.util.Arrays; +import java.util.List; import java.util.Properties; import org.junit.Before; import org.junit.Test; @@ -43,7 +45,7 @@ import org.onap.aai.babel.xml.generator.types.ModelType; public class TestModel { private Service serviceModel = new Service(); - private Resource resourceModel = new VirtualFunction(); + private List resourceModels = Arrays.asList(new VirtualFunction()); private Widget widgetModel = new OamNetwork(); private Model anonymousModel; @@ -54,8 +56,10 @@ public class TestModel { /** * Load the Widget to UUID mappings from the Artifact Generator properties. * - * @throws FileNotFoundException if the properties file is missing - * @throws IOException if the properties file is not loaded + * @throws FileNotFoundException + * if the properties file is missing + * @throws IOException + * if the properties file is not loaded */ @Before public void setup() throws FileNotFoundException, IOException { @@ -90,6 +94,8 @@ public class TestModel { assertThat(Model.getModelFor("any.unknown.type"), is(nullValue())); assertThat(Model.getModelFor("org.openecomp.resource.vf.allottedResource"), instanceOf(AllotedResource.class)); + assertThat(Model.getModelFor("org.openecomp.resource.vf.allottedResource.with.sub.type"), + instanceOf(AllotedResource.class)); assertThat(Model.getModelFor("org.openecomp.resource.vfc.AllottedResource"), instanceOf(ProvidingService.class)); assertThat(Model.getModelFor("org.openecomp.resource.vfc"), instanceOf(VServerWidget.class)); @@ -107,13 +113,15 @@ public class TestModel { @Test public void testGetCardinality() { - resourceModel.getCardinality(); + resourceModels.get(0).getCardinality(); } @Test public void testGetModelType() { assertThat(serviceModel.getModelType(), is(ModelType.SERVICE)); - assertThat(resourceModel.getModelType(), is(ModelType.RESOURCE)); + for (Resource resourceModel : resourceModels) { + assertThat(resourceModel.getModelType(), is(ModelType.RESOURCE)); + } assertThat(widgetModel.getModelType(), is(ModelType.WIDGET)); assertThat(anonymousModel.getModelType(), is(nullValue())); } @@ -126,6 +134,9 @@ public class TestModel { @Test(expected = org.onap.aai.babel.xml.generator.error.IllegalAccessException.class) public void testGetModelNameVersionIdIsUnsupported() { assertThat(widgetModel.getModelNameVersionId(), is(nullValue())); + assertThat(resourceModels.get(0).getModelType(), is(ModelType.RESOURCE)); + assertThat(widgetModel.getModelType(), is(ModelType.WIDGET)); + assertThat(anonymousModel.getModelType(), is(nullValue())); } } -- 2.16.6