Remove all references to artifactgenerator config
[aai/babel.git] / src / test / java / org / onap / aai / babel / util / ArtifactTestUtils.java
index 74f0c0e..066911e 100644 (file)
@@ -2,8 +2,8 @@
  * ============LICENSE_START=======================================================
  * org.onap.aai
  * ================================================================================
- * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
- * Copyright © 2017 European Software Marketing Ltd.
+ * 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.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * ============LICENSE_END=========================================================
- *
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.
  */
+
 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.junit.Assert.assertThat;
 
-import com.google.common.base.Throwables;
-import java.io.BufferedReader;
-import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.InputStreamReader;
+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.Set;
+import java.util.Map;
+import java.util.Properties;
 import java.util.stream.Collectors;
 import org.apache.commons.io.IOUtils;
-import org.openecomp.sdc.generator.data.Artifact;
+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 tests.
+ * This class provides some utilities to assist with running local unit tests.
  */
 public class ArtifactTestUtils {
 
-    public void performYmlAsserts(List<Artifact> toscaFiles, List<String> ymlPayloadsToLoad) {
-        assertThat("An unexpected number of yml files have been extracted", toscaFiles.size(),
-                is(ymlPayloadsToLoad.size()));
-
-        Set<String> ymlPayloads = ymlPayloadsToLoad.stream().map(s -> {
-            try {
-                return loadResourceAsString(s);
-            } catch (IOException e) {
-                throw Throwables.propagate(e);
-            }
-        }).collect(Collectors.toSet());
-
-        toscaFiles.forEach(ts -> {
-            boolean payloadFound = false;
-
-            String s = bytesToString(ts.getPayload());
-
-            for (String ymlPayload : ymlPayloads) {
-                String tscontent = ymlPayload;
-
-                if (s.endsWith(tscontent)) {
-                    payloadFound = true;
-                    break;
-                }
-            }
-            assertThat("The content of each yml file must match the actual content of the file extracted ("
-                    + ts.getName() + ")", payloadFound, is(true));
-        });
+    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));
     }
 
-    public byte[] loadResource(String resourceName) throws IOException {
+    /**
+     * 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));
+    }
 
-        return IOUtils.toByteArray(ArtifactTestUtils.class.getClassLoader().getResource(resourceName));
+    /**
+     * 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);
+            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)));
+        }
     }
 
-    public String loadResourceAsString(String resourceName) throws IOException {
+    /**
+     * 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();
+    }
 
-        InputStream is = ArtifactTestUtils.class.getClassLoader().getResource(resourceName).openStream();
+    public byte[] getCompressedArtifact(String resourceName) throws IOException {
+        return loadResourceBytes(CSAR_INPUTS_FOLDER + resourceName);
+    }
 
-        String result = new BufferedReader(new InputStreamReader(is)).lines().collect(Collectors.joining("\n"));
+    public byte[] loadResourceBytes(String resourceName) throws IOException {
+        return IOUtils.toByteArray(getResource(resourceName));
+    }
 
-        return result;
+    public String loadResourceAsString(String resourceName) throws IOException {
+        return IOUtils.toString(getResource(resourceName), Charset.defaultCharset());
+    }
+
+    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());
     }
 
-    public String bytesToString(byte[] source) {
-        ByteArrayInputStream bis = new ByteArrayInputStream(Base64.getDecoder().decode(source));
+    /**
+     * 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;
+    }
 
-        String result = new BufferedReader(new InputStreamReader(bis)).lines().collect(Collectors.joining("\n"));
+    public String getResourcePath(String resourceName) {
+        return getResource(resourceName).getPath();
+    }
 
-        return result;
+    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());
     }
 
 }