bf970a667c5f30bae5b438476affc060373d03bd
[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.junit.Assert.assertThat;
27
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import org.hamcrest.BaseMatcher;
34 import org.hamcrest.Description;
35 import org.hamcrest.Matcher;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.ExpectedException;
41 import org.onap.aai.babel.csar.CsarConverterException;
42 import org.onap.aai.babel.csar.CsarToXmlConverter;
43 import org.onap.aai.babel.parser.ArtifactGeneratorToscaParser;
44 import org.onap.aai.babel.service.data.BabelArtifact;
45 import org.onap.aai.babel.testdata.CsarTest;
46 import org.onap.aai.babel.util.ArtifactTestUtils;
47 import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
48
49 /**
50  * Tests {@link CsarToXmlConverter}.
51  */
52 public class CsarToXmlConverterTest {
53
54     private static final String ARTIFACT_GENERATOR_CONFIG = "artifact-generator.properties";
55     private static final String FILTER_TYPES_CONFIG = "filter-types.properties";
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         System.setProperty(ArtifactGeneratorToscaParser.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE,
75                 new ArtifactTestUtils().getResourcePath(ARTIFACT_GENERATOR_CONFIG));
76
77         System.setProperty(ArtifactGeneratorToscaParser.PROPERTY_GROUP_FILTERS_CONFIG_FILE,
78                 new ArtifactTestUtils().getResourcePath(FILTER_TYPES_CONFIG));
79
80         converter = new CsarToXmlConverter();
81     }
82
83     @After
84     public void tearDown() {
85         converter = null;
86     }
87
88     @Test(expected = NullPointerException.class)
89     public void testNullArtifactSupplied() throws CsarConverterException {
90         converter.generateXmlFromCsar(null, null, null);
91     }
92
93     @Test(expected = NullPointerException.class)
94     public void testMissingName() throws CsarConverterException, IOException {
95         converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), null, null);
96     }
97
98     @Test(expected = NullPointerException.class)
99     public void testMissingVersion() throws CsarConverterException, IOException {
100         converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), INCORRECT_CSAR_NAME, null);
101     }
102
103     @Test(expected = CsarConverterException.class)
104     public void testNoPayloadExists() throws CsarConverterException {
105         converter.generateXmlFromCsar(new byte[0], INCORRECT_CSAR_NAME, SERVICE_VERSION);
106     }
107
108     @Test(expected = CsarConverterException.class)
109     public void testCsarFileHasNoYmlFiles() throws CsarConverterException, IOException {
110         converter.generateXmlFromCsar(CsarTest.NO_YAML_FILES.getContent(), CsarTest.NO_YAML_FILES.getName(),
111                 SERVICE_VERSION);
112     }
113
114     /**
115      * Test that an Exception is thrown when the Artifact Generator properties are not present.
116      *
117      * @throws CsarConverterException if there is an error either extracting the YAML files or generating XML artifacts
118      * @throws IOException if an I/O exception occurs loading the test CSAR file
119      * @throws IOException
120      * @throws XmlArtifactGenerationException
121      * @throws CsarConverterException
122      */
123     @Test
124     public void testArtifactGeneratorConfigMissing() throws CsarConverterException, IOException {
125         exception.expect(CsarConverterException.class);
126         exception.expectMessage("Cannot generate artifacts. System property artifactgenerator.config not configured");
127
128         // Unset the required system property
129         System.clearProperty(ArtifactGeneratorToscaParser.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE);
130         converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), CsarTest.SD_WAN_CSAR_FILE.getName(),
131                 SERVICE_VERSION);
132     }
133
134     /**
135      * Test that an Exception is thrown when the Artifact Generator's Group Filter properties are not present.
136      *
137      * @throws IOException
138      * @throws XmlArtifactGenerationException
139      * @throws CsarConverterException
140      */
141     @Test
142     public void generateXmlFromCsarFilterTypesSystemPropertyNotSet()
143             throws IOException, XmlArtifactGenerationException, CsarConverterException {
144         exception.expect(CsarConverterException.class);
145         exception.expectMessage("Cannot generate artifacts. System property groupfilter.config not configured");
146
147         // Unset the required system property
148         System.clearProperty(ArtifactGeneratorToscaParser.PROPERTY_GROUP_FILTERS_CONFIG_FILE);
149         converter.generateXmlFromCsar(CsarTest.SD_WAN_CSAR_FILE.getContent(), CsarTest.SD_WAN_CSAR_FILE.getName(),
150                 SERVICE_VERSION);
151     }
152
153     @Test
154     public void testServiceMetadataMissing()
155             throws IOException, XmlArtifactGenerationException, CsarConverterException {
156         converter.generateXmlFromCsar(CsarTest.MISSING_METADATA_CSAR.getContent(),
157                 CsarTest.MISSING_METADATA_CSAR.getName(), SERVICE_VERSION);
158     }
159
160     @Test
161     public void generateXmlFromSdWanCsar() throws IOException, CsarConverterException {
162         List<String> filesToLoad = new ArrayList<>();
163         filesToLoad.add("AAI-SD-WAN-Service-Test-service-1.0.xml");
164         filesToLoad.add("AAI-SdWanTestVsp..DUMMY..module-0-resource-2.xml");
165         filesToLoad.add("AAI-Tunnel_XConnTest-resource-2.0.xml");
166         filesToLoad.add("AAI-SD-WAN-Test-VSP-resource-1.0.xml");
167         assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad), CsarTest.SD_WAN_CSAR_FILE);
168     }
169
170     @Test
171     public void generatePortMirrorConfigurationModel()
172             throws CsarConverterException, IOException, XmlArtifactGenerationException {
173         List<String> filesToLoad = new ArrayList<>();
174         filesToLoad.add("AAI-Port Mirror_Test-service-1.0.xml");
175         filesToLoad.add("AAI-Port Mirroring Configuration-resource-35.0.xml");
176         assertThatGeneratedFilesMatchExpected(createExpectedXmlFiles(filesToLoad), CsarTest.PORT_MIRROR_CSAR);
177     }
178
179     public Matcher<String> matches(final String expected) {
180         return new BaseMatcher<String>() {
181             protected String theExpected = expected;
182
183             @Override
184             public boolean matches(Object item) {
185                 return new ArtifactTestUtils().compareXmlStrings((String) item, theExpected);
186             }
187
188             @Override
189             public void describeTo(Description description) {
190                 description.appendText(theExpected.toString());
191             }
192         };
193     }
194
195     private Map<String, String> createExpectedXmlFiles(List<String> filesToLoad) throws IOException {
196         Map<String, String> xmlMap = new HashMap<>();
197         for (String filename : filesToLoad) {
198             xmlMap.put(filename, new ArtifactTestUtils().loadResourceAsString("generatedXml/" + filename));
199         }
200         return xmlMap;
201     }
202
203     private void assertThatGeneratedFilesMatchExpected(Map<String, String> expectedXmlFiles, CsarTest csarFile)
204             throws CsarConverterException, IOException {
205         List<BabelArtifact> generatedArtifacts = converter.generateXmlFromCsar(csarFile.getContent(),
206                 csarFile.getName(), SERVICE_VERSION);
207         assertThat("Incorrect number of files generated", //
208                 generatedArtifacts.size(), is(equalTo(expectedXmlFiles.size())));
209         generatedArtifacts
210                 .forEach(ga -> assertThat("The content of " + ga.getName() + " must match the expected content",
211                         ga.getPayload(), matches(expectedXmlFiles.get(ga.getName()))));
212     }
213 }