11
11
import org .apache .http .StatusLine ;
12
12
import org .apache .http .entity .ContentType ;
13
13
import org .apache .http .entity .StringEntity ;
14
+ import org .elasticsearch .client .Request ;
14
15
import org .elasticsearch .client .Response ;
15
16
import org .elasticsearch .client .ResponseException ;
16
17
import org .elasticsearch .client .RestClient ;
20
21
import org .elasticsearch .rest .RestStatus ;
21
22
import org .elasticsearch .test .ESTestCase ;
22
23
import org .elasticsearch .xpack .monitoring .exporter .http .PublishableHttpResource .CheckResponse ;
24
+ import org .mockito .ArgumentCaptor ;
25
+ import org .mockito .Mockito ;
23
26
24
27
import java .io .IOException ;
25
28
import java .util .Map ;
30
33
import static org .elasticsearch .xpack .monitoring .exporter .http .PublishableHttpResource .GET_DOES_NOT_EXIST ;
31
34
import static org .elasticsearch .xpack .monitoring .exporter .http .PublishableHttpResource .GET_EXISTS ;
32
35
import static org .hamcrest .Matchers .is ;
33
- import static org .mockito .Matchers .any ;
34
- import static org .mockito .Matchers .eq ;
36
+ import static org .hamcrest .Matchers .instanceOf ;
35
37
import static org .mockito .Mockito .mock ;
36
38
import static org .mockito .Mockito .when ;
39
+ import static org .mockito .Mockito .verify ;
37
40
38
41
/**
39
42
* Base test helper for any {@link PublishableHttpResource}.
@@ -87,7 +90,9 @@ protected void assertCheckWithException(final PublishableHttpResource resource,
87
90
final ResponseException responseException = responseException ("GET" , endpoint , failedCheckStatus ());
88
91
final Exception e = randomFrom (new IOException ("expected" ), new RuntimeException ("expected" ), responseException );
89
92
90
- when (client .performRequest ("GET" , endpoint , getParameters (resource .getParameters ()))).thenThrow (e );
93
+ Request request = new Request ("GET" , endpoint );
94
+ addParameters (request , getParameters (resource .getParameters ()));
95
+ when (client .performRequest (request )).thenThrow (e );
91
96
92
97
assertThat (resource .doCheck (client ), is (CheckResponse .ERROR ));
93
98
}
@@ -123,7 +128,9 @@ protected void assertCheckAsDeleteWithException(final PublishableHttpResource re
123
128
final ResponseException responseException = responseException ("DELETE" , endpoint , failedCheckStatus ());
124
129
final Exception e = randomFrom (new IOException ("expected" ), new RuntimeException ("expected" ), responseException );
125
130
126
- when (client .performRequest ("DELETE" , endpoint , deleteParameters (resource .getParameters ()))).thenThrow (e );
131
+ Request request = new Request ("DELETE" , endpoint );
132
+ addParameters (request , deleteParameters (resource .getParameters ()));
133
+ when (client .performRequest (request )).thenThrow (e );
127
134
128
135
assertThat (resource .doCheck (client ), is (CheckResponse .ERROR ));
129
136
}
@@ -173,9 +180,15 @@ protected void assertPublishWithException(final PublishableHttpResource resource
173
180
final String endpoint = concatenateEndpoint (resourceBasePath , resourceName );
174
181
final Exception e = randomFrom (new IOException ("expected" ), new RuntimeException ("expected" ));
175
182
176
- when (client .performRequest (eq ( "PUT" ), eq ( endpoint ), eq ( resource . getParameters ()), any (bodyType ))).thenThrow (e );
183
+ when (client .performRequest (Mockito . any (Request . class ))).thenThrow (e );
177
184
178
185
assertThat (resource .doPublish (client ), is (false ));
186
+ ArgumentCaptor <Request > request = ArgumentCaptor .forClass (Request .class );
187
+ verify (client ).performRequest (request .capture ());
188
+ assertThat (request .getValue ().getMethod (), is ("PUT" ));
189
+ assertThat (request .getValue ().getEndpoint (), is (endpoint ));
190
+ assertThat (request .getValue ().getParameters (), is (resource .getParameters ()));
191
+ assertThat (request .getValue ().getEntity (), instanceOf (bodyType ));
179
192
}
180
193
181
194
protected void assertParameters (final PublishableHttpResource resource ) {
@@ -244,7 +257,9 @@ protected void doCheckWithStatusCode(final PublishableHttpResource resource, fin
244
257
final String endpoint , final CheckResponse expected ,
245
258
final Response response )
246
259
throws IOException {
247
- when (client .performRequest ("GET" , endpoint , expectedParameters )).thenReturn (response );
260
+ Request request = new Request ("GET" , endpoint );
261
+ addParameters (request , expectedParameters );
262
+ when (client .performRequest (request )).thenReturn (response );
248
263
249
264
assertThat (resource .doCheck (client ), is (expected ));
250
265
}
@@ -257,9 +272,14 @@ private void doPublishWithStatusCode(final PublishableHttpResource resource, fin
257
272
final String endpoint = concatenateEndpoint (resourceBasePath , resourceName );
258
273
final Response response = response ("GET" , endpoint , status );
259
274
260
- when (client .performRequest (eq ("PUT" ), eq (endpoint ), eq (resource .getParameters ()), any (bodyType ))).thenReturn (response );
275
+ ArgumentCaptor <Request > request = ArgumentCaptor .forClass (Request .class );
276
+ when (client .performRequest (request .capture ())).thenReturn (response );
261
277
262
278
assertThat (resource .doPublish (client ), is (expected ));
279
+ assertThat (request .getValue ().getMethod (), is ("PUT" ));
280
+ assertThat (request .getValue ().getEndpoint (), is (endpoint ));
281
+ assertThat (request .getValue ().getParameters (), is (resource .getParameters ()));
282
+ assertThat (request .getValue ().getEntity (), instanceOf (bodyType ));
263
283
}
264
284
265
285
protected void doCheckAsDeleteWithStatusCode (final PublishableHttpResource resource ,
@@ -277,7 +297,9 @@ protected void doCheckAsDeleteWithStatusCode(final PublishableHttpResource resou
277
297
final String endpoint , final CheckResponse expected ,
278
298
final Response response )
279
299
throws IOException {
280
- when (client .performRequest ("DELETE" , endpoint , deleteParameters (resource .getParameters ()))).thenReturn (response );
300
+ Request request = new Request ("DELETE" , endpoint );
301
+ addParameters (request , deleteParameters (resource .getParameters ()));
302
+ when (client .performRequest (request )).thenReturn (response );
281
303
282
304
assertThat (resource .doCheck (client ), is (expected ));
283
305
}
@@ -427,4 +449,9 @@ protected HttpEntity entityForClusterAlert(final CheckResponse expected, final i
427
449
return entity ;
428
450
}
429
451
452
+ protected void addParameters (Request request , Map <String , String > parameters ) {
453
+ for (Map .Entry <String , String > param : parameters .entrySet ()) {
454
+ request .addParameter (param .getKey (), param .getValue ());
455
+ }
456
+ }
430
457
}
0 commit comments