Skip to content

Commit 6695d11

Browse files
authored
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 abc06d5 commit 6695d11

File tree

14 files changed

+145
-118
lines changed

14 files changed

+145
-118
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-44
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,25 +33,21 @@
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.hamcrest.Matchers.equalTo;
47-
import static org.hamcrest.Matchers.not;
4842

4943
public class IndexingIT extends ESRestTestCase {
5044

5145
private int indexDocs(String index, final int idStart, final int numDocs) throws IOException {
5246
for (int i = 0; i < numDocs; i++) {
5347
final int id = idStart + i;
54-
assertOK(client().performRequest("PUT", index + "/test/" + id, emptyMap(),
55-
new StringEntity("{\"test\": \"test_" + randomAsciiOfLength(2) + "\"}", ContentType.APPLICATION_JSON)));
48+
Request request = new Request("PUT", index + "/test/" + id);
49+
request.setJsonEntity("{\"test\": \"test_" + randomAlphaOfLength(2) + "\"}");
50+
assertOK(client().performRequest(request));
5651
}
5752
return numDocs;
5853
}
@@ -105,7 +100,7 @@ public void testIndexVersionPropagation() throws Exception {
105100
logger.info("allowing shards on all nodes");
106101
updateIndexSettings(index, Settings.builder().putNull("index.routing.allocation.include._name"));
107102
ensureGreen(index);
108-
assertOK(client().performRequest("POST", index + "/_refresh"));
103+
assertOK(client().performRequest(new Request("POST", index + "/_refresh")));
109104
List<Shard> shards = buildShards(index, nodes, newNodeClient);
110105
Shard primary = buildShards(index, nodes, newNodeClient).stream().filter(Shard::isPrimary).findFirst().get();
111106
logger.info("primary resolved to: " + primary.getNode().getNodeName());
@@ -117,7 +112,7 @@ public void testIndexVersionPropagation() throws Exception {
117112
nUpdates = randomIntBetween(minUpdates, maxUpdates);
118113
logger.info("indexing docs with [{}] concurrent updates after allowing shards on all nodes", nUpdates);
119114
final int finalVersionForDoc2 = indexDocWithConcurrentUpdates(index, 2, nUpdates);
120-
assertOK(client().performRequest("POST", index + "/_refresh"));
115+
assertOK(client().performRequest(new Request("POST", index + "/_refresh")));
121116
shards = buildShards(index, nodes, newNodeClient);
122117
primary = shards.stream().filter(Shard::isPrimary).findFirst().get();
123118
logger.info("primary resolved to: " + primary.getNode().getNodeName());
@@ -133,7 +128,7 @@ public void testIndexVersionPropagation() throws Exception {
133128
nUpdates = randomIntBetween(minUpdates, maxUpdates);
134129
logger.info("indexing docs with [{}] concurrent updates after moving primary", nUpdates);
135130
final int finalVersionForDoc3 = indexDocWithConcurrentUpdates(index, 3, nUpdates);
136-
assertOK(client().performRequest("POST", index + "/_refresh"));
131+
assertOK(client().performRequest(new Request("POST", index + "/_refresh")));
137132
shards = buildShards(index, nodes, newNodeClient);
138133
for (Shard shard : shards) {
139134
assertVersion(index, 3, "_only_nodes:" + shard.getNode().getNodeName(), finalVersionForDoc3);
@@ -146,7 +141,7 @@ public void testIndexVersionPropagation() throws Exception {
146141
nUpdates = randomIntBetween(minUpdates, maxUpdates);
147142
logger.info("indexing doc with [{}] concurrent updates after setting number of replicas to 0", nUpdates);
148143
final int finalVersionForDoc4 = indexDocWithConcurrentUpdates(index, 4, nUpdates);
149-
assertOK(client().performRequest("POST", index + "/_refresh"));
144+
assertOK(client().performRequest(new Request("POST", index + "/_refresh")));
150145
shards = buildShards(index, nodes, newNodeClient);
151146
for (Shard shard : shards) {
152147
assertVersion(index, 4, "_only_nodes:" + shard.getNode().getNodeName(), finalVersionForDoc4);
@@ -159,7 +154,7 @@ public void testIndexVersionPropagation() throws Exception {
159154
nUpdates = randomIntBetween(minUpdates, maxUpdates);
160155
logger.info("indexing doc with [{}] concurrent updates after setting number of replicas to 1", nUpdates);
161156
final int finalVersionForDoc5 = indexDocWithConcurrentUpdates(index, 5, nUpdates);
162-
assertOK(client().performRequest("POST", index + "/_refresh"));
157+
assertOK(client().performRequest(new Request("POST", index + "/_refresh")));
163158
shards = buildShards(index, nodes, newNodeClient);
164159
for (Shard shard : shards) {
165160
assertVersion(index, 5, "_only_nodes:" + shard.getNode().getNodeName(), finalVersionForDoc5);
@@ -191,7 +186,7 @@ public void testSeqNoCheckpoints() throws Exception {
191186
logger.info("allowing shards on all nodes");
192187
updateIndexSettings(index, Settings.builder().putNull("index.routing.allocation.include._name"));
193188
ensureGreen(index);
194-
assertOK(client().performRequest("POST", index + "/_refresh"));
189+
assertOK(client().performRequest(new Request("POST", index + "/_refresh")));
195190
for (final String bwcName : bwcNamesList) {
196191
assertCount(index, "_only_nodes:" + bwcName, numDocs);
197192
}
@@ -222,7 +217,7 @@ public void testSeqNoCheckpoints() throws Exception {
222217
logger.info("setting number of replicas to 1");
223218
updateIndexSettings(index, Settings.builder().put("index.number_of_replicas", 1));
224219
ensureGreen(index);
225-
assertOK(client().performRequest("POST", index + "/_refresh"));
220+
assertOK(client().performRequest(new Request("POST", index + "/_refresh")));
226221

227222
for (Shard shard : buildShards(index, nodes, newNodeClient)) {
228223
assertCount(index, "_only_nodes:" + shard.node.nodeName, numDocs);
@@ -237,20 +232,18 @@ public void testUpdateSnapshotStatus() throws Exception {
237232
logger.info("cluster discovered: {}", nodes.toString());
238233

239234
// Create the repository before taking the snapshot.
240-
String repoConfig = Strings
235+
Request request = new Request("PUT", "/_snapshot/repo");
236+
request.setJsonEntity(Strings
241237
.toString(JsonXContent.contentBuilder()
242238
.startObject()
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(
251-
client().performRequest("PUT", "/_snapshot/repo", emptyMap(),
252-
new StringEntity(repoConfig, ContentType.APPLICATION_JSON))
253-
);
239+
.field("type", "fs")
240+
.startObject("settings")
241+
.field("compress", randomBoolean())
242+
.field("location", System.getProperty("tests.path.repo"))
243+
.endObject()
244+
.endObject()));
245+
246+
assertOK(client().performRequest(request));
254247

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

@@ -264,34 +257,36 @@ public void testUpdateSnapshotStatus() throws Exception {
264257
createIndex(index, settings.build());
265258
indexDocs(index, 0, between(50, 100));
266259
ensureGreen(index);
267-
assertOK(client().performRequest("POST", index + "/_refresh"));
260+
assertOK(client().performRequest(new Request("POST", index + "/_refresh")));
268261

269-
assertOK(
270-
client().performRequest("PUT", "/_snapshot/repo/bwc-snapshot", singletonMap("wait_for_completion", "true"),
271-
new StringEntity("{\"indices\": \"" + index + "\"}", ContentType.APPLICATION_JSON))
272-
);
262+
request = new Request("PUT", "/_snapshot/repo/bwc-snapshot");
263+
request.addParameter("wait_for_completion", "true");
264+
request.setJsonEntity("{\"indices\": \"" + index + "\"}");
265+
assertOK(client().performRequest(request));
273266

274267
// Allocating shards on all nodes, taking snapshots should happen on all nodes.
275268
updateIndexSettings(index, Settings.builder().putNull("index.routing.allocation.include._name"));
276269
ensureGreen(index);
277-
assertOK(client().performRequest("POST", index + "/_refresh"));
270+
assertOK(client().performRequest(new Request("POST", index + "/_refresh")));
278271

279-
assertOK(
280-
client().performRequest("PUT", "/_snapshot/repo/mixed-snapshot", singletonMap("wait_for_completion", "true"),
281-
new StringEntity("{\"indices\": \"" + index + "\"}", ContentType.APPLICATION_JSON))
282-
);
272+
request = new Request("PUT", "/_snapshot/repo/mixed-snapshot");
273+
request.addParameter("wait_for_completion", "true");
274+
request.setJsonEntity("{\"indices\": \"" + index + "\"}");
283275
}
284276

285277
private void assertCount(final String index, final String preference, final int expectedCount) throws IOException {
286-
final Response response = client().performRequest("GET", index + "/_count", Collections.singletonMap("preference", preference));
278+
Request request = new Request("GET", index + "/_count");
279+
request.addParameter("preference", preference);
280+
final Response response = client().performRequest(request);
287281
assertOK(response);
288282
final int actualCount = Integer.parseInt(ObjectPath.createFromResponse(response).evaluate("count").toString());
289283
assertThat(actualCount, equalTo(expectedCount));
290284
}
291285

292286
private void assertVersion(final String index, final int docId, final String preference, final int expectedVersion) throws IOException {
293-
final Response response = client().performRequest("GET", index + "/test/" + docId,
294-
Collections.singletonMap("preference", preference));
287+
Request request = new Request("GET", index + "/test/" + docId);
288+
request.addParameter("preference", preference);
289+
final Response response = client().performRequest(request);
295290
assertOK(response);
296291
final int actualVersion = Integer.parseInt(ObjectPath.createFromResponse(response).evaluate("_version").toString());
297292
assertThat("version mismatch for doc [" + docId + "] preference [" + preference + "]", actualVersion, equalTo(expectedVersion));
@@ -323,7 +318,9 @@ private void assertSeqNoOnShards(String index, Nodes nodes, int numDocs, RestCli
323318
}
324319

325320
private List<Shard> buildShards(String index, Nodes nodes, RestClient client) throws IOException {
326-
Response response = client.performRequest("GET", index + "/_stats", singletonMap("level", "shards"));
321+
Request request = new Request("GET", index + "/_stats");
322+
request.addParameter("level", "shards");
323+
Response response = client.performRequest(request);
327324
List<Object> shardStats = ObjectPath.createFromResponse(response).evaluate("indices." + index + ".shards.0");
328325
ArrayList<Shard> shards = new ArrayList<>();
329326
for (Object shard : shardStats) {
@@ -341,7 +338,7 @@ private List<Shard> buildShards(String index, Nodes nodes, RestClient client) th
341338
}
342339

343340
private Nodes buildNodeAndVersions() throws IOException {
344-
Response response = client().performRequest("GET", "_nodes");
341+
Response response = client().performRequest(new Request("GET", "_nodes"));
345342
ObjectPath objectPath = ObjectPath.createFromResponse(response);
346343
Map<String, Object> nodesAsMap = objectPath.evaluate("nodes");
347344
Nodes nodes = new Nodes();
@@ -352,7 +349,7 @@ private Nodes buildNodeAndVersions() throws IOException {
352349
Version.fromString(objectPath.evaluate("nodes." + id + ".version")),
353350
HttpHost.create(objectPath.evaluate("nodes." + id + ".http.publish_address"))));
354351
}
355-
response = client().performRequest("GET", "_cluster/state");
352+
response = client().performRequest(new Request("GET", "_cluster/state"));
356353
nodes.setMasterNodeId(ObjectPath.createFromResponse(response).evaluate("master_node"));
357354
return nodes;
358355
}

0 commit comments

Comments
 (0)