Throw an Exception for XML test file mismatches
[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-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.util;
22
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;
28
29 import java.io.IOException;
30 import java.net.URISyntaxException;
31 import java.net.URL;
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;
38 import java.util.Map;
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;
44
45 /**
46  * This class provides some utilities to assist with running tests.
47  */
48 public class ArtifactTestUtils {
49
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/";
53
54     /**
55      * Specific test method for the YAML Extractor test.
56      *
57      * @param toscaFiles
58      *        files extracted by the YamlExtractor
59      * @param ymlPayloadsToLoad
60      *        the expected YAML files
61      * @throws IOException
62      *         if an I/O exception occurs
63      */
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())));
67
68         Map<String, String> ymlMap = new HashMap<>();
69         for (String filename : ymlPayloadsToLoad) {
70             ymlMap.put(filename, loadResourceAsString(filename));
71         }
72
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)));
79         }
80     }
81
82     /**
83      * Compare two XML strings to see if they have the same content.
84      *
85      * @param string1
86      *        XML content
87      * @param string2
88      *        XML content
89      * @return true if XML content is similar
90      * @throws IOException
91      *         if an I/O exception occurs
92      * @throws SAXException
93      *         if the XML parsing fails
94      */
95     public boolean compareXmlStrings(String string1, String string2) throws SAXException, IOException {
96         return new Diff(string1, string2).similar();
97     }
98
99     public byte[] getCompressedArtifact(String resourceName) throws IOException {
100         return loadResourceBytes(CSAR_INPUTS_FOLDER + resourceName);
101     }
102
103     public byte[] loadResourceBytes(String resourceName) throws IOException {
104         return IOUtils.toByteArray(getResource(resourceName));
105     }
106
107     public String loadResourceAsString(String resourceName) throws IOException {
108         return IOUtils.toString(getResource(resourceName), Charset.defaultCharset());
109     }
110
111     public String getRequestJson(String resource) throws IOException {
112         return loadResourceAsString(JSON_REQUESTS_FOLDER + resource);
113     }
114
115     public String getResponseJson(String jsonResponse) throws IOException, URISyntaxException {
116         return readstringFromFile(JSON_RESPONSES_FOLDER + jsonResponse);
117     }
118
119     public String readstringFromFile(String resourceFile) throws IOException, URISyntaxException {
120         return Files.lines(Paths.get(getResource(resourceFile).toURI())).collect(Collectors.joining());
121     }
122
123     public String getResourcePath(String resourceName) {
124         return getResource(resourceName).getPath();
125     }
126
127     private URL getResource(String resourceName) {
128         return ArtifactTestUtils.class.getClassLoader().getResource(resourceName);
129     }
130
131     private String convertToString(byte[] byteArray) {
132         return new String(Base64.getDecoder().decode(byteArray), Charset.defaultCharset());
133     }
134
135 }