Switch non-x-pack to new style requests (#32106)

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 most of the calls not in X-Pack to their new versions.
This commit is contained in:
Nik Everett 2018-07-16 17:44:19 -04:00 committed by GitHub
parent 791b9b147c
commit d596447f3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 33 deletions

View File

@ -19,7 +19,8 @@
package org.elasticsearch.rest; package org.elasticsearch.rest;
import org.apache.http.message.BasicHeader; import org.elasticsearch.client.Request;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.Response; import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException; import org.elasticsearch.client.ResponseException;
import org.elasticsearch.common.settings.Setting; import org.elasticsearch.common.settings.Setting;
@ -43,7 +44,7 @@ import static org.hamcrest.Matchers.hasToString;
public class Netty4BadRequestIT extends ESRestTestCase { public class Netty4BadRequestIT extends ESRestTestCase {
public void testBadRequest() throws IOException { public void testBadRequest() throws IOException {
final Response response = client().performRequest("GET", "/_nodes/settings", Collections.emptyMap()); final Response response = client().performRequest(new Request("GET", "/_nodes/settings"));
final ObjectPath objectPath = ObjectPath.createFromResponse(response); final ObjectPath objectPath = ObjectPath.createFromResponse(response);
final Map<String, Object> map = objectPath.evaluate("nodes"); final Map<String, Object> map = objectPath.evaluate("nodes");
int maxMaxInitialLineLength = Integer.MIN_VALUE; int maxMaxInitialLineLength = Integer.MIN_VALUE;
@ -77,9 +78,9 @@ public class Netty4BadRequestIT extends ESRestTestCase {
} }
public void testInvalidParameterValue() throws IOException { public void testInvalidParameterValue() throws IOException {
final ResponseException e = expectThrows( final Request request = new Request("GET", "/_cluster/settings");
ResponseException.class, request.addParameter("pretty", "neither-true-nor-false");
() -> client().performRequest("GET", "/_cluster/settings", Collections.singletonMap("pretty", "neither-true-nor-false"))); final ResponseException e = expectThrows(ResponseException.class, () -> client().performRequest(request));
final Response response = e.getResponse(); final Response response = e.getResponse();
assertThat(response.getStatusLine().getStatusCode(), equalTo(400)); assertThat(response.getStatusLine().getStatusCode(), equalTo(400));
final ObjectPath objectPath = ObjectPath.createFromResponse(response); final ObjectPath objectPath = ObjectPath.createFromResponse(response);
@ -89,9 +90,11 @@ public class Netty4BadRequestIT extends ESRestTestCase {
} }
public void testInvalidHeaderValue() throws IOException { public void testInvalidHeaderValue() throws IOException {
final BasicHeader header = new BasicHeader("Content-Type", "\t"); final Request request = new Request("GET", "/_cluster/settings");
final ResponseException e = final RequestOptions.Builder options = request.getOptions().toBuilder();
expectThrows(ResponseException.class, () -> client().performRequest("GET", "/_cluster/settings", header)); options.addHeader("Content-Type", "\t");
request.setOptions(options);
final ResponseException e = expectThrows(ResponseException.class, () -> client().performRequest(request));
final Response response = e.getResponse(); final Response response = e.getResponse();
assertThat(response.getStatusLine().getStatusCode(), equalTo(400)); assertThat(response.getStatusLine().getStatusCode(), equalTo(400));
final ObjectPath objectPath = ObjectPath.createFromResponse(response); final ObjectPath objectPath = ObjectPath.createFromResponse(response);

View File

@ -19,8 +19,7 @@
package org.elasticsearch.rest; package org.elasticsearch.rest;
import org.apache.http.entity.ContentType; import org.elasticsearch.client.Request;
import org.apache.http.entity.StringEntity;
import org.elasticsearch.client.Response; import org.elasticsearch.client.Response;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
@ -57,8 +56,9 @@ public class Netty4HeadBodyIsEmptyIT extends ESRestTestCase {
builder.field("test", "test"); builder.field("test", "test");
} }
builder.endObject(); builder.endObject();
client().performRequest("PUT", "/" + indexName + "/" + typeName + "/" + "1", emptyMap(), Request request = new Request("PUT", "/" + indexName + "/" + typeName + "/" + "1");
new StringEntity(Strings.toString(builder), ContentType.APPLICATION_JSON)); request.setJsonEntity(Strings.toString(builder));
client().performRequest(request);
} }
} }
@ -109,8 +109,9 @@ public class Netty4HeadBodyIsEmptyIT extends ESRestTestCase {
} }
builder.endObject(); builder.endObject();
client().performRequest("POST", "_aliases", emptyMap(), new StringEntity(Strings.toString(builder), Request request = new Request("POST", "/_aliases");
ContentType.APPLICATION_JSON)); request.setJsonEntity(Strings.toString(builder));
client().performRequest(request);
headTestCase("/_alias/test_alias", emptyMap(), greaterThan(0)); headTestCase("/_alias/test_alias", emptyMap(), greaterThan(0));
headTestCase("/test/_alias/test_alias", emptyMap(), greaterThan(0)); headTestCase("/test/_alias/test_alias", emptyMap(), greaterThan(0));
} }
@ -135,8 +136,9 @@ public class Netty4HeadBodyIsEmptyIT extends ESRestTestCase {
} }
builder.endObject(); builder.endObject();
client().performRequest("PUT", "/_template/template", emptyMap(), Request request = new Request("PUT", "/_template/template");
new StringEntity(Strings.toString(builder), ContentType.APPLICATION_JSON)); request.setJsonEntity(Strings.toString(builder));
client().performRequest(request);
headTestCase("/_template/template", emptyMap(), greaterThan(0)); headTestCase("/_template/template", emptyMap(), greaterThan(0));
} }
} }
@ -164,8 +166,10 @@ public class Netty4HeadBodyIsEmptyIT extends ESRestTestCase {
builder.endObject(); builder.endObject();
} }
builder.endObject(); builder.endObject();
client().performRequest("PUT", "/test-no-source", emptyMap(), new StringEntity(Strings.toString(builder),
ContentType.APPLICATION_JSON)); Request request = new Request("PUT", "/test-no-source");
request.setJsonEntity(Strings.toString(builder));
client().performRequest(request);
createTestDoc("test-no-source", "test-no-source"); createTestDoc("test-no-source", "test-no-source");
headTestCase("/test-no-source/test-no-source/1/_source", emptyMap(), NOT_FOUND.getStatus(), equalTo(0)); headTestCase("/test-no-source/test-no-source/1/_source", emptyMap(), NOT_FOUND.getStatus(), equalTo(0));
} }
@ -190,7 +194,11 @@ public class Netty4HeadBodyIsEmptyIT extends ESRestTestCase {
final Map<String, String> params, final Map<String, String> params,
final int expectedStatusCode, final int expectedStatusCode,
final Matcher<Integer> matcher) throws IOException { final Matcher<Integer> matcher) throws IOException {
Response response = client().performRequest("HEAD", url, params); Request request = new Request("HEAD", url);
for (Map.Entry<String, String> param : params.entrySet()) {
request.addParameter(param.getKey(), param.getValue());
}
Response response = client().performRequest(request);
assertEquals(expectedStatusCode, response.getStatusLine().getStatusCode()); assertEquals(expectedStatusCode, response.getStatusLine().getStatusCode());
assertThat(Integer.valueOf(response.getHeader("Content-Length")), matcher); assertThat(Integer.valueOf(response.getHeader("Content-Length")), matcher);
assertNull("HEAD requests shouldn't have a response body but " + url + " did", response.getEntity()); assertNull("HEAD requests shouldn't have a response body but " + url + " did", response.getEntity());

View File

@ -21,16 +21,13 @@ package org.elasticsearch.repositories.hdfs;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.security.AccessController; import java.security.AccessController;
import java.security.PrivilegedActionException; import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction; import java.security.PrivilegedExceptionAction;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ha.BadFencingConfigurationException; import org.apache.hadoop.ha.BadFencingConfigurationException;
@ -42,9 +39,7 @@ import org.apache.hadoop.ha.protocolPB.HAServiceProtocolClientSideTranslatorPB;
import org.apache.hadoop.hdfs.tools.DFSHAAdmin; import org.apache.hadoop.hdfs.tools.DFSHAAdmin;
import org.apache.hadoop.security.SecurityUtil; import org.apache.hadoop.security.SecurityUtil;
import org.apache.hadoop.security.UserGroupInformation; import org.apache.hadoop.security.UserGroupInformation;
import org.apache.http.Header; import org.elasticsearch.client.Request;
import org.apache.http.message.BasicHeader;
import org.apache.http.nio.entity.NStringEntity;
import org.elasticsearch.client.Response; import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClient;
import org.elasticsearch.common.io.PathUtils; import org.elasticsearch.common.io.PathUtils;
@ -58,8 +53,6 @@ public class HaHdfsFailoverTestSuiteIT extends ESRestTestCase {
public void testHAFailoverWithRepository() throws Exception { public void testHAFailoverWithRepository() throws Exception {
RestClient client = client(); RestClient client = client();
Map<String, String> emptyParams = Collections.emptyMap();
Header contentHeader = new BasicHeader("Content-Type", "application/json");
String esKerberosPrincipal = System.getProperty("test.krb5.principal.es"); String esKerberosPrincipal = System.getProperty("test.krb5.principal.es");
String hdfsKerberosPrincipal = System.getProperty("test.krb5.principal.hdfs"); String hdfsKerberosPrincipal = System.getProperty("test.krb5.principal.hdfs");
@ -106,7 +99,8 @@ public class HaHdfsFailoverTestSuiteIT extends ESRestTestCase {
// Create repository // Create repository
{ {
Response response = client.performRequest("PUT", "/_snapshot/hdfs_ha_repo_read", emptyParams, new NStringEntity( Request request = new Request("PUT", "/_snapshot/hdfs_ha_repo_read");
request.setJsonEntity(
"{" + "{" +
"\"type\":\"hdfs\"," + "\"type\":\"hdfs\"," +
"\"settings\":{" + "\"settings\":{" +
@ -121,15 +115,15 @@ public class HaHdfsFailoverTestSuiteIT extends ESRestTestCase {
"\"conf.dfs.client.failover.proxy.provider.ha-hdfs\": " + "\"conf.dfs.client.failover.proxy.provider.ha-hdfs\": " +
"\"org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider\"" + "\"org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider\"" +
"}" + "}" +
"}", "}");
Charset.defaultCharset()), contentHeader); Response response = client.performRequest(request);
Assert.assertEquals(200, response.getStatusLine().getStatusCode()); Assert.assertEquals(200, response.getStatusLine().getStatusCode());
} }
// Get repository // Get repository
{ {
Response response = client.performRequest("GET", "/_snapshot/hdfs_ha_repo_read/_all", emptyParams); Response response = client.performRequest(new Request("GET", "/_snapshot/hdfs_ha_repo_read/_all"));
Assert.assertEquals(200, response.getStatusLine().getStatusCode()); Assert.assertEquals(200, response.getStatusLine().getStatusCode());
} }
@ -138,7 +132,7 @@ public class HaHdfsFailoverTestSuiteIT extends ESRestTestCase {
// Get repository again // Get repository again
{ {
Response response = client.performRequest("GET", "/_snapshot/hdfs_ha_repo_read/_all", emptyParams); Response response = client.performRequest(new Request("GET", "/_snapshot/hdfs_ha_repo_read/_all"));
Assert.assertEquals(200, response.getStatusLine().getStatusCode()); Assert.assertEquals(200, response.getStatusLine().getStatusCode());
} }
} }

View File

@ -20,6 +20,7 @@
package org.elasticsearch.qa.verify_version_constants; package org.elasticsearch.qa.verify_version_constants;
import org.elasticsearch.Version; import org.elasticsearch.Version;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response; import org.elasticsearch.client.Response;
import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.test.rest.yaml.ObjectPath; import org.elasticsearch.test.rest.yaml.ObjectPath;
@ -32,7 +33,7 @@ import static org.hamcrest.CoreMatchers.equalTo;
public class VerifyVersionConstantsIT extends ESRestTestCase { public class VerifyVersionConstantsIT extends ESRestTestCase {
public void testLuceneVersionConstant() throws IOException, ParseException { public void testLuceneVersionConstant() throws IOException, ParseException {
final Response response = client().performRequest("GET", "/"); final Response response = client().performRequest(new Request("GET", "/"));
assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
final ObjectPath objectPath = ObjectPath.createFromResponse(response); final ObjectPath objectPath = ObjectPath.createFromResponse(response);
final String elasticsearchVersionString = objectPath.evaluate("version.number").toString(); final String elasticsearchVersionString = objectPath.evaluate("version.number").toString();