|
@@ -19,6 +19,7 @@
|
|
|
|
|
|
package org.elasticsearch.script;
|
|
|
|
|
|
+import org.elasticsearch.ElasticsearchParseException;
|
|
|
import org.elasticsearch.common.Strings;
|
|
|
import org.elasticsearch.common.io.stream.InputStreamStreamInput;
|
|
|
import org.elasticsearch.common.io.stream.OutputStreamStreamOutput;
|
|
@@ -26,6 +27,7 @@ import org.elasticsearch.common.settings.Settings;
|
|
|
import org.elasticsearch.common.xcontent.ToXContent;
|
|
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
|
import org.elasticsearch.common.xcontent.XContentFactory;
|
|
|
+import org.elasticsearch.common.xcontent.XContentHelper;
|
|
|
import org.elasticsearch.common.xcontent.XContentParser;
|
|
|
import org.elasticsearch.common.xcontent.XContentType;
|
|
|
import org.elasticsearch.test.ESTestCase;
|
|
@@ -34,6 +36,7 @@ import java.io.ByteArrayInputStream;
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.util.Collections;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import static org.hamcrest.Matchers.equalTo;
|
|
@@ -96,4 +99,115 @@ public class ScriptTests extends ESTestCase {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public void testParseFromObjectShortSyntax() {
|
|
|
+ Script script = Script.parse("doc['my_field']");
|
|
|
+ assertEquals(Script.DEFAULT_SCRIPT_LANG, script.getLang());
|
|
|
+ assertEquals("doc['my_field']", script.getIdOrCode());
|
|
|
+ assertEquals(0, script.getParams().size());
|
|
|
+ assertEquals(0, script.getOptions().size());
|
|
|
+ assertEquals(ScriptType.INLINE, script.getType());
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testParseFromObject() {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("source", "doc['my_field']");
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ int numParams = randomIntBetween(0, 3);
|
|
|
+ for (int i = 0; i < numParams; i++) {
|
|
|
+ params.put("param" + i, i);
|
|
|
+ }
|
|
|
+ map.put("params", params);
|
|
|
+ Map<String, String> options = new HashMap<>();
|
|
|
+ int numOptions = randomIntBetween(0, 3);
|
|
|
+ for (int i = 0; i < numOptions; i++) {
|
|
|
+ options.put("option" + i, Integer.toString(i));
|
|
|
+ }
|
|
|
+ map.put("options", options);
|
|
|
+ String lang = Script.DEFAULT_SCRIPT_LANG;;
|
|
|
+ if (randomBoolean()) {
|
|
|
+ map.put("lang", lang);
|
|
|
+ } else if(randomBoolean()) {
|
|
|
+ lang = "expression";
|
|
|
+ map.put("lang", lang);
|
|
|
+ }
|
|
|
+
|
|
|
+ Script script = Script.parse(map);
|
|
|
+ assertEquals(lang, script.getLang());
|
|
|
+ assertEquals("doc['my_field']", script.getIdOrCode());
|
|
|
+ assertEquals(ScriptType.INLINE, script.getType());
|
|
|
+ assertEquals(params, script.getParams());
|
|
|
+ assertEquals(options, script.getOptions());
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testParseFromObjectFromScript() {
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ int numParams = randomIntBetween(0, 3);
|
|
|
+ for (int i = 0; i < numParams; i++) {
|
|
|
+ params.put("param" + i, i);
|
|
|
+ }
|
|
|
+ Map<String, String> options = new HashMap<>();
|
|
|
+ int numOptions = randomIntBetween(0, 3);
|
|
|
+ for (int i = 0; i < numOptions; i++) {
|
|
|
+ options.put("option" + i, Integer.toString(i));
|
|
|
+ }
|
|
|
+ Script script = new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, "doc['field']", options, params);
|
|
|
+ Map<String, Object> scriptObject = XContentHelper.convertToMap(XContentType.JSON.xContent(), Strings.toString(script), false);
|
|
|
+ Script parsedScript = Script.parse(scriptObject);
|
|
|
+ assertEquals(script, parsedScript);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testParseFromObjectWrongFormat() {
|
|
|
+ {
|
|
|
+ NullPointerException exc = expectThrows(
|
|
|
+ NullPointerException.class,
|
|
|
+ () -> Script.parse((Object)null)
|
|
|
+ );
|
|
|
+ assertEquals("Script must not be null", exc.getMessage());
|
|
|
+ }
|
|
|
+ {
|
|
|
+ IllegalArgumentException exc = expectThrows(
|
|
|
+ IllegalArgumentException.class,
|
|
|
+ () -> Script.parse(3)
|
|
|
+ );
|
|
|
+ assertEquals("Script value should be a String or a Map", exc.getMessage());
|
|
|
+ }
|
|
|
+ {
|
|
|
+ ElasticsearchParseException exc = expectThrows(
|
|
|
+ ElasticsearchParseException.class,
|
|
|
+ () -> Script.parse(Collections.emptyMap())
|
|
|
+ );
|
|
|
+ assertEquals("Expected one of [source] or [id] fields, but found none", exc.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testParseFromObjectUnsupportedFields() {
|
|
|
+ ElasticsearchParseException exc = expectThrows(
|
|
|
+ ElasticsearchParseException.class,
|
|
|
+ () -> Script.parse(Map.of("source", "script", "unsupported", "value"))
|
|
|
+ );
|
|
|
+ assertEquals("Unsupported field [unsupported]", exc.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testParseFromObjectWrongOptionsFormat() {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("source", "doc['my_field']");
|
|
|
+ map.put("options", 3);
|
|
|
+ ElasticsearchParseException exc = expectThrows(
|
|
|
+ ElasticsearchParseException.class,
|
|
|
+ () -> Script.parse(map)
|
|
|
+ );
|
|
|
+ assertEquals("Value must be of type Map: [options]", exc.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testParseFromObjectWrongParamsFormat() {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("source", "doc['my_field']");
|
|
|
+ map.put("params", 3);
|
|
|
+ ElasticsearchParseException exc = expectThrows(
|
|
|
+ ElasticsearchParseException.class,
|
|
|
+ () -> Script.parse(map)
|
|
|
+ );
|
|
|
+ assertEquals("Value must be of type Map: [params]", exc.getMessage());
|
|
|
+ }
|
|
|
}
|