1
- package org .elasticsearch .client ;/*
1
+ package org .elasticsearch .client ;
2
+ /*
2
3
* Licensed to Elasticsearch under one or more contributor
3
4
* license agreements. See the NOTICE file distributed with
4
5
* this work for additional information regarding copyright
17
18
* under the License.
18
19
*/
19
20
20
-
21
- import org .apache .http .entity .ContentType ;
22
- import org .apache .http .entity .StringEntity ;
23
- import org .apache .http .util .EntityUtils ;
24
21
import org .elasticsearch .ElasticsearchStatusException ;
25
22
import org .elasticsearch .action .admin .cluster .storedscripts .DeleteStoredScriptRequest ;
26
23
import org .elasticsearch .action .admin .cluster .storedscripts .DeleteStoredScriptResponse ;
27
24
import org .elasticsearch .action .admin .cluster .storedscripts .GetStoredScriptRequest ;
28
25
import org .elasticsearch .action .admin .cluster .storedscripts .GetStoredScriptResponse ;
29
- import org .elasticsearch .common .Strings ;
30
- import org .elasticsearch .common .xcontent .ToXContent ;
26
+ import org .elasticsearch .action .admin .cluster .storedscripts .PutStoredScriptRequest ;
27
+ import org .elasticsearch .action .admin .cluster .storedscripts .PutStoredScriptResponse ;
28
+ import org .elasticsearch .common .bytes .BytesArray ;
31
29
import org .elasticsearch .common .xcontent .XContentType ;
32
30
import org .elasticsearch .rest .RestStatus ;
33
31
import org .elasticsearch .script .Script ;
34
32
import org .elasticsearch .script .StoredScriptSource ;
35
33
36
34
import java .util .Collections ;
35
+ import java .util .Map ;
37
36
38
- import static java .util .Collections .emptyMap ;
39
- import static org .elasticsearch .common .xcontent .XContentFactory .jsonBuilder ;
37
+ import static org .elasticsearch .common .xcontent .support .XContentMapValues .extractValue ;
40
38
import static org .hamcrest .Matchers .equalTo ;
41
39
42
40
public class StoredScriptsIT extends ESRestHighLevelClientTestCase {
43
41
44
- final String id = "calculate-score" ;
42
+ private static final String id = "calculate-score" ;
45
43
46
44
public void testGetStoredScript () throws Exception {
47
45
final StoredScriptSource scriptSource =
48
46
new StoredScriptSource ("painless" ,
49
47
"Math.log(_score * 2) + params.my_modifier" ,
50
48
Collections .singletonMap (Script .CONTENT_TYPE_OPTION , XContentType .JSON .mediaType ()));
51
49
52
- final String script = Strings .toString (scriptSource .toXContent (jsonBuilder (), ToXContent .EMPTY_PARAMS ));
53
- // TODO: change to HighLevel PutStoredScriptRequest when it will be ready
54
- // so far - using low-level REST API
55
- Response putResponse =
56
- adminClient ()
57
- .performRequest ("PUT" , "/_scripts/calculate-score" , emptyMap (),
58
- new StringEntity ("{\" script\" :" + script + "}" ,
59
- ContentType .APPLICATION_JSON ));
60
- assertEquals (putResponse .getStatusLine ().getReasonPhrase (), 200 , putResponse .getStatusLine ().getStatusCode ());
61
- assertEquals ("{\" acknowledged\" :true}" , EntityUtils .toString (putResponse .getEntity ()));
62
-
63
- GetStoredScriptRequest getRequest = new GetStoredScriptRequest ("calculate-score" );
50
+ PutStoredScriptRequest request =
51
+ new PutStoredScriptRequest (id , "search" , new BytesArray ("{}" ), XContentType .JSON , scriptSource );
52
+ PutStoredScriptResponse putResponse = execute (request , highLevelClient ()::putScript ,
53
+ highLevelClient ()::putScriptAsync );
54
+ assertThat (putResponse .isAcknowledged (), equalTo (true ));
55
+
56
+ GetStoredScriptRequest getRequest = new GetStoredScriptRequest (id );
64
57
getRequest .masterNodeTimeout ("50s" );
65
58
66
59
GetStoredScriptResponse getResponse = execute (getRequest , highLevelClient ()::getScript ,
@@ -75,16 +68,11 @@ public void testDeleteStoredScript() throws Exception {
75
68
"Math.log(_score * 2) + params.my_modifier" ,
76
69
Collections .singletonMap (Script .CONTENT_TYPE_OPTION , XContentType .JSON .mediaType ()));
77
70
78
- final String script = Strings .toString (scriptSource .toXContent (jsonBuilder (), ToXContent .EMPTY_PARAMS ));
79
- // TODO: change to HighLevel PutStoredScriptRequest when it will be ready
80
- // so far - using low-level REST API
81
- Response putResponse =
82
- adminClient ()
83
- .performRequest ("PUT" , "/_scripts/" + id , emptyMap (),
84
- new StringEntity ("{\" script\" :" + script + "}" ,
85
- ContentType .APPLICATION_JSON ));
86
- assertEquals (putResponse .getStatusLine ().getReasonPhrase (), 200 , putResponse .getStatusLine ().getStatusCode ());
87
- assertEquals ("{\" acknowledged\" :true}" , EntityUtils .toString (putResponse .getEntity ()));
71
+ PutStoredScriptRequest request =
72
+ new PutStoredScriptRequest (id , "search" , new BytesArray ("{}" ), XContentType .JSON , scriptSource );
73
+ PutStoredScriptResponse putResponse = execute (request , highLevelClient ()::putScript ,
74
+ highLevelClient ()::putScriptAsync );
75
+ assertThat (putResponse .isAcknowledged (), equalTo (true ));
88
76
89
77
DeleteStoredScriptRequest deleteRequest = new DeleteStoredScriptRequest (id );
90
78
deleteRequest .masterNodeTimeout ("50s" );
@@ -102,4 +90,23 @@ public void testDeleteStoredScript() throws Exception {
102
90
highLevelClient ()::getScriptAsync ));
103
91
assertThat (statusException .status (), equalTo (RestStatus .NOT_FOUND ));
104
92
}
93
+
94
+ public void testPutScript () throws Exception {
95
+ final StoredScriptSource scriptSource =
96
+ new StoredScriptSource ("painless" ,
97
+ "Math.log(_score * 2) + params.my_modifier" ,
98
+ Collections .singletonMap (Script .CONTENT_TYPE_OPTION , XContentType .JSON .mediaType ()));
99
+
100
+ PutStoredScriptRequest request =
101
+ new PutStoredScriptRequest (id , "search" , new BytesArray ("{}" ), XContentType .JSON , scriptSource );
102
+ PutStoredScriptResponse putResponse = execute (request , highLevelClient ()::putScript ,
103
+ highLevelClient ()::putScriptAsync );
104
+ assertThat (putResponse .isAcknowledged (), equalTo (true ));
105
+
106
+ Map <String , Object > script = getAsMap ("/_scripts/" + id );
107
+ assertThat (extractValue ("_id" , script ), equalTo (id ));
108
+ assertThat (extractValue ("found" , script ), equalTo (true ));
109
+ assertThat (extractValue ("script.lang" , script ), equalTo ("painless" ));
110
+ assertThat (extractValue ("script.source" , script ), equalTo ("Math.log(_score * 2) + params.my_modifier" ));
111
+ }
105
112
}
0 commit comments