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