From 6a6df685e90192e4f67c5aa350e38539d3842899 Mon Sep 17 00:00:00 2001 From: Mark Tozzi Date: Fri, 21 Sep 2018 08:53:22 -0400 Subject: [PATCH 01/14] Lay out protocol classes --- .../xpack/watcher/DeactivateWatchRequest.java | 9 +++ .../watcher/DeactivateWatchResponse.java | 76 +++++++++++++++++++ .../watcher/DeactivateWatchResponseTests.java | 27 +++++++ 3 files changed, 112 insertions(+) create mode 100644 x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeactivateWatchRequest.java create mode 100644 x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeactivateWatchResponse.java create mode 100644 x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeactivateWatchResponseTests.java diff --git a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeactivateWatchRequest.java b/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeactivateWatchRequest.java new file mode 100644 index 0000000000000..5c22121dec631 --- /dev/null +++ b/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeactivateWatchRequest.java @@ -0,0 +1,9 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.protocol.xpack.watcher; + +public class DeactivateWatchRequest { +} diff --git a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeactivateWatchResponse.java b/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeactivateWatchResponse.java new file mode 100644 index 0000000000000..c141b9b361f86 --- /dev/null +++ b/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeactivateWatchResponse.java @@ -0,0 +1,76 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.protocol.xpack.watcher; + +import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.xcontent.ObjectParser; +import org.elasticsearch.common.xcontent.ToXContentObject; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentParser; + +import java.io.IOException; + +public class DeactivateWatchResponse extends ActionResponse implements ToXContentObject { + + private static final ObjectParser PARSER + = new ObjectParser<>("x_pack_deactivate_watch_response", DeactivateWatchResponse::new); + static { + PARSER.declareString(DeactivateWatchResponse::setId, new ParseField("_id")); + PARSER.declareLong(DeactivateWatchResponse::setVersion, new ParseField("_version")); + PARSER.declareString(DeactivateWatchResponse::setStatus, new ParseField("status")); + } + + public static DeactivateWatchResponse fromXContent(XContentParser parser) throws IOException { + return PARSER.parse(parser, null); + } + + private String id; + private long version; + // TODO: Replace with migrated WatchStatus class + private String status; + + // TODO: Do we need some kind of actions object here? + + public DeactivateWatchResponse() { + + } + + public DeactivateWatchResponse(String id, long version, String status) { + this.id = id; + this.version = version; + this.status = status; + } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + return null; + } + + public String getId() { + return id; + } + + private void setId(String id) { + this.id = id; + } + + public long getVersion() { + return version; + } + + private void setVersion(long version) { + this.version = version; + } + + public String getStatus() { + return status; + } + + private void setStatus(String status) { + this.status = status; + } +} diff --git a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeactivateWatchResponseTests.java b/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeactivateWatchResponseTests.java new file mode 100644 index 0000000000000..7a4fd9dab9ee8 --- /dev/null +++ b/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeactivateWatchResponseTests.java @@ -0,0 +1,27 @@ +package org.elasticsearch.protocol.xpack.watcher; + +import org.elasticsearch.protocol.xpack.watcher.DeactivateWatchResponse; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.test.AbstractXContentTestCase; + +import java.io.IOException; + +public class DeactivateWatchResponseTests extends AbstractXContentTestCase { + @Override + protected DeactivateWatchResponse createTestInstance() { + String id = randomAlphaOfLength(10); + long version = randomLongBetween(1, 10); + // TODO: Randomized Status + return new DeactivateWatchResponse(id, version, null); + } + + @Override + protected DeactivateWatchResponse doParseInstance(XContentParser parser) throws IOException { + return DeactivateWatchResponse.fromXContent(parser); + } + + @Override + protected boolean supportsUnknownFields() { + return false; + } +} From 22ebecb93c50563dfecb10c255bb46553af449a6 Mon Sep 17 00:00:00 2001 From: Mark Tozzi Date: Mon, 24 Sep 2018 09:31:41 -0400 Subject: [PATCH 02/14] Serialization for protocol side response class --- .../watcher/DeactivateWatchResponse.java | 48 +++++++++++++++++-- .../watcher/DeactivateWatchResponseTests.java | 4 +- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeactivateWatchResponse.java b/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeactivateWatchResponse.java index c141b9b361f86..596e2f99a85f2 100644 --- a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeactivateWatchResponse.java +++ b/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeactivateWatchResponse.java @@ -7,12 +7,15 @@ import org.elasticsearch.action.ActionResponse; import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import java.io.IOException; +import java.util.Objects; public class DeactivateWatchResponse extends ActionResponse implements ToXContentObject { @@ -39,17 +42,27 @@ public DeactivateWatchResponse() { } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + DeactivateWatchResponse that = (DeactivateWatchResponse) o; + return version == that.version && + Objects.equals(id, that.id) && + Objects.equals(status, that.status); + } + + @Override + public int hashCode() { + return Objects.hash(id, version, status); + } + public DeactivateWatchResponse(String id, long version, String status) { this.id = id; this.version = version; this.status = status; } - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return null; - } - public String getId() { return id; } @@ -73,4 +86,29 @@ public String getStatus() { private void setStatus(String status) { this.status = status; } + + @Override + public void readFrom(StreamInput in) throws IOException { + super.readFrom(in); + id = in.readString(); + version = in.readVLong(); + status = in.readString(); + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + super.writeTo(out); + out.writeString(id); + out.writeVLong(version); + out.writeString(status); + } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + return builder.startObject() + .field("_id", id) + .field("_version", version) + .field("status", status) + .endObject(); + } } diff --git a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeactivateWatchResponseTests.java b/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeactivateWatchResponseTests.java index 7a4fd9dab9ee8..9e634527ccffb 100644 --- a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeactivateWatchResponseTests.java +++ b/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeactivateWatchResponseTests.java @@ -1,6 +1,5 @@ package org.elasticsearch.protocol.xpack.watcher; -import org.elasticsearch.protocol.xpack.watcher.DeactivateWatchResponse; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; @@ -12,7 +11,8 @@ protected DeactivateWatchResponse createTestInstance() { String id = randomAlphaOfLength(10); long version = randomLongBetween(1, 10); // TODO: Randomized Status - return new DeactivateWatchResponse(id, version, null); + String status = randomAlphaOfLength(10); + return new DeactivateWatchResponse(id, version, status); } @Override From f52c5fa7d31b577a71830041ad64fee645319b5a Mon Sep 17 00:00:00 2001 From: Mark Tozzi Date: Mon, 24 Sep 2018 15:06:22 -0400 Subject: [PATCH 03/14] fixed paths & packages to new standard --- .../org/elasticsearch/client}/watcher/DeactivateWatchRequest.java | 0 .../elasticsearch/client}/watcher/DeactivateWatchResponse.java | 0 .../client}/watcher/DeactivateWatchResponseTests.java | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename {x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/main/java/org/elasticsearch/client}/watcher/DeactivateWatchRequest.java (100%) rename {x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/main/java/org/elasticsearch/client}/watcher/DeactivateWatchResponse.java (100%) rename {x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack => client/rest-high-level/src/test/java/org/elasticsearch/client}/watcher/DeactivateWatchResponseTests.java (100%) diff --git a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeactivateWatchRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java similarity index 100% rename from x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeactivateWatchRequest.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java diff --git a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeactivateWatchResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchResponse.java similarity index 100% rename from x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeactivateWatchResponse.java rename to client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchResponse.java diff --git a/x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeactivateWatchResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchResponseTests.java similarity index 100% rename from x-pack/protocol/src/test/java/org/elasticsearch/protocol/xpack/watcher/DeactivateWatchResponseTests.java rename to client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchResponseTests.java From be7515712e0859d10b48c91623fb215d5003540c Mon Sep 17 00:00:00 2001 From: Mark Tozzi Date: Tue, 25 Sep 2018 11:54:45 -0400 Subject: [PATCH 04/14] request parsing --- .../client/WatcherRequestConverters.java | 12 ++++++++++++ .../client/watcher/DeactivateWatchRequest.java | 11 ++++++++++- .../client/WatcherRequestConvertersTests.java | 10 ++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherRequestConverters.java index 3b52d1c7b9943..cf6642b02cd80 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherRequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherRequestConverters.java @@ -23,6 +23,7 @@ import org.apache.http.client.methods.HttpPut; import org.apache.http.entity.ByteArrayEntity; import org.apache.http.entity.ContentType; +import org.elasticsearch.client.watcher.DeactivateWatchRequest; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest; import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; @@ -48,6 +49,17 @@ static Request putWatch(PutWatchRequest putWatchRequest) { return request; } + static Request deactivateWatch(DeactivateWatchRequest deactivateWatchRequest) { + String endpoint = new RequestConverters.EndpointBuilder() + .addPathPartAsIs("_xpack") + .addPathPartAsIs("watcher") + .addPathPartAsIs("watch") + .addPathPart(deactivateWatchRequest.getWatchId()) + .addPathPartAsIs("_deactivate") + .build(); + return new Request(HttpPut.METHOD_NAME, endpoint); + } + static Request deleteWatch(DeleteWatchRequest deleteWatchRequest) { String endpoint = new RequestConverters.EndpointBuilder() .addPathPartAsIs("_xpack") diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java index 5c22121dec631..f87a4e3bd908a 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java @@ -3,7 +3,16 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -package org.elasticsearch.protocol.xpack.watcher; +package org.elasticsearch.client.watcher; public class DeactivateWatchRequest { + private final String watchId; + + public DeactivateWatchRequest(String watchId) { + this.watchId = watchId; + } + + public String getWatchId() { + return watchId; + } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherRequestConvertersTests.java index cf5af1dd5949f..32034fc2d76b5 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherRequestConvertersTests.java @@ -21,6 +21,7 @@ import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpPut; +import org.elasticsearch.client.watcher.DeactivateWatchRequest; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest; @@ -65,6 +66,15 @@ public void testPutWatch() throws Exception { assertThat(bos.toString("UTF-8"), is(body)); } + public void testDeactivateWatch() { + String watchId = randomAlphaOfLength(10); + DeactivateWatchRequest deactivateWatchRequest = new DeactivateWatchRequest(watchId); + Request request = WatcherRequestConverters.deactivateWatch(deactivateWatchRequest); + + assertEquals(HttpPut.METHOD_NAME, request.getMethod()); + assertEquals("/_xpack/watcher/watch/" + watchId + "/_deactivate", request.getEndpoint()); + } + public void testDeleteWatch() { DeleteWatchRequest deleteWatchRequest = new DeleteWatchRequest(); String watchId = randomAlphaOfLength(10); From 590ae61b1ebe2d95e1940964663abe2194712be8 Mon Sep 17 00:00:00 2001 From: Mark Tozzi Date: Tue, 25 Sep 2018 11:55:14 -0400 Subject: [PATCH 05/14] Response parsing & basic test --- .../watcher/DeactivateWatchResponse.java | 99 ++++--------------- .../watcher/DeactivateWatchResponseTests.java | 45 +++++---- 2 files changed, 46 insertions(+), 98 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchResponse.java index 596e2f99a85f2..ea98ff34c4a6b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchResponse.java @@ -3,43 +3,21 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -package org.elasticsearch.protocol.xpack.watcher; +package org.elasticsearch.client.watcher; -import org.elasticsearch.action.ActionResponse; import org.elasticsearch.common.ParseField; -import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.ObjectParser; -import org.elasticsearch.common.xcontent.ToXContentObject; -import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import java.io.IOException; import java.util.Objects; -public class DeactivateWatchResponse extends ActionResponse implements ToXContentObject { - - private static final ObjectParser PARSER - = new ObjectParser<>("x_pack_deactivate_watch_response", DeactivateWatchResponse::new); - static { - PARSER.declareString(DeactivateWatchResponse::setId, new ParseField("_id")); - PARSER.declareLong(DeactivateWatchResponse::setVersion, new ParseField("_version")); - PARSER.declareString(DeactivateWatchResponse::setStatus, new ParseField("status")); - } - - public static DeactivateWatchResponse fromXContent(XContentParser parser) throws IOException { - return PARSER.parse(parser, null); - } - - private String id; - private long version; - // TODO: Replace with migrated WatchStatus class - private String status; - - // TODO: Do we need some kind of actions object here? - - public DeactivateWatchResponse() { +public class DeactivateWatchResponse { + private WatchStatus status; + public DeactivateWatchResponse(WatchStatus status) { + this.status = status; } @Override @@ -47,68 +25,29 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; DeactivateWatchResponse that = (DeactivateWatchResponse) o; - return version == that.version && - Objects.equals(id, that.id) && - Objects.equals(status, that.status); + return Objects.equals(status, that.status); } @Override public int hashCode() { - return Objects.hash(id, version, status); - } - - public DeactivateWatchResponse(String id, long version, String status) { - this.id = id; - this.version = version; - this.status = status; - } - - public String getId() { - return id; + return Objects.hash(status); } - private void setId(String id) { - this.id = id; - } - - public long getVersion() { - return version; - } - - private void setVersion(long version) { - this.version = version; - } - - public String getStatus() { + public WatchStatus getStatus() { return status; } - private void setStatus(String status) { - this.status = status; - } - - @Override - public void readFrom(StreamInput in) throws IOException { - super.readFrom(in); - id = in.readString(); - version = in.readVLong(); - status = in.readString(); - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - super.writeTo(out); - out.writeString(id); - out.writeVLong(version); - out.writeString(status); + private static final ParseField STATUS_FIELD = new ParseField("status"); + private static final ConstructingObjectParser PARSER + = new ConstructingObjectParser<>("x_pack_deactivate_watch_response", true, + (fields) -> new DeactivateWatchResponse((WatchStatus) fields[0])); + static { + PARSER.declareObject(ConstructingObjectParser.constructorArg(), + (parser, context) -> WatchStatus.parse(parser), + STATUS_FIELD); } - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.startObject() - .field("_id", id) - .field("_version", version) - .field("status", status) - .endObject(); + public static DeactivateWatchResponse fromXContent(XContentParser parser) throws IOException { + return PARSER.parse(parser, null); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchResponseTests.java index 9e634527ccffb..68f4da896a2ba 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchResponseTests.java @@ -1,27 +1,36 @@ -package org.elasticsearch.protocol.xpack.watcher; +package org.elasticsearch.client.watcher; -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.test.AbstractXContentTestCase; + +import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.xcontent.*; +import org.elasticsearch.test.ESTestCase; import java.io.IOException; -public class DeactivateWatchResponseTests extends AbstractXContentTestCase { - @Override - protected DeactivateWatchResponse createTestInstance() { - String id = randomAlphaOfLength(10); - long version = randomLongBetween(1, 10); - // TODO: Randomized Status - String status = randomAlphaOfLength(10); - return new DeactivateWatchResponse(id, version, status); - } +public class DeactivateWatchResponseTests extends ESTestCase { - @Override - protected DeactivateWatchResponse doParseInstance(XContentParser parser) throws IOException { - return DeactivateWatchResponse.fromXContent(parser); + public void testBasicParsing() throws IOException { + XContentType contentType = randomFrom(XContentType.values()); + int version = randomInt(); + ExecutionState executionState = randomFrom(ExecutionState.values()); + XContentBuilder builder = XContentFactory.contentBuilder(contentType).startObject() + .startObject("status") + .field("version", version) + .field("execution_state", executionState) + .endObject() + .endObject(); + BytesReference bytes = BytesReference.bytes(builder); + DeactivateWatchResponse response = parse(contentType, bytes); + WatchStatus status = response.getStatus(); + assertNotNull(status); + assertEquals(version, status.version()); + assertEquals(executionState, status.getExecutionState()); } - @Override - protected boolean supportsUnknownFields() { - return false; + private DeactivateWatchResponse parse(XContentType contentType, BytesReference bytes) throws IOException { + XContentParser parser = XContentFactory.xContent(contentType) + .createParser(NamedXContentRegistry.EMPTY, null, bytes.streamInput()); + parser.nextToken(); + return DeactivateWatchResponse.fromXContent(parser); } } From e829881359020836c65eb19d71ed4865f04a23ea Mon Sep 17 00:00:00 2001 From: Mark Tozzi Date: Tue, 25 Sep 2018 13:15:08 -0400 Subject: [PATCH 06/14] Request validation & tests --- .../watcher/DeactivateWatchRequest.java | 23 +++++++++++++++- .../watcher/DeactivateWatchRequestTests.java | 26 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchRequestTests.java diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java index f87a4e3bd908a..7e3872c2c3c60 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java @@ -5,7 +5,13 @@ */ package org.elasticsearch.client.watcher; -public class DeactivateWatchRequest { +import org.elasticsearch.client.Validatable; +import org.elasticsearch.client.ValidationException; +import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; + +import java.util.Optional; + +public class DeactivateWatchRequest implements Validatable { private final String watchId; public DeactivateWatchRequest(String watchId) { @@ -15,4 +21,19 @@ public DeactivateWatchRequest(String watchId) { public String getWatchId() { return watchId; } + + @Override + public Optional validate() { + ValidationException exception = new ValidationException(); + + if (watchId == null) { + exception.addValidationError("watch id is missing"); + } else if (PutWatchRequest.isValidId(watchId) == false) { + exception.addValidationError("watch id contains whitespace"); + } + + return exception.validationErrors().isEmpty() + ? Optional.empty() // empty indicates no validation errors + : Optional.of(exception); + } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchRequestTests.java new file mode 100644 index 0000000000000..b5f862c3af329 --- /dev/null +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchRequestTests.java @@ -0,0 +1,26 @@ +package org.elasticsearch.client.watcher; + +import org.elasticsearch.client.ValidationException; +import org.elasticsearch.test.ESTestCase; + +import java.util.Optional; + +import static org.hamcrest.Matchers.hasItem; + +public class DeactivateWatchRequestTests extends ESTestCase { + + public void testNullId() { + DeactivateWatchRequest request = new DeactivateWatchRequest(null); + Optional actual = request.validate(); + assertTrue(actual.isPresent()); + assertThat(actual.get().validationErrors(), hasItem("watch id is missing")); + } + + public void testInvalidId() { + DeactivateWatchRequest request = new DeactivateWatchRequest("Watch Id with spaces"); + Optional actual = request.validate(); + assertTrue(actual.isPresent()); + assertThat(actual.get().validationErrors(), hasItem("watch id contains whitespace")); + } + +} From 6efaed5d8ba58fdfc9cb222983c500ffa92e4120 Mon Sep 17 00:00:00 2001 From: Mark Tozzi Date: Wed, 26 Sep 2018 10:08:10 -0400 Subject: [PATCH 07/14] Top level client methods --- .../elasticsearch/client/WatcherClient.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherClient.java index b1a3eb3f87bf9..698cc2fbbece4 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherClient.java @@ -19,6 +19,8 @@ package org.elasticsearch.client; import org.elasticsearch.action.ActionListener; +import org.elasticsearch.client.watcher.DeactivateWatchRequest; +import org.elasticsearch.client.watcher.DeactivateWatchResponse; import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest; import org.elasticsearch.protocol.xpack.watcher.DeleteWatchResponse; import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; @@ -65,6 +67,35 @@ public void putWatchAsync(PutWatchRequest request, RequestOptions options, PutWatchResponse::fromXContent, listener, emptySet()); } + /** + * Deactivate an existing watch + * See + * the docs for more. + * @param request the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @return the response + * @throws IOException in case there is a problem sending the request or parsing back the response + */ + public DeactivateWatchResponse deactivateWatch(DeactivateWatchRequest request, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseEntity(request, WatcherRequestConverters::deactivateWatch, options, + DeactivateWatchResponse::fromXContent, emptySet()); + } + + /** + * Asynchronously deactivate an existing watch + * See + * the docs for more. + * + * @param request the request + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized + * @param listener the listener to be notified upon request completion + */ + public void deactivateWatchAsync(DeactivateWatchRequest request, RequestOptions options, + ActionListener listener) { + restHighLevelClient.performRequestAsyncAndParseEntity(request, WatcherRequestConverters::deactivateWatch, options, + DeactivateWatchResponse::fromXContent, listener, emptySet()); + } + /** * Deletes a watch from the cluster * See From 476dc7ba5fbbedf1c8bdbec937f4401c94b0b19c Mon Sep 17 00:00:00 2001 From: Mark Tozzi Date: Wed, 26 Sep 2018 10:39:23 -0400 Subject: [PATCH 08/14] Fix license notices --- .../watcher/DeactivateWatchRequest.java | 19 +++++++++++++++--- .../watcher/DeactivateWatchResponse.java | 20 +++++++++++++++---- .../watcher/DeactivateWatchRequestTests.java | 19 ++++++++++++++++++ 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java index 7e3872c2c3c60..792d3e307f02e 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java @@ -1,7 +1,20 @@ /* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.elasticsearch.client.watcher; diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchResponse.java index ea98ff34c4a6b..e4158d920f6ed 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchResponse.java @@ -1,13 +1,25 @@ /* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ package org.elasticsearch.client.watcher; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.xcontent.ConstructingObjectParser; -import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.XContentParser; import java.io.IOException; diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchRequestTests.java index b5f862c3af329..223e7500eaeaf 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchRequestTests.java @@ -1,3 +1,22 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + package org.elasticsearch.client.watcher; import org.elasticsearch.client.ValidationException; From b0c977afc4694d0d036ee9e51440509e81beff0f Mon Sep 17 00:00:00 2001 From: Mark Tozzi Date: Wed, 26 Sep 2018 14:50:46 -0400 Subject: [PATCH 09/14] Fixed license & import style --- .../watcher/DeactivateWatchResponseTests.java | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchResponseTests.java index 68f4da896a2ba..dd56c8b054e64 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchResponseTests.java @@ -1,8 +1,30 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ package org.elasticsearch.client.watcher; import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.common.xcontent.*; +import org.elasticsearch.common.xcontent.NamedXContentRegistry; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.test.ESTestCase; import java.io.IOException; From b616452b2a7966d050d182f4257367f20e755a52 Mon Sep 17 00:00:00 2001 From: Mark Tozzi Date: Wed, 26 Sep 2018 15:48:50 -0400 Subject: [PATCH 10/14] Integration tests --- .../org/elasticsearch/client/WatcherIT.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherIT.java index 491992735afbf..44c985972d3eb 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherIT.java @@ -18,6 +18,9 @@ */ package org.elasticsearch.client; +import org.elasticsearch.ElasticsearchStatusException; +import org.elasticsearch.client.watcher.DeactivateWatchRequest; +import org.elasticsearch.client.watcher.DeactivateWatchResponse; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.XContentType; @@ -25,6 +28,7 @@ import org.elasticsearch.protocol.xpack.watcher.DeleteWatchResponse; import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; import org.elasticsearch.protocol.xpack.watcher.PutWatchResponse; +import org.elasticsearch.rest.RestStatus; import static org.hamcrest.Matchers.is; @@ -49,6 +53,25 @@ private PutWatchResponse createWatch(String watchId) throws Exception { return highLevelClient().watcher().putWatch(putWatchRequest, RequestOptions.DEFAULT); } + public void testDeactivateWatch() throws Exception { + // Deactivate a watch that exists + { + String watchId = randomAlphaOfLength(10); + createWatch(watchId); + DeactivateWatchResponse response = highLevelClient().watcher().deactivateWatch( + new DeactivateWatchRequest(watchId), RequestOptions.DEFAULT); + assertThat(response.getStatus().state().isActive(), is(false)); + } + // Deactivate a watch that does not exist + { + String watchId = randomAlphaOfLength(10); + ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, + () -> highLevelClient().watcher().deactivateWatch(new DeactivateWatchRequest(watchId), RequestOptions.DEFAULT)); + assertEquals(RestStatus.NOT_FOUND, exception.status()); + + } + } + public void testDeleteWatch() throws Exception { // delete watch that exists { From eb1be9709391d52e9380905ab7d2f1e84fd6338b Mon Sep 17 00:00:00 2001 From: Mark Tozzi Date: Wed, 3 Oct 2018 14:05:49 -0400 Subject: [PATCH 11/14] response to PR feedback --- .../watcher/DeactivateWatchRequest.java | 24 ++++++++-------- .../watcher/DeactivateWatchResponse.java | 28 +++++++++---------- .../org/elasticsearch/client/WatcherIT.java | 24 ++++++++-------- .../watcher/DeactivateWatchRequestTests.java | 16 ++++------- 4 files changed, 44 insertions(+), 48 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java index 792d3e307f02e..a6a13ddc512db 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java @@ -28,6 +28,17 @@ public class DeactivateWatchRequest implements Validatable { private final String watchId; public DeactivateWatchRequest(String watchId) { + + if (watchId == null) { + ValidationException exception = new ValidationException(); + exception.addValidationError("watch id is missing"); + throw exception; + } else if (PutWatchRequest.isValidId(watchId) == false) { + ValidationException exception = new ValidationException(); + exception.addValidationError("watch id contains whitespace"); + throw exception; + } + this.watchId = watchId; } @@ -35,18 +46,9 @@ public String getWatchId() { return watchId; } + // as per discussion https://github.com/elastic/elasticsearch/pull/34192/files#r221994527, keeping validate method as a no-op relic @Override public Optional validate() { - ValidationException exception = new ValidationException(); - - if (watchId == null) { - exception.addValidationError("watch id is missing"); - } else if (PutWatchRequest.isValidId(watchId) == false) { - exception.addValidationError("watch id contains whitespace"); - } - - return exception.validationErrors().isEmpty() - ? Optional.empty() // empty indicates no validation errors - : Optional.of(exception); + return Optional.empty(); // empty indicates no validation errors } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchResponse.java index e4158d920f6ed..08edd211d5b8f 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchResponse.java @@ -28,6 +28,20 @@ public class DeactivateWatchResponse { private WatchStatus status; + private static final ParseField STATUS_FIELD = new ParseField("status"); + private static final ConstructingObjectParser PARSER + = new ConstructingObjectParser<>("x_pack_deactivate_watch_response", true, + (fields) -> new DeactivateWatchResponse((WatchStatus) fields[0])); + static { + PARSER.declareObject(ConstructingObjectParser.constructorArg(), + (parser, context) -> WatchStatus.parse(parser), + STATUS_FIELD); + } + + public static DeactivateWatchResponse fromXContent(XContentParser parser) throws IOException { + return PARSER.parse(parser, null); + } + public DeactivateWatchResponse(WatchStatus status) { this.status = status; } @@ -48,18 +62,4 @@ public int hashCode() { public WatchStatus getStatus() { return status; } - - private static final ParseField STATUS_FIELD = new ParseField("status"); - private static final ConstructingObjectParser PARSER - = new ConstructingObjectParser<>("x_pack_deactivate_watch_response", true, - (fields) -> new DeactivateWatchResponse((WatchStatus) fields[0])); - static { - PARSER.declareObject(ConstructingObjectParser.constructorArg(), - (parser, context) -> WatchStatus.parse(parser), - STATUS_FIELD); - } - - public static DeactivateWatchResponse fromXContent(XContentParser parser) throws IOException { - return PARSER.parse(parser, null); - } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherIT.java index 44c985972d3eb..c44c2681c313e 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherIT.java @@ -55,21 +55,19 @@ private PutWatchResponse createWatch(String watchId) throws Exception { public void testDeactivateWatch() throws Exception { // Deactivate a watch that exists - { - String watchId = randomAlphaOfLength(10); - createWatch(watchId); - DeactivateWatchResponse response = highLevelClient().watcher().deactivateWatch( - new DeactivateWatchRequest(watchId), RequestOptions.DEFAULT); - assertThat(response.getStatus().state().isActive(), is(false)); - } + String watchId = randomAlphaOfLength(10); + createWatch(watchId); + DeactivateWatchResponse response = highLevelClient().watcher().deactivateWatch( + new DeactivateWatchRequest(watchId), RequestOptions.DEFAULT); + assertThat(response.getStatus().state().isActive(), is(false)); + } + public void testDeactivateWatch404() throws Exception { // Deactivate a watch that does not exist - { - String watchId = randomAlphaOfLength(10); - ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, - () -> highLevelClient().watcher().deactivateWatch(new DeactivateWatchRequest(watchId), RequestOptions.DEFAULT)); - assertEquals(RestStatus.NOT_FOUND, exception.status()); + String watchId = randomAlphaOfLength(10); + ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, + () -> highLevelClient().watcher().deactivateWatch(new DeactivateWatchRequest(watchId), RequestOptions.DEFAULT)); + assertEquals(RestStatus.NOT_FOUND, exception.status()); - } } public void testDeleteWatch() throws Exception { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchRequestTests.java index 223e7500eaeaf..cc6c9aad3705c 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchRequestTests.java @@ -22,24 +22,20 @@ import org.elasticsearch.client.ValidationException; import org.elasticsearch.test.ESTestCase; -import java.util.Optional; - import static org.hamcrest.Matchers.hasItem; public class DeactivateWatchRequestTests extends ESTestCase { public void testNullId() { - DeactivateWatchRequest request = new DeactivateWatchRequest(null); - Optional actual = request.validate(); - assertTrue(actual.isPresent()); - assertThat(actual.get().validationErrors(), hasItem("watch id is missing")); + ValidationException actual = expectThrows(ValidationException.class, () -> new DeactivateWatchRequest(null)); + assertNotNull(actual); + assertThat(actual.validationErrors(), hasItem("watch id is missing")); } public void testInvalidId() { - DeactivateWatchRequest request = new DeactivateWatchRequest("Watch Id with spaces"); - Optional actual = request.validate(); - assertTrue(actual.isPresent()); - assertThat(actual.get().validationErrors(), hasItem("watch id contains whitespace")); + ValidationException actual = expectThrows(ValidationException.class, () -> new DeactivateWatchRequest("Watch id has spaces")); + assertNotNull(actual); + assertThat(actual.validationErrors(), hasItem("watch id contains whitespace")); } } From 2076f9428fa7fde4a2e4994d0fb5014a583b9471 Mon Sep 17 00:00:00 2001 From: Mark Tozzi Date: Wed, 3 Oct 2018 17:18:48 -0400 Subject: [PATCH 12/14] Docs & doc tests --- .../documentation/WatcherDocumentationIT.java | 54 +++++++++++++++++++ .../high-level/supported-apis.asciidoc | 2 + .../watcher/deactivate-watch.asciidoc | 10 ++++ 3 files changed, 66 insertions(+) create mode 100644 docs/java-rest/high-level/watcher/deactivate-watch.asciidoc diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/WatcherDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/WatcherDocumentationIT.java index 48052f86a0063..46ba76910474f 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/WatcherDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/WatcherDocumentationIT.java @@ -29,6 +29,8 @@ import org.elasticsearch.client.watcher.AckWatchResponse; import org.elasticsearch.client.watcher.ActionStatus; import org.elasticsearch.client.watcher.ActionStatus.AckStatus; +import org.elasticsearch.client.watcher.DeactivateWatchRequest; +import org.elasticsearch.client.watcher.DeactivateWatchResponse; import org.elasticsearch.client.watcher.WatchStatus; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; @@ -42,6 +44,8 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import static org.hamcrest.Matchers.is; + public class WatcherDocumentationIT extends ESRestHighLevelClientTestCase { public void testWatcher() throws Exception { @@ -203,4 +207,54 @@ public void onFailure(Exception e) { } } + public void testDeactivateWatch() throws Exception { + RestHighLevelClient client = highLevelClient(); + + { + BytesReference watch = new BytesArray("{ \n" + + " \"trigger\": { \"schedule\": { \"interval\": \"10h\" } },\n" + + " \"input\": { \"simple\": { \"foo\" : \"bar\" } },\n" + + " \"actions\": { \"logme\": { \"logging\": { \"text\": \"{{ctx.payload}}\" } } }\n" + + "}"); + PutWatchRequest putWatchRequest = new PutWatchRequest("my_watch_id", watch, XContentType.JSON); + client.watcher().putWatch(putWatchRequest, RequestOptions.DEFAULT); + } + + { + //tag::deactivate-watch-execute + DeactivateWatchRequest request = new DeactivateWatchRequest("my_watch_id"); + DeactivateWatchResponse response = client.watcher().deactivateWatch(request, RequestOptions.DEFAULT); + //end::deactivate-watch-execute + + assertThat(response.getStatus().state().isActive(), is(false)); + } + + { + DeactivateWatchRequest request = new DeactivateWatchRequest("my_watch_id"); + // tag::deactivate-watch-execute-listener + ActionListener listener = new ActionListener() { + @Override + public void onResponse(DeactivateWatchResponse response) { + // <1> + } + + @Override + public void onFailure(Exception e) { + // <2> + } + }; + // end::deactivate-watch-execute-listener + + // For testing, replace the empty listener by a blocking listener. + final CountDownLatch latch = new CountDownLatch(1); + listener = new LatchedActionListener<>(listener, latch); + + // tag::deactivate-watch-execute-async + client.watcher().deactivateWatchAsync(request, RequestOptions.DEFAULT, listener); // <1> + // end::deactivate-watch-execute-async + + assertTrue(latch.await(30L, TimeUnit.SECONDS)); + } + } + } diff --git a/docs/java-rest/high-level/supported-apis.asciidoc b/docs/java-rest/high-level/supported-apis.asciidoc index bb326cbb9c66d..6640c167825eb 100644 --- a/docs/java-rest/high-level/supported-apis.asciidoc +++ b/docs/java-rest/high-level/supported-apis.asciidoc @@ -311,10 +311,12 @@ The Java High Level REST Client supports the following Watcher APIs: * <> * <> * <> +* <> include::watcher/put-watch.asciidoc[] include::watcher/delete-watch.asciidoc[] include::watcher/ack-watch.asciidoc[] +include::watcher/deactivate-watch.asciidoc[] == Graph APIs diff --git a/docs/java-rest/high-level/watcher/deactivate-watch.asciidoc b/docs/java-rest/high-level/watcher/deactivate-watch.asciidoc new file mode 100644 index 0000000000000..673423b69b983 --- /dev/null +++ b/docs/java-rest/high-level/watcher/deactivate-watch.asciidoc @@ -0,0 +1,10 @@ +-- +:api: deactivate-watch +:request: deactivateWatchRequet +:response: deactivateWatchResponse +:doc-tests-file: {doc-tests}/WatcherDocumentationIT.java +-- +[[java-rest-high-watcher-deactivate-watch]] +=== Deactivate Watch API + +include::../execution.asciidoc[] From e441b66ecc6dc69fc0f0827fd587dc9ecdd35a49 Mon Sep 17 00:00:00 2001 From: Mark Tozzi Date: Mon, 22 Oct 2018 13:33:13 -0400 Subject: [PATCH 13/14] response to PR feedback --- .../watcher/DeactivateWatchRequest.java | 19 +++---------------- .../watcher/DeactivateWatchRequestTests.java | 12 +++--------- 2 files changed, 6 insertions(+), 25 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java index a6a13ddc512db..f6f88b087c697 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java @@ -22,6 +22,7 @@ import org.elasticsearch.client.ValidationException; import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; +import java.util.Objects; import java.util.Optional; public class DeactivateWatchRequest implements Validatable { @@ -29,26 +30,12 @@ public class DeactivateWatchRequest implements Validatable { public DeactivateWatchRequest(String watchId) { - if (watchId == null) { - ValidationException exception = new ValidationException(); - exception.addValidationError("watch id is missing"); - throw exception; - } else if (PutWatchRequest.isValidId(watchId) == false) { - ValidationException exception = new ValidationException(); - exception.addValidationError("watch id contains whitespace"); - throw exception; - } - + Objects.requireNonNull(watchId, "watch id is missing"); this.watchId = watchId; } public String getWatchId() { return watchId; } - - // as per discussion https://github.com/elastic/elasticsearch/pull/34192/files#r221994527, keeping validate method as a no-op relic - @Override - public Optional validate() { - return Optional.empty(); // empty indicates no validation errors - } } + diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchRequestTests.java index cc6c9aad3705c..c3890ebbf2317 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchRequestTests.java @@ -23,19 +23,13 @@ import org.elasticsearch.test.ESTestCase; import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.is; public class DeactivateWatchRequestTests extends ESTestCase { public void testNullId() { - ValidationException actual = expectThrows(ValidationException.class, () -> new DeactivateWatchRequest(null)); + NullPointerException actual = expectThrows(NullPointerException.class, () -> new DeactivateWatchRequest(null)); assertNotNull(actual); - assertThat(actual.validationErrors(), hasItem("watch id is missing")); + assertThat(actual.getMessage(), is("watch id is missing")); } - - public void testInvalidId() { - ValidationException actual = expectThrows(ValidationException.class, () -> new DeactivateWatchRequest("Watch id has spaces")); - assertNotNull(actual); - assertThat(actual.validationErrors(), hasItem("watch id contains whitespace")); - } - } From febaee04d10862932d7056a4ebcf2e5121bd3e2b Mon Sep 17 00:00:00 2001 From: Mark Tozzi Date: Mon, 22 Oct 2018 16:25:03 -0400 Subject: [PATCH 14/14] Revised validation logic --- .../client/watcher/DeactivateWatchRequest.java | 6 ++++-- .../client/watcher/DeactivateWatchRequestTests.java | 10 ++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java index f6f88b087c697..b20a56c361f8f 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/DeactivateWatchRequest.java @@ -19,11 +19,9 @@ package org.elasticsearch.client.watcher; import org.elasticsearch.client.Validatable; -import org.elasticsearch.client.ValidationException; import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; import java.util.Objects; -import java.util.Optional; public class DeactivateWatchRequest implements Validatable { private final String watchId; @@ -31,6 +29,10 @@ public class DeactivateWatchRequest implements Validatable { public DeactivateWatchRequest(String watchId) { Objects.requireNonNull(watchId, "watch id is missing"); + if (PutWatchRequest.isValidId(watchId) == false) { + throw new IllegalArgumentException("watch id contains whitespace"); + } + this.watchId = watchId; } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchRequestTests.java index c3890ebbf2317..d92a51f96c26a 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/DeactivateWatchRequestTests.java @@ -19,10 +19,8 @@ package org.elasticsearch.client.watcher; -import org.elasticsearch.client.ValidationException; import org.elasticsearch.test.ESTestCase; -import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.is; public class DeactivateWatchRequestTests extends ESTestCase { @@ -32,4 +30,12 @@ public void testNullId() { assertNotNull(actual); assertThat(actual.getMessage(), is("watch id is missing")); } + + public void testInvalidId() { + IllegalArgumentException actual = expectThrows(IllegalArgumentException.class, + () -> new DeactivateWatchRequest("Watch id has spaces")); + assertNotNull(actual); + assertThat(actual.getMessage(), is("watch id contains whitespace")); + } + }