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.is;
24 import static org.junit.Assert.assertThat;
26 import java.io.IOException;
27 import java.net.URISyntaxException;
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;
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;
43 * This class provides some utilities to assist with running tests.
45 public class ArtifactTestUtils {
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/";
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()));
55 Function<? super String, ? extends String> loadResource = s -> {
57 return loadResourceAsString(s);
58 } catch (IOException e) {
59 throw new RuntimeException(e);
62 Set<String> ymlPayloads = ymlPayloadsToLoad.stream().map(loadResource).collect(Collectors.toSet());
63 compareXmlPayloads(toscaFiles, ymlPayloads);
67 * Compare 2 XML strings to see if they have the same content
71 * @return true if similar
73 public boolean compareXmlStrings(String string1, String string2) {
74 boolean similar = false;
77 similar = new Diff(string1, string2).similar();
78 } catch (SAXException | IOException e) { // NOSONAR
85 public byte[] getCompressedArtifact(String resourceName) throws IOException {
86 return loadResourceBytes(CSAR_INPUTS_FOLDER + resourceName);
89 public byte[] loadResourceBytes(String resourceName) throws IOException {
90 return IOUtils.toByteArray(getResource(resourceName));
93 public String loadResourceAsString(String resourceName) throws IOException {
94 return IOUtils.toString(getResource(resourceName), Charset.defaultCharset());
97 public String getRequestJson(String resource) throws IOException {
98 return loadResourceAsString(JSON_REQUESTS_FOLDER + resource);
101 public String getResponseJson(String jsonResponse) throws IOException, URISyntaxException {
102 return readstringFromFile(JSON_RESPONSES_FOLDER + jsonResponse);
105 public String readstringFromFile(String resourceFile) throws IOException, URISyntaxException {
106 return Files.lines(Paths.get(getResource(resourceFile).toURI())).collect(Collectors.joining());
109 public String getResourcePath(String resourceName) {
110 return getResource(resourceName).getPath();
113 private URL getResource(String resourceName) {
114 return ArtifactTestUtils.class.getClassLoader().getResource(resourceName);
117 private void compareXmlPayloads(List<Artifact> toscaFiles, Set<String> ymlPayloads) {
118 for (Artifact artifact : toscaFiles) {
119 boolean payloadFound = false;
120 for (String ymlPayload : ymlPayloads) {
122 if (compareXmlStrings(convertToString(artifact.getPayload()), ymlPayload)) {
127 assertThat("The content of each YAML file must match the actual content of the file extracted ("
128 + artifact.getName() + ")", payloadFound, is(true));
132 private String convertToString(byte[] byteArray) {
133 return new String(Base64.getDecoder().decode(byteArray), Charset.defaultCharset());