Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HLRC: Deactivate Watch API #34192

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6a6df68
Lay out protocol classes
not-napoleon Sep 21, 2018
cb92865
Merge branch 'master' into feature/hlrc-deactivate-watch
not-napoleon Sep 21, 2018
22ebecb
Serialization for protocol side response class
not-napoleon Sep 24, 2018
f52c5fa
fixed paths & packages to new standard
not-napoleon Sep 24, 2018
be75157
request parsing
not-napoleon Sep 25, 2018
590ae61
Response parsing & basic test
not-napoleon Sep 25, 2018
e829881
Request validation & tests
not-napoleon Sep 25, 2018
6efaed5
Top level client methods
not-napoleon Sep 26, 2018
476dc7b
Fix license notices
not-napoleon Sep 26, 2018
b0c977a
Fixed license & import style
not-napoleon Sep 26, 2018
b616452
Integration tests
not-napoleon Sep 26, 2018
8c3d516
Merge branch 'master' into feature/hlrc-deactivate-watch
not-napoleon Oct 1, 2018
0214d1b
Merge branch 'master' into feature/hlrc-deactivate-watch
not-napoleon Oct 2, 2018
eb1be97
response to PR feedback
not-napoleon Oct 3, 2018
3d1b605
Merge branch 'master' into feature/hlrc-deactivate-watch
not-napoleon Oct 3, 2018
2076f94
Docs & doc tests
not-napoleon Oct 3, 2018
4bc9bcb
Merge branch 'master' into feature/hlrc-deactivate-watch
not-napoleon Oct 4, 2018
5cdb181
Merge branch 'master' into feature/hlrc-deactivate-watch
not-napoleon Oct 10, 2018
707434a
Merge branch 'master' into feature/hlrc-deactivate-watch
not-napoleon Oct 22, 2018
e441b66
response to PR feedback
not-napoleon Oct 22, 2018
febaee0
Revised validation logic
not-napoleon Oct 22, 2018
61a9388
Merge branch 'master' into feature/hlrc-deactivate-watch
not-napoleon Oct 23, 2018
f7b7b16
Merge branch 'master' into feature/hlrc-deactivate-watch
not-napoleon Oct 24, 2018
345cecc
Merge branch 'master' into feature/hlrc-deactivate-watch
not-napoleon Oct 24, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,27 @@ public class DeactivateWatchRequest implements Validatable {
private final String watchId;

public DeactivateWatchRequest(String watchId) {

if (watchId == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I did not explain myself well enuf. This is good, but we dont need the ValidationException here at all, really. You can instead just use Objects.requireNotNull(obj, msg) and it will throw exceptions for you. its more concise.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so does that mean we don't need to check for white space in the watch id (via PutWatchRequest.isValidId())?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also validate this as well in the constructor.

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;
}

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to just remove the comment here and the whole method instead of just returning empty since the default does this already.

@Override
public Optional<ValidationException> 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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@
public class DeactivateWatchResponse {
private WatchStatus status;

private static final ParseField STATUS_FIELD = new ParseField("status");
private static final ConstructingObjectParser<DeactivateWatchResponse, Void> 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;
}
Expand All @@ -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<DeactivateWatchResponse, Void> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worth adding a second test case for imho

{
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ValidationException> 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<ValidationException> 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"));
}

}