6
6
package org .elasticsearch .xpack .watcher ;
7
7
8
8
import org .elasticsearch .Version ;
9
+ import org .elasticsearch .action .ActionListener ;
10
+ import org .elasticsearch .action .admin .indices .refresh .RefreshAction ;
9
11
import org .elasticsearch .action .admin .indices .refresh .RefreshRequest ;
10
12
import org .elasticsearch .action .admin .indices .refresh .RefreshResponse ;
13
+ import org .elasticsearch .action .search .ClearScrollAction ;
11
14
import org .elasticsearch .action .search .ClearScrollRequest ;
12
15
import org .elasticsearch .action .search .ClearScrollResponse ;
16
+ import org .elasticsearch .action .search .SearchAction ;
13
17
import org .elasticsearch .action .search .SearchRequest ;
14
18
import org .elasticsearch .action .search .SearchResponse ;
15
19
import org .elasticsearch .action .search .SearchResponseSections ;
20
+ import org .elasticsearch .action .search .SearchScrollAction ;
16
21
import org .elasticsearch .action .search .SearchScrollRequest ;
17
22
import org .elasticsearch .action .search .ShardSearchFailure ;
18
- import org .elasticsearch .action .support .PlainActionFuture ;
19
- import org .elasticsearch .client .AdminClient ;
20
23
import org .elasticsearch .client .Client ;
21
- import org .elasticsearch .client .IndicesAdminClient ;
22
24
import org .elasticsearch .cluster .ClusterName ;
23
25
import org .elasticsearch .cluster .ClusterState ;
24
26
import org .elasticsearch .cluster .metadata .IndexMetaData ;
42
44
import org .elasticsearch .search .SearchShardTarget ;
43
45
import org .elasticsearch .test .ESTestCase ;
44
46
import org .elasticsearch .threadpool .ThreadPool ;
45
- import org .elasticsearch .xpack .core .XPackSettings ;
46
47
import org .elasticsearch .xpack .core .watcher .trigger .Trigger ;
47
48
import org .elasticsearch .xpack .core .watcher .watch .Watch ;
48
49
import org .elasticsearch .xpack .core .watcher .watch .WatchStatus ;
55
56
import org .elasticsearch .xpack .watcher .watch .WatchParser ;
56
57
import org .joda .time .DateTime ;
57
58
import org .joda .time .DateTimeZone ;
59
+ import org .junit .Before ;
58
60
import org .mockito .ArgumentCaptor ;
59
61
60
62
import java .util .Collections ;
67
69
import static org .hamcrest .Matchers .is ;
68
70
import static org .mockito .Matchers .any ;
69
71
import static org .mockito .Matchers .eq ;
72
+ import static org .mockito .Mockito .doAnswer ;
70
73
import static org .mockito .Mockito .mock ;
71
74
import static org .mockito .Mockito .never ;
72
75
import static org .mockito .Mockito .verify ;
@@ -76,14 +79,24 @@ public class WatcherServiceTests extends ESTestCase {
76
79
77
80
private final ExecutorService executorService = EsExecutors .newDirectExecutorService ();
78
81
82
+ private final Client client = mock (Client .class );
83
+
84
+ @ Before
85
+ public void configureMockClient () {
86
+ when (client .settings ()).thenReturn (Settings .EMPTY );
87
+ ThreadPool threadPool = mock (ThreadPool .class );
88
+ when (client .threadPool ()).thenReturn (threadPool );
89
+ when (threadPool .getThreadContext ()).thenReturn (new ThreadContext (Settings .EMPTY ));
90
+ }
91
+
79
92
public void testValidateStartWithClosedIndex () {
80
93
TriggerService triggerService = mock (TriggerService .class );
81
94
TriggeredWatchStore triggeredWatchStore = mock (TriggeredWatchStore .class );
82
95
ExecutionService executionService = mock (ExecutionService .class );
83
96
WatchParser parser = mock (WatchParser .class );
84
97
85
98
WatcherService service = new WatcherService (Settings .EMPTY , triggerService , triggeredWatchStore ,
86
- executionService , parser , mock ( Client . class ) , executorService ) {
99
+ executionService , parser , client , executorService ) {
87
100
@ Override
88
101
void stopExecutor () {
89
102
}
@@ -102,18 +115,11 @@ void stopExecutor() {
102
115
}
103
116
104
117
public void testLoadOnlyActiveWatches () throws Exception {
105
- // this is just, so we dont have to add any mocking to the threadpool
106
- Settings settings = Settings .builder ().put (XPackSettings .SECURITY_ENABLED .getKey (), false ).build ();
107
-
108
118
TriggerService triggerService = mock (TriggerService .class );
109
119
TriggeredWatchStore triggeredWatchStore = mock (TriggeredWatchStore .class );
110
120
ExecutionService executionService = mock (ExecutionService .class );
111
121
WatchParser parser = mock (WatchParser .class );
112
- Client client = mock (Client .class );
113
- ThreadPool threadPool = mock (ThreadPool .class );
114
- when (client .threadPool ()).thenReturn (threadPool );
115
- when (threadPool .getThreadContext ()).thenReturn (new ThreadContext (Settings .EMPTY ));
116
- WatcherService service = new WatcherService (settings , triggerService , triggeredWatchStore ,
122
+ WatcherService service = new WatcherService (Settings .EMPTY , triggerService , triggeredWatchStore ,
117
123
executionService , parser , client , executorService ) {
118
124
@ Override
119
125
void stopExecutor () {
@@ -150,21 +156,21 @@ void stopExecutor() {
150
156
RefreshResponse refreshResponse = mock (RefreshResponse .class );
151
157
when (refreshResponse .getSuccessfulShards ())
152
158
.thenReturn (clusterState .getMetaData ().getIndices ().get (Watch .INDEX ).getNumberOfShards ());
153
- AdminClient adminClient = mock (AdminClient .class );
154
- IndicesAdminClient indicesAdminClient = mock (IndicesAdminClient .class );
155
- when (client .admin ()).thenReturn (adminClient );
156
- when (adminClient .indices ()).thenReturn (indicesAdminClient );
157
- PlainActionFuture <RefreshResponse > refreshFuture = new PlainActionFuture <>();
158
- when (indicesAdminClient .refresh (any (RefreshRequest .class ))).thenReturn (refreshFuture );
159
- refreshFuture .onResponse (refreshResponse );
159
+ doAnswer (invocation -> {
160
+ ActionListener <RefreshResponse > listener = (ActionListener <RefreshResponse >) invocation .getArguments ()[2 ];
161
+ listener .onResponse (refreshResponse );
162
+ return null ;
163
+ }).when (client ).execute (eq (RefreshAction .INSTANCE ), any (RefreshRequest .class ), any (ActionListener .class ));
160
164
161
165
// empty scroll response, no further scrolling needed
162
166
SearchResponseSections scrollSearchSections = new SearchResponseSections (SearchHits .empty (), null , null , false , false , null , 1 );
163
167
SearchResponse scrollSearchResponse = new SearchResponse (scrollSearchSections , "scrollId" , 1 , 1 , 0 , 10 ,
164
168
ShardSearchFailure .EMPTY_ARRAY , SearchResponse .Clusters .EMPTY );
165
- PlainActionFuture <SearchResponse > searchScrollResponseFuture = new PlainActionFuture <>();
166
- when (client .searchScroll (any (SearchScrollRequest .class ))).thenReturn (searchScrollResponseFuture );
167
- searchScrollResponseFuture .onResponse (scrollSearchResponse );
169
+ doAnswer (invocation -> {
170
+ ActionListener <SearchResponse > listener = (ActionListener <SearchResponse >) invocation .getArguments ()[2 ];
171
+ listener .onResponse (scrollSearchResponse );
172
+ return null ;
173
+ }).when (client ).execute (eq (SearchScrollAction .INSTANCE ), any (SearchScrollRequest .class ), any (ActionListener .class ));
168
174
169
175
// one search response containing active and inactive watches
170
176
int count = randomIntBetween (2 , 200 );
@@ -192,13 +198,17 @@ void stopExecutor() {
192
198
SearchResponseSections sections = new SearchResponseSections (searchHits , null , null , false , false , null , 1 );
193
199
SearchResponse searchResponse = new SearchResponse (sections , "scrollId" , 1 , 1 , 0 , 10 , ShardSearchFailure .EMPTY_ARRAY ,
194
200
SearchResponse .Clusters .EMPTY );
195
- PlainActionFuture <SearchResponse > searchResponseFuture = new PlainActionFuture <>();
196
- when (client .search (any (SearchRequest .class ))).thenReturn (searchResponseFuture );
197
- searchResponseFuture .onResponse (searchResponse );
198
-
199
- PlainActionFuture <ClearScrollResponse > clearScrollFuture = new PlainActionFuture <>();
200
- when (client .clearScroll (any (ClearScrollRequest .class ))).thenReturn (clearScrollFuture );
201
- clearScrollFuture .onResponse (new ClearScrollResponse (true , 1 ));
201
+ doAnswer (invocation -> {
202
+ ActionListener <SearchResponse > listener = (ActionListener <SearchResponse >) invocation .getArguments ()[2 ];
203
+ listener .onResponse (searchResponse );
204
+ return null ;
205
+ }).when (client ).execute (eq (SearchAction .INSTANCE ), any (SearchRequest .class ), any (ActionListener .class ));
206
+
207
+ doAnswer (invocation -> {
208
+ ActionListener <ClearScrollResponse > listener = (ActionListener <ClearScrollResponse >) invocation .getArguments ()[2 ];
209
+ listener .onResponse (new ClearScrollResponse (true , 1 ));
210
+ return null ;
211
+ }).when (client ).execute (eq (ClearScrollAction .INSTANCE ), any (ClearScrollRequest .class ), any (ActionListener .class ));
202
212
203
213
service .start (clusterState , () -> {});
204
214
@@ -228,7 +238,7 @@ public void testPausingWatcherServiceAlsoPausesTriggerService() {
228
238
assertThat (triggerService .count (), is (1L ));
229
239
230
240
WatcherService service = new WatcherService (Settings .EMPTY , triggerService , mock (TriggeredWatchStore .class ),
231
- mock (ExecutionService .class ), mock (WatchParser .class ), mock ( Client . class ) , executorService ) {
241
+ mock (ExecutionService .class ), mock (WatchParser .class ), client , executorService ) {
232
242
@ Override
233
243
void stopExecutor () {
234
244
}
@@ -245,7 +255,7 @@ public void testReloadingWatcherDoesNotPauseExecutionService() {
245
255
ExecutionService executionService = mock (ExecutionService .class );
246
256
TriggerService triggerService = mock (TriggerService .class );
247
257
WatcherService service = new WatcherService (Settings .EMPTY , triggerService , mock (TriggeredWatchStore .class ),
248
- executionService , mock (WatchParser .class ), mock ( Client . class ) , executorService ) {
258
+ executionService , mock (WatchParser .class ), client , executorService ) {
249
259
@ Override
250
260
void stopExecutor () {
251
261
}
0 commit comments