Skip to content

Commit fcf8cad

Browse files
authored
Switch some x-pack tests to new style Requests (#32500)
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 `x-pack/qa/audit-tests`, `x-pack/qa/ml-disabled`, and `x-pack/qa/multi-node` projects to use the new versions.
1 parent 28a0df2 commit fcf8cad

File tree

3 files changed

+53
-46
lines changed

3 files changed

+53
-46
lines changed

x-pack/plugin/ml/qa/disabled/src/test/java/org/elasticsearch/xpack/ml/integration/MlPluginDisabledIT.java

+33-26
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@
55
*/
66
package org.elasticsearch.xpack.ml.integration;
77

8-
import org.apache.http.entity.ContentType;
9-
import org.apache.http.entity.StringEntity;
8+
import org.elasticsearch.client.Request;
109
import org.elasticsearch.client.ResponseException;
1110
import org.elasticsearch.common.Strings;
1211
import org.elasticsearch.common.xcontent.XContentBuilder;
1312
import org.elasticsearch.test.rest.ESRestTestCase;
1413
import org.elasticsearch.xpack.ml.MachineLearning;
1514

16-
import java.util.Collections;
17-
1815
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
1916
import static org.hamcrest.Matchers.containsString;
2017

@@ -27,30 +24,40 @@ public class MlPluginDisabledIT extends ESRestTestCase {
2724
public void testActionsFail() throws Exception {
2825
XContentBuilder xContentBuilder = jsonBuilder();
2926
xContentBuilder.startObject();
30-
xContentBuilder.field("actions-fail-job", "foo");
31-
xContentBuilder.field("description", "Analysis of response time by airline");
32-
33-
xContentBuilder.startObject("analysis_config");
34-
xContentBuilder.field("bucket_span", "3600s");
35-
xContentBuilder.startArray("detectors");
36-
xContentBuilder.startObject();
37-
xContentBuilder.field("function", "metric");
38-
xContentBuilder.field("field_name", "responsetime");
39-
xContentBuilder.field("by_field_name", "airline");
40-
xContentBuilder.endObject();
41-
xContentBuilder.endArray();
42-
xContentBuilder.endObject();
43-
44-
xContentBuilder.startObject("data_description");
45-
xContentBuilder.field("format", "xcontent");
46-
xContentBuilder.field("time_field", "time");
47-
xContentBuilder.field("time_format", "epoch");
48-
xContentBuilder.endObject();
27+
{
28+
xContentBuilder.field("actions-fail-job", "foo");
29+
xContentBuilder.field("description", "Analysis of response time by airline");
30+
31+
xContentBuilder.startObject("analysis_config");
32+
{
33+
xContentBuilder.field("bucket_span", "3600s");
34+
xContentBuilder.startArray("detectors");
35+
{
36+
xContentBuilder.startObject();
37+
{
38+
xContentBuilder.field("function", "metric");
39+
xContentBuilder.field("field_name", "responsetime");
40+
xContentBuilder.field("by_field_name", "airline");
41+
}
42+
xContentBuilder.endObject();
43+
}
44+
xContentBuilder.endArray();
45+
}
46+
xContentBuilder.endObject();
47+
48+
xContentBuilder.startObject("data_description");
49+
{
50+
xContentBuilder.field("format", "xcontent");
51+
xContentBuilder.field("time_field", "time");
52+
xContentBuilder.field("time_format", "epoch");
53+
}
54+
xContentBuilder.endObject();
55+
}
4956
xContentBuilder.endObject();
5057

51-
ResponseException exception = expectThrows(ResponseException.class, () -> client().performRequest("put",
52-
MachineLearning.BASE_PATH + "anomaly_detectors/foo", Collections.emptyMap(),
53-
new StringEntity(Strings.toString(xContentBuilder), ContentType.APPLICATION_JSON)));
58+
Request request = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/foo");
59+
request.setJsonEntity(Strings.toString(xContentBuilder));
60+
ResponseException exception = expectThrows(ResponseException.class, () -> client().performRequest(request));
5461
assertThat(exception.getMessage(), containsString("no handler found for uri [/_xpack/ml/anomaly_detectors/foo] and method [PUT]"));
5562
}
5663
}

x-pack/qa/audit-tests/src/test/java/org/elasticsearch/xpack/security/audit/IndexAuditIT.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package org.elasticsearch.xpack.security.audit;
77

88
import com.carrotsearch.hppc.cursors.ObjectCursor;
9-
import org.apache.http.message.BasicHeader;
109
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse;
1110
import org.elasticsearch.action.search.SearchResponse;
1211
import org.elasticsearch.action.support.master.AcknowledgedResponse;
@@ -111,10 +110,12 @@ public NamedWriteableRegistry getNamedWriteableRegistry() {
111110
}
112111

113112
public void testIndexAuditTrailWorking() throws Exception {
114-
Response response = getRestClient().performRequest("GET", "/",
115-
new BasicHeader(UsernamePasswordToken.BASIC_AUTH_HEADER,
116-
UsernamePasswordToken.basicAuthHeaderValue(USER, new SecureString(PASS.toCharArray()))));
117-
assertThat(response.getStatusLine().getStatusCode(), is(200));
113+
Request request = new Request("GET", "/");
114+
RequestOptions.Builder options = request.getOptions().toBuilder();
115+
options.addHeader(UsernamePasswordToken.BASIC_AUTH_HEADER,
116+
UsernamePasswordToken.basicAuthHeaderValue(USER, new SecureString(PASS.toCharArray())));
117+
request.setOptions(options);
118+
Response response = getRestClient().performRequest(request);
118119
final AtomicReference<ClusterState> lastClusterState = new AtomicReference<>();
119120
final boolean found = awaitSecurityAuditIndex(lastClusterState, QueryBuilders.matchQuery("principal", USER));
120121

x-pack/qa/multi-node/src/test/java/org/elasticsearch/multi_node/GlobalCheckpointSyncActionIT.java

+14-15
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
*/
66
package org.elasticsearch.multi_node;
77

8-
import org.apache.http.entity.ContentType;
9-
import org.apache.http.entity.StringEntity;
8+
import org.elasticsearch.client.Request;
109
import org.elasticsearch.client.Response;
1110
import org.elasticsearch.common.Strings;
1211
import org.elasticsearch.common.settings.SecureString;
@@ -16,10 +15,6 @@
1615
import org.elasticsearch.test.rest.ESRestTestCase;
1716
import org.elasticsearch.test.rest.yaml.ObjectPath;
1817

19-
import java.util.Collections;
20-
import java.util.HashMap;
21-
import java.util.Map;
22-
2318
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
2419
import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue;
2520
import static org.hamcrest.Matchers.equalTo;
@@ -59,12 +54,15 @@ public void testGlobalCheckpointSyncActionRunsAsPrivilegedUser() throws Exceptio
5954
builder.endObject();
6055
}
6156
builder.endObject();
62-
final StringEntity entity = new StringEntity(Strings.toString(builder), ContentType.APPLICATION_JSON);
63-
client().performRequest("PUT", "test-index", Collections.emptyMap(), entity);
57+
Request createIndexRequest = new Request("PUT", "/test-index");
58+
createIndexRequest.setJsonEntity(Strings.toString(builder));
59+
client().performRequest(createIndexRequest);
6460
}
6561

6662
// wait for the replica to recover
67-
client().performRequest("GET", "/_cluster/health", Collections.singletonMap("wait_for_status", "green"));
63+
Request healthRequest = new Request("GET", "/_cluster/health");
64+
healthRequest.addParameter("wait_for_status", "green");
65+
client().performRequest(healthRequest);
6866

6967
// index some documents
7068
final int numberOfDocuments = randomIntBetween(0, 128);
@@ -75,17 +73,18 @@ public void testGlobalCheckpointSyncActionRunsAsPrivilegedUser() throws Exceptio
7573
builder.field("foo", i);
7674
}
7775
builder.endObject();
78-
final StringEntity entity = new StringEntity(Strings.toString(builder), ContentType.APPLICATION_JSON);
79-
client().performRequest("PUT", "/test-index/test-type/" + i, Collections.emptyMap(), entity);
76+
Request indexRequest = new Request("PUT", "/test-index/test-type/" + i);
77+
indexRequest.setJsonEntity(Strings.toString(builder));
78+
client().performRequest(indexRequest);
8079
}
8180
}
8281

8382
// we have to wait for the post-operation global checkpoint sync to propagate to the replica
8483
assertBusy(() -> {
85-
final Map<String, String> params = new HashMap<>(2);
86-
params.put("level", "shards");
87-
params.put("filter_path", "**.seq_no");
88-
final Response response = client().performRequest("GET", "/test-index/_stats", params);
84+
final Request request = new Request("GET", "/test-index/_stats");
85+
request.addParameter("level", "shards");
86+
request.addParameter("filter_path", "**.seq_no");
87+
final Response response = client().performRequest(request);
8988
final ObjectPath path = ObjectPath.createFromResponse(response);
9089
// int looks funny here since global checkpoints are longs but the response parser does not know enough to treat them as long
9190
final int shard0GlobalCheckpoint = path.evaluate("indices.test-index.shards.0.0.seq_no.global_checkpoint");

0 commit comments

Comments
 (0)