use of org.opensearch.script.MockScriptEngine in project OpenSearch by opensearch-project.
the class ConditionalProcessorTests method assertMutatingCtxThrows.
private static void assertMutatingCtxThrows(Consumer<Map<String, Object>> mutation) throws Exception {
String scriptName = "conditionalScript";
CompletableFuture<Exception> expectedException = new CompletableFuture<>();
ScriptService scriptService = new ScriptService(Settings.builder().build(), Collections.singletonMap(Script.DEFAULT_SCRIPT_LANG, new MockScriptEngine(Script.DEFAULT_SCRIPT_LANG, Collections.singletonMap(scriptName, ctx -> {
try {
mutation.accept(ctx);
} catch (Exception e) {
expectedException.complete(e);
}
return false;
}), Collections.emptyMap())), new HashMap<>(ScriptModule.CORE_CONTEXTS));
Map<String, Object> document = new HashMap<>();
ConditionalProcessor processor = new ConditionalProcessor(randomAlphaOfLength(10), "desription", new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, scriptName, Collections.emptyMap()), scriptService, null);
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
ingestDocument.setFieldValue("listField", new ArrayList<>());
processor.execute(ingestDocument, (result, e) -> {
});
Exception e = expectedException.get();
assertThat(e, instanceOf(UnsupportedOperationException.class));
assertEquals("Mutating ingest documents in conditionals is not supported", e.getMessage());
assertStats(processor, 0, 0, 0);
}
use of org.opensearch.script.MockScriptEngine in project OpenSearch by opensearch-project.
the class ScriptProcessorFactoryTests method testInlineIsCompiled.
public void testInlineIsCompiled() throws Exception {
String scriptName = "foo";
ScriptService scriptService = new ScriptService(Settings.builder().build(), Collections.singletonMap(Script.DEFAULT_SCRIPT_LANG, new MockScriptEngine(Script.DEFAULT_SCRIPT_LANG, Collections.singletonMap(scriptName, ctx -> {
ctx.put("foo", "bar");
return null;
}), Collections.emptyMap())), new HashMap<>(ScriptModule.CORE_CONTEXTS));
factory = new ScriptProcessor.Factory(scriptService);
Map<String, Object> configMap = new HashMap<>();
configMap.put("source", scriptName);
ScriptProcessor processor = factory.create(null, null, randomAlphaOfLength(10), configMap);
assertThat(processor.getScript().getLang(), equalTo(Script.DEFAULT_SCRIPT_LANG));
assertThat(processor.getScript().getType(), equalTo(ScriptType.INLINE));
assertThat(processor.getScript().getParams(), equalTo(Collections.emptyMap()));
assertNotNull(processor.getPrecompiledIngestScript());
Map<String, Object> ctx = new HashMap<>();
processor.getPrecompiledIngestScript().execute(ctx);
assertThat(ctx.get("foo"), equalTo("bar"));
}
use of org.opensearch.script.MockScriptEngine in project OpenSearch by opensearch-project.
the class ScriptProcessorTests method setupScripting.
@Before
public void setupScripting() {
String scriptName = "script";
scriptService = new ScriptService(Settings.builder().build(), Collections.singletonMap(Script.DEFAULT_SCRIPT_LANG, new MockScriptEngine(Script.DEFAULT_SCRIPT_LANG, Collections.singletonMap(scriptName, ctx -> {
Integer bytesIn = (Integer) ctx.get("bytes_in");
Integer bytesOut = (Integer) ctx.get("bytes_out");
ctx.put("bytes_total", bytesIn + bytesOut);
return null;
}), Collections.emptyMap())), new HashMap<>(ScriptModule.CORE_CONTEXTS));
script = new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, scriptName, Collections.emptyMap());
ingestScript = scriptService.compile(script, IngestScript.CONTEXT).newInstance(script.getParams());
}
use of org.opensearch.script.MockScriptEngine in project OpenSearch by opensearch-project.
the class ScriptProcessorTests method testTypeDeprecation.
public void testTypeDeprecation() throws Exception {
String scriptName = "script";
ScriptService scriptService = new ScriptService(Settings.builder().build(), Collections.singletonMap(Script.DEFAULT_SCRIPT_LANG, new MockScriptEngine(Script.DEFAULT_SCRIPT_LANG, Collections.singletonMap(scriptName, ctx -> {
ctx.get("_type");
return null;
}), Collections.emptyMap())), new HashMap<>(ScriptModule.CORE_CONTEXTS));
Script script = new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, scriptName, Collections.emptyMap());
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.emptyMap());
ScriptProcessor processor = new ScriptProcessor(randomAlphaOfLength(10), null, script, null, scriptService);
processor.execute(ingestDocument);
assertWarnings("[types removal] Looking up doc types [_type] in scripts is deprecated.");
}
use of org.opensearch.script.MockScriptEngine in project OpenSearch by opensearch-project.
the class ConditionalProcessorTests method testTypeDeprecation.
public void testTypeDeprecation() throws Exception {
ScriptService scriptService = new ScriptService(Settings.builder().build(), Collections.singletonMap(Script.DEFAULT_SCRIPT_LANG, new MockScriptEngine(Script.DEFAULT_SCRIPT_LANG, Collections.singletonMap(scriptName, ctx -> {
ctx.get("_type");
return true;
}), Collections.emptyMap())), new HashMap<>(ScriptModule.CORE_CONTEXTS));
LongSupplier relativeTimeProvider = mock(LongSupplier.class);
when(relativeTimeProvider.getAsLong()).thenReturn(0L, TimeUnit.MILLISECONDS.toNanos(1), 0L, TimeUnit.MILLISECONDS.toNanos(2));
ConditionalProcessor processor = new ConditionalProcessor(randomAlphaOfLength(10), "description", new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, scriptName, Collections.emptyMap()), scriptService, new Processor() {
@Override
public IngestDocument execute(final IngestDocument ingestDocument) {
return ingestDocument;
}
@Override
public String getType() {
return null;
}
@Override
public String getTag() {
return null;
}
@Override
public String getDescription() {
return null;
}
}, relativeTimeProvider);
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), Collections.emptyMap());
processor.execute(ingestDocument, (result, e) -> {
});
assertWarnings("[types removal] Looking up doc types [_type] in scripts is deprecated.");
}
Aggregations