Metadata tests integrated to run with REST API test framework

git-svn-id: https://asterixdb.googlecode.com/svn/branches/asterix_stabilization@1421 eaa15691-b419-025a-1212-ee371bd00084
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 fd5af9c..f68f712 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
@@ -14,9 +14,13 @@
  */
 package edu.uci.ics.asterix.test.metadata;
 
+import java.io.BufferedReader;
 import java.io.File;
+import java.io.FileReader;
+import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.List;
 import java.util.logging.Logger;
 
 import org.apache.commons.io.FileUtils;
@@ -27,11 +31,26 @@
 import org.junit.runners.Parameterized;
 import org.junit.runners.Parameterized.Parameters;
 
+import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpStatus;
+import org.apache.commons.httpclient.NameValuePair;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.httpclient.params.HttpMethodParams;
+import org.apache.commons.io.FileUtils;
+import org.json.JSONObject;
+
 import edu.uci.ics.asterix.api.common.AsterixHyracksIntegrationUtil;
 import edu.uci.ics.asterix.common.config.GlobalConfig;
 import edu.uci.ics.asterix.test.aql.TestsUtils;
 import edu.uci.ics.asterix.testframework.context.TestCaseContext;
 
+import edu.uci.ics.asterix.external.dataset.adapter.FileSystemBasedAdapter;
+import edu.uci.ics.asterix.external.util.IdentitiyResolverFactory;
+import edu.uci.ics.asterix.testframework.context.TestFileContext;
+import edu.uci.ics.asterix.testframework.xml.TestCase.CompilationUnit;
+
+
 /**
  * Executes the Metadata tests.
  */
@@ -46,37 +65,6 @@
     private static final String TEST_CONFIG_FILE_NAME = "test.properties";
     private static final String WEB_SERVER_PORT = "19002";
 
-    public MetadataTest(TestCaseContext tcCtx) {
-        this.tcCtx = tcCtx;
-    }
-
-    @Test
-    public void test() throws Exception {
-        // TODO(madhusudancs): Uncomment
-        /*
-        List<CompilationUnit> cUnits = tcCtx.getTestCase().getCompilationUnit();
-        for (CompilationUnit cUnit : cUnits) {
-            File testFile = tcCtx.getTestFile(cUnit);
-            File expectedResultFile = tcCtx.getExpectedResultFile(cUnit);
-            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();
-            try {
-                // Khurram
-                //TestsUtils.runScriptAndCompareWithResult(AsterixHyracksIntegrationUtil.getHyracksClientConnection(),
-                        // testFile, new PrintWriter(System.err), expectedResultFile, actualFile);
-            } catch (Exception e) {
-                LOGGER.severe("Test \"" + testFile + "\" FAILED!");
-                e.printStackTrace();
-                if (cUnit.getExpectedError().isEmpty()) {
-                    throw new Exception("Test \"" + testFile + "\" FAILED!", e);
-                }
-            }
-        }*/
-    }
-
     @BeforeClass
     public static void setUp() throws Exception {
         System.setProperty(GlobalConfig.CONFIG_FILE_PROPERTY, TEST_CONFIG_FILE_NAME);
@@ -125,4 +113,187 @@
         return testArgs;
     }
 
+    public MetadataTest(TestCaseContext tcCtx) {
+        this.tcCtx = tcCtx;
+    }
+
+    // Method that reads a DDL/Update/Query File
+    // and returns the contents as a string
+    // This string is later passed to REST API for execution.
+    public String readTestFile(File testFile) throws Exception {
+        BufferedReader reader = new BufferedReader(new FileReader(testFile));
+        String line = null;
+        StringBuilder stringBuilder = new StringBuilder();
+        String ls = System.getProperty("line.separator");
+
+        while ((line = reader.readLine()) != null) {
+            stringBuilder.append(line);
+            stringBuilder.append(ls);
+        }
+
+        return stringBuilder.toString();
+    }
+
+    // To execute DDL and Update statements
+    // create type statement
+    // create dataset statement
+    // create index statement
+    // create dataverse statement
+    // create function statement
+    public void executeDDL(String str) throws Exception {
+        final String url = "http://localhost:19101/ddl";
+
+        // Create an instance of HttpClient.
+        HttpClient client = new HttpClient();
+
+        // Create a method instance.
+        GetMethod method = new GetMethod(url);
+
+        method.setQueryString(new NameValuePair[] { new NameValuePair("ddl", str) });
+
+        // Provide custom retry handler is necessary
+        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
+
+        // Execute the method.
+        int statusCode = client.executeMethod(method);
+
+        // Read the response body as String.
+        String responseBody = method.getResponseBodyAsString();
+
+        System.out.println("DDL: " + responseBody);
+
+        // Check if the method was executed successfully.
+        if (statusCode != HttpStatus.SC_OK) {
+            System.err.println("Method failed: " + method.getStatusLine());
+        }
+    }
+
+   // To execute Update statements
+    // Insert and Delete statements are executed here
+    public void executeUpdate(String str) throws Exception {
+        final String url = "http://localhost:19101/update";
+
+        // Create an instance of HttpClient.
+        HttpClient client = new HttpClient();
+
+        // Create a method instance.
+        GetMethod method = new GetMethod(url);
+
+        method.setQueryString(new NameValuePair[] { new NameValuePair("statements", str) });
+
+        // Provide custom retry handler is necessary
+        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
+
+        // Execute the method.
+        int statusCode = client.executeMethod(method);
+
+        // Read the response body as String.
+        String responseBody = method.getResponseBodyAsString();
+
+        System.out.println("Update: " + responseBody);
+
+        // Check if the method was executed successfully.
+        if (statusCode != HttpStatus.SC_OK) {
+            System.err.println("Method failed: " + method.getStatusLine());
+        }
+    }
+ 
+   // Executes Query and returns results as JSONArray
+    public JSONObject executeQuery(String str) throws Exception {
+
+        final String url = "http://localhost:19101/query";
+
+        // Create an instance of HttpClient.
+        HttpClient client = new HttpClient();
+
+        // Create a method instance.
+        GetMethod method = new GetMethod(url);
+
+        method.setQueryString(new NameValuePair[] { new NameValuePair("query", str) });
+
+        // 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());
+            }
+
+            // Read the response body as String.
+            String responseBody = method.getResponseBodyAsString();
+
+            System.out.println(responseBody);
+
+            result = new JSONObject(responseBody);
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
+            e.printStackTrace();
+        }
+        return result;
+    }
+ 
+   @Test
+    public void test() throws Exception {
+        List<TestFileContext> testFileCtxs;
+        List<TestFileContext> expectedResultFileCtxs;
+
+        File testFile;
+        File expectedResultFile;
+        String statement;
+
+        int queryCount = 0;
+        JSONObject result;
+
+        List<CompilationUnit> cUnits = tcCtx.getTestCase().getCompilationUnit();
+        for (CompilationUnit cUnit : cUnits) {
+            testFileCtxs = tcCtx.getTestFiles(cUnit);
+            expectedResultFileCtxs = tcCtx.getExpectedResultFiles(cUnit);
+
+            for (TestFileContext ctx : testFileCtxs) {
+                testFile = ctx.getFile();
+                statement = readTestFile(testFile);
+                try {
+                    switch (ctx.getType()) {
+                        case "ddl":
+                            executeDDL(statement);
+                            break;
+                        case "update":
+                            executeUpdate(statement);
+                            break;
+                        case "query":
+                            result = executeQuery(statement);
+                            if (!cUnit.getExpectedError().isEmpty()) {
+                                if (!result.has("error")) {
+                                    throw new Exception("Test \"" + testFile + "\" FAILED!");
+                                }
+                            } else {
+                                expectedResultFile = expectedResultFileCtxs.get(queryCount).getFile();
+                                TestsUtils
+                                        .runScriptAndCompareWithResult(
+                                                AsterixHyracksIntegrationUtil.getHyracksClientConnection(), testFile,
+                                                new PrintWriter(System.err), expectedResultFile,
+                                                result.getJSONArray("results"));
+                            }
+                            queryCount++;
+                            break;
+                       default:
+                            throw new IllegalArgumentException("No statements of type " + ctx.getType());
+                    }
+                } catch (Exception e) {
+                    LOGGER.severe("Test \"" + testFile + "\" FAILED!");
+                    e.printStackTrace();
+                    if (cUnit.getExpectedError().isEmpty()) {
+                        throw new Exception("Test \"" + testFile + "\" FAILED!", e);
+                    }
+                }
+            }
+        }
+    }
+     
 }
diff --git a/asterix-app/src/test/resources/metadata/testsuite.xml b/asterix-app/src/test/resources/metadata/testsuite.xml
index 6494cbf..d4fb915 100644
--- a/asterix-app/src/test/resources/metadata/testsuite.xml
+++ b/asterix-app/src/test/resources/metadata/testsuite.xml
@@ -2,183 +2,213 @@
   <test-group name="basic">
     <test-case FilePath="basic">
       <compilation-unit name="meta01">
-        <output-file compare="Text">meta01.adm</output-file>
+        <output-dir compare="Text">meta01</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="basic">
       <compilation-unit name="meta02">
-        <output-file compare="Text">meta02.adm</output-file>
+        <output-dir compare="Text">meta02</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="basic">
       <compilation-unit name="meta03">
-        <output-file compare="Text">meta03.adm</output-file>
+        <output-dir compare="Text">meta03</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="basic">
       <compilation-unit name="meta04">
-        <output-file compare="Text">meta04.adm</output-file>
+        <output-dir compare="Text">meta04</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="basic">
       <compilation-unit name="meta05">
-        <output-file compare="Text">meta05.adm</output-file>
+        <output-dir compare="Text">meta05</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="basic">
       <compilation-unit name="meta06">
-        <output-file compare="Text">meta06.adm</output-file>
+        <output-dir compare="Text">meta06</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="basic">
       <compilation-unit name="meta07">
-        <output-file compare="Text">meta07.adm</output-file>
+        <output-dir compare="Text">meta07</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="basic">
       <compilation-unit name="meta08">
-        <output-file compare="Text">meta08.adm</output-file>
+        <output-dir compare="Text">meta08</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="basic">
       <compilation-unit name="meta09">
-        <output-file compare="Text">meta09.adm</output-file>
+        <output-dir compare="Text">meta09</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="basic">
       <compilation-unit name="meta10">
-        <output-file compare="Text">meta10.adm</output-file>
+        <output-dir compare="Text">meta10</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="basic">
       <compilation-unit name="meta11">
-        <output-file compare="Text">meta11.adm</output-file>
+        <output-dir compare="Text">meta11</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="basic">
       <compilation-unit name="meta12">
-        <output-file compare="Text">meta12.adm</output-file>
+        <output-dir compare="Text">meta12</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="basic">
       <compilation-unit name="meta13">
-        <output-file compare="Text">meta13.adm</output-file>
+        <output-dir compare="Text">meta13</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="basic">
       <compilation-unit name="meta14">
-        <output-file compare="Text">meta14.adm</output-file>
+        <output-dir compare="Text">meta14</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="basic">
       <compilation-unit name="meta15">
-        <output-file compare="Text">meta15.adm</output-file>
+        <output-dir compare="Text">meta15</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="basic">
       <compilation-unit name="meta16">
-        <output-file compare="Text">meta16.adm</output-file>
+        <output-dir compare="Text">meta16</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="basic">
       <compilation-unit name="meta17">
-        <output-file compare="Text">meta17.adm</output-file>
+        <output-dir compare="Text">meta17</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="basic">
       <compilation-unit name="meta18">
-        <output-file compare="Text">meta18.adm</output-file>
+        <output-dir compare="Text">meta18</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="basic">
       <compilation-unit name="meta19">
-        <output-file compare="Text">meta19.adm</output-file>
+        <output-dir compare="Text">meta19</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="basic">
       <compilation-unit name="meta20">
-        <output-file compare="Text">meta20.adm</output-file>
+        <output-dir compare="Text">meta20</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="basic">
       <compilation-unit name="meta21">
-        <output-file compare="Text">meta21.adm</output-file>
+        <output-dir compare="Text">meta21</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="basic">
+      <compilation-unit name="metadata_dataset">
+        <output-dir compare="Text">metadata_dataset</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="basic">
+      <compilation-unit name="metadata_dataverse">
+        <output-dir compare="Text">metadata_dataverse</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="basic">
+      <compilation-unit name="metadata_datatype">
+        <output-dir compare="Text">metadata_datatype</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="basic">
+      <compilation-unit name="metadata_index">
+        <output-dir compare="Text">metadata_index</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="basic">
+      <compilation-unit name="metadata_node">
+        <output-dir compare="Text">metadata_node</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="basic">
+      <compilation-unit name="metadata_nodegroup">
+        <output-dir compare="Text">metadata_nodegroup</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="basic">
       <compilation-unit name="issue_251_dataset_hint_2">
-        <output-file compare="Text">issue_251_dataset_hint_2.adm</output-file>
+        <output-dir compare="Text">issue_251_dataset_hint_2</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="basic">
       <compilation-unit name="issue_251_dataset_hint_3">
-        <output-file compare="Text">issue_251_dataset_hint_3.adm</output-file>
+        <output-dir compare="Text">issue_251_dataset_hint_3</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="basic">
       <compilation-unit name="issue_251_dataset_hint_4">
-        <output-file compare="Text">issue_251_dataset_hint_4.adm</output-file>
+        <output-dir compare="Text">issue_251_dataset_hint_4</output-dir>
       </compilation-unit>
     </test-case>
   </test-group>
   <test-group name="exception">
     <test-case FilePath="exception">
       <compilation-unit name="issue_239_drop_system_dataset_1">
-        <output-file compare="Text">none.adm</output-file>
+        <output-dir compare="Text">none</output-dir>
         <expected-error>MetadataException</expected-error>
       </compilation-unit>
     </test-case>
     <test-case FilePath="exception">
       <compilation-unit name="issue_239_drop_system_dataset_2">
-        <output-file compare="Text">none.adm</output-file>
+        <output-dir compare="Text">none</output-dir>
         <expected-error>MetadataException</expected-error>
       </compilation-unit>
     </test-case>
     <test-case FilePath="exception">
       <compilation-unit name="issue_239_drop_system_dataset_3">
-        <output-file compare="Text">none.adm</output-file>
+        <output-dir compare="Text">none</output-dir>
         <expected-error>MetadataException</expected-error>
       </compilation-unit>
     </test-case>
     <test-case FilePath="exception">
       <compilation-unit name="issue_239_drop_system_dataset_4">
-        <output-file compare="Text">none.adm</output-file>
+        <output-dir compare="Text">none</output-dir>
         <expected-error>MetadataException</expected-error>
       </compilation-unit>
     </test-case>
     <test-case FilePath="exception">
       <compilation-unit name="issue_239_drop_system_dataset_5">
-        <output-file compare="Text">none.adm</output-file>
+        <output-dir compare="Text">none</output-dir>
         <expected-error>MetadataException</expected-error>
       </compilation-unit>
     </test-case>
     <test-case FilePath="exception">
       <compilation-unit name="issue_239_drop_system_dataset_6">
-        <output-file compare="Text">none.adm</output-file>
+        <output-dir compare="Text">none</output-dir>
         <expected-error>MetadataException</expected-error>
       </compilation-unit>
     </test-case>
     <test-case FilePath="exception">
       <compilation-unit name="issue_239_drop_system_dataset_7">
-        <output-file compare="Text">none.adm</output-file>
+        <output-dir compare="Text">none</output-dir>
         <expected-error>MetadataException</expected-error>
       </compilation-unit>
     </test-case>
     <test-case FilePath="exception">
       <compilation-unit name="issue_239_drop_system_dataset_8">
-        <output-file compare="Text">none.adm</output-file>
+        <output-dir compare="Text">none</output-dir>
         <expected-error>MetadataException</expected-error>
       </compilation-unit>
     </test-case>
     <test-case FilePath="exception">
       <compilation-unit name="issue_251_dataset_hint_error_1">
-        <output-file compare="Text">none.adm</output-file>
+        <output-dir compare="Text">none</output-dir>
         <expected-error>AsterixException</expected-error>
       </compilation-unit>
     </test-case>
     <test-case FilePath="exception">
       <compilation-unit name="issue_251_dataset_hint_error_2">
-        <output-file compare="Text">none.adm</output-file>
+        <output-dir compare="Text">none</output-dir>
         <expected-error>AsterixException</expected-error>
       </compilation-unit>
     </test-case>
@@ -186,20 +216,20 @@
   <test-group name="transaction">
     <test-case FilePath="transaction">
       <compilation-unit name="failure_previous_success">
-        <output-file compare="Text">failure_previous_success.adm</output-file>
+        <output-dir compare="Text">failure_previous_success</output-dir>
         <expected-error>MetadataException</expected-error>
       </compilation-unit>
       <compilation-unit name="verify_failure_previous_success">
-        <output-file compare="Text">verify_failure_previous_success.adm</output-file>
+        <output-dir compare="Text">verify_failure_previous_success</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="transaction">
       <compilation-unit name="failure_subsequent_no_execution">
-        <output-file compare="Text">failure_subsequent_no_execution.adm</output-file>
+        <output-dir compare="Text">failure_subsequent_no_execution</output-dir>
         <expected-error>MetadataException</expected-error>
       </compilation-unit>
       <compilation-unit name="verify_failure_subsequent_no_execution">
-        <output-file compare="Text">verify_failure_subsequent_no_execution.adm</output-file>
+        <output-dir compare="Text">verify_failure_subsequent_no_execution</output-dir>
       </compilation-unit>
     </test-case>
   </test-group>