Incorporate the ECOMP SDC Artefact Generator code
[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.File;
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.service.data.BabelArtifact;
42 import org.onap.aai.babel.util.ArtifactTestUtils;
43 import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
44 import org.onap.aai.babel.xml.generator.data.GeneratorConstants;
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 CSAR_FOLDER = "compressedArtifacts";
53     private static final String VALID_CSAR_FILE = "service-SdWanServiceTest-csar.csar";
54     private static final String INCORRECT_CSAR_NAME = "the_name_of_the_csar_file.csar";
55     private static final String SERVICE_VERSION = "1.0";
56
57     private CsarToXmlConverter converter;
58
59     static {
60         if (System.getProperty("AJSC_HOME") == null) {
61             System.setProperty("AJSC_HOME", ".");
62         }
63     }
64
65     @Rule
66     public ExpectedException exception = ExpectedException.none();
67     private ArtifactTestUtils artifactTestUtils;
68
69     @Before
70     public void setup() {
71         System.setProperty(GeneratorConstants.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE,
72                 CsarToXmlConverterTest.class.getClassLoader().getResource(ARTIFACT_GENERATOR_CONFIG).getPath());
73         converter = new CsarToXmlConverter();
74         artifactTestUtils = new ArtifactTestUtils();
75     }
76
77     @After
78     public void tearDown() {
79         converter = null;
80     }
81
82     @Test(expected = NullPointerException.class)
83     public void generateXmlFromCsar_nullArtifactSupplied() throws CsarConverterException {
84         converter.generateXmlFromCsar(null, null, null);
85     }
86
87     @Test(expected = NullPointerException.class)
88     public void generateXmlFromCsar_missingName() throws CsarConverterException, IOException {
89         converter.generateXmlFromCsar(getCsar(VALID_CSAR_FILE), null, null);
90     }
91
92     @Test(expected = NullPointerException.class)
93     public void generateXmlFromCsar_missingVersion() throws CsarConverterException, IOException {
94         converter.generateXmlFromCsar(getCsar(VALID_CSAR_FILE), INCORRECT_CSAR_NAME, null);
95     }
96
97     @Test(expected = CsarConverterException.class)
98     public void generateXmlFromCsar_noPayloadExists() throws CsarConverterException {
99         converter.generateXmlFromCsar(new byte[0], INCORRECT_CSAR_NAME, SERVICE_VERSION);
100     }
101
102     @Test(expected = CsarConverterException.class)
103     public void generateXmlFromCsar_csarFileHasNoYmlFiles() throws CsarConverterException, IOException {
104         converter.generateXmlFromCsar(getCsar("noYmlFilesArchive.zip"), "noYmlFilesArchive.zip", SERVICE_VERSION);
105     }
106
107     @Test
108     public void generateXmlFromCsar_artifactgenerator_config_systemPropertyNotSet()
109             throws IOException, XmlArtifactGenerationException, CsarConverterException {
110         exception.expect(CsarConverterException.class);
111         exception.expectMessage("Cannot generate artifacts. artifactgenerator.config system property not configured");
112
113         // Unset the required system property
114         System.clearProperty(GeneratorConstants.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE);
115         converter.generateXmlFromCsar(getCsar(VALID_CSAR_FILE), VALID_CSAR_FILE, SERVICE_VERSION);
116     }
117
118     @Test
119     public void generateXmlFromCsar() throws CsarConverterException, IOException, XmlArtifactGenerationException {
120         Map<String, String> expectedXmlFiles = createExpectedXmlFiles();
121         List<BabelArtifact> generatedArtifacts =
122                 converter.generateXmlFromCsar(getCsar(VALID_CSAR_FILE), VALID_CSAR_FILE, SERVICE_VERSION);
123
124         generatedArtifacts
125                 .forEach(ga -> assertThat("The content of " + ga.getName() + " must match the expected content",
126                         ga.getPayload(), matches(expectedXmlFiles.get(ga.getName()))));
127     }
128
129     public Matcher<String> matches(final String expected) {
130         return new BaseMatcher<String>() {
131             protected String theExpected = expected;
132
133             @Override
134             public boolean matches(Object o) {
135                 return artifactTestUtils.compareXMLStrings((String) o, theExpected);
136             }
137
138             @Override
139             public void describeTo(Description description) {
140                 description.appendText(theExpected.toString());
141             }
142         };
143     }
144
145     private byte[] getCsar(String csarFileName) throws IOException {
146         return artifactTestUtils.loadResource(CSAR_FOLDER + File.separator + csarFileName);
147     }
148
149     private Map<String, String> createExpectedXmlFiles() throws IOException {
150         Map<String, String> xmlMap = new HashMap<>();
151
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
158         for (String filename : filesToLoad) {
159             xmlMap.put(filename, artifactTestUtils.loadResourceAsString("generatedXml" + File.separator + filename));
160         }
161
162         return xmlMap;
163     }
164 }