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=========================================================
21 package org.onap.aai.babel.service;
23 import static org.junit.Assert.assertThat;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
31 import org.hamcrest.BaseMatcher;
32 import org.hamcrest.Description;
33 import org.hamcrest.Matcher;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.rules.ExpectedException;
39 import org.onap.aai.babel.csar.CsarConverterException;
40 import org.onap.aai.babel.csar.CsarToXmlConverter;
41 import org.onap.aai.babel.service.data.BabelArtifact;
42 import org.onap.aai.babel.util.ArtifactTestUtils;
43 import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
44 import org.onap.aai.babel.xml.generator.data.GeneratorConstants;
47 * Tests {@link CsarToXmlConverter}
49 public class CsarToXmlConverterTest {
51 private static final String ARTIFACT_GENERATOR_CONFIG = "artifact-generator.properties";
52 private static final String CSAR_FOLDER = "compressedArtifacts";
53 private static final String VALID_CSAR_FILE = "service-SdWanServiceTest-csar.csar";
54 private static final String INCORRECT_CSAR_NAME = "the_name_of_the_csar_file.csar";
55 private static final String SERVICE_VERSION = "1.0";
57 private CsarToXmlConverter converter;
60 if (System.getProperty("AJSC_HOME") == null) {
61 System.setProperty("AJSC_HOME", ".");
66 public ExpectedException exception = ExpectedException.none();
67 private ArtifactTestUtils artifactTestUtils;
71 System.setProperty(GeneratorConstants.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE,
72 CsarToXmlConverterTest.class.getClassLoader().getResource(ARTIFACT_GENERATOR_CONFIG).getPath());
73 converter = new CsarToXmlConverter();
74 artifactTestUtils = new ArtifactTestUtils();
78 public void tearDown() {
82 @Test(expected = NullPointerException.class)
83 public void generateXmlFromCsar_nullArtifactSupplied() throws CsarConverterException {
84 converter.generateXmlFromCsar(null, null, null);
87 @Test(expected = NullPointerException.class)
88 public void generateXmlFromCsar_missingName() throws CsarConverterException, IOException {
89 converter.generateXmlFromCsar(getCsar(VALID_CSAR_FILE), null, null);
92 @Test(expected = NullPointerException.class)
93 public void generateXmlFromCsar_missingVersion() throws CsarConverterException, IOException {
94 converter.generateXmlFromCsar(getCsar(VALID_CSAR_FILE), INCORRECT_CSAR_NAME, null);
97 @Test(expected = CsarConverterException.class)
98 public void generateXmlFromCsar_noPayloadExists() throws CsarConverterException {
99 converter.generateXmlFromCsar(new byte[0], INCORRECT_CSAR_NAME, SERVICE_VERSION);
102 @Test(expected = CsarConverterException.class)
103 public void generateXmlFromCsar_csarFileHasNoYmlFiles() throws CsarConverterException, IOException {
104 converter.generateXmlFromCsar(getCsar("noYmlFilesArchive.zip"), "noYmlFilesArchive.zip", SERVICE_VERSION);
108 public void generateXmlFromCsar_artifactgenerator_config_systemPropertyNotSet()
109 throws IOException, XmlArtifactGenerationException, CsarConverterException {
110 exception.expect(CsarConverterException.class);
111 exception.expectMessage("Cannot generate artifacts. artifactgenerator.config system property not configured");
113 // Unset the required system property
114 System.clearProperty(GeneratorConstants.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE);
115 converter.generateXmlFromCsar(getCsar(VALID_CSAR_FILE), VALID_CSAR_FILE, SERVICE_VERSION);
119 public void generateXmlFromCsar() throws CsarConverterException, IOException, XmlArtifactGenerationException {
120 Map<String, String> expectedXmlFiles = createExpectedXmlFiles();
121 List<BabelArtifact> generatedArtifacts =
122 converter.generateXmlFromCsar(getCsar(VALID_CSAR_FILE), VALID_CSAR_FILE, SERVICE_VERSION);
125 .forEach(ga -> assertThat("The content of " + ga.getName() + " must match the expected content",
126 ga.getPayload(), matches(expectedXmlFiles.get(ga.getName()))));
129 public Matcher<String> matches(final String expected) {
130 return new BaseMatcher<String>() {
131 protected String theExpected = expected;
134 public boolean matches(Object o) {
135 return artifactTestUtils.compareXMLStrings((String) o, theExpected);
139 public void describeTo(Description description) {
140 description.appendText(theExpected.toString());
145 private byte[] getCsar(String csarFileName) throws IOException {
146 return artifactTestUtils.loadResource(CSAR_FOLDER + File.separator + csarFileName);
149 private Map<String, String> createExpectedXmlFiles() throws IOException {
150 Map<String, String> xmlMap = new HashMap<>();
152 List<String> filesToLoad = new ArrayList<>();
153 filesToLoad.add("AAI-SD-WAN-Service-Test-service-1.0.xml");
154 filesToLoad.add("AAI-SdWanTestVsp..DUMMY..module-0-resource-2.xml");
155 filesToLoad.add("AAI-Tunnel_XConnTest-resource-2.0.xml");
156 filesToLoad.add("AAI-SD-WAN-Test-VSP-resource-1.0.xml");
158 for (String filename : filesToLoad) {
159 xmlMap.put(filename, artifactTestUtils.loadResourceAsString("generatedXml" + File.separator + filename));