From 0c9acb0e4e7ecde50c274ba60535fafc968f59ba Mon Sep 17 00:00:00 2001 From: "mark.j.leonard" Date: Wed, 5 Dec 2018 18:06:02 +0000 Subject: [PATCH] Compare YML files as Strings, not XML Simplify the JUnit testing for the YAML file extractor and compare the actual and expected files using Strings (with line endings normalized). Change-Id: Ie78d3506511b4a1d5333c1b0b95a676bbc2ab5e2 Issue-ID: AAI-1991 Signed-off-by: mark.j.leonard --- .../org/onap/aai/babel/util/ArtifactTestUtils.java | 51 +++++++++------------- 1 file changed, 21 insertions(+), 30 deletions(-) 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..3cbe194 100644 --- a/src/test/java/org/onap/aai/babel/util/ArtifactTestUtils.java +++ b/src/test/java/org/onap/aai/babel/util/ArtifactTestUtils.java @@ -20,7 +20,10 @@ */ 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; @@ -30,9 +33,9 @@ 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.stream.Collectors; import org.apache.commons.io.IOUtils; import org.custommonkey.xmlunit.Diff; @@ -48,19 +51,22 @@ public class ArtifactTestUtils { 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); + 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)); + } + + 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))); + } } /** @@ -114,21 +120,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()); } -- 2.16.6