|
19 | 19 |
|
20 | 20 | package org.elasticsearch.script; |
21 | 21 |
|
| 22 | +import org.elasticsearch.ElasticsearchParseException; |
22 | 23 | import org.elasticsearch.common.Strings; |
23 | 24 | import org.elasticsearch.common.io.stream.InputStreamStreamInput; |
24 | 25 | import org.elasticsearch.common.io.stream.OutputStreamStreamOutput; |
25 | 26 | import org.elasticsearch.common.settings.Settings; |
26 | 27 | import org.elasticsearch.common.xcontent.ToXContent; |
27 | 28 | import org.elasticsearch.common.xcontent.XContentBuilder; |
28 | 29 | import org.elasticsearch.common.xcontent.XContentFactory; |
| 30 | +import org.elasticsearch.common.xcontent.XContentHelper; |
29 | 31 | import org.elasticsearch.common.xcontent.XContentParser; |
30 | 32 | import org.elasticsearch.common.xcontent.XContentType; |
31 | 33 | import org.elasticsearch.test.ESTestCase; |
|
34 | 36 | import java.io.ByteArrayOutputStream; |
35 | 37 | import java.io.IOException; |
36 | 38 | import java.util.Collections; |
| 39 | +import java.util.HashMap; |
37 | 40 | import java.util.Map; |
38 | 41 |
|
39 | 42 | import static org.hamcrest.Matchers.equalTo; |
@@ -96,4 +99,115 @@ public void testParse() throws IOException { |
96 | 99 | } |
97 | 100 | } |
98 | 101 | } |
| 102 | + |
| 103 | + public void testParseFromObjectShortSyntax() { |
| 104 | + Script script = Script.parse("doc['my_field']"); |
| 105 | + assertEquals(Script.DEFAULT_SCRIPT_LANG, script.getLang()); |
| 106 | + assertEquals("doc['my_field']", script.getIdOrCode()); |
| 107 | + assertEquals(0, script.getParams().size()); |
| 108 | + assertEquals(0, script.getOptions().size()); |
| 109 | + assertEquals(ScriptType.INLINE, script.getType()); |
| 110 | + } |
| 111 | + |
| 112 | + public void testParseFromObject() { |
| 113 | + Map<String, Object> map = new HashMap<>(); |
| 114 | + map.put("source", "doc['my_field']"); |
| 115 | + Map<String, Object> params = new HashMap<>(); |
| 116 | + int numParams = randomIntBetween(0, 3); |
| 117 | + for (int i = 0; i < numParams; i++) { |
| 118 | + params.put("param" + i, i); |
| 119 | + } |
| 120 | + map.put("params", params); |
| 121 | + Map<String, String> options = new HashMap<>(); |
| 122 | + int numOptions = randomIntBetween(0, 3); |
| 123 | + for (int i = 0; i < numOptions; i++) { |
| 124 | + options.put("option" + i, Integer.toString(i)); |
| 125 | + } |
| 126 | + map.put("options", options); |
| 127 | + String lang = Script.DEFAULT_SCRIPT_LANG;; |
| 128 | + if (randomBoolean()) { |
| 129 | + map.put("lang", lang); |
| 130 | + } else if(randomBoolean()) { |
| 131 | + lang = "expression"; |
| 132 | + map.put("lang", lang); |
| 133 | + } |
| 134 | + |
| 135 | + Script script = Script.parse(map); |
| 136 | + assertEquals(lang, script.getLang()); |
| 137 | + assertEquals("doc['my_field']", script.getIdOrCode()); |
| 138 | + assertEquals(ScriptType.INLINE, script.getType()); |
| 139 | + assertEquals(params, script.getParams()); |
| 140 | + assertEquals(options, script.getOptions()); |
| 141 | + } |
| 142 | + |
| 143 | + public void testParseFromObjectFromScript() { |
| 144 | + Map<String, Object> params = new HashMap<>(); |
| 145 | + int numParams = randomIntBetween(0, 3); |
| 146 | + for (int i = 0; i < numParams; i++) { |
| 147 | + params.put("param" + i, i); |
| 148 | + } |
| 149 | + Map<String, String> options = new HashMap<>(); |
| 150 | + int numOptions = randomIntBetween(0, 3); |
| 151 | + for (int i = 0; i < numOptions; i++) { |
| 152 | + options.put("option" + i, Integer.toString(i)); |
| 153 | + } |
| 154 | + Script script = new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, "doc['field']", options, params); |
| 155 | + Map<String, Object> scriptObject = XContentHelper.convertToMap(XContentType.JSON.xContent(), Strings.toString(script), false); |
| 156 | + Script parsedScript = Script.parse(scriptObject); |
| 157 | + assertEquals(script, parsedScript); |
| 158 | + } |
| 159 | + |
| 160 | + public void testParseFromObjectWrongFormat() { |
| 161 | + { |
| 162 | + NullPointerException exc = expectThrows( |
| 163 | + NullPointerException.class, |
| 164 | + () -> Script.parse((Object)null) |
| 165 | + ); |
| 166 | + assertEquals("Script must not be null", exc.getMessage()); |
| 167 | + } |
| 168 | + { |
| 169 | + IllegalArgumentException exc = expectThrows( |
| 170 | + IllegalArgumentException.class, |
| 171 | + () -> Script.parse(3) |
| 172 | + ); |
| 173 | + assertEquals("Script value should be a String or a Map", exc.getMessage()); |
| 174 | + } |
| 175 | + { |
| 176 | + ElasticsearchParseException exc = expectThrows( |
| 177 | + ElasticsearchParseException.class, |
| 178 | + () -> Script.parse(Collections.emptyMap()) |
| 179 | + ); |
| 180 | + assertEquals("Expected one of [source] or [id] fields, but found none", exc.getMessage()); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + public void testParseFromObjectUnsupportedFields() { |
| 185 | + ElasticsearchParseException exc = expectThrows( |
| 186 | + ElasticsearchParseException.class, |
| 187 | + () -> Script.parse(Map.of("source", "script", "unsupported", "value")) |
| 188 | + ); |
| 189 | + assertEquals("Unsupported field [unsupported]", exc.getMessage()); |
| 190 | + } |
| 191 | + |
| 192 | + public void testParseFromObjectWrongOptionsFormat() { |
| 193 | + Map<String, Object> map = new HashMap<>(); |
| 194 | + map.put("source", "doc['my_field']"); |
| 195 | + map.put("options", 3); |
| 196 | + ElasticsearchParseException exc = expectThrows( |
| 197 | + ElasticsearchParseException.class, |
| 198 | + () -> Script.parse(map) |
| 199 | + ); |
| 200 | + assertEquals("Value must be of type Map: [options]", exc.getMessage()); |
| 201 | + } |
| 202 | + |
| 203 | + public void testParseFromObjectWrongParamsFormat() { |
| 204 | + Map<String, Object> map = new HashMap<>(); |
| 205 | + map.put("source", "doc['my_field']"); |
| 206 | + map.put("params", 3); |
| 207 | + ElasticsearchParseException exc = expectThrows( |
| 208 | + ElasticsearchParseException.class, |
| 209 | + () -> Script.parse(map) |
| 210 | + ); |
| 211 | + assertEquals("Value must be of type Map: [params]", exc.getMessage()); |
| 212 | + } |
99 | 213 | } |
0 commit comments