This is an automated email from the ASF dual-hosted git repository.
mdrob pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/solr.git
The following commit(s) were added to refs/heads/main by this push:
new 626f2c7 Refactoring RestTestBase and JSONTestUtil (#641)
626f2c7 is described below
commit 626f2c7b05e30daef9072c9a03c0c8b80dba3582
Author: Mike Drob <[email protected]>
AuthorDate: Sat Feb 19 20:42:52 2022 -0600
Refactoring RestTestBase and JSONTestUtil (#641)
Pull duplicate setup code into common methods
Narrow thrown exception type declarations
Fix a few spelling errors
Have test results lead to fail() instead of exception
---
.../test/org/apache/solr/ltr/TestRerankBase.java | 24 +-
.../apache/solr/ltr/feature/TestValueFeature.java | 22 +-
.../transform/TestInterleavingTransformer.java | 11 +-
.../org/apache/solr/common/params/SolrParams.java | 25 +-
.../java/org/apache/solr/common/util/StrUtils.java | 3 +-
.../src/java/org/apache/solr/JSONTestUtil.java | 23 +-
.../java/org/apache/solr/util/RestTestBase.java | 341 +++++++--------------
.../java/org/apache/solr/util/RestTestHarness.java | 6 +-
8 files changed, 163 insertions(+), 292 deletions(-)
diff --git a/solr/modules/ltr/src/test/org/apache/solr/ltr/TestRerankBase.java
b/solr/modules/ltr/src/test/org/apache/solr/ltr/TestRerankBase.java
index b02c4c5..4fb042b 100644
--- a/solr/modules/ltr/src/test/org/apache/solr/ltr/TestRerankBase.java
+++ b/solr/modules/ltr/src/test/org/apache/solr/ltr/TestRerankBase.java
@@ -17,6 +17,7 @@
package org.apache.solr.ltr;
import java.io.File;
+import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.net.URL;
import java.nio.file.Files;
@@ -258,20 +259,31 @@ public class TestRerankBase extends RestTestBase {
return sb.toString();
}
+ /** Load a feature from the test fstore and verify that it succeeded */
protected static void loadFeature(String name, String type, String params)
- throws Exception {
- final String feature = getFeatureInJson(name, type, "test", params);
- log.info("loading feauture \n{} ", feature);
- assertJPut(ManagedFeatureStore.REST_END_POINT, feature,
- "/responseHeader/status==0");
+ throws IOException {
+ loadFeature(name, type, "test", params, 0);
+ }
+
+ /** Load a feature and expect a given status code (i.e. testing for failure)
*/
+ protected static void loadFeature(String name, String type, String params,
int responseCode)
+ throws IOException {
+ loadFeature(name, type, "test", params, responseCode);
}
+ /** Load a feature from a custom fstore and verify that it succeeded */
protected static void loadFeature(String name, String type, String fstore,
String params) throws Exception {
+ loadFeature(name, type, fstore, params, 0);
+ }
+
+ /** Load a feature from a custom fstore and expect a given status code (i.e.
testing for failure) */
+ protected static void loadFeature(String name, String type, String fstore,
+ String params, int responseCode) throws
IOException {
final String feature = getFeatureInJson(name, type, fstore, params);
log.info("loading feauture \n{} ", feature);
assertJPut(ManagedFeatureStore.REST_END_POINT, feature,
- "/responseHeader/status==0");
+ "/responseHeader/status==" + responseCode);
}
protected static void loadModel(String name, String type, String[] features,
diff --git
a/solr/modules/ltr/src/test/org/apache/solr/ltr/feature/TestValueFeature.java
b/solr/modules/ltr/src/test/org/apache/solr/ltr/feature/TestValueFeature.java
index 8a3b014..9bfdcdc 100644
---
a/solr/modules/ltr/src/test/org/apache/solr/ltr/feature/TestValueFeature.java
+++
b/solr/modules/ltr/src/test/org/apache/solr/ltr/feature/TestValueFeature.java
@@ -48,23 +48,11 @@ public class TestValueFeature extends TestRerankBase {
}
@Test
- public void testValueFeatureWithEmptyValue() throws Exception {
- final RuntimeException expectedException =
- new RuntimeException("mismatch: '0'!='500' @ responseHeader/status");
- RuntimeException e = expectThrows(RuntimeException.class, () -> {
- loadFeature("c2", ValueFeature.class.getName(), "{\"value\":\"\"}");
- });
- assertEquals(expectedException.toString(), e.toString());
- }
-
- @Test
- public void testValueFeatureWithWhitespaceValue() throws Exception {
- final RuntimeException expectedException =
- new RuntimeException("mismatch: '0'!='500' @ responseHeader/status");
- RuntimeException e = expectThrows(RuntimeException.class, () -> {
- loadFeature("c2", ValueFeature.class.getName(), "{\"value\":\" \"}");
- });
- assertEquals(expectedException.toString(), e.toString());
+ public void testValueFeaturesWithBadValues() throws Exception {
+ // Empty Value
+ loadFeature("c2", ValueFeature.class.getName(), "{\"value\":\"\"}", 500);
+ // Whitespace Value
+ loadFeature("c2", ValueFeature.class.getName(), "{\"value\":\" \"}", 500);
}
@Test
diff --git
a/solr/modules/ltr/src/test/org/apache/solr/ltr/response/transform/TestInterleavingTransformer.java
b/solr/modules/ltr/src/test/org/apache/solr/ltr/response/transform/TestInterleavingTransformer.java
index 4f048d5..f4601a7 100644
---
a/solr/modules/ltr/src/test/org/apache/solr/ltr/response/transform/TestInterleavingTransformer.java
+++
b/solr/modules/ltr/src/test/org/apache/solr/ltr/response/transform/TestInterleavingTransformer.java
@@ -261,17 +261,8 @@ public class TestInterleavingTransformer extends
TestRerankBase {
int[] nullFeatureVectorIndexes = new int[]{1, 2, 4};
for (int index : nullFeatureVectorIndexes) {
TeamDraftInterleaving.setRANDOM(new Random(10101010));
- String[] nullFeatureVectorTests = new String[1];
- try {
- nullFeatureVectorTests[0] = "/response/docs/[" + index +
"]/features==";
- assertJQ("/query" + query.toQueryString(), nullFeatureVectorTests);
- } catch (Exception e) {
- assertEquals("Path not found: /response/docs/[" + index +
"]/features", e.getMessage());
- continue;
- }
-
+ assertJQ("/query" + query.toQueryString(), "!/response/docs/[" + index +
"]/features==");
}
-
}
}
diff --git a/solr/solrj/src/java/org/apache/solr/common/params/SolrParams.java
b/solr/solrj/src/java/org/apache/solr/common/params/SolrParams.java
index bea4756..9373308 100644
--- a/solr/solrj/src/java/org/apache/solr/common/params/SolrParams.java
+++ b/solr/solrj/src/java/org/apache/solr/common/params/SolrParams.java
@@ -614,23 +614,18 @@ public abstract class SolrParams implements Serializable,
MapWriter, Iterable<Ma
@Override
public String toString() {
final StringBuilder sb = new StringBuilder(128);
- try {
- boolean first=true;
- for (final Iterator<String> it = getParameterNamesIterator();
it.hasNext();) {
- final String name = it.next();
- for (String val : getParams(name)) {
- if (!first) sb.append('&');
- first=false;
- StrUtils.partialURLEncodeVal(sb, name);
- sb.append('=');
- StrUtils.partialURLEncodeVal(sb, val);
- }
+ boolean first = true;
+ for (final Iterator<String> it = getParameterNamesIterator();
it.hasNext();) {
+ final String name = it.next();
+ for (String val : getParams(name)) {
+ if (!first) sb.append('&');
+ first = false;
+ StrUtils.partialURLEncodeVal(sb, name);
+ sb.append('=');
+ StrUtils.partialURLEncodeVal(sb, val);
}
- return sb.toString();
- } catch (IOException e) {
- // impossible!
- throw new AssertionError(e);
}
+ return sb.toString();
}
}
diff --git a/solr/solrj/src/java/org/apache/solr/common/util/StrUtils.java
b/solr/solrj/src/java/org/apache/solr/common/util/StrUtils.java
index 09d4095..a2c5c50 100644
--- a/solr/solrj/src/java/org/apache/solr/common/util/StrUtils.java
+++ b/solr/solrj/src/java/org/apache/solr/common/util/StrUtils.java
@@ -16,7 +16,6 @@
*/
package org.apache.solr.common.util;
-import java.io.IOException;
import java.nio.CharBuffer;
import java.text.MessageFormat;
import java.util.ArrayList;
@@ -309,7 +308,7 @@ public class StrUtils {
* Characters with a numeric value less than 32 are encoded.
* &,=,%,+,space are encoded.
*/
- public static void partialURLEncodeVal(Appendable dest, String val) throws
IOException {
+ public static void partialURLEncodeVal(StringBuilder dest, String val) {
for (int i = 0; i < val.length(); i++) {
char ch = val.charAt(i);
if (ch < 32) {
diff --git a/solr/test-framework/src/java/org/apache/solr/JSONTestUtil.java
b/solr/test-framework/src/java/org/apache/solr/JSONTestUtil.java
index b1acca8..c9449cf 100644
--- a/solr/test-framework/src/java/org/apache/solr/JSONTestUtil.java
+++ b/solr/test-framework/src/java/org/apache/solr/JSONTestUtil.java
@@ -39,7 +39,7 @@ public class JSONTestUtil {
* @see #DEFAULT_DELTA
* @see #match(String,String,double)
*/
- public static String match(String input, String pathAndExpected) throws
Exception {
+ public static String match(String input, String pathAndExpected) throws
IOException {
return match(input, pathAndExpected, DEFAULT_DELTA);
}
@@ -48,7 +48,7 @@ public class JSONTestUtil {
* @see #DEFAULT_DELTA
* @see #match(String,String,String,double)
*/
- public static String match(String path, String input, String expected)
throws Exception {
+ public static String match(String path, String input, String expected)
throws IOException {
return match(path, input, expected, DEFAULT_DELTA);
}
@@ -57,16 +57,16 @@ public class JSONTestUtil {
* @see #DEFAULT_DELTA
* @see #matchObj(String,Object,Object,double)
*/
- public static String matchObj(String path, Object input, Object expected)
throws Exception {
+ public static String matchObj(String path, Object input, Object expected)
throws IOException {
return matchObj(path,input,expected, DEFAULT_DELTA);
}
/**
* @param input JSON Structure to parse and test against
* @param pathAndExpected JSON path expression + '==' + expected value
- * @param delta tollerance allowed in comparing float/double values
+ * @param delta tolerance allowed in comparing float/double values
*/
- public static String match(String input, String pathAndExpected, double
delta) throws Exception {
+ public static String match(String input, String pathAndExpected, double
delta) throws IOException {
int pos = pathAndExpected.indexOf("==");
String path = pos>=0 ? pathAndExpected.substring(0,pos) : null;
String expected = pos>=0 ? pathAndExpected.substring(pos+2) :
pathAndExpected;
@@ -76,9 +76,9 @@ public class JSONTestUtil {
/**
* @param input Object structure to parse and test against
* @param pathAndExpected JSON path expression + '==' + expected value
- * @param delta tollerance allowed in comparing float/double values
+ * @param delta tolerance allowed in comparing float/double values
*/
- public static String matchObj(Object input, String pathAndExpected, double
delta) throws Exception {
+ public static String matchObj(Object input, String pathAndExpected, double
delta) throws IOException {
int pos = pathAndExpected.indexOf("==");
String path = pos>=0 ? pathAndExpected.substring(0,pos) : null;
String expected = pos>=0 ? pathAndExpected.substring(pos+2) :
pathAndExpected;
@@ -90,9 +90,9 @@ public class JSONTestUtil {
* @param path JSON path expression
* @param input JSON Structure to parse and test against
* @param expected expected value of path
- * @param delta tollerance allowed in comparing float/double values
+ * @param delta tolerance allowed in comparing float/double values
*/
- public static String match(String path, String input, String expected,
double delta) throws Exception {
+ public static String match(String path, String input, String expected,
double delta) throws IOException {
Object inputObj = failRepeatedKeys ? new NoDupsObjectBuilder(new
JSONParser(input)).getVal() : ObjectBuilder.fromJSON(input);
Object expectObj = failRepeatedKeys ? new NoDupsObjectBuilder(new
JSONParser(expected)).getVal() : ObjectBuilder.fromJSON(expected);
return matchObj(path, inputObj, expectObj, delta);
@@ -117,14 +117,15 @@ public class JSONTestUtil {
* @param path JSON path expression
* @param input JSON Structure
* @param expected expected JSON Object
- * @param delta tollerance allowed in comparing float/double values
+ * @param delta tolerance allowed in comparing float/double values
+ * @return the error message from the match, or null if match was good
*/
public static String matchObj(String path, Object input, Object expected,
double delta) {
CollectionTester tester = new CollectionTester(input,delta);
boolean reversed = path.startsWith("!");
String positivePath = reversed ? path.substring(1) : path;
if (!tester.seek(positivePath) ^ reversed) {
- return "Path not found: " + path;
+ return "Path " + (reversed ? "" : "not ") + "found: " + path;
}
if (expected != null && (!tester.match(expected) ^ reversed)) {
return tester.err + " @ " + tester.getPath();
diff --git
a/solr/test-framework/src/java/org/apache/solr/util/RestTestBase.java
b/solr/test-framework/src/java/org/apache/solr/util/RestTestBase.java
index 1cd90be..f69aa3d 100644
--- a/solr/test-framework/src/java/org/apache/solr/util/RestTestBase.java
+++ b/solr/test-framework/src/java/org/apache/solr/util/RestTestBase.java
@@ -33,16 +33,17 @@ import java.lang.invoke.MethodHandles;
import java.util.Map;
import java.util.SortedMap;
-abstract public class RestTestBase extends SolrJettyTestBase {
+public abstract class RestTestBase extends SolrJettyTestBase {
private static final Logger log =
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
protected static RestTestHarness restTestHarness;
@AfterClass
public static void cleanUpHarness() throws IOException {
- if (restTestHarness != null) {
- restTestHarness.close();
+ RestTestHarness localHarness = restTestHarness;
+ if (localHarness != null) {
+ localHarness.close();
+ restTestHarness = null;
}
- restTestHarness = null;
}
public static void createJettyAndHarness
@@ -134,12 +135,8 @@ abstract public class RestTestBase extends
SolrJettyTestBase {
String results = TestHarness.validateXPath(response, tests);
if (null != results) {
- String msg = "REQUEST FAILED: xpath=" + results
- + "\n\txml response was: " + response
- + "\n\trequest was:" + request;
-
- log.error(msg);
- throw new RuntimeException(msg);
+ log.error("REQUEST FAILED: xpath={}\n\txml response was: {}\n\trequest
was:{}", results, response, request);
+ fail(results);
}
} catch (XPathExpressionException e1) {
@@ -155,32 +152,15 @@ abstract public class RestTestBase extends
SolrJettyTestBase {
*
* @param request a URL path with optional query params, e.g.
"/schema/fields?fl=id,_version_"
*/
- public static String JQ(String request) 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");
+ public static String JQ(String request) throws IOException {
+ request = setWtJsonAndIndent(request);
- String response;
- boolean failed=true;
try {
- response = restTestHarness.query(request);
- failed = false;
- } finally {
- if (failed) {
- log.error("REQUEST FAILED: {}", request);
- }
+ return restTestHarness.query(request);
+ } catch (IOException e) {
+ log.error("REQUEST FAILED: {}", request);
+ throw e;
}
-
- return response;
}
/**
@@ -206,64 +186,31 @@ abstract public class RestTestBase extends
SolrJettyTestBase {
* @param delta tolerance allowed in comparing float/double values
* @param tests JSON path expression + '==' + expected value
*/
- public static void assertJQ(String request, 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");
+ public static void assertJQ(String request, double delta, String... tests)
throws IOException {
+ request = setWtJsonAndIndent(request);
String response;
- boolean failed = true;
try {
response = restTestHarness.query(request);
- failed = false;
- } finally {
- if (failed) {
- log.error("REQUEST FAILED: {}", request);
- }
+ } catch (IOException e) {
+ log.error("REQUEST FAILED: {}", request);
+ throw e;
}
for (String test : tests) {
if (null == test || 0 == test.length()) continue;
- String testJSON = json(test);
-
- try {
- failed = true;
- String err = JSONTestUtil.match(response, testJSON, delta);
- failed = false;
- if (err != null) {
- log.error("query failed JSON validation. error={}"
- + "\n expected ={}\n response = {}\n request = {}\n"
- , err, testJSON, response, request);
- throw new RuntimeException(err);
- }
- } finally {
- if (failed) {
- log.error("JSON query validation threw an exception."
- +"\n expected ={}\n response = {}\n request = {}\n"
- , testJSON, response, request);
- }
- }
+ assertJsonMatches(request, response, json(test), delta);
}
}
-
-
+
/**
* 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 {
+ public static void assertJPut(String request, String content, String...
tests) throws IOException {
assertJPut(request, content, JSONTestUtil.DEFAULT_DELTA, tests);
}
@@ -283,52 +230,37 @@ abstract public class RestTestBase extends
SolrJettyTestBase {
* @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");
+ public static void assertJPut(String request, String content, double delta,
String... tests) throws IOException {
+ request = setWtJsonAndIndent(request);
String response;
- boolean failed = true;
try {
response = restTestHarness.put(request, content);
- failed = false;
- } finally {
- if (failed) {
- log.error("REQUEST FAILED: {}", request);
- }
+ } catch (IOException e) {
+ log.error("REQUEST FAILED: {}", request);
+ throw e;
}
for (String test : tests) {
if (null == test || 0 == test.length()) continue;
- String testJSON = json(test);
-
- try {
- failed = true;
- String err = JSONTestUtil.match(response, testJSON, delta);
- failed = false;
- if (err != null) {
- log.error("query failed JSON validation. error={}"
- + "\n expected ={}\n response = {}\n request = {}\n"
- ,err, testJSON, response, request);
- throw new RuntimeException(err);
- }
- } finally {
- if (failed) {
- log.error("JSON query validation threw an exception."
- + "\n expected ={}\n response = {}\n request = {}"
- , testJSON, response, request);
- }
+ assertJsonMatches(request, response, json(test), delta);
+ }
+ }
+
+ private static void assertJsonMatches(String request, String response,
String testJSON, double delta) throws IOException {
+ try {
+ String err = JSONTestUtil.match(response, testJSON, delta);
+ if (err != null) {
+ log.error("query failed JSON validation. error: {}"
+ + "\n expected: {}\n response: {}\n request: {}\n"
+ , err, testJSON, response, request);
+ fail(err);
}
+ } catch (IOException e) {
+ log.error("JSON query validation threw an exception."
+ +"\n expected: {}\n response: {}\n request: {}"
+ , testJSON, response, request);
+ throw e;
}
}
@@ -359,51 +291,19 @@ abstract public class RestTestBase extends
SolrJettyTestBase {
* @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");
+ request = setWtJsonAndIndent(request);
String response;
- boolean failed = true;
try {
response = restTestHarness.post(request, content);
- failed = false;
- } finally {
- if (failed) {
- log.error("REQUEST FAILED: {}", request);
- }
+ } catch (IOException e) {
+ log.error("REQUEST FAILED: {}", request);
+ throw e;
}
for (String test : tests) {
if (null == test || 0 == test.length()) continue;
- String testJSON = json(test);
-
- try {
- failed = true;
- String err = JSONTestUtil.match(response, testJSON, delta);
- failed = false;
- if (err != null) {
- log.error("query failed JSON validation. error={}"
- + "\n expected ={}\n response = {}\n request = {}\n"
- , err, testJSON, response, request);
- throw new RuntimeException(err);
- }
- } finally {
- if (failed) {
- log.error("JSON query validation threw an exception." +
- "\n expected ={}\n response = {}\n request = {}\n"
- ,testJSON, response, request);
- }
- }
+ assertJsonMatches(request, response, json(test), delta);
}
}
@@ -422,6 +322,28 @@ abstract public class RestTestBase extends
SolrJettyTestBase {
* response using the specified double delta tolerance.
*/
public static void assertJDelete(String request, double delta, String...
tests) throws Exception {
+ request = setWtJsonAndIndent(request);
+
+ String response;
+ try {
+ response = restTestHarness.delete(request);
+ } catch (IOException e) {
+ log.error("REQUEST FAILED: {}", request);
+ throw e;
+ }
+
+ for (String test : tests) {
+ if (null == test || 0 == test.length()) continue;
+ assertJsonMatches(request, response, json(test), delta);
+ }
+ }
+
+ /**
+ * Parse the request string and set wt=json&indent=on for json queries
+ * @param request the original request
+ * @return the new (possibly unmodified) request
+ */
+ private static String setWtJsonAndIndent(String request) {
int queryStartPos = request.indexOf('?');
String query;
String path;
@@ -434,45 +356,12 @@ abstract public class RestTestBase extends
SolrJettyTestBase {
}
query = setParam(query, "wt", "json");
request = path + '?' + setParam(query, "indent", "on");
-
- String response;
- boolean failed = true;
- try {
- response = restTestHarness.delete(request);
- failed = false;
- } finally {
- if (failed) {
- log.error("REQUEST FAILED: {}", request);
- }
- }
-
- for (String test : tests) {
- if (null == test || 0 == test.length()) continue;
- String testJSON = json(test);
-
- try {
- failed = true;
- String err = JSONTestUtil.match(response, testJSON, delta);
- failed = false;
- if (err != null) {
- log.error("query failed JSON validation. error={}\n expected ={}"
- + "\n response = {}\n request = {}"
- , err, testJSON, response, request);
- throw new RuntimeException(err);
- }
- } finally {
- if (failed) {
- log.error("JSON query validation threw an exception.\n expected
={}\n"
- + "\n response = {}\n request = {}"
- , testJSON, response, request
- );
- }
- }
- }
+ return request;
}
+
/**
- * Insures that the given param is included in the query with the given
value.
+ * Ensures that the given param is included in the query with the given
value.
*
* <ol>
* <li>If the param is already included with the given value, the request
is returned unchanged.</li>
@@ -492,54 +381,50 @@ abstract public class RestTestBase extends
SolrJettyTestBase {
if (null == valueToSet) {
valueToSet = "";
}
- try {
- StringBuilder builder = new StringBuilder();
- if (null == query || query.trim().isEmpty()) {
- // empty query -> return "paramToSet=valueToSet"
- builder.append(paramToSet);
- builder.append('=');
- StrUtils.partialURLEncodeVal(builder, valueToSet);
- return builder.toString();
- }
- MultiMapSolrParams requestParams =
SolrRequestParsers.parseQueryString(query);
- String[] values = requestParams.getParams(paramToSet);
- if (null == values) {
- // paramToSet isn't present in the request -> append
"¶mToSet=valueToSet"
- builder.append(query);
- builder.append('&');
- builder.append(paramToSet);
- builder.append('=');
- StrUtils.partialURLEncodeVal(builder, valueToSet);
- return builder.toString();
- }
- if (1 == values.length && valueToSet.equals(values[0])) {
- // paramToSet=valueToSet is already in the query - just return the
query as-is.
- return query;
- }
- // More than one value for paramToSet on the request, or paramToSet's
value is not valueToSet
- // -> rebuild the query
- boolean isFirst = true;
- for (Map.Entry<String,String[]> entry :
requestParams.getMap().entrySet()) {
- String key = entry.getKey();
- String[] valarr = entry.getValue();
-
- if ( ! key.equals(paramToSet)) {
- for (String val : valarr) {
- builder.append(isFirst ? "" : '&');
- isFirst = false;
- builder.append(key);
- builder.append('=');
- StrUtils.partialURLEncodeVal(builder, null == val ? "" : val);
- }
- }
- }
- builder.append(isFirst ? "" : '&');
+ StringBuilder builder = new StringBuilder();
+ if (null == query || query.trim().isEmpty()) {
+ // empty query -> return "paramToSet=valueToSet"
builder.append(paramToSet);
builder.append('=');
StrUtils.partialURLEncodeVal(builder, valueToSet);
return builder.toString();
- } catch (IOException e) {
- throw new RuntimeException(e);
}
+ MultiMapSolrParams requestParams =
SolrRequestParsers.parseQueryString(query);
+ String[] values = requestParams.getParams(paramToSet);
+ if (null == values) {
+ // paramToSet isn't present in the request -> append
"¶mToSet=valueToSet"
+ builder.append(query);
+ builder.append('&');
+ builder.append(paramToSet);
+ builder.append('=');
+ StrUtils.partialURLEncodeVal(builder, valueToSet);
+ return builder.toString();
+ }
+ if (1 == values.length && valueToSet.equals(values[0])) {
+ // paramToSet=valueToSet is already in the query - just return the query
as-is.
+ return query;
+ }
+ // More than one value for paramToSet on the request, or paramToSet's
value is not valueToSet
+ // -> rebuild the query
+ boolean isFirst = true;
+ for (Map.Entry<String,String[]> entry : requestParams.getMap().entrySet())
{
+ String key = entry.getKey();
+ String[] valarr = entry.getValue();
+
+ if ( ! key.equals(paramToSet)) {
+ for (String val : valarr) {
+ builder.append(isFirst ? "" : '&');
+ isFirst = false;
+ builder.append(key);
+ builder.append('=');
+ StrUtils.partialURLEncodeVal(builder, null == val ? "" : val);
+ }
+ }
+ }
+ builder.append(isFirst ? "" : '&');
+ builder.append(paramToSet);
+ builder.append('=');
+ StrUtils.partialURLEncodeVal(builder, valueToSet);
+ return builder.toString();
}
}
diff --git
a/solr/test-framework/src/java/org/apache/solr/util/RestTestHarness.java
b/solr/test-framework/src/java/org/apache/solr/util/RestTestHarness.java
index 2769d9b..21f0c11 100644
--- a/solr/test-framework/src/java/org/apache/solr/util/RestTestHarness.java
+++ b/solr/test-framework/src/java/org/apache/solr/util/RestTestHarness.java
@@ -101,13 +101,13 @@ public class RestTestHarness extends BaseTestHarness
implements Closeable {
*
* @param request the URL path and optional query params
* @return The response to the query
- * @exception Exception any exception in the response.
+ * @exception IOException any exception in the response.
*/
- public String query(String request) throws Exception {
+ public String query(String request) throws IOException {
return getResponse(new HttpGet(getBaseURL() + request));
}
- public String adminQuery(String request) throws Exception {
+ public String adminQuery(String request) throws IOException {
return getResponse(new HttpGet(getAdminURL() + request));
}