X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Forg%2Fonap%2Faai%2Fbabel%2Futil%2FArtifactTestUtils.java;h=a98b7c2a5eecb70eb28814d08cfce0f62109209f;hb=60457e41054465e9fa10c8e6bfdbfa62a45d1f0a;hp=cf96d246efbf21e9506da228d999b2eb8f69f28d;hpb=f3bf5dd360b93fd7ef6cc8d871a9903de9f27e5a;p=aai%2Fbabel.git diff --git a/src/test/java/org/onap/aai/babel/util/ArtifactTestUtils.java b/src/test/java/org/onap/aai/babel/util/ArtifactTestUtils.java index cf96d24..a98b7c2 100644 --- a/src/test/java/org/onap/aai/babel/util/ArtifactTestUtils.java +++ b/src/test/java/org/onap/aai/babel/util/ArtifactTestUtils.java @@ -2,8 +2,8 @@ * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ - * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017-2018 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. @@ -18,68 +18,104 @@ * 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.junit.Assert.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.Set; -import java.util.function.Function; +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 tests. + * 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/"; - private static final String CSAR_INPUTS_FOLDER = "compressedArtifacts/"; - - public void performYmlAsserts(List toscaFiles, List ymlPayloadsToLoad) { - assertThat("An unexpected number of YAML files have been extracted", toscaFiles.size(), - is(ymlPayloadsToLoad.size())); - - Function loadResource = s -> { - try { - return loadResourceAsString(s); - } catch (IOException e) { - throw new RuntimeException(e); - } - }; - Set ymlPayloads = ymlPayloadsToLoad.stream().map(loadResource).collect(Collectors.toSet()); - compareXmlPayloads(toscaFiles, ymlPayloads); + + /** + * 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)); } /** - * Compare 2 XML strings to see if they have the same content + * Specific test method for the YAML Extractor test. * - * @param string1 - * @param string2 - * @return true if similar + * @param toscaFiles + * files extracted by the YamlExtractor + * @param ymlPayloadsToLoad + * the expected YAML files + * @throws IOException + * if an I/O exception occurs */ - public boolean compareXmlStrings(String string1, String string2) { - boolean similar = false; + public void performYmlAsserts(List toscaFiles, List ymlPayloadsToLoad) throws IOException { + assertThat("An incorrect number of YAML files have been extracted", toscaFiles.size(), + is(equalTo(ymlPayloadsToLoad.size()))); - try { - similar = new Diff(string1, string2).similar(); - } catch (SAXException | IOException e) { // NOSONAR - similar = true; + Map ymlMap = new HashMap<>(); + for (String filename : ymlPayloadsToLoad) { + ymlMap.put(filename, loadResourceAsString(filename)); } - return similar; + 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))); + } + } + + /** + * 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 { @@ -91,7 +127,11 @@ public class ArtifactTestUtils { } 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 { @@ -106,6 +146,23 @@ public class ArtifactTestUtils { 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(); } @@ -114,21 +171,6 @@ public class ArtifactTestUtils { return ArtifactTestUtils.class.getClassLoader().getResource(resourceName); } - private void compareXmlPayloads(List toscaFiles, Set ymlPayloads) { - for (Artifact artifact : toscaFiles) { - boolean payloadFound = false; - for (String ymlPayload : ymlPayloads) { - - if (compareXmlStrings(convertToString(artifact.getPayload()), ymlPayload)) { - payloadFound = true; - break; - } - } - assertThat("The content of each YAML file must match the actual content of the file extracted (" - + artifact.getName() + ")", payloadFound, is(true)); - } - } - private String convertToString(byte[] byteArray) { return new String(Base64.getDecoder().decode(byteArray), Charset.defaultCharset()); }