Skip to content

Commit d3d7309

Browse files
committed
Switch reindex tests to new style requests (#31941)
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 `modules/reindex` project to use the new versions.
1 parent 907a0fe commit d3d7309

File tree

2 files changed

+70
-67
lines changed

2 files changed

+70
-67
lines changed

modules/reindex/src/test/java/org/elasticsearch/index/reindex/ManyDocumentsIT.java

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

2020
package org.elasticsearch.index.reindex;
2121

22-
import org.apache.http.entity.ContentType;
23-
import org.apache.http.entity.StringEntity;
24-
import org.elasticsearch.client.Response;
25-
import org.elasticsearch.common.xcontent.XContentHelper;
26-
import org.elasticsearch.common.xcontent.json.JsonXContent;
22+
import org.elasticsearch.client.Request;
2723
import org.elasticsearch.test.rest.ESRestTestCase;
2824
import org.junit.Before;
2925

3026
import java.io.IOException;
3127
import java.util.Map;
3228

33-
import static java.util.Collections.emptyMap;
34-
import static java.util.Collections.singletonMap;
3529
import static org.hamcrest.Matchers.hasEntry;
3630

3731
/**
@@ -50,48 +44,69 @@ public void setupTestIndex() throws IOException {
5044
bulk.append("{\"index\":{}}\n");
5145
bulk.append("{\"test\":\"test\"}\n");
5246
}
53-
client().performRequest("POST", "/test/test/_bulk", singletonMap("refresh", "true"),
54-
new StringEntity(bulk.toString(), ContentType.APPLICATION_JSON));
47+
Request request = new Request("POST", "/test/test/_bulk");
48+
request.addParameter("refresh", "true");
49+
request.setJsonEntity(bulk.toString());
50+
client().performRequest(request);
5551
}
5652

5753
public void testReindex() throws IOException {
58-
Map<String, Object> response = toMap(client().performRequest("POST", "/_reindex", emptyMap(), new StringEntity(
59-
"{\"source\":{\"index\":\"test\"}, \"dest\":{\"index\":\"des\"}}",
60-
ContentType.APPLICATION_JSON)));
54+
Request request = new Request("POST", "/_reindex");
55+
request.setJsonEntity(
56+
"{\n" +
57+
" \"source\":{\n" +
58+
" \"index\":\"test\"\n" +
59+
" },\n" +
60+
" \"dest\":{\n" +
61+
" \"index\":\"des\"\n" +
62+
" }\n" +
63+
"}");
64+
Map<String, Object> response = entityAsMap(client().performRequest(request));
6165
assertThat(response, hasEntry("total", count));
6266
assertThat(response, hasEntry("created", count));
6367
}
6468

6569
public void testReindexFromRemote() throws IOException {
66-
Map<?, ?> nodesInfo = toMap(client().performRequest("GET", "/_nodes/http"));
70+
Map<?, ?> nodesInfo = entityAsMap(client().performRequest(new Request("GET", "/_nodes/http")));
6771
nodesInfo = (Map<?, ?>) nodesInfo.get("nodes");
6872
Map<?, ?> nodeInfo = (Map<?, ?>) nodesInfo.values().iterator().next();
6973
Map<?, ?> http = (Map<?, ?>) nodeInfo.get("http");
7074
String remote = "http://"+ http.get("publish_address");
71-
Map<String, Object> response = toMap(client().performRequest("POST", "/_reindex", emptyMap(), new StringEntity(
72-
"{\"source\":{\"index\":\"test\",\"remote\":{\"host\":\"" + remote + "\"}}, \"dest\":{\"index\":\"des\"}}",
73-
ContentType.APPLICATION_JSON)));
75+
Request request = new Request("POST", "/_reindex");
76+
request.setJsonEntity(
77+
"{\n" +
78+
" \"source\":{\n" +
79+
" \"index\":\"test\",\n" +
80+
" \"remote\":{\n" +
81+
" \"host\":\"" + remote + "\"\n" +
82+
" }\n" +
83+
" }\n," +
84+
" \"dest\":{\n" +
85+
" \"index\":\"des\"\n" +
86+
" }\n" +
87+
"}");
88+
Map<String, Object> response = entityAsMap(client().performRequest(request));
7489
assertThat(response, hasEntry("total", count));
7590
assertThat(response, hasEntry("created", count));
7691
}
7792

7893

7994
public void testUpdateByQuery() throws IOException {
80-
Map<String, Object> response = toMap(client().performRequest("POST", "/test/_update_by_query"));
95+
Map<String, Object> response = entityAsMap(client().performRequest(new Request("POST", "/test/_update_by_query")));
8196
assertThat(response, hasEntry("total", count));
8297
assertThat(response, hasEntry("updated", count));
8398
}
8499

85100
public void testDeleteByQuery() throws IOException {
86-
Map<String, Object> response = toMap(client().performRequest("POST", "/test/_delete_by_query", emptyMap(), new StringEntity(
87-
"{\"query\":{\"match_all\":{}}}",
88-
ContentType.APPLICATION_JSON)));
101+
Request request = new Request("POST", "/test/_delete_by_query");
102+
request.setJsonEntity(
103+
"{\n" +
104+
" \"query\":{\n" +
105+
" \"match_all\": {}\n" +
106+
" }\n" +
107+
"}");
108+
Map<String, Object> response = entityAsMap(client().performRequest(request));
89109
assertThat(response, hasEntry("total", count));
90110
assertThat(response, hasEntry("deleted", count));
91111
}
92-
93-
static Map<String, Object> toMap(Response response) throws IOException {
94-
return XContentHelper.convertToMap(JsonXContent.jsonXContent, response.getEntity().getContent(), false);
95-
}
96-
97112
}

modules/reindex/src/test/java/org/elasticsearch/index/reindex/remote/ReindexFromOldRemoteIT.java

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,43 +19,44 @@
1919

2020
package org.elasticsearch.index.reindex.remote;
2121

22-
import org.apache.http.HttpEntity;
2322
import org.apache.http.HttpHost;
24-
import org.apache.http.entity.ContentType;
25-
import org.apache.http.entity.StringEntity;
2623
import org.apache.http.util.EntityUtils;
24+
import org.elasticsearch.client.Request;
2725
import org.elasticsearch.client.Response;
28-
import org.elasticsearch.client.ResponseException;
2926
import org.elasticsearch.client.RestClient;
3027
import org.elasticsearch.common.Booleans;
3128
import org.elasticsearch.test.rest.ESRestTestCase;
3229

3330
import java.io.IOException;
34-
import java.util.Map;
35-
import java.util.TreeMap;
3631

37-
import static java.util.Collections.singletonMap;
3832
import static org.hamcrest.Matchers.containsString;
3933

4034
public class ReindexFromOldRemoteIT extends ESRestTestCase {
35+
/**
36+
* Number of documents to test when reindexing from an old version.
37+
*/
38+
private static final int DOCS = 5;
39+
4140
private void oldEsTestCase(String portPropertyName, String requestsPerSecond) throws IOException {
4241
boolean enabled = Booleans.parseBoolean(System.getProperty("tests.fromOld"));
4342
assumeTrue("test is disabled, probably because this is windows", enabled);
4443

4544
int oldEsPort = Integer.parseInt(System.getProperty(portPropertyName));
4645
try (RestClient oldEs = RestClient.builder(new HttpHost("127.0.0.1", oldEsPort)).build()) {
4746
try {
48-
HttpEntity entity = new StringEntity("{\"settings\":{\"number_of_shards\": 1}}", ContentType.APPLICATION_JSON);
49-
oldEs.performRequest("PUT", "/test", singletonMap("refresh", "true"), entity);
50-
51-
entity = new StringEntity("{\"test\":\"test\"}", ContentType.APPLICATION_JSON);
52-
oldEs.performRequest("PUT", "/test/doc/testdoc1", singletonMap("refresh", "true"), entity);
53-
oldEs.performRequest("PUT", "/test/doc/testdoc2", singletonMap("refresh", "true"), entity);
54-
oldEs.performRequest("PUT", "/test/doc/testdoc3", singletonMap("refresh", "true"), entity);
55-
oldEs.performRequest("PUT", "/test/doc/testdoc4", singletonMap("refresh", "true"), entity);
56-
oldEs.performRequest("PUT", "/test/doc/testdoc5", singletonMap("refresh", "true"), entity);
47+
Request createIndex = new Request("PUT", "/test");
48+
createIndex.setJsonEntity("{\"settings\":{\"number_of_shards\": 1}}");
49+
oldEs.performRequest(createIndex);
50+
51+
for (int i = 0; i < DOCS; i++) {
52+
Request doc = new Request("PUT", "/test/doc/testdoc" + i);
53+
doc.addParameter("refresh", "true");
54+
doc.setJsonEntity("{\"test\":\"test\"}");
55+
oldEs.performRequest(doc);
56+
}
5757

58-
entity = new StringEntity(
58+
Request reindex = new Request("POST", "/_reindex");
59+
reindex.setJsonEntity(
5960
"{\n"
6061
+ " \"source\":{\n"
6162
+ " \"index\": \"test\",\n"
@@ -67,36 +68,23 @@ private void oldEsTestCase(String portPropertyName, String requestsPerSecond) th
6768
+ " \"dest\": {\n"
6869
+ " \"index\": \"test\"\n"
6970
+ " }\n"
70-
+ "}",
71-
ContentType.APPLICATION_JSON);
72-
Map<String, String> params = new TreeMap<>();
73-
params.put("refresh", "true");
74-
params.put("pretty", "true");
71+
+ "}");
72+
reindex.addParameter("refresh", "true");
73+
reindex.addParameter("pretty", "true");
7574
if (requestsPerSecond != null) {
76-
params.put("requests_per_second", requestsPerSecond);
75+
reindex.addParameter("requests_per_second", requestsPerSecond);
7776
}
78-
client().performRequest("POST", "/_reindex", params, entity);
77+
client().performRequest(reindex);
7978

80-
Response response = client().performRequest("POST", "test/_search", singletonMap("pretty", "true"));
79+
Request search = new Request("POST", "/test/_search");
80+
search.addParameter("pretty", "true");
81+
Response response = client().performRequest(search);
8182
String result = EntityUtils.toString(response.getEntity());
82-
assertThat(result, containsString("\"_id\" : \"testdoc1\""));
83-
} finally {
84-
try {
85-
oldEs.performRequest("DELETE", "/test");
86-
} catch (ResponseException e) {
87-
/* Try not to throw ResponseException for as it'll eat the
88-
* real exception. This is because the rest client throws
89-
* exceptions in a "funny" way that isn't compatible with
90-
* `suppressed`. In the case of 404s we'll just log something
91-
* and move on because that just means that a previous
92-
* failure caused the index not to be created. */
93-
if (e.getResponse().getStatusLine().getStatusCode() == 404) {
94-
logger.warn("old index not deleted because it doesn't exist");
95-
} else {
96-
logger.error("failed to remove old index", e);
97-
fail("failed to remove old index, see log");
98-
}
83+
for (int i = 0; i < DOCS; i++) {
84+
assertThat(result, containsString("\"_id\" : \"testdoc" + i + "\""));
9985
}
86+
} finally {
87+
oldEs.performRequest(new Request("DELETE", "/test"));
10088
}
10189
}
10290
}

0 commit comments

Comments
 (0)