Search in sources :

Example 16 with ScriptException

use of org.opensearch.script.ScriptException in project OpenSearch by opensearch-project.

the class ExpressionNumberSortScriptTests method testLinkError.

public void testLinkError() {
    ScriptException e = expectThrows(ScriptException.class, () -> {
        compile("doc['nonexistent'].value * 5");
    });
    assertTrue(e.getCause() instanceof ParseException);
}
Also used : ScriptException(org.opensearch.script.ScriptException) ParseException(java.text.ParseException)

Example 17 with ScriptException

use of org.opensearch.script.ScriptException in project OpenSearch by opensearch-project.

the class ConditionalProcessorTests method testPrecompiledError.

public void testPrecompiledError() {
    ScriptService scriptService = MockScriptService.singleContext(IngestConditionalScript.CONTEXT, code -> {
        throw new ScriptException("bad script", new ParseException("error", 0), org.opensearch.common.collect.List.of(), "", "lang", null);
    }, org.opensearch.common.collect.Map.of());
    Script script = new Script(ScriptType.INLINE, "lang", "foo", org.opensearch.common.collect.Map.of());
    ScriptException e = expectThrows(ScriptException.class, () -> new ConditionalProcessor(null, null, script, scriptService, null));
    assertThat(e.getMessage(), equalTo("bad script"));
}
Also used : ScriptService(org.opensearch.script.ScriptService) MockScriptService(org.opensearch.script.MockScriptService) ScriptException(org.opensearch.script.ScriptException) Script(org.opensearch.script.Script) IngestConditionalScript(org.opensearch.script.IngestConditionalScript) ParseException(java.text.ParseException)

Example 18 with ScriptException

use of org.opensearch.script.ScriptException 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

ScriptException (org.opensearch.script.ScriptException)18 ParseException (java.text.ParseException)8 Map (java.util.Map)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Script (org.opensearch.script.Script)3 ScriptService (org.opensearch.script.ScriptService)3 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Matchers.emptyOrNullString (org.hamcrest.Matchers.emptyOrNullString)2 IngestConditionalScript (org.opensearch.script.IngestConditionalScript)2 MockScriptService (org.opensearch.script.MockScriptService)2 Mustache (com.github.mustachejava.Mustache)1 MustacheException (com.github.mustachejava.MustacheException)1 MustacheFactory (com.github.mustachejava.MustacheFactory)1 EOFException (java.io.EOFException)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Reader (java.io.Reader)1