21
21
import org .elasticsearch .common .bytes .BytesArray ;
22
22
import org .elasticsearch .common .bytes .BytesReference ;
23
23
import org .elasticsearch .common .xcontent .XContentType ;
24
+ import org .elasticsearch .protocol .xpack .watcher .DeleteWatchRequest ;
25
+ import org .elasticsearch .protocol .xpack .watcher .DeleteWatchResponse ;
24
26
import org .elasticsearch .protocol .xpack .watcher .PutWatchRequest ;
25
27
import org .elasticsearch .protocol .xpack .watcher .PutWatchResponse ;
26
28
@@ -30,17 +32,44 @@ public class WatcherIT extends ESRestHighLevelClientTestCase {
30
32
31
33
public void testPutWatch () throws Exception {
32
34
String watchId = randomAlphaOfLength (10 );
35
+ PutWatchResponse putWatchResponse = createWatch (watchId );
36
+ assertThat (putWatchResponse .isCreated (), is (true ));
37
+ assertThat (putWatchResponse .getId (), is (watchId ));
38
+ assertThat (putWatchResponse .getVersion (), is (1L ));
39
+ }
40
+
41
+ private PutWatchResponse createWatch (String watchId ) throws Exception {
33
42
String json = "{ \n " +
34
43
" \" trigger\" : { \" schedule\" : { \" interval\" : \" 10h\" } },\n " +
35
44
" \" input\" : { \" none\" : {} },\n " +
36
45
" \" actions\" : { \" logme\" : { \" logging\" : { \" text\" : \" {{ctx.payload}}\" } } }\n " +
37
46
"}" ;
38
47
BytesReference bytesReference = new BytesArray (json );
39
48
PutWatchRequest putWatchRequest = new PutWatchRequest (watchId , bytesReference , XContentType .JSON );
40
- PutWatchResponse putWatchResponse = highLevelClient ().xpack ().watcher ().putWatch (putWatchRequest , RequestOptions .DEFAULT );
41
- assertThat (putWatchResponse .isCreated (), is (true ));
42
- assertThat (putWatchResponse .getId (), is (watchId ));
43
- assertThat (putWatchResponse .getVersion (), is (1L ));
49
+ return highLevelClient ().xpack ().watcher ().putWatch (putWatchRequest , RequestOptions .DEFAULT );
50
+ }
51
+
52
+ public void testDeleteWatch () throws Exception {
53
+ // delete watch that exists
54
+ {
55
+ String watchId = randomAlphaOfLength (10 );
56
+ createWatch (watchId );
57
+ DeleteWatchResponse deleteWatchResponse = highLevelClient ().xpack ().watcher ().deleteWatch (new DeleteWatchRequest (watchId ),
58
+ RequestOptions .DEFAULT );
59
+ assertThat (deleteWatchResponse .getId (), is (watchId ));
60
+ assertThat (deleteWatchResponse .getVersion (), is (2L ));
61
+ assertThat (deleteWatchResponse .isFound (), is (true ));
62
+ }
63
+
64
+ // delete watch that does not exist
65
+ {
66
+ String watchId = randomAlphaOfLength (10 );
67
+ DeleteWatchResponse deleteWatchResponse = highLevelClient ().xpack ().watcher ().deleteWatch (new DeleteWatchRequest (watchId ),
68
+ RequestOptions .DEFAULT );
69
+ assertThat (deleteWatchResponse .getId (), is (watchId ));
70
+ assertThat (deleteWatchResponse .getVersion (), is (1L ));
71
+ assertThat (deleteWatchResponse .isFound (), is (false ));
72
+ }
44
73
}
45
74
46
75
}
0 commit comments