Search in sources :

Example 1 with CompoundProcessor

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

the class ForEachProcessorTests method testExecuteWithFailure.

public void testExecuteWithFailure() throws Exception {
    IngestDocument ingestDocument = new IngestDocument("_index", "_type", "_id", null, null, Collections.singletonMap("values", Arrays.asList("a", "b", "c")));
    TestProcessor testProcessor = new TestProcessor(id -> {
        if ("c".equals(id.getFieldValue("_ingest._value", String.class))) {
            throw new RuntimeException("failure");
        }
    });
    ForEachProcessor processor = new ForEachProcessor("_tag", "values", testProcessor);
    try {
        processor.execute(ingestDocument);
        fail("exception expected");
    } catch (RuntimeException e) {
        assertThat(e.getMessage(), equalTo("failure"));
    }
    assertThat(testProcessor.getInvokedCounter(), equalTo(3));
    assertThat(ingestDocument.getFieldValue("values", List.class), equalTo(Arrays.asList("a", "b", "c")));
    testProcessor = new TestProcessor(id -> {
        String value = id.getFieldValue("_ingest._value", String.class);
        if ("c".equals(value)) {
            throw new RuntimeException("failure");
        } else {
            id.setFieldValue("_ingest._value", value.toUpperCase(Locale.ROOT));
        }
    });
    Processor onFailureProcessor = new TestProcessor(ingestDocument1 -> {
    });
    processor = new ForEachProcessor("_tag", "values", new CompoundProcessor(false, Arrays.asList(testProcessor), Arrays.asList(onFailureProcessor)));
    processor.execute(ingestDocument);
    assertThat(testProcessor.getInvokedCounter(), equalTo(3));
    assertThat(ingestDocument.getFieldValue("values", List.class), equalTo(Arrays.asList("A", "B", "c")));
}
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) Processor(org.elasticsearch.ingest.Processor) TestProcessor(org.elasticsearch.ingest.TestProcessor) CompoundProcessor(org.elasticsearch.ingest.CompoundProcessor) CompoundProcessor(org.elasticsearch.ingest.CompoundProcessor) IngestDocument(org.elasticsearch.ingest.IngestDocument) TestProcessor(org.elasticsearch.ingest.TestProcessor) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with CompoundProcessor

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

the class TrackingResultProcessor method decorate.

public static CompoundProcessor decorate(CompoundProcessor compoundProcessor, List<SimulateProcessorResult> processorResultList) {
    List<Processor> processors = new ArrayList<>(compoundProcessor.getProcessors().size());
    for (Processor processor : compoundProcessor.getProcessors()) {
        if (processor instanceof CompoundProcessor) {
            processors.add(decorate((CompoundProcessor) processor, processorResultList));
        } else {
            processors.add(new TrackingResultProcessor(compoundProcessor.isIgnoreFailure(), processor, processorResultList));
        }
    }
    List<Processor> onFailureProcessors = new ArrayList<>(compoundProcessor.getProcessors().size());
    for (Processor processor : compoundProcessor.getOnFailureProcessors()) {
        if (processor instanceof CompoundProcessor) {
            onFailureProcessors.add(decorate((CompoundProcessor) processor, processorResultList));
        } else {
            onFailureProcessors.add(new TrackingResultProcessor(compoundProcessor.isIgnoreFailure(), processor, processorResultList));
        }
    }
    return new CompoundProcessor(compoundProcessor.isIgnoreFailure(), processors, onFailureProcessors);
}
Also used : Processor(org.elasticsearch.ingest.Processor) CompoundProcessor(org.elasticsearch.ingest.CompoundProcessor) ArrayList(java.util.ArrayList) CompoundProcessor(org.elasticsearch.ingest.CompoundProcessor)

Example 3 with CompoundProcessor

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

the class TrackingResultProcessorTests method testActualCompoundProcessorWithIgnoreFailure.

public void testActualCompoundProcessorWithIgnoreFailure() throws Exception {
    RuntimeException exception = new RuntimeException("processor failed");
    TestProcessor testProcessor = new TestProcessor(ingestDocument -> {
        throw exception;
    });
    CompoundProcessor actualProcessor = new CompoundProcessor(true, Collections.singletonList(testProcessor), Collections.emptyList());
    CompoundProcessor trackingProcessor = decorate(actualProcessor, resultList);
    trackingProcessor.execute(ingestDocument);
    SimulateProcessorResult expectedResult = new SimulateProcessorResult(testProcessor.getTag(), ingestDocument);
    assertThat(testProcessor.getInvokedCounter(), equalTo(1));
    assertThat(resultList.size(), equalTo(1));
    assertThat(resultList.get(0).getIngestDocument(), equalTo(expectedResult.getIngestDocument()));
    assertThat(resultList.get(0).getFailure(), sameInstance(exception));
    assertThat(resultList.get(0).getProcessorTag(), equalTo(expectedResult.getProcessorTag()));
}
Also used : CompoundProcessor(org.elasticsearch.ingest.CompoundProcessor) TestProcessor(org.elasticsearch.ingest.TestProcessor)

Example 4 with CompoundProcessor

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

the class TrackingResultProcessorTests method testActualCompoundProcessorWithOnFailure.

public void testActualCompoundProcessorWithOnFailure() throws Exception {
    RuntimeException exception = new RuntimeException("fail");
    TestProcessor failProcessor = new TestProcessor("fail", "test", ingestDocument -> {
        throw exception;
    });
    TestProcessor onFailureProcessor = new TestProcessor("success", "test", ingestDocument -> {
    });
    CompoundProcessor actualProcessor = new CompoundProcessor(false, Arrays.asList(new CompoundProcessor(false, Arrays.asList(failProcessor, onFailureProcessor), Arrays.asList(onFailureProcessor, failProcessor))), Arrays.asList(onFailureProcessor));
    CompoundProcessor trackingProcessor = decorate(actualProcessor, resultList);
    trackingProcessor.execute(ingestDocument);
    SimulateProcessorResult expectedFailResult = new SimulateProcessorResult(failProcessor.getTag(), ingestDocument);
    SimulateProcessorResult expectedSuccessResult = new SimulateProcessorResult(onFailureProcessor.getTag(), ingestDocument);
    assertThat(failProcessor.getInvokedCounter(), equalTo(2));
    assertThat(onFailureProcessor.getInvokedCounter(), equalTo(2));
    assertThat(resultList.size(), equalTo(4));
    assertThat(resultList.get(0).getIngestDocument(), nullValue());
    assertThat(resultList.get(0).getFailure(), equalTo(exception));
    assertThat(resultList.get(0).getProcessorTag(), equalTo(expectedFailResult.getProcessorTag()));
    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()));
    assertThat(resultList.get(2).getIngestDocument(), nullValue());
    assertThat(resultList.get(2).getFailure(), equalTo(exception));
    assertThat(resultList.get(2).getProcessorTag(), equalTo(expectedFailResult.getProcessorTag()));
    metadata = resultList.get(3).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(3).getFailure(), nullValue());
    assertThat(resultList.get(3).getProcessorTag(), equalTo(expectedSuccessResult.getProcessorTag()));
}
Also used : CompoundProcessor(org.elasticsearch.ingest.CompoundProcessor) TestProcessor(org.elasticsearch.ingest.TestProcessor)

Example 5 with CompoundProcessor

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

the class SimulateExecutionServiceTests method testExecuteItem.

public void testExecuteItem() throws Exception {
    TestProcessor processor = new TestProcessor("processor_0", "mock", ingestDocument -> {
    });
    Pipeline pipeline = new Pipeline("_id", "_description", version, new CompoundProcessor(processor, processor));
    SimulateDocumentResult actualItemResponse = executionService.executeDocument(pipeline, ingestDocument, false);
    assertThat(processor.getInvokedCounter(), equalTo(2));
    assertThat(actualItemResponse, instanceOf(SimulateDocumentBaseResult.class));
    SimulateDocumentBaseResult simulateDocumentBaseResult = (SimulateDocumentBaseResult) actualItemResponse;
    assertThat(simulateDocumentBaseResult.getIngestDocument(), equalTo(ingestDocument));
    assertThat(simulateDocumentBaseResult.getFailure(), nullValue());
}
Also used : CompoundProcessor(org.elasticsearch.ingest.CompoundProcessor) TestProcessor(org.elasticsearch.ingest.TestProcessor) Pipeline(org.elasticsearch.ingest.Pipeline)

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