You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by sa...@apache.org on 2013/04/23 16:29:13 UTC

svn commit: r1470979 [5/5] - in /lucene/dev/branches/branch_4x: ./ solr/ solr/contrib/ solr/contrib/clustering/src/java/org/apache/solr/handler/clustering/carrot2/ solr/contrib/dataimporthandler-extras/src/test/org/apache/solr/handler/dataimport/ solr/...

Modified: lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/util/RestTestBase.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/util/RestTestBase.java?rev=1470979&r1=1470978&r2=1470979&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/util/RestTestBase.java (original)
+++ lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/util/RestTestBase.java Tue Apr 23 14:29:10 2013
@@ -229,7 +229,8 @@ abstract public class RestTestBase exten
 
     for (String test : tests) {
       if (null == test || 0 == test.length()) continue;
-      String testJSON = test.replace('\'', '"');
+      String testJSON = test.replaceAll("(?<!\\\\)\'", "\"");
+      testJSON = testJSON.replaceAll("\\\\\'", "'");
 
       try {
         failed = true;
@@ -255,6 +256,170 @@ abstract public class RestTestBase exten
     }
   }
 
+  
+  
+  /**
+   * Validates the response from a PUT request matches some JSON test expressions
+   * 
+   * @see org.apache.solr.JSONTestUtil#DEFAULT_DELTA
+   * @see #assertJQ(String,double,String...)
+   */
+  public static void assertJPut(String request, String content, String... tests) throws Exception {
+    assertJPut(request, content, JSONTestUtil.DEFAULT_DELTA, tests);
+  }
+
+
+  /**
+   * Validates the response from a PUT request matches some JSON test expressions
+   * and closes the query. The text expression is of the form path==JSON.
+   * To facilitate easy embedding in Java strings, the JSON can have double
+   * quotes replaced with single quotes.
+   * <p>
+   * Please use this with care: this makes it easy to match complete
+   * structures, but doing so can result in fragile tests if you are
+   * matching more than what you want to test.
+   * </p>
+   * @param request a URL path with optional query params, e.g. "/schema/fields?fl=id,_version_"
+   * @param content The content to include with the PUT request
+   * @param delta tolerance allowed in comparing float/double values
+   * @param tests JSON path expression + '==' + expected value
+   */
+  public static void assertJPut(String request, String content, double delta, String... tests) throws Exception {
+    int queryStartPos = request.indexOf('?');
+    String query;
+    String path;
+    if (-1 == queryStartPos) {
+      query = "";
+      path = request;
+    } else {
+      query = request.substring(queryStartPos + 1);
+      path = request.substring(0, queryStartPos);
+    }
+    query = setParam(query, "wt", "json");
+    request = path + '?' + setParam(query, "indent", "on");
+
+    String response;
+    boolean failed = true;
+    try {
+      response = restTestHarness.put(request, content);
+      failed = false;
+    } finally {
+      if (failed) {
+        log.error("REQUEST FAILED: " + request);
+      }
+    }
+
+    for (String test : tests) {
+      if (null == test || 0 == test.length()) continue;
+      String testJSON = test.replaceAll("(?<!\\\\)\'", "\"");
+      testJSON = testJSON.replaceAll("\\\\\'", "'");
+
+      try {
+        failed = true;
+        String err = JSONTestUtil.match(response, testJSON, delta);
+        failed = false;
+        if (err != null) {
+          log.error("query failed JSON validation. error=" + err +
+              "\n expected =" + testJSON +
+              "\n response = " + response +
+              "\n request = " + request + "\n"
+          );
+          throw new RuntimeException(err);
+        }
+      } finally {
+        if (failed) {
+          log.error("JSON query validation threw an exception." +
+              "\n expected =" + testJSON +
+              "\n response = " + response +
+              "\n request = " + request + "\n"
+          );
+        }
+      }
+    }
+  }
+
+  /**
+   * Validates the response from a POST request matches some JSON test expressions
+   *
+   * @see org.apache.solr.JSONTestUtil#DEFAULT_DELTA
+   * @see #assertJQ(String,double,String...)
+   */
+  public static void assertJPost(String request, String content, String... tests) throws Exception {
+    assertJPost(request, content, JSONTestUtil.DEFAULT_DELTA, tests);
+  }
+
+
+  /**
+   * Validates the response from a PUT request matches some JSON test expressions
+   * and closes the query. The text expression is of the form path==JSON.
+   * To facilitate easy embedding in Java strings, the JSON can have double
+   * quotes replaced with single quotes.
+   * <p>
+   * Please use this with care: this makes it easy to match complete
+   * structures, but doing so can result in fragile tests if you are
+   * matching more than what you want to test.
+   * </p>
+   * @param request a URL path with optional query params, e.g. "/schema/fields?fl=id,_version_"
+   * @param content The content to include with the PUT request
+   * @param delta tolerance allowed in comparing float/double values
+   * @param tests JSON path expression + '==' + expected value
+   */
+  public static void assertJPost(String request, String content, double delta, String... tests) throws Exception {
+    int queryStartPos = request.indexOf('?');
+    String query;
+    String path;
+    if (-1 == queryStartPos) {
+      query = "";
+      path = request;
+    } else {
+      query = request.substring(queryStartPos + 1);
+      path = request.substring(0, queryStartPos);
+    }
+    query = setParam(query, "wt", "json");
+    request = path + '?' + setParam(query, "indent", "on");
+
+    String response;
+    boolean failed = true;
+    try {
+      response = restTestHarness.post(request, content);
+      failed = false;
+    } finally {
+      if (failed) {
+        log.error("REQUEST FAILED: " + request);
+      }
+    }
+
+    for (String test : tests) {
+      if (null == test || 0 == test.length()) continue;
+      String testJSON = test.replaceAll("(?<!\\\\)\'", "\"");
+      testJSON = testJSON.replaceAll("\\\\\'", "'");
+
+      try {
+        failed = true;
+        String err = JSONTestUtil.match(response, testJSON, delta);
+        failed = false;
+        if (err != null) {
+          log.error("query failed JSON validation. error=" + err +
+              "\n expected =" + testJSON +
+              "\n response = " + response +
+              "\n request = " + request + "\n"
+          );
+          throw new RuntimeException(err);
+        }
+      } finally {
+        if (failed) {
+          log.error("JSON query validation threw an exception." +
+              "\n expected =" + testJSON +
+              "\n response = " + response +
+              "\n request = " + request + "\n"
+          );
+        }
+      }
+    }
+  }
+
+
+
   /**
    * Insures that the given param is included in the query with the given value.
    *

Modified: lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/util/RestTestHarness.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/util/RestTestHarness.java?rev=1470979&r1=1470978&r2=1470979&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/util/RestTestHarness.java (original)
+++ lucene/dev/branches/branch_4x/solr/test-framework/src/java/org/apache/solr/util/RestTestHarness.java Tue Apr 23 14:29:10 2013
@@ -17,12 +17,12 @@ package org.apache.solr.util;
  */
 
 import org.apache.commons.io.IOUtils;
-import org.eclipse.jetty.util.IO;
 
 import javax.xml.xpath.XPathExpressionException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
 import java.io.StringWriter;
 import java.net.HttpURLConnection;
 import java.net.URL;
@@ -43,20 +43,37 @@ public class RestTestHarness extends Bas
   }
   
   /**
-   * Validates a "query" response against an array of XPath test strings
+   * Validates an XML "query" response against an array of XPath test strings
    *
    * @param request the Query to process
    * @return null if all good, otherwise the first test that fails.
    * @exception Exception any exception in the response.
    * @exception java.io.IOException if there is a problem writing the XML
    */
-  public String validateQuery(String request, String... tests)
-      throws Exception {
+  public String validateQuery(String request, String... tests) throws Exception {
 
     String res = query(request);
     return validateXPath(res, tests);
   }
 
+
+  /**
+   * Validates an XML PUT response against an array of XPath test strings
+   *
+   * @param request the PUT request to process
+   * @param content the content to send with the PUT request
+   * @param tests the validating XPath tests
+   * @return null if all good, otherwise the first test that fails.
+   * @exception Exception any exception in the response.
+   * @exception java.io.IOException if there is a problem writing the XML
+   */
+  public String validatePut(String request, String content, String... tests) throws Exception {
+
+    String res = put(request, content);
+    return validateXPath(res, tests);
+  }
+
+
   /**
    * Processes a "query" using a URL path (with no context path) + optional query params,
    * e.g. "/schema/fields?indent=on"
@@ -84,7 +101,74 @@ public class RestTestHarness extends Bas
     return strWriter.toString();
   }
 
-  public String checkQueryStatus(String xml, String code) throws Exception {
+  /**
+   * Processes a PUT request using a URL path (with no context path) + optional query params,
+   * e.g. "/schema/fields/newfield", PUTs the given content, and returns the response content.
+   * 
+   * @param request The URL path and optional query params
+   * @param content The content to include with the PUT request
+   * @return The response to the PUT request
+   */
+  public String put(String request, String content) throws IOException {
+    URL url = new URL(getBaseURL() + request);
+    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
+    connection.setDoOutput(true);
+    connection.setRequestMethod("PUT");
+    OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
+    out.write(content);
+    out.close();
+    InputStream inputStream = null;
+    StringWriter stringWriter;
+    try {
+      try {
+        inputStream = connection.getInputStream();
+      } catch (IOException e) {
+        inputStream = connection.getErrorStream();
+      }
+      stringWriter = new StringWriter();
+      IOUtils.copy(new InputStreamReader(inputStream, "UTF-8"), stringWriter);
+    } finally {
+      IOUtils.closeQuietly(inputStream);
+    }
+    return stringWriter.toString();
+  }
+
+  /**
+   * Processes a POST request using a URL path (with no context path) + optional query params,
+   * e.g. "/schema/fields/newfield", PUTs the given content, and returns the response content.
+   *
+   * @param request The URL path and optional query params
+   * @param content The content to include with the POST request
+   * @return The response to the PUT request
+   */
+  public String post(String request, String content) throws IOException {
+    URL url = new URL(getBaseURL() + request);
+    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
+    connection.setDoOutput(true);
+    connection.setRequestMethod("POST");
+    connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
+
+    OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
+    out.write(content);
+    out.close();
+    InputStream inputStream = null;
+    StringWriter stringWriter;
+    try {
+      try {
+        inputStream = connection.getInputStream();
+      } catch (IOException e) {
+        inputStream = connection.getErrorStream();
+      }
+      stringWriter = new StringWriter();
+      IOUtils.copy(new InputStreamReader(inputStream, "UTF-8"), stringWriter);
+    } finally {
+      IOUtils.closeQuietly(inputStream);
+    }
+    return stringWriter.toString();
+  }
+
+
+  public String checkResponseStatus(String xml, String code) throws Exception {
     try {
       String response = query(xml);
       String valid = validateXPath(response, "//int[@name='status']="+code );
@@ -94,9 +178,10 @@ public class RestTestHarness extends Bas
     }
   }
 
+  
   @Override
   public void reload() throws Exception {
-    String xml = checkQueryStatus("/admin/cores?action=RELOAD", "0");
+    String xml = checkResponseStatus("/admin/cores?action=RELOAD", "0");
     if (null != xml) {
       throw new RuntimeException("RELOAD failed:\n" + xml);
     }
@@ -112,7 +197,7 @@ public class RestTestHarness extends Bas
   @Override
   public String update(String xml) {
     try {
-      return query("/update?stream.base=" + URLEncoder.encode(xml, "UTF-8"));
+      return query("/update?stream.body=" + URLEncoder.encode(xml, "UTF-8"));
     } catch (Exception e) {
       throw new RuntimeException(e);
     }