From f6107af893b29094f84fb2eea2b9dd8acc78dc53 Mon Sep 17 00:00:00 2001 From: "mark.j.leonard" Date: Mon, 25 Mar 2019 18:12:48 +0000 Subject: 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 +++- .../org/onap/aai/babel/parser/TestToscaParser.java | 83 ------- .../aai/babel/service/CsarToXmlConverterTest.java | 239 --------------------- .../aai/babel/service/TestCsarToXmlConverter.java | 239 +++++++++++++++++++++ .../org/onap/aai/babel/util/ArtifactTestUtils.java | 8 +- .../xml/generator/TestAaiArtifactGenerator.java | 115 ++++++++++ .../aai/babel/xml/generator/model/TestModel.java | 30 ++- 9 files changed, 428 insertions(+), 342 deletions(-) delete mode 100644 src/test/java/org/onap/aai/babel/parser/TestToscaParser.java delete mode 100644 src/test/java/org/onap/aai/babel/service/CsarToXmlConverterTest.java create mode 100644 src/test/java/org/onap/aai/babel/service/TestCsarToXmlConverter.java create mode 100644 src/test/java/org/onap/aai/babel/xml/generator/TestAaiArtifactGenerator.java 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/parser/TestToscaParser.java b/src/test/java/org/onap/aai/babel/parser/TestToscaParser.java deleted file mode 100644 index f300f7b..0000000 --- a/src/test/java/org/onap/aai/babel/parser/TestToscaParser.java +++ /dev/null @@ -1,83 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * 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. - * 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.parser; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; - -import java.io.IOException; -import java.util.HashMap; -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.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; - -/** - * Direct tests of the {@link AaiArtifactGenerator} to improve code coverage. - */ -public class TestToscaParser { - - @Before - public void setup() { - new ArtifactTestUtils().setGeneratorSystemProperties(); - } - - @Test - public void testParserWithInvalidMappings() throws IOException, InvalidArchiveException { - System.setProperty(ArtifactGeneratorToscaParser.PROPERTY_TOSCA_MAPPING_FILE, - new ArtifactTestUtils().getResourcePath(Resources.INVALID_TOSCA_MAPPING_CONFIG)); - - GenerationData data = generateArtifactsFromCsarFile(); - assertThat("Number of errors produced " + data.getErrorData(), data.getErrorData().size(), is(equalTo(1))); - assertThat("Number of resources generated", data.getResultData().size(), is(equalTo(0))); - } - - @Test - public void testParserWithCsarFile() throws IOException, InvalidArchiveException { - GenerationData data = generateArtifactsFromCsarFile(); - assertThat("Number of errors produced " + data.getErrorData(), data.getErrorData().size(), is(equalTo(0))); - assertThat("Number of resources generated", data.getResultData().size(), is(equalTo(2))); - } - - /** - * Invoke the generator with a sample CSAR file. - * - * @return the generated AAI Artifacts - * @throws InvalidArchiveException - * if the test CSAR file is invalid - * @throws IOException - * if there are I/O errors reading the CSAR content - */ - private GenerationData generateArtifactsFromCsarFile() throws InvalidArchiveException, IOException { - Map additionalParams = new HashMap<>(); - additionalParams.put(AdditionalParams.SERVICE_VERSION.getName(), "1.0"); - return new AaiArtifactGenerator().generateArtifact(CsarTest.VNF_VENDOR_CSAR.getContent(), - CsarTest.VNF_VENDOR_CSAR.extractArtifacts(), additionalParams); - } -} diff --git a/src/test/java/org/onap/aai/babel/service/CsarToXmlConverterTest.java b/src/test/java/org/onap/aai/babel/service/CsarToXmlConverterTest.java deleted file mode 100644 index 5001a09..0000000 --- a/src/test/java/org/onap/aai/babel/service/CsarToXmlConverterTest.java +++ /dev/null @@ -1,239 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * 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. - * 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.service; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.not; -import static org.hamcrest.CoreMatchers.nullValue; -import static org.junit.Assert.assertThat; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import org.hamcrest.BaseMatcher; -import org.hamcrest.Description; -import org.hamcrest.Matcher; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.onap.aai.babel.csar.CsarConverterException; -import org.onap.aai.babel.csar.CsarToXmlConverter; -import org.onap.aai.babel.parser.ArtifactGeneratorToscaParser; -import org.onap.aai.babel.service.data.BabelArtifact; -import org.onap.aai.babel.testdata.CsarTest; -import org.onap.aai.babel.util.ArtifactTestUtils; -import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException; -import org.xml.sax.SAXException; - -/** - * Tests {@link CsarToXmlConverter}. - */ -public class CsarToXmlConverterTest { - - private static final String INCORRECT_CSAR_NAME = "the_name_of_the_csar_file.csar"; - private static final String SERVICE_VERSION = "1.0"; - - // The class to be tested. - private CsarToXmlConverter converter; - - @Rule - public ExpectedException exception = ExpectedException.none(); - - @Before - public void setup() { - new ArtifactTestUtils().setGeneratorSystemProperties(); - converter = new CsarToXmlConverter(); - } - - @After - public void tearDown() { - converter = null; - } - - @Test(expected = NullPointerException.class) - public void testNullArtifactSupplied() throws CsarConverterException { - converter.generateXmlFromCsar(null, null, null); - } - - @Test(expected = NullPointerException.class) - public void testMissingName() throws CsarConverterException, IOException { - converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), null, null); - } - - @Test(expected = NullPointerException.class) - public void testMissingVersion() throws CsarConverterException, IOException { - converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), INCORRECT_CSAR_NAME, null); - } - - @Test(expected = CsarConverterException.class) - public void testNoPayloadExists() throws CsarConverterException { - converter.generateXmlFromCsar(new byte[0], INCORRECT_CSAR_NAME, SERVICE_VERSION); - } - - @Test(expected = CsarConverterException.class) - public void testCsarFileHasNoYmlFiles() throws CsarConverterException, IOException { - converter.generateXmlFromCsar(CsarTest.NO_YAML_FILES.getContent(), CsarTest.NO_YAML_FILES.getName(), - SERVICE_VERSION); - } - - /** - * Test that an Exception is thrown when the Artifact Generator properties are not present. - * - * @throws CsarConverterException - * if there is an error either extracting the YAML files or generating XML artifacts - * @throws IOException - * if an I/O exception occurs loading the test CSAR file - */ - @Test - public void testArtifactGeneratorConfigMissing() throws CsarConverterException, IOException { - exception.expect(IllegalArgumentException.class); - exception.expectMessage("Cannot generate artifacts. System property " - + ArtifactGeneratorToscaParser.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE + " not configured"); - - // Unset the required system property - System.clearProperty(ArtifactGeneratorToscaParser.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE); - converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), CsarTest.SD_WAN_CSAR_FILE.getName(), - SERVICE_VERSION); - } - - /** - * Test that an Exception is thrown when the Artifact Generator's TOSCA Mappings configuration file is not present. - * - * @throws CsarConverterException - * if there is an error either extracting the YAML files or generating XML artifacts - * @throws IOException - * if an I/O exception occurs - */ - @Test - public void generateXmlFromCsarMappingSystemPropertyNotSet() throws CsarConverterException, IOException { - exception.expect(IllegalArgumentException.class); - exception.expectMessage("Cannot generate artifacts. System property " - + ArtifactGeneratorToscaParser.PROPERTY_TOSCA_MAPPING_FILE + " not configured"); - - // Unset the required system property - System.clearProperty(ArtifactGeneratorToscaParser.PROPERTY_TOSCA_MAPPING_FILE); - converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), CsarTest.SD_WAN_CSAR_FILE.getName(), - SERVICE_VERSION); - } - - @Test - public void testServiceMetadataMissing() - throws IOException, XmlArtifactGenerationException, CsarConverterException { - converter.generateXmlFromCsar(CsarTest.MISSING_METADATA_CSAR.getContent(), - CsarTest.MISSING_METADATA_CSAR.getName(), SERVICE_VERSION); - } - - @Test - public void generateXmlFromSdWanCsar() throws IOException, CsarConverterException { - List filesToLoad = new ArrayList<>(); - filesToLoad.add("AAI-SD-WAN-Service-Test-service-1.0.xml"); - filesToLoad.add("AAI-SdWanTestVsp..DUMMY..module-0-resource-2.xml"); - filesToLoad.add("AAI-Tunnel_XConnTest-resource-2.0.xml"); - filesToLoad.add("AAI-SD-WAN-Test-VSP-resource-1.0.xml"); - assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad), CsarTest.SD_WAN_CSAR_FILE); - } - - @Test - public void generateXmlFromNetworkCollectionCsar() throws IOException, CsarConverterException { - List filesToLoad = new ArrayList<>(); - filesToLoad.add("AAI-TEST SVC_1-service-1.0.xml"); - filesToLoad.add("AAI-TEST CR_1-resource-7.0.xml"); - filesToLoad.add("AAI-testcr_1..NetworkCollection..0-resource-1.xml"); - filesToLoad.add("AAI-ExtVL-resource-40.0.xml"); - assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad), - CsarTest.NETWORK_COLLECTION_CSAR_FILE); - } - - @Test - public void generatePortMirrorConfigurationModel() - throws CsarConverterException, IOException, XmlArtifactGenerationException { - List filesToLoad = new ArrayList<>(); - filesToLoad.add("AAI-Port Mirror_Test-service-1.0.xml"); - filesToLoad.add("AAI-Port Mirroring Configuration-resource-35.0.xml"); - assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad), CsarTest.PORT_MIRROR_CSAR); - } - - @Test - public void generateXmlFromServiceProxyCsar() - throws CsarConverterException, IOException, XmlArtifactGenerationException { - List filesToLoad = new ArrayList<>(); - filesToLoad.add("AAI-Grouping Service for Test-service-1.0.xml"); - filesToLoad.add("AAI-groupingservicefortest..ResourceInstanceGroup..0-resource-1.xml"); - filesToLoad.add("AAI-groupingservicefortest..ResourceInstanceGroup..1-resource-1.xml"); - assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad), CsarTest.SERVICE_PROXY_CSAR_FILE); - } - - /** - * 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 - */ - public Matcher matches(final String expected) { - return new BaseMatcher() { - protected String theExpected = expected; - - @Override - public boolean matches(Object item) { - try { - return new ArtifactTestUtils().compareXmlStrings((String) item, theExpected); - } catch (SAXException | IOException e) { - throw new RuntimeException(e); - } - } - - @Override - public void describeTo(Description description) { - description.appendText(theExpected.toString()); - } - }; - } - - private Map createExpectedXmlFiles(List filesToLoad) throws IOException { - Map xmlMap = new HashMap<>(); - for (String filename : filesToLoad) { - xmlMap.put(filename, new ArtifactTestUtils().loadResourceAsString("generatedXml/" + filename)); - } - return xmlMap; - } - - private void assertThatGeneratedFilesMatchExpected(Map expectedXmlFiles, CsarTest csarFile) - throws CsarConverterException, IOException { - List generatedArtifacts = - converter.generateXmlFromCsar(csarFile.getContent(), csarFile.getName(), SERVICE_VERSION); - assertThat("Incorrect number of files generated", // - generatedArtifacts.size(), is(equalTo(expectedXmlFiles.size()))); - for (BabelArtifact generated : generatedArtifacts) { - String fileName = generated.getName(); - String expectedXml = expectedXmlFiles.get(fileName); - assertThat("Missing expected content for " + generated.getName(), expectedXml, is(not(nullValue()))); - assertThat("The content of " + generated.getName() + " must match the expected content", - generated.getPayload(), matches(expectedXml)); - } - } -} diff --git a/src/test/java/org/onap/aai/babel/service/TestCsarToXmlConverter.java b/src/test/java/org/onap/aai/babel/service/TestCsarToXmlConverter.java new file mode 100644 index 0000000..244eccd --- /dev/null +++ b/src/test/java/org/onap/aai/babel/service/TestCsarToXmlConverter.java @@ -0,0 +1,239 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * 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. + * 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.service; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertThat; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.onap.aai.babel.csar.CsarConverterException; +import org.onap.aai.babel.csar.CsarToXmlConverter; +import org.onap.aai.babel.parser.ArtifactGeneratorToscaParser; +import org.onap.aai.babel.service.data.BabelArtifact; +import org.onap.aai.babel.testdata.CsarTest; +import org.onap.aai.babel.util.ArtifactTestUtils; +import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException; +import org.xml.sax.SAXException; + +/** + * Tests {@link CsarToXmlConverter}. + */ +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"; + + // The class to be tested. + private CsarToXmlConverter converter; + + @Rule + public ExpectedException exception = ExpectedException.none(); + + @Before + public void setup() { + new ArtifactTestUtils().setGeneratorSystemProperties(); + converter = new CsarToXmlConverter(); + } + + @After + public void tearDown() { + converter = null; + } + + @Test(expected = NullPointerException.class) + public void testNullArtifactSupplied() throws CsarConverterException { + converter.generateXmlFromCsar(null, null, null); + } + + @Test(expected = NullPointerException.class) + public void testMissingName() throws CsarConverterException, IOException { + converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), null, null); + } + + @Test(expected = NullPointerException.class) + public void testMissingVersion() throws CsarConverterException, IOException { + converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), INCORRECT_CSAR_NAME, null); + } + + @Test(expected = CsarConverterException.class) + public void testNoPayloadExists() throws CsarConverterException { + converter.generateXmlFromCsar(new byte[0], INCORRECT_CSAR_NAME, SERVICE_VERSION); + } + + @Test(expected = CsarConverterException.class) + public void testCsarFileHasNoYmlFiles() throws CsarConverterException, IOException { + converter.generateXmlFromCsar(CsarTest.NO_YAML_FILES.getContent(), CsarTest.NO_YAML_FILES.getName(), + SERVICE_VERSION); + } + + /** + * Test that an Exception is thrown when the Artifact Generator properties are not present. + * + * @throws CsarConverterException + * if there is an error either extracting the YAML files or generating XML artifacts + * @throws IOException + * if an I/O exception occurs loading the test CSAR file + */ + @Test + public void testArtifactGeneratorConfigMissing() throws CsarConverterException, IOException { + exception.expect(IllegalArgumentException.class); + exception.expectMessage("Cannot generate artifacts. System property " + + ArtifactGeneratorToscaParser.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE + " not configured"); + + // Unset the required system property + System.clearProperty(ArtifactGeneratorToscaParser.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE); + converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), CsarTest.SD_WAN_CSAR_FILE.getName(), + SERVICE_VERSION); + } + + /** + * Test that an Exception is thrown when the Artifact Generator's TOSCA Mappings configuration file is not present. + * + * @throws CsarConverterException + * if there is an error either extracting the YAML files or generating XML artifacts + * @throws IOException + * if an I/O exception occurs + */ + @Test + public void generateXmlFromCsarMappingSystemPropertyNotSet() throws CsarConverterException, IOException { + exception.expect(IllegalArgumentException.class); + exception.expectMessage("Cannot generate artifacts. System property " + + ArtifactGeneratorToscaParser.PROPERTY_TOSCA_MAPPING_FILE + " not configured"); + + // Unset the required system property + System.clearProperty(ArtifactGeneratorToscaParser.PROPERTY_TOSCA_MAPPING_FILE); + converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), CsarTest.SD_WAN_CSAR_FILE.getName(), + SERVICE_VERSION); + } + + @Test + public void testServiceMetadataMissing() + throws IOException, XmlArtifactGenerationException, CsarConverterException { + converter.generateXmlFromCsar(CsarTest.MISSING_METADATA_CSAR.getContent(), + CsarTest.MISSING_METADATA_CSAR.getName(), SERVICE_VERSION); + } + + @Test + public void generateXmlFromSdWanCsar() throws IOException, CsarConverterException { + List filesToLoad = new ArrayList<>(); + filesToLoad.add("AAI-SD-WAN-Service-Test-service-1.0.xml"); + filesToLoad.add("AAI-SdWanTestVsp..DUMMY..module-0-resource-2.xml"); + filesToLoad.add("AAI-Tunnel_XConnTest-resource-2.0.xml"); + filesToLoad.add("AAI-SD-WAN-Test-VSP-resource-1.0.xml"); + assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad), CsarTest.SD_WAN_CSAR_FILE); + } + + @Test + public void generateXmlFromNetworkCollectionCsar() throws IOException, CsarConverterException { + List filesToLoad = new ArrayList<>(); + filesToLoad.add("AAI-TEST SVC_1-service-1.0.xml"); + filesToLoad.add("AAI-TEST CR_1-resource-7.0.xml"); + filesToLoad.add("AAI-testcr_1..NetworkCollection..0-resource-1.xml"); + filesToLoad.add("AAI-ExtVL-resource-40.0.xml"); + assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad), + CsarTest.NETWORK_COLLECTION_CSAR_FILE); + } + + @Test + public void generatePortMirrorConfigurationModel() + throws CsarConverterException, IOException, XmlArtifactGenerationException { + List filesToLoad = new ArrayList<>(); + filesToLoad.add("AAI-Port Mirror_Test-service-1.0.xml"); + filesToLoad.add("AAI-Port Mirroring Configuration-resource-35.0.xml"); + assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad), CsarTest.PORT_MIRROR_CSAR); + } + + @Test + public void generateXmlFromServiceProxyCsar() + throws CsarConverterException, IOException, XmlArtifactGenerationException { + List filesToLoad = new ArrayList<>(); + filesToLoad.add("AAI-Grouping Service for Test-service-1.0.xml"); + filesToLoad.add("AAI-groupingservicefortest..ResourceInstanceGroup..0-resource-1.xml"); + filesToLoad.add("AAI-groupingservicefortest..ResourceInstanceGroup..1-resource-1.xml"); + assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad), CsarTest.SERVICE_PROXY_CSAR_FILE); + } + + /** + * 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 + */ + public Matcher matches(final String expected) { + return new BaseMatcher() { + protected String theExpected = expected; + + @Override + public boolean matches(Object item) { + try { + return new ArtifactTestUtils().compareXmlStrings((String) item, theExpected); + } catch (SAXException | IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public void describeTo(Description description) { + description.appendText(theExpected.toString()); + } + }; + } + + private Map createExpectedXmlFiles(List filesToLoad) throws IOException { + Map xmlMap = new HashMap<>(); + for (String filename : filesToLoad) { + xmlMap.put(filename, new ArtifactTestUtils().loadResourceAsString("generatedXml/" + filename)); + } + return xmlMap; + } + + private void assertThatGeneratedFilesMatchExpected(Map expectedXmlFiles, CsarTest csarFile) + throws CsarConverterException, IOException { + List generatedArtifacts = + converter.generateXmlFromCsar(csarFile.getContent(), csarFile.getName(), SERVICE_VERSION); + assertThat("Incorrect number of files generated", // + generatedArtifacts.size(), is(equalTo(expectedXmlFiles.size()))); + for (BabelArtifact generated : generatedArtifacts) { + String fileName = generated.getName(); + String expectedXml = expectedXmlFiles.get(fileName); + assertThat("Missing expected content for " + generated.getName(), expectedXml, is(not(nullValue()))); + assertThat("The content of " + generated.getName() + " must match the expected content", + generated.getPayload(), matches(expectedXml)); + } + } +} 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/xml/generator/TestAaiArtifactGenerator.java b/src/test/java/org/onap/aai/babel/xml/generator/TestAaiArtifactGenerator.java new file mode 100644 index 0000000..d95291b --- /dev/null +++ b/src/test/java/org/onap/aai/babel/xml/generator/TestAaiArtifactGenerator.java @@ -0,0 +1,115 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * 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. + * 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.xml.generator; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.io.IOException; +import java.util.HashMap; +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 TestAaiArtifactGenerator { + + private ArtifactTestUtils testUtils; + + @Before + public void setup() { + testUtils = new ArtifactTestUtils(); + testUtils.setGeneratorSystemProperties(); + } + + @Test + public void testParserWithInvalidMappings() throws IOException, InvalidArchiveException { + System.setProperty(ArtifactGeneratorToscaParser.PROPERTY_TOSCA_MAPPING_FILE, + new ArtifactTestUtils().getResourcePath(Resources.INVALID_TOSCA_MAPPING_CONFIG)); + + GenerationData data = generateArtifactsFromCsarFile(); + assertThat("Number of errors produced " + data.getErrorData(), data.getErrorData().size(), is(equalTo(1))); + assertThat("Number of resources generated", data.getResultData().size(), is(equalTo(0))); + } + + @Test + public void testParserWithCsarFile() throws IOException, InvalidArchiveException { + GenerationData data = generateArtifactsFromCsarFile(); + assertThat("Number of errors produced " + data.getErrorData(), data.getErrorData().size(), is(equalTo(0))); + 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 + * @throws IOException + * if there are I/O errors reading the CSAR content + */ + private GenerationData generateArtifactsFromCsarFile() throws InvalidArchiveException, IOException { + Map additionalParams = new HashMap<>(); + additionalParams.put(AdditionalParams.SERVICE_VERSION.getName(), "1.0"); + return new AaiArtifactGenerator().generateArtifact(CsarTest.VNF_VENDOR_CSAR.getContent(), + CsarTest.VNF_VENDOR_CSAR.extractArtifacts(), additionalParams); + } +} 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 -- cgit 1.2.3-korg