From f6107af893b29094f84fb2eea2b9dd8acc78dc53 Mon Sep 17 00:00:00 2001 From: "mark.j.leonard" Date: Mon, 25 Mar 2019 18:12:48 +0000 Subject: [PATCH] Additional JUnit tests for Model classes Increase code coverage for Model and Service Change-Id: I02430f470cfa6497535a372a0b18be8678279270 Issue-ID: AAI-2280 Signed-off-by: mark.j.leonard --- .../xml/generator/api/AaiArtifactGenerator.java | 2 +- .../aai/babel/xml/generator/types/ModelType.java | 11 +++--- .../parser/TestArtifactGeneratorToscaParser.java | 43 ++++++++++++++++++---- ...verterTest.java => TestCsarToXmlConverter.java} | 4 +- .../org/onap/aai/babel/util/ArtifactTestUtils.java | 8 ++-- .../generator/TestAaiArtifactGenerator.java} | 40 ++++++++++++++++++-- .../aai/babel/xml/generator/model/TestModel.java | 30 ++++++++++++++- 7 files changed, 112 insertions(+), 26 deletions(-) rename src/test/java/org/onap/aai/babel/service/{CsarToXmlConverterTest.java => TestCsarToXmlConverter.java} (99%) rename src/test/java/org/onap/aai/babel/{parser/TestToscaParser.java => xml/generator/TestAaiArtifactGenerator.java} (67%) diff --git a/src/main/java/org/onap/aai/babel/xml/generator/api/AaiArtifactGenerator.java b/src/main/java/org/onap/aai/babel/xml/generator/api/AaiArtifactGenerator.java index d35814a..7415702 100644 --- a/src/main/java/org/onap/aai/babel/xml/generator/api/AaiArtifactGenerator.java +++ b/src/main/java/org/onap/aai/babel/xml/generator/api/AaiArtifactGenerator.java @@ -125,7 +125,7 @@ public class AaiArtifactGenerator implements ArtifactGenerator { * @return the generated Artifacts (containing XML models) * @throws XmlArtifactGenerationException */ - private GenerationData generateAllArtifacts(final String serviceVersion, ISdcCsarHelper csarHelper) + public GenerationData generateAllArtifacts(final String serviceVersion, ISdcCsarHelper csarHelper) throws XmlArtifactGenerationException { List serviceNodeTemplates = csarHelper.getServiceNodeTemplates(); if (serviceNodeTemplates == null) { diff --git a/src/main/java/org/onap/aai/babel/xml/generator/types/ModelType.java b/src/main/java/org/onap/aai/babel/xml/generator/types/ModelType.java index 7dd86c6..8215536 100644 --- a/src/main/java/org/onap/aai/babel/xml/generator/types/ModelType.java +++ b/src/main/java/org/onap/aai/babel/xml/generator/types/ModelType.java @@ -2,8 +2,8 @@ * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ - * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017-2018 European Software Marketing Ltd. + * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (c) 2017-2019 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. @@ -18,10 +18,11 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.onap.aai.babel.xml.generator.types; public enum ModelType { - SERVICE, - RESOURCE, - WIDGET; + SERVICE, // top-level model from the CSAR file + RESOURCE, // model type for which we generate XML and which may have children + WIDGET; // leaf-level type, pre-defined in A&AI } diff --git a/src/test/java/org/onap/aai/babel/parser/TestArtifactGeneratorToscaParser.java b/src/test/java/org/onap/aai/babel/parser/TestArtifactGeneratorToscaParser.java index 50f0aa9..7bc9fa8 100644 --- a/src/test/java/org/onap/aai/babel/parser/TestArtifactGeneratorToscaParser.java +++ b/src/test/java/org/onap/aai/babel/parser/TestArtifactGeneratorToscaParser.java @@ -37,8 +37,11 @@ import org.onap.aai.babel.util.Resources; import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException; import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil; import org.onap.aai.babel.xml.generator.data.WidgetMapping; +import org.onap.aai.babel.xml.generator.model.Model; import org.onap.aai.babel.xml.generator.model.Resource; +import org.onap.aai.babel.xml.generator.model.Service; import org.onap.aai.babel.xml.generator.model.WidgetType; +import org.onap.aai.babel.xml.generator.types.ModelType; import org.onap.sdc.tosca.parser.api.ISdcCsarHelper; import org.onap.sdc.toscaparser.api.Group; import org.onap.sdc.toscaparser.api.NodeTemplate; @@ -55,7 +58,7 @@ public class TestArtifactGeneratorToscaParser { /** * Initialize the Generator with an invalid artifact generator properties file path. - * + * * @throws IOException * if an error occurs reading the configuration properties */ @@ -67,7 +70,7 @@ public class TestArtifactGeneratorToscaParser { /** * Initialize the Generator with an invalid mappings file path. - * + * * @throws IOException * if the file content could not be read successfully */ @@ -78,7 +81,7 @@ public class TestArtifactGeneratorToscaParser { /** * Initialize the Generator with no Widget Mappings content. - * + * * @throws IOException * if the file content could not be read successfully */ @@ -90,7 +93,7 @@ public class TestArtifactGeneratorToscaParser { /** * Initialize the Generator with invalid Widget Mappings content. - * + * * @throws IOException * if the file content could not be read successfully */ @@ -123,7 +126,7 @@ public class TestArtifactGeneratorToscaParser { /** * Initialize the Artifact Generator Widget Mapping config with incomplete data (no type). - * + * * @throws IOException * if a WidgetMapping is invalid */ @@ -136,7 +139,7 @@ public class TestArtifactGeneratorToscaParser { /** * Initialize the Artifact Generator Widget Mapping config with invalid data (type value). - * + * * @throws IOException * if a WidgetMapping is invalid */ @@ -149,7 +152,7 @@ public class TestArtifactGeneratorToscaParser { /** * Initialize the Artifact Generator Widget Mapping config with incomplete data (no widget name). - * + * * @throws IOException * if a WidgetMapping is invalid */ @@ -160,9 +163,33 @@ public class TestArtifactGeneratorToscaParser { WidgetConfigurationUtil.setWidgetMappings(Collections.singletonList(invalidMapping)); } + /** + * Create a Resource with a Widget model type and add this to a Service. Note that there are no test CSAR files + * which require this functionality, but the code path exists to support it. + * + * @throws IOException + * if the widget mappings are not loaded + * @throws XmlArtifactGenerationException + * if there is no configuration defined for the test resource's widget type + */ + @Test + public void testAddWidgetToService() throws IOException, XmlArtifactGenerationException { + ArtifactTestUtils testUtils = new ArtifactTestUtils(); + testUtils.loadWidgetMappings(); + testUtils.loadWidgetToUuidMappings(); + + Model serviceModel = new Service(); + Resource resourceModel = new Resource(WidgetType.valueOf("VF"), false); + resourceModel.setModelType(ModelType.WIDGET); + + ISdcCsarHelper helper = Mockito.mock(ISdcCsarHelper.class); + ArtifactGeneratorToscaParser parser = new ArtifactGeneratorToscaParser(helper); + parser.addRelatedModel(serviceModel, resourceModel); + } + /** * Process a dummy Group object for a Service Resource. - * + * * @throws XmlArtifactGenerationException * if there is no configuration defined for a member Widget of an instance group * @throws IOException diff --git a/src/test/java/org/onap/aai/babel/service/CsarToXmlConverterTest.java b/src/test/java/org/onap/aai/babel/service/TestCsarToXmlConverter.java similarity index 99% rename from src/test/java/org/onap/aai/babel/service/CsarToXmlConverterTest.java rename to src/test/java/org/onap/aai/babel/service/TestCsarToXmlConverter.java index 5001a09..244eccd 100644 --- a/src/test/java/org/onap/aai/babel/service/CsarToXmlConverterTest.java +++ b/src/test/java/org/onap/aai/babel/service/TestCsarToXmlConverter.java @@ -52,7 +52,7 @@ import org.xml.sax.SAXException; /** * Tests {@link CsarToXmlConverter}. */ -public class CsarToXmlConverterTest { +public class TestCsarToXmlConverter { private static final String INCORRECT_CSAR_NAME = "the_name_of_the_csar_file.csar"; private static final String SERVICE_VERSION = "1.0"; @@ -189,7 +189,7 @@ public class CsarToXmlConverterTest { /** * A Matcher for comparing generated XML Strings with expected XML content. - * + * * @param expected * the expected XML String * @return a new Matcher for comparing XML Strings diff --git a/src/test/java/org/onap/aai/babel/util/ArtifactTestUtils.java b/src/test/java/org/onap/aai/babel/util/ArtifactTestUtils.java index df27c22..164c80b 100644 --- a/src/test/java/org/onap/aai/babel/util/ArtifactTestUtils.java +++ b/src/test/java/org/onap/aai/babel/util/ArtifactTestUtils.java @@ -52,9 +52,9 @@ import org.xml.sax.SAXException; */ public class ArtifactTestUtils { + public static final String CSAR_INPUTS_FOLDER = "compressedArtifacts/"; private static final String JSON_REQUESTS_FOLDER = "jsonFiles/"; private static final String JSON_RESPONSES_FOLDER = "response/"; - private static final String CSAR_INPUTS_FOLDER = "compressedArtifacts/"; /** * Initialize System Properties for test configuration files. @@ -69,7 +69,7 @@ public class ArtifactTestUtils { /** * Load the Widget to UUID mappings from the Artifact Generator Properties (resource). - * + * * @throws IOException * if the properties file is not loaded */ @@ -79,7 +79,7 @@ public class ArtifactTestUtils { /** * Load the Widget type mappings (resource). - * + * * @throws IOException * if the configuration file is not loaded */ @@ -158,7 +158,7 @@ public class ArtifactTestUtils { /** * Create Properties from the content of the named resource (e.g. a file on the classpath). - * + * * @param resourceName * the resource name * @return Properties loaded from the named resource diff --git a/src/test/java/org/onap/aai/babel/parser/TestToscaParser.java b/src/test/java/org/onap/aai/babel/xml/generator/TestAaiArtifactGenerator.java similarity index 67% rename from src/test/java/org/onap/aai/babel/parser/TestToscaParser.java rename to src/test/java/org/onap/aai/babel/xml/generator/TestAaiArtifactGenerator.java index f300f7b..d95291b 100644 --- a/src/test/java/org/onap/aai/babel/parser/TestToscaParser.java +++ b/src/test/java/org/onap/aai/babel/xml/generator/TestAaiArtifactGenerator.java @@ -19,7 +19,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.aai.babel.parser; +package org.onap.aai.babel.xml.generator; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.is; @@ -31,21 +31,29 @@ import java.util.Map; import org.junit.Before; import org.junit.Test; import org.onap.aai.babel.csar.extractor.InvalidArchiveException; +import org.onap.aai.babel.parser.ArtifactGeneratorToscaParser; import org.onap.aai.babel.testdata.CsarTest; import org.onap.aai.babel.util.ArtifactTestUtils; import org.onap.aai.babel.util.Resources; import org.onap.aai.babel.xml.generator.api.AaiArtifactGenerator; import org.onap.aai.babel.xml.generator.data.AdditionalParams; import org.onap.aai.babel.xml.generator.data.GenerationData; +import org.onap.aai.babel.xml.generator.model.WidgetType; +import org.onap.sdc.tosca.parser.api.ISdcCsarHelper; +import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException; +import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory; /** * Direct tests of the {@link AaiArtifactGenerator} to improve code coverage. */ -public class TestToscaParser { +public class TestAaiArtifactGenerator { + + private ArtifactTestUtils testUtils; @Before public void setup() { - new ArtifactTestUtils().setGeneratorSystemProperties(); + testUtils = new ArtifactTestUtils(); + testUtils.setGeneratorSystemProperties(); } @Test @@ -65,9 +73,33 @@ public class TestToscaParser { assertThat("Number of resources generated", data.getResultData().size(), is(equalTo(2))); } + /** + * Test that an Exception is thrown when a Widget Type (such as ALLOTTED_RESOURCE) required by Babel is not present + * in the WidgetType dynamic enumeration. + * + * @throws SdcToscaParserException + * @throws IOException + * @throws XmlArtifactGenerationException + */ + @Test(expected = IllegalArgumentException.class) + public void testParserWithIncompleteMappings() + throws SdcToscaParserException, IOException, XmlArtifactGenerationException { + testUtils.loadWidgetMappings(); + testUtils.loadWidgetToUuidMappings(); + + AaiArtifactGenerator artifactGenerator = new AaiArtifactGenerator(); + WidgetType.clearElements(); // Remove all WidgetTypes so that the generator fails + + ISdcCsarHelper csarHelper = SdcToscaParserFactory.getInstance() + .getSdcCsarHelper(TestAaiArtifactGenerator.class.getClassLoader() + .getResource(ArtifactTestUtils.CSAR_INPUTS_FOLDER + CsarTest.VNF_VENDOR_CSAR.getName()) + .getFile().toString()); + artifactGenerator.generateAllArtifacts("1.0", csarHelper); + } + /** * Invoke the generator with a sample CSAR file. - * + * * @return the generated AAI Artifacts * @throws InvalidArchiveException * if the test CSAR file is invalid 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 ed4f5c1..f1a579e 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 @@ -73,9 +73,35 @@ public class TestModel { assertMapping("org.openecomp.resource.vfc", "an.unknown.type", WidgetType.valueOf("VSERVER")); } + /** + * Test that there is no exception if processing a Model that has no metadata properties. + */ + @Test + public void testNullIdentProperties() { + createTestModel().populateModelIdentificationInformation(null); + } + + /** + * Test that an exception occurs if calling code passes an unsupported Widget Type value to the base implementation + * of the hasWidgetType() method. + */ + @Test(expected = IllegalArgumentException.class) + public void testUnknownWidgetType() { + createTestModel().hasWidgetType(null); + } + + /** + * Create any Model with a valid WidgetType, for method testing. + * + * @return a valid Model for testing purposes + */ + private Model createTestModel() { + return new Resource(WidgetType.valueOf("VSERVER"), false); + } + /** * Assert that the TOSCA type String is mapped to the expected Widget Type. - * + * * @param toscaType * the TOSCA type or prefix * @param widgetType @@ -87,7 +113,7 @@ public class TestModel { /** * Assert that the TOSCA metadata type is mapped to the expected Widget Type. - * + * * @param toscaType * the name (or name prefix) of the TOSCA type * @param metadataType -- 2.16.6