Skip to content

Commit c3438bc

Browse files
authored
Switch some watcher tests to new style Requests (#33044)
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/smoke-test-monitoring-with-watcher`, `x-pack/qa/smoke-test-watcher`, and `x-pack/qa/smoke-test-watcher-with-security` projects to use the new versions.
1 parent 393eec1 commit c3438bc

File tree

4 files changed

+81
-93
lines changed

4 files changed

+81
-93
lines changed

x-pack/qa/smoke-test-monitoring-with-watcher/src/test/java/org/elasticsearch/smoketest/MonitoringWithWatcherRestIT.java

+33-36
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
*/
66
package org.elasticsearch.smoketest;
77

8-
import org.apache.http.entity.ContentType;
9-
import org.apache.http.entity.StringEntity;
108
import org.apache.lucene.util.LuceneTestCase.AwaitsFix;
9+
import org.elasticsearch.client.Request;
1110
import org.elasticsearch.client.Response;
1211
import org.elasticsearch.common.Strings;
13-
import org.elasticsearch.common.bytes.BytesReference;
1412
import org.elasticsearch.common.xcontent.XContentType;
1513
import org.elasticsearch.test.junit.annotations.TestLogging;
1614
import org.elasticsearch.test.rest.ESRestTestCase;
@@ -23,7 +21,6 @@
2321
import org.junit.After;
2422

2523
import java.io.IOException;
26-
import java.util.Collections;
2724

2825
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
2926
import static org.elasticsearch.xpack.watcher.input.InputBuilders.simpleInput;
@@ -36,25 +33,25 @@ public class MonitoringWithWatcherRestIT extends ESRestTestCase {
3633

3734
@After
3835
public void cleanExporters() throws Exception {
39-
String body = Strings.toString(jsonBuilder().startObject().startObject("transient")
40-
.nullField("xpack.monitoring.exporters.*")
41-
.endObject().endObject());
42-
assertOK(adminClient().performRequest("PUT", "_cluster/settings", Collections.emptyMap(),
43-
new StringEntity(body, ContentType.APPLICATION_JSON)));
44-
45-
assertOK(adminClient().performRequest("DELETE", ".watch*", Collections.emptyMap()));
36+
Request request = new Request("PUT", "/_cluster/settings");
37+
request.setJsonEntity(Strings.toString(jsonBuilder().startObject()
38+
.startObject("transient")
39+
.nullField("xpack.monitoring.exporters.*")
40+
.endObject().endObject()));
41+
adminClient().performRequest(request);
42+
adminClient().performRequest(new Request("DELETE", "/.watch*"));
4643
}
4744

4845
public void testThatLocalExporterAddsWatches() throws Exception {
4946
String watchId = createMonitoringWatch();
5047

51-
String body = BytesReference.bytes(jsonBuilder().startObject().startObject("transient")
52-
.field("xpack.monitoring.exporters.my_local_exporter.type", "local")
53-
.field("xpack.monitoring.exporters.my_local_exporter.cluster_alerts.management.enabled", true)
54-
.endObject().endObject()).utf8ToString();
55-
56-
adminClient().performRequest("PUT", "_cluster/settings", Collections.emptyMap(),
57-
new StringEntity(body, ContentType.APPLICATION_JSON));
48+
Request request = new Request("PUT", "/_cluster/settings");
49+
request.setJsonEntity(Strings.toString(jsonBuilder().startObject()
50+
.startObject("transient")
51+
.field("xpack.monitoring.exporters.my_local_exporter.type", "local")
52+
.field("xpack.monitoring.exporters.my_local_exporter.cluster_alerts.management.enabled", true)
53+
.endObject().endObject()));
54+
adminClient().performRequest(request);
5855

5956
assertTotalWatchCount(ClusterAlertsUtil.WATCH_IDS.length);
6057

@@ -65,30 +62,30 @@ public void testThatHttpExporterAddsWatches() throws Exception {
6562
String watchId = createMonitoringWatch();
6663
String httpHost = getHttpHost();
6764

68-
String body = BytesReference.bytes(jsonBuilder().startObject().startObject("transient")
69-
.field("xpack.monitoring.exporters.my_http_exporter.type", "http")
70-
.field("xpack.monitoring.exporters.my_http_exporter.host", httpHost)
71-
.field("xpack.monitoring.exporters.my_http_exporter.cluster_alerts.management.enabled", true)
72-
.endObject().endObject()).utf8ToString();
73-
74-
adminClient().performRequest("PUT", "_cluster/settings", Collections.emptyMap(),
75-
new StringEntity(body, ContentType.APPLICATION_JSON));
65+
Request request = new Request("PUT", "/_cluster/settings");
66+
request.setJsonEntity(Strings.toString(jsonBuilder().startObject()
67+
.startObject("transient")
68+
.field("xpack.monitoring.exporters.my_http_exporter.type", "http")
69+
.field("xpack.monitoring.exporters.my_http_exporter.host", httpHost)
70+
.field("xpack.monitoring.exporters.my_http_exporter.cluster_alerts.management.enabled", true)
71+
.endObject().endObject()));
72+
adminClient().performRequest(request);
7673

7774
assertTotalWatchCount(ClusterAlertsUtil.WATCH_IDS.length);
7875

7976
assertMonitoringWatchHasBeenOverWritten(watchId);
8077
}
8178

8279
private void assertMonitoringWatchHasBeenOverWritten(String watchId) throws Exception {
83-
ObjectPath path = ObjectPath.createFromResponse(client().performRequest("GET", "_xpack/watcher/watch/" + watchId));
80+
ObjectPath path = ObjectPath.createFromResponse(client().performRequest(new Request("GET", "/_xpack/watcher/watch/" + watchId)));
8481
String interval = path.evaluate("watch.trigger.schedule.interval");
8582
assertThat(interval, is("1m"));
8683
}
8784

8885
private void assertTotalWatchCount(int expectedWatches) throws Exception {
8986
assertBusy(() -> {
90-
assertOK(client().performRequest("POST", ".watches/_refresh"));
91-
ObjectPath path = ObjectPath.createFromResponse(client().performRequest("POST", ".watches/_count"));
87+
assertOK(client().performRequest(new Request("POST", "/.watches/_refresh")));
88+
ObjectPath path = ObjectPath.createFromResponse(client().performRequest(new Request("POST", "/.watches/_count")));
9289
int count = path.evaluate("count");
9390
assertThat(count, is(expectedWatches));
9491
});
@@ -97,28 +94,28 @@ private void assertTotalWatchCount(int expectedWatches) throws Exception {
9794
private String createMonitoringWatch() throws Exception {
9895
String clusterUUID = getClusterUUID();
9996
String watchId = clusterUUID + "_kibana_version_mismatch";
100-
String sampleWatch = WatchSourceBuilders.watchBuilder()
97+
Request request = new Request("PUT", "/_xpack/watcher/watch/" + watchId);
98+
request.setJsonEntity(WatchSourceBuilders.watchBuilder()
10199
.trigger(TriggerBuilders.schedule(new IntervalSchedule(new IntervalSchedule.Interval(1000, MINUTES))))
102100
.input(simpleInput())
103101
.addAction("logme", ActionBuilders.loggingAction("foo"))
104-
.buildAsBytes(XContentType.JSON).utf8ToString();
105-
client().performRequest("PUT", "_xpack/watcher/watch/" + watchId, Collections.emptyMap(),
106-
new StringEntity(sampleWatch, ContentType.APPLICATION_JSON));
102+
.buildAsBytes(XContentType.JSON).utf8ToString());
103+
client().performRequest(request);
107104
return watchId;
108105
}
109106

110107
private String getClusterUUID() throws Exception {
111-
Response response = client().performRequest("GET", "_cluster/state/metadata", Collections.emptyMap());
108+
Response response = client().performRequest(new Request("GET", "/_cluster/state/metadata"));
112109
ObjectPath objectPath = ObjectPath.createFromResponse(response);
113110
String clusterUUID = objectPath.evaluate("metadata.cluster_uuid");
114111
return clusterUUID;
115112
}
116113

117114
public String getHttpHost() throws IOException {
118-
ObjectPath path = ObjectPath.createFromResponse(client().performRequest("GET", "_cluster/state", Collections.emptyMap()));
115+
ObjectPath path = ObjectPath.createFromResponse(client().performRequest(new Request("GET", "/_cluster/state")));
119116
String masterNodeId = path.evaluate("master_node");
120117

121-
ObjectPath nodesPath = ObjectPath.createFromResponse(client().performRequest("GET", "_nodes", Collections.emptyMap()));
118+
ObjectPath nodesPath = ObjectPath.createFromResponse(client().performRequest(new Request("GET", "/_nodes")));
122119
String httpHost = nodesPath.evaluate("nodes." + masterNodeId + ".http.publish_address");
123120
return httpHost;
124121
}

x-pack/qa/smoke-test-watcher-with-security/src/test/java/org/elasticsearch/smoketest/SmokeTestWatcherWithSecurityClientYamlTestSuiteIT.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77

88
import com.carrotsearch.randomizedtesting.annotations.Name;
99
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
10-
import org.apache.http.entity.ContentType;
11-
import org.apache.http.entity.StringEntity;
12-
import org.elasticsearch.client.Response;
10+
import org.elasticsearch.client.Request;
1311
import org.elasticsearch.common.settings.SecureString;
1412
import org.elasticsearch.common.settings.Settings;
1513
import org.elasticsearch.common.util.concurrent.ThreadContext;
@@ -49,9 +47,9 @@ public void startWatcher() throws Exception {
4947
emptyList(), emptyMap());
5048

5149
// create one document in this index, so we can test in the YAML tests, that the index cannot be accessed
52-
Response resp = adminClient().performRequest("PUT", "/index_not_allowed_to_read/doc/1", Collections.emptyMap(),
53-
new StringEntity("{\"foo\":\"bar\"}", ContentType.APPLICATION_JSON));
54-
assertThat(resp.getStatusLine().getStatusCode(), is(201));
50+
Request request = new Request("PUT", "/index_not_allowed_to_read/doc/1");
51+
request.setJsonEntity("{\"foo\":\"bar\"}");
52+
adminClient().performRequest(request);
5553

5654
assertBusy(() -> {
5755
ClientYamlTestResponse response =
@@ -129,4 +127,3 @@ protected Settings restAdminSettings() {
129127
.build();
130128
}
131129
}
132-

x-pack/qa/smoke-test-watcher-with-security/src/test/java/org/elasticsearch/smoketest/SmokeTestWatcherWithSecurityIT.java

+26-30
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
*/
66
package org.elasticsearch.smoketest;
77

8-
import org.apache.http.entity.ContentType;
9-
import org.apache.http.entity.StringEntity;
108
import org.apache.http.util.EntityUtils;
9+
import org.elasticsearch.client.Request;
1110
import org.elasticsearch.client.Response;
1211
import org.elasticsearch.common.Strings;
1312
import org.elasticsearch.common.settings.SecureString;
@@ -21,7 +20,6 @@
2120
import org.junit.Before;
2221

2322
import java.io.IOException;
24-
import java.util.Collections;
2523
import java.util.Map;
2624
import java.util.concurrent.atomic.AtomicReference;
2725

@@ -41,27 +39,28 @@ public class SmokeTestWatcherWithSecurityIT extends ESRestTestCase {
4139

4240
@Before
4341
public void startWatcher() throws Exception {
44-
StringEntity entity = new StringEntity("{ \"value\" : \"15\" }", ContentType.APPLICATION_JSON);
45-
assertOK(adminClient().performRequest("PUT", "my_test_index/doc/1", Collections.singletonMap("refresh", "true"), entity));
42+
Request createAllowedDoc = new Request("PUT", "/my_test_index/doc/1");
43+
createAllowedDoc.setJsonEntity("{ \"value\" : \"15\" }");
44+
createAllowedDoc.addParameter("refresh", "true");
45+
adminClient().performRequest(createAllowedDoc);
4646

4747
// delete the watcher history to not clutter with entries from other test
48-
adminClient().performRequest("DELETE", ".watcher-history-*", Collections.emptyMap());
48+
adminClient().performRequest(new Request("DELETE", ".watcher-history-*"));
4949

5050
// create one document in this index, so we can test in the YAML tests, that the index cannot be accessed
51-
Response resp = adminClient().performRequest("PUT", "/index_not_allowed_to_read/doc/1", Collections.emptyMap(),
52-
new StringEntity("{\"foo\":\"bar\"}", ContentType.APPLICATION_JSON));
53-
assertThat(resp.getStatusLine().getStatusCode(), is(201));
51+
Request createNotAllowedDoc = new Request("PUT", "/index_not_allowed_to_read/doc/1");
52+
createNotAllowedDoc.setJsonEntity("{\"foo\":\"bar\"}");
53+
adminClient().performRequest(createNotAllowedDoc);
5454

5555
assertBusy(() -> {
5656
try {
57-
Response statsResponse = adminClient().performRequest("GET", "_xpack/watcher/stats");
57+
Response statsResponse = adminClient().performRequest(new Request("GET", "/_xpack/watcher/stats"));
5858
ObjectPath objectPath = ObjectPath.createFromResponse(statsResponse);
5959
String state = objectPath.evaluate("stats.0.watcher_state");
6060

6161
switch (state) {
6262
case "stopped":
63-
Response startResponse = adminClient().performRequest("POST", "_xpack/watcher/_start");
64-
assertOK(startResponse);
63+
Response startResponse = adminClient().performRequest(new Request("POST", "/_xpack/watcher/_start"));
6564
String body = EntityUtils.toString(startResponse.getEntity());
6665
assertThat(body, containsString("\"acknowledged\":true"));
6766
break;
@@ -82,18 +81,18 @@ public void startWatcher() throws Exception {
8281

8382
assertBusy(() -> {
8483
for (String template : WatcherIndexTemplateRegistryField.TEMPLATE_NAMES) {
85-
assertOK(adminClient().performRequest("HEAD", "_template/" + template));
84+
assertOK(adminClient().performRequest(new Request("HEAD", "_template/" + template)));
8685
}
8786
});
8887
}
8988

9089
@After
9190
public void stopWatcher() throws Exception {
92-
assertOK(adminClient().performRequest("DELETE", "my_test_index"));
91+
adminClient().performRequest(new Request("DELETE", "/my_test_index"));
9392

9493
assertBusy(() -> {
9594
try {
96-
Response statsResponse = adminClient().performRequest("GET", "_xpack/watcher/stats");
95+
Response statsResponse = adminClient().performRequest(new Request("GET", "/_xpack/watcher/stats"));
9796
ObjectPath objectPath = ObjectPath.createFromResponse(statsResponse);
9897
String state = objectPath.evaluate("stats.0.watcher_state");
9998

@@ -106,8 +105,7 @@ public void stopWatcher() throws Exception {
106105
case "starting":
107106
throw new AssertionError("waiting until starting state reached started state to stop");
108107
case "started":
109-
Response stopResponse = adminClient().performRequest("POST", "_xpack/watcher/_stop", Collections.emptyMap());
110-
assertOK(stopResponse);
108+
Response stopResponse = adminClient().performRequest(new Request("POST", "/_xpack/watcher/_stop"));
111109
String body = EntityUtils.toString(stopResponse.getEntity());
112110
assertThat(body, containsString("\"acknowledged\":true"));
113111
break;
@@ -210,7 +208,7 @@ public void testSearchTransformHasPermissions() throws Exception {
210208
boolean conditionMet = objectPath.evaluate("hits.hits.0._source.result.condition.met");
211209
assertThat(conditionMet, is(true));
212210

213-
ObjectPath getObjectPath = ObjectPath.createFromResponse(client().performRequest("GET", "my_test_index/doc/my-id"));
211+
ObjectPath getObjectPath = ObjectPath.createFromResponse(client().performRequest(new Request("GET", "/my_test_index/doc/my-id")));
214212
String value = getObjectPath.evaluate("_source.hits.hits.0._source.value");
215213
assertThat(value, is("15"));
216214
}
@@ -238,8 +236,7 @@ public void testSearchTransformInsufficientPermissions() throws Exception {
238236

239237
getWatchHistoryEntry(watchId);
240238

241-
Response response = adminClient().performRequest("GET", "my_test_index/doc/some-id",
242-
Collections.singletonMap("ignore", "404"));
239+
Response response = adminClient().performRequest(new Request("HEAD", "/my_test_index/doc/some-id"));
243240
assertThat(response.getStatusLine().getStatusCode(), is(404));
244241
}
245242

@@ -262,7 +259,7 @@ public void testIndexActionHasPermissions() throws Exception {
262259
boolean conditionMet = objectPath.evaluate("hits.hits.0._source.result.condition.met");
263260
assertThat(conditionMet, is(true));
264261

265-
ObjectPath getObjectPath = ObjectPath.createFromResponse(client().performRequest("GET", "my_test_index/doc/my-id"));
262+
ObjectPath getObjectPath = ObjectPath.createFromResponse(client().performRequest(new Request("GET", "/my_test_index/doc/my-id")));
266263
String spam = getObjectPath.evaluate("_source.spam");
267264
assertThat(spam, is("eggs"));
268265
}
@@ -286,16 +283,14 @@ public void testIndexActionInsufficientPrivileges() throws Exception {
286283
boolean conditionMet = objectPath.evaluate("hits.hits.0._source.result.condition.met");
287284
assertThat(conditionMet, is(true));
288285

289-
Response response = adminClient().performRequest("GET", "index_not_allowed_to_read/doc/my-id",
290-
Collections.singletonMap("ignore", "404"));
286+
Response response = adminClient().performRequest(new Request("HEAD", "/index_not_allowed_to_read/doc/my-id"));
291287
assertThat(response.getStatusLine().getStatusCode(), is(404));
292288
}
293289

294290
private void indexWatch(String watchId, XContentBuilder builder) throws Exception {
295-
StringEntity entity = new StringEntity(Strings.toString(builder), ContentType.APPLICATION_JSON);
296-
297-
Response response = client().performRequest("PUT", "_xpack/watcher/watch/" + watchId, Collections.emptyMap(), entity);
298-
assertOK(response);
291+
Request request = new Request("PUT", "/_xpack/watcher/watch/" + watchId);
292+
request.setJsonEntity(Strings.toString(builder));
293+
Response response = client().performRequest(request);
299294
Map<String, Object> responseMap = entityAsMap(response);
300295
assertThat(responseMap, hasEntry("_id", watchId));
301296
}
@@ -307,7 +302,7 @@ private ObjectPath getWatchHistoryEntry(String watchId) throws Exception {
307302
private ObjectPath getWatchHistoryEntry(String watchId, String state) throws Exception {
308303
final AtomicReference<ObjectPath> objectPathReference = new AtomicReference<>();
309304
assertBusy(() -> {
310-
client().performRequest("POST", ".watcher-history-*/_refresh");
305+
client().performRequest(new Request("POST", "/.watcher-history-*/_refresh"));
311306

312307
try (XContentBuilder builder = jsonBuilder()) {
313308
builder.startObject();
@@ -323,8 +318,9 @@ private ObjectPath getWatchHistoryEntry(String watchId, String state) throws Exc
323318
.endObject().endArray();
324319
builder.endObject();
325320

326-
StringEntity entity = new StringEntity(Strings.toString(builder), ContentType.APPLICATION_JSON);
327-
Response response = client().performRequest("POST", ".watcher-history-*/_search", Collections.emptyMap(), entity);
321+
Request searchRequest = new Request("POST", "/.watcher-history-*/_search");
322+
searchRequest.setJsonEntity(Strings.toString(builder));
323+
Response response = client().performRequest(searchRequest);
328324
ObjectPath objectPath = ObjectPath.createFromResponse(response);
329325
int totalHits = objectPath.evaluate("hits.total");
330326
assertThat(totalHits, is(greaterThanOrEqualTo(1)));

0 commit comments

Comments
 (0)