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 FILTER_TYPES_CONFIG = "filter-types.properties";
57 private static final String INCORRECT_CSAR_NAME = "the_name_of_the_csar_file.csar";
58 private static final String SERVICE_VERSION = "1.0";
61 if (System.getProperty("APP_HOME") == null) {
62 System.setProperty("APP_HOME", ".");
66 // The class to be tested.
67 private CsarToXmlConverter converter;
70 public ExpectedException exception = ExpectedException.none();
74 System.setProperty(ArtifactGeneratorToscaParser.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE,
75 new ArtifactTestUtils().getResourcePath(ARTIFACT_GENERATOR_CONFIG));
77 System.setProperty(ArtifactGeneratorToscaParser.PROPERTY_GROUP_FILTERS_CONFIG_FILE,
78 new ArtifactTestUtils().getResourcePath(FILTER_TYPES_CONFIG));
80 converter = new CsarToXmlConverter();
84 public void tearDown() {
88 @Test(expected = NullPointerException.class)
89 public void testNullArtifactSupplied() throws CsarConverterException {
90 converter.generateXmlFromCsar(null, null, null);
93 @Test(expected = NullPointerException.class)
94 public void testMissingName() throws CsarConverterException, IOException {
95 converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), null, null);
98 @Test(expected = NullPointerException.class)
99 public void testMissingVersion() throws CsarConverterException, IOException {
100 converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), INCORRECT_CSAR_NAME, null);
103 @Test(expected = CsarConverterException.class)
104 public void testNoPayloadExists() throws CsarConverterException {
105 converter.generateXmlFromCsar(new byte[0], INCORRECT_CSAR_NAME, SERVICE_VERSION);
108 @Test(expected = CsarConverterException.class)
109 public void testCsarFileHasNoYmlFiles() throws CsarConverterException, IOException {
110 converter.generateXmlFromCsar(CsarTest.NO_YAML_FILES.getContent(), CsarTest.NO_YAML_FILES.getName(),
115 * Test that an Exception is thrown when the Artifact Generator properties are not present.
117 * @throws CsarConverterException if there is an error either extracting the YAML files or generating XML artifacts
118 * @throws IOException if an I/O exception occurs loading the test CSAR file
119 * @throws IOException
120 * @throws XmlArtifactGenerationException
121 * @throws CsarConverterException
124 public void testArtifactGeneratorConfigMissing() throws CsarConverterException, IOException {
125 exception.expect(CsarConverterException.class);
126 exception.expectMessage("Cannot generate artifacts. System property artifactgenerator.config not configured");
128 // Unset the required system property
129 System.clearProperty(ArtifactGeneratorToscaParser.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE);
130 converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), CsarTest.SD_WAN_CSAR_FILE.getName(),
135 * Test that an Exception is thrown when the Artifact Generator's Group Filter properties are not present.
137 * @throws IOException
138 * @throws XmlArtifactGenerationException
139 * @throws CsarConverterException
142 public void generateXmlFromCsarFilterTypesSystemPropertyNotSet()
143 throws IOException, XmlArtifactGenerationException, CsarConverterException {
144 exception.expect(CsarConverterException.class);
145 exception.expectMessage("Cannot generate artifacts. System property groupfilter.config not configured");
147 // Unset the required system property
148 System.clearProperty(ArtifactGeneratorToscaParser.PROPERTY_GROUP_FILTERS_CONFIG_FILE);
149 converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), CsarTest.SD_WAN_CSAR_FILE.getName(),
154 public void testServiceMetadataMissing()
155 throws IOException, XmlArtifactGenerationException, CsarConverterException {
156 converter.generateXmlFromCsar(CsarTest.MISSING_METADATA_CSAR.getContent(),
157 CsarTest.MISSING_METADATA_CSAR.getName(), SERVICE_VERSION);
161 public void generateXmlFromSdWanCsar() throws IOException, CsarConverterException {
162 List<String> filesToLoad = new ArrayList<>();
163 filesToLoad.add("AAI-SD-WAN-Service-Test-service-1.0.xml");
164 filesToLoad.add("AAI-SdWanTestVsp..DUMMY..module-0-resource-2.xml");
165 filesToLoad.add("AAI-Tunnel_XConnTest-resource-2.0.xml");
166 filesToLoad.add("AAI-SD-WAN-Test-VSP-resource-1.0.xml");
167 assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad), CsarTest.SD_WAN_CSAR_FILE);
171 public void generatePortMirrorConfigurationModel()
172 throws CsarConverterException, IOException, XmlArtifactGenerationException {
173 List<String> filesToLoad = new ArrayList<>();
174 filesToLoad.add("AAI-Port Mirror_Test-service-1.0.xml");
175 filesToLoad.add("AAI-Port Mirroring Configuration-resource-35.0.xml");
176 assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad), CsarTest.PORT_MIRROR_CSAR);
179 public Matcher<String> matches(final String expected) {
180 return new BaseMatcher<String>() {
181 protected String theExpected = expected;
184 public boolean matches(Object item) {
185 return new ArtifactTestUtils().compareXmlStrings((String) item, theExpected);
189 public void describeTo(Description description) {
190 description.appendText(theExpected.toString());
195 private Map<String, String> createExpectedXmlFiles(List<String> filesToLoad) throws IOException {
196 Map<String, String> xmlMap = new HashMap<>();
197 for (String filename : filesToLoad) {
198 xmlMap.put(filename, new ArtifactTestUtils().loadResourceAsString("generatedXml/" + filename));
203 private void assertThatGeneratedFilesMatchExpected(Map<String, String> expectedXmlFiles, CsarTest csarFile)
204 throws CsarConverterException, IOException {
205 List<BabelArtifact> generatedArtifacts = converter.generateXmlFromCsar(csarFile.getContent(),
206 csarFile.getName(), SERVICE_VERSION);
207 assertThat("Incorrect number of files generated", //
208 generatedArtifacts.size(), is(equalTo(expectedXmlFiles.size())));
210 .forEach(ga -> assertThat("The content of " + ga.getName() + " must match the expected content",
211 ga.getPayload(), matches(expectedXmlFiles.get(ga.getName()))));