Modified test framework to write the results from REST API to file before comparing.

git-svn-id: https://asterixdb.googlecode.com/svn/branches/asterix_stabilization_api_cleanup@1500 eaa15691-b419-025a-1212-ee371bd00084
diff --git a/asterix-app/src/test/java/edu/uci/ics/asterix/test/aql/TestsUtils.java b/asterix-app/src/test/java/edu/uci/ics/asterix/test/aql/TestsUtils.java
index 1555549..b82c13c 100644
--- a/asterix-app/src/test/java/edu/uci/ics/asterix/test/aql/TestsUtils.java
+++ b/asterix-app/src/test/java/edu/uci/ics/asterix/test/aql/TestsUtils.java
@@ -3,14 +3,21 @@
 import static org.junit.Assert.fail;
 
 import java.io.BufferedReader;
+import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileWriter;
+import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.PrintWriter;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
 
 import org.json.JSONArray;
 import org.json.JSONException;
+import org.json.JSONObject;
 
+import edu.uci.ics.hyracks.algebricks.common.exceptions.NotImplementedException;
 import edu.uci.ics.hyracks.api.client.IHyracksClientConnection;
 
 public class TestsUtils {
@@ -31,57 +38,44 @@
         return path.delete();
     }
 
-    public static String getNextResult(JSONArray jArray) throws JSONException {
-        String result = null;
-        for (int i = 0; i < jArray.length(); i++) {
-            JSONArray resultArray = jArray.getJSONArray(i);
-            for (int j = 0; j < resultArray.length(); j++) {
-                return resultArray.getString(j);
-            }
-        }
-        return result;
-    }
-
     public static void runScriptAndCompareWithResult(IHyracksClientConnection hcc, File scriptFile, PrintWriter print,
-            File expectedFile, JSONArray jArray) throws Exception {
+            File expectedFile, File actualFile) throws Exception {
         BufferedReader readerExpected = new BufferedReader(new InputStreamReader(new FileInputStream(expectedFile),
                 "UTF-8"));
-
+        BufferedReader readerActual = new BufferedReader(
+                new InputStreamReader(new FileInputStream(actualFile), "UTF-8"));
         String lineExpected, lineActual;
-        int num = 0;
-        int chunkCounter = 0;
-        int recordCounter = 0;
-
+        int num = 1;
         try {
             while ((lineExpected = readerExpected.readLine()) != null) {
-                // Skip the blank line in the expected file.
-                if (lineExpected.isEmpty()) {
-                    continue;
+                lineActual = readerActual.readLine();
+                // Assert.assertEquals(lineExpected, lineActual);
+                if (lineActual == null) {
+                    if (lineExpected.isEmpty()) {
+                        continue;
+                    }
+                    throw new Exception("Result for " + scriptFile + " changed at line " + num + ":\n< " + lineExpected
+                            + "\n> ");
                 }
-                if (jArray.length() <= chunkCounter) {
-                    throw new Exception("No more results available.");
-                }
-                JSONArray resultArray = jArray.getJSONArray(chunkCounter);
 
-                if ((lineActual = resultArray.getString(recordCounter)) == null) {
-                    throw new Exception("Result for " + scriptFile + " changed at line " + num + ":\n<" + lineExpected
-                            + "\n>");
-
-                }
                 if (!equalStrings(lineExpected.split("Timestamp")[0], lineActual.split("Timestamp")[0])) {
                     fail("Result for " + scriptFile + " changed at line " + num + ":\n< " + lineExpected + "\n> "
                             + lineActual);
                 }
 
-                recordCounter++;
-                if (recordCounter >= resultArray.length()) {
-                    chunkCounter++;
-                    recordCounter = 0;
-                }
+                ++num;
             }
+            lineActual = readerActual.readLine();
+            // Assert.assertEquals(null, lineActual);
+            if (lineActual != null) {
+                throw new Exception("Result for " + scriptFile + " changed at line " + num + ":\n< \n> " + lineActual);
+            }
+            // actualFile.delete();
         } finally {
             readerExpected.close();
+            readerActual.close();
         }
+
     }
 
     private static boolean equalStrings(String s1, String s2) {
@@ -127,4 +121,80 @@
         return fname.substring(0, dot + 1) + EXTENSION_AQL_RESULT;
     }
 
+    public static void writeResultsToFile(File actualFile, JSONObject result) throws IOException, JSONException {
+        System.out.println("Writer filename: " + actualFile.toString());
+        BufferedWriter writer = new BufferedWriter(new FileWriter(actualFile));
+        Results res = new Results(result);
+        for (String line : res) {
+            writer.write(line);
+            writer.newLine();
+        }
+        writer.close();
+    }
+
+    public static class Results implements Iterable<String> {
+        private final JSONArray chunks;
+
+        public Results(JSONObject result) throws JSONException {
+            chunks = result.getJSONArray("results");
+        }
+
+        public Iterator<String> iterator() {
+            return new ResultIterator(chunks);
+        }
+    }
+
+    public static class ResultIterator implements Iterator<String> {
+        private final JSONArray chunks;
+
+        private int chunkCounter = 0;
+        private int recordCounter = 0;
+
+        public ResultIterator(JSONArray chunks) {
+            this.chunks = chunks;
+        }
+
+        @Override
+        public boolean hasNext() {
+            JSONArray resultArray;
+            try {
+                resultArray = chunks.getJSONArray(chunkCounter);
+                if (resultArray.getString(recordCounter) != null) {
+                    return true;
+                }
+            } catch (JSONException e) {
+                return false;
+            }
+            return false;
+        }
+
+        @Override
+        public String next() throws NoSuchElementException {
+            JSONArray resultArray;
+            String item = "";
+
+            try {
+                resultArray = chunks.getJSONArray(chunkCounter);
+                item = resultArray.getString(recordCounter);
+                if (item == null) {
+                    throw new NoSuchElementException();
+                }
+                item = item.trim();
+
+                recordCounter++;
+                if (recordCounter >= resultArray.length()) {
+                    chunkCounter++;
+                    recordCounter = 0;
+                }
+            } catch (JSONException e) {
+                throw new NoSuchElementException(e.getMessage());
+            }
+            return item;
+        }
+
+        @Override
+        public void remove() {
+            throw new NotImplementedException();
+        }
+    }
 }
diff --git a/asterix-app/src/test/java/edu/uci/ics/asterix/test/metadata/MetadataTest.java b/asterix-app/src/test/java/edu/uci/ics/asterix/test/metadata/MetadataTest.java
index 2519544..7ab39f4 100644
--- a/asterix-app/src/test/java/edu/uci/ics/asterix/test/metadata/MetadataTest.java
+++ b/asterix-app/src/test/java/edu/uci/ics/asterix/test/metadata/MetadataTest.java
@@ -260,11 +260,19 @@
                                 }
                             } else {
                                 expectedResultFile = expectedResultFileCtxs.get(queryCount).getFile();
-                                TestsUtils
-                                        .runScriptAndCompareWithResult(
-                                                AsterixHyracksIntegrationUtil.getHyracksClientConnection(), testFile,
-                                                new PrintWriter(System.err), expectedResultFile,
-                                                result.getJSONArray("results"));
+
+                                File actualFile = new File(PATH_ACTUAL + File.separator
+                                        + tcCtx.getTestCase().getFilePath().replace(File.separator, "_") + "_"
+                                        + cUnit.getName() + ".adm");
+
+                                File actualResultFile = tcCtx.getActualResultFile(cUnit, new File(PATH_ACTUAL));
+                                actualResultFile.getParentFile().mkdirs();
+
+                                TestsUtils.writeResultsToFile(actualFile, result);
+
+                                TestsUtils.runScriptAndCompareWithResult(
+                                        AsterixHyracksIntegrationUtil.getHyracksClientConnection(), testFile,
+                                        new PrintWriter(System.err), expectedResultFile, actualFile);
                             }
                             queryCount++;
                             break;
diff --git a/asterix-app/src/test/java/edu/uci/ics/asterix/test/runtime/ExecutionTest.java b/asterix-app/src/test/java/edu/uci/ics/asterix/test/runtime/ExecutionTest.java
index aacec61..974eb94 100644
--- a/asterix-app/src/test/java/edu/uci/ics/asterix/test/runtime/ExecutionTest.java
+++ b/asterix-app/src/test/java/edu/uci/ics/asterix/test/runtime/ExecutionTest.java
@@ -233,13 +233,6 @@
         for (CompilationUnit cUnit : cUnits) {
             LOGGER.info("[TEST]: " + tcCtx.getTestCase().getFilePath() + "/" + cUnit.getName());
 
-//            if (!(tcCtx.getTestCase().getFilePath().contains("dml") && cUnit.getName().equals(
-//                    "delete-from-loaded-dataset-with-index"))) {
-//                continue;
-//            }
-//
-//            System.out.println("/Test/: " + tcCtx.getTestCase().getFilePath() + "/" + cUnit.getName());
-
             testFileCtxs = tcCtx.getTestFiles(cUnit);
             expectedResultFileCtxs = tcCtx.getExpectedResultFiles(cUnit);
 
@@ -262,11 +255,19 @@
                                 }
                             } else {
                                 expectedResultFile = expectedResultFileCtxs.get(queryCount).getFile();
-                                TestsUtils
-                                        .runScriptAndCompareWithResult(
-                                                AsterixHyracksIntegrationUtil.getHyracksClientConnection(), testFile,
-                                                new PrintWriter(System.err), expectedResultFile,
-                                                result.getJSONArray("results"));
+
+                                File actualFile = new File(PATH_ACTUAL + File.separator
+                                        + tcCtx.getTestCase().getFilePath().replace(File.separator, "_") + "_"
+                                        + cUnit.getName() + ".adm");
+
+                                File actualResultFile = tcCtx.getActualResultFile(cUnit, new File(PATH_ACTUAL));
+                                actualResultFile.getParentFile().mkdirs();
+
+                                TestsUtils.writeResultsToFile(actualFile, result);
+
+                                TestsUtils.runScriptAndCompareWithResult(
+                                        AsterixHyracksIntegrationUtil.getHyracksClientConnection(), testFile,
+                                        new PrintWriter(System.err), expectedResultFile, actualFile);
                             }
                             queryCount++;
                             break;
diff --git a/asterix-test-framework/src/main/java/edu/uci/ics/asterix/testframework/context/TestCaseContext.java b/asterix-test-framework/src/main/java/edu/uci/ics/asterix/testframework/context/TestCaseContext.java
index d1818ff..bb89a76 100644
--- a/asterix-test-framework/src/main/java/edu/uci/ics/asterix/testframework/context/TestCaseContext.java
+++ b/asterix-test-framework/src/main/java/edu/uci/ics/asterix/testframework/context/TestCaseContext.java
@@ -107,6 +107,13 @@
         return resultFileCtxs;
     }
 
+    public File getActualResultFile(CompilationUnit cUnit, File actualResultsBase) {
+        File path = actualResultsBase;
+        path = new File(path, testSuite.getResultOffsetPath());
+        path = new File(path, testCase.getFilePath());
+        return new File(path, cUnit.getOutputDir().getValue() + ".adm");
+    }
+
     public static class Builder {
         public Builder() {
         }