2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.babel.service;
25 import java.io.BufferedReader;
26 import java.io.ByteArrayInputStream;
27 import java.io.IOException;
28 import java.io.InputStreamReader;
30 import java.util.Base64;
31 import java.util.HashMap;
32 import java.util.List;
34 import java.util.stream.Collectors;
35 import org.custommonkey.xmlunit.XMLTestCase;
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.junit.runner.RunWith;
42 import org.onap.aai.babel.csar.CsarConverterException;
43 import org.onap.aai.babel.csar.CsarToXmlConverter;
44 import org.onap.aai.babel.service.data.BabelArtifact;
45 import org.onap.aai.babel.util.ArtifactTestUtils;
46 import org.onap.aai.babel.xml.generator.ModelGenerator;
47 import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
48 import org.powermock.core.classloader.annotations.PrepareForTest;
49 import org.powermock.modules.junit4.PowerMockRunner;
50 import org.xml.sax.SAXException;
53 * Tests {@link CsarToXmlConverter}
55 @RunWith(PowerMockRunner.class)
56 @PrepareForTest(ModelGenerator.class)
57 public class CsarToXmlConverterTest extends XMLTestCase {
59 private static final String NAME = "the_name_of_the_csar_file.csar";
60 private static final String VERSION = "v1";
62 private CsarToXmlConverter converter;
65 public ExpectedException exception = ExpectedException.none();
69 converter = new CsarToXmlConverter();
70 URL url = CsarToXmlConverterTest.class.getClassLoader().getResource("artifact-generator.properties");
71 System.setProperty("artifactgenerator.config", url.getPath());
75 public void tearDown() {
79 @Test(expected = NullPointerException.class)
80 public void generateXmlFromCsar_nullArtifactSupplied() throws CsarConverterException {
81 converter.generateXmlFromCsar(null, null, null);
84 @Test(expected = NullPointerException.class)
85 public void generateXmlFromCsar_missingName() throws CsarConverterException, IOException {
86 byte[] csarArchive = new ArtifactTestUtils().loadResource("compressedArtifacts/service-VscpaasTest-csar.csar");
87 converter.generateXmlFromCsar(csarArchive, null, null);
90 @Test(expected = NullPointerException.class)
91 public void generateXmlFromCsar_missingVersion() throws CsarConverterException, IOException {
92 byte[] csarArchive = new ArtifactTestUtils().loadResource("compressedArtifacts/service-VscpaasTest-csar.csar");
93 converter.generateXmlFromCsar(csarArchive, NAME, null);
96 @Test(expected = CsarConverterException.class)
97 public void generateXmlFromCsar_noPayloadExists() throws CsarConverterException {
98 converter.generateXmlFromCsar(new byte[0], NAME, VERSION);
101 @Test(expected = CsarConverterException.class)
102 public void generateXmlFromCsar_csarFileHasNoYmlFiles() throws CsarConverterException, IOException {
103 byte[] csarArchive = new ArtifactTestUtils().loadResource("compressedArtifacts/noYmlFilesArchive.zip");
104 converter.generateXmlFromCsar(csarArchive, "noYmlFilesArchive.zip", 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");
114 new ArtifactTestUtils().loadResource("compressedArtifacts/service-SdWanServiceTest-csar.csar");
116 // Unset the required system property
117 System.clearProperty("artifactgenerator.config");
118 converter.generateXmlFromCsar(csarArchive, VERSION, "service-SdWanServiceTest-csar.csar");
122 public void generateXmlFromCsar() throws CsarConverterException, IOException, XmlArtifactGenerationException {
124 new ArtifactTestUtils().loadResource("compressedArtifacts/service-SdWanServiceTest-csar.csar");
126 Map<String, String> expectedXmlFiles = createExpectedXmlFiles();
127 List<BabelArtifact> generatedArtifacts =
128 converter.generateXmlFromCsar(csarArchive, VERSION, "service-SdWanServiceTest-csar.csar");
130 generatedArtifacts.forEach(ga -> {
133 String x1 = expectedXmlFiles.get(ga.getName());
135 String x2 = bytesToString(ga.getPayload());
137 assertXMLEqual("The content of " + ga.getName() + " must match the expected content", x1, x2);
139 } catch (SAXException | IOException e) {
140 fail("There was an Exception parsing the XML: "+e.getMessage());
145 public String bytesToString(byte[] source) {
146 ByteArrayInputStream bis = new ByteArrayInputStream(Base64.getDecoder().decode(source));
148 String result = new BufferedReader(new InputStreamReader(bis)).lines().collect(Collectors.joining("\n"));
154 private Map<String, String> createExpectedXmlFiles() throws IOException {
155 Map<String, String> xml = new HashMap<>();
157 ArtifactTestUtils utils = new ArtifactTestUtils();
159 String[] filesToLoad =
160 {"AAI-SD-WAN-Service-Test-service-1.0.xml", "AAI-SdWanTestVsp..DUMMY..module-0-resource-2.xml",
161 "AAI-Tunnel_XConnTest-resource-2.0.xml", "AAI-SD-WAN-Test-VSP-resource-1.0.xml"};
163 for (String s : filesToLoad) {
164 xml.put(s, utils.loadResourceAsString("generatedXml/" + s));