Search in sources :

Example 1 with IngestConditionalScript

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"));
    });
}
Also used : ScriptService(org.opensearch.script.ScriptService) MockScriptService(org.opensearch.script.MockScriptService) Script(org.opensearch.script.Script) IngestConditionalScript(org.opensearch.script.IngestConditionalScript) IngestConditionalScript(org.opensearch.script.IngestConditionalScript)

Example 2 with IngestConditionalScript

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)));
}
Also used : DynamicMap(org.opensearch.script.DynamicMap) IngestConditionalScript(org.opensearch.script.IngestConditionalScript)

Example 3 with IngestConditionalScript

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"));
    });
}
Also used : StoredScriptSource(org.opensearch.script.StoredScriptSource) LongSupplier(java.util.function.LongSupplier) ScriptModule(org.opensearch.script.ScriptModule) Matchers.not(org.hamcrest.Matchers.not) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Matchers.hasKey(org.hamcrest.Matchers.hasKey) ScriptException(org.opensearch.script.ScriptException) ArrayList(java.util.ArrayList) ScriptType(org.opensearch.script.ScriptType) Map(java.util.Map) Is.is(org.hamcrest.core.Is.is) MockScriptEngine(org.opensearch.script.MockScriptEngine) ParseException(java.text.ParseException) ScriptService(org.opensearch.script.ScriptService) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) Script(org.opensearch.script.Script) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Settings(org.opensearch.common.settings.Settings) IngestConditionalScript(org.opensearch.script.IngestConditionalScript) Mockito.when(org.mockito.Mockito.when) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Collections(java.util.Collections) MockScriptService(org.opensearch.script.MockScriptService) Mockito.mock(org.mockito.Mockito.mock) Script(org.opensearch.script.Script) IngestConditionalScript(org.opensearch.script.IngestConditionalScript) IngestConditionalScript(org.opensearch.script.IngestConditionalScript) HashMap(java.util.HashMap) StoredScriptSource(org.opensearch.script.StoredScriptSource) ScriptService(org.opensearch.script.ScriptService) MockScriptService(org.opensearch.script.MockScriptService) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ScriptException(org.opensearch.script.ScriptException) ParseException(java.text.ParseException) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

IngestConditionalScript (org.opensearch.script.IngestConditionalScript)3 MockScriptService (org.opensearch.script.MockScriptService)2 Script (org.opensearch.script.Script)2 ScriptService (org.opensearch.script.ScriptService)2 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Consumer (java.util.function.Consumer)1 LongSupplier (java.util.function.LongSupplier)1 Matchers.equalTo (org.hamcrest.Matchers.equalTo)1 Matchers.greaterThanOrEqualTo (org.hamcrest.Matchers.greaterThanOrEqualTo)1 Matchers.hasKey (org.hamcrest.Matchers.hasKey)1 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)1 Matchers.not (org.hamcrest.Matchers.not)1