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;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
30 import org.hamcrest.BaseMatcher;
31 import org.hamcrest.Description;
32 import org.hamcrest.Matcher;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.rules.ExpectedException;
38 import org.onap.aai.babel.csar.CsarConverterException;
39 import org.onap.aai.babel.csar.CsarToXmlConverter;
40 import org.onap.aai.babel.service.data.BabelArtifact;
41 import org.onap.aai.babel.util.ArtifactTestUtils;
42 import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
43 import org.onap.aai.babel.xml.generator.data.GeneratorConstants;
46 * Tests {@link CsarToXmlConverter}
48 public class CsarToXmlConverterTest {
50 private static final String ARTIFACT_GENERATOR_CONFIG = "artifact-generator.properties";
51 private static final String INCORRECT_CSAR_NAME = "the_name_of_the_csar_file.csar";
52 private static final String SERVICE_VERSION = "1.0";
55 if (System.getProperty("APP_HOME") == null) {
56 System.setProperty("APP_HOME", ".");
60 private enum CsarTest {
61 VALID_CSAR_FILE("service-SdWanServiceTest-csar.csar"), NO_YAML_FILES("noYmlFilesArchive.zip");
63 private String filename;
64 private ArtifactTestUtils artifactTestUtils = new ArtifactTestUtils();
66 CsarTest(String filename) {
67 this.filename = filename;
70 public String getName() {
74 public byte[] getContent() throws IOException {
75 return artifactTestUtils.getCompressedArtifact(filename);
79 // The class to be tested.
80 private CsarToXmlConverter converter;
83 public ExpectedException exception = ExpectedException.none();
87 System.setProperty(GeneratorConstants.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE,
88 new ArtifactTestUtils().getResourcePath(ARTIFACT_GENERATOR_CONFIG));
89 converter = new CsarToXmlConverter();
93 public void tearDown() {
97 @Test(expected = NullPointerException.class)
98 public void generateXmlFromCsar_nullArtifactSupplied() throws CsarConverterException {
99 converter.generateXmlFromCsar(null, null, null);
102 @Test(expected = NullPointerException.class)
103 public void generateXmlFromCsar_missingName() throws CsarConverterException, IOException {
104 converter.generateXmlFromCsar(CsarTest.VALID_CSAR_FILE.getContent(), null, null);
107 @Test(expected = NullPointerException.class)
108 public void generateXmlFromCsar_missingVersion() throws CsarConverterException, IOException {
109 converter.generateXmlFromCsar(CsarTest.VALID_CSAR_FILE.getContent(), INCORRECT_CSAR_NAME, null);
112 @Test(expected = CsarConverterException.class)
113 public void generateXmlFromCsar_noPayloadExists() throws CsarConverterException {
114 converter.generateXmlFromCsar(new byte[0], INCORRECT_CSAR_NAME, SERVICE_VERSION);
117 @Test(expected = CsarConverterException.class)
118 public void generateXmlFromCsar_csarFileHasNoYmlFiles() throws CsarConverterException, IOException {
119 converter.generateXmlFromCsar(CsarTest.NO_YAML_FILES.getContent(), "noYmlFilesArchive.zip", SERVICE_VERSION);
123 public void testArtifactGeneratorConfigMissing()
124 throws IOException, XmlArtifactGenerationException, CsarConverterException {
125 exception.expect(CsarConverterException.class);
126 exception.expectMessage("Cannot generate artifacts. artifactgenerator.config system property not configured");
128 // Unset the required system property
129 System.clearProperty(GeneratorConstants.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE);
130 converter.generateXmlFromCsar(CsarTest.VALID_CSAR_FILE.getContent(), CsarTest.VALID_CSAR_FILE.getName(),
135 public void generateXmlFromCsar() throws CsarConverterException, IOException, XmlArtifactGenerationException {
136 Map<String, String> expectedXmlFiles = createExpectedXmlFiles();
137 List<BabelArtifact> generatedArtifacts = converter.generateXmlFromCsar(CsarTest.VALID_CSAR_FILE.getContent(),
138 CsarTest.VALID_CSAR_FILE.getName(), SERVICE_VERSION);
141 .forEach(ga -> assertThat("The content of " + ga.getName() + " must match the expected content",
142 ga.getPayload(), matches(expectedXmlFiles.get(ga.getName()))));
145 public Matcher<String> matches(final String expected) {
146 return new BaseMatcher<String>() {
147 protected String theExpected = expected;
150 public boolean matches(Object item) {
151 return new ArtifactTestUtils().compareXmlStrings((String) item, theExpected);
155 public void describeTo(Description description) {
156 description.appendText(theExpected.toString());
161 private Map<String, String> createExpectedXmlFiles() throws IOException {
162 Map<String, String> xmlMap = new HashMap<>();
164 List<String> filesToLoad = new ArrayList<>();
165 filesToLoad.add("AAI-SD-WAN-Service-Test-service-1.0.xml");
166 filesToLoad.add("AAI-SdWanTestVsp..DUMMY..module-0-resource-2.xml");
167 filesToLoad.add("AAI-Tunnel_XConnTest-resource-2.0.xml");
168 filesToLoad.add("AAI-SD-WAN-Test-VSP-resource-1.0.xml");
170 for (String filename : filesToLoad) {
171 xmlMap.put(filename, new ArtifactTestUtils().loadResourceAsString("generatedXml/" + filename));