Search in sources :

Example 1 with MockScriptEngine

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

the class IngestServiceTests method testGetProcessorsInPipelineComplexConditional.

public void testGetProcessorsInPipelineComplexConditional() throws Exception {
    LongSupplier relativeTimeProvider = mock(LongSupplier.class);
    String scriptName = "conditionalScript";
    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));
    Map<String, Processor.Factory> processors = new HashMap<>();
    processors.put("complexSet", (factories, tag, description, config) -> {
        String field = (String) config.remove("field");
        String value = (String) config.remove("value");
        return new ConditionalProcessor(randomAlphaOfLength(10), null, new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, scriptName, Collections.emptyMap()), scriptService, new ConditionalProcessor(randomAlphaOfLength(10) + "-nested", null, new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, scriptName, Collections.emptyMap()), scriptService, new FakeProcessor("complexSet", tag, description, (ingestDocument) -> ingestDocument.setFieldValue(field, value))));
    });
    IngestService ingestService = createWithProcessors(processors);
    String id = "_id";
    Pipeline pipeline = ingestService.getPipeline(id);
    assertThat(pipeline, nullValue());
    // Start empty
    ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).build();
    PutPipelineRequest putRequest = new PutPipelineRequest(id, new BytesArray("{\"processors\": [{\"complexSet\" : {\"field\": \"_field\", \"value\": \"_value\"}}]}"), XContentType.JSON);
    ClusterState previousClusterState = clusterState;
    clusterState = IngestService.innerPut(putRequest, clusterState);
    ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState));
    pipeline = ingestService.getPipeline(id);
    assertThat(pipeline, notNullValue());
    assertThat(ingestService.getProcessorsInPipeline(id, Processor.class).size(), equalTo(3));
    assertThat(ingestService.getProcessorsInPipeline(id, WrappingProcessor.class).size(), equalTo(2));
    assertThat(ingestService.getProcessorsInPipeline(id, FakeProcessor.class).size(), equalTo(1));
    assertThat(ingestService.getProcessorsInPipeline(id, ConditionalProcessor.class).size(), equalTo(2));
    assertThat(ingestService.getProcessorsInPipeline(id, WrappingProcessorImpl.class).size(), equalTo(0));
}
Also used : DeletePipelineRequest(org.opensearch.action.ingest.DeletePipelineRequest) Arrays(java.util.Arrays) Metadata(org.opensearch.cluster.metadata.Metadata) LongSupplier(java.util.function.LongSupplier) ScriptModule(org.opensearch.script.ScriptModule) BulkRequest(org.opensearch.action.bulk.BulkRequest) IntConsumer(java.util.function.IntConsumer) Level(org.apache.logging.log4j.Level) Version(org.opensearch.Version) PutPipelineRequest(org.opensearch.action.ingest.PutPipelineRequest) Mockito.argThat(org.mockito.Mockito.argThat) ScriptType(org.opensearch.script.ScriptType) ArgumentMatcher(org.mockito.ArgumentMatcher) Mockito.doThrow(org.mockito.Mockito.doThrow) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Mockito.doAnswer(org.mockito.Mockito.doAnswer) MockScriptEngine(org.opensearch.script.MockScriptEngine) DeleteRequest(org.opensearch.action.delete.DeleteRequest) ScriptService(org.opensearch.script.ScriptService) Client(org.opensearch.client.Client) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Script(org.opensearch.script.Script) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) OpenSearchParseException(org.opensearch.OpenSearchParseException) Settings(org.opensearch.common.settings.Settings) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) VersionType(org.opensearch.index.VersionType) List(java.util.List) BytesArray(org.opensearch.common.bytes.BytesArray) Matchers.equalTo(org.hamcrest.Matchers.equalTo) IndexSettings(org.opensearch.index.IndexSettings) UpdateRequest(org.opensearch.action.update.UpdateRequest) XContentType(org.opensearch.common.xcontent.XContentType) Matchers.is(org.hamcrest.Matchers.is) Mockito.any(org.mockito.Mockito.any) TransportBulkAction(org.opensearch.action.bulk.TransportBulkAction) Names(org.opensearch.threadpool.ThreadPool.Names) Mockito.eq(org.mockito.Mockito.eq) Mockito.mock(org.mockito.Mockito.mock) MockLogAppender(org.opensearch.test.MockLogAppender) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) CborXContent(org.opensearch.common.xcontent.cbor.CborXContent) ThreadPool(org.opensearch.threadpool.ThreadPool) DocWriteRequest(org.opensearch.action.DocWriteRequest) HashMap(java.util.HashMap) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) ResourceNotFoundException(org.opensearch.ResourceNotFoundException) OpenSearchExecutors(org.opensearch.common.util.concurrent.OpenSearchExecutors) AtomicReference(java.util.concurrent.atomic.AtomicReference) Requests(org.opensearch.client.Requests) ClusterState(org.opensearch.cluster.ClusterState) InvocationOnMock(org.mockito.invocation.InvocationOnMock) BiConsumer(java.util.function.BiConsumer) Mockito.anyString(org.mockito.Mockito.anyString) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) Collections.emptyMap(java.util.Collections.emptyMap) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) SetOnce(org.apache.lucene.util.SetOnce) Collections.emptySet(java.util.Collections.emptySet) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) Consumer(java.util.function.Consumer) Mockito.never(org.mockito.Mockito.never) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) ClusterService(org.opensearch.cluster.service.ClusterService) ClusterName(org.opensearch.cluster.ClusterName) Mockito.anyInt(org.mockito.Mockito.anyInt) IndexRequest(org.opensearch.action.index.IndexRequest) Comparator(java.util.Comparator) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) IngestPlugin(org.opensearch.plugins.IngestPlugin) ClusterChangedEvent(org.opensearch.cluster.ClusterChangedEvent) Script(org.opensearch.script.Script) ClusterState(org.opensearch.cluster.ClusterState) BytesArray(org.opensearch.common.bytes.BytesArray) HashMap(java.util.HashMap) ClusterChangedEvent(org.opensearch.cluster.ClusterChangedEvent) Mockito.anyString(org.mockito.Mockito.anyString) ScriptService(org.opensearch.script.ScriptService) MockScriptEngine(org.opensearch.script.MockScriptEngine) ClusterName(org.opensearch.cluster.ClusterName) PutPipelineRequest(org.opensearch.action.ingest.PutPipelineRequest) LongSupplier(java.util.function.LongSupplier)

Example 2 with MockScriptEngine

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

the class ConditionalProcessorTests method testChecksCondition.

public void testChecksCondition() throws Exception {
    String conditionalField = "field1";
    String scriptName = "conditionalScript";
    String trueValue = "truthy";
    ScriptService scriptService = new ScriptService(Settings.builder().build(), Collections.singletonMap(Script.DEFAULT_SCRIPT_LANG, new MockScriptEngine(Script.DEFAULT_SCRIPT_LANG, Collections.singletonMap(scriptName, ctx -> trueValue.equals(ctx.get(conditionalField))), Collections.emptyMap())), new HashMap<>(ScriptModule.CORE_CONTEXTS));
    Map<String, Object> document = new HashMap<>();
    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) {
            if (ingestDocument.hasField("error")) {
                throw new RuntimeException("error");
            }
            ingestDocument.setFieldValue("foo", "bar");
            return ingestDocument;
        }

        @Override
        public String getType() {
            return null;
        }

        @Override
        public String getTag() {
            return null;
        }

        @Override
        public String getDescription() {
            return null;
        }
    }, relativeTimeProvider);
    // false, never call processor never increments metrics
    String falseValue = "falsy";
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
    ingestDocument.setFieldValue(conditionalField, falseValue);
    processor.execute(ingestDocument, (result, e) -> {
    });
    assertThat(ingestDocument.getSourceAndMetadata().get(conditionalField), is(falseValue));
    assertThat(ingestDocument.getSourceAndMetadata(), not(hasKey("foo")));
    assertStats(processor, 0, 0, 0);
    assertEquals(scriptName, processor.getCondition());
    ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
    ingestDocument.setFieldValue(conditionalField, falseValue);
    ingestDocument.setFieldValue("error", true);
    processor.execute(ingestDocument, (result, e) -> {
    });
    assertStats(processor, 0, 0, 0);
    // true, always call processor and increments metrics
    ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
    ingestDocument.setFieldValue(conditionalField, trueValue);
    processor.execute(ingestDocument, (result, e) -> {
    });
    assertThat(ingestDocument.getSourceAndMetadata().get(conditionalField), is(trueValue));
    assertThat(ingestDocument.getSourceAndMetadata().get("foo"), is("bar"));
    assertStats(processor, 1, 0, 1);
    ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
    ingestDocument.setFieldValue(conditionalField, trueValue);
    ingestDocument.setFieldValue("error", true);
    IngestDocument finalIngestDocument = ingestDocument;
    Exception[] holder = new Exception[1];
    processor.execute(finalIngestDocument, (result, e) -> {
        holder[0] = e;
    });
    assertThat(holder[0], instanceOf(RuntimeException.class));
    assertStats(processor, 2, 1, 2);
}
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) MockScriptEngine(org.opensearch.script.MockScriptEngine) LongSupplier(java.util.function.LongSupplier)

Example 3 with MockScriptEngine

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

the class TrackingResultProcessorTests method testActualPipelineProcessorWithFalseConditional.

public void testActualPipelineProcessorWithFalseConditional() throws Exception {
    String pipelineId1 = "pipeline1";
    String pipelineId2 = "pipeline2";
    IngestService ingestService = createIngestService();
    Map<String, Object> pipelineConfig0 = new HashMap<>();
    pipelineConfig0.put("name", pipelineId1);
    Map<String, Object> pipelineConfig1 = new HashMap<>();
    pipelineConfig1.put("name", pipelineId1);
    Map<String, Object> pipelineConfig2 = new HashMap<>();
    pipelineConfig2.put("name", pipelineId2);
    PipelineProcessor.Factory factory = new PipelineProcessor.Factory(ingestService);
    String key1 = randomAlphaOfLength(10);
    String key2 = randomAlphaOfLength(10);
    String key3 = randomAlphaOfLength(10);
    String scriptName = "conditionalScript";
    ScriptService scriptService = new ScriptService(Settings.builder().build(), Collections.singletonMap(Script.DEFAULT_SCRIPT_LANG, new MockScriptEngine(Script.DEFAULT_SCRIPT_LANG, Collections.singletonMap(scriptName, ctx -> false), Collections.emptyMap())), new HashMap<>(ScriptModule.CORE_CONTEXTS));
    Pipeline pipeline1 = new Pipeline(pipelineId1, null, null, new CompoundProcessor(new TestProcessor(ingestDocument -> {
        ingestDocument.setFieldValue(key1, randomInt());
    }), new ConditionalProcessor(randomAlphaOfLength(10), null, new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, scriptName, Collections.emptyMap()), scriptService, factory.create(Collections.emptyMap(), null, null, pipelineConfig2)), new TestProcessor(ingestDocument -> {
        ingestDocument.setFieldValue(key3, randomInt());
    })));
    Pipeline pipeline2 = new Pipeline(pipelineId2, null, null, new CompoundProcessor(new TestProcessor(ingestDocument -> {
        ingestDocument.setFieldValue(key2, randomInt());
    })));
    when(ingestService.getPipeline(pipelineId1)).thenReturn(pipeline1);
    when(ingestService.getPipeline(pipelineId2)).thenReturn(pipeline2);
    PipelineProcessor pipelineProcessor = factory.create(Collections.emptyMap(), null, null, pipelineConfig0);
    CompoundProcessor actualProcessor = new CompoundProcessor(pipelineProcessor);
    CompoundProcessor trackingProcessor = decorate(actualProcessor, null, resultList);
    trackingProcessor.execute(ingestDocument, (result, e) -> {
    });
    SimulateProcessorResult expectedResult = new SimulateProcessorResult(actualProcessor.getType(), actualProcessor.getTag(), actualProcessor.getDescription(), ingestDocument, null);
    expectedResult.getIngestDocument().getIngestMetadata().put("pipeline", pipelineId1);
    verify(ingestService, Mockito.atLeast(1)).getPipeline(pipelineId1);
    verify(ingestService, Mockito.never()).getPipeline(pipelineId2);
    assertThat(resultList.size(), equalTo(4));
    assertNull(resultList.get(0).getConditionalWithResult());
    assertThat(resultList.get(0).getType(), equalTo("pipeline"));
    assertTrue(resultList.get(1).getIngestDocument().hasField(key1));
    assertFalse(resultList.get(1).getIngestDocument().hasField(key2));
    assertFalse(resultList.get(1).getIngestDocument().hasField(key3));
    assertThat(resultList.get(2).getConditionalWithResult().v1(), equalTo(scriptName));
    assertThat(resultList.get(2).getConditionalWithResult().v2(), is(Boolean.FALSE));
    assertThat(resultList.get(3).getIngestDocument(), equalTo(expectedResult.getIngestDocument()));
    assertThat(resultList.get(3).getFailure(), nullValue());
    assertThat(resultList.get(3).getProcessorTag(), nullValue());
}
Also used : PipelineProcessorTests.createIngestService(org.opensearch.ingest.PipelineProcessorTests.createIngestService) Arrays(java.util.Arrays) ON_FAILURE_PROCESSOR_TAG_FIELD(org.opensearch.ingest.CompoundProcessor.ON_FAILURE_PROCESSOR_TAG_FIELD) TrackingResultProcessor.decorate(org.opensearch.ingest.TrackingResultProcessor.decorate) ScriptModule(org.opensearch.script.ScriptModule) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) CoreMatchers.not(org.hamcrest.CoreMatchers.not) HashMap(java.util.HashMap) SimulateProcessorResult(org.opensearch.action.ingest.SimulateProcessorResult) ArrayList(java.util.ArrayList) ScriptType(org.opensearch.script.ScriptType) ON_FAILURE_PROCESSOR_TYPE_FIELD(org.opensearch.ingest.CompoundProcessor.ON_FAILURE_PROCESSOR_TYPE_FIELD) ON_FAILURE_MESSAGE_FIELD(org.opensearch.ingest.CompoundProcessor.ON_FAILURE_MESSAGE_FIELD) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) MockScriptEngine(org.opensearch.script.MockScriptEngine) Before(org.junit.Before) ScriptService(org.opensearch.script.ScriptService) Script(org.opensearch.script.Script) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Settings(org.opensearch.common.settings.Settings) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Mockito(org.mockito.Mockito) List(java.util.List) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) Script(org.opensearch.script.Script) PipelineProcessorTests.createIngestService(org.opensearch.ingest.PipelineProcessorTests.createIngestService) HashMap(java.util.HashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) ScriptService(org.opensearch.script.ScriptService) SimulateProcessorResult(org.opensearch.action.ingest.SimulateProcessorResult) MockScriptEngine(org.opensearch.script.MockScriptEngine)

Example 4 with MockScriptEngine

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

the class TrackingResultProcessorTests method testActualCompoundProcessorWithFalseConditional.

public void testActualCompoundProcessorWithFalseConditional() throws Exception {
    String key1 = randomAlphaOfLength(10);
    String key2 = randomAlphaOfLength(10);
    String key3 = randomAlphaOfLength(10);
    String scriptName = "conditionalScript";
    ScriptService scriptService = new ScriptService(Settings.builder().build(), Collections.singletonMap(Script.DEFAULT_SCRIPT_LANG, new MockScriptEngine(Script.DEFAULT_SCRIPT_LANG, Collections.singletonMap(scriptName, ctx -> false), Collections.emptyMap())), new HashMap<>(ScriptModule.CORE_CONTEXTS));
    CompoundProcessor compoundProcessor = new CompoundProcessor(new TestProcessor(ingestDocument -> {
        ingestDocument.setFieldValue(key1, randomInt());
    }), new ConditionalProcessor(randomAlphaOfLength(10), null, new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, scriptName, Collections.emptyMap()), scriptService, new TestProcessor(ingestDocument -> {
        ingestDocument.setFieldValue(key2, randomInt());
    })), new TestProcessor(ingestDocument -> {
        ingestDocument.setFieldValue(key3, randomInt());
    }));
    CompoundProcessor trackingProcessor = decorate(compoundProcessor, null, resultList);
    trackingProcessor.execute(ingestDocument, (result, e) -> {
    });
    SimulateProcessorResult expectedResult = new SimulateProcessorResult(compoundProcessor.getType(), compoundProcessor.getTag(), compoundProcessor.getDescription(), ingestDocument, null);
    assertThat(resultList.size(), equalTo(3));
    assertTrue(resultList.get(0).getIngestDocument().hasField(key1));
    assertFalse(resultList.get(0).getIngestDocument().hasField(key2));
    assertFalse(resultList.get(0).getIngestDocument().hasField(key3));
    assertThat(resultList.get(1).getConditionalWithResult().v1(), equalTo(scriptName));
    assertThat(resultList.get(1).getConditionalWithResult().v2(), is(Boolean.FALSE));
    assertTrue(resultList.get(2).getIngestDocument().hasField(key1));
    assertFalse(resultList.get(2).getIngestDocument().hasField(key2));
    assertTrue(resultList.get(2).getIngestDocument().hasField(key3));
    assertThat(resultList.get(2).getIngestDocument(), equalTo(expectedResult.getIngestDocument()));
    assertThat(resultList.get(2).getFailure(), nullValue());
    assertThat(resultList.get(2).getProcessorTag(), nullValue());
}
Also used : ScriptService(org.opensearch.script.ScriptService) PipelineProcessorTests.createIngestService(org.opensearch.ingest.PipelineProcessorTests.createIngestService) Arrays(java.util.Arrays) ON_FAILURE_PROCESSOR_TAG_FIELD(org.opensearch.ingest.CompoundProcessor.ON_FAILURE_PROCESSOR_TAG_FIELD) TrackingResultProcessor.decorate(org.opensearch.ingest.TrackingResultProcessor.decorate) ScriptModule(org.opensearch.script.ScriptModule) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) CoreMatchers.not(org.hamcrest.CoreMatchers.not) HashMap(java.util.HashMap) SimulateProcessorResult(org.opensearch.action.ingest.SimulateProcessorResult) ArrayList(java.util.ArrayList) ScriptType(org.opensearch.script.ScriptType) ON_FAILURE_PROCESSOR_TYPE_FIELD(org.opensearch.ingest.CompoundProcessor.ON_FAILURE_PROCESSOR_TYPE_FIELD) ON_FAILURE_MESSAGE_FIELD(org.opensearch.ingest.CompoundProcessor.ON_FAILURE_MESSAGE_FIELD) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) MockScriptEngine(org.opensearch.script.MockScriptEngine) Before(org.junit.Before) ScriptService(org.opensearch.script.ScriptService) Script(org.opensearch.script.Script) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Settings(org.opensearch.common.settings.Settings) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Mockito(org.mockito.Mockito) List(java.util.List) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) Script(org.opensearch.script.Script) SimulateProcessorResult(org.opensearch.action.ingest.SimulateProcessorResult) MockScriptEngine(org.opensearch.script.MockScriptEngine) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 5 with MockScriptEngine

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

the class TrackingResultProcessorTests method testActualCompoundProcessorWithOnFailureAndTrueCondition.

public void testActualCompoundProcessorWithOnFailureAndTrueCondition() throws Exception {
    String scriptName = "conditionalScript";
    ScriptService scriptService = new ScriptService(Settings.builder().build(), Collections.singletonMap(Script.DEFAULT_SCRIPT_LANG, new MockScriptEngine(Script.DEFAULT_SCRIPT_LANG, Collections.singletonMap(scriptName, ctx -> true), Collections.emptyMap())), new HashMap<>(ScriptModule.CORE_CONTEXTS));
    RuntimeException exception = new RuntimeException("fail");
    TestProcessor failProcessor = new TestProcessor("fail", "test", null, exception);
    ConditionalProcessor conditionalProcessor = new ConditionalProcessor(randomAlphaOfLength(10), null, new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, scriptName, Collections.emptyMap()), scriptService, failProcessor);
    TestProcessor onFailureProcessor = new TestProcessor("success", "test", null, ingestDocument -> {
    });
    CompoundProcessor actualProcessor = new CompoundProcessor(false, Arrays.asList(conditionalProcessor), Arrays.asList(onFailureProcessor));
    CompoundProcessor trackingProcessor = decorate(actualProcessor, null, resultList);
    trackingProcessor.execute(ingestDocument, (result, e) -> {
    });
    SimulateProcessorResult expectedFailResult = new SimulateProcessorResult(failProcessor.getType(), failProcessor.getTag(), failProcessor.getDescription(), ingestDocument, null);
    SimulateProcessorResult expectedSuccessResult = new SimulateProcessorResult(onFailureProcessor.getType(), onFailureProcessor.getTag(), onFailureProcessor.getDescription(), ingestDocument, null);
    assertThat(failProcessor.getInvokedCounter(), equalTo(1));
    assertThat(onFailureProcessor.getInvokedCounter(), equalTo(1));
    assertThat(resultList.size(), equalTo(2));
    assertThat(resultList.get(0).getIngestDocument(), nullValue());
    assertThat(resultList.get(0).getFailure(), equalTo(exception));
    assertThat(resultList.get(0).getProcessorTag(), equalTo(expectedFailResult.getProcessorTag()));
    assertThat(resultList.get(0).getConditionalWithResult().v1(), equalTo(scriptName));
    assertThat(resultList.get(0).getConditionalWithResult().v2(), is(Boolean.TRUE));
    Map<String, Object> metadata = resultList.get(1).getIngestDocument().getIngestMetadata();
    assertThat(metadata.get(ON_FAILURE_MESSAGE_FIELD), equalTo("fail"));
    assertThat(metadata.get(ON_FAILURE_PROCESSOR_TYPE_FIELD), equalTo("test"));
    assertThat(metadata.get(ON_FAILURE_PROCESSOR_TAG_FIELD), equalTo("fail"));
    assertThat(resultList.get(1).getFailure(), nullValue());
    assertThat(resultList.get(1).getProcessorTag(), equalTo(expectedSuccessResult.getProcessorTag()));
}
Also used : PipelineProcessorTests.createIngestService(org.opensearch.ingest.PipelineProcessorTests.createIngestService) Arrays(java.util.Arrays) ON_FAILURE_PROCESSOR_TAG_FIELD(org.opensearch.ingest.CompoundProcessor.ON_FAILURE_PROCESSOR_TAG_FIELD) TrackingResultProcessor.decorate(org.opensearch.ingest.TrackingResultProcessor.decorate) ScriptModule(org.opensearch.script.ScriptModule) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) CoreMatchers.not(org.hamcrest.CoreMatchers.not) HashMap(java.util.HashMap) SimulateProcessorResult(org.opensearch.action.ingest.SimulateProcessorResult) ArrayList(java.util.ArrayList) ScriptType(org.opensearch.script.ScriptType) ON_FAILURE_PROCESSOR_TYPE_FIELD(org.opensearch.ingest.CompoundProcessor.ON_FAILURE_PROCESSOR_TYPE_FIELD) ON_FAILURE_MESSAGE_FIELD(org.opensearch.ingest.CompoundProcessor.ON_FAILURE_MESSAGE_FIELD) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) MockScriptEngine(org.opensearch.script.MockScriptEngine) Before(org.junit.Before) ScriptService(org.opensearch.script.ScriptService) Script(org.opensearch.script.Script) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Settings(org.opensearch.common.settings.Settings) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) Mockito(org.mockito.Mockito) List(java.util.List) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) Script(org.opensearch.script.Script) Matchers.containsString(org.hamcrest.Matchers.containsString) ScriptService(org.opensearch.script.ScriptService) SimulateProcessorResult(org.opensearch.action.ingest.SimulateProcessorResult) MockScriptEngine(org.opensearch.script.MockScriptEngine)

Aggregations

MockScriptEngine (org.opensearch.script.MockScriptEngine)29 ScriptService (org.opensearch.script.ScriptService)29 HashMap (java.util.HashMap)21 Settings (org.opensearch.common.settings.Settings)20 Map (java.util.Map)19 Script (org.opensearch.script.Script)19 ScriptEngine (org.opensearch.script.ScriptEngine)18 ScriptModule (org.opensearch.script.ScriptModule)18 ScriptType (org.opensearch.script.ScriptType)18 Collections (java.util.Collections)16 List (java.util.List)15 ArrayList (java.util.ArrayList)13 Function (java.util.function.Function)12 Arrays (java.util.Arrays)11 Consumer (java.util.function.Consumer)11 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)10 Before (org.junit.Before)9 Mockito.when (org.mockito.Mockito.when)9 IOException (java.io.IOException)8 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)8