Skip to content

Commit aa6a1c5

Browse files
authored
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 d268b49 commit aa6a1c5

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
@@ -612,7 +612,7 @@ public void testOpenExistingIndex() throws IOException {
612612
createIndex(index, Settings.EMPTY);
613613
closeIndex(index);
614614
ResponseException exception = expectThrows(ResponseException.class,
615-
() -> client().performRequest(HttpGet.METHOD_NAME, index + "/_search"));
615+
() -> client().performRequest(new Request(HttpGet.METHOD_NAME, index + "/_search")));
616616
assertThat(exception.getResponse().getStatusLine().getStatusCode(), equalTo(RestStatus.BAD_REQUEST.getStatus()));
617617
assertThat(exception.getMessage().contains(index), equalTo(true));
618618

@@ -621,7 +621,7 @@ public void testOpenExistingIndex() throws IOException {
621621
highLevelClient().indices()::openAsync);
622622
assertTrue(openIndexResponse.isAcknowledged());
623623

624-
Response response = client().performRequest(HttpGet.METHOD_NAME, index + "/_search");
624+
Response response = client().performRequest(new Request(HttpGet.METHOD_NAME, index + "/_search"));
625625
assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus()));
626626
}
627627

@@ -650,7 +650,7 @@ public void testOpenNonExistentIndex() throws IOException {
650650
public void testCloseExistingIndex() throws IOException {
651651
String index = "index";
652652
createIndex(index, Settings.EMPTY);
653-
Response response = client().performRequest(HttpGet.METHOD_NAME, index + "/_search");
653+
Response response = client().performRequest(new Request(HttpGet.METHOD_NAME, index + "/_search"));
654654
assertThat(response.getStatusLine().getStatusCode(), equalTo(RestStatus.OK.getStatus()));
655655

656656
CloseIndexRequest closeIndexRequest = new CloseIndexRequest(index);
@@ -659,7 +659,7 @@ public void testCloseExistingIndex() throws IOException {
659659
assertTrue(closeIndexResponse.isAcknowledged());
660660

661661
ResponseException exception = expectThrows(ResponseException.class,
662-
() -> client().performRequest(HttpGet.METHOD_NAME, index + "/_search"));
662+
() -> client().performRequest(new Request(HttpGet.METHOD_NAME, index + "/_search")));
663663
assertThat(exception.getResponse().getStatusLine().getStatusCode(), equalTo(RestStatus.BAD_REQUEST.getStatus()));
664664
assertThat(exception.getMessage().contains(index), equalTo(true));
665665
}
@@ -817,7 +817,7 @@ public void testExistsAlias() throws IOException {
817817
assertFalse(execute(getAliasesRequest, highLevelClient().indices()::existsAlias, highLevelClient().indices()::existsAliasAsync));
818818

819819
createIndex("index", Settings.EMPTY);
820-
client().performRequest(HttpPut.METHOD_NAME, "/index/_alias/alias");
820+
client().performRequest(new Request(HttpPut.METHOD_NAME, "/index/_alias/alias"));
821821
assertTrue(execute(getAliasesRequest, highLevelClient().indices()::existsAlias, highLevelClient().indices()::existsAliasAsync));
822822

823823
GetAliasesRequest getAliasesRequest2 = new GetAliasesRequest();
@@ -936,10 +936,10 @@ public void testRollover() throws IOException {
936936
public void testGetAlias() throws IOException {
937937
{
938938
createIndex("index1", Settings.EMPTY);
939-
client().performRequest(HttpPut.METHOD_NAME, "/index1/_alias/alias1");
939+
client().performRequest(new Request(HttpPut.METHOD_NAME, "/index1/_alias/alias1"));
940940

941941
createIndex("index2", Settings.EMPTY);
942-
client().performRequest(HttpPut.METHOD_NAME, "/index2/_alias/alias2");
942+
client().performRequest(new Request(HttpPut.METHOD_NAME, "/index2/_alias/alias2"));
943943

944944
createIndex("index3", Settings.EMPTY);
945945
}
@@ -1075,7 +1075,7 @@ public void testGetAliasesNonExistentIndexOrAlias() throws IOException {
10751075
assertThat(getAliasesResponse.getError(), equalTo("alias [" + alias + "] missing"));
10761076
}
10771077
createIndex(index, Settings.EMPTY);
1078-
client().performRequest(HttpPut.METHOD_NAME, index + "/_alias/" + alias);
1078+
client().performRequest(new Request(HttpPut.METHOD_NAME, index + "/_alias/" + alias));
10791079
{
10801080
GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices(index, "non_existent_index");
10811081
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);
@@ -94,7 +89,7 @@ public void testRankEvalRequest() throws IOException {
9489
assertEquals(7, hitsAndRatings.size());
9590
for (RatedSearchHit hit : hitsAndRatings) {
9691
String id = hit.getSearchHit().getId();
97-
if (id.equals("1") || id.equals("6")) {
92+
if (id.equals("berlin") || id.equals("amsterdam5")) {
9893
assertFalse(hit.getRating().isPresent());
9994
} else {
10095
assertEquals(1, hit.getRating().get().intValue());
@@ -106,15 +101,15 @@ public void testRankEvalRequest() throws IOException {
106101
assertEquals(7, hitsAndRatings.size());
107102
for (RatedSearchHit hit : hitsAndRatings) {
108103
String id = hit.getSearchHit().getId();
109-
if (id.equals("1")) {
104+
if (id.equals("berlin")) {
110105
assertEquals(1, hit.getRating().get().intValue());
111106
} else {
112107
assertFalse(hit.getRating().isPresent());
113108
}
114109
}
115110

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

0 commit comments

Comments
 (0)