5c1e2131dfa10bdbbf2adbeee94a2a50a650b8d0
[aai/babel.git] / src / test / java / org / onap / aai / babel / service / CsarToXmlConverterTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.babel.service;
24
25 import java.io.BufferedReader;
26 import java.io.ByteArrayInputStream;
27 import java.io.IOException;
28 import java.io.InputStreamReader;
29 import java.net.URL;
30 import java.util.Base64;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
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;
51
52 /**
53  * Tests {@link CsarToXmlConverter}
54  */
55 @RunWith(PowerMockRunner.class)
56 @PrepareForTest(ModelGenerator.class)
57 public class CsarToXmlConverterTest extends XMLTestCase {
58
59     private static final String NAME = "the_name_of_the_csar_file.csar";
60     private static final String VERSION = "v1";
61
62     private CsarToXmlConverter converter;
63
64     @Rule
65     public ExpectedException exception = ExpectedException.none();
66
67     @Before
68     public void setUp() {
69         converter = new CsarToXmlConverter();
70         URL url = CsarToXmlConverterTest.class.getClassLoader().getResource("artifact-generator.properties");
71         System.setProperty("artifactgenerator.config", url.getPath());
72     }
73
74     @After
75     public void tearDown() {
76         converter = null;
77     }
78
79     @Test(expected = NullPointerException.class)
80     public void generateXmlFromCsar_nullArtifactSupplied() throws CsarConverterException {
81         converter.generateXmlFromCsar(null, null, null);
82     }
83
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);
88     }
89
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);
94     }
95
96     @Test(expected = CsarConverterException.class)
97     public void generateXmlFromCsar_noPayloadExists() throws CsarConverterException {
98         converter.generateXmlFromCsar(new byte[0], NAME, VERSION);
99     }
100
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);
105     }
106
107     @Test
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");
112
113         byte[] csarArchive =
114                 new ArtifactTestUtils().loadResource("compressedArtifacts/service-SdWanServiceTest-csar.csar");
115
116         // Unset the required system property
117         System.clearProperty("artifactgenerator.config");
118         converter.generateXmlFromCsar(csarArchive, VERSION, "service-SdWanServiceTest-csar.csar");
119     }
120
121     @Test
122     public void generateXmlFromCsar() throws CsarConverterException, IOException, XmlArtifactGenerationException {
123         byte[] csarArchive =
124                 new ArtifactTestUtils().loadResource("compressedArtifacts/service-SdWanServiceTest-csar.csar");
125
126         Map<String, String> expectedXmlFiles = createExpectedXmlFiles();
127         List<BabelArtifact> generatedArtifacts =
128                 converter.generateXmlFromCsar(csarArchive, VERSION, "service-SdWanServiceTest-csar.csar");
129
130         generatedArtifacts.forEach(ga -> {
131             try {
132
133                 String x1 = expectedXmlFiles.get(ga.getName());
134
135                 String x2 = bytesToString(ga.getPayload());
136
137                 assertXMLEqual("The content of " + ga.getName() + " must match the expected content", x1, x2);
138
139             } catch (SAXException | IOException e) {
140                 fail("There was an Exception parsing the XML: "+e.getMessage());
141             }
142         });
143     }
144
145     public String bytesToString(byte[] source) {
146         ByteArrayInputStream bis = new ByteArrayInputStream(Base64.getDecoder().decode(source));
147
148         String result = new BufferedReader(new InputStreamReader(bis)).lines().collect(Collectors.joining("\n"));
149
150         return result;
151
152     }
153
154     private Map<String, String> createExpectedXmlFiles() throws IOException {
155         Map<String, String> xml = new HashMap<>();
156
157         ArtifactTestUtils utils = new ArtifactTestUtils();
158
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"};
162
163         for (String s : filesToLoad) {
164             xml.put(s, utils.loadResourceAsString("generatedXml/" + s));
165
166         }
167
168         return xml;
169     }
170 }