2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2019 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.hamcrest.CoreMatchers.not;
27 import static org.hamcrest.CoreMatchers.nullValue;
28 import static org.junit.Assert.assertThat;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
35 import org.hamcrest.BaseMatcher;
36 import org.hamcrest.Description;
37 import org.hamcrest.Matcher;
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.junit.rules.ExpectedException;
43 import org.onap.aai.babel.csar.CsarConverterException;
44 import org.onap.aai.babel.csar.CsarToXmlConverter;
45 import org.onap.aai.babel.parser.ArtifactGeneratorToscaParser;
46 import org.onap.aai.babel.service.data.BabelArtifact;
47 import org.onap.aai.babel.testdata.CsarTest;
48 import org.onap.aai.babel.util.ArtifactTestUtils;
49 import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
50 import org.xml.sax.SAXException;
53 * Tests {@link CsarToXmlConverter}.
55 public class CsarToXmlConverterTest {
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 new ArtifactTestUtils().setGeneratorSystemProperties();
75 converter = new CsarToXmlConverter();
79 public void tearDown() {
83 @Test(expected = NullPointerException.class)
84 public void testNullArtifactSupplied() throws CsarConverterException {
85 converter.generateXmlFromCsar(null, null, null);
88 @Test(expected = NullPointerException.class)
89 public void testMissingName() throws CsarConverterException, IOException {
90 converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), null, null);
93 @Test(expected = NullPointerException.class)
94 public void testMissingVersion() throws CsarConverterException, IOException {
95 converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), INCORRECT_CSAR_NAME, null);
98 @Test(expected = CsarConverterException.class)
99 public void testNoPayloadExists() throws CsarConverterException {
100 converter.generateXmlFromCsar(new byte[0], INCORRECT_CSAR_NAME, SERVICE_VERSION);
103 @Test(expected = CsarConverterException.class)
104 public void testCsarFileHasNoYmlFiles() throws CsarConverterException, IOException {
105 converter.generateXmlFromCsar(CsarTest.NO_YAML_FILES.getContent(), CsarTest.NO_YAML_FILES.getName(),
110 * Test that an Exception is thrown when the Artifact Generator properties are not present.
112 * @throws CsarConverterException
113 * if there is an error either extracting the YAML files or generating XML artifacts
114 * @throws IOException
115 * if an I/O exception occurs loading the test CSAR file
116 * @throws IOException
117 * @throws XmlArtifactGenerationException
118 * @throws CsarConverterException
121 public void testArtifactGeneratorConfigMissing() throws CsarConverterException, IOException {
122 exception.expect(CsarConverterException.class);
123 exception.expectMessage("Cannot generate artifacts. System property artifactgenerator.config not configured");
125 // Unset the required system property
126 System.clearProperty(ArtifactGeneratorToscaParser.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE);
127 converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), CsarTest.SD_WAN_CSAR_FILE.getName(),
132 * Test that an Exception is thrown when the Artifact Generator's Group Filter properties are not present.
134 * @throws IOException
135 * @throws XmlArtifactGenerationException
136 * @throws CsarConverterException
139 public void generateXmlFromCsarFilterTypesSystemPropertyNotSet()
140 throws IOException, XmlArtifactGenerationException, CsarConverterException {
141 exception.expect(CsarConverterException.class);
142 exception.expectMessage("Cannot generate artifacts. System property "
143 + ArtifactGeneratorToscaParser.PROPERTY_GROUP_FILTERS_CONFIG_FILE + " not configured");
145 // Unset the required system property
146 System.clearProperty(ArtifactGeneratorToscaParser.PROPERTY_GROUP_FILTERS_CONFIG_FILE);
147 converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), CsarTest.SD_WAN_CSAR_FILE.getName(),
152 public void testServiceMetadataMissing()
153 throws IOException, XmlArtifactGenerationException, CsarConverterException {
154 converter.generateXmlFromCsar(CsarTest.MISSING_METADATA_CSAR.getContent(),
155 CsarTest.MISSING_METADATA_CSAR.getName(), SERVICE_VERSION);
159 public void generateXmlFromSdWanCsar() throws IOException, CsarConverterException {
160 List<String> filesToLoad = new ArrayList<>();
161 filesToLoad.add("AAI-SD-WAN-Service-Test-service-1.0.xml");
162 filesToLoad.add("AAI-SdWanTestVsp..DUMMY..module-0-resource-2.xml");
163 filesToLoad.add("AAI-Tunnel_XConnTest-resource-2.0.xml");
164 filesToLoad.add("AAI-SD-WAN-Test-VSP-resource-1.0.xml");
165 assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad), CsarTest.SD_WAN_CSAR_FILE);
169 public void generateXmlFromNetworkCollectionCsar() throws IOException, CsarConverterException {
170 List<String> filesToLoad = new ArrayList<>();
171 filesToLoad.add("AAI-TEST SVC_1-service-1.0.xml");
172 filesToLoad.add("AAI-TEST CR_1-resource-7.0.xml");
173 filesToLoad.add("AAI-testcr_1..NetworkCollection..0-resource-1.xml");
174 filesToLoad.add("AAI-ExtVL-resource-40.0.xml");
175 assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad),
176 CsarTest.NETWORK_COLLECTION_CSAR_FILE);
180 public void generatePortMirrorConfigurationModel()
181 throws CsarConverterException, IOException, XmlArtifactGenerationException {
182 List<String> filesToLoad = new ArrayList<>();
183 filesToLoad.add("AAI-Port Mirror_Test-service-1.0.xml");
184 filesToLoad.add("AAI-Port Mirroring Configuration-resource-35.0.xml");
185 assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad), CsarTest.PORT_MIRROR_CSAR);
189 public void generateXmlFromServiceProxyCsar()
190 throws CsarConverterException, IOException, XmlArtifactGenerationException {
191 List<String> filesToLoad = new ArrayList<>();
192 filesToLoad.add("AAI-Grouping Service for Test-service-1.0.xml");
193 filesToLoad.add("AAI-groupingservicefortest..ResourceInstanceGroup..0-resource-1.xml");
194 filesToLoad.add("AAI-groupingservicefortest..ResourceInstanceGroup..1-resource-1.xml");
195 assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad), CsarTest.SERVICE_PROXY_CSAR_FILE);
198 public Matcher<String> matches(final String expected) {
199 return new BaseMatcher<String>() {
200 protected String theExpected = expected;
203 public boolean matches(Object item) {
205 return new ArtifactTestUtils().compareXmlStrings((String) item, theExpected);
206 } catch (SAXException | IOException e) {
207 throw new RuntimeException(e);
212 public void describeTo(Description description) {
213 description.appendText(theExpected.toString());
218 private Map<String, String> createExpectedXmlFiles(List<String> filesToLoad) throws IOException {
219 Map<String, String> xmlMap = new HashMap<>();
220 for (String filename : filesToLoad) {
221 xmlMap.put(filename, new ArtifactTestUtils().loadResourceAsString("generatedXml/" + filename));
226 private void assertThatGeneratedFilesMatchExpected(Map<String, String> expectedXmlFiles, CsarTest csarFile)
227 throws CsarConverterException, IOException {
228 List<BabelArtifact> generatedArtifacts =
229 converter.generateXmlFromCsar(csarFile.getContent(), csarFile.getName(), SERVICE_VERSION);
230 assertThat("Incorrect number of files generated", //
231 generatedArtifacts.size(), is(equalTo(expectedXmlFiles.size())));
232 for (BabelArtifact generated : generatedArtifacts) {
233 String fileName = generated.getName();
234 String expectedXml = expectedXmlFiles.get(fileName);
235 assertThat("Missing expected content for " + generated.getName(), expectedXml, is(not(nullValue())));
236 assertThat("The content of " + generated.getName() + " must match the expected content",
237 generated.getPayload(), matches(expectedXml));