aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/aai/babel/util/ArtifactTestUtils.java
blob: 2cf221d1d949a57f4b8f0c9f592f87661eea191d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved.
 * Copyright (c) 2017-2019 European Software Marketing Ltd.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.aai.babel.util;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;

import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;
import org.apache.commons.io.IOUtils;
import org.custommonkey.xmlunit.Diff;
import org.onap.aai.babel.parser.ArtifactGeneratorToscaParser;
import org.onap.aai.babel.xml.generator.data.Artifact;
import org.xml.sax.SAXException;

/**
 * This class provides some utilities to assist with running local unit tests.
 */
public class ArtifactTestUtils {

    public static final String CSAR_INPUTS_FOLDER = "compressedArtifacts/";
    private static final String JSON_REQUESTS_FOLDER = "jsonFiles/";
    private static final String JSON_RESPONSES_FOLDER = "response/";

    /**
     * Initialize System Properties for test configuration files.
     */
    public void setGeneratorSystemProperties() {
        System.setProperty(ArtifactGeneratorToscaParser.PROPERTY_TOSCA_MAPPING_FILE,
                getResourcePath(Resources.TOSCA_MAPPING_CONFIG));
    }

    /**
     * Load the Widget type mappings (resource).
     * 
     * @throws IOException
     *             if the configuration file is not loaded
     */
    public void loadWidgetMappings() throws IOException {
        ArtifactGeneratorToscaParser.initToscaMappingsConfiguration(getResourcePath(Resources.TOSCA_MAPPING_CONFIG));
    }

    /**
     * Specific test method for the YAML Extractor test.
     *
     * @param toscaFiles
     *            files extracted by the YamlExtractor
     * @param ymlPayloadsToLoad
     *            the expected YAML files
     * @throws IOException
     *             if an I/O exception occurs
     */
    public void performYmlAsserts(List<Artifact> toscaFiles, List<String> ymlPayloadsToLoad) throws IOException {
        assertThat("An incorrect number of YAML files have been extracted", toscaFiles.size(),
                is(equalTo(ymlPayloadsToLoad.size())));

        Map<String, String> ymlMap = new HashMap<>();
        for (String filename : ymlPayloadsToLoad) {
            ymlMap.put(filename, loadResourceAsString(filename));
        }

        for (Artifact artifact : toscaFiles) {
            String fileName = artifact.getName().replaceFirst("Definitions/", "ymlFiles/");
            String expectedYaml = ymlMap.get(fileName).replaceAll("\\r\\n?", "\n");
            assertThat("Missing expected content for " + fileName, expectedYaml, is(not(nullValue())));
            assertThat("The content of " + fileName + " must match the expected content",
                    convertToString(artifact.getPayload()).replaceAll("\\r\\n?", "\n"), is(equalTo(expectedYaml)));
        }
    }

    /**
     * Compare two XML strings to see if they have the same content.
     *
     * @param string1
     *            XML content
     * @param string2
     *            XML content
     * @return true if XML content is similar
     * @throws IOException
     *             if an I/O exception occurs
     * @throws SAXException
     *             if the XML parsing fails
     */
    public boolean compareXmlStrings(String string1, String string2) throws SAXException, IOException {
        return new Diff(string1, string2).similar();
    }

    public byte[] getCompressedArtifact(String resourceName) throws IOException {
        return loadResourceBytes(CSAR_INPUTS_FOLDER + resourceName);
    }

    public byte[] loadResourceBytes(String resourceName) throws IOException {
        return IOUtils.toByteArray(getResource(resourceName));
    }

    public String loadResourceAsString(String resourceName) throws IOException {
        try {
            return IOUtils.toString(getResource(resourceName), Charset.defaultCharset());
        } catch (NullPointerException e) {
            throw new IllegalArgumentException("No such resource " + resourceName);
        }
    }

    public String getRequestJson(String resource) throws IOException {
        return loadResourceAsString(JSON_REQUESTS_FOLDER + resource);
    }

    public String getResponseJson(String jsonResponse) throws IOException, URISyntaxException {
        return readstringFromFile(JSON_RESPONSES_FOLDER + jsonResponse);
    }

    public String readstringFromFile(String resourceFile) throws IOException, URISyntaxException {
        return Files.lines(Paths.get(getResource(resourceFile).toURI())).collect(Collectors.joining());
    }

    /**
     * Create Properties from the content of the named resource (e.g. a file on the classpath).
     * 
     * @param resourceName
     *            the resource name
     * @return Properties loaded from the named resource
     * @throws IOException
     *             if an error occurred when reading from the named resource
     */
    public Properties getResourceAsProperties(String resourceName) throws IOException {
        final Properties properties = new Properties();
        InputStream in = ArtifactTestUtils.class.getClassLoader().getResourceAsStream(resourceName);
        properties.load(in);
        in.close();
        return properties;
    }

    public String getResourcePath(String resourceName) {
        return getResource(resourceName).getPath();
    }

    private URL getResource(String resourceName) {
        return ArtifactTestUtils.class.getClassLoader().getResource(resourceName);
    }

    private String convertToString(byte[] byteArray) {
        return new String(Base64.getDecoder().decode(byteArray), Charset.defaultCharset());
    }

}