2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.onap.aai.babel.util;
23 import static org.hamcrest.CoreMatchers.equalTo;
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.hamcrest.CoreMatchers.not;
26 import static org.hamcrest.CoreMatchers.nullValue;
27 import static org.junit.Assert.assertThat;
29 import java.io.IOException;
30 import java.net.URISyntaxException;
32 import java.nio.charset.Charset;
33 import java.nio.file.Files;
34 import java.nio.file.Paths;
35 import java.util.Base64;
36 import java.util.HashMap;
37 import java.util.List;
39 import java.util.stream.Collectors;
40 import org.apache.commons.io.IOUtils;
41 import org.custommonkey.xmlunit.Diff;
42 import org.onap.aai.babel.xml.generator.data.Artifact;
43 import org.xml.sax.SAXException;
46 * This class provides some utilities to assist with running tests.
48 public class ArtifactTestUtils {
50 private static final String JSON_REQUESTS_FOLDER = "jsonFiles/";
51 private static final String JSON_RESPONSES_FOLDER = "response/";
52 private static final String CSAR_INPUTS_FOLDER = "compressedArtifacts/";
55 * Specific test method for the YAML Extractor test.
58 * files extracted by the YamlExtractor
59 * @param ymlPayloadsToLoad
60 * the expected YAML files
62 * if an I/O exception occurs
64 public void performYmlAsserts(List<Artifact> toscaFiles, List<String> ymlPayloadsToLoad) throws IOException {
65 assertThat("An incorrect number of YAML files have been extracted", toscaFiles.size(),
66 is(equalTo(ymlPayloadsToLoad.size())));
68 Map<String, String> ymlMap = new HashMap<>();
69 for (String filename : ymlPayloadsToLoad) {
70 ymlMap.put(filename, loadResourceAsString(filename));
73 for (Artifact artifact : toscaFiles) {
74 String fileName = artifact.getName().replaceFirst("Definitions/", "ymlFiles/");
75 String expectedYaml = ymlMap.get(fileName);
76 assertThat("Missing expected content for " + fileName, expectedYaml, is(not(nullValue())));
77 assertThat("The content of " + fileName + " must match the expected content",
78 convertToString(artifact.getPayload()).replaceAll("\\r\\n?", "\n"), is(equalTo(expectedYaml)));
83 * Compare two XML strings to see if they have the same content.
89 * @return true if XML content is similar
91 * if an I/O exception occurs
92 * @throws SAXException
93 * if the XML parsing fails
95 public boolean compareXmlStrings(String string1, String string2) throws SAXException, IOException {
96 return new Diff(string1, string2).similar();
99 public byte[] getCompressedArtifact(String resourceName) throws IOException {
100 return loadResourceBytes(CSAR_INPUTS_FOLDER + resourceName);
103 public byte[] loadResourceBytes(String resourceName) throws IOException {
104 return IOUtils.toByteArray(getResource(resourceName));
107 public String loadResourceAsString(String resourceName) throws IOException {
108 return IOUtils.toString(getResource(resourceName), Charset.defaultCharset());
111 public String getRequestJson(String resource) throws IOException {
112 return loadResourceAsString(JSON_REQUESTS_FOLDER + resource);
115 public String getResponseJson(String jsonResponse) throws IOException, URISyntaxException {
116 return readstringFromFile(JSON_RESPONSES_FOLDER + jsonResponse);
119 public String readstringFromFile(String resourceFile) throws IOException, URISyntaxException {
120 return Files.lines(Paths.get(getResource(resourceFile).toURI())).collect(Collectors.joining());
123 public String getResourcePath(String resourceName) {
124 return getResource(resourceName).getPath();
127 private URL getResource(String resourceName) {
128 return ArtifactTestUtils.class.getClassLoader().getResource(resourceName);
131 private String convertToString(byte[] byteArray) {
132 return new String(Base64.getDecoder().decode(byteArray), Charset.defaultCharset());