Search in sources :

Example 11 with CompoundProcessor

use of org.elasticsearch.ingest.CompoundProcessor in project elasticsearch by elastic.

the class SimulateExecutionServiceTests method testExecuteVerboseItemExceptionWithoutOnFailure.

public void testExecuteVerboseItemExceptionWithoutOnFailure() throws Exception {
    TestProcessor processor1 = new TestProcessor("processor_0", "mock", ingestDocument -> {
    });
    TestProcessor processor2 = new TestProcessor("processor_1", "mock", ingestDocument -> {
        throw new RuntimeException("processor failed");
    });
    TestProcessor processor3 = new TestProcessor("processor_2", "mock", ingestDocument -> {
    });
    Pipeline pipeline = new Pipeline("_id", "_description", version, new CompoundProcessor(processor1, processor2, processor3));
    SimulateDocumentResult actualItemResponse = executionService.executeDocument(pipeline, ingestDocument, true);
    assertThat(processor1.getInvokedCounter(), equalTo(1));
    assertThat(processor2.getInvokedCounter(), equalTo(1));
    assertThat(processor3.getInvokedCounter(), equalTo(0));
    assertThat(actualItemResponse, instanceOf(SimulateDocumentVerboseResult.class));
    SimulateDocumentVerboseResult simulateDocumentVerboseResult = (SimulateDocumentVerboseResult) actualItemResponse;
    assertThat(simulateDocumentVerboseResult.getProcessorResults().size(), equalTo(2));
    assertThat(simulateDocumentVerboseResult.getProcessorResults().get(0).getProcessorTag(), equalTo("processor_0"));
    assertThat(simulateDocumentVerboseResult.getProcessorResults().get(0).getFailure(), nullValue());
    assertThat(simulateDocumentVerboseResult.getProcessorResults().get(0).getIngestDocument(), not(sameInstance(ingestDocument)));
    assertIngestDocument(simulateDocumentVerboseResult.getProcessorResults().get(0).getIngestDocument(), ingestDocument);
    assertThat(simulateDocumentVerboseResult.getProcessorResults().get(0).getIngestDocument().getSourceAndMetadata(), not(sameInstance(ingestDocument.getSourceAndMetadata())));
    assertThat(simulateDocumentVerboseResult.getProcessorResults().get(1).getProcessorTag(), equalTo("processor_1"));
    assertThat(simulateDocumentVerboseResult.getProcessorResults().get(1).getIngestDocument(), nullValue());
    assertThat(simulateDocumentVerboseResult.getProcessorResults().get(1).getFailure(), instanceOf(RuntimeException.class));
    RuntimeException runtimeException = (RuntimeException) simulateDocumentVerboseResult.getProcessorResults().get(1).getFailure();
    assertThat(runtimeException.getMessage(), equalTo("processor failed"));
}
Also used : CompoundProcessor(org.elasticsearch.ingest.CompoundProcessor) TestProcessor(org.elasticsearch.ingest.TestProcessor) Pipeline(org.elasticsearch.ingest.Pipeline)

Example 12 with CompoundProcessor

use of org.elasticsearch.ingest.CompoundProcessor in project elasticsearch by elastic.

the class SimulateExecutionServiceTests method testExecuteVerboseItemWithOnFailure.

public void testExecuteVerboseItemWithOnFailure() throws Exception {
    TestProcessor processor1 = new TestProcessor("processor_0", "mock", ingestDocument -> {
        throw new RuntimeException("processor failed");
    });
    TestProcessor processor2 = new TestProcessor("processor_1", "mock", ingestDocument -> {
    });
    TestProcessor processor3 = new TestProcessor("processor_2", "mock", ingestDocument -> {
    });
    Pipeline pipeline = new Pipeline("_id", "_description", version, new CompoundProcessor(new CompoundProcessor(false, Collections.singletonList(processor1), Collections.singletonList(processor2)), processor3));
    SimulateDocumentResult actualItemResponse = executionService.executeDocument(pipeline, ingestDocument, true);
    assertThat(processor1.getInvokedCounter(), equalTo(1));
    assertThat(processor2.getInvokedCounter(), equalTo(1));
    assertThat(actualItemResponse, instanceOf(SimulateDocumentVerboseResult.class));
    SimulateDocumentVerboseResult simulateDocumentVerboseResult = (SimulateDocumentVerboseResult) actualItemResponse;
    assertThat(simulateDocumentVerboseResult.getProcessorResults().size(), equalTo(3));
    assertThat(simulateDocumentVerboseResult.getProcessorResults().get(0).getProcessorTag(), equalTo("processor_0"));
    assertThat(simulateDocumentVerboseResult.getProcessorResults().get(0).getIngestDocument(), nullValue());
    assertThat(simulateDocumentVerboseResult.getProcessorResults().get(0).getFailure(), instanceOf(RuntimeException.class));
    RuntimeException runtimeException = (RuntimeException) simulateDocumentVerboseResult.getProcessorResults().get(0).getFailure();
    assertThat(runtimeException.getMessage(), equalTo("processor failed"));
    assertThat(simulateDocumentVerboseResult.getProcessorResults().get(1).getProcessorTag(), equalTo("processor_1"));
    assertThat(simulateDocumentVerboseResult.getProcessorResults().get(1).getIngestDocument(), not(sameInstance(ingestDocument)));
    IngestDocument ingestDocumentWithOnFailureMetadata = new IngestDocument(ingestDocument);
    Map<String, Object> metadata = ingestDocumentWithOnFailureMetadata.getIngestMetadata();
    metadata.put(CompoundProcessor.ON_FAILURE_PROCESSOR_TYPE_FIELD, "mock");
    metadata.put(CompoundProcessor.ON_FAILURE_PROCESSOR_TAG_FIELD, "processor_0");
    metadata.put(CompoundProcessor.ON_FAILURE_MESSAGE_FIELD, "processor failed");
    assertIngestDocument(simulateDocumentVerboseResult.getProcessorResults().get(1).getIngestDocument(), ingestDocumentWithOnFailureMetadata);
    assertThat(simulateDocumentVerboseResult.getProcessorResults().get(1).getFailure(), nullValue());
    assertThat(simulateDocumentVerboseResult.getProcessorResults().get(2).getProcessorTag(), equalTo("processor_2"));
    assertThat(simulateDocumentVerboseResult.getProcessorResults().get(2).getIngestDocument(), not(sameInstance(ingestDocument)));
    assertIngestDocument(simulateDocumentVerboseResult.getProcessorResults().get(2).getIngestDocument(), ingestDocument);
    assertThat(simulateDocumentVerboseResult.getProcessorResults().get(2).getFailure(), nullValue());
}
Also used : CompoundProcessor(org.elasticsearch.ingest.CompoundProcessor) TestProcessor(org.elasticsearch.ingest.TestProcessor) IngestDocumentMatcher.assertIngestDocument(org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument) IngestDocument(org.elasticsearch.ingest.IngestDocument) Pipeline(org.elasticsearch.ingest.Pipeline)

Example 13 with CompoundProcessor

use of org.elasticsearch.ingest.CompoundProcessor in project elasticsearch by elastic.

the class SimulatePipelineRequestParsingTests method init.

@Before
public void init() throws IOException {
    TestProcessor processor = new TestProcessor(ingestDocument -> {
    });
    CompoundProcessor pipelineCompoundProcessor = new CompoundProcessor(processor);
    Pipeline pipeline = new Pipeline(SIMULATED_PIPELINE_ID, null, null, pipelineCompoundProcessor);
    Map<String, Processor.Factory> registry = Collections.singletonMap("mock_processor", (factories, tag, config) -> processor);
    store = mock(PipelineStore.class);
    when(store.get(SIMULATED_PIPELINE_ID)).thenReturn(pipeline);
    when(store.getProcessorFactories()).thenReturn(registry);
}
Also used : PipelineStore(org.elasticsearch.ingest.PipelineStore) CompoundProcessor(org.elasticsearch.ingest.CompoundProcessor) TestProcessor(org.elasticsearch.ingest.TestProcessor) Pipeline(org.elasticsearch.ingest.Pipeline) Before(org.junit.Before)

Example 14 with CompoundProcessor

use of org.elasticsearch.ingest.CompoundProcessor in project elasticsearch by elastic.

the class TrackingResultProcessorTests method testActualCompoundProcessorWithoutOnFailure.

public void testActualCompoundProcessorWithoutOnFailure() throws Exception {
    RuntimeException exception = new RuntimeException("processor failed");
    TestProcessor testProcessor = new TestProcessor(ingestDocument -> {
        throw exception;
    });
    CompoundProcessor actualProcessor = new CompoundProcessor(testProcessor);
    CompoundProcessor trackingProcessor = decorate(actualProcessor, resultList);
    try {
        trackingProcessor.execute(ingestDocument);
        fail("processor should throw exception");
    } catch (ElasticsearchException e) {
        assertThat(e.getRootCause().getMessage(), equalTo(exception.getMessage()));
    }
    SimulateProcessorResult expectedFirstResult = new SimulateProcessorResult(testProcessor.getTag(), ingestDocument);
    assertThat(testProcessor.getInvokedCounter(), equalTo(1));
    assertThat(resultList.size(), equalTo(1));
    assertThat(resultList.get(0).getIngestDocument(), nullValue());
    assertThat(resultList.get(0).getFailure(), equalTo(exception));
    assertThat(resultList.get(0).getProcessorTag(), equalTo(expectedFirstResult.getProcessorTag()));
}
Also used : CompoundProcessor(org.elasticsearch.ingest.CompoundProcessor) TestProcessor(org.elasticsearch.ingest.TestProcessor) ElasticsearchException(org.elasticsearch.ElasticsearchException)

Example 15 with CompoundProcessor

use of org.elasticsearch.ingest.CompoundProcessor in project elasticsearch by elastic.

the class ForEachProcessorTests method testModifyFieldsOutsideArray.

public void testModifyFieldsOutsideArray() throws Exception {
    List<Object> values = new ArrayList<>();
    values.add("string");
    values.add(1);
    values.add(null);
    IngestDocument ingestDocument = new IngestDocument("_index", "_type", "_id", null, null, Collections.singletonMap("values", values));
    TemplateService ts = TestTemplateService.instance();
    ForEachProcessor processor = new ForEachProcessor("_tag", "values", new CompoundProcessor(false, Collections.singletonList(new UppercaseProcessor("_tag_upper", "_ingest._value", false)), Collections.singletonList(new AppendProcessor("_tag", ts.compile("errors"), (model) -> (Collections.singletonList("added"))))));
    processor.execute(ingestDocument);
    List result = ingestDocument.getFieldValue("values", List.class);
    assertThat(result.get(0), equalTo("STRING"));
    assertThat(result.get(1), equalTo(1));
    assertThat(result.get(2), equalTo(null));
    List errors = ingestDocument.getFieldValue("errors", List.class);
    assertThat(errors.size(), equalTo(2));
}
Also used : Arrays(java.util.Arrays) Processor(org.elasticsearch.ingest.Processor) TestProcessor(org.elasticsearch.ingest.TestProcessor) HashMap(java.util.HashMap) CompoundProcessor(org.elasticsearch.ingest.CompoundProcessor) ArrayList(java.util.ArrayList) List(java.util.List) TemplateService(org.elasticsearch.ingest.TemplateService) Locale(java.util.Locale) Map(java.util.Map) Matchers.equalTo(org.hamcrest.Matchers.equalTo) TestTemplateService(org.elasticsearch.ingest.TestTemplateService) ESTestCase(org.elasticsearch.test.ESTestCase) Collections(java.util.Collections) IngestDocument(org.elasticsearch.ingest.IngestDocument) ArrayList(java.util.ArrayList) CompoundProcessor(org.elasticsearch.ingest.CompoundProcessor) IngestDocument(org.elasticsearch.ingest.IngestDocument) ArrayList(java.util.ArrayList) List(java.util.List) TemplateService(org.elasticsearch.ingest.TemplateService) TestTemplateService(org.elasticsearch.ingest.TestTemplateService)

Aggregations

CompoundProcessor (org.elasticsearch.ingest.CompoundProcessor)15 TestProcessor (org.elasticsearch.ingest.TestProcessor)13 Pipeline (org.elasticsearch.ingest.Pipeline)8 ArrayList (java.util.ArrayList)4 IngestDocument (org.elasticsearch.ingest.IngestDocument)4 Processor (org.elasticsearch.ingest.Processor)3 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Locale (java.util.Locale)2 Map (java.util.Map)2 ElasticsearchException (org.elasticsearch.ElasticsearchException)2 IngestDocumentMatcher.assertIngestDocument (org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument)2 TemplateService (org.elasticsearch.ingest.TemplateService)2 TestTemplateService (org.elasticsearch.ingest.TestTemplateService)2 ESTestCase (org.elasticsearch.test.ESTestCase)2 Matchers.equalTo (org.hamcrest.Matchers.equalTo)2 PipelineStore (org.elasticsearch.ingest.PipelineStore)1 Before (org.junit.Before)1