Skip to content

Commit 44eb5c5

Browse files
dsmileyclaude
andauthored
SOLR-18188: Tests, switch from Apache to Jetty HttpClient (#4266)
Switches many tests that were using Apache HttpClient directly to instead use Jetty HttpClient (or in some cases a plain SolrClient given we can do a lot with them). SimplePrinciple was made a record and used in places where an Apache variant was used. Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 6b21201 commit 44eb5c5

55 files changed

Lines changed: 1597 additions & 1997 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

solr/core/src/java/org/apache/solr/security/SimplePrincipal.java

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,12 @@
1818
package org.apache.solr.security;
1919

2020
import java.security.Principal;
21-
import java.util.Objects;
2221

2322
/** A basic name-only {@link Principal}. */
24-
public final class SimplePrincipal implements Principal {
25-
// TODO use a record. But javadoc tooling complained as it was confused (Java version issue?)
26-
private final String name;
27-
28-
public SimplePrincipal(String name) {
29-
this.name = name;
30-
}
23+
public record SimplePrincipal(String name) implements Principal {
3124

3225
@Override
3326
public String getName() {
3427
return name;
3528
}
36-
37-
@Override
38-
public boolean equals(Object obj) {
39-
if (obj == this) return true;
40-
if (obj == null || obj.getClass() != this.getClass()) return false;
41-
var that = (SimplePrincipal) obj;
42-
return Objects.equals(this.name, that.name);
43-
}
44-
45-
@Override
46-
public int hashCode() {
47-
return Objects.hash(name);
48-
}
49-
50-
@Override
51-
public String toString() {
52-
return "SimplePrincipal[" + "name=" + name + ']';
53-
}
5429
}

solr/core/src/test/org/apache/solr/cloud/AliasIntegrationTest.java

Lines changed: 67 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,16 @@
1919
import static org.apache.solr.common.cloud.ZkStateReader.ALIASES;
2020

2121
import java.io.IOException;
22+
import java.nio.charset.StandardCharsets;
2223
import java.util.List;
2324
import java.util.Locale;
2425
import java.util.Map;
2526
import java.util.concurrent.TimeUnit;
2627
import java.util.function.Consumer;
2728
import java.util.function.UnaryOperator;
28-
import org.apache.http.client.methods.CloseableHttpResponse;
29-
import org.apache.http.client.methods.HttpDelete;
30-
import org.apache.http.client.methods.HttpGet;
31-
import org.apache.http.client.methods.HttpPut;
32-
import org.apache.http.client.methods.HttpUriRequest;
33-
import org.apache.http.entity.ContentType;
34-
import org.apache.http.entity.StringEntity;
35-
import org.apache.http.impl.client.CloseableHttpClient;
36-
import org.apache.http.util.EntityUtils;
3729
import org.apache.solr.client.solrj.SolrClient;
3830
import org.apache.solr.client.solrj.SolrRequest;
3931
import org.apache.solr.client.solrj.SolrServerException;
40-
import org.apache.solr.client.solrj.apache.CloudLegacySolrClient;
4132
import org.apache.solr.client.solrj.cloud.SolrCloudManager;
4233
import org.apache.solr.client.solrj.impl.CloudSolrClient;
4334
import org.apache.solr.client.solrj.impl.ClusterStateProvider;
@@ -60,15 +51,17 @@
6051
import org.apache.solr.embedded.JettySolrRunner;
6152
import org.apache.solr.util.TimeOut;
6253
import org.apache.zookeeper.KeeperException;
54+
import org.eclipse.jetty.client.ContentResponse;
55+
import org.eclipse.jetty.client.HttpClient;
56+
import org.eclipse.jetty.client.StringRequestContent;
57+
import org.eclipse.jetty.http.HttpMethod;
6358
import org.junit.After;
6459
import org.junit.Before;
6560
import org.junit.BeforeClass;
6661
import org.junit.Test;
6762

6863
public class AliasIntegrationTest extends SolrCloudTestCase {
6964

70-
private CloseableHttpClient httpClient;
71-
7265
@BeforeClass
7366
public static void setupCluster() throws Exception {
7467
configureCluster(2).addConfig("conf", configset("cloud-minimal")).configure();
@@ -78,9 +71,6 @@ public static void setupCluster() throws Exception {
7871
@Override
7972
public void setUp() throws Exception {
8073
super.setUp();
81-
82-
httpClient =
83-
(CloseableHttpClient) ((CloudLegacySolrClient) cluster.getSolrClient()).getHttpClient();
8474
}
8575

8676
@After
@@ -238,49 +228,53 @@ public void testProperties() throws Exception {
238228
public void testModifyPropertiesV2() throws Exception {
239229
final String aliasName = getSaferTestName();
240230
ZkStateReader zkStateReader = createColectionsAndAlias(aliasName);
241-
final String baseUrl = cluster.getRandomJetty(random()).getBaseURLV2().toString();
231+
final JettySolrRunner runner = cluster.getRandomJetty(random());
232+
final String baseUrl = runner.getBaseURLV2().toString();
233+
final HttpClient httpClient = runner.getSolrClient().getHttpClient();
242234
String aliasApi = String.format(Locale.ENGLISH, "/aliases/%s/properties", aliasName);
243235

244-
HttpPut withoutBody = new HttpPut(baseUrl + aliasApi);
245-
assertEquals(400, httpClient.execute(withoutBody).getStatusLine().getStatusCode());
246-
247-
HttpPut update = new HttpPut(baseUrl + aliasApi);
248-
update.setEntity(
249-
new StringEntity(
250-
"{\n"
251-
+ " \"properties\":\n"
252-
+ " {\n"
253-
+ " \"foo\": \"baz\",\n"
254-
+ " \"bar\": \"bam\"\n"
255-
+ " }\n"
256-
+ "}",
257-
ContentType.APPLICATION_JSON));
258-
assertSuccess(update);
236+
assertEquals(
237+
400, httpClient.newRequest(baseUrl + aliasApi).method(HttpMethod.PUT).send().getStatus());
238+
239+
String body =
240+
"{\n"
241+
+ " \"properties\":\n"
242+
+ " {\n"
243+
+ " \"foo\": \"baz\",\n"
244+
+ " \"bar\": \"bam\"\n"
245+
+ " }\n"
246+
+ "}";
247+
assertSuccess(
248+
httpClient
249+
.newRequest(baseUrl + aliasApi)
250+
.method(HttpMethod.PUT)
251+
.body(new StringRequestContent("application/json", body, StandardCharsets.UTF_8))
252+
.send());
259253
checkFooAndBarMeta(aliasName, zkStateReader, "baz", "bam");
260254

261255
String aliasPropertyApi =
262256
String.format(Locale.ENGLISH, "/aliases/%s/properties/%s", aliasName, "foo");
263-
HttpPut updateByProperty = new HttpPut(baseUrl + aliasPropertyApi);
264-
updateByProperty.setEntity(
265-
new StringEntity("{ \"value\": \"zab\" }", ContentType.APPLICATION_JSON));
266-
assertSuccess(updateByProperty);
257+
assertSuccess(
258+
httpClient
259+
.newRequest(baseUrl + aliasPropertyApi)
260+
.method(HttpMethod.PUT)
261+
.body(
262+
new StringRequestContent(
263+
"application/json", "{ \"value\": \"zab\" }", StandardCharsets.UTF_8))
264+
.send());
267265
checkFooAndBarMeta(aliasName, zkStateReader, "zab", "bam");
268266

269-
HttpDelete deleteByProperty = new HttpDelete(baseUrl + aliasPropertyApi);
270-
assertSuccess(deleteByProperty);
267+
assertSuccess(
268+
httpClient.newRequest(baseUrl + aliasPropertyApi).method(HttpMethod.DELETE).send());
271269
checkFooAndBarMeta(aliasName, zkStateReader, null, "bam");
272270

273-
HttpPut deleteByEmptyValue = new HttpPut(baseUrl + aliasApi);
274-
deleteByEmptyValue.setEntity(
275-
new StringEntity(
276-
"{\n"
277-
+ " \"properties\":\n"
278-
+ " {\n"
279-
+ " \"bar\": \"\"\n"
280-
+ " }\n"
281-
+ "}",
282-
ContentType.APPLICATION_JSON));
283-
assertSuccess(deleteByEmptyValue);
271+
body = "{ \"properties\": { \"bar\": \"\" } }";
272+
assertSuccess(
273+
httpClient
274+
.newRequest(baseUrl + aliasApi)
275+
.method(HttpMethod.PUT)
276+
.body(new StringRequestContent("application/json", body, StandardCharsets.UTF_8))
277+
.send());
284278
checkFooAndBarMeta(aliasName, zkStateReader, null, null);
285279
}
286280

@@ -289,29 +283,29 @@ public void testModifyPropertiesV1() throws Exception {
289283
// note we don't use TZ in this test, thus it's UTC
290284
final String aliasName = getSaferTestName();
291285
ZkStateReader zkStateReader = createColectionsAndAlias(aliasName);
292-
final String baseUrl = cluster.getRandomJetty(random()).getBaseUrl().toString();
293-
HttpGet get =
294-
new HttpGet(
295-
baseUrl
296-
+ "/admin/collections?action=ALIASPROP"
297-
+ "&wt=xml"
298-
+ "&name="
299-
+ aliasName
300-
+ "&property.foo=baz"
301-
+ "&property.bar=bam");
302-
assertSuccess(get);
286+
final JettySolrRunner runner = cluster.getRandomJetty(random());
287+
final String baseUrl = runner.getBaseUrl().toString();
288+
final HttpClient httpClient = runner.getSolrClient().getHttpClient();
289+
String url =
290+
baseUrl
291+
+ "/admin/collections?action=ALIASPROP"
292+
+ "&wt=xml"
293+
+ "&name="
294+
+ aliasName
295+
+ "&property.foo=baz"
296+
+ "&property.bar=bam";
297+
assertSuccess(httpClient.GET(url));
303298
checkFooAndBarMeta(aliasName, zkStateReader, "baz", "bam");
304299

305-
HttpGet remove =
306-
new HttpGet(
307-
baseUrl
308-
+ "/admin/collections?action=ALIASPROP"
309-
+ "&wt=xml"
310-
+ "&name="
311-
+ aliasName
312-
+ "&property.foo="
313-
+ "&property.bar=bar");
314-
assertSuccess(remove);
300+
url =
301+
baseUrl
302+
+ "/admin/collections?action=ALIASPROP"
303+
+ "&wt=xml"
304+
+ "&name="
305+
+ aliasName
306+
+ "&property.foo="
307+
+ "&property.bar=bar";
308+
assertSuccess(httpClient.GET(url));
315309
checkFooAndBarMeta(aliasName, zkStateReader, null, "bar");
316310
}
317311

@@ -508,12 +502,10 @@ private ZkStateReader createColectionsAndAlias(String aliasName)
508502
return zkStateReader;
509503
}
510504

511-
private void assertSuccess(HttpUriRequest msg) throws IOException {
512-
try (CloseableHttpResponse response = httpClient.execute(msg)) {
513-
if (200 != response.getStatusLine().getStatusCode()) {
514-
System.err.println(EntityUtils.toString(response.getEntity()));
515-
fail("Unexpected status: " + response.getStatusLine());
516-
}
505+
private void assertSuccess(ContentResponse response) {
506+
if (200 != response.getStatus()) {
507+
System.err.println(response.getContentAsString());
508+
fail("Unexpected status: " + response.getStatus());
517509
}
518510
}
519511

solr/core/src/test/org/apache/solr/cloud/BalanceReplicasTest.java

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
package org.apache.solr.cloud;
1919

20-
import static java.nio.charset.StandardCharsets.UTF_8;
21-
2220
import java.io.IOException;
2321
import java.lang.invoke.MethodHandles;
2422
import java.util.ArrayList;
@@ -27,21 +25,16 @@
2725
import java.util.Map;
2826
import java.util.Set;
2927
import java.util.stream.Collectors;
30-
import org.apache.http.HttpEntity;
31-
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
32-
import org.apache.http.client.methods.HttpPost;
33-
import org.apache.http.entity.ByteArrayEntity;
34-
import org.apache.http.entity.ContentType;
35-
import org.apache.http.util.EntityUtils;
3628
import org.apache.solr.client.api.model.BalanceReplicasRequestBody;
37-
import org.apache.solr.client.solrj.apache.CloudLegacySolrClient;
3829
import org.apache.solr.client.solrj.impl.CloudSolrClient;
3930
import org.apache.solr.client.solrj.request.CollectionAdminRequest;
4031
import org.apache.solr.common.cloud.DocCollection;
4132
import org.apache.solr.common.cloud.Replica;
4233
import org.apache.solr.common.util.SimpleOrderedMap;
4334
import org.apache.solr.common.util.StrUtils;
4435
import org.apache.solr.common.util.Utils;
36+
import org.eclipse.jetty.client.BytesRequestContent;
37+
import org.eclipse.jetty.client.HttpClient;
4538
import org.junit.Before;
4639
import org.junit.BeforeClass;
4740
import org.junit.Test;
@@ -99,8 +92,7 @@ public void testAllNodes() throws Exception {
9992
DocCollection collection = cloudClient.getClusterState().getCollection(coll);
10093
log.debug("### Before balancing: {}", collection);
10194

102-
postDataAndGetResponse(
103-
cluster.getSolrClient(), "/api/cluster/replicas/balance", SimpleOrderedMap.of());
95+
postDataAndGetResponse("/api/cluster/replicas/balance", SimpleOrderedMap.of());
10496

10597
collection = cloudClient.getClusterState().getCollectionOrNull(coll, false);
10698
log.debug("### After balancing: {}", collection);
@@ -146,7 +138,6 @@ public void testSomeNodes() throws Exception {
146138
log.debug("### Before balancing: {}", collection);
147139

148140
postDataAndGetResponse(
149-
cluster.getSolrClient(),
150141
"/api/cluster/replicas/balance",
151142
Utils.getReflectWriter(
152143
new BalanceReplicasRequestBody(new HashSet<>(l.subList(1, 4)), true, null)));
@@ -162,31 +153,24 @@ public void testSomeNodes() throws Exception {
162153
"A non-balanced node gained replicas during balancing", replicaNodes.contains(l.get(4)));
163154
}
164155

165-
public Map<?, ?> postDataAndGetResponse(CloudSolrClient cloudClient, String uri, Object body)
166-
throws IOException {
167-
HttpEntityEnclosingRequestBase httpRequest = null;
168-
HttpEntity entity;
169-
String response = null;
170-
Map<?, ?> m = null;
156+
public Map<?, ?> postDataAndGetResponse(String uri, Object jsonBody) throws IOException {
157+
String rspStr = null;
171158

172159
uri = cluster.getJettySolrRunners().get(0).getBaseUrl().toString().replace("/solr", "") + uri;
160+
HttpClient httpClient = cluster.getRandomJetty(random()).getSolrClient().getHttpClient();
173161
try {
174-
httpRequest = new HttpPost(uri);
175-
176-
httpRequest.setEntity(new ByteArrayEntity(Utils.toJSON(body), ContentType.APPLICATION_JSON));
177-
httpRequest.setHeader("Accept", "application/json");
178-
entity =
179-
((CloudLegacySolrClient) cloudClient).getHttpClient().execute(httpRequest).getEntity();
180-
try {
181-
response = EntityUtils.toString(entity, UTF_8);
182-
m = (Map<?, ?>) Utils.fromJSONString(response);
183-
} catch (JSONParser.ParseException e) {
184-
log.error("err response: {}", response);
185-
throw new AssertionError(e);
186-
}
187-
} finally {
188-
httpRequest.releaseConnection();
162+
var rsp =
163+
httpClient
164+
.POST(uri)
165+
.body(new BytesRequestContent("application/json", Utils.toJSON(jsonBody)))
166+
.send();
167+
rspStr = rsp.getContentAsString();
168+
return (Map<?, ?>) Utils.fromJSONString(rspStr);
169+
} catch (JSONParser.ParseException e) {
170+
log.error("err response: {}", rspStr);
171+
throw new AssertionError(e);
172+
} catch (Exception e) {
173+
throw new IOException(e);
189174
}
190-
return m;
191175
}
192176
}

0 commit comments

Comments
 (0)