Search in sources :

Example 16 with PutPipelineRequest

use of org.opensearch.action.ingest.PutPipelineRequest 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 17 with PutPipelineRequest

use of org.opensearch.action.ingest.PutPipelineRequest in project OpenSearch by opensearch-project.

the class IngestServiceTests method testGetProcessorsInPipeline.

public void testGetProcessorsInPipeline() throws Exception {
    IngestService ingestService = createWithProcessors();
    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\": [{\"set\" : {\"field\": \"_field\", \"value\": \"_value\", \"tag\": \"tag1\"}}," + "{\"remove\" : {\"field\": \"_field\", \"tag\": \"tag2\"}}]}"), 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, WrappingProcessorImpl.class).size(), equalTo(1));
    assertThat(ingestService.getProcessorsInPipeline(id, WrappingProcessor.class).size(), equalTo(1));
    assertThat(ingestService.getProcessorsInPipeline(id, FakeProcessor.class).size(), equalTo(2));
    assertThat(ingestService.getProcessorsInPipeline(id, ConditionalProcessor.class).size(), equalTo(0));
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> ingestService.getProcessorsInPipeline("fakeID", Processor.class));
    assertThat("pipeline with id [fakeID] does not exist", equalTo(e.getMessage()));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) BytesArray(org.opensearch.common.bytes.BytesArray) ClusterName(org.opensearch.cluster.ClusterName) PutPipelineRequest(org.opensearch.action.ingest.PutPipelineRequest) ClusterChangedEvent(org.opensearch.cluster.ClusterChangedEvent) Mockito.anyString(org.mockito.Mockito.anyString)

Example 18 with PutPipelineRequest

use of org.opensearch.action.ingest.PutPipelineRequest in project OpenSearch by opensearch-project.

the class IngestServiceTests method testValidateNoIngestInfo.

public void testValidateNoIngestInfo() throws Exception {
    IngestService ingestService = createWithProcessors();
    PutPipelineRequest putRequest = new PutPipelineRequest("_id", new BytesArray("{\"processors\": [{\"set\" : {\"field\": \"_field\", \"value\": \"_value\"}}]}"), XContentType.JSON);
    Exception e = expectThrows(IllegalStateException.class, () -> ingestService.validatePipeline(emptyMap(), putRequest));
    assertEquals("Ingest info is empty", e.getMessage());
    DiscoveryNode discoveryNode = new DiscoveryNode("_node_id", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT);
    IngestInfo ingestInfo = new IngestInfo(Collections.singletonList(new ProcessorInfo("set")));
    ingestService.validatePipeline(Collections.singletonMap(discoveryNode, ingestInfo), putRequest);
}
Also used : BytesArray(org.opensearch.common.bytes.BytesArray) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) PutPipelineRequest(org.opensearch.action.ingest.PutPipelineRequest) OpenSearchParseException(org.opensearch.OpenSearchParseException) ResourceNotFoundException(org.opensearch.ResourceNotFoundException)

Example 19 with PutPipelineRequest

use of org.opensearch.action.ingest.PutPipelineRequest in project OpenSearch by opensearch-project.

the class IngestServiceTests method testPut.

public void testPut() {
    IngestService ingestService = createWithProcessors();
    String id = "_id";
    Pipeline pipeline = ingestService.getPipeline(id);
    assertThat(pipeline, nullValue());
    ClusterState clusterState = ClusterState.builder(new ClusterName("_name")).build();
    // add a new pipeline:
    PutPipelineRequest putRequest = new PutPipelineRequest(id, new BytesArray("{\"processors\": []}"), XContentType.JSON);
    ClusterState previousClusterState = clusterState;
    clusterState = IngestService.innerPut(putRequest, clusterState);
    ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState));
    pipeline = ingestService.getPipeline(id);
    assertThat(pipeline, notNullValue());
    assertThat(pipeline.getId(), equalTo(id));
    assertThat(pipeline.getDescription(), nullValue());
    assertThat(pipeline.getProcessors().size(), equalTo(0));
    // overwrite existing pipeline:
    putRequest = new PutPipelineRequest(id, new BytesArray("{\"processors\": [], \"description\": \"_description\"}"), XContentType.JSON);
    previousClusterState = clusterState;
    clusterState = IngestService.innerPut(putRequest, clusterState);
    ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState));
    pipeline = ingestService.getPipeline(id);
    assertThat(pipeline, notNullValue());
    assertThat(pipeline.getId(), equalTo(id));
    assertThat(pipeline.getDescription(), equalTo("_description"));
    assertThat(pipeline.getProcessors().size(), equalTo(0));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) BytesArray(org.opensearch.common.bytes.BytesArray) ClusterName(org.opensearch.cluster.ClusterName) PutPipelineRequest(org.opensearch.action.ingest.PutPipelineRequest) ClusterChangedEvent(org.opensearch.cluster.ClusterChangedEvent) Mockito.anyString(org.mockito.Mockito.anyString)

Example 20 with PutPipelineRequest

use of org.opensearch.action.ingest.PutPipelineRequest in project OpenSearch by opensearch-project.

the class IngestServiceTests method testCrud.

public void testCrud() throws Exception {
    IngestService ingestService = createWithProcessors();
    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\": [{\"set\" : {\"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(pipeline.getId(), equalTo(id));
    assertThat(pipeline.getDescription(), nullValue());
    assertThat(pipeline.getProcessors().size(), equalTo(1));
    assertThat(pipeline.getProcessors().get(0).getType(), equalTo("set"));
    DeletePipelineRequest deleteRequest = new DeletePipelineRequest(id);
    previousClusterState = clusterState;
    clusterState = IngestService.innerDelete(deleteRequest, clusterState);
    ingestService.applyClusterState(new ClusterChangedEvent("", clusterState, previousClusterState));
    pipeline = ingestService.getPipeline(id);
    assertThat(pipeline, nullValue());
}
Also used : DeletePipelineRequest(org.opensearch.action.ingest.DeletePipelineRequest) ClusterState(org.opensearch.cluster.ClusterState) BytesArray(org.opensearch.common.bytes.BytesArray) ClusterName(org.opensearch.cluster.ClusterName) PutPipelineRequest(org.opensearch.action.ingest.PutPipelineRequest) ClusterChangedEvent(org.opensearch.cluster.ClusterChangedEvent) Mockito.anyString(org.mockito.Mockito.anyString)

Aggregations

PutPipelineRequest (org.opensearch.action.ingest.PutPipelineRequest)47 BytesArray (org.opensearch.common.bytes.BytesArray)29 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)21 OpenSearchParseException (org.opensearch.OpenSearchParseException)19 IndexRequest (org.opensearch.action.index.IndexRequest)19 ClusterChangedEvent (org.opensearch.cluster.ClusterChangedEvent)19 ClusterName (org.opensearch.cluster.ClusterName)19 ClusterState (org.opensearch.cluster.ClusterState)19 Settings (org.opensearch.common.settings.Settings)19 Mockito.anyString (org.mockito.Mockito.anyString)18 HashMap (java.util.HashMap)16 ResourceNotFoundException (org.opensearch.ResourceNotFoundException)16 DeletePipelineRequest (org.opensearch.action.ingest.DeletePipelineRequest)16 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 BulkRequest (org.opensearch.action.bulk.BulkRequest)15 XContentType (org.opensearch.common.xcontent.XContentType)15 List (java.util.List)13 BiConsumer (java.util.function.BiConsumer)13 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)13 Arrays (java.util.Arrays)12