Skip to content

Commit

Permalink
Switch non-x-pack to new style requests (#32106)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nik9000 committed Jul 16, 2018
1 parent b00de36 commit 0382984
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

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.ResponseException;
import org.elasticsearch.common.settings.Setting;
Expand All @@ -43,7 +44,7 @@
public class Netty4BadRequestIT extends ESRestTestCase {

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 Map<String, Object> map = objectPath.evaluate("nodes");
int maxMaxInitialLineLength = Integer.MIN_VALUE;
Expand Down Expand Up @@ -77,9 +78,9 @@ public void testBadRequest() throws IOException {
}

public void testInvalidParameterValue() throws IOException {
final ResponseException e = expectThrows(
ResponseException.class,
() -> client().performRequest("GET", "/_cluster/settings", Collections.singletonMap("pretty", "neither-true-nor-false")));
final Request request = new Request("GET", "/_cluster/settings");
request.addParameter("pretty", "neither-true-nor-false");
final ResponseException e = expectThrows(ResponseException.class, () -> client().performRequest(request));
final Response response = e.getResponse();
assertThat(response.getStatusLine().getStatusCode(), equalTo(400));
final ObjectPath objectPath = ObjectPath.createFromResponse(response);
Expand All @@ -89,9 +90,11 @@ public void testInvalidParameterValue() throws IOException {
}

public void testInvalidHeaderValue() throws IOException {
final BasicHeader header = new BasicHeader("Content-Type", "\t");
final ResponseException e =
expectThrows(ResponseException.class, () -> client().performRequest("GET", "/_cluster/settings", header));
final Request request = new Request("GET", "/_cluster/settings");
final RequestOptions.Builder options = request.getOptions().toBuilder();
options.addHeader("Content-Type", "\t");
request.setOptions(options);
final ResponseException e = expectThrows(ResponseException.class, () -> client().performRequest(request));
final Response response = e.getResponse();
assertThat(response.getStatusLine().getStatusCode(), equalTo(400));
final ObjectPath objectPath = ObjectPath.createFromResponse(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

package org.elasticsearch.rest;

import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.XContentBuilder;
Expand Down Expand Up @@ -57,8 +56,9 @@ private void createTestDoc(final String indexName, final String typeName) throws
builder.field("test", "test");
}
builder.endObject();
client().performRequest("PUT", "/" + indexName + "/" + typeName + "/" + "1", emptyMap(),
new StringEntity(Strings.toString(builder), ContentType.APPLICATION_JSON));
Request request = new Request("PUT", "/" + indexName + "/" + typeName + "/" + "1");
request.setJsonEntity(Strings.toString(builder));
client().performRequest(request);
}
}

Expand Down Expand Up @@ -109,8 +109,9 @@ public void testAliasExists() throws IOException {
}
builder.endObject();

client().performRequest("POST", "_aliases", emptyMap(), new StringEntity(Strings.toString(builder),
ContentType.APPLICATION_JSON));
Request request = new Request("POST", "/_aliases");
request.setJsonEntity(Strings.toString(builder));
client().performRequest(request);
headTestCase("/_alias/test_alias", emptyMap(), greaterThan(0));
headTestCase("/test/_alias/test_alias", emptyMap(), greaterThan(0));
}
Expand All @@ -135,8 +136,9 @@ public void testTemplateExists() throws IOException {
}
builder.endObject();

client().performRequest("PUT", "/_template/template", emptyMap(),
new StringEntity(Strings.toString(builder), ContentType.APPLICATION_JSON));
Request request = new Request("PUT", "/_template/template");
request.setJsonEntity(Strings.toString(builder));
client().performRequest(request);
headTestCase("/_template/template", emptyMap(), greaterThan(0));
}
}
Expand Down Expand Up @@ -164,8 +166,10 @@ public void testGetSourceAction() throws IOException {
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");
headTestCase("/test-no-source/test-no-source/1/_source", emptyMap(), NOT_FOUND.getStatus(), equalTo(0));
}
Expand All @@ -190,7 +194,11 @@ private void headTestCase(
final Map<String, String> params,
final int expectedStatusCode,
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());
assertThat(Integer.valueOf(response.getHeader("Content-Length")), matcher);
assertNull("HEAD requests shouldn't have a response body but " + url + " did", response.getEntity());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,13 @@

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ha.BadFencingConfigurationException;
Expand All @@ -42,9 +39,7 @@
import org.apache.hadoop.hdfs.tools.DFSHAAdmin;
import org.apache.hadoop.security.SecurityUtil;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.apache.http.nio.entity.NStringEntity;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.common.io.PathUtils;
Expand All @@ -58,8 +53,6 @@ public class HaHdfsFailoverTestSuiteIT extends ESRestTestCase {

public void testHAFailoverWithRepository() throws Exception {
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 hdfsKerberosPrincipal = System.getProperty("test.krb5.principal.hdfs");
Expand Down Expand Up @@ -106,7 +99,8 @@ public void testHAFailoverWithRepository() throws Exception {

// 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\"," +
"\"settings\":{" +
Expand All @@ -121,15 +115,15 @@ public void testHAFailoverWithRepository() throws Exception {
"\"conf.dfs.client.failover.proxy.provider.ha-hdfs\": " +
"\"org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider\"" +
"}" +
"}",
Charset.defaultCharset()), contentHeader);
"}");
Response response = client.performRequest(request);

Assert.assertEquals(200, response.getStatusLine().getStatusCode());
}

// 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());
}

Expand All @@ -138,7 +132,7 @@ public void testHAFailoverWithRepository() throws Exception {

// 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());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.qa.verify_version_constants;

import org.elasticsearch.Version;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.test.rest.yaml.ObjectPath;
Expand All @@ -32,7 +33,7 @@
public class VerifyVersionConstantsIT extends ESRestTestCase {

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));
final ObjectPath objectPath = ObjectPath.createFromResponse(response);
final String elasticsearchVersionString = objectPath.evaluate("version.number").toString();
Expand Down

0 comments on commit 0382984

Please sign in to comment.