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;
28 import java.nio.charset.Charset;
29 import java.util.Base64;
30 import java.util.List;
32 import java.util.stream.Collectors;
33 import org.apache.commons.io.IOUtils;
34 import org.custommonkey.xmlunit.Diff;
35 import org.onap.aai.babel.xml.generator.data.Artifact;
36 import org.xml.sax.SAXException;
39 * This class provides some utilities to assist with running tests.
41 public class ArtifactTestUtils {
43 public void performYmlAsserts(List<Artifact> toscaFiles, List<String> ymlPayloadsToLoad) {
44 assertThat("An unexpected number of yml files have been extracted", toscaFiles.size(),
45 is(ymlPayloadsToLoad.size()));
47 Set<String> ymlPayloads = ymlPayloadsToLoad.stream().map(s -> {
49 return loadResourceAsString(s);
50 } catch (IOException e) {
51 throw new RuntimeException(e);
53 }).collect(Collectors.toSet());
55 compareXMLPayloads(toscaFiles, ymlPayloads);
59 * Compare 2 XML strings to see if they have the same content
63 * @return true if similar
65 public boolean compareXMLStrings(String string1, String string2) {
66 boolean similar = false;
69 similar = new Diff(string1, string2).similar();
70 } catch (SAXException | IOException e) { // NOSONAR
77 public byte[] loadResource(String resourceName) throws IOException {
78 return IOUtils.toByteArray(getResource(resourceName));
81 public String loadResourceAsString(String resourceName) throws IOException {
82 return IOUtils.toString(getResource(resourceName));
85 private void compareXMLPayloads(List<Artifact> toscaFiles, Set<String> ymlPayloads) {
86 for (Artifact artifact : toscaFiles) {
87 boolean payloadFound = false;
88 for (String ymlPayload : ymlPayloads) {
90 if (compareXMLStrings(convertToString(artifact.getPayload()), ymlPayload)) {
95 assertThat("The content of each yml file must match the actual content of the file extracted ("
96 + artifact.getName() + ")", payloadFound, is(true));
100 private URL getResource(String resourceName) {
101 return ArtifactTestUtils.class.getClassLoader().getResource(resourceName);
104 private String convertToString(byte[] byteArray) {
105 return new String(Base64.getDecoder().decode(byteArray), Charset.defaultCharset());