dsmiley commented on code in PR #4042:
URL: https://github.com/apache/solr/pull/4042#discussion_r2680032910


##########
solr/test-framework/src/java/org/apache/solr/SolrXpathTestCase.java:
##########
@@ -0,0 +1,96 @@
+package org.apache.solr;
+
+import org.apache.solr.client.solrj.SolrClient;
+import org.apache.solr.client.solrj.request.QueryRequest;
+import org.apache.solr.client.solrj.response.InputStreamResponseParser;
+import org.apache.solr.client.solrj.response.QueryResponse;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.request.SolrQueryRequest;
+import org.apache.solr.util.BaseTestHarness;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import javax.xml.xpath.XPathExpressionException;
+import java.io.InputStream;
+import java.lang.invoke.MethodHandles;
+import java.nio.charset.StandardCharsets;
+
+public class SolrXpathTestCase extends SolrTestCase {
+
+  private static final Logger log = 
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+  /**
+   * Executes a query using SolrClient and validates the XML response against 
XPath expressions.
+   * This provides a similar interface to assertQ() in SolrTestCaseJ4 but 
works with SolrClient.
+   *
+   * @param req the query parameters
+   * @param tests XPath expressions to validate against the response
+   *
+   * @see SolrTestCaseJ4#assertQ(String, SolrQueryRequest, String...)
+   */
+  public void assertQ(QueryRequest req, String... tests) {
+    try {
+
+      // Process request and extract raw response
+      QueryResponse rsp = req.process(getSolrClient());
+      NamedList<Object> rawResponse = rsp.getResponse();
+      InputStream stream = (InputStream) rawResponse.get("stream");
+      String response = new String(stream.readAllBytes(), 
StandardCharsets.UTF_8);
+
+      String results = BaseTestHarness.validateXPath(response, tests);
+
+      if (results != null) {
+        String msg =
+            "REQUEST FAILED: xpath="
+                + results
+                + "\n\txml response was: "
+                + response
+                + "\n\trequest was:"
+                + req.getQueryParams();
+
+        fail(msg);
+      }
+    } catch (XPathExpressionException e1) {
+      throw new RuntimeException("XPath is invalid", e1);
+    } catch (Throwable e3) {
+      log.error("REQUEST FAILED: {}", req.getParams(), e3);
+      throw new RuntimeException("Exception during query", e3);
+    }
+  }
+
+  /**
+   * Generates a QueryRequest
+   *
+   * @see SolrTestCaseJ4#req(String...)
+   */
+  public static QueryRequest req(String... q) {
+    ModifiableSolrParams params = new ModifiableSolrParams();
+
+    if (q.length == 1) {
+      params.set("q", q);
+    }
+    if (q.length % 2 != 0) {
+      throw new RuntimeException(
+          "The length of the string array (query arguments) needs to be even");
+    }
+    for (int i = 0; i < q.length; i += 2) {
+      params.set(q[i], q[i + 1]);
+    }
+
+    params.set("wt", "xml");
+    params.set("indent", params.get("indent", "off"));

Review Comment:
   `wt` should *not* be be set since it's the job of the ResponseParser to 
declare it's `wt`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to