Merge branch 'madhusudancs/testframework-update'
diff --git a/asterix-app/src/main/java/edu/uci/ics/asterix/api/http/servlet/RESTAPIServlet.java b/asterix-app/src/main/java/edu/uci/ics/asterix/api/http/servlet/RESTAPIServlet.java
index c5d0e57..8caf05e 100644
--- a/asterix-app/src/main/java/edu/uci/ics/asterix/api/http/servlet/RESTAPIServlet.java
+++ b/asterix-app/src/main/java/edu/uci/ics/asterix/api/http/servlet/RESTAPIServlet.java
@@ -101,9 +101,11 @@
             String errorMessage = ResultUtils.buildParseExceptionMessage(pe, query);
             JSONObject errorResp = ResultUtils.getErrorResponse(2, errorMessage, "", "");
             out.write(errorResp.toString());
+            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
         } catch (Exception e) {
             GlobalConfig.ASTERIX_LOGGER.log(Level.SEVERE, e.getMessage(), e);
             ResultUtils.apiErrorHandler(out, e);
+            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
         }
     }
 
diff --git a/asterix-app/src/test/resources/runtimets/queries/aggregate/query-issue400/query-issue400.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/aggregate/query-issue400/query-issue400.3.query.aql
index 9819455..4611058 100644
--- a/asterix-app/src/test/resources/runtimets/queries/aggregate/query-issue400/query-issue400.3.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/aggregate/query-issue400/query-issue400.3.query.aql
@@ -6,4 +6,4 @@
  */
  
 let $l := [[1,2,3,4,5],[6,7,8,9]]
-return count(for $i in $l return $i)
\ No newline at end of file
+return count(for $i in $l return $i)
diff --git a/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/udf29/udf29.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/udf29/udf29.1.ddl.aql
index edede00..f9f1e25 100644
--- a/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/udf29/udf29.1.ddl.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/udf29/udf29.1.ddl.aql
@@ -8,9 +8,3 @@
 drop dataverse test if exists;
 create dataverse test;
 
-use dataverse test;
-
-declare function test.f1(){
-100
-};
-
diff --git a/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/udf29/udf29.2.query.aql b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/udf29/udf29.2.query.aql
index 2f9e763..5755963 100644
--- a/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/udf29/udf29.2.query.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/udf29/udf29.2.query.aql
@@ -5,5 +5,10 @@
  */
 
 use dataverse test;
+
+declare function f1(){
+100
+};
+
 let $x:=f1()
 return $x
diff --git a/asterix-common/pom.xml b/asterix-common/pom.xml
index db20c29..fa48b9e 100644
--- a/asterix-common/pom.xml
+++ b/asterix-common/pom.xml
@@ -114,6 +114,16 @@
 			<version>4.8.1</version>
 			<scope>test</scope>
 		</dependency>
+		<dependency>
+			<groupId>com.fasterxml.jackson.core</groupId>
+			<artifactId>jackson-core</artifactId>
+			<version>2.2.0</version>
+		</dependency>
+		<dependency>
+			<groupId>org.codehaus.jackson</groupId>
+			<artifactId>jackson-mapper-asl</artifactId>
+			<version>1.9.12</version>
+		</dependency>
 	</dependencies>
 
 </project>
diff --git a/asterix-common/src/test/java/edu/uci/ics/asterix/test/aql/TestsUtils.java b/asterix-common/src/test/java/edu/uci/ics/asterix/test/aql/TestsUtils.java
index 5e599c3..ac8a3bd 100644
--- a/asterix-common/src/test/java/edu/uci/ics/asterix/test/aql/TestsUtils.java
+++ b/asterix-common/src/test/java/edu/uci/ics/asterix/test/aql/TestsUtils.java
@@ -9,13 +9,13 @@
 import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.PrintWriter;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
-import java.util.Iterator;
 import java.util.List;
-import java.util.NoSuchElementException;
+import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
@@ -24,10 +24,15 @@
 import org.apache.commons.httpclient.NameValuePair;
 import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.commons.httpclient.params.HttpMethodParams;
-import org.json.JSONArray;
+import org.codehaus.jackson.map.JsonMappingException;
 import org.json.JSONException;
 import org.json.JSONObject;
 
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonToken;
+
+import edu.uci.ics.asterix.common.config.GlobalConfig;
 import edu.uci.ics.asterix.testframework.context.TestCaseContext;
 import edu.uci.ics.asterix.testframework.context.TestFileContext;
 import edu.uci.ics.asterix.testframework.xml.TestCase.CompilationUnit;
@@ -135,84 +140,46 @@
         return fname.substring(0, dot + 1) + EXTENSION_AQL_RESULT;
     }
 
-    public static void writeResultsToFile(File actualFile, JSONObject result) throws IOException, JSONException {
+    public static void writeResultsToFile(File actualFile, InputStream resultStream) throws IOException, JSONException {
         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);
+        try {
+            JsonFactory jsonFactory = new JsonFactory();
+            JsonParser resultParser = jsonFactory.createParser(resultStream);
+            while (resultParser.nextToken() == JsonToken.START_OBJECT) {
+                while (resultParser.nextToken() != JsonToken.END_OBJECT) {
+                    String key = resultParser.getCurrentName();
+                    if (key.equals("results")) {
+                        // Start of array.
+                        resultParser.nextToken();
+                        while (resultParser.nextToken() != JsonToken.END_ARRAY) {
+                            String record = resultParser.getValueAsString();
+                            writer.write(record);
+                        }
+                    } else {
+                        String summary = resultParser.getValueAsString();
+                        if (key.equals("summary")) {
+                            writer.write(summary);
+                            throw new JsonMappingException("Could not find results key in the JSON Object");
+                        }
+                    }
+                }
+            }
+        } finally {
+            writer.close();
         }
     }
 
-    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 UnsupportedOperationException();
-        }
+    private static String[] handleError(GetMethod method) throws Exception {
+        String errorBody = method.getResponseBodyAsString();
+        JSONObject result = new JSONObject(errorBody);
+        String[] errors = { result.getJSONArray("error-code").getString(0), result.getString("summary"),
+                result.getString("stacktrace") };
+        return errors;
     }
 
     // Executes Query and returns results as JSONArray
-    public static JSONObject executeQuery(String str) throws Exception {
+    public static InputStream executeQuery(String str) throws Exception {
+        InputStream resultStream = null;
 
         final String url = "http://localhost:19101/query";
 
@@ -227,26 +194,22 @@
         // Provide custom retry handler is necessary
         method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
 
-        JSONObject result = null;
-
         try {
             // Execute the method.
             int statusCode = client.executeMethod(method);
 
             // Check if the method was executed successfully.
             if (statusCode != HttpStatus.SC_OK) {
-                System.err.println("Method failed: " + method.getStatusLine());
+                GlobalConfig.ASTERIX_LOGGER.log(Level.SEVERE, "Method failed: " + method.getStatusLine());
             }
 
-            // Read the response body as String.
-            String responseBody = method.getResponseBodyAsString();
-
-            result = new JSONObject(responseBody);
+            // Read the response body as stream
+            resultStream = method.getResponseBodyAsStream();
         } catch (Exception e) {
-            System.out.println(e.getMessage());
+            GlobalConfig.ASTERIX_LOGGER.log(Level.SEVERE, e.getMessage(), e);
             e.printStackTrace();
         }
-        return result;
+        return resultStream;
     }
 
     // To execute Update statements
@@ -270,7 +233,10 @@
 
         // Check if the method was executed successfully.
         if (statusCode != HttpStatus.SC_OK) {
-            System.err.println("Method failed: " + method.getStatusLine());
+            GlobalConfig.ASTERIX_LOGGER.log(Level.SEVERE, "Method failed: " + method.getStatusLine());
+            String[] errors = handleError(method);
+            GlobalConfig.ASTERIX_LOGGER.log(Level.SEVERE, errors[2]);
+            throw new Exception("DDL operation failed: " + errors[0]);
         }
     }
 
@@ -299,7 +265,10 @@
 
         // Check if the method was executed successfully.
         if (statusCode != HttpStatus.SC_OK) {
-            System.err.println("Method failed: " + method.getStatusLine());
+            GlobalConfig.ASTERIX_LOGGER.log(Level.SEVERE, "Method failed: " + method.getStatusLine());
+            String[] errors = handleError(method);
+            GlobalConfig.ASTERIX_LOGGER.log(Level.SEVERE, errors[2]);
+            throw new Exception("DDL operation failed: " + errors[0]);
         }
     }
 
@@ -342,7 +311,7 @@
 
         List<CompilationUnit> cUnits = testCaseCtx.getTestCase().getCompilationUnit();
         for (CompilationUnit cUnit : cUnits) {
-            
+
             testFileCtxs = testCaseCtx.getTestFiles(cUnit);
             expectedResultFileCtxs = testCaseCtx.getExpectedResultFiles(cUnit);
 
@@ -358,24 +327,24 @@
                             TestsUtils.executeUpdate(statement);
                             break;
                         case "query":
-                            result = TestsUtils.executeQuery(statement);
-                            if (result.has("error-code")) {
-                                throw new Exception("Test \"" + testFile + "\" FAILED!\n" + result + "\n");
-                            } else {
+                            try {
+                                InputStream resultStream = executeQuery(statement);
                                 expectedResultFile = expectedResultFileCtxs.get(queryCount).getFile();
 
                                 File actualFile = new File(actualPath + File.separator
                                         + testCaseCtx.getTestCase().getFilePath().replace(File.separator, "_") + "_"
                                         + cUnit.getName() + ".adm");
+                                TestsUtils.writeResultsToFile(actualFile, resultStream);
 
                                 File actualResultFile = testCaseCtx.getActualResultFile(cUnit, new File(actualPath));
                                 actualResultFile.getParentFile().mkdirs();
 
-                                TestsUtils.writeResultsToFile(actualFile, result);
-
                                 TestsUtils.runScriptAndCompareWithResult(testFile, new PrintWriter(System.err),
                                         expectedResultFile, actualFile);
-                                LOGGER.info("[TEST]: " + testCaseCtx.getTestCase().getFilePath() + "/" + cUnit.getName() +  " PASSED ");
+                                LOGGER.info("[TEST]: " + testCaseCtx.getTestCase().getFilePath() + "/"
+                                        + cUnit.getName() + " PASSED ");
+                            } catch (JsonMappingException e) {
+                                throw new Exception("Test \"" + testFile + "\" FAILED!\n");
                             }
                             queryCount++;
                             break;
@@ -385,7 +354,7 @@
                         default:
                             throw new IllegalArgumentException("No statements of type " + ctx.getType());
                     }
-                    
+
                 } catch (Exception e) {
                     if (cUnit.getExpectedError().isEmpty()) {
                         throw new Exception("Test \"" + testFile + "\" FAILED!", e);
@@ -393,7 +362,6 @@
                 }
             }
         }
-
     }
 }