|
19 | 19 |
|
20 | 20 | package org.elasticsearch.ingest.common;
|
21 | 21 |
|
| 22 | +import java.util.Collections; |
22 | 23 | import java.util.HashMap;
|
23 | 24 | import java.util.Map;
|
24 | 25 |
|
| 26 | +import org.elasticsearch.common.settings.Settings; |
25 | 27 | import org.elasticsearch.ingest.IngestDocument;
|
26 | 28 | import org.elasticsearch.ingest.RandomDocumentPicks;
|
27 |
| -import org.elasticsearch.script.ExecutableScript; |
| 29 | +import org.elasticsearch.script.MockScriptEngine; |
28 | 30 | import org.elasticsearch.script.Script;
|
| 31 | +import org.elasticsearch.script.ScriptModule; |
29 | 32 | import org.elasticsearch.script.ScriptService;
|
| 33 | +import org.elasticsearch.script.ScriptType; |
30 | 34 | import org.elasticsearch.test.ESTestCase;
|
31 | 35 |
|
32 | 36 | import static org.hamcrest.Matchers.hasKey;
|
33 | 37 | import static org.hamcrest.core.Is.is;
|
34 |
| -import static org.mockito.Mockito.any; |
35 |
| -import static org.mockito.Mockito.doAnswer; |
36 |
| -import static org.mockito.Mockito.mock; |
37 |
| -import static org.mockito.Mockito.when; |
38 | 38 |
|
39 | 39 | public class ScriptProcessorTests extends ESTestCase {
|
40 | 40 |
|
41 | 41 | public void testScripting() throws Exception {
|
42 | 42 | int randomBytesIn = randomInt();
|
43 | 43 | int randomBytesOut = randomInt();
|
44 | 44 | int randomBytesTotal = randomBytesIn + randomBytesOut;
|
45 |
| - |
46 |
| - ScriptService scriptService = mock(ScriptService.class); |
47 |
| - Script script = mockScript("_script"); |
48 |
| - ExecutableScript.Factory factory = mock(ExecutableScript.Factory.class); |
49 |
| - ExecutableScript executableScript = mock(ExecutableScript.class); |
50 |
| - when(scriptService.compile(script, ExecutableScript.INGEST_CONTEXT)).thenReturn(factory); |
51 |
| - when(factory.newInstance(any())).thenReturn(executableScript); |
| 45 | + String scriptName = "script"; |
| 46 | + ScriptService scriptService = new ScriptService(Settings.builder().build(), |
| 47 | + Collections.singletonMap( |
| 48 | + Script.DEFAULT_SCRIPT_LANG, new MockScriptEngine( |
| 49 | + Script.DEFAULT_SCRIPT_LANG, |
| 50 | + Collections.singletonMap( |
| 51 | + scriptName, ctx -> { |
| 52 | + ctx.put("bytes_total", randomBytesTotal); |
| 53 | + return null; |
| 54 | + } |
| 55 | + ) |
| 56 | + ) |
| 57 | + ), |
| 58 | + new HashMap<>(ScriptModule.CORE_CONTEXTS) |
| 59 | + ); |
| 60 | + Script script = new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, scriptName, Collections.emptyMap()); |
52 | 61 |
|
53 | 62 | Map<String, Object> document = new HashMap<>();
|
54 | 63 | document.put("bytes_in", randomInt());
|
55 | 64 | document.put("bytes_out", randomInt());
|
56 | 65 | IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
|
57 | 66 |
|
58 |
| - doAnswer(invocationOnMock -> { |
59 |
| - ingestDocument.setFieldValue("bytes_total", randomBytesTotal); |
60 |
| - return null; |
61 |
| - }).when(executableScript).run(); |
62 |
| - |
63 | 67 | ScriptProcessor processor = new ScriptProcessor(randomAlphaOfLength(10), script, scriptService);
|
64 | 68 |
|
65 | 69 | processor.execute(ingestDocument);
|
|
0 commit comments