9cfdf9b50cb4563c9b5582773e0535819166953f
[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.junit.Assert.assertThat;
25
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import org.hamcrest.BaseMatcher;
32 import org.hamcrest.Description;
33 import org.hamcrest.Matcher;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.rules.ExpectedException;
39 import org.onap.aai.babel.csar.CsarConverterException;
40 import org.onap.aai.babel.csar.CsarToXmlConverter;
41 import org.onap.aai.babel.parser.ArtifactGeneratorToscaParser;
42 import org.onap.aai.babel.service.data.BabelArtifact;
43 import org.onap.aai.babel.util.ArtifactTestUtils;
44 import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
45
46 /**
47  * Tests {@link CsarToXmlConverter}.
48  */
49 public class CsarToXmlConverterTest {
50
51     private static final String ARTIFACT_GENERATOR_CONFIG = "artifact-generator.properties";
52     private static final String INCORRECT_CSAR_NAME = "the_name_of_the_csar_file.csar";
53     private static final String SERVICE_VERSION = "1.0";
54
55     static {
56         if (System.getProperty("APP_HOME") == null) {
57             System.setProperty("APP_HOME", ".");
58         }
59     }
60
61     private enum CsarTest {
62         VALID_CSAR_FILE(
63                 "service-SdWanServiceTest-csar.csar"
64         ),
65         MISSING_METADATA_CSAR(
66                 "service-MissingMetadataTest.csar"
67         ),
68         NO_YAML_FILES(
69                 "noYmlFilesArchive.zip"
70         ),
71         PORT_MIRROR_CSAR(
72                 "service_PortMirror.csar"
73         );
74
75         private String filename;
76         private ArtifactTestUtils artifactTestUtils = new ArtifactTestUtils();
77
78         CsarTest(String filename) {
79             this.filename = filename;
80         }
81
82         public String getName() {
83             return filename;
84         }
85
86         public byte[] getContent() throws IOException {
87             return artifactTestUtils.getCompressedArtifact(filename);
88         }
89     }
90
91     // The class to be tested.
92     private CsarToXmlConverter converter;
93
94     @Rule
95     public ExpectedException exception = ExpectedException.none();
96
97     @Before
98     public void setup() {
99         System.setProperty(ArtifactGeneratorToscaParser.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE,
100                 new ArtifactTestUtils().getResourcePath(ARTIFACT_GENERATOR_CONFIG));
101         converter = new CsarToXmlConverter();
102     }
103
104     @After
105     public void tearDown() {
106         converter = null;
107     }
108
109     @Test(expected = NullPointerException.class)
110     public void testNullArtifactSupplied() throws CsarConverterException {
111         converter.generateXmlFromCsar(null, null, null);
112     }
113
114     @Test(expected = NullPointerException.class)
115     public void testMissingName() throws CsarConverterException, IOException {
116         converter.generateXmlFromCsar(CsarTest.VALID_CSAR_FILE.getContent(), null, null);
117     }
118
119     @Test(expected = NullPointerException.class)
120     public void testMissingVersion() throws CsarConverterException, IOException {
121         converter.generateXmlFromCsar(CsarTest.VALID_CSAR_FILE.getContent(), INCORRECT_CSAR_NAME, null);
122     }
123
124     @Test(expected = CsarConverterException.class)
125     public void testNoPayloadExists() throws CsarConverterException {
126         converter.generateXmlFromCsar(new byte[0], INCORRECT_CSAR_NAME, SERVICE_VERSION);
127     }
128
129     @Test(expected = CsarConverterException.class)
130     public void testCsarFileHasNoYmlFiles() throws CsarConverterException, IOException {
131         converter.generateXmlFromCsar(CsarTest.NO_YAML_FILES.getContent(), "noYmlFilesArchive.zip", SERVICE_VERSION);
132     }
133
134     @Test
135     public void testArtifactGeneratorConfigMissing()
136             throws IOException, XmlArtifactGenerationException, CsarConverterException {
137         exception.expect(CsarConverterException.class);
138         exception.expectMessage("Cannot generate artifacts. artifactgenerator.config system property not configured");
139
140         // Unset the required system property
141         System.clearProperty(ArtifactGeneratorToscaParser.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE);
142         converter.generateXmlFromCsar(CsarTest.VALID_CSAR_FILE.getContent(), CsarTest.VALID_CSAR_FILE.getName(),
143                 SERVICE_VERSION);
144     }
145
146     @Test
147     public void testServiceMetadataMissing()
148             throws IOException, XmlArtifactGenerationException, CsarConverterException {
149         converter.generateXmlFromCsar(CsarTest.MISSING_METADATA_CSAR.getContent(),
150                 CsarTest.MISSING_METADATA_CSAR.getName(), SERVICE_VERSION);
151     }
152
153     @Test
154     public void generateXmlFromCsar() throws CsarConverterException, IOException, XmlArtifactGenerationException {
155         Map<String, String> expectedXmlFiles = createExpectedXmlFiles();
156         List<BabelArtifact> generatedArtifacts = converter.generateXmlFromCsar(CsarTest.VALID_CSAR_FILE.getContent(),
157                 CsarTest.VALID_CSAR_FILE.getName(), SERVICE_VERSION);
158
159         generatedArtifacts
160                 .forEach(ga -> assertThat("The content of " + ga.getName() + " must match the expected content",
161                         ga.getPayload(), matches(expectedXmlFiles.get(ga.getName()))));
162     }
163
164     @Test
165     public void generatePortMirrorConfigurationModel()
166             throws CsarConverterException, IOException, XmlArtifactGenerationException {
167         Map<String, String> expectedXmlFiles = createExpectedXmlFiles();
168         List<BabelArtifact> generatedArtifacts = converter.generateXmlFromCsar(CsarTest.PORT_MIRROR_CSAR.getContent(),
169                 CsarTest.PORT_MIRROR_CSAR.getName(), SERVICE_VERSION);
170
171         generatedArtifacts
172                 .forEach(ga -> assertThat("The content of " + ga.getName() + " must match the expected content",
173                         ga.getPayload(), matches(expectedXmlFiles.get(ga.getName()))));
174     }
175
176     public Matcher<String> matches(final String expected) {
177         return new BaseMatcher<String>() {
178             protected String theExpected = expected;
179
180             @Override
181             public boolean matches(Object item) {
182                 return new ArtifactTestUtils().compareXmlStrings((String) item, theExpected);
183             }
184
185             @Override
186             public void describeTo(Description description) {
187                 description.appendText(theExpected.toString());
188             }
189         };
190     }
191
192     private Map<String, String> createExpectedXmlFiles() throws IOException {
193         Map<String, String> xmlMap = new HashMap<>();
194
195         List<String> filesToLoad = new ArrayList<>();
196         filesToLoad.add("AAI-SD-WAN-Service-Test-service-1.0.xml");
197         filesToLoad.add("AAI-SdWanTestVsp..DUMMY..module-0-resource-2.xml");
198         filesToLoad.add("AAI-Tunnel_XConnTest-resource-2.0.xml");
199         filesToLoad.add("AAI-SD-WAN-Test-VSP-resource-1.0.xml");
200         filesToLoad.add("AAI-Port Mirror_Test-service-1.0.xml");
201         filesToLoad.add("AAI-Port Mirroring Configuration-resource-35.0.xml");
202
203         for (String filename : filesToLoad) {
204             xmlMap.put(filename, new ArtifactTestUtils().loadResourceAsString("generatedXml/" + filename));
205         }
206
207         return xmlMap;
208     }
209 }