Fix remaining Sonar code smells
[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.is;
24 import static org.junit.Assert.assertThat;
25
26 import java.io.IOException;
27 import java.net.URISyntaxException;
28 import java.net.URL;
29 import java.nio.charset.Charset;
30 import java.nio.file.Files;
31 import java.nio.file.Paths;
32 import java.util.Base64;
33 import java.util.List;
34 import java.util.Set;
35 import java.util.function.Function;
36 import java.util.stream.Collectors;
37 import org.apache.commons.io.IOUtils;
38 import org.custommonkey.xmlunit.Diff;
39 import org.onap.aai.babel.xml.generator.data.Artifact;
40 import org.xml.sax.SAXException;
41
42 /**
43  * This class provides some utilities to assist with running tests.
44  */
45 public class ArtifactTestUtils {
46
47     private static final String JSON_REQUESTS_FOLDER = "jsonFiles/";
48     private static final String JSON_RESPONSES_FOLDER = "response/";
49     private static final String CSAR_INPUTS_FOLDER = "compressedArtifacts/";
50
51     public void performYmlAsserts(List<Artifact> toscaFiles, List<String> ymlPayloadsToLoad) {
52         assertThat("An unexpected number of YAML files have been extracted", toscaFiles.size(),
53                 is(ymlPayloadsToLoad.size()));
54
55         Function<? super String, ? extends String> loadResource = s -> {
56             try {
57                 return loadResourceAsString(s);
58             } catch (IOException e) {
59                 throw new RuntimeException(e);
60             }
61         };
62         Set<String> ymlPayloads = ymlPayloadsToLoad.stream().map(loadResource).collect(Collectors.toSet());
63         compareXmlPayloads(toscaFiles, ymlPayloads);
64     }
65
66     /**
67      * Compare 2 XML strings to see if they have the same content
68      *
69      * @param string1
70      * @param string2
71      * @return true if similar
72      */
73     public boolean compareXmlStrings(String string1, String string2) {
74         boolean similar = false;
75
76         try {
77             similar = new Diff(string1, string2).similar();
78         } catch (SAXException | IOException e) { // NOSONAR
79             similar = true;
80         }
81
82         return similar;
83     }
84
85     public byte[] getCompressedArtifact(String resourceName) throws IOException {
86         return loadResourceBytes(CSAR_INPUTS_FOLDER + resourceName);
87     }
88
89     public byte[] loadResourceBytes(String resourceName) throws IOException {
90         return IOUtils.toByteArray(getResource(resourceName));
91     }
92
93     public String loadResourceAsString(String resourceName) throws IOException {
94         return IOUtils.toString(getResource(resourceName), Charset.defaultCharset());
95     }
96
97     public String getRequestJson(String resource) throws IOException {
98         return loadResourceAsString(JSON_REQUESTS_FOLDER + resource);
99     }
100
101     public String getResponseJson(String jsonResponse) throws IOException, URISyntaxException {
102         return readstringFromFile(JSON_RESPONSES_FOLDER + jsonResponse);
103     }
104
105     public String readstringFromFile(String resourceFile) throws IOException, URISyntaxException {
106         return Files.lines(Paths.get(getResource(resourceFile).toURI())).collect(Collectors.joining());
107     }
108
109     public String getResourcePath(String resourceName) {
110         return getResource(resourceName).getPath();
111     }
112
113     private URL getResource(String resourceName) {
114         return ArtifactTestUtils.class.getClassLoader().getResource(resourceName);
115     }
116
117     private void compareXmlPayloads(List<Artifact> toscaFiles, Set<String> ymlPayloads) {
118         for (Artifact artifact : toscaFiles) {
119             boolean payloadFound = false;
120             for (String ymlPayload : ymlPayloads) {
121
122                 if (compareXmlStrings(convertToString(artifact.getPayload()), ymlPayload)) {
123                     payloadFound = true;
124                     break;
125                 }
126             }
127             assertThat("The content of each YAML file must match the actual content of the file extracted ("
128                     + artifact.getName() + ")", payloadFound, is(true));
129         }
130     }
131
132     private String convertToString(byte[] byteArray) {
133         return new String(Base64.getDecoder().decode(byteArray), Charset.defaultCharset());
134     }
135
136 }