Skip to content

Commit 351bbb8

Browse files
authored
Switch distribution to new style Requests (#30595)
In #29623 we added `Request` object flavored requests to the low level REST client and in #30315 we deprecated the old `performRequest`s. This changes all calls in the `distribution/archives/integ-test-zip` project to use the new versions.
1 parent 91d8371 commit 351bbb8

File tree

4 files changed

+74
-73
lines changed

4 files changed

+74
-73
lines changed

distribution/archives/integ-test-zip/src/test/java/org/elasticsearch/test/rest/CreatedLocationHeaderIT.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,11 @@
1919

2020
package org.elasticsearch.test.rest;
2121

22-
import org.apache.http.entity.ContentType;
23-
import org.apache.http.entity.StringEntity;
2422
import org.elasticsearch.client.Request;
2523
import org.elasticsearch.client.Response;
2624

2725
import java.io.IOException;
2826

29-
import static java.util.Collections.emptyMap;
3027
import static java.util.Collections.singletonMap;
3128
import static org.hamcrest.Matchers.equalTo;
3229
import static org.hamcrest.Matchers.startsWith;
@@ -49,26 +46,31 @@ public void testIndexWithoutId() throws IOException {
4946
}
5047

5148
public void testUpsert() throws IOException {
52-
locationTestCase(client().performRequest("POST", "test/test/1/_update", emptyMap(), new StringEntity("{"
53-
+ "\"doc\": {\"test\": \"test\"},"
54-
+ "\"doc_as_upsert\": true}", ContentType.APPLICATION_JSON)));
49+
Request request = new Request("POST", "test/test/1/_update");
50+
request.setJsonEntity("{"
51+
+ "\"doc\": {\"test\": \"test\"},"
52+
+ "\"doc_as_upsert\": true}");
53+
locationTestCase(client().performRequest(request));
5554
}
5655

5756
private void locationTestCase(String method, String url) throws IOException {
58-
locationTestCase(client().performRequest(method, url, emptyMap(),
59-
new StringEntity("{\"test\": \"test\"}", ContentType.APPLICATION_JSON)));
57+
final Request request = new Request(method, url);
58+
request.setJsonEntity("{\"test\": \"test\"}");
59+
locationTestCase(client().performRequest(request));
6060
// we have to delete the index otherwise the second indexing request will route to the single shard and not produce a 201
6161
final Response response = client().performRequest(new Request("DELETE", "test"));
6262
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
63-
locationTestCase(client().performRequest(method, url + "?routing=cat", emptyMap(),
64-
new StringEntity("{\"test\": \"test\"}", ContentType.APPLICATION_JSON)));
63+
final Request withRouting = new Request(method, url);
64+
withRouting.addParameter("routing", "cat");
65+
withRouting.setJsonEntity("{\"test\": \"test\"}");
66+
locationTestCase(client().performRequest(withRouting));
6567
}
6668

6769
private void locationTestCase(Response response) throws IOException {
6870
assertEquals(201, response.getStatusLine().getStatusCode());
6971
String location = response.getHeader("Location");
7072
assertThat(location, startsWith("/test/test/"));
71-
Response getResponse = client().performRequest("GET", location);
73+
Response getResponse = client().performRequest(new Request("GET", location));
7274
assertEquals(singletonMap("test", "test"), entityAsMap(getResponse).get("_source"));
7375
}
7476

distribution/archives/integ-test-zip/src/test/java/org/elasticsearch/test/rest/NodeRestUsageIT.java

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@
1919

2020
package org.elasticsearch.test.rest;
2121

22-
import org.apache.http.entity.ContentType;
23-
import org.apache.http.entity.StringEntity;
2422
import org.elasticsearch.client.Response;
2523
import org.elasticsearch.client.ResponseException;
24+
import org.elasticsearch.client.Request;
2625

2726
import java.io.IOException;
28-
import java.util.Collections;
2927
import java.util.HashMap;
3028
import java.util.Map;
3129

@@ -39,8 +37,8 @@ public class NodeRestUsageIT extends ESRestTestCase {
3937
@SuppressWarnings("unchecked")
4038
public void testWithRestUsage() throws IOException {
4139
// First get the current usage figures
42-
Response beforeResponse = client().performRequest("GET",
43-
randomFrom("_nodes/usage", "_nodes/usage/rest_actions", "_nodes/usage/_all"));
40+
String path = randomFrom("_nodes/usage", "_nodes/usage/rest_actions", "_nodes/usage/_all");
41+
Response beforeResponse = client().performRequest(new Request("GET", path));
4442
Map<String, Object> beforeResponseBodyMap = entityAsMap(beforeResponse);
4543
assertThat(beforeResponseBodyMap, notNullValue());
4644
Map<String, Object> before_nodesMap = (Map<String, Object>) beforeResponseBodyMap.get("_nodes");
@@ -80,24 +78,24 @@ public void testWithRestUsage() throws IOException {
8078
}
8179

8280
// Do some requests to get some rest usage stats
83-
client().performRequest("PUT", "/test");
84-
client().performRequest("POST", "/test/doc/1", Collections.emptyMap(),
85-
new StringEntity("{ \"foo\": \"bar\"}", ContentType.APPLICATION_JSON));
86-
client().performRequest("POST", "/test/doc/2", Collections.emptyMap(),
87-
new StringEntity("{ \"foo\": \"bar\"}", ContentType.APPLICATION_JSON));
88-
client().performRequest("POST", "/test/doc/3", Collections.emptyMap(),
89-
new StringEntity("{ \"foo\": \"bar\"}", ContentType.APPLICATION_JSON));
90-
client().performRequest("GET", "/test/_search");
91-
client().performRequest("POST", "/test/doc/4", Collections.emptyMap(),
92-
new StringEntity("{ \"foo\": \"bar\"}", ContentType.APPLICATION_JSON));
93-
client().performRequest("POST", "/test/_refresh");
94-
client().performRequest("GET", "/_cat/indices");
95-
client().performRequest("GET", "/_nodes");
96-
client().performRequest("GET", "/test/_search");
97-
client().performRequest("GET", "/_nodes/stats");
98-
client().performRequest("DELETE", "/test");
81+
client().performRequest(new Request("PUT", "/test"));
82+
for (int i = 0; i < 3; i++) {
83+
final Request index = new Request("POST", "/test/doc/1");
84+
index.setJsonEntity("{\"foo\": \"bar\"}");
85+
client().performRequest(index);
86+
}
87+
client().performRequest(new Request("GET", "/test/_search"));
88+
final Request index4 = new Request("POST", "/test/doc/4");
89+
index4.setJsonEntity("{\"foo\": \"bar\"}");
90+
client().performRequest(index4);
91+
client().performRequest(new Request("POST", "/test/_refresh"));
92+
client().performRequest(new Request("GET", "/_cat/indices"));
93+
client().performRequest(new Request("GET", "/_nodes"));
94+
client().performRequest(new Request("GET", "/test/_search"));
95+
client().performRequest(new Request("GET", "/_nodes/stats"));
96+
client().performRequest(new Request("DELETE", "/test"));
9997

100-
Response response = client().performRequest("GET", "_nodes/usage");
98+
Response response = client().performRequest(new Request("GET", "_nodes/usage"));
10199
Map<String, Object> responseBodyMap = entityAsMap(response);
102100
assertThat(responseBodyMap, notNullValue());
103101
Map<String, Object> _nodesMap = (Map<String, Object>) responseBodyMap.get("_nodes");
@@ -139,7 +137,7 @@ public void testWithRestUsage() throws IOException {
139137

140138
public void testMetricsWithAll() throws IOException {
141139
ResponseException exception = expectThrows(ResponseException.class,
142-
() -> client().performRequest("GET", "_nodes/usage/_all,rest_actions"));
140+
() -> client().performRequest(new Request("GET", "_nodes/usage/_all,rest_actions")));
143141
assertNotNull(exception);
144142
assertThat(exception.getMessage(), containsString("\"type\":\"illegal_argument_exception\","
145143
+ "\"reason\":\"request [_nodes/usage/_all,rest_actions] contains _all and individual metrics [_all,rest_actions]\""));

distribution/archives/integ-test-zip/src/test/java/org/elasticsearch/test/rest/RequestsWithoutContentIT.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.test.rest;
2121

2222
import org.elasticsearch.client.ResponseException;
23+
import org.elasticsearch.client.Request;
2324

2425
import java.io.IOException;
2526

@@ -28,56 +29,56 @@
2829
public class RequestsWithoutContentIT extends ESRestTestCase {
2930

3031
public void testIndexMissingBody() throws IOException {
31-
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest(
32-
randomBoolean() ? "POST" : "PUT", "/idx/type/123"));
32+
ResponseException responseException = expectThrows(ResponseException.class, () ->
33+
client().performRequest(new Request(randomBoolean() ? "POST" : "PUT", "/idx/type/123")));
3334
assertResponseException(responseException, "request body is required");
3435
}
3536

3637
public void testBulkMissingBody() throws IOException {
37-
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest(
38-
randomBoolean() ? "POST" : "PUT", "/_bulk"));
38+
ResponseException responseException = expectThrows(ResponseException.class, () ->
39+
client().performRequest(new Request(randomBoolean() ? "POST" : "PUT", "/_bulk")));
3940
assertResponseException(responseException, "request body is required");
4041
}
4142

4243
public void testPutSettingsMissingBody() throws IOException {
43-
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest(
44-
"PUT", "/_settings"));
44+
ResponseException responseException = expectThrows(ResponseException.class, () ->
45+
client().performRequest(new Request("PUT", "/_settings")));
4546
assertResponseException(responseException, "request body is required");
4647
}
4748

4849
public void testPutMappingsMissingBody() throws IOException {
49-
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest(
50-
randomBoolean() ? "POST" : "PUT", "/test_index/test_type/_mapping"));
50+
ResponseException responseException = expectThrows(ResponseException.class, () ->
51+
client().performRequest(new Request(randomBoolean() ? "POST" : "PUT", "/test_index/test_type/_mapping")));
5152
assertResponseException(responseException, "request body is required");
5253
}
5354

5455
public void testPutIndexTemplateMissingBody() throws IOException {
55-
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest(
56-
randomBoolean() ? "PUT" : "POST", "/_template/my_template"));
56+
ResponseException responseException = expectThrows(ResponseException.class, () ->
57+
client().performRequest(new Request(randomBoolean() ? "PUT" : "POST", "/_template/my_template")));
5758
assertResponseException(responseException, "request body is required");
5859
}
5960

6061
public void testMultiSearchMissingBody() throws IOException {
61-
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest(
62-
randomBoolean() ? "POST" : "GET", "/_msearch"));
62+
ResponseException responseException = expectThrows(ResponseException.class, () ->
63+
client().performRequest(new Request(randomBoolean() ? "POST" : "GET", "/_msearch")));
6364
assertResponseException(responseException, "request body or source parameter is required");
6465
}
6566

6667
public void testPutPipelineMissingBody() throws IOException {
67-
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest(
68-
"PUT", "/_ingest/pipeline/my_pipeline"));
68+
ResponseException responseException = expectThrows(ResponseException.class, () ->
69+
client().performRequest(new Request("PUT", "/_ingest/pipeline/my_pipeline")));
6970
assertResponseException(responseException, "request body or source parameter is required");
7071
}
7172

7273
public void testSimulatePipelineMissingBody() throws IOException {
73-
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest(
74-
randomBoolean() ? "POST" : "GET", "/_ingest/pipeline/my_pipeline/_simulate"));
74+
ResponseException responseException = expectThrows(ResponseException.class, () ->
75+
client().performRequest(new Request(randomBoolean() ? "POST" : "GET", "/_ingest/pipeline/my_pipeline/_simulate")));
7576
assertResponseException(responseException, "request body or source parameter is required");
7677
}
7778

7879
public void testPutScriptMissingBody() throws IOException {
79-
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest(
80-
randomBoolean() ? "POST" : "PUT", "/_scripts/lang"));
80+
ResponseException responseException = expectThrows(ResponseException.class, () ->
81+
client().performRequest(new Request(randomBoolean() ? "POST" : "PUT", "/_scripts/lang")));
8182
assertResponseException(responseException, "request body is required");
8283
}
8384

distribution/archives/integ-test-zip/src/test/java/org/elasticsearch/test/rest/WaitForRefreshAndCloseTests.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,40 +19,36 @@
1919

2020
package org.elasticsearch.test.rest;
2121

22-
import org.apache.http.HttpEntity;
23-
import org.apache.http.entity.ContentType;
24-
import org.apache.http.entity.StringEntity;
2522
import org.apache.http.util.EntityUtils;
2623
import org.elasticsearch.action.ActionFuture;
2724
import org.elasticsearch.action.support.PlainActionFuture;
2825
import org.elasticsearch.client.Response;
2926
import org.elasticsearch.client.ResponseException;
3027
import org.elasticsearch.client.ResponseListener;
28+
import org.elasticsearch.client.Request;
3129
import org.junit.After;
3230
import org.junit.Before;
3331

3432
import java.io.IOException;
3533
import java.nio.charset.StandardCharsets;
36-
import java.util.HashMap;
3734
import java.util.Locale;
3835
import java.util.Map;
3936

40-
import static java.util.Collections.emptyMap;
41-
4237
/**
4338
* Tests that wait for refresh is fired if the index is closed.
4439
*/
4540
public class WaitForRefreshAndCloseTests extends ESRestTestCase {
4641
@Before
4742
public void setupIndex() throws IOException {
4843
try {
49-
client().performRequest("DELETE", indexName());
44+
client().performRequest(new Request("DELETE", indexName()));
5045
} catch (ResponseException e) {
5146
// If we get an error, it should be because the index doesn't exist
5247
assertEquals(404, e.getResponse().getStatusLine().getStatusCode());
5348
}
54-
client().performRequest("PUT", indexName(), emptyMap(),
55-
new StringEntity("{\"settings\":{\"refresh_interval\":-1}}", ContentType.APPLICATION_JSON));
49+
Request request = new Request("PUT", indexName());
50+
request.setJsonEntity("{\"settings\":{\"refresh_interval\":-1}}");
51+
client().performRequest(request);
5652
}
5753

5854
@After
@@ -69,17 +65,20 @@ private String docPath() {
6965
}
7066

7167
public void testIndexAndThenClose() throws Exception {
72-
closeWhileListenerEngaged(start("PUT", "", new StringEntity("{\"test\":\"test\"}", ContentType.APPLICATION_JSON)));
68+
closeWhileListenerEngaged(start("PUT", "", "{\"test\":\"test\"}"));
7369
}
7470

7571
public void testUpdateAndThenClose() throws Exception {
76-
client().performRequest("PUT", docPath(), emptyMap(), new StringEntity("{\"test\":\"test\"}", ContentType.APPLICATION_JSON));
77-
closeWhileListenerEngaged(start("POST", "/_update",
78-
new StringEntity("{\"doc\":{\"name\":\"test\"}}", ContentType.APPLICATION_JSON)));
72+
Request request = new Request("PUT", docPath());
73+
request.setJsonEntity("{\"test\":\"test\"}");
74+
client().performRequest(request);
75+
closeWhileListenerEngaged(start("POST", "/_update", "{\"doc\":{\"name\":\"test\"}}"));
7976
}
8077

8178
public void testDeleteAndThenClose() throws Exception {
82-
client().performRequest("PUT", docPath(), emptyMap(), new StringEntity("{\"test\":\"test\"}", ContentType.APPLICATION_JSON));
79+
Request request = new Request("PUT", docPath());
80+
request.setJsonEntity("{\"test\":\"test\"}");
81+
client().performRequest(request);
8382
closeWhileListenerEngaged(start("DELETE", "", null));
8483
}
8584

@@ -88,7 +87,7 @@ private void closeWhileListenerEngaged(ActionFuture<String> future) throws Excep
8887
assertBusy(() -> {
8988
Map<String, Object> stats;
9089
try {
91-
stats = entityAsMap(client().performRequest("GET", indexName() + "/_stats/refresh"));
90+
stats = entityAsMap(client().performRequest(new Request("GET", indexName() + "/_stats/refresh")));
9291
} catch (IOException e) {
9392
throw new RuntimeException(e);
9493
}
@@ -105,18 +104,19 @@ private void closeWhileListenerEngaged(ActionFuture<String> future) throws Excep
105104
});
106105

107106
// Close the index. That should flush the listener.
108-
client().performRequest("POST", indexName() + "/_close");
107+
client().performRequest(new Request("POST", indexName() + "/_close"));
109108

110109
// The request shouldn't fail. It certainly shouldn't hang.
111110
future.get();
112111
}
113112

114-
private ActionFuture<String> start(String method, String path, HttpEntity body) {
113+
private ActionFuture<String> start(String method, String path, String body) {
115114
PlainActionFuture<String> future = new PlainActionFuture<>();
116-
Map<String, String> params = new HashMap<>();
117-
params.put("refresh", "wait_for");
118-
params.put("error_trace", "");
119-
client().performRequestAsync(method, docPath() + path, params, body, new ResponseListener() {
115+
Request request = new Request(method, docPath() + path);
116+
request.addParameter("refresh", "wait_for");
117+
request.addParameter("error_trace", "");
118+
request.setJsonEntity(body);
119+
client().performRequestAsync(request, new ResponseListener() {
120120
@Override
121121
public void onSuccess(Response response) {
122122
try {

0 commit comments

Comments
 (0)