|
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.IngestScript; |
| 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.Matchers.anyMapOf; |
35 | | -import static org.mockito.Mockito.any; |
36 | | -import static org.mockito.Mockito.doAnswer; |
37 | | -import static org.mockito.Mockito.mock; |
38 | | -import static org.mockito.Mockito.when; |
39 | 38 |
|
40 | 39 | public class ScriptProcessorTests extends ESTestCase { |
41 | 40 |
|
42 | 41 | public void testScripting() throws Exception { |
43 | 42 | int randomBytesIn = randomInt(); |
44 | 43 | int randomBytesOut = randomInt(); |
45 | 44 | int randomBytesTotal = randomBytesIn + randomBytesOut; |
46 | | - |
47 | | - ScriptService scriptService = mock(ScriptService.class); |
48 | | - Script script = mockScript("_script"); |
49 | | - IngestScript.Factory factory = mock(IngestScript.Factory.class); |
50 | | - IngestScript executableScript = mock(IngestScript.class); |
51 | | - when(scriptService.compile(script, IngestScript.CONTEXT)).thenReturn(factory); |
52 | | - 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()); |
53 | 61 |
|
54 | 62 | Map<String, Object> document = new HashMap<>(); |
55 | 63 | document.put("bytes_in", randomInt()); |
56 | 64 | document.put("bytes_out", randomInt()); |
57 | 65 | IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document); |
58 | 66 |
|
59 | | - doAnswer(invocationOnMock -> { |
60 | | - @SuppressWarnings("unchecked") |
61 | | - Map<String, Object> ctx = (Map<String, Object>) invocationOnMock.getArguments()[0]; |
62 | | - ctx.put("bytes_total", randomBytesTotal); |
63 | | - return null; |
64 | | - }).when(executableScript).execute(anyMapOf(String.class, Object.class)); |
65 | | - |
66 | 67 | ScriptProcessor processor = new ScriptProcessor(randomAlphaOfLength(10), script, scriptService); |
67 | 68 |
|
68 | 69 | processor.execute(ingestDocument); |
|
0 commit comments