Compare YML files as Strings, not XML
[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     public void performYmlAsserts(List<Artifact> toscaFiles, List<String> ymlPayloadsToLoad) throws IOException {
55         assertThat("An incorrect number of YAML files have been extracted", toscaFiles.size(),
56                 is(equalTo(ymlPayloadsToLoad.size())));
57
58         Map<String, String> ymlMap = new HashMap<>();
59         for (String filename : ymlPayloadsToLoad) {
60             ymlMap.put(filename, loadResourceAsString(filename));
61         }
62
63         for (Artifact artifact : toscaFiles) {
64             String fileName = artifact.getName().replaceFirst("Definitions/", "ymlFiles/");
65             String expectedYaml = ymlMap.get(fileName);
66             assertThat("Missing expected content for " + fileName, expectedYaml, is(not(nullValue())));
67             assertThat("The content of " + fileName + " must match the expected content",
68                     convertToString(artifact.getPayload()).replaceAll("\\r\\n?", "\n"), is(equalTo(expectedYaml)));
69         }
70     }
71
72     /**
73      * Compare 2 XML strings to see if they have the same content
74      *
75      * @param string1
76      * @param string2
77      * @return true if similar
78      */
79     public boolean compareXmlStrings(String string1, String string2) {
80         boolean similar = false;
81
82         try {
83             similar = new Diff(string1, string2).similar();
84         } catch (SAXException | IOException e) { // NOSONAR
85             similar = true;
86         }
87
88         return similar;
89     }
90
91     public byte[] getCompressedArtifact(String resourceName) throws IOException {
92         return loadResourceBytes(CSAR_INPUTS_FOLDER + resourceName);
93     }
94
95     public byte[] loadResourceBytes(String resourceName) throws IOException {
96         return IOUtils.toByteArray(getResource(resourceName));
97     }
98
99     public String loadResourceAsString(String resourceName) throws IOException {
100         return IOUtils.toString(getResource(resourceName), Charset.defaultCharset());
101     }
102
103     public String getRequestJson(String resource) throws IOException {
104         return loadResourceAsString(JSON_REQUESTS_FOLDER + resource);
105     }
106
107     public String getResponseJson(String jsonResponse) throws IOException, URISyntaxException {
108         return readstringFromFile(JSON_RESPONSES_FOLDER + jsonResponse);
109     }
110
111     public String readstringFromFile(String resourceFile) throws IOException, URISyntaxException {
112         return Files.lines(Paths.get(getResource(resourceFile).toURI())).collect(Collectors.joining());
113     }
114
115     public String getResourcePath(String resourceName) {
116         return getResource(resourceName).getPath();
117     }
118
119     private URL getResource(String resourceName) {
120         return ArtifactTestUtils.class.getClassLoader().getResource(resourceName);
121     }
122
123     private String convertToString(byte[] byteArray) {
124         return new String(Base64.getDecoder().decode(byteArray), Charset.defaultCharset());
125     }
126
127 }