Skip to content

Commit 91f917c

Browse files
committed
Move request validation into the constructor.
1 parent 5848d9a commit 91f917c

File tree

2 files changed

+29
-31
lines changed

2 files changed

+29
-31
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/AckWatchRequest.java

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest;
2525

2626
import java.util.Locale;
27-
import java.util.Optional;
2827

2928
/**
3029
* A request to explicitly acknowledge a watch.
@@ -35,29 +34,13 @@ public class AckWatchRequest implements Validatable {
3534
private final String[] actionIds;
3635

3736
public AckWatchRequest(String watchId, String... actionIds) {
37+
validateIds(watchId, actionIds);
3838
this.watchId = watchId;
3939
this.actionIds = actionIds;
4040
}
4141

42-
/**
43-
* @return The ID of the watch to be acked.
44-
*/
45-
public String getWatchId() {
46-
return watchId;
47-
}
48-
49-
/**
50-
* @return The IDs of the actions to be acked. If omitted,
51-
* all actions for the given watch will be acknowledged.
52-
*/
53-
public String[] getActionIds() {
54-
return actionIds;
55-
}
56-
57-
@Override
58-
public Optional<ValidationException> validate() {
42+
private void validateIds(String watchId, String... actionIds) {
5943
ValidationException exception = new ValidationException();
60-
6144
if (watchId == null) {
6245
exception.addValidationError("watch id is missing");
6346
} else if (PutWatchRequest.isValidId(watchId) == false) {
@@ -75,9 +58,24 @@ public Optional<ValidationException> validate() {
7558
}
7659
}
7760

78-
return !exception.validationErrors().isEmpty()
79-
? Optional.of(exception)
80-
: Optional.empty();
61+
if (!exception.validationErrors().isEmpty()) {
62+
throw exception;
63+
}
64+
}
65+
66+
/**
67+
* @return The ID of the watch to be acked.
68+
*/
69+
public String getWatchId() {
70+
return watchId;
71+
}
72+
73+
/**
74+
* @return The IDs of the actions to be acked. If omitted,
75+
* all actions for the given watch will be acknowledged.
76+
*/
77+
public String[] getActionIds() {
78+
return actionIds;
8179
}
8280

8381
@Override

client/rest-high-level/src/test/java/org/elasticsearch/client/watcher/WatchRequestValidationTests.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@
3636
public class WatchRequestValidationTests extends ESTestCase {
3737

3838
public void testAcknowledgeWatchInvalidWatchId() {
39-
Optional<ValidationException> e = new AckWatchRequest("id with whitespaces").validate();
40-
assertTrue(e.isPresent());
41-
assertThat(e.get().validationErrors(), hasItem("watch id contains whitespace"));
39+
ValidationException e = expectThrows(ValidationException.class,
40+
() -> new AckWatchRequest("id with whitespaces"));
41+
assertThat(e.validationErrors(), hasItem("watch id contains whitespace"));
4242
}
4343

4444
public void testAcknowledgeWatchInvalidActionId() {
45-
Optional<ValidationException> e = new AckWatchRequest("_id", "action id with whitespaces").validate();
46-
assertTrue(e.isPresent());
47-
assertThat(e.get().validationErrors(), hasItem("action id [action id with whitespaces] contains whitespace"));
45+
ValidationException e = expectThrows(ValidationException.class,
46+
() -> new AckWatchRequest("_id", "action id with whitespaces"));
47+
assertThat(e.validationErrors(), hasItem("action id [action id with whitespaces] contains whitespace"));
4848
}
4949

5050
public void testAcknowledgeWatchNullActionArray() {
@@ -55,9 +55,9 @@ public void testAcknowledgeWatchNullActionArray() {
5555
}
5656

5757
public void testAcknowledgeWatchNullActionId() {
58-
Optional<ValidationException> e = new AckWatchRequest("_id", new String[] {null}).validate();
59-
assertTrue(e.isPresent());
60-
assertThat(e.get().validationErrors(), hasItem("action id may not be null"));
58+
ValidationException e = expectThrows(ValidationException.class,
59+
() -> new AckWatchRequest("_id", new String[] {null}));
60+
assertThat(e.validationErrors(), hasItem("action id may not be null"));
6161
}
6262

6363
public void testDeleteWatchInvalidWatchId() {

0 commit comments

Comments
 (0)