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