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