2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * Copyright (c) 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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.aai.babel.util;
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;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.net.URISyntaxException;
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;
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.xml.sax.SAXException;
50 * This class provides some utilities to assist with running local unit tests.
52 public class ArtifactTestUtils {
54 public static final String CSAR_INPUTS_FOLDER = "compressedArtifacts/";
55 private static final String JSON_REQUESTS_FOLDER = "jsonFiles/";
56 private static final String JSON_RESPONSES_FOLDER = "response/";
59 * Initialize System Properties for test configuration files.
61 public void setGeneratorSystemProperties() {
62 System.setProperty(ArtifactGeneratorToscaParser.PROPERTY_TOSCA_MAPPING_FILE,
63 getResourcePath(Resources.TOSCA_MAPPING_CONFIG));
67 * Load the Widget type mappings (resource).
70 * if the configuration file is not loaded
72 public void loadWidgetMappings() throws IOException {
73 ArtifactGeneratorToscaParser.initToscaMappingsConfiguration(getResourcePath(Resources.TOSCA_MAPPING_CONFIG));
77 * Specific test method for the YAML Extractor test.
80 * files extracted by the YamlExtractor
81 * @param ymlPayloadsToLoad
82 * the expected YAML files
84 * if an I/O exception occurs
86 public void performYmlAsserts(List<Artifact> toscaFiles, List<String> ymlPayloadsToLoad) throws IOException {
87 assertThat("An incorrect number of YAML files have been extracted", toscaFiles.size(),
88 is(equalTo(ymlPayloadsToLoad.size())));
90 Map<String, String> ymlMap = new HashMap<>();
91 for (String filename : ymlPayloadsToLoad) {
92 ymlMap.put(filename, loadResourceAsString(filename));
95 for (Artifact artifact : toscaFiles) {
96 String fileName = artifact.getName().replaceFirst("Definitions/", "ymlFiles/");
97 String expectedYaml = ymlMap.get(fileName);
98 assertThat("Missing expected content for " + fileName, expectedYaml, is(not(nullValue())));
99 assertThat("The content of " + fileName + " must match the expected content",
100 convertToString(artifact.getPayload()).replaceAll("\\r\\n?", "\n"), is(equalTo(expectedYaml)));
105 * Compare two XML strings to see if they have the same content.
111 * @return true if XML content is similar
112 * @throws IOException
113 * if an I/O exception occurs
114 * @throws SAXException
115 * if the XML parsing fails
117 public boolean compareXmlStrings(String string1, String string2) throws SAXException, IOException {
118 return new Diff(string1, string2).similar();
121 public byte[] getCompressedArtifact(String resourceName) throws IOException {
122 return loadResourceBytes(CSAR_INPUTS_FOLDER + resourceName);
125 public byte[] loadResourceBytes(String resourceName) throws IOException {
126 return IOUtils.toByteArray(getResource(resourceName));
129 public String loadResourceAsString(String resourceName) throws IOException {
130 return IOUtils.toString(getResource(resourceName), Charset.defaultCharset());
133 public String getRequestJson(String resource) throws IOException {
134 return loadResourceAsString(JSON_REQUESTS_FOLDER + resource);
137 public String getResponseJson(String jsonResponse) throws IOException, URISyntaxException {
138 return readstringFromFile(JSON_RESPONSES_FOLDER + jsonResponse);
141 public String readstringFromFile(String resourceFile) throws IOException, URISyntaxException {
142 return Files.lines(Paths.get(getResource(resourceFile).toURI())).collect(Collectors.joining());
146 * Create Properties from the content of the named resource (e.g. a file on the classpath).
148 * @param resourceName
150 * @return Properties loaded from the named resource
151 * @throws IOException
152 * if an error occurred when reading from the named resource
154 public Properties getResourceAsProperties(String resourceName) throws IOException {
155 final Properties properties = new Properties();
156 InputStream in = ArtifactTestUtils.class.getClassLoader().getResourceAsStream(resourceName);
162 public String getResourcePath(String resourceName) {
163 return getResource(resourceName).getPath();
166 private URL getResource(String resourceName) {
167 return ArtifactTestUtils.class.getClassLoader().getResource(resourceName);
170 private String convertToString(byte[] byteArray) {
171 return new String(Base64.getDecoder().decode(byteArray), Charset.defaultCharset());