Skip to content

Commit 1c60a32

Browse files
committed
Switch high level rest tests to new style requests (#31937)
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 `client/rest-high-level` project to use the new versions.
1 parent 8b174c4 commit 1c60a32

File tree

5 files changed

+194
-174
lines changed

5 files changed

+194
-174
lines changed

client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ public void testOpenExistingIndex() throws IOException {
631631
createIndex(index, Settings.EMPTY);
632632
closeIndex(index);
633633
ResponseException exception = expectThrows(ResponseException.class,
634-
() -> client().performRequest(HttpGet.METHOD_NAME, index + "/_search"));
634+
() -> client().performRequest(new Request(HttpGet.METHOD_NAME, index + "/_search")));
635635
assertThat(exception.getResponse().getStatusLine().getStatusCode(), equalTo(RestStatus.BAD_REQUEST.getStatus()));
636636
assertThat(exception.getMessage().contains(index), equalTo(true));
637637

@@ -642,7 +642,7 @@ public void testOpenExistingIndex() throws IOException {
642642
assertTrue(openIndexResponse.isAcknowledged());
643643
assertTrue(openIndexResponse.isShardsAcknowledged());
644644

645-
Response response = client().performRequest(HttpGet.METHOD_NAME, index + "/_search");
645+
Response response = client().performRequest(new Request(HttpGet.METHOD_NAME, index + "/_search"));
646646
assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus()));
647647
}
648648

@@ -674,7 +674,7 @@ public void testOpenNonExistentIndex() throws IOException {
674674
public void testCloseExistingIndex() throws IOException {
675675
String index = "index";
676676
createIndex(index, Settings.EMPTY);
677-
Response response = client().performRequest(HttpGet.METHOD_NAME, index + "/_search");
677+
Response response = client().performRequest(new Request(HttpGet.METHOD_NAME, index + "/_search"));
678678
assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus()));
679679

680680
CloseIndexRequest closeIndexRequest = new CloseIndexRequest(index);
@@ -684,7 +684,7 @@ public void testCloseExistingIndex() throws IOException {
684684
assertTrue(closeIndexResponse.isAcknowledged());
685685

686686
ResponseException exception = expectThrows(ResponseException.class,
687-
() -> client().performRequest(HttpGet.METHOD_NAME, index + "/_search"));
687+
() -> client().performRequest(new Request(HttpGet.METHOD_NAME, index + "/_search")));
688688
assertThat(exception.getResponse().getStatusLine().getStatusCode(), equalTo(RestStatus.BAD_REQUEST.getStatus()));
689689
assertThat(exception.getMessage().contains(index), equalTo(true));
690690
}
@@ -851,7 +851,7 @@ public void testExistsAlias() throws IOException {
851851
highLevelClient().indices()::existsAlias, highLevelClient().indices()::existsAliasAsync));
852852

853853
createIndex("index", Settings.EMPTY);
854-
client().performRequest(HttpPut.METHOD_NAME, "/index/_alias/alias");
854+
client().performRequest(new Request(HttpPut.METHOD_NAME, "/index/_alias/alias"));
855855
assertTrue(execute(getAliasesRequest, highLevelClient().indices()::existsAlias, highLevelClient().indices()::existsAliasAsync,
856856
highLevelClient().indices()::existsAlias, highLevelClient().indices()::existsAliasAsync));
857857

@@ -972,10 +972,10 @@ public void testRollover() throws IOException {
972972
public void testGetAlias() throws IOException {
973973
{
974974
createIndex("index1", Settings.EMPTY);
975-
client().performRequest(HttpPut.METHOD_NAME, "/index1/_alias/alias1");
975+
client().performRequest(new Request(HttpPut.METHOD_NAME, "/index1/_alias/alias1"));
976976

977977
createIndex("index2", Settings.EMPTY);
978-
client().performRequest(HttpPut.METHOD_NAME, "/index2/_alias/alias2");
978+
client().performRequest(new Request(HttpPut.METHOD_NAME, "/index2/_alias/alias2"));
979979

980980
createIndex("index3", Settings.EMPTY);
981981
}
@@ -1111,7 +1111,7 @@ public void testGetAliasesNonExistentIndexOrAlias() throws IOException {
11111111
assertThat(getAliasesResponse.getError(), equalTo("alias [" + alias + "] missing"));
11121112
}
11131113
createIndex(index, Settings.EMPTY);
1114-
client().performRequest(HttpPut.METHOD_NAME, index + "/_alias/" + alias);
1114+
client().performRequest(new Request(HttpPut.METHOD_NAME, index + "/_alias/" + alias));
11151115
{
11161116
GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices(index, "non_existent_index");
11171117
GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias,

client/rest-high-level/src/test/java/org/elasticsearch/client/PingAndInfoIT.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void testPing() throws IOException {
3939
public void testInfo() throws IOException {
4040
MainResponse info = highLevelClient().info(RequestOptions.DEFAULT);
4141
// compare with what the low level client outputs
42-
Map<String, Object> infoAsMap = entityAsMap(adminClient().performRequest(HttpGet.METHOD_NAME, "/"));
42+
Map<String, Object> infoAsMap = entityAsMap(adminClient().performRequest(new Request(HttpGet.METHOD_NAME, "/")));
4343
assertEquals(infoAsMap.get("cluster_name"), info.getClusterName().value());
4444
assertEquals(infoAsMap.get("cluster_uuid"), info.getClusterUuid());
4545

client/rest-high-level/src/test/java/org/elasticsearch/client/RankEvalIT.java

+17-22
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
package org.elasticsearch.client;
2121

22-
import org.apache.http.entity.ContentType;
23-
import org.apache.http.entity.StringEntity;
2422
import org.elasticsearch.action.search.SearchRequest;
2523
import org.elasticsearch.action.support.IndicesOptions;
2624
import org.elasticsearch.index.query.MatchAllQueryBuilder;
@@ -37,7 +35,6 @@
3735

3836
import java.io.IOException;
3937
import java.util.ArrayList;
40-
import java.util.Collections;
4138
import java.util.List;
4239
import java.util.Map;
4340
import java.util.stream.Collectors;
@@ -49,19 +46,17 @@ public class RankEvalIT extends ESRestHighLevelClientTestCase {
4946

5047
@Before
5148
public void indexDocuments() throws IOException {
52-
StringEntity doc = new StringEntity("{\"text\":\"berlin\"}", ContentType.APPLICATION_JSON);
53-
client().performRequest("PUT", "/index/doc/1", Collections.emptyMap(), doc);
54-
doc = new StringEntity("{\"text\":\"amsterdam\"}", ContentType.APPLICATION_JSON);
55-
client().performRequest("PUT", "/index/doc/2", Collections.emptyMap(), doc);
56-
client().performRequest("PUT", "/index/doc/3", Collections.emptyMap(), doc);
57-
client().performRequest("PUT", "/index/doc/4", Collections.emptyMap(), doc);
58-
client().performRequest("PUT", "/index/doc/5", Collections.emptyMap(), doc);
59-
client().performRequest("PUT", "/index/doc/6", Collections.emptyMap(), doc);
60-
client().performRequest("POST", "/index/_refresh");
61-
62-
// add another index to test basic multi index support
63-
client().performRequest("PUT", "/index2/doc/7", Collections.emptyMap(), doc);
64-
client().performRequest("POST", "/index2/_refresh");
49+
Request berlin = new Request("PUT", "/index/doc/berlin");
50+
berlin.setJsonEntity("{\"text\":\"berlin\"}");
51+
client().performRequest(berlin);
52+
for (int i = 0; i < 6; i++) {
53+
// add another index to test basic multi index support
54+
String index = i == 0 ? "index2" : "index";
55+
Request amsterdam = new Request("PUT", "/" + index + "/doc/amsterdam" + i);
56+
amsterdam.setJsonEntity("{\"text\":\"amsterdam\"}");
57+
client().performRequest(amsterdam);
58+
}
59+
client().performRequest(new Request("POST", "/_refresh"));
6560
}
6661

6762
/**
@@ -71,10 +66,10 @@ public void indexDocuments() throws IOException {
7166
public void testRankEvalRequest() throws IOException {
7267
SearchSourceBuilder testQuery = new SearchSourceBuilder();
7368
testQuery.query(new MatchAllQueryBuilder());
74-
List<RatedDocument> amsterdamRatedDocs = createRelevant("index" , "2", "3", "4", "5");
75-
amsterdamRatedDocs.addAll(createRelevant("index2", "7"));
69+
List<RatedDocument> amsterdamRatedDocs = createRelevant("index" , "amsterdam1", "amsterdam2", "amsterdam3", "amsterdam4");
70+
amsterdamRatedDocs.addAll(createRelevant("index2", "amsterdam0"));
7671
RatedRequest amsterdamRequest = new RatedRequest("amsterdam_query", amsterdamRatedDocs, testQuery);
77-
RatedRequest berlinRequest = new RatedRequest("berlin_query", createRelevant("index", "1"), testQuery);
72+
RatedRequest berlinRequest = new RatedRequest("berlin_query", createRelevant("index", "berlin"), testQuery);
7873
List<RatedRequest> specifications = new ArrayList<>();
7974
specifications.add(amsterdamRequest);
8075
specifications.add(berlinRequest);
@@ -95,7 +90,7 @@ public void testRankEvalRequest() throws IOException {
9590
assertEquals(7, hitsAndRatings.size());
9691
for (RatedSearchHit hit : hitsAndRatings) {
9792
String id = hit.getSearchHit().getId();
98-
if (id.equals("1") || id.equals("6")) {
93+
if (id.equals("berlin") || id.equals("amsterdam5")) {
9994
assertFalse(hit.getRating().isPresent());
10095
} else {
10196
assertEquals(1, hit.getRating().get().intValue());
@@ -107,15 +102,15 @@ public void testRankEvalRequest() throws IOException {
107102
assertEquals(7, hitsAndRatings.size());
108103
for (RatedSearchHit hit : hitsAndRatings) {
109104
String id = hit.getSearchHit().getId();
110-
if (id.equals("1")) {
105+
if (id.equals("berlin")) {
111106
assertEquals(1, hit.getRating().get().intValue());
112107
} else {
113108
assertFalse(hit.getRating().isPresent());
114109
}
115110
}
116111

117112
// now try this when test2 is closed
118-
client().performRequest("POST", "index2/_close", Collections.emptyMap());
113+
client().performRequest(new Request("POST", "index2/_close"));
119114
rankEvalRequest.indicesOptions(IndicesOptions.fromParameters(null, "true", null, SearchRequest.DEFAULT_INDICES_OPTIONS));
120115
response = execute(rankEvalRequest, highLevelClient()::rankEval, highLevelClient()::rankEvalAsync,
121116
highLevelClient()::rankEval, highLevelClient()::rankEvalAsync);

0 commit comments

Comments
 (0)