6608c0096ffa79b20eb26a5f6d33a1dfd1beb76d
[aai/babel.git] / src / test / java / org / onap / aai / babel / util / ArtifactTestUtils.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2019 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.util;
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.io.InputStream;
32 import java.net.URISyntaxException;
33 import java.net.URL;
34 import java.nio.charset.Charset;
35 import java.nio.file.Files;
36 import java.nio.file.Paths;
37 import java.util.Base64;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Properties;
42 import java.util.stream.Collectors;
43 import org.apache.commons.io.IOUtils;
44 import org.custommonkey.xmlunit.Diff;
45 import org.onap.aai.babel.parser.ArtifactGeneratorToscaParser;
46 import org.onap.aai.babel.xml.generator.data.Artifact;
47 import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil;
48 import org.xml.sax.SAXException;
49
50 /**
51  * This class provides some utilities to assist with running local unit tests.
52  */
53 public class ArtifactTestUtils {
54
55     private static final String JSON_REQUESTS_FOLDER = "jsonFiles/";
56     private static final String JSON_RESPONSES_FOLDER = "response/";
57     private static final String CSAR_INPUTS_FOLDER = "compressedArtifacts/";
58
59     public void setGeneratorSystemProperties() {
60         System.setProperty(ArtifactGeneratorToscaParser.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE,
61                 getResourcePath(Resources.ARTIFACT_GENERATOR_CONFIG));
62
63         System.setProperty(ArtifactGeneratorToscaParser.PROPERTY_GROUP_FILTERS_CONFIG_FILE,
64                 getResourcePath(Resources.FILTER_TYPES_CONFIG));
65     }
66
67     /**
68      * Load the Widget to UUID mappings from the Artifact Generator Properties (resource).
69      * 
70      * @throws IOException
71      *             if the properties file is not loaded
72      */
73     public void loadWidgetToUuidMappings() throws IOException {
74         WidgetConfigurationUtil.setConfig(getResourceAsProperties(Resources.ARTIFACT_GENERATOR_CONFIG));
75     }
76
77     /**
78      * Specific test method for the YAML Extractor test.
79      *
80      * @param toscaFiles
81      *            files extracted by the YamlExtractor
82      * @param ymlPayloadsToLoad
83      *            the expected YAML files
84      * @throws IOException
85      *             if an I/O exception occurs
86      */
87     public void performYmlAsserts(List<Artifact> toscaFiles, List<String> ymlPayloadsToLoad) throws IOException {
88         assertThat("An incorrect number of YAML files have been extracted", toscaFiles.size(),
89                 is(equalTo(ymlPayloadsToLoad.size())));
90
91         Map<String, String> ymlMap = new HashMap<>();
92         for (String filename : ymlPayloadsToLoad) {
93             ymlMap.put(filename, loadResourceAsString(filename));
94         }
95
96         for (Artifact artifact : toscaFiles) {
97             String fileName = artifact.getName().replaceFirst("Definitions/", "ymlFiles/");
98             String expectedYaml = ymlMap.get(fileName);
99             assertThat("Missing expected content for " + fileName, expectedYaml, is(not(nullValue())));
100             assertThat("The content of " + fileName + " must match the expected content",
101                     convertToString(artifact.getPayload()).replaceAll("\\r\\n?", "\n"), is(equalTo(expectedYaml)));
102         }
103     }
104
105     /**
106      * Compare two XML strings to see if they have the same content.
107      *
108      * @param string1
109      *            XML content
110      * @param string2
111      *            XML content
112      * @return true if XML content is similar
113      * @throws IOException
114      *             if an I/O exception occurs
115      * @throws SAXException
116      *             if the XML parsing fails
117      */
118     public boolean compareXmlStrings(String string1, String string2) throws SAXException, IOException {
119         return new Diff(string1, string2).similar();
120     }
121
122     public byte[] getCompressedArtifact(String resourceName) throws IOException {
123         return loadResourceBytes(CSAR_INPUTS_FOLDER + resourceName);
124     }
125
126     public byte[] loadResourceBytes(String resourceName) throws IOException {
127         return IOUtils.toByteArray(getResource(resourceName));
128     }
129
130     public String loadResourceAsString(String resourceName) throws IOException {
131         return IOUtils.toString(getResource(resourceName), Charset.defaultCharset());
132     }
133
134     public String getRequestJson(String resource) throws IOException {
135         return loadResourceAsString(JSON_REQUESTS_FOLDER + resource);
136     }
137
138     public String getResponseJson(String jsonResponse) throws IOException, URISyntaxException {
139         return readstringFromFile(JSON_RESPONSES_FOLDER + jsonResponse);
140     }
141
142     public String readstringFromFile(String resourceFile) throws IOException, URISyntaxException {
143         return Files.lines(Paths.get(getResource(resourceFile).toURI())).collect(Collectors.joining());
144     }
145
146     public Properties getResourceAsProperties(String resourceName) throws IOException {
147         final Properties properties = new Properties();
148         InputStream in = ArtifactTestUtils.class.getClassLoader().getResourceAsStream(resourceName);
149         properties.load(in);
150         in.close();
151         return properties;
152     }
153
154     public String getResourcePath(String resourceName) {
155         return getResource(resourceName).getPath();
156     }
157
158     private URL getResource(String resourceName) {
159         return ArtifactTestUtils.class.getClassLoader().getResource(resourceName);
160     }
161
162     private String convertToString(byte[] byteArray) {
163         return new String(Base64.getDecoder().decode(byteArray), Charset.defaultCharset());
164     }
165
166 }