Additional tests for VNF image extraction 95/64995/1
authormark.j.leonard <mark.j.leonard@gmail.com>
Thu, 6 Sep 2018 12:12:23 +0000 (13:12 +0100)
committermark.j.leonard <mark.j.leonard@gmail.com>
Thu, 6 Sep 2018 12:12:23 +0000 (13:12 +0100)
New tests for extracting software versions.

Change-Id: I26a9d654d03154a1d026741e623c8f953946a5f4
Issue-ID: AAI-1250
Signed-off-by: mark.j.leonard <mark.j.leonard@gmail.com>
src/main/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractor.java
src/test/java/org/onap/aai/babel/csar/vnfcatalog/SdcToscaHelper.java [new file with mode: 0644]
src/test/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractorTest.java
src/test/java/org/onap/aai/babel/service/TestGenerateArtifactsServiceImpl.java
src/test/java/org/onap/aai/babel/testdata/CsarTest.java
src/test/java/org/onap/aai/babel/xml/generator/model/TestModel.java

index d98c622..247dfee 100644 (file)
@@ -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<String> extractSoftwareVersions(SubstitutionMappings sm) throws ToscaToCatalogException {
+    List<String> extractSoftwareVersions(SubstitutionMappings sm) throws ToscaToCatalogException {
         applicationLogger.debug("Trying to extract the software versions for the vnf configuration");
 
         List<NodeTemplate> 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 (file)
index 0000000..9fcc5a4
--- /dev/null
@@ -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<NodeTemplate> smnodetemplates = new ArrayList<>();
+
+    /**
+     * @return
+     */
+    public SubstitutionMappings buildMappings() {
+        LinkedHashMap<String, Object> defProps = getImagesDefProps();
+
+        LinkedHashMap<String, Object> defs = buildNodeTemplateTypeInfo(defProps);
+        LinkedHashMap<String, Object> caps = new LinkedHashMap<>();
+        LinkedHashMap<String, Object> reqs = new LinkedHashMap<>();
+
+        String type = "tosca.nodes.custom";
+
+        LinkedHashMap<String, Object> smsubMappingDef = new LinkedHashMap<>();
+        smsubMappingDef.put("node_type", type);
+        smsubMappingDef.put("capabilities", caps);
+        smsubMappingDef.put("requirements", reqs);
+
+        LinkedHashMap<String, Object> smcustomDefs = buildCustomTypeDefinitions(type, defs);
+
+        return new SubstitutionMappings(smsubMappingDef, smnodetemplates, null, null, null, null, smcustomDefs);
+    }
+
+    private LinkedHashMap<String, Object> getImagesDefProps() {
+        LinkedHashMap<String, Object> imagesDef = new LinkedHashMap<>();
+        imagesDef.put("type", "map");
+        imagesDef.put("required", false);
+        imagesDef.put("entry_schema", "{type=org.openecomp.datatypes.ImageInfo}");
+
+        LinkedHashMap<String, Object> defProps = new LinkedHashMap<>();
+        defProps.put("images", imagesDef);
+        return defProps;
+    }
+
+    private LinkedHashMap<String, Object> buildCustomTypeDefinitions(String type,
+            LinkedHashMap<String, Object> typeInfo) {
+        LinkedHashMap<String, Object> customDefs = new LinkedHashMap<>();
+        customDefs.put(type, typeInfo);
+        return customDefs;
+    }
+
+    private LinkedHashMap<String, Object> buildNodeTemplateTypeInfo(LinkedHashMap<String, Object> props) {
+        LinkedHashMap<String, Object> 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<String, Object> nodeTemplate = new LinkedHashMap<>();
+        nodeTemplate.put("type", type);
+        nodeTemplate.put("properties", null);
+
+        LinkedHashMap<String, Object> ntnodeTemplates = buildCustomTypeDefinitions(name, nodeTemplate);
+        ntnodeTemplates.put("derived_from", null);
+        ntnodeTemplates.put("properties", getImagesDefProps());
+
+        LinkedHashMap<String, Object> typeInfo = buildNodeTemplateTypeInfo(getImagesDefProps());
+        LinkedHashMap<String, Object> customDefs = buildCustomTypeDefinitions(type, typeInfo);
+        smnodetemplates.add(new NodeTemplate(name, ntnodeTemplates, customDefs, null, null));
+    }
+
+    /**
+     * @param images
+     */
+    public void addNodeTemplate(Object images) {
+        LinkedHashMap<String, Object> properties = new LinkedHashMap<>();
+        properties.put("images", images);
+
+        String type = "tosca.nodes.custom";
+        LinkedHashMap<String, Object> nodeTemplate = new LinkedHashMap<>();
+        nodeTemplate.put("type", type);
+        nodeTemplate.put("properties", properties);
+
+        String name = "node name";
+        LinkedHashMap<String, Object> ntnodeTemplates = buildCustomTypeDefinitions(name, nodeTemplate);
+        ntnodeTemplates.put("derived_from", null);
+        ntnodeTemplates.put("properties", getImagesDefProps());
+
+        LinkedHashMap<String, Object> typeInfo = buildNodeTemplateTypeInfo(getImagesDefProps());
+        LinkedHashMap<String, Object> customDefs = buildCustomTypeDefinitions(type, typeInfo);
+
+        smnodetemplates.add(new NodeTemplate(name, ntnodeTemplates, customDefs, null, null));
+    }
+}
+
index b1229b4..a6f98b8 100644 (file)
@@ -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<String> 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<String, Object> 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<String, Object> 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"));
+    }
 }
index 5bd5484..599b3ff 100644 (file)
@@ -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)
index d884e4f..d922e8e 100644 (file)
@@ -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
 
index 8d6fe3e..2bd6fc7 100644 (file)
@@ -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<Resource> 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()));
     }
 
 }