5
5
*/
6
6
package org .elasticsearch .smoketest ;
7
7
8
- import org .apache .http .entity .ContentType ;
9
- import org .apache .http .entity .StringEntity ;
10
8
import org .apache .http .util .EntityUtils ;
9
+ import org .elasticsearch .client .Request ;
11
10
import org .elasticsearch .client .Response ;
12
11
import org .elasticsearch .common .Strings ;
13
12
import org .elasticsearch .common .settings .SecureString ;
21
20
import org .junit .Before ;
22
21
23
22
import java .io .IOException ;
24
- import java .util .Collections ;
25
23
import java .util .Map ;
26
24
import java .util .concurrent .atomic .AtomicReference ;
27
25
@@ -41,27 +39,28 @@ public class SmokeTestWatcherWithSecurityIT extends ESRestTestCase {
41
39
42
40
@ Before
43
41
public void startWatcher () throws Exception {
44
- StringEntity entity = new StringEntity ("{ \" value\" : \" 15\" }" , ContentType .APPLICATION_JSON );
45
- assertOK (adminClient ().performRequest ("PUT" , "my_test_index/doc/1" , Collections .singletonMap ("refresh" , "true" ), entity ));
42
+ Request createAllowedDoc = new Request ("PUT" , "/my_test_index/doc/1" );
43
+ createAllowedDoc .setJsonEntity ("{ \" value\" : \" 15\" }" );
44
+ createAllowedDoc .addParameter ("refresh" , "true" );
45
+ adminClient ().performRequest (createAllowedDoc );
46
46
47
47
// delete the watcher history to not clutter with entries from other test
48
- adminClient ().performRequest ("DELETE" , ".watcher-history-*" , Collections . emptyMap ( ));
48
+ adminClient ().performRequest (new Request ( "DELETE" , ".watcher-history-*" ));
49
49
50
50
// create one document in this index, so we can test in the YAML tests, that the index cannot be accessed
51
- Response resp = adminClient (). performRequest ( "PUT" , "/index_not_allowed_to_read/doc/1" , Collections . emptyMap (),
52
- new StringEntity ("{\" foo\" :\" bar\" }" , ContentType . APPLICATION_JSON ) );
53
- assertThat ( resp . getStatusLine (). getStatusCode (), is ( 201 ) );
51
+ Request createNotAllowedDoc = new Request ( "PUT" , "/index_not_allowed_to_read/doc/1" );
52
+ createNotAllowedDoc . setJsonEntity ("{\" foo\" :\" bar\" }" );
53
+ adminClient (). performRequest ( createNotAllowedDoc );
54
54
55
55
assertBusy (() -> {
56
56
try {
57
- Response statsResponse = adminClient ().performRequest ("GET" , "_xpack/watcher/stats" );
57
+ Response statsResponse = adminClient ().performRequest (new Request ( "GET" , "/ _xpack/watcher/stats" ) );
58
58
ObjectPath objectPath = ObjectPath .createFromResponse (statsResponse );
59
59
String state = objectPath .evaluate ("stats.0.watcher_state" );
60
60
61
61
switch (state ) {
62
62
case "stopped" :
63
- Response startResponse = adminClient ().performRequest ("POST" , "_xpack/watcher/_start" );
64
- assertOK (startResponse );
63
+ Response startResponse = adminClient ().performRequest (new Request ("POST" , "/_xpack/watcher/_start" ));
65
64
String body = EntityUtils .toString (startResponse .getEntity ());
66
65
assertThat (body , containsString ("\" acknowledged\" :true" ));
67
66
break ;
@@ -82,18 +81,18 @@ public void startWatcher() throws Exception {
82
81
83
82
assertBusy (() -> {
84
83
for (String template : WatcherIndexTemplateRegistryField .TEMPLATE_NAMES ) {
85
- assertOK (adminClient ().performRequest ("HEAD" , "_template/" + template ));
84
+ assertOK (adminClient ().performRequest (new Request ( "HEAD" , "_template/" + template ) ));
86
85
}
87
86
});
88
87
}
89
88
90
89
@ After
91
90
public void stopWatcher () throws Exception {
92
- assertOK ( adminClient ().performRequest ("DELETE" , "my_test_index" ));
91
+ adminClient ().performRequest (new Request ( "DELETE" , "/ my_test_index" ));
93
92
94
93
assertBusy (() -> {
95
94
try {
96
- Response statsResponse = adminClient ().performRequest ("GET" , "_xpack/watcher/stats" );
95
+ Response statsResponse = adminClient ().performRequest (new Request ( "GET" , "/ _xpack/watcher/stats" ) );
97
96
ObjectPath objectPath = ObjectPath .createFromResponse (statsResponse );
98
97
String state = objectPath .evaluate ("stats.0.watcher_state" );
99
98
@@ -106,8 +105,7 @@ public void stopWatcher() throws Exception {
106
105
case "starting" :
107
106
throw new AssertionError ("waiting until starting state reached started state to stop" );
108
107
case "started" :
109
- Response stopResponse = adminClient ().performRequest ("POST" , "_xpack/watcher/_stop" , Collections .emptyMap ());
110
- assertOK (stopResponse );
108
+ Response stopResponse = adminClient ().performRequest (new Request ("POST" , "/_xpack/watcher/_stop" ));
111
109
String body = EntityUtils .toString (stopResponse .getEntity ());
112
110
assertThat (body , containsString ("\" acknowledged\" :true" ));
113
111
break ;
@@ -210,7 +208,7 @@ public void testSearchTransformHasPermissions() throws Exception {
210
208
boolean conditionMet = objectPath .evaluate ("hits.hits.0._source.result.condition.met" );
211
209
assertThat (conditionMet , is (true ));
212
210
213
- ObjectPath getObjectPath = ObjectPath .createFromResponse (client ().performRequest ("GET" , "my_test_index/doc/my-id" ));
211
+ ObjectPath getObjectPath = ObjectPath .createFromResponse (client ().performRequest (new Request ( "GET" , "/ my_test_index/doc/my-id" ) ));
214
212
String value = getObjectPath .evaluate ("_source.hits.hits.0._source.value" );
215
213
assertThat (value , is ("15" ));
216
214
}
@@ -238,8 +236,7 @@ public void testSearchTransformInsufficientPermissions() throws Exception {
238
236
239
237
getWatchHistoryEntry (watchId );
240
238
241
- Response response = adminClient ().performRequest ("GET" , "my_test_index/doc/some-id" ,
242
- Collections .singletonMap ("ignore" , "404" ));
239
+ Response response = adminClient ().performRequest (new Request ("HEAD" , "/my_test_index/doc/some-id" ));
243
240
assertThat (response .getStatusLine ().getStatusCode (), is (404 ));
244
241
}
245
242
@@ -262,7 +259,7 @@ public void testIndexActionHasPermissions() throws Exception {
262
259
boolean conditionMet = objectPath .evaluate ("hits.hits.0._source.result.condition.met" );
263
260
assertThat (conditionMet , is (true ));
264
261
265
- ObjectPath getObjectPath = ObjectPath .createFromResponse (client ().performRequest ("GET" , "my_test_index/doc/my-id" ));
262
+ ObjectPath getObjectPath = ObjectPath .createFromResponse (client ().performRequest (new Request ( "GET" , "/ my_test_index/doc/my-id" ) ));
266
263
String spam = getObjectPath .evaluate ("_source.spam" );
267
264
assertThat (spam , is ("eggs" ));
268
265
}
@@ -286,16 +283,14 @@ public void testIndexActionInsufficientPrivileges() throws Exception {
286
283
boolean conditionMet = objectPath .evaluate ("hits.hits.0._source.result.condition.met" );
287
284
assertThat (conditionMet , is (true ));
288
285
289
- Response response = adminClient ().performRequest ("GET" , "index_not_allowed_to_read/doc/my-id" ,
290
- Collections .singletonMap ("ignore" , "404" ));
286
+ Response response = adminClient ().performRequest (new Request ("HEAD" , "/index_not_allowed_to_read/doc/my-id" ));
291
287
assertThat (response .getStatusLine ().getStatusCode (), is (404 ));
292
288
}
293
289
294
290
private void indexWatch (String watchId , XContentBuilder builder ) throws Exception {
295
- StringEntity entity = new StringEntity (Strings .toString (builder ), ContentType .APPLICATION_JSON );
296
-
297
- Response response = client ().performRequest ("PUT" , "_xpack/watcher/watch/" + watchId , Collections .emptyMap (), entity );
298
- assertOK (response );
291
+ Request request = new Request ("PUT" , "/_xpack/watcher/watch/" + watchId );
292
+ request .setJsonEntity (Strings .toString (builder ));
293
+ Response response = client ().performRequest (request );
299
294
Map <String , Object > responseMap = entityAsMap (response );
300
295
assertThat (responseMap , hasEntry ("_id" , watchId ));
301
296
}
@@ -307,7 +302,7 @@ private ObjectPath getWatchHistoryEntry(String watchId) throws Exception {
307
302
private ObjectPath getWatchHistoryEntry (String watchId , String state ) throws Exception {
308
303
final AtomicReference <ObjectPath > objectPathReference = new AtomicReference <>();
309
304
assertBusy (() -> {
310
- client ().performRequest ("POST" , ".watcher-history-*/_refresh" );
305
+ client ().performRequest (new Request ( "POST" , "/ .watcher-history-*/_refresh" ) );
311
306
312
307
try (XContentBuilder builder = jsonBuilder ()) {
313
308
builder .startObject ();
@@ -323,8 +318,9 @@ private ObjectPath getWatchHistoryEntry(String watchId, String state) throws Exc
323
318
.endObject ().endArray ();
324
319
builder .endObject ();
325
320
326
- StringEntity entity = new StringEntity (Strings .toString (builder ), ContentType .APPLICATION_JSON );
327
- Response response = client ().performRequest ("POST" , ".watcher-history-*/_search" , Collections .emptyMap (), entity );
321
+ Request searchRequest = new Request ("POST" , "/.watcher-history-*/_search" );
322
+ searchRequest .setJsonEntity (Strings .toString (builder ));
323
+ Response response = client ().performRequest (searchRequest );
328
324
ObjectPath objectPath = ObjectPath .createFromResponse (response );
329
325
int totalHits = objectPath .evaluate ("hits.total" );
330
326
assertThat (totalHits , is (greaterThanOrEqualTo (1 )));
0 commit comments