Throw an Exception for XML test file mismatches
[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-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
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
22 package org.onap.aai.babel.service;
23
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;
29
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
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;
51
52 /**
53  * Tests {@link CsarToXmlConverter}.
54  */
55 public class CsarToXmlConverterTest {
56
57     private static final String ARTIFACT_GENERATOR_CONFIG = "artifact-generator.properties";
58     private static final String FILTER_TYPES_CONFIG = "filter-types.properties";
59
60     private static final String INCORRECT_CSAR_NAME = "the_name_of_the_csar_file.csar";
61     private static final String SERVICE_VERSION = "1.0";
62
63     static {
64         if (System.getProperty("APP_HOME") == null) {
65             System.setProperty("APP_HOME", ".");
66         }
67     }
68
69     // The class to be tested.
70     private CsarToXmlConverter converter;
71
72     @Rule
73     public ExpectedException exception = ExpectedException.none();
74
75     @Before
76     public void setup() {
77         System.setProperty(ArtifactGeneratorToscaParser.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE,
78                 new ArtifactTestUtils().getResourcePath(ARTIFACT_GENERATOR_CONFIG));
79
80         System.setProperty(ArtifactGeneratorToscaParser.PROPERTY_GROUP_FILTERS_CONFIG_FILE,
81                 new ArtifactTestUtils().getResourcePath(FILTER_TYPES_CONFIG));
82
83         converter = new CsarToXmlConverter();
84     }
85
86     @After
87     public void tearDown() {
88         converter = null;
89     }
90
91     @Test(expected = NullPointerException.class)
92     public void testNullArtifactSupplied() throws CsarConverterException {
93         converter.generateXmlFromCsar(null, null, null);
94     }
95
96     @Test(expected = NullPointerException.class)
97     public void testMissingName() throws CsarConverterException, IOException {
98         converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), null, null);
99     }
100
101     @Test(expected = NullPointerException.class)
102     public void testMissingVersion() throws CsarConverterException, IOException {
103         converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), INCORRECT_CSAR_NAME, null);
104     }
105
106     @Test(expected = CsarConverterException.class)
107     public void testNoPayloadExists() throws CsarConverterException {
108         converter.generateXmlFromCsar(new byte[0], INCORRECT_CSAR_NAME, SERVICE_VERSION);
109     }
110
111     @Test(expected = CsarConverterException.class)
112     public void testCsarFileHasNoYmlFiles() throws CsarConverterException, IOException {
113         converter.generateXmlFromCsar(CsarTest.NO_YAML_FILES.getContent(), CsarTest.NO_YAML_FILES.getName(),
114                 SERVICE_VERSION);
115     }
116
117     /**
118      * Test that an Exception is thrown when the Artifact Generator properties are not present.
119      *
120      * @throws CsarConverterException
121      *         if there is an error either extracting the YAML files or generating XML artifacts
122      * @throws IOException
123      *         if an I/O exception occurs loading the test CSAR file
124      * @throws IOException
125      * @throws XmlArtifactGenerationException
126      * @throws CsarConverterException
127      */
128     @Test
129     public void testArtifactGeneratorConfigMissing() throws CsarConverterException, IOException {
130         exception.expect(CsarConverterException.class);
131         exception.expectMessage("Cannot generate artifacts. System property artifactgenerator.config not configured");
132
133         // Unset the required system property
134         System.clearProperty(ArtifactGeneratorToscaParser.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE);
135         converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), CsarTest.SD_WAN_CSAR_FILE.getName(),
136                 SERVICE_VERSION);
137     }
138
139     /**
140      * Test that an Exception is thrown when the Artifact Generator's Group Filter properties are not present.
141      *
142      * @throws IOException
143      * @throws XmlArtifactGenerationException
144      * @throws CsarConverterException
145      */
146     @Test
147     public void generateXmlFromCsarFilterTypesSystemPropertyNotSet()
148             throws IOException, XmlArtifactGenerationException, CsarConverterException {
149         exception.expect(CsarConverterException.class);
150         exception.expectMessage("Cannot generate artifacts. System property groupfilter.config not configured");
151
152         // Unset the required system property
153         System.clearProperty(ArtifactGeneratorToscaParser.PROPERTY_GROUP_FILTERS_CONFIG_FILE);
154         converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), CsarTest.SD_WAN_CSAR_FILE.getName(),
155                 SERVICE_VERSION);
156     }
157
158     @Test
159     public void testServiceMetadataMissing()
160             throws IOException, XmlArtifactGenerationException, CsarConverterException {
161         converter.generateXmlFromCsar(CsarTest.MISSING_METADATA_CSAR.getContent(),
162                 CsarTest.MISSING_METADATA_CSAR.getName(), SERVICE_VERSION);
163     }
164
165     @Test
166     public void generateXmlFromSdWanCsar() throws IOException, CsarConverterException {
167         List<String> filesToLoad = new ArrayList<>();
168         filesToLoad.add("AAI-SD-WAN-Service-Test-service-1.0.xml");
169         filesToLoad.add("AAI-SdWanTestVsp..DUMMY..module-0-resource-2.xml");
170         filesToLoad.add("AAI-Tunnel_XConnTest-resource-2.0.xml");
171         filesToLoad.add("AAI-SD-WAN-Test-VSP-resource-1.0.xml");
172         assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad), CsarTest.SD_WAN_CSAR_FILE);
173     }
174
175     @Test
176     public void generateXmlFromNetworkCollectionCsar() throws IOException, CsarConverterException {
177         List<String> filesToLoad = new ArrayList<>();
178         filesToLoad.add("AAI-TEST SVC_1-service-1.0.xml");
179         filesToLoad.add("AAI-TEST CR_1-resource-7.0.xml");
180         filesToLoad.add("AAI-testcr_1..NetworkCollection..0-resource-1.xml");
181         filesToLoad.add("AAI-ExtVL-resource-40.0.xml");
182         assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad),
183                 CsarTest.NETWORK_COLLECTION_CSAR_FILE);
184     }
185
186     @Test
187     public void generatePortMirrorConfigurationModel()
188             throws CsarConverterException, IOException, XmlArtifactGenerationException {
189         List<String> filesToLoad = new ArrayList<>();
190         filesToLoad.add("AAI-Port Mirror_Test-service-1.0.xml");
191         filesToLoad.add("AAI-Port Mirroring Configuration-resource-35.0.xml");
192         assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad), CsarTest.PORT_MIRROR_CSAR);
193     }
194
195     @Test
196     public void generateXmlFromServiceProxyCsar()
197             throws CsarConverterException, IOException, XmlArtifactGenerationException {
198         List<String> filesToLoad = new ArrayList<>();
199         filesToLoad.add("AAI-Grouping Service for Test-service-1.0.xml");
200         filesToLoad.add("AAI-groupingservicefortest..ResourceInstanceGroup..0-resource-1.xml");
201         filesToLoad.add("AAI-groupingservicefortest..ResourceInstanceGroup..1-resource-1.xml");
202         assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad), CsarTest.SERVICE_PROXY_CSAR_FILE);
203     }
204
205     public Matcher<String> matches(final String expected) {
206         return new BaseMatcher<String>() {
207             protected String theExpected = expected;
208
209             @Override
210             public boolean matches(Object item) {
211                 try {
212                     return new ArtifactTestUtils().compareXmlStrings((String) item, theExpected);
213                 } catch (SAXException | IOException e) {
214                     throw new RuntimeException(e);
215                 }
216             }
217
218             @Override
219             public void describeTo(Description description) {
220                 description.appendText(theExpected.toString());
221             }
222         };
223     }
224
225     private Map<String, String> createExpectedXmlFiles(List<String> filesToLoad) throws IOException {
226         Map<String, String> xmlMap = new HashMap<>();
227         for (String filename : filesToLoad) {
228             xmlMap.put(filename, new ArtifactTestUtils().loadResourceAsString("generatedXml/" + filename));
229         }
230         return xmlMap;
231     }
232
233     private void assertThatGeneratedFilesMatchExpected(Map<String, String> expectedXmlFiles, CsarTest csarFile)
234             throws CsarConverterException, IOException {
235         List<BabelArtifact> generatedArtifacts =
236                 converter.generateXmlFromCsar(csarFile.getContent(), csarFile.getName(), SERVICE_VERSION);
237         assertThat("Incorrect number of files generated", //
238                 generatedArtifacts.size(), is(equalTo(expectedXmlFiles.size())));
239         for (BabelArtifact generated : generatedArtifacts) {
240             String fileName = generated.getName();
241             String expectedXml = expectedXmlFiles.get(fileName);
242             assertThat("Missing expected content for " + generated.getName(), expectedXml, is(not(nullValue())));
243             assertThat("The content of " + generated.getName() + " must match the expected content",
244                     generated.getPayload(), matches(expectedXml));
245         }
246     }
247 }