Incorporate the ECOMP SDC Artefact Generator code
[aai/babel.git] / src / test / java / org / onap / aai / babel / util / ArtifactTestUtils.java
index 74f0c0e..5a4224c 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 © 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright © 2017-2018 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.is;
 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.URL;
+import java.nio.charset.Charset;
 import java.util.Base64;
 import java.util.List;
 import java.util.Set;
 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.xml.generator.data.Artifact;
+import org.xml.sax.SAXException;
 
 /**
  * This class provides some utilities to assist with running tests.
@@ -51,50 +48,60 @@ public class ArtifactTestUtils {
             try {
                 return loadResourceAsString(s);
             } catch (IOException e) {
-                throw Throwables.propagate(e);
+                throw new RuntimeException(e);
             }
         }).collect(Collectors.toSet());
 
-        toscaFiles.forEach(ts -> {
-            boolean payloadFound = false;
+        compareXMLPayloads(toscaFiles, ymlPayloads);
+    }
+
+    /**
+     * Compare 2 XML strings to see if they have the same content
+     *
+     * @param string1
+     * @param string2
+     * @return true if similar
+     */
+    public boolean compareXMLStrings(String string1, String string2) {
+        boolean similar = false;
+
+        try {
+            similar = new Diff(string1, string2).similar();
+        } catch (SAXException | IOException e) { // NOSONAR
+            similar = true;
+        }
+
+        return similar;
+    }
+
+    public byte[] loadResource(String resourceName) throws IOException {
+        return IOUtils.toByteArray(getResource(resourceName));
+    }
 
-            String s = bytesToString(ts.getPayload());
+    public String loadResourceAsString(String resourceName) throws IOException {
+        return IOUtils.toString(getResource(resourceName));
+    }
 
+    private void compareXMLPayloads(List<Artifact> toscaFiles, Set<String> ymlPayloads) {
+        for (Artifact artifact : toscaFiles) {
+            boolean payloadFound = false;
             for (String ymlPayload : ymlPayloads) {
-                String tscontent = ymlPayload;
 
-                if (s.endsWith(tscontent)) {
+                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 ("
-                    + ts.getName() + ")", payloadFound, is(true));
-        });
-    }
-
-    public byte[] loadResource(String resourceName) throws IOException {
-
-        return IOUtils.toByteArray(ArtifactTestUtils.class.getClassLoader().getResource(resourceName));
+                    + artifact.getName() + ")", payloadFound, is(true));
+        }
     }
 
-    public String loadResourceAsString(String resourceName) throws IOException {
-
-        InputStream is = ArtifactTestUtils.class.getClassLoader().getResource(resourceName).openStream();
-
-        String result = new BufferedReader(new InputStreamReader(is)).lines().collect(Collectors.joining("\n"));
-
-        return result;
-
+    private URL getResource(String resourceName) {
+        return ArtifactTestUtils.class.getClassLoader().getResource(resourceName);
     }
 
-    public String bytesToString(byte[] source) {
-        ByteArrayInputStream bis = new ByteArrayInputStream(Base64.getDecoder().decode(source));
-
-        String result = new BufferedReader(new InputStreamReader(bis)).lines().collect(Collectors.joining("\n"));
-
-        return result;
-
+    private String convertToString(byte[] byteArray) {
+        return new String(Base64.getDecoder().decode(byteArray), Charset.defaultCharset());
     }
-
 }