Update aai-parent in babel to 1.13.3
[aai/babel.git] / src / test / java / org / onap / aai / babel / service / TestCsarToXmlConverter.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Copyright (c) 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.hamcrest.MatcherAssert.assertThat;
29 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
30 import static org.junit.jupiter.api.Assertions.assertThrows;
31 import static org.junit.jupiter.api.Assertions.assertTrue;
32
33 import java.io.IOException;
34 import java.util.ArrayList;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38 import org.hamcrest.BaseMatcher;
39 import org.hamcrest.Description;
40 import org.hamcrest.Matcher;
41 import org.junit.jupiter.api.AfterEach;
42 import org.junit.jupiter.api.BeforeEach;
43 import org.junit.jupiter.api.Test;
44 import org.onap.aai.babel.csar.CsarConverterException;
45 import org.onap.aai.babel.csar.CsarToXmlConverter;
46 import org.onap.aai.babel.parser.ArtifactGeneratorToscaParser;
47 import org.onap.aai.babel.service.data.BabelArtifact;
48 import org.onap.aai.babel.testdata.CsarTest;
49 import org.onap.aai.babel.util.ArtifactTestUtils;
50 import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
51 import org.xml.sax.SAXException;
52
53 /**
54  * Tests {@link CsarToXmlConverter}.
55  */
56 public class TestCsarToXmlConverter {
57
58     private static final String INCORRECT_CSAR_NAME = "the_name_of_the_csar_file.csar";
59     private static final String SERVICE_VERSION = "1.0";
60
61     // The class to be tested.
62     private CsarToXmlConverter converter;
63
64     @BeforeEach
65     public void setup() {
66         new ArtifactTestUtils().setGeneratorSystemProperties();
67         converter = new CsarToXmlConverter();
68     }
69
70     @AfterEach
71     public void tearDown() {
72         converter = null;
73     }
74
75     @Test
76     public void testNullArtifactSupplied() throws CsarConverterException {
77         assertThrows(NullPointerException.class, () -> {
78             converter.generateXmlFromCsar(null, null, null);
79         });
80     }
81
82     @Test
83     public void testMissingName() throws CsarConverterException, IOException {
84         assertThrows(NullPointerException.class, () -> {
85             converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), null, null);
86         });
87     }
88
89     @Test
90     public void testMissingVersion() throws CsarConverterException, IOException {
91         assertThrows(NullPointerException.class, () -> {
92             converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), INCORRECT_CSAR_NAME, null);
93         });
94     }
95
96     @Test
97     public void testNoPayloadExists() throws CsarConverterException {
98         assertThrows(CsarConverterException.class, () -> {
99             converter.generateXmlFromCsar(new byte[0], INCORRECT_CSAR_NAME, SERVICE_VERSION);
100         });
101     }
102
103     @Test
104     public void testCsarFileHasNoYmlFiles() throws CsarConverterException, IOException {
105         assertThrows(CsarConverterException.class, () -> {
106             converter.generateXmlFromCsar(CsarTest.NO_YAML_FILES.getContent(), CsarTest.NO_YAML_FILES.getName(),
107                     SERVICE_VERSION);
108         });
109     }
110
111     /**
112      * Test that an Exception is thrown when the Artifact Generator's TOSCA Mappings configuration file is not present.
113      *
114      * @throws CsarConverterException
115      *             if there is an error either extracting the YAML files or generating XML artifacts
116      * @throws IOException
117      *             if an I/O exception occurs
118      */
119     @Test
120     public void generateXmlFromCsarMappingSystemPropertyNotSet() throws CsarConverterException, IOException {
121         Throwable exception = assertThrows(IllegalArgumentException.class, () -> {
122
123             // Unset the required system property
124             System.clearProperty(ArtifactGeneratorToscaParser.PROPERTY_TOSCA_MAPPING_FILE);
125             converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), CsarTest.SD_WAN_CSAR_FILE.getName(),
126                     SERVICE_VERSION);
127         });
128         assertTrue(exception.getMessage().contains("Cannot generate artifacts. System property "
129                 + ArtifactGeneratorToscaParser.PROPERTY_TOSCA_MAPPING_FILE + " not configured"));
130     }
131
132     @Test
133     public void testServiceMetadataMissing()
134             throws IOException, CsarConverterException {
135         assertDoesNotThrow(() -> {
136             converter.generateXmlFromCsar(CsarTest.MISSING_METADATA_CSAR.getContent(),
137                     CsarTest.MISSING_METADATA_CSAR.getName(), SERVICE_VERSION);
138         });
139     }
140
141     @Test
142     public void generateXmlFromSdWanCsar() throws IOException, CsarConverterException {
143         List<String> filesToLoad = new ArrayList<>();
144         filesToLoad.add("AAI-SD-WAN-Service-Test-service-1.0.xml");
145         filesToLoad.add("AAI-SdWanTestVsp..DUMMY..module-0-resource-2.xml");
146         filesToLoad.add("AAI-Tunnel_XConnTest-resource-2.0.xml");
147         filesToLoad.add("AAI-SD-WAN-Test-VSP-resource-1.0.xml");
148         assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad), CsarTest.SD_WAN_CSAR_FILE);
149     }
150
151     @Test
152     public void generateXmlFromNetworkCollectionCsar() throws IOException, CsarConverterException {
153         List<String> filesToLoad = new ArrayList<>();
154         filesToLoad.add("AAI-TEST SVC_1-service-1.0.xml");
155         filesToLoad.add("AAI-TEST CR_1-resource-7.0.xml");
156         filesToLoad.add("AAI-testcr_1..NetworkCollection..0-resource-1.xml");
157         filesToLoad.add("AAI-ExtVL-resource-40.0.xml");
158         assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad),
159                 CsarTest.NETWORK_COLLECTION_CSAR_FILE);
160     }
161
162     @Test
163     public void generateXmlFromCosAvpnCsar() throws IOException, CsarConverterException {
164         List<String> filesToLoad = new ArrayList<>();
165         filesToLoad.add("AAI-COS_AVPN-service-1.0.xml");
166         filesToLoad.add("AAI-VLAN Network Receptor Configuration-resource-3.0.xml");
167         filesToLoad.add("AAI-VRF Entry Configuration-resource-3.0.xml");
168         assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad), CsarTest.COS_AVPN_CSAR_FILE);
169     }
170
171     @Test
172     public void generateXmlFromRgCollector615Csar() throws IOException, CsarConverterException {
173         List<String> filesToLoad = new ArrayList<>();
174         filesToLoad.add("AAI-RG_collector_6-15-service-1.0.xml");
175         filesToLoad.add("AAI-Network Res 1806-resource-4.0.xml");
176         filesToLoad.add("AAI-Network Res 1806..NetworkCollection..0-resource-1.xml");
177         filesToLoad.add("AAI-ExtVL-resource-48.0.xml");
178         assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad), CsarTest.RG_COLLECTOR_615_CSAR_FILE);
179     }
180
181     @Test
182     public void generatePortMirrorConfigurationModel()
183             throws CsarConverterException, IOException, XmlArtifactGenerationException {
184         List<String> filesToLoad = new ArrayList<>();
185         filesToLoad.add("AAI-Port Mirror_Test-service-1.0.xml");
186         filesToLoad.add("AAI-Port Mirroring Configuration-resource-35.0.xml");
187         assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad), CsarTest.PORT_MIRROR_CSAR);
188     }
189
190     @Test
191     public void generateXmlFromServiceProxyCsar()
192             throws CsarConverterException, IOException, XmlArtifactGenerationException {
193         List<String> filesToLoad = new ArrayList<>();
194         filesToLoad.add("AAI-Grouping Service for Test-service-1.0.xml");
195         filesToLoad.add("AAI-groupingservicefortest..ResourceInstanceGroup..0-resource-1.xml");
196         filesToLoad.add("AAI-groupingservicefortest..ResourceInstanceGroup..1-resource-1.xml");
197         assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad), CsarTest.SERVICE_PROXY_CSAR_FILE);
198     }
199
200
201     public void generateChildResourcesCsar()
202             throws CsarConverterException, IOException, XmlArtifactGenerationException {
203
204         List<String> filesToLoad = new ArrayList<>();
205         filesToLoad.add("AAI-Network Cloud VNF Service MOCK-service-1.0.xml");
206         filesToLoad.add("AAI-NetworkCloudVnfMock..NC1_Test_Heat_08152018_base..module-0-resource-1.xml");
207         filesToLoad.add("AAI-Network Cloud VNF MOCK-resource-1.0.xml");
208         filesToLoad.add("AAI-Fabric Configuration-resource-10.0.xml");
209         assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad), CsarTest.CHILD_RESOURCE_CSAR_FILE);
210     }
211
212     /**
213      * A Matcher for comparing generated XML Strings with expected XML content.
214      *
215      * @param expected
216      *            the expected XML String
217      * @return a new Matcher for comparing XML Strings
218      */
219     public Matcher<String> matches(final String expected) {
220         return new BaseMatcher<String>() {
221             protected String theExpected = expected;
222
223             @Override
224             public boolean matches(Object item) {
225                 try {
226                     return new ArtifactTestUtils().compareXmlStrings((String) item, theExpected);
227                 } catch (SAXException | IOException e) {
228                     throw new RuntimeException(e);
229                 }
230             }
231
232             @Override
233             public void describeTo(Description description) {
234                 description.appendText(theExpected.toString());
235             }
236         };
237     }
238
239     private Map<String, String> createExpectedXmlFiles(List<String> filesToLoad) throws IOException {
240         Map<String, String> xmlMap = new HashMap<>();
241         for (String filename : filesToLoad) {
242             xmlMap.put(filename, new ArtifactTestUtils().loadResourceAsString("generatedXml/" + filename));
243         }
244         return xmlMap;
245     }
246
247     private void assertThatGeneratedFilesMatchExpected(Map<String, String> expectedXmlFiles, CsarTest csarFile)
248             throws CsarConverterException, IOException {
249         List<BabelArtifact> generatedArtifacts =
250                 converter.generateXmlFromCsar(csarFile.getContent(), csarFile.getName(), SERVICE_VERSION);
251         assertThat("Incorrect number of files generated", //
252                 generatedArtifacts.size(), is(equalTo(expectedXmlFiles.size())));
253         for (BabelArtifact generated : generatedArtifacts) {
254             String fileName = generated.getName();
255             String expectedXml = expectedXmlFiles.get(fileName);
256             assertThat("Missing expected content for " + generated.getName(), expectedXml, is(not(nullValue())));
257             assertThat("The content of " + generated.getName() + " must match the expected content",
258                     generated.getPayload(), matches(expectedXml));
259         }
260     }
261 }