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.
This commit is contained in:
Nik Everett 2018-08-22 14:02:39 -04:00 committed by GitHub
parent 393eec1482
commit c3438bc8d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 81 additions and 93 deletions

View File

@ -5,12 +5,10 @@
*/ */
package org.elasticsearch.smoketest; package org.elasticsearch.smoketest;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.lucene.util.LuceneTestCase.AwaitsFix; import org.apache.lucene.util.LuceneTestCase.AwaitsFix;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response; import org.elasticsearch.client.Response;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.test.junit.annotations.TestLogging; import org.elasticsearch.test.junit.annotations.TestLogging;
import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.test.rest.ESRestTestCase;
@ -23,7 +21,6 @@ import org.elasticsearch.xpack.watcher.trigger.schedule.IntervalSchedule;
import org.junit.After; import org.junit.After;
import java.io.IOException; import java.io.IOException;
import java.util.Collections;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.xpack.watcher.input.InputBuilders.simpleInput; import static org.elasticsearch.xpack.watcher.input.InputBuilders.simpleInput;
@ -36,25 +33,25 @@ public class MonitoringWithWatcherRestIT extends ESRestTestCase {
@After @After
public void cleanExporters() throws Exception { public void cleanExporters() throws Exception {
String body = Strings.toString(jsonBuilder().startObject().startObject("transient") Request request = new Request("PUT", "/_cluster/settings");
.nullField("xpack.monitoring.exporters.*") request.setJsonEntity(Strings.toString(jsonBuilder().startObject()
.endObject().endObject()); .startObject("transient")
assertOK(adminClient().performRequest("PUT", "_cluster/settings", Collections.emptyMap(), .nullField("xpack.monitoring.exporters.*")
new StringEntity(body, ContentType.APPLICATION_JSON))); .endObject().endObject()));
adminClient().performRequest(request);
assertOK(adminClient().performRequest("DELETE", ".watch*", Collections.emptyMap())); adminClient().performRequest(new Request("DELETE", "/.watch*"));
} }
public void testThatLocalExporterAddsWatches() throws Exception { public void testThatLocalExporterAddsWatches() throws Exception {
String watchId = createMonitoringWatch(); String watchId = createMonitoringWatch();
String body = BytesReference.bytes(jsonBuilder().startObject().startObject("transient") Request request = new Request("PUT", "/_cluster/settings");
.field("xpack.monitoring.exporters.my_local_exporter.type", "local") request.setJsonEntity(Strings.toString(jsonBuilder().startObject()
.field("xpack.monitoring.exporters.my_local_exporter.cluster_alerts.management.enabled", true) .startObject("transient")
.endObject().endObject()).utf8ToString(); .field("xpack.monitoring.exporters.my_local_exporter.type", "local")
.field("xpack.monitoring.exporters.my_local_exporter.cluster_alerts.management.enabled", true)
adminClient().performRequest("PUT", "_cluster/settings", Collections.emptyMap(), .endObject().endObject()));
new StringEntity(body, ContentType.APPLICATION_JSON)); adminClient().performRequest(request);
assertTotalWatchCount(ClusterAlertsUtil.WATCH_IDS.length); assertTotalWatchCount(ClusterAlertsUtil.WATCH_IDS.length);
@ -65,14 +62,14 @@ public class MonitoringWithWatcherRestIT extends ESRestTestCase {
String watchId = createMonitoringWatch(); String watchId = createMonitoringWatch();
String httpHost = getHttpHost(); String httpHost = getHttpHost();
String body = BytesReference.bytes(jsonBuilder().startObject().startObject("transient") Request request = new Request("PUT", "/_cluster/settings");
.field("xpack.monitoring.exporters.my_http_exporter.type", "http") request.setJsonEntity(Strings.toString(jsonBuilder().startObject()
.field("xpack.monitoring.exporters.my_http_exporter.host", httpHost) .startObject("transient")
.field("xpack.monitoring.exporters.my_http_exporter.cluster_alerts.management.enabled", true) .field("xpack.monitoring.exporters.my_http_exporter.type", "http")
.endObject().endObject()).utf8ToString(); .field("xpack.monitoring.exporters.my_http_exporter.host", httpHost)
.field("xpack.monitoring.exporters.my_http_exporter.cluster_alerts.management.enabled", true)
adminClient().performRequest("PUT", "_cluster/settings", Collections.emptyMap(), .endObject().endObject()));
new StringEntity(body, ContentType.APPLICATION_JSON)); adminClient().performRequest(request);
assertTotalWatchCount(ClusterAlertsUtil.WATCH_IDS.length); assertTotalWatchCount(ClusterAlertsUtil.WATCH_IDS.length);
@ -80,15 +77,15 @@ public class MonitoringWithWatcherRestIT extends ESRestTestCase {
} }
private void assertMonitoringWatchHasBeenOverWritten(String watchId) throws Exception { private void assertMonitoringWatchHasBeenOverWritten(String watchId) throws Exception {
ObjectPath path = ObjectPath.createFromResponse(client().performRequest("GET", "_xpack/watcher/watch/" + watchId)); ObjectPath path = ObjectPath.createFromResponse(client().performRequest(new Request("GET", "/_xpack/watcher/watch/" + watchId)));
String interval = path.evaluate("watch.trigger.schedule.interval"); String interval = path.evaluate("watch.trigger.schedule.interval");
assertThat(interval, is("1m")); assertThat(interval, is("1m"));
} }
private void assertTotalWatchCount(int expectedWatches) throws Exception { private void assertTotalWatchCount(int expectedWatches) throws Exception {
assertBusy(() -> { assertBusy(() -> {
assertOK(client().performRequest("POST", ".watches/_refresh")); assertOK(client().performRequest(new Request("POST", "/.watches/_refresh")));
ObjectPath path = ObjectPath.createFromResponse(client().performRequest("POST", ".watches/_count")); ObjectPath path = ObjectPath.createFromResponse(client().performRequest(new Request("POST", "/.watches/_count")));
int count = path.evaluate("count"); int count = path.evaluate("count");
assertThat(count, is(expectedWatches)); assertThat(count, is(expectedWatches));
}); });
@ -97,28 +94,28 @@ public class MonitoringWithWatcherRestIT extends ESRestTestCase {
private String createMonitoringWatch() throws Exception { private String createMonitoringWatch() throws Exception {
String clusterUUID = getClusterUUID(); String clusterUUID = getClusterUUID();
String watchId = clusterUUID + "_kibana_version_mismatch"; String watchId = clusterUUID + "_kibana_version_mismatch";
String sampleWatch = WatchSourceBuilders.watchBuilder() Request request = new Request("PUT", "/_xpack/watcher/watch/" + watchId);
request.setJsonEntity(WatchSourceBuilders.watchBuilder()
.trigger(TriggerBuilders.schedule(new IntervalSchedule(new IntervalSchedule.Interval(1000, MINUTES)))) .trigger(TriggerBuilders.schedule(new IntervalSchedule(new IntervalSchedule.Interval(1000, MINUTES))))
.input(simpleInput()) .input(simpleInput())
.addAction("logme", ActionBuilders.loggingAction("foo")) .addAction("logme", ActionBuilders.loggingAction("foo"))
.buildAsBytes(XContentType.JSON).utf8ToString(); .buildAsBytes(XContentType.JSON).utf8ToString());
client().performRequest("PUT", "_xpack/watcher/watch/" + watchId, Collections.emptyMap(), client().performRequest(request);
new StringEntity(sampleWatch, ContentType.APPLICATION_JSON));
return watchId; return watchId;
} }
private String getClusterUUID() throws Exception { private String getClusterUUID() throws Exception {
Response response = client().performRequest("GET", "_cluster/state/metadata", Collections.emptyMap()); Response response = client().performRequest(new Request("GET", "/_cluster/state/metadata"));
ObjectPath objectPath = ObjectPath.createFromResponse(response); ObjectPath objectPath = ObjectPath.createFromResponse(response);
String clusterUUID = objectPath.evaluate("metadata.cluster_uuid"); String clusterUUID = objectPath.evaluate("metadata.cluster_uuid");
return clusterUUID; return clusterUUID;
} }
public String getHttpHost() throws IOException { public String getHttpHost() throws IOException {
ObjectPath path = ObjectPath.createFromResponse(client().performRequest("GET", "_cluster/state", Collections.emptyMap())); ObjectPath path = ObjectPath.createFromResponse(client().performRequest(new Request("GET", "/_cluster/state")));
String masterNodeId = path.evaluate("master_node"); String masterNodeId = path.evaluate("master_node");
ObjectPath nodesPath = ObjectPath.createFromResponse(client().performRequest("GET", "_nodes", Collections.emptyMap())); ObjectPath nodesPath = ObjectPath.createFromResponse(client().performRequest(new Request("GET", "/_nodes")));
String httpHost = nodesPath.evaluate("nodes." + masterNodeId + ".http.publish_address"); String httpHost = nodesPath.evaluate("nodes." + masterNodeId + ".http.publish_address");
return httpHost; return httpHost;
} }

View File

@ -7,9 +7,7 @@ package org.elasticsearch.smoketest;
import com.carrotsearch.randomizedtesting.annotations.Name; import com.carrotsearch.randomizedtesting.annotations.Name;
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
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.common.settings.SecureString; import org.elasticsearch.common.settings.SecureString;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.common.util.concurrent.ThreadContext;
@ -49,9 +47,9 @@ public class SmokeTestWatcherWithSecurityClientYamlTestSuiteIT extends ESClientY
emptyList(), emptyMap()); emptyList(), emptyMap());
// create one document in this index, so we can test in the YAML tests, that the index cannot be accessed // create one document in this index, so we can test in the YAML tests, that the index cannot be accessed
Response resp = adminClient().performRequest("PUT", "/index_not_allowed_to_read/doc/1", Collections.emptyMap(), Request request = new Request("PUT", "/index_not_allowed_to_read/doc/1");
new StringEntity("{\"foo\":\"bar\"}", ContentType.APPLICATION_JSON)); request.setJsonEntity("{\"foo\":\"bar\"}");
assertThat(resp.getStatusLine().getStatusCode(), is(201)); adminClient().performRequest(request);
assertBusy(() -> { assertBusy(() -> {
ClientYamlTestResponse response = ClientYamlTestResponse response =
@ -129,4 +127,3 @@ public class SmokeTestWatcherWithSecurityClientYamlTestSuiteIT extends ESClientY
.build(); .build();
} }
} }

View File

@ -5,9 +5,8 @@
*/ */
package org.elasticsearch.smoketest; package org.elasticsearch.smoketest;
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.client.Request;
import org.elasticsearch.client.Response; import org.elasticsearch.client.Response;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.SecureString;
@ -21,7 +20,6 @@ import org.junit.After;
import org.junit.Before; import org.junit.Before;
import java.io.IOException; import java.io.IOException;
import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
@ -41,27 +39,28 @@ public class SmokeTestWatcherWithSecurityIT extends ESRestTestCase {
@Before @Before
public void startWatcher() throws Exception { public void startWatcher() throws Exception {
StringEntity entity = new StringEntity("{ \"value\" : \"15\" }", ContentType.APPLICATION_JSON); Request createAllowedDoc = new Request("PUT", "/my_test_index/doc/1");
assertOK(adminClient().performRequest("PUT", "my_test_index/doc/1", Collections.singletonMap("refresh", "true"), entity)); createAllowedDoc.setJsonEntity("{ \"value\" : \"15\" }");
createAllowedDoc.addParameter("refresh", "true");
adminClient().performRequest(createAllowedDoc);
// delete the watcher history to not clutter with entries from other test // delete the watcher history to not clutter with entries from other test
adminClient().performRequest("DELETE", ".watcher-history-*", Collections.emptyMap()); adminClient().performRequest(new Request("DELETE", ".watcher-history-*"));
// create one document in this index, so we can test in the YAML tests, that the index cannot be accessed // create one document in this index, so we can test in the YAML tests, that the index cannot be accessed
Response resp = adminClient().performRequest("PUT", "/index_not_allowed_to_read/doc/1", Collections.emptyMap(), Request createNotAllowedDoc = new Request("PUT", "/index_not_allowed_to_read/doc/1");
new StringEntity("{\"foo\":\"bar\"}", ContentType.APPLICATION_JSON)); createNotAllowedDoc.setJsonEntity("{\"foo\":\"bar\"}");
assertThat(resp.getStatusLine().getStatusCode(), is(201)); adminClient().performRequest(createNotAllowedDoc);
assertBusy(() -> { assertBusy(() -> {
try { try {
Response statsResponse = adminClient().performRequest("GET", "_xpack/watcher/stats"); Response statsResponse = adminClient().performRequest(new Request("GET", "/_xpack/watcher/stats"));
ObjectPath objectPath = ObjectPath.createFromResponse(statsResponse); ObjectPath objectPath = ObjectPath.createFromResponse(statsResponse);
String state = objectPath.evaluate("stats.0.watcher_state"); String state = objectPath.evaluate("stats.0.watcher_state");
switch (state) { switch (state) {
case "stopped": case "stopped":
Response startResponse = adminClient().performRequest("POST", "_xpack/watcher/_start"); Response startResponse = adminClient().performRequest(new Request("POST", "/_xpack/watcher/_start"));
assertOK(startResponse);
String body = EntityUtils.toString(startResponse.getEntity()); String body = EntityUtils.toString(startResponse.getEntity());
assertThat(body, containsString("\"acknowledged\":true")); assertThat(body, containsString("\"acknowledged\":true"));
break; break;
@ -82,18 +81,18 @@ public class SmokeTestWatcherWithSecurityIT extends ESRestTestCase {
assertBusy(() -> { assertBusy(() -> {
for (String template : WatcherIndexTemplateRegistryField.TEMPLATE_NAMES) { for (String template : WatcherIndexTemplateRegistryField.TEMPLATE_NAMES) {
assertOK(adminClient().performRequest("HEAD", "_template/" + template)); assertOK(adminClient().performRequest(new Request("HEAD", "_template/" + template)));
} }
}); });
} }
@After @After
public void stopWatcher() throws Exception { public void stopWatcher() throws Exception {
assertOK(adminClient().performRequest("DELETE", "my_test_index")); adminClient().performRequest(new Request("DELETE", "/my_test_index"));
assertBusy(() -> { assertBusy(() -> {
try { try {
Response statsResponse = adminClient().performRequest("GET", "_xpack/watcher/stats"); Response statsResponse = adminClient().performRequest(new Request("GET", "/_xpack/watcher/stats"));
ObjectPath objectPath = ObjectPath.createFromResponse(statsResponse); ObjectPath objectPath = ObjectPath.createFromResponse(statsResponse);
String state = objectPath.evaluate("stats.0.watcher_state"); String state = objectPath.evaluate("stats.0.watcher_state");
@ -106,8 +105,7 @@ public class SmokeTestWatcherWithSecurityIT extends ESRestTestCase {
case "starting": case "starting":
throw new AssertionError("waiting until starting state reached started state to stop"); throw new AssertionError("waiting until starting state reached started state to stop");
case "started": case "started":
Response stopResponse = adminClient().performRequest("POST", "_xpack/watcher/_stop", Collections.emptyMap()); Response stopResponse = adminClient().performRequest(new Request("POST", "/_xpack/watcher/_stop"));
assertOK(stopResponse);
String body = EntityUtils.toString(stopResponse.getEntity()); String body = EntityUtils.toString(stopResponse.getEntity());
assertThat(body, containsString("\"acknowledged\":true")); assertThat(body, containsString("\"acknowledged\":true"));
break; break;
@ -210,7 +208,7 @@ public class SmokeTestWatcherWithSecurityIT extends ESRestTestCase {
boolean conditionMet = objectPath.evaluate("hits.hits.0._source.result.condition.met"); boolean conditionMet = objectPath.evaluate("hits.hits.0._source.result.condition.met");
assertThat(conditionMet, is(true)); assertThat(conditionMet, is(true));
ObjectPath getObjectPath = ObjectPath.createFromResponse(client().performRequest("GET", "my_test_index/doc/my-id")); ObjectPath getObjectPath = ObjectPath.createFromResponse(client().performRequest(new Request("GET", "/my_test_index/doc/my-id")));
String value = getObjectPath.evaluate("_source.hits.hits.0._source.value"); String value = getObjectPath.evaluate("_source.hits.hits.0._source.value");
assertThat(value, is("15")); assertThat(value, is("15"));
} }
@ -238,8 +236,7 @@ public class SmokeTestWatcherWithSecurityIT extends ESRestTestCase {
getWatchHistoryEntry(watchId); getWatchHistoryEntry(watchId);
Response response = adminClient().performRequest("GET", "my_test_index/doc/some-id", Response response = adminClient().performRequest(new Request("HEAD", "/my_test_index/doc/some-id"));
Collections.singletonMap("ignore", "404"));
assertThat(response.getStatusLine().getStatusCode(), is(404)); assertThat(response.getStatusLine().getStatusCode(), is(404));
} }
@ -262,7 +259,7 @@ public class SmokeTestWatcherWithSecurityIT extends ESRestTestCase {
boolean conditionMet = objectPath.evaluate("hits.hits.0._source.result.condition.met"); boolean conditionMet = objectPath.evaluate("hits.hits.0._source.result.condition.met");
assertThat(conditionMet, is(true)); assertThat(conditionMet, is(true));
ObjectPath getObjectPath = ObjectPath.createFromResponse(client().performRequest("GET", "my_test_index/doc/my-id")); ObjectPath getObjectPath = ObjectPath.createFromResponse(client().performRequest(new Request("GET", "/my_test_index/doc/my-id")));
String spam = getObjectPath.evaluate("_source.spam"); String spam = getObjectPath.evaluate("_source.spam");
assertThat(spam, is("eggs")); assertThat(spam, is("eggs"));
} }
@ -286,16 +283,14 @@ public class SmokeTestWatcherWithSecurityIT extends ESRestTestCase {
boolean conditionMet = objectPath.evaluate("hits.hits.0._source.result.condition.met"); boolean conditionMet = objectPath.evaluate("hits.hits.0._source.result.condition.met");
assertThat(conditionMet, is(true)); assertThat(conditionMet, is(true));
Response response = adminClient().performRequest("GET", "index_not_allowed_to_read/doc/my-id", Response response = adminClient().performRequest(new Request("HEAD", "/index_not_allowed_to_read/doc/my-id"));
Collections.singletonMap("ignore", "404"));
assertThat(response.getStatusLine().getStatusCode(), is(404)); assertThat(response.getStatusLine().getStatusCode(), is(404));
} }
private void indexWatch(String watchId, XContentBuilder builder) throws Exception { private void indexWatch(String watchId, XContentBuilder builder) throws Exception {
StringEntity entity = new StringEntity(Strings.toString(builder), ContentType.APPLICATION_JSON); Request request = new Request("PUT", "/_xpack/watcher/watch/" + watchId);
request.setJsonEntity(Strings.toString(builder));
Response response = client().performRequest("PUT", "_xpack/watcher/watch/" + watchId, Collections.emptyMap(), entity); Response response = client().performRequest(request);
assertOK(response);
Map<String, Object> responseMap = entityAsMap(response); Map<String, Object> responseMap = entityAsMap(response);
assertThat(responseMap, hasEntry("_id", watchId)); assertThat(responseMap, hasEntry("_id", watchId));
} }
@ -307,7 +302,7 @@ public class SmokeTestWatcherWithSecurityIT extends ESRestTestCase {
private ObjectPath getWatchHistoryEntry(String watchId, String state) throws Exception { private ObjectPath getWatchHistoryEntry(String watchId, String state) throws Exception {
final AtomicReference<ObjectPath> objectPathReference = new AtomicReference<>(); final AtomicReference<ObjectPath> objectPathReference = new AtomicReference<>();
assertBusy(() -> { assertBusy(() -> {
client().performRequest("POST", ".watcher-history-*/_refresh"); client().performRequest(new Request("POST", "/.watcher-history-*/_refresh"));
try (XContentBuilder builder = jsonBuilder()) { try (XContentBuilder builder = jsonBuilder()) {
builder.startObject(); builder.startObject();
@ -323,8 +318,9 @@ public class SmokeTestWatcherWithSecurityIT extends ESRestTestCase {
.endObject().endArray(); .endObject().endArray();
builder.endObject(); builder.endObject();
StringEntity entity = new StringEntity(Strings.toString(builder), ContentType.APPLICATION_JSON); Request searchRequest = new Request("POST", "/.watcher-history-*/_search");
Response response = client().performRequest("POST", ".watcher-history-*/_search", Collections.emptyMap(), entity); searchRequest.setJsonEntity(Strings.toString(builder));
Response response = client().performRequest(searchRequest);
ObjectPath objectPath = ObjectPath.createFromResponse(response); ObjectPath objectPath = ObjectPath.createFromResponse(response);
int totalHits = objectPath.evaluate("hits.total"); int totalHits = objectPath.evaluate("hits.total");
assertThat(totalHits, is(greaterThanOrEqualTo(1))); assertThat(totalHits, is(greaterThanOrEqualTo(1)));

View File

@ -5,8 +5,7 @@
*/ */
package org.elasticsearch.smoketest; package org.elasticsearch.smoketest;
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.settings.SecureString; import org.elasticsearch.common.settings.SecureString;
@ -23,7 +22,6 @@ import java.io.IOException;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import static java.util.Collections.emptyMap;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue; import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue;
import static org.hamcrest.Matchers.greaterThanOrEqualTo; import static org.hamcrest.Matchers.greaterThanOrEqualTo;
@ -39,15 +37,15 @@ public class SmokeTestWatcherTestSuiteIT extends ESRestTestCase {
@Before @Before
public void startWatcher() throws Exception { public void startWatcher() throws Exception {
// delete the watcher history to not clutter with entries from other test // delete the watcher history to not clutter with entries from other test
assertOK(adminClient().performRequest("DELETE", ".watcher-history-*")); assertOK(adminClient().performRequest(new Request("DELETE", "/.watcher-history-*")));
assertBusy(() -> { assertBusy(() -> {
Response response = adminClient().performRequest("GET", "_xpack/watcher/stats"); Response response = adminClient().performRequest(new Request("GET", "/_xpack/watcher/stats"));
String state = ObjectPath.createFromResponse(response).evaluate("stats.0.watcher_state"); String state = ObjectPath.createFromResponse(response).evaluate("stats.0.watcher_state");
switch (state) { switch (state) {
case "stopped": case "stopped":
Response startResponse = adminClient().performRequest("POST", "/_xpack/watcher/_start"); Response startResponse = adminClient().performRequest(new Request("POST", "/_xpack/watcher/_start"));
boolean isAcknowledged = ObjectPath.createFromResponse(startResponse).evaluate("acknowledged"); boolean isAcknowledged = ObjectPath.createFromResponse(startResponse).evaluate("acknowledged");
assertThat(isAcknowledged, is(true)); assertThat(isAcknowledged, is(true));
break; break;
@ -65,7 +63,7 @@ public class SmokeTestWatcherTestSuiteIT extends ESRestTestCase {
assertBusy(() -> { assertBusy(() -> {
for (String template : WatcherIndexTemplateRegistryField.TEMPLATE_NAMES) { for (String template : WatcherIndexTemplateRegistryField.TEMPLATE_NAMES) {
Response templateExistsResponse = adminClient().performRequest("HEAD", "_template/" + template, emptyMap()); Response templateExistsResponse = adminClient().performRequest(new Request("HEAD", "/_template/" + template));
assertThat(templateExistsResponse.getStatusLine().getStatusCode(), is(200)); assertThat(templateExistsResponse.getStatusLine().getStatusCode(), is(200));
} }
}); });
@ -74,7 +72,7 @@ public class SmokeTestWatcherTestSuiteIT extends ESRestTestCase {
@After @After
public void stopWatcher() throws Exception { public void stopWatcher() throws Exception {
assertBusy(() -> { assertBusy(() -> {
Response response = adminClient().performRequest("GET", "_xpack/watcher/stats", emptyMap()); Response response = adminClient().performRequest(new Request("GET", "/_xpack/watcher/stats"));
String state = ObjectPath.createFromResponse(response).evaluate("stats.0.watcher_state"); String state = ObjectPath.createFromResponse(response).evaluate("stats.0.watcher_state");
switch (state) { switch (state) {
@ -86,7 +84,7 @@ public class SmokeTestWatcherTestSuiteIT extends ESRestTestCase {
case "starting": case "starting":
throw new AssertionError("waiting until starting state reached started state to stop"); throw new AssertionError("waiting until starting state reached started state to stop");
case "started": case "started":
Response stopResponse = adminClient().performRequest("POST", "/_xpack/watcher/_stop", emptyMap()); Response stopResponse = adminClient().performRequest(new Request("POST", "/_xpack/watcher/_stop"));
boolean isAcknowledged = ObjectPath.createFromResponse(stopResponse).evaluate("acknowledged"); boolean isAcknowledged = ObjectPath.createFromResponse(stopResponse).evaluate("acknowledged");
assertThat(isAcknowledged, is(true)); assertThat(isAcknowledged, is(true));
break; break;
@ -112,12 +110,12 @@ public class SmokeTestWatcherTestSuiteIT extends ESRestTestCase {
String watchId = "cluster_health_watch"; String watchId = "cluster_health_watch";
// get master publish address // get master publish address
Response clusterStateResponse = adminClient().performRequest("GET", "_cluster/state"); Response clusterStateResponse = adminClient().performRequest(new Request("GET", "/_cluster/state"));
ObjectPath clusterState = ObjectPath.createFromResponse(clusterStateResponse); ObjectPath clusterState = ObjectPath.createFromResponse(clusterStateResponse);
String masterNode = clusterState.evaluate("master_node"); String masterNode = clusterState.evaluate("master_node");
assertThat(masterNode, is(notNullValue())); assertThat(masterNode, is(notNullValue()));
Response statsResponse = adminClient().performRequest("GET", "_nodes"); Response statsResponse = adminClient().performRequest(new Request("GET", "/_nodes"));
ObjectPath stats = ObjectPath.createFromResponse(statsResponse); ObjectPath stats = ObjectPath.createFromResponse(statsResponse);
String address = stats.evaluate("nodes." + masterNode + ".http.publish_address"); String address = stats.evaluate("nodes." + masterNode + ".http.publish_address");
assertThat(address, is(notNullValue())); assertThat(address, is(notNullValue()));
@ -163,16 +161,15 @@ public class SmokeTestWatcherTestSuiteIT extends ESRestTestCase {
} }
private void indexWatch(String watchId, XContentBuilder builder) throws Exception { private void indexWatch(String watchId, XContentBuilder builder) throws Exception {
StringEntity entity = new StringEntity(Strings.toString(builder), ContentType.APPLICATION_JSON); Request request = new Request("PUT", "/_xpack/watcher/watch/" + watchId);
request.setJsonEntity(Strings.toString(builder));
Response response = client().performRequest("PUT", "_xpack/watcher/watch/" + watchId, emptyMap(), entity); Response response = client().performRequest(request);
assertOK(response);
Map<String, Object> responseMap = entityAsMap(response); Map<String, Object> responseMap = entityAsMap(response);
assertThat(responseMap, hasEntry("_id", watchId)); assertThat(responseMap, hasEntry("_id", watchId));
} }
private void deleteWatch(String watchId) throws IOException { private void deleteWatch(String watchId) throws IOException {
Response response = client().performRequest("DELETE", "_xpack/watcher/watch/" + watchId); Response response = client().performRequest(new Request("DELETE", "/_xpack/watcher/watch/" + watchId));
assertOK(response); assertOK(response);
ObjectPath path = ObjectPath.createFromResponse(response); ObjectPath path = ObjectPath.createFromResponse(response);
boolean found = path.evaluate("found"); boolean found = path.evaluate("found");
@ -182,7 +179,7 @@ public class SmokeTestWatcherTestSuiteIT extends ESRestTestCase {
private ObjectPath getWatchHistoryEntry(String watchId) throws Exception { private ObjectPath getWatchHistoryEntry(String watchId) throws Exception {
final AtomicReference<ObjectPath> objectPathReference = new AtomicReference<>(); final AtomicReference<ObjectPath> objectPathReference = new AtomicReference<>();
assertBusy(() -> { assertBusy(() -> {
client().performRequest("POST", ".watcher-history-*/_refresh"); client().performRequest(new Request("POST", "/.watcher-history-*/_refresh"));
try (XContentBuilder builder = jsonBuilder()) { try (XContentBuilder builder = jsonBuilder()) {
builder.startObject(); builder.startObject();
@ -194,8 +191,9 @@ public class SmokeTestWatcherTestSuiteIT extends ESRestTestCase {
.endObject().endArray(); .endObject().endArray();
builder.endObject(); builder.endObject();
StringEntity entity = new StringEntity(Strings.toString(builder), ContentType.APPLICATION_JSON); Request searchRequest = new Request("POST", "/.watcher-history-*/_search");
Response response = client().performRequest("POST", ".watcher-history-*/_search", emptyMap(), entity); searchRequest.setJsonEntity(Strings.toString(builder));
Response response = client().performRequest(searchRequest);
ObjectPath objectPath = ObjectPath.createFromResponse(response); ObjectPath objectPath = ObjectPath.createFromResponse(response);
int totalHits = objectPath.evaluate("hits.total"); int totalHits = objectPath.evaluate("hits.total");
assertThat(totalHits, is(greaterThanOrEqualTo(1))); assertThat(totalHits, is(greaterThanOrEqualTo(1)));
@ -208,7 +206,7 @@ public class SmokeTestWatcherTestSuiteIT extends ESRestTestCase {
} }
private void assertWatchCount(int expectedWatches) throws IOException { private void assertWatchCount(int expectedWatches) throws IOException {
Response watcherStatsResponse = adminClient().performRequest("GET", "_xpack/watcher/stats"); Response watcherStatsResponse = adminClient().performRequest(new Request("GET", "/_xpack/watcher/stats"));
ObjectPath objectPath = ObjectPath.createFromResponse(watcherStatsResponse); ObjectPath objectPath = ObjectPath.createFromResponse(watcherStatsResponse);
int watchCount = objectPath.evaluate("stats.0.watch_count"); int watchCount = objectPath.evaluate("stats.0.watch_count");
assertThat(watchCount, is(expectedWatches)); assertThat(watchCount, is(expectedWatches));