-
Notifications
You must be signed in to change notification settings - Fork 821
SOLR-18188: Tests, switch from Apache HttpClient to Jetty HttpClient #4266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
0989327
00e89df
09f3bfb
ed8b57b
3f37f1c
5c7bf2e
548abfd
40b9991
e77d8db
0dd173a
2683172
ad3f730
11ee2f2
3eee082
c6b32c5
cd72b3f
28d1faf
30597c6
2b02520
f376e61
8ae9852
49fbdcb
b96cfe1
ad62248
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
dsmiley marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,37 +18,12 @@ | |
| package org.apache.solr.security; | ||
|
|
||
| import java.security.Principal; | ||
| import java.util.Objects; | ||
|
|
||
| /** A basic name-only {@link Principal}. */ | ||
| public final class SimplePrincipal implements Principal { | ||
| // TODO use a record. But javadoc tooling complained as it was confused (Java version issue?) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this mystery was solved previously, unblocking this nice conversion |
||
| private final String name; | ||
|
|
||
| public SimplePrincipal(String name) { | ||
| this.name = name; | ||
| } | ||
| public record SimplePrincipal(String name) implements Principal { | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (obj == this) return true; | ||
| if (obj == null || obj.getClass() != this.getClass()) return false; | ||
| var that = (SimplePrincipal) obj; | ||
| return Objects.equals(this.name, that.name); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(name); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "SimplePrincipal[" + "name=" + name + ']'; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,25 +19,16 @@ | |
| import static org.apache.solr.common.cloud.ZkStateReader.ALIASES; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.function.Consumer; | ||
| import java.util.function.UnaryOperator; | ||
| import org.apache.http.client.methods.CloseableHttpResponse; | ||
| import org.apache.http.client.methods.HttpDelete; | ||
| import org.apache.http.client.methods.HttpGet; | ||
| import org.apache.http.client.methods.HttpPut; | ||
| import org.apache.http.client.methods.HttpUriRequest; | ||
| import org.apache.http.entity.ContentType; | ||
| import org.apache.http.entity.StringEntity; | ||
| import org.apache.http.impl.client.CloseableHttpClient; | ||
| import org.apache.http.util.EntityUtils; | ||
| import org.apache.solr.client.solrj.SolrClient; | ||
| import org.apache.solr.client.solrj.SolrRequest; | ||
| import org.apache.solr.client.solrj.SolrServerException; | ||
| import org.apache.solr.client.solrj.apache.CloudLegacySolrClient; | ||
| import org.apache.solr.client.solrj.cloud.SolrCloudManager; | ||
| import org.apache.solr.client.solrj.impl.CloudSolrClient; | ||
| import org.apache.solr.client.solrj.impl.ClusterStateProvider; | ||
|
|
@@ -60,15 +51,17 @@ | |
| import org.apache.solr.embedded.JettySolrRunner; | ||
| import org.apache.solr.util.TimeOut; | ||
| import org.apache.zookeeper.KeeperException; | ||
| import org.eclipse.jetty.client.ContentResponse; | ||
| import org.eclipse.jetty.client.HttpClient; | ||
| import org.eclipse.jetty.client.StringRequestContent; | ||
| import org.eclipse.jetty.http.HttpMethod; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
|
|
||
| public class AliasIntegrationTest extends SolrCloudTestCase { | ||
|
|
||
| private CloseableHttpClient httpClient; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. usually no field is needed since it's easy to grab a managed Jetty HttpClient from places |
||
|
|
||
| @BeforeClass | ||
| public static void setupCluster() throws Exception { | ||
| configureCluster(2).addConfig("conf", configset("cloud-minimal")).configure(); | ||
|
|
@@ -78,9 +71,6 @@ public static void setupCluster() throws Exception { | |
| @Override | ||
| public void setUp() throws Exception { | ||
| super.setUp(); | ||
|
|
||
| httpClient = | ||
| (CloseableHttpClient) ((CloudLegacySolrClient) cluster.getSolrClient()).getHttpClient(); | ||
| } | ||
|
|
||
| @After | ||
|
|
@@ -238,49 +228,53 @@ public void testProperties() throws Exception { | |
| public void testModifyPropertiesV2() throws Exception { | ||
| final String aliasName = getSaferTestName(); | ||
| ZkStateReader zkStateReader = createColectionsAndAlias(aliasName); | ||
| final String baseUrl = cluster.getRandomJetty(random()).getBaseURLV2().toString(); | ||
| final JettySolrRunner runner = cluster.getRandomJetty(random()); | ||
| final String baseUrl = runner.getBaseURLV2().toString(); | ||
| final HttpClient httpClient = runner.getSolrClient().getHttpClient(); | ||
| String aliasApi = String.format(Locale.ENGLISH, "/aliases/%s/properties", aliasName); | ||
|
|
||
| HttpPut withoutBody = new HttpPut(baseUrl + aliasApi); | ||
| assertEquals(400, httpClient.execute(withoutBody).getStatusLine().getStatusCode()); | ||
|
|
||
| HttpPut update = new HttpPut(baseUrl + aliasApi); | ||
| update.setEntity( | ||
| new StringEntity( | ||
| "{\n" | ||
| + " \"properties\":\n" | ||
| + " {\n" | ||
| + " \"foo\": \"baz\",\n" | ||
| + " \"bar\": \"bam\"\n" | ||
| + " }\n" | ||
| + "}", | ||
| ContentType.APPLICATION_JSON)); | ||
| assertSuccess(update); | ||
| assertEquals( | ||
| 400, httpClient.newRequest(baseUrl + aliasApi).method(HttpMethod.PUT).send().getStatus()); | ||
|
|
||
| String body = | ||
| "{\n" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i had good luck with the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I may change some of these where I add new code but otherwise don't want to change more lines in a massive PR.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get that! I was thinking since whitespace changed, but I guess you can diff wihtout whitespace and that reduces the change. |
||
| + " \"properties\":\n" | ||
| + " {\n" | ||
| + " \"foo\": \"baz\",\n" | ||
| + " \"bar\": \"bam\"\n" | ||
| + " }\n" | ||
| + "}"; | ||
| assertSuccess( | ||
| httpClient | ||
| .newRequest(baseUrl + aliasApi) | ||
| .method(HttpMethod.PUT) | ||
| .body(new StringRequestContent("application/json", body, StandardCharsets.UTF_8)) | ||
| .send()); | ||
|
dsmiley marked this conversation as resolved.
|
||
| checkFooAndBarMeta(aliasName, zkStateReader, "baz", "bam"); | ||
|
|
||
| String aliasPropertyApi = | ||
| String.format(Locale.ENGLISH, "/aliases/%s/properties/%s", aliasName, "foo"); | ||
| HttpPut updateByProperty = new HttpPut(baseUrl + aliasPropertyApi); | ||
| updateByProperty.setEntity( | ||
| new StringEntity("{ \"value\": \"zab\" }", ContentType.APPLICATION_JSON)); | ||
| assertSuccess(updateByProperty); | ||
| assertSuccess( | ||
| httpClient | ||
| .newRequest(baseUrl + aliasPropertyApi) | ||
| .method(HttpMethod.PUT) | ||
| .body( | ||
| new StringRequestContent( | ||
| "application/json", "{ \"value\": \"zab\" }", StandardCharsets.UTF_8)) | ||
| .send()); | ||
| checkFooAndBarMeta(aliasName, zkStateReader, "zab", "bam"); | ||
|
|
||
| HttpDelete deleteByProperty = new HttpDelete(baseUrl + aliasPropertyApi); | ||
| assertSuccess(deleteByProperty); | ||
| assertSuccess( | ||
| httpClient.newRequest(baseUrl + aliasPropertyApi).method(HttpMethod.DELETE).send()); | ||
| checkFooAndBarMeta(aliasName, zkStateReader, null, "bam"); | ||
|
|
||
| HttpPut deleteByEmptyValue = new HttpPut(baseUrl + aliasApi); | ||
| deleteByEmptyValue.setEntity( | ||
| new StringEntity( | ||
| "{\n" | ||
| + " \"properties\":\n" | ||
| + " {\n" | ||
| + " \"bar\": \"\"\n" | ||
| + " }\n" | ||
| + "}", | ||
| ContentType.APPLICATION_JSON)); | ||
| assertSuccess(deleteByEmptyValue); | ||
| body = "{ \"properties\": { \"bar\": \"\" } }"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i know you can make the json more compact, but the long form makes the nesting easier to read. maybe just my personal preference.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Triple-quote would be clearer and also compact -- can then remove the quote escaping (I think).
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for one-liners, no it forces you to use another line. So I don't plan to update much for this matter.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that works. |
||
| assertSuccess( | ||
| httpClient | ||
| .newRequest(baseUrl + aliasApi) | ||
| .method(HttpMethod.PUT) | ||
| .body(new StringRequestContent("application/json", body, StandardCharsets.UTF_8)) | ||
| .send()); | ||
| checkFooAndBarMeta(aliasName, zkStateReader, null, null); | ||
| } | ||
|
|
||
|
|
@@ -289,29 +283,29 @@ public void testModifyPropertiesV1() throws Exception { | |
| // note we don't use TZ in this test, thus it's UTC | ||
| final String aliasName = getSaferTestName(); | ||
| ZkStateReader zkStateReader = createColectionsAndAlias(aliasName); | ||
| final String baseUrl = cluster.getRandomJetty(random()).getBaseUrl().toString(); | ||
| HttpGet get = | ||
| new HttpGet( | ||
| baseUrl | ||
| + "/admin/collections?action=ALIASPROP" | ||
| + "&wt=xml" | ||
| + "&name=" | ||
| + aliasName | ||
| + "&property.foo=baz" | ||
| + "&property.bar=bam"); | ||
| assertSuccess(get); | ||
| final JettySolrRunner runner = cluster.getRandomJetty(random()); | ||
| final String baseUrl = runner.getBaseUrl().toString(); | ||
| final HttpClient httpClient = runner.getSolrClient().getHttpClient(); | ||
| String url = | ||
| baseUrl | ||
| + "/admin/collections?action=ALIASPROP" | ||
| + "&wt=xml" | ||
| + "&name=" | ||
| + aliasName | ||
| + "&property.foo=baz" | ||
| + "&property.bar=bam"; | ||
| assertSuccess(httpClient.GET(url)); | ||
| checkFooAndBarMeta(aliasName, zkStateReader, "baz", "bam"); | ||
|
|
||
| HttpGet remove = | ||
| new HttpGet( | ||
| baseUrl | ||
| + "/admin/collections?action=ALIASPROP" | ||
| + "&wt=xml" | ||
| + "&name=" | ||
| + aliasName | ||
| + "&property.foo=" | ||
| + "&property.bar=bar"); | ||
| assertSuccess(remove); | ||
| url = | ||
| baseUrl | ||
| + "/admin/collections?action=ALIASPROP" | ||
| + "&wt=xml" | ||
| + "&name=" | ||
| + aliasName | ||
| + "&property.foo=" | ||
| + "&property.bar=bar"; | ||
| assertSuccess(httpClient.GET(url)); | ||
| checkFooAndBarMeta(aliasName, zkStateReader, null, "bar"); | ||
| } | ||
|
|
||
|
|
@@ -508,12 +502,10 @@ private ZkStateReader createColectionsAndAlias(String aliasName) | |
| return zkStateReader; | ||
| } | ||
|
|
||
| private void assertSuccess(HttpUriRequest msg) throws IOException { | ||
| try (CloseableHttpResponse response = httpClient.execute(msg)) { | ||
| if (200 != response.getStatusLine().getStatusCode()) { | ||
| System.err.println(EntityUtils.toString(response.getEntity())); | ||
| fail("Unexpected status: " + response.getStatusLine()); | ||
| } | ||
| private void assertSuccess(ContentResponse response) { | ||
|
dsmiley marked this conversation as resolved.
|
||
| if (200 != response.getStatus()) { | ||
| System.err.println(response.getContentAsString()); | ||
| fail("Unexpected status: " + response.getStatus()); | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
progress