Skip to content

Commit c350c22

Browse files
committed
Switch many QA projects to use new style requests (#30574)
In #29623 we added `Request` object flavored requests to the low level REST client and in #30315 we deprecated the the old requests. This changes many calls in the `qa` projects to use the new version.
1 parent 32c7b06 commit c350c22

File tree

14 files changed

+150
-117
lines changed

14 files changed

+150
-117
lines changed

qa/ccs-unavailable-clusters/src/test/java/org/elasticsearch/search/CrossClusterSearchUnavailableClusterIT.java

+12-8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.elasticsearch.action.search.SearchRequest;
3636
import org.elasticsearch.action.search.SearchResponse;
3737
import org.elasticsearch.action.search.SearchScrollRequest;
38+
import org.elasticsearch.client.Request;
3839
import org.elasticsearch.client.Response;
3940
import org.elasticsearch.client.ResponseException;
4041
import org.elasticsearch.client.RestClient;
@@ -134,7 +135,7 @@ public void testSearchSkipUnavailable() throws IOException {
134135
for (int i = 0; i < 10; i++) {
135136
restHighLevelClient.index(new IndexRequest("index", "doc", String.valueOf(i)).source("field", "value"));
136137
}
137-
Response refreshResponse = client().performRequest("POST", "/index/_refresh");
138+
Response refreshResponse = client().performRequest(new Request("POST", "/index/_refresh"));
138139
assertEquals(200, refreshResponse.getStatusLine().getStatusCode());
139140

140141
{
@@ -223,10 +224,11 @@ public void testSkipUnavailableDependsOnSeeds() throws IOException {
223224

224225
{
225226
//check that skip_unavailable alone cannot be set
226-
HttpEntity clusterSettingsEntity = buildUpdateSettingsRequestBody(
227-
Collections.singletonMap("skip_unavailable", randomBoolean()));
227+
Request request = new Request("PUT", "/_cluster/settings");
228+
request.setEntity(buildUpdateSettingsRequestBody(
229+
Collections.singletonMap("skip_unavailable", randomBoolean())));
228230
ResponseException responseException = expectThrows(ResponseException.class,
229-
() -> client().performRequest("PUT", "/_cluster/settings", Collections.emptyMap(), clusterSettingsEntity));
231+
() -> client().performRequest(request));
230232
assertEquals(400, responseException.getResponse().getStatusLine().getStatusCode());
231233
assertThat(responseException.getMessage(),
232234
containsString("Missing required setting [search.remote.remote1.seeds] " +
@@ -240,9 +242,10 @@ public void testSkipUnavailableDependsOnSeeds() throws IOException {
240242

241243
{
242244
//check that seeds cannot be reset alone if skip_unavailable is set
243-
HttpEntity clusterSettingsEntity = buildUpdateSettingsRequestBody(Collections.singletonMap("seeds", null));
245+
Request request = new Request("PUT", "/_cluster/settings");
246+
request.setEntity(buildUpdateSettingsRequestBody(Collections.singletonMap("seeds", null)));
244247
ResponseException responseException = expectThrows(ResponseException.class,
245-
() -> client().performRequest("PUT", "/_cluster/settings", Collections.emptyMap(), clusterSettingsEntity));
248+
() -> client().performRequest(request));
246249
assertEquals(400, responseException.getResponse().getStatusLine().getStatusCode());
247250
assertThat(responseException.getMessage(), containsString("Missing required setting [search.remote.remote1.seeds] " +
248251
"for setting [search.remote.remote1.skip_unavailable]"));
@@ -284,8 +287,9 @@ private static void assertSearchConnectFailure() {
284287

285288

286289
private static void updateRemoteClusterSettings(Map<String, Object> settings) throws IOException {
287-
HttpEntity clusterSettingsEntity = buildUpdateSettingsRequestBody(settings);
288-
Response response = client().performRequest("PUT", "/_cluster/settings", Collections.emptyMap(), clusterSettingsEntity);
290+
Request request = new Request("PUT", "/_cluster/settings");
291+
request.setEntity(buildUpdateSettingsRequestBody(settings));
292+
Response response = client().performRequest(request);
289293
assertEquals(200, response.getStatusLine().getStatusCode());
290294
}
291295

qa/die-with-dignity/src/test/java/org/elasticsearch/qa/die_with_dignity/DieWithDignityIT.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.apache.http.ConnectionClosedException;
2323
import org.apache.lucene.util.Constants;
24+
import org.elasticsearch.client.Request;
2425
import org.elasticsearch.common.io.PathUtils;
2526
import org.elasticsearch.test.rest.ESRestTestCase;
2627
import org.hamcrest.Matcher;
@@ -51,7 +52,8 @@ public void testDieWithDignity() throws Exception {
5152
assertThat(pidFileLines, hasSize(1));
5253
final int pid = Integer.parseInt(pidFileLines.get(0));
5354
Files.delete(pidFile);
54-
IOException e = expectThrows(IOException.class, () -> client().performRequest("GET", "/_die_with_dignity"));
55+
IOException e = expectThrows(IOException.class,
56+
() -> client().performRequest(new Request("GET", "/_die_with_dignity")));
5557
Matcher<IOException> failureMatcher = instanceOf(ConnectionClosedException.class);
5658
if (Constants.WINDOWS) {
5759
/*

qa/mixed-cluster/src/test/java/org/elasticsearch/backwards/IndexingIT.java

+41-45
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919
package org.elasticsearch.backwards;
2020

2121
import org.apache.http.HttpHost;
22-
import org.apache.http.entity.ContentType;
23-
import org.apache.http.entity.StringEntity;
2422
import org.elasticsearch.Version;
23+
import org.elasticsearch.client.Request;
2524
import org.elasticsearch.client.Response;
2625
import org.elasticsearch.client.RestClient;
2726
import org.elasticsearch.cluster.metadata.IndexMetaData;
@@ -34,28 +33,23 @@
3433

3534
import java.io.IOException;
3635
import java.util.ArrayList;
37-
import java.util.Collections;
3836
import java.util.HashMap;
3937
import java.util.List;
4038
import java.util.Map;
4139
import java.util.stream.Collectors;
4240

43-
import static com.carrotsearch.randomizedtesting.RandomizedTest.randomAsciiOfLength;
44-
import static java.util.Collections.emptyMap;
45-
import static java.util.Collections.singletonMap;
4641
import static org.elasticsearch.index.seqno.SequenceNumbers.NO_OPS_PERFORMED;
4742
import static org.elasticsearch.index.seqno.SequenceNumbers.UNASSIGNED_SEQ_NO;
48-
import static org.hamcrest.Matchers.empty;
4943
import static org.hamcrest.Matchers.equalTo;
50-
import static org.hamcrest.Matchers.not;
5144

5245
public class IndexingIT extends ESRestTestCase {
5346

5447
private int indexDocs(String index, final int idStart, final int numDocs) throws IOException {
5548
for (int i = 0; i < numDocs; i++) {
5649
final int id = idStart + i;
57-
assertOK(client().performRequest("PUT", index + "/test/" + id, emptyMap(),
58-
new StringEntity("{\"test\": \"test_" + randomAsciiOfLength(2) + "\"}", ContentType.APPLICATION_JSON)));
50+
Request request = new Request("PUT", index + "/test/" + id);
51+
request.setJsonEntity("{\"test\": \"test_" + randomAlphaOfLength(2) + "\"}");
52+
assertOK(client().performRequest(request));
5953
}
6054
return numDocs;
6155
}
@@ -108,7 +102,7 @@ public void testIndexVersionPropagation() throws Exception {
108102
logger.info("allowing shards on all nodes");
109103
updateIndexSettings(index, Settings.builder().putNull("index.routing.allocation.include._name"));
110104
ensureGreen(index);
111-
assertOK(client().performRequest("POST", index + "/_refresh"));
105+
assertOK(client().performRequest(new Request("POST", index + "/_refresh")));
112106
List<Shard> shards = buildShards(index, nodes, newNodeClient);
113107
Shard primary = buildShards(index, nodes, newNodeClient).stream().filter(Shard::isPrimary).findFirst().get();
114108
logger.info("primary resolved to: " + primary.getNode().getNodeName());
@@ -120,7 +114,7 @@ public void testIndexVersionPropagation() throws Exception {
120114
nUpdates = randomIntBetween(minUpdates, maxUpdates);
121115
logger.info("indexing docs with [{}] concurrent updates after allowing shards on all nodes", nUpdates);
122116
final int finalVersionForDoc2 = indexDocWithConcurrentUpdates(index, 2, nUpdates);
123-
assertOK(client().performRequest("POST", index + "/_refresh"));
117+
assertOK(client().performRequest(new Request("POST", index + "/_refresh")));
124118
shards = buildShards(index, nodes, newNodeClient);
125119
primary = shards.stream().filter(Shard::isPrimary).findFirst().get();
126120
logger.info("primary resolved to: " + primary.getNode().getNodeName());
@@ -136,7 +130,7 @@ public void testIndexVersionPropagation() throws Exception {
136130
nUpdates = randomIntBetween(minUpdates, maxUpdates);
137131
logger.info("indexing docs with [{}] concurrent updates after moving primary", nUpdates);
138132
final int finalVersionForDoc3 = indexDocWithConcurrentUpdates(index, 3, nUpdates);
139-
assertOK(client().performRequest("POST", index + "/_refresh"));
133+
assertOK(client().performRequest(new Request("POST", index + "/_refresh")));
140134
shards = buildShards(index, nodes, newNodeClient);
141135
for (Shard shard : shards) {
142136
assertVersion(index, 3, "_only_nodes:" + shard.getNode().getNodeName(), finalVersionForDoc3);
@@ -149,7 +143,7 @@ public void testIndexVersionPropagation() throws Exception {
149143
nUpdates = randomIntBetween(minUpdates, maxUpdates);
150144
logger.info("indexing doc with [{}] concurrent updates after setting number of replicas to 0", nUpdates);
151145
final int finalVersionForDoc4 = indexDocWithConcurrentUpdates(index, 4, nUpdates);
152-
assertOK(client().performRequest("POST", index + "/_refresh"));
146+
assertOK(client().performRequest(new Request("POST", index + "/_refresh")));
153147
shards = buildShards(index, nodes, newNodeClient);
154148
for (Shard shard : shards) {
155149
assertVersion(index, 4, "_only_nodes:" + shard.getNode().getNodeName(), finalVersionForDoc4);
@@ -162,7 +156,7 @@ public void testIndexVersionPropagation() throws Exception {
162156
nUpdates = randomIntBetween(minUpdates, maxUpdates);
163157
logger.info("indexing doc with [{}] concurrent updates after setting number of replicas to 1", nUpdates);
164158
final int finalVersionForDoc5 = indexDocWithConcurrentUpdates(index, 5, nUpdates);
165-
assertOK(client().performRequest("POST", index + "/_refresh"));
159+
assertOK(client().performRequest(new Request("POST", index + "/_refresh")));
166160
shards = buildShards(index, nodes, newNodeClient);
167161
for (Shard shard : shards) {
168162
assertVersion(index, 5, "_only_nodes:" + shard.getNode().getNodeName(), finalVersionForDoc5);
@@ -197,7 +191,7 @@ public void testSeqNoCheckpoints() throws Exception {
197191
logger.info("allowing shards on all nodes");
198192
updateIndexSettings(index, Settings.builder().putNull("index.routing.allocation.include._name"));
199193
ensureGreen(index);
200-
assertOK(client().performRequest("POST", index + "/_refresh"));
194+
assertOK(client().performRequest(new Request("POST", index + "/_refresh")));
201195
for (final String bwcName : bwcNamesList) {
202196
assertCount(index, "_only_nodes:" + bwcName, numDocs);
203197
}
@@ -228,7 +222,7 @@ public void testSeqNoCheckpoints() throws Exception {
228222
logger.info("setting number of replicas to 1");
229223
updateIndexSettings(index, Settings.builder().put("index.number_of_replicas", 1));
230224
ensureGreen(index);
231-
assertOK(client().performRequest("POST", index + "/_refresh"));
225+
assertOK(client().performRequest(new Request("POST", index + "/_refresh")));
232226
// the number of documents on the primary and on the recovered replica should match the number of indexed documents
233227
assertCount(index, "_primary", numDocs);
234228
assertCount(index, "_replica", numDocs);
@@ -242,20 +236,18 @@ public void testUpdateSnapshotStatus() throws Exception {
242236
logger.info("cluster discovered: {}", nodes.toString());
243237

244238
// Create the repository before taking the snapshot.
245-
String repoConfig = Strings
239+
Request request = new Request("PUT", "/_snapshot/repo");
240+
request.setJsonEntity(Strings
246241
.toString(JsonXContent.contentBuilder()
247242
.startObject()
248-
.field("type", "fs")
249-
.startObject("settings")
250-
.field("compress", randomBoolean())
251-
.field("location", System.getProperty("tests.path.repo"))
252-
.endObject()
253-
.endObject());
254-
255-
assertOK(
256-
client().performRequest("PUT", "/_snapshot/repo", emptyMap(),
257-
new StringEntity(repoConfig, ContentType.APPLICATION_JSON))
258-
);
243+
.field("type", "fs")
244+
.startObject("settings")
245+
.field("compress", randomBoolean())
246+
.field("location", System.getProperty("tests.path.repo"))
247+
.endObject()
248+
.endObject()));
249+
250+
assertOK(client().performRequest(request));
259251

260252
String bwcNames = nodes.getBWCNodes().stream().map(Node::getNodeName).collect(Collectors.joining(","));
261253

@@ -269,34 +261,36 @@ public void testUpdateSnapshotStatus() throws Exception {
269261
createIndex(index, settings.build());
270262
indexDocs(index, 0, between(50, 100));
271263
ensureGreen(index);
272-
assertOK(client().performRequest("POST", index + "/_refresh"));
264+
assertOK(client().performRequest(new Request("POST", index + "/_refresh")));
273265

274-
assertOK(
275-
client().performRequest("PUT", "/_snapshot/repo/bwc-snapshot", singletonMap("wait_for_completion", "true"),
276-
new StringEntity("{\"indices\": \"" + index + "\"}", ContentType.APPLICATION_JSON))
277-
);
266+
request = new Request("PUT", "/_snapshot/repo/bwc-snapshot");
267+
request.addParameter("wait_for_completion", "true");
268+
request.setJsonEntity("{\"indices\": \"" + index + "\"}");
269+
assertOK(client().performRequest(request));
278270

279271
// Allocating shards on all nodes, taking snapshots should happen on all nodes.
280272
updateIndexSettings(index, Settings.builder().putNull("index.routing.allocation.include._name"));
281273
ensureGreen(index);
282-
assertOK(client().performRequest("POST", index + "/_refresh"));
274+
assertOK(client().performRequest(new Request("POST", index + "/_refresh")));
283275

284-
assertOK(
285-
client().performRequest("PUT", "/_snapshot/repo/mixed-snapshot", singletonMap("wait_for_completion", "true"),
286-
new StringEntity("{\"indices\": \"" + index + "\"}", ContentType.APPLICATION_JSON))
287-
);
276+
request = new Request("PUT", "/_snapshot/repo/mixed-snapshot");
277+
request.addParameter("wait_for_completion", "true");
278+
request.setJsonEntity("{\"indices\": \"" + index + "\"}");
288279
}
289280

290281
private void assertCount(final String index, final String preference, final int expectedCount) throws IOException {
291-
final Response response = client().performRequest("GET", index + "/_count", Collections.singletonMap("preference", preference));
282+
Request request = new Request("GET", index + "/_count");
283+
request.addParameter("preference", preference);
284+
final Response response = client().performRequest(request);
292285
assertOK(response);
293286
final int actualCount = Integer.parseInt(ObjectPath.createFromResponse(response).evaluate("count").toString());
294287
assertThat(actualCount, equalTo(expectedCount));
295288
}
296289

297290
private void assertVersion(final String index, final int docId, final String preference, final int expectedVersion) throws IOException {
298-
final Response response = client().performRequest("GET", index + "/test/" + docId,
299-
Collections.singletonMap("preference", preference));
291+
Request request = new Request("GET", index + "/test/" + docId);
292+
request.addParameter("preference", preference);
293+
final Response response = client().performRequest(request);
300294
assertOK(response);
301295
final int actualVersion = Integer.parseInt(ObjectPath.createFromResponse(response).evaluate("_version").toString());
302296
assertThat("version mismatch for doc [" + docId + "] preference [" + preference + "]", actualVersion, equalTo(expectedVersion));
@@ -339,7 +333,9 @@ private void assertSeqNoOnShards(String index, Nodes nodes, int numDocs, RestCli
339333
}
340334

341335
private List<Shard> buildShards(String index, Nodes nodes, RestClient client) throws IOException {
342-
Response response = client.performRequest("GET", index + "/_stats", singletonMap("level", "shards"));
336+
Request request = new Request("GET", index + "/_stats");
337+
request.addParameter("level", "shards");
338+
Response response = client.performRequest(request);
343339
List<Object> shardStats = ObjectPath.createFromResponse(response).evaluate("indices." + index + ".shards.0");
344340
ArrayList<Shard> shards = new ArrayList<>();
345341
for (Object shard : shardStats) {
@@ -361,7 +357,7 @@ private List<Shard> buildShards(String index, Nodes nodes, RestClient client) th
361357
}
362358

363359
private Nodes buildNodeAndVersions() throws IOException {
364-
Response response = client().performRequest("GET", "_nodes");
360+
Response response = client().performRequest(new Request("GET", "_nodes"));
365361
ObjectPath objectPath = ObjectPath.createFromResponse(response);
366362
Map<String, Object> nodesAsMap = objectPath.evaluate("nodes");
367363
Nodes nodes = new Nodes();
@@ -372,7 +368,7 @@ private Nodes buildNodeAndVersions() throws IOException {
372368
Version.fromString(objectPath.evaluate("nodes." + id + ".version")),
373369
HttpHost.create(objectPath.evaluate("nodes." + id + ".http.publish_address"))));
374370
}
375-
response = client().performRequest("GET", "_cluster/state");
371+
response = client().performRequest(new Request("GET", "_cluster/state"));
376372
nodes.setMasterNodeId(ObjectPath.createFromResponse(response).evaluate("master_node"));
377373
return nodes;
378374
}

0 commit comments

Comments
 (0)