Fix remaining Sonar code smells
[aai/babel.git] / src / test / java / org / onap / aai / babel / util / ArtifactTestUtils.java
index 69d7819..cf96d24 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * ============LICENSE_START=======================================================
+ * ============LICENSE_START=======================================================
  * org.onap.aai
  * ================================================================================
  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
@@ -24,11 +24,15 @@ import static org.hamcrest.CoreMatchers.is;
 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.List;
 import java.util.Set;
+import java.util.function.Function;
 import java.util.stream.Collectors;
 import org.apache.commons.io.IOUtils;
 import org.custommonkey.xmlunit.Diff;
@@ -40,19 +44,23 @@ import org.xml.sax.SAXException;
  */
 public class ArtifactTestUtils {
 
+    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<Artifact> toscaFiles, List<String> ymlPayloadsToLoad) {
-        assertThat("An unexpected number of yml files have been extracted", toscaFiles.size(),
+        assertThat("An unexpected number of YAML files have been extracted", toscaFiles.size(),
                 is(ymlPayloadsToLoad.size()));
 
-        Set<String> ymlPayloads = ymlPayloadsToLoad.stream().map(s -> {
+        Function<? super String, ? extends String> loadResource = s -> {
             try {
                 return loadResourceAsString(s);
             } catch (IOException e) {
                 throw new RuntimeException(e);
             }
-        }).collect(Collectors.toSet());
-
-        compareXMLPayloads(toscaFiles, ymlPayloads);
+        };
+        Set<String> ymlPayloads = ymlPayloadsToLoad.stream().map(loadResource).collect(Collectors.toSet());
+        compareXmlPayloads(toscaFiles, ymlPayloads);
     }
 
     /**
@@ -62,7 +70,7 @@ public class ArtifactTestUtils {
      * @param string2
      * @return true if similar
      */
-    public boolean compareXMLStrings(String string1, String string2) {
+    public boolean compareXmlStrings(String string1, String string2) {
         boolean similar = false;
 
         try {
@@ -74,7 +82,11 @@ public class ArtifactTestUtils {
         return similar;
     }
 
-    public byte[] loadResource(String resourceName) throws IOException {
+    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));
     }
 
@@ -82,26 +94,43 @@ public class ArtifactTestUtils {
         return IOUtils.toString(getResource(resourceName), Charset.defaultCharset());
     }
 
-    private void compareXMLPayloads(List<Artifact> toscaFiles, Set<String> ymlPayloads) {
+    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) {
+        return ArtifactTestUtils.class.getClassLoader().getResource(resourceName);
+    }
+
+    private void compareXmlPayloads(List<Artifact> toscaFiles, Set<String> ymlPayloads) {
         for (Artifact artifact : toscaFiles) {
             boolean payloadFound = false;
             for (String ymlPayload : ymlPayloads) {
 
-                if (compareXMLStrings(convertToString(artifact.getPayload()), ymlPayload)) {
+                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 ("
+            assertThat("The content of each YAML file must match the actual content of the file extracted ("
                     + artifact.getName() + ")", payloadFound, is(true));
         }
     }
 
-    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());
     }
+
 }