Incorporate the ECOMP SDC Artefact Generator code
[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.URL;
28 import java.nio.charset.Charset;
29 import java.util.Base64;
30 import java.util.List;
31 import java.util.Set;
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;
37
38 /**
39  * This class provides some utilities to assist with running tests.
40  */
41 public class ArtifactTestUtils {
42
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()));
46
47         Set<String> ymlPayloads = ymlPayloadsToLoad.stream().map(s -> {
48             try {
49                 return loadResourceAsString(s);
50             } catch (IOException e) {
51                 throw new RuntimeException(e);
52             }
53         }).collect(Collectors.toSet());
54
55         compareXMLPayloads(toscaFiles, ymlPayloads);
56     }
57
58     /**
59      * Compare 2 XML strings to see if they have the same content
60      *
61      * @param string1
62      * @param string2
63      * @return true if similar
64      */
65     public boolean compareXMLStrings(String string1, String string2) {
66         boolean similar = false;
67
68         try {
69             similar = new Diff(string1, string2).similar();
70         } catch (SAXException | IOException e) { // NOSONAR
71             similar = true;
72         }
73
74         return similar;
75     }
76
77     public byte[] loadResource(String resourceName) throws IOException {
78         return IOUtils.toByteArray(getResource(resourceName));
79     }
80
81     public String loadResourceAsString(String resourceName) throws IOException {
82         return IOUtils.toString(getResource(resourceName));
83     }
84
85     private void compareXMLPayloads(List<Artifact> toscaFiles, Set<String> ymlPayloads) {
86         for (Artifact artifact : toscaFiles) {
87             boolean payloadFound = false;
88             for (String ymlPayload : ymlPayloads) {
89
90                 if (compareXMLStrings(convertToString(artifact.getPayload()), ymlPayload)) {
91                     payloadFound = true;
92                     break;
93                 }
94             }
95             assertThat("The content of each yml file must match the actual content of the file extracted ("
96                     + artifact.getName() + ")", payloadFound, is(true));
97         }
98     }
99
100     private URL getResource(String resourceName) {
101         return ArtifactTestUtils.class.getClassLoader().getResource(resourceName);
102     }
103
104     private String convertToString(byte[] byteArray) {
105         return new String(Base64.getDecoder().decode(byteArray), Charset.defaultCharset());
106     }
107 }