Search in sources :

Example 11 with BulkWriterResponse

use of org.apache.metron.common.writer.BulkWriterResponse in project metron by apache.

the class BulkWriterComponentTest method writeShouldThrowExceptionWhenHandleErrorIsFalse.

@Test
public void writeShouldThrowExceptionWhenHandleErrorIsFalse() throws Exception {
    exception.expect(IllegalStateException.class);
    Throwable e = new Exception("test exception");
    BulkWriterResponse response = new BulkWriterResponse();
    response.addAllErrors(e, tupleList);
    when(bulkMessageWriter.write(sensorType, configurations, Arrays.asList(tuple1, tuple2), Arrays.asList(message1, message2))).thenReturn(response);
    BulkWriterComponent<JSONObject> bulkWriterComponent = new BulkWriterComponent<>(collector, true, false);
    bulkWriterComponent.write(sensorType, tuple1, message1, bulkMessageWriter, configurations, messageGetStrategy);
    bulkWriterComponent.write(sensorType, tuple2, message2, bulkMessageWriter, configurations, messageGetStrategy);
}
Also used : JSONObject(org.json.simple.JSONObject) ExpectedException(org.junit.rules.ExpectedException) BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 12 with BulkWriterResponse

use of org.apache.metron.common.writer.BulkWriterResponse in project metron by apache.

the class ElasticsearchWriter method buildWriteReponse.

protected BulkWriterResponse buildWriteReponse(Iterable<Tuple> tuples, BulkResponse bulkResponse) throws Exception {
    // Elasticsearch responses are in the same order as the request, giving us an implicit mapping with Tuples
    BulkWriterResponse writerResponse = new BulkWriterResponse();
    if (bulkResponse.hasFailures()) {
        Iterator<BulkItemResponse> respIter = bulkResponse.iterator();
        Iterator<Tuple> tupleIter = tuples.iterator();
        while (respIter.hasNext() && tupleIter.hasNext()) {
            BulkItemResponse item = respIter.next();
            Tuple tuple = tupleIter.next();
            if (item.isFailed()) {
                writerResponse.addError(item.getFailure().getCause(), tuple);
            } else {
                writerResponse.addSuccess(tuple);
            }
            // Should never happen, so fail the entire batch if it does.
            if (respIter.hasNext() != tupleIter.hasNext()) {
                throw new Exception(bulkResponse.buildFailureMessage());
            }
        }
    } else {
        writerResponse.addAllSuccesses(tuples);
    }
    return writerResponse;
}
Also used : BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse) Tuple(org.apache.storm.tuple.Tuple)

Example 13 with BulkWriterResponse

use of org.apache.metron.common.writer.BulkWriterResponse in project metron by apache.

the class ElasticsearchWriterTest method testSuccessAndFailure.

@Test
public void testSuccessAndFailure() throws Exception {
    Tuple tuple1 = mock(Tuple.class);
    Tuple tuple2 = mock(Tuple.class);
    BulkResponse response = mock(BulkResponse.class);
    when(response.hasFailures()).thenReturn(true);
    Exception e = new IllegalStateException("Cause");
    BulkItemResponse itemResponse = buildBulkItemFailure(e);
    BulkItemResponse itemResponse2 = mock(BulkItemResponse.class);
    when(itemResponse2.isFailed()).thenReturn(false);
    when(response.iterator()).thenReturn(ImmutableList.of(itemResponse, itemResponse2).iterator());
    BulkWriterResponse expected = new BulkWriterResponse();
    expected.addError(e, tuple1);
    expected.addSuccess(tuple2);
    ElasticsearchWriter esWriter = new ElasticsearchWriter();
    BulkWriterResponse actual = esWriter.buildWriteReponse(ImmutableList.of(tuple1, tuple2), response);
    assertEquals("Response should have one error and one success", expected, actual);
}
Also used : BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) Tuple(org.apache.storm.tuple.Tuple) BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse) Test(org.junit.Test)

Example 14 with BulkWriterResponse

use of org.apache.metron.common.writer.BulkWriterResponse in project metron by apache.

the class ElasticsearchWriterTest method testSingleSuccesses.

@Test
public void testSingleSuccesses() throws Exception {
    Tuple tuple1 = mock(Tuple.class);
    BulkResponse response = mock(BulkResponse.class);
    when(response.hasFailures()).thenReturn(false);
    BulkWriterResponse expected = new BulkWriterResponse();
    expected.addSuccess(tuple1);
    ElasticsearchWriter esWriter = new ElasticsearchWriter();
    BulkWriterResponse actual = esWriter.buildWriteReponse(ImmutableList.of(tuple1), response);
    assertEquals("Response should have no errors and single success", expected, actual);
}
Also used : BulkResponse(org.elasticsearch.action.bulk.BulkResponse) Tuple(org.apache.storm.tuple.Tuple) BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse) Test(org.junit.Test)

Example 15 with BulkWriterResponse

use of org.apache.metron.common.writer.BulkWriterResponse in project metron by apache.

the class ElasticsearchWriterTest method testMultipleSuccesses.

@Test
public void testMultipleSuccesses() throws Exception {
    Tuple tuple1 = mock(Tuple.class);
    Tuple tuple2 = mock(Tuple.class);
    BulkResponse response = mock(BulkResponse.class);
    when(response.hasFailures()).thenReturn(false);
    BulkWriterResponse expected = new BulkWriterResponse();
    expected.addSuccess(tuple1);
    expected.addSuccess(tuple2);
    ElasticsearchWriter esWriter = new ElasticsearchWriter();
    BulkWriterResponse actual = esWriter.buildWriteReponse(ImmutableList.of(tuple1, tuple2), response);
    assertEquals("Response should have no errors and two successes", expected, actual);
}
Also used : BulkResponse(org.elasticsearch.action.bulk.BulkResponse) Tuple(org.apache.storm.tuple.Tuple) BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse) Test(org.junit.Test)

Aggregations

BulkWriterResponse (org.apache.metron.common.writer.BulkWriterResponse)27 Test (org.junit.Test)20 JSONObject (org.json.simple.JSONObject)15 Tuple (org.apache.storm.tuple.Tuple)13 HashMap (java.util.HashMap)8 ParserConfigurations (org.apache.metron.common.configuration.ParserConfigurations)7 BaseBoltTest (org.apache.metron.test.bolt.BaseBoltTest)7 WriterConfiguration (org.apache.metron.common.configuration.writer.WriterConfiguration)6 TopologyContext (org.apache.storm.task.TopologyContext)6 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)6 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 FileInputStream (java.io.FileInputStream)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 ParserWriterConfiguration (org.apache.metron.common.configuration.writer.ParserWriterConfiguration)3 MetronError (org.apache.metron.common.error.MetronError)3 Context (org.apache.metron.stellar.dsl.Context)3 BaseEnrichmentBoltTest (org.apache.metron.test.bolt.BaseEnrichmentBoltTest)3 BulkMessageWriterBolt (org.apache.metron.writer.bolt.BulkMessageWriterBolt)3