|
| 1 | +package org.elasticsearch.client;/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | + |
| 21 | +import org.apache.http.entity.ContentType; |
| 22 | +import org.apache.http.entity.StringEntity; |
| 23 | +import org.apache.http.util.EntityUtils; |
| 24 | +import org.elasticsearch.ElasticsearchStatusException; |
| 25 | +import org.elasticsearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest; |
| 26 | +import org.elasticsearch.action.admin.cluster.storedscripts.DeleteStoredScriptResponse; |
| 27 | +import org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptRequest; |
| 28 | +import org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptResponse; |
| 29 | +import org.elasticsearch.common.Strings; |
| 30 | +import org.elasticsearch.common.xcontent.ToXContent; |
| 31 | +import org.elasticsearch.common.xcontent.XContentType; |
| 32 | +import org.elasticsearch.rest.RestStatus; |
| 33 | +import org.elasticsearch.script.Script; |
| 34 | +import org.elasticsearch.script.StoredScriptSource; |
| 35 | + |
| 36 | +import java.util.Collections; |
| 37 | + |
| 38 | +import static java.util.Collections.emptyMap; |
| 39 | +import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; |
| 40 | +import static org.hamcrest.Matchers.equalTo; |
| 41 | + |
| 42 | +public class StoredScriptsIT extends ESRestHighLevelClientTestCase { |
| 43 | + |
| 44 | + final String id = "calculate-score"; |
| 45 | + |
| 46 | + public void testGetStoredScript() throws Exception { |
| 47 | + final StoredScriptSource scriptSource = |
| 48 | + new StoredScriptSource("painless", |
| 49 | + "Math.log(_score * 2) + params.my_modifier", |
| 50 | + Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType())); |
| 51 | + |
| 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"); |
| 64 | + getRequest.masterNodeTimeout("50s"); |
| 65 | + |
| 66 | + GetStoredScriptResponse getResponse = execute(getRequest, highLevelClient()::getScript, |
| 67 | + highLevelClient()::getScriptAsync); |
| 68 | + |
| 69 | + assertThat(getResponse.getSource(), equalTo(scriptSource)); |
| 70 | + } |
| 71 | + |
| 72 | + public void testDeleteStoredScript() throws Exception { |
| 73 | + final StoredScriptSource scriptSource = |
| 74 | + new StoredScriptSource("painless", |
| 75 | + "Math.log(_score * 2) + params.my_modifier", |
| 76 | + Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType())); |
| 77 | + |
| 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())); |
| 88 | + |
| 89 | + DeleteStoredScriptRequest deleteRequest = new DeleteStoredScriptRequest(id); |
| 90 | + deleteRequest.masterNodeTimeout("50s"); |
| 91 | + deleteRequest.timeout("50s"); |
| 92 | + |
| 93 | + DeleteStoredScriptResponse deleteResponse = execute(deleteRequest, highLevelClient()::deleteScript, |
| 94 | + highLevelClient()::deleteScriptAsync); |
| 95 | + |
| 96 | + assertThat(deleteResponse.isAcknowledged(), equalTo(true)); |
| 97 | + |
| 98 | + GetStoredScriptRequest getRequest = new GetStoredScriptRequest(id); |
| 99 | + |
| 100 | + final ElasticsearchStatusException statusException = expectThrows(ElasticsearchStatusException.class, |
| 101 | + () -> execute(getRequest, highLevelClient()::getScript, |
| 102 | + highLevelClient()::getScriptAsync)); |
| 103 | + assertThat(statusException.status(), equalTo(RestStatus.NOT_FOUND)); |
| 104 | + } |
| 105 | +} |
0 commit comments