use of org.opensearch.script.IngestConditionalScript in project OpenSearch by opensearch-project.
the class ConditionalProcessorTests method testRuntimeError.
public void testRuntimeError() {
ScriptService scriptService = MockScriptService.singleContext(IngestConditionalScript.CONTEXT, code -> params -> new IngestConditionalScript(params) {
@Override
public boolean execute(Map<String, Object> ctx) {
throw new IllegalArgumentException("runtime problem");
}
}, org.opensearch.common.collect.Map.of());
Script script = new Script(ScriptType.INLINE, "lang", "foo", org.opensearch.common.collect.Map.of());
ConditionalProcessor processor = new ConditionalProcessor(null, null, script, scriptService, null);
IngestDocument ingestDoc = new IngestDocument(org.opensearch.common.collect.Map.of(), org.opensearch.common.collect.Map.of());
processor.execute(ingestDoc, (doc, e) -> {
assertThat(e.getMessage(), equalTo("runtime problem"));
});
}
use of org.opensearch.script.IngestConditionalScript in project OpenSearch by opensearch-project.
the class ConditionalProcessor method evaluate.
boolean evaluate(IngestDocument ingestDocument) {
IngestConditionalScript script = precompiledConditionScript;
if (script == null) {
IngestConditionalScript.Factory factory = scriptService.compile(condition, IngestConditionalScript.CONTEXT);
script = factory.newInstance(condition.getParams());
}
return script.execute(new UnmodifiableIngestData(new DynamicMap(ingestDocument.getSourceAndMetadata(), FUNCTIONS)));
}
use of org.opensearch.script.IngestConditionalScript in project OpenSearch by opensearch-project.
the class ConditionalProcessorTests method testRuntimeCompileError.
public void testRuntimeCompileError() {
AtomicBoolean fail = new AtomicBoolean(false);
Map<String, StoredScriptSource> storedScripts = new HashMap<>();
storedScripts.put("foo", new StoredScriptSource("lang", "", org.opensearch.common.collect.Map.of()));
ScriptService scriptService = MockScriptService.singleContext(IngestConditionalScript.CONTEXT, code -> {
if (fail.get()) {
throw new ScriptException("bad script", new ParseException("error", 0), org.opensearch.common.collect.List.of(), "", "lang", null);
} else {
return params -> new IngestConditionalScript(params) {
@Override
public boolean execute(Map<String, Object> ctx) {
return false;
}
};
}
}, storedScripts);
Script script = new Script(ScriptType.STORED, null, "foo", org.opensearch.common.collect.Map.of());
ConditionalProcessor processor = new ConditionalProcessor(null, null, script, scriptService, null);
fail.set(true);
// must change the script source or the cached version will be used
storedScripts.put("foo", new StoredScriptSource("lang", "changed", org.opensearch.common.collect.Map.of()));
IngestDocument ingestDoc = new IngestDocument(org.opensearch.common.collect.Map.of(), org.opensearch.common.collect.Map.of());
processor.execute(ingestDoc, (doc, e) -> {
assertThat(e.getMessage(), equalTo("bad script"));
});
}
Aggregations