Switch distribution to new style Requests (#30595)

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 `distribution/archives/integ-test-zip` project
to use the new versions.
This commit is contained in:
Nik Everett 2018-07-17 20:25:27 -04:00 committed by GitHub
parent 91d8371325
commit 351bbb8906
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 74 additions and 73 deletions

View File

@ -19,14 +19,11 @@
package org.elasticsearch.test.rest; package org.elasticsearch.test.rest;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.elasticsearch.client.Request; import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response; import org.elasticsearch.client.Response;
import java.io.IOException; import java.io.IOException;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap; import static java.util.Collections.singletonMap;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.startsWith; import static org.hamcrest.Matchers.startsWith;
@ -49,26 +46,31 @@ public class CreatedLocationHeaderIT extends ESRestTestCase {
} }
public void testUpsert() throws IOException { public void testUpsert() throws IOException {
locationTestCase(client().performRequest("POST", "test/test/1/_update", emptyMap(), new StringEntity("{" Request request = new Request("POST", "test/test/1/_update");
request.setJsonEntity("{"
+ "\"doc\": {\"test\": \"test\"}," + "\"doc\": {\"test\": \"test\"},"
+ "\"doc_as_upsert\": true}", ContentType.APPLICATION_JSON))); + "\"doc_as_upsert\": true}");
locationTestCase(client().performRequest(request));
} }
private void locationTestCase(String method, String url) throws IOException { private void locationTestCase(String method, String url) throws IOException {
locationTestCase(client().performRequest(method, url, emptyMap(), final Request request = new Request(method, url);
new StringEntity("{\"test\": \"test\"}", ContentType.APPLICATION_JSON))); request.setJsonEntity("{\"test\": \"test\"}");
locationTestCase(client().performRequest(request));
// we have to delete the index otherwise the second indexing request will route to the single shard and not produce a 201 // we have to delete the index otherwise the second indexing request will route to the single shard and not produce a 201
final Response response = client().performRequest(new Request("DELETE", "test")); final Response response = client().performRequest(new Request("DELETE", "test"));
assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
locationTestCase(client().performRequest(method, url + "?routing=cat", emptyMap(), final Request withRouting = new Request(method, url);
new StringEntity("{\"test\": \"test\"}", ContentType.APPLICATION_JSON))); withRouting.addParameter("routing", "cat");
withRouting.setJsonEntity("{\"test\": \"test\"}");
locationTestCase(client().performRequest(withRouting));
} }
private void locationTestCase(Response response) throws IOException { private void locationTestCase(Response response) throws IOException {
assertEquals(201, response.getStatusLine().getStatusCode()); assertEquals(201, response.getStatusLine().getStatusCode());
String location = response.getHeader("Location"); String location = response.getHeader("Location");
assertThat(location, startsWith("/test/test/")); assertThat(location, startsWith("/test/test/"));
Response getResponse = client().performRequest("GET", location); Response getResponse = client().performRequest(new Request("GET", location));
assertEquals(singletonMap("test", "test"), entityAsMap(getResponse).get("_source")); assertEquals(singletonMap("test", "test"), entityAsMap(getResponse).get("_source"));
} }

View File

@ -19,13 +19,11 @@
package org.elasticsearch.test.rest; package org.elasticsearch.test.rest;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.elasticsearch.client.Response; import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException; import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.Request;
import java.io.IOException; import java.io.IOException;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -39,8 +37,8 @@ public class NodeRestUsageIT extends ESRestTestCase {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testWithRestUsage() throws IOException { public void testWithRestUsage() throws IOException {
// First get the current usage figures // First get the current usage figures
Response beforeResponse = client().performRequest("GET", String path = randomFrom("_nodes/usage", "_nodes/usage/rest_actions", "_nodes/usage/_all");
randomFrom("_nodes/usage", "_nodes/usage/rest_actions", "_nodes/usage/_all")); Response beforeResponse = client().performRequest(new Request("GET", path));
Map<String, Object> beforeResponseBodyMap = entityAsMap(beforeResponse); Map<String, Object> beforeResponseBodyMap = entityAsMap(beforeResponse);
assertThat(beforeResponseBodyMap, notNullValue()); assertThat(beforeResponseBodyMap, notNullValue());
Map<String, Object> before_nodesMap = (Map<String, Object>) beforeResponseBodyMap.get("_nodes"); Map<String, Object> before_nodesMap = (Map<String, Object>) beforeResponseBodyMap.get("_nodes");
@ -80,24 +78,24 @@ public class NodeRestUsageIT extends ESRestTestCase {
} }
// Do some requests to get some rest usage stats // Do some requests to get some rest usage stats
client().performRequest("PUT", "/test"); client().performRequest(new Request("PUT", "/test"));
client().performRequest("POST", "/test/doc/1", Collections.emptyMap(), for (int i = 0; i < 3; i++) {
new StringEntity("{ \"foo\": \"bar\"}", ContentType.APPLICATION_JSON)); final Request index = new Request("POST", "/test/doc/1");
client().performRequest("POST", "/test/doc/2", Collections.emptyMap(), index.setJsonEntity("{\"foo\": \"bar\"}");
new StringEntity("{ \"foo\": \"bar\"}", ContentType.APPLICATION_JSON)); client().performRequest(index);
client().performRequest("POST", "/test/doc/3", Collections.emptyMap(), }
new StringEntity("{ \"foo\": \"bar\"}", ContentType.APPLICATION_JSON)); client().performRequest(new Request("GET", "/test/_search"));
client().performRequest("GET", "/test/_search"); final Request index4 = new Request("POST", "/test/doc/4");
client().performRequest("POST", "/test/doc/4", Collections.emptyMap(), index4.setJsonEntity("{\"foo\": \"bar\"}");
new StringEntity("{ \"foo\": \"bar\"}", ContentType.APPLICATION_JSON)); client().performRequest(index4);
client().performRequest("POST", "/test/_refresh"); client().performRequest(new Request("POST", "/test/_refresh"));
client().performRequest("GET", "/_cat/indices"); client().performRequest(new Request("GET", "/_cat/indices"));
client().performRequest("GET", "/_nodes"); client().performRequest(new Request("GET", "/_nodes"));
client().performRequest("GET", "/test/_search"); client().performRequest(new Request("GET", "/test/_search"));
client().performRequest("GET", "/_nodes/stats"); client().performRequest(new Request("GET", "/_nodes/stats"));
client().performRequest("DELETE", "/test"); client().performRequest(new Request("DELETE", "/test"));
Response response = client().performRequest("GET", "_nodes/usage"); Response response = client().performRequest(new Request("GET", "_nodes/usage"));
Map<String, Object> responseBodyMap = entityAsMap(response); Map<String, Object> responseBodyMap = entityAsMap(response);
assertThat(responseBodyMap, notNullValue()); assertThat(responseBodyMap, notNullValue());
Map<String, Object> _nodesMap = (Map<String, Object>) responseBodyMap.get("_nodes"); Map<String, Object> _nodesMap = (Map<String, Object>) responseBodyMap.get("_nodes");
@ -139,7 +137,7 @@ public class NodeRestUsageIT extends ESRestTestCase {
public void testMetricsWithAll() throws IOException { public void testMetricsWithAll() throws IOException {
ResponseException exception = expectThrows(ResponseException.class, ResponseException exception = expectThrows(ResponseException.class,
() -> client().performRequest("GET", "_nodes/usage/_all,rest_actions")); () -> client().performRequest(new Request("GET", "_nodes/usage/_all,rest_actions")));
assertNotNull(exception); assertNotNull(exception);
assertThat(exception.getMessage(), containsString("\"type\":\"illegal_argument_exception\"," assertThat(exception.getMessage(), containsString("\"type\":\"illegal_argument_exception\","
+ "\"reason\":\"request [_nodes/usage/_all,rest_actions] contains _all and individual metrics [_all,rest_actions]\"")); + "\"reason\":\"request [_nodes/usage/_all,rest_actions] contains _all and individual metrics [_all,rest_actions]\""));

View File

@ -20,6 +20,7 @@
package org.elasticsearch.test.rest; package org.elasticsearch.test.rest;
import org.elasticsearch.client.ResponseException; import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.Request;
import java.io.IOException; import java.io.IOException;
@ -28,56 +29,56 @@ import static org.hamcrest.CoreMatchers.containsString;
public class RequestsWithoutContentIT extends ESRestTestCase { public class RequestsWithoutContentIT extends ESRestTestCase {
public void testIndexMissingBody() throws IOException { public void testIndexMissingBody() throws IOException {
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest( ResponseException responseException = expectThrows(ResponseException.class, () ->
randomBoolean() ? "POST" : "PUT", "/idx/type/123")); client().performRequest(new Request(randomBoolean() ? "POST" : "PUT", "/idx/type/123")));
assertResponseException(responseException, "request body is required"); assertResponseException(responseException, "request body is required");
} }
public void testBulkMissingBody() throws IOException { public void testBulkMissingBody() throws IOException {
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest( ResponseException responseException = expectThrows(ResponseException.class, () ->
randomBoolean() ? "POST" : "PUT", "/_bulk")); client().performRequest(new Request(randomBoolean() ? "POST" : "PUT", "/_bulk")));
assertResponseException(responseException, "request body is required"); assertResponseException(responseException, "request body is required");
} }
public void testPutSettingsMissingBody() throws IOException { public void testPutSettingsMissingBody() throws IOException {
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest( ResponseException responseException = expectThrows(ResponseException.class, () ->
"PUT", "/_settings")); client().performRequest(new Request("PUT", "/_settings")));
assertResponseException(responseException, "request body is required"); assertResponseException(responseException, "request body is required");
} }
public void testPutMappingsMissingBody() throws IOException { public void testPutMappingsMissingBody() throws IOException {
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest( ResponseException responseException = expectThrows(ResponseException.class, () ->
randomBoolean() ? "POST" : "PUT", "/test_index/test_type/_mapping")); client().performRequest(new Request(randomBoolean() ? "POST" : "PUT", "/test_index/test_type/_mapping")));
assertResponseException(responseException, "request body is required"); assertResponseException(responseException, "request body is required");
} }
public void testPutIndexTemplateMissingBody() throws IOException { public void testPutIndexTemplateMissingBody() throws IOException {
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest( ResponseException responseException = expectThrows(ResponseException.class, () ->
randomBoolean() ? "PUT" : "POST", "/_template/my_template")); client().performRequest(new Request(randomBoolean() ? "PUT" : "POST", "/_template/my_template")));
assertResponseException(responseException, "request body is required"); assertResponseException(responseException, "request body is required");
} }
public void testMultiSearchMissingBody() throws IOException { public void testMultiSearchMissingBody() throws IOException {
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest( ResponseException responseException = expectThrows(ResponseException.class, () ->
randomBoolean() ? "POST" : "GET", "/_msearch")); client().performRequest(new Request(randomBoolean() ? "POST" : "GET", "/_msearch")));
assertResponseException(responseException, "request body or source parameter is required"); assertResponseException(responseException, "request body or source parameter is required");
} }
public void testPutPipelineMissingBody() throws IOException { public void testPutPipelineMissingBody() throws IOException {
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest( ResponseException responseException = expectThrows(ResponseException.class, () ->
"PUT", "/_ingest/pipeline/my_pipeline")); client().performRequest(new Request("PUT", "/_ingest/pipeline/my_pipeline")));
assertResponseException(responseException, "request body or source parameter is required"); assertResponseException(responseException, "request body or source parameter is required");
} }
public void testSimulatePipelineMissingBody() throws IOException { public void testSimulatePipelineMissingBody() throws IOException {
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest( ResponseException responseException = expectThrows(ResponseException.class, () ->
randomBoolean() ? "POST" : "GET", "/_ingest/pipeline/my_pipeline/_simulate")); client().performRequest(new Request(randomBoolean() ? "POST" : "GET", "/_ingest/pipeline/my_pipeline/_simulate")));
assertResponseException(responseException, "request body or source parameter is required"); assertResponseException(responseException, "request body or source parameter is required");
} }
public void testPutScriptMissingBody() throws IOException { public void testPutScriptMissingBody() throws IOException {
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest( ResponseException responseException = expectThrows(ResponseException.class, () ->
randomBoolean() ? "POST" : "PUT", "/_scripts/lang")); client().performRequest(new Request(randomBoolean() ? "POST" : "PUT", "/_scripts/lang")));
assertResponseException(responseException, "request body is required"); assertResponseException(responseException, "request body is required");
} }

View File

@ -19,26 +19,21 @@
package org.elasticsearch.test.rest; package org.elasticsearch.test.rest;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.elasticsearch.action.ActionFuture; import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.action.support.PlainActionFuture;
import org.elasticsearch.client.Response; import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException; import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.ResponseListener; import org.elasticsearch.client.ResponseListener;
import org.elasticsearch.client.Request;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import static java.util.Collections.emptyMap;
/** /**
* Tests that wait for refresh is fired if the index is closed. * Tests that wait for refresh is fired if the index is closed.
*/ */
@ -46,13 +41,14 @@ public class WaitForRefreshAndCloseTests extends ESRestTestCase {
@Before @Before
public void setupIndex() throws IOException { public void setupIndex() throws IOException {
try { try {
client().performRequest("DELETE", indexName()); client().performRequest(new Request("DELETE", indexName()));
} catch (ResponseException e) { } catch (ResponseException e) {
// If we get an error, it should be because the index doesn't exist // If we get an error, it should be because the index doesn't exist
assertEquals(404, e.getResponse().getStatusLine().getStatusCode()); assertEquals(404, e.getResponse().getStatusLine().getStatusCode());
} }
client().performRequest("PUT", indexName(), emptyMap(), Request request = new Request("PUT", indexName());
new StringEntity("{\"settings\":{\"refresh_interval\":-1}}", ContentType.APPLICATION_JSON)); request.setJsonEntity("{\"settings\":{\"refresh_interval\":-1}}");
client().performRequest(request);
} }
@After @After
@ -69,17 +65,20 @@ public class WaitForRefreshAndCloseTests extends ESRestTestCase {
} }
public void testIndexAndThenClose() throws Exception { public void testIndexAndThenClose() throws Exception {
closeWhileListenerEngaged(start("PUT", "", new StringEntity("{\"test\":\"test\"}", ContentType.APPLICATION_JSON))); closeWhileListenerEngaged(start("PUT", "", "{\"test\":\"test\"}"));
} }
public void testUpdateAndThenClose() throws Exception { public void testUpdateAndThenClose() throws Exception {
client().performRequest("PUT", docPath(), emptyMap(), new StringEntity("{\"test\":\"test\"}", ContentType.APPLICATION_JSON)); Request request = new Request("PUT", docPath());
closeWhileListenerEngaged(start("POST", "/_update", request.setJsonEntity("{\"test\":\"test\"}");
new StringEntity("{\"doc\":{\"name\":\"test\"}}", ContentType.APPLICATION_JSON))); client().performRequest(request);
closeWhileListenerEngaged(start("POST", "/_update", "{\"doc\":{\"name\":\"test\"}}"));
} }
public void testDeleteAndThenClose() throws Exception { public void testDeleteAndThenClose() throws Exception {
client().performRequest("PUT", docPath(), emptyMap(), new StringEntity("{\"test\":\"test\"}", ContentType.APPLICATION_JSON)); Request request = new Request("PUT", docPath());
request.setJsonEntity("{\"test\":\"test\"}");
client().performRequest(request);
closeWhileListenerEngaged(start("DELETE", "", null)); closeWhileListenerEngaged(start("DELETE", "", null));
} }
@ -88,7 +87,7 @@ public class WaitForRefreshAndCloseTests extends ESRestTestCase {
assertBusy(() -> { assertBusy(() -> {
Map<String, Object> stats; Map<String, Object> stats;
try { try {
stats = entityAsMap(client().performRequest("GET", indexName() + "/_stats/refresh")); stats = entityAsMap(client().performRequest(new Request("GET", indexName() + "/_stats/refresh")));
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -105,18 +104,19 @@ public class WaitForRefreshAndCloseTests extends ESRestTestCase {
}); });
// Close the index. That should flush the listener. // Close the index. That should flush the listener.
client().performRequest("POST", indexName() + "/_close"); client().performRequest(new Request("POST", indexName() + "/_close"));
// The request shouldn't fail. It certainly shouldn't hang. // The request shouldn't fail. It certainly shouldn't hang.
future.get(); future.get();
} }
private ActionFuture<String> start(String method, String path, HttpEntity body) { private ActionFuture<String> start(String method, String path, String body) {
PlainActionFuture<String> future = new PlainActionFuture<>(); PlainActionFuture<String> future = new PlainActionFuture<>();
Map<String, String> params = new HashMap<>(); Request request = new Request(method, docPath() + path);
params.put("refresh", "wait_for"); request.addParameter("refresh", "wait_for");
params.put("error_trace", ""); request.addParameter("error_trace", "");
client().performRequestAsync(method, docPath() + path, params, body, new ResponseListener() { request.setJsonEntity(body);
client().performRequestAsync(request, new ResponseListener() {
@Override @Override
public void onSuccess(Response response) { public void onSuccess(Response response) {
try { try {