2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 European Software Marketing Ltd.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.aai.babel.service;
24 import static org.hamcrest.CoreMatchers.equalTo;
25 import static org.hamcrest.CoreMatchers.is;
26 import static org.junit.Assert.assertThat;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
33 import org.hamcrest.BaseMatcher;
34 import org.hamcrest.Description;
35 import org.hamcrest.Matcher;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.ExpectedException;
41 import org.onap.aai.babel.csar.CsarConverterException;
42 import org.onap.aai.babel.csar.CsarToXmlConverter;
43 import org.onap.aai.babel.parser.ArtifactGeneratorToscaParser;
44 import org.onap.aai.babel.service.data.BabelArtifact;
45 import org.onap.aai.babel.testdata.CsarTest;
46 import org.onap.aai.babel.util.ArtifactTestUtils;
47 import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
50 * Tests {@link CsarToXmlConverter}.
52 public class CsarToXmlConverterTest {
54 private static final String ARTIFACT_GENERATOR_CONFIG = "artifact-generator.properties";
55 private static final String INCORRECT_CSAR_NAME = "the_name_of_the_csar_file.csar";
56 private static final String SERVICE_VERSION = "1.0";
59 if (System.getProperty("APP_HOME") == null) {
60 System.setProperty("APP_HOME", ".");
64 // The class to be tested.
65 private CsarToXmlConverter converter;
68 public ExpectedException exception = ExpectedException.none();
72 System.setProperty(ArtifactGeneratorToscaParser.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE,
73 new ArtifactTestUtils().getResourcePath(ARTIFACT_GENERATOR_CONFIG));
74 converter = new CsarToXmlConverter();
78 public void tearDown() {
82 @Test(expected = NullPointerException.class)
83 public void testNullArtifactSupplied() throws CsarConverterException {
84 converter.generateXmlFromCsar(null, null, null);
87 @Test(expected = NullPointerException.class)
88 public void testMissingName() throws CsarConverterException, IOException {
89 converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), null, null);
92 @Test(expected = NullPointerException.class)
93 public void testMissingVersion() throws CsarConverterException, IOException {
94 converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), INCORRECT_CSAR_NAME, null);
97 @Test(expected = CsarConverterException.class)
98 public void testNoPayloadExists() throws CsarConverterException {
99 converter.generateXmlFromCsar(new byte[0], INCORRECT_CSAR_NAME, SERVICE_VERSION);
102 @Test(expected = CsarConverterException.class)
103 public void testCsarFileHasNoYmlFiles() throws CsarConverterException, IOException {
104 converter.generateXmlFromCsar(CsarTest.NO_YAML_FILES.getContent(), CsarTest.NO_YAML_FILES.getName(),
109 * Test that an Exception is thrown when the Artifact Generator properties are not present.
111 * @throws CsarConverterException if there is an error either extracting the YAML files or generating XML artifacts
112 * @throws IOException if an I/O exception occurs loading the test CSAR file
115 public void testArtifactGeneratorConfigMissing() throws CsarConverterException, IOException {
116 exception.expect(CsarConverterException.class);
117 exception.expectMessage(
118 "An error occurred trying to generate XML files from a collection of YAML files :"
119 + " org.onap.aai.babel.xml.generator.XmlArtifactGenerationException: "
120 + "Error occurred during artifact generation: "
121 + "{AAI=[Cannot generate artifacts. artifactgenerator.config system property not configured]}");
123 // Unset the required system property
124 System.clearProperty(ArtifactGeneratorToscaParser.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE);
125 converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), CsarTest.SD_WAN_CSAR_FILE.getName(),
130 public void testServiceMetadataMissing()
131 throws IOException, XmlArtifactGenerationException, CsarConverterException {
132 converter.generateXmlFromCsar(CsarTest.MISSING_METADATA_CSAR.getContent(),
133 CsarTest.MISSING_METADATA_CSAR.getName(), SERVICE_VERSION);
137 public void generateXmlFromSdWanCsar() throws IOException, CsarConverterException {
138 List<String> filesToLoad = new ArrayList<>();
139 filesToLoad.add("AAI-SD-WAN-Service-Test-service-1.0.xml");
140 filesToLoad.add("AAI-SdWanTestVsp..DUMMY..module-0-resource-2.xml");
141 filesToLoad.add("AAI-Tunnel_XConnTest-resource-2.0.xml");
142 filesToLoad.add("AAI-SD-WAN-Test-VSP-resource-1.0.xml");
143 assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad), CsarTest.SD_WAN_CSAR_FILE);
147 public void generatePortMirrorConfigurationModel()
148 throws CsarConverterException, IOException, XmlArtifactGenerationException {
149 List<String> filesToLoad = new ArrayList<>();
150 filesToLoad.add("AAI-Port Mirror_Test-service-1.0.xml");
151 filesToLoad.add("AAI-Port Mirroring Configuration-resource-35.0.xml");
152 assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad), CsarTest.PORT_MIRROR_CSAR);
155 public Matcher<String> matches(final String expected) {
156 return new BaseMatcher<String>() {
157 protected String theExpected = expected;
160 public boolean matches(Object item) {
161 return new ArtifactTestUtils().compareXmlStrings((String) item, theExpected);
165 public void describeTo(Description description) {
166 description.appendText(theExpected.toString());
171 private Map<String, String> createExpectedXmlFiles(List<String> filesToLoad) throws IOException {
172 Map<String, String> xmlMap = new HashMap<>();
173 for (String filename : filesToLoad) {
174 xmlMap.put(filename, new ArtifactTestUtils().loadResourceAsString("generatedXml/" + filename));
179 private void assertThatGeneratedFilesMatchExpected(Map<String, String> expectedXmlFiles, CsarTest csarFile)
180 throws CsarConverterException, IOException {
181 List<BabelArtifact> generatedArtifacts = converter.generateXmlFromCsar(csarFile.getContent(),
182 csarFile.getName(), SERVICE_VERSION);
183 assertThat("Incorrect number of files generated", //
184 generatedArtifacts.size(), is(equalTo(expectedXmlFiles.size())));
186 .forEach(ga -> assertThat("The content of " + ga.getName() + " must match the expected content",
187 ga.getPayload(), matches(expectedXmlFiles.get(ga.getName()))));