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