X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Forg%2Fonap%2Faai%2Fbabel%2Futil%2FArtifactTestUtils.java;h=fa0b78458b69163b95eafd4cf5a18ca060a79985;hb=0408ea82dc1a55b4f30e7062eb91e76b94091392;hp=5a4224cdfe28d3bfab5fef42af189548cecd24eb;hpb=66b3afa06776e9944ad515206d281d67747c9770;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 5a4224c..fa0b784 100644 --- a/src/test/java/org/onap/aai/babel/util/ArtifactTestUtils.java +++ b/src/test/java/org/onap/aai/babel/util/ArtifactTestUtils.java @@ -20,15 +20,22 @@ */ 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.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.stream.Collectors; import org.apache.commons.io.IOUtils; import org.custommonkey.xmlunit.Diff; @@ -40,61 +47,81 @@ import org.xml.sax.SAXException; */ public class ArtifactTestUtils { - public void performYmlAsserts(List toscaFiles, List ymlPayloadsToLoad) { - assertThat("An unexpected number of yml files have been extracted", toscaFiles.size(), - is(ymlPayloadsToLoad.size())); + private static final String JSON_REQUESTS_FOLDER = "jsonFiles/"; + private static final String JSON_RESPONSES_FOLDER = "response/"; + private static final String CSAR_INPUTS_FOLDER = "compressedArtifacts/"; - Set ymlPayloads = ymlPayloadsToLoad.stream().map(s -> { - try { - return loadResourceAsString(s); - } catch (IOException e) { - throw new RuntimeException(e); - } - }).collect(Collectors.toSet()); + /** + * 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 toscaFiles, List ymlPayloadsToLoad) throws IOException { + assertThat("An incorrect number of YAML files have been extracted", toscaFiles.size(), + is(equalTo(ymlPayloadsToLoad.size()))); + + Map ymlMap = new HashMap<>(); + for (String filename : ymlPayloadsToLoad) { + ymlMap.put(filename, loadResourceAsString(filename)); + } - compareXMLPayloads(toscaFiles, ymlPayloads); + 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 2 XML strings to see if they have the same content + * Compare two XML strings to see if they have the same content. * * @param string1 + * XML content * @param string2 - * @return true if similar + * 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) { - boolean similar = false; - - try { - similar = new Diff(string1, string2).similar(); - } catch (SAXException | IOException e) { // NOSONAR - similar = true; - } + public boolean compareXmlStrings(String string1, String string2) throws SAXException, IOException { + return new Diff(string1, string2).similar(); + } - return similar; + public byte[] getCompressedArtifact(String resourceName) throws IOException { + return loadResourceBytes(CSAR_INPUTS_FOLDER + resourceName); } - public byte[] loadResource(String resourceName) throws IOException { + public byte[] loadResourceBytes(String resourceName) throws IOException { return IOUtils.toByteArray(getResource(resourceName)); } public String loadResourceAsString(String resourceName) throws IOException { - return IOUtils.toString(getResource(resourceName)); + return IOUtils.toString(getResource(resourceName), Charset.defaultCharset()); } - 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 yml file must match the actual content of the file extracted (" - + artifact.getName() + ")", payloadFound, is(true)); - } + 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 getResourcePath(String resourceName) { + return getResource(resourceName).getPath(); } private URL getResource(String resourceName) { @@ -104,4 +131,5 @@ public class ArtifactTestUtils { private String convertToString(byte[] byteArray) { return new String(Base64.getDecoder().decode(byteArray), Charset.defaultCharset()); } + }