Search in sources :

Example 26 with MockScriptEngine

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);
}
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) HashMap(java.util.HashMap) ScriptException(org.opensearch.script.ScriptException) ParseException(java.text.ParseException) ScriptService(org.opensearch.script.ScriptService) MockScriptService(org.opensearch.script.MockScriptService) CompletableFuture(java.util.concurrent.CompletableFuture) MockScriptEngine(org.opensearch.script.MockScriptEngine)

Example 27 with MockScriptEngine

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"));
}
Also used : ScriptService(org.opensearch.script.ScriptService) ScriptService(org.opensearch.script.ScriptService) ScriptModule(org.opensearch.script.ScriptModule) Script(org.opensearch.script.Script) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Settings(org.opensearch.common.settings.Settings) HashMap(java.util.HashMap) Mockito.when(org.mockito.Mockito.when) OpenSearchException(org.opensearch.OpenSearchException) ScriptException(org.opensearch.script.ScriptException) ScriptType(org.opensearch.script.ScriptType) IngestScript(org.opensearch.script.IngestScript) Map(java.util.Map) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.is(org.hamcrest.Matchers.is) XContentParseException(org.opensearch.common.xcontent.XContentParseException) MockScriptEngine(org.opensearch.script.MockScriptEngine) Mockito.any(org.mockito.Mockito.any) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) Before(org.junit.Before) Mockito.mock(org.mockito.Mockito.mock) HashMap(java.util.HashMap) MockScriptEngine(org.opensearch.script.MockScriptEngine) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 28 with MockScriptEngine

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());
}
Also used : ScriptService(org.opensearch.script.ScriptService) Script(org.opensearch.script.Script) IngestScript(org.opensearch.script.IngestScript) HashMap(java.util.HashMap) MockScriptEngine(org.opensearch.script.MockScriptEngine) Before(org.junit.Before)

Example 29 with MockScriptEngine

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.");
}
Also used : ScriptService(org.opensearch.script.ScriptService) ScriptService(org.opensearch.script.ScriptService) ScriptModule(org.opensearch.script.ScriptModule) Script(org.opensearch.script.Script) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Settings(org.opensearch.common.settings.Settings) HashMap(java.util.HashMap) Matchers.hasKey(org.hamcrest.Matchers.hasKey) ScriptType(org.opensearch.script.ScriptType) IngestDocument(org.opensearch.ingest.IngestDocument) IngestScript(org.opensearch.script.IngestScript) Map(java.util.Map) Is.is(org.hamcrest.core.Is.is) RandomDocumentPicks(org.opensearch.ingest.RandomDocumentPicks) MockScriptEngine(org.opensearch.script.MockScriptEngine) Collections(java.util.Collections) Before(org.junit.Before) Script(org.opensearch.script.Script) IngestScript(org.opensearch.script.IngestScript) MockScriptEngine(org.opensearch.script.MockScriptEngine) IngestDocument(org.opensearch.ingest.IngestDocument)

Example 30 with MockScriptEngine

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.");
}
Also used : ScriptService(org.opensearch.script.ScriptService) MockScriptService(org.opensearch.script.MockScriptService) 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) MockScriptEngine(org.opensearch.script.MockScriptEngine) LongSupplier(java.util.function.LongSupplier)

Aggregations

MockScriptEngine (org.opensearch.script.MockScriptEngine)30 ScriptService (org.opensearch.script.ScriptService)30 HashMap (java.util.HashMap)22 Settings (org.opensearch.common.settings.Settings)21 Map (java.util.Map)20 Script (org.opensearch.script.Script)20 ScriptEngine (org.opensearch.script.ScriptEngine)19 ScriptModule (org.opensearch.script.ScriptModule)19 ScriptType (org.opensearch.script.ScriptType)19 Collections (java.util.Collections)17 List (java.util.List)16 ArrayList (java.util.ArrayList)14 Function (java.util.function.Function)13 Consumer (java.util.function.Consumer)12 Arrays (java.util.Arrays)11 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)10 IOException (java.io.IOException)9 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)9 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)9 RandomIndexWriter (org.apache.lucene.tests.index.RandomIndexWriter)9