Search in sources :

Example 6 with BulkMessageWriter

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

the class BulkMessageWriterBoltTest method testFlushOnBatchSize.

@Test
public void testFlushOnBatchSize() throws Exception {
    Map stormConf = new HashMap();
    bulkMessageWriterBolt.setCuratorFramework(client);
    bulkMessageWriterBolt.setZKCache(cache);
    bulkMessageWriterBolt.getConfigurations().updateSensorIndexingConfig(BaseEnrichmentBoltTest.sensorType, new FileInputStream("../" + BaseEnrichmentBoltTest.sampleSensorIndexingConfigPath));
    {
        doThrow(new Exception()).when(bulkMessageWriter).init(eq(stormConf), any(WriterConfiguration.class));
        assertThrows(RuntimeException.class, () -> bulkMessageWriterBolt.prepare(stormConf, topologyContext, outputCollector));
        reset(bulkMessageWriter);
    }
    {
        when(bulkMessageWriter.getName()).thenReturn("hdfs");
        bulkMessageWriterBolt.prepare(stormConf, topologyContext, outputCollector);
        verify(bulkMessageWriter, times(1)).init(eq(stormConf), any(WriterConfiguration.class));
    }
    {
        for (int i = 0; i < 4; i++) {
            bulkMessageWriterBolt.execute(tupleList.get(i));
            verify(bulkMessageWriter, times(0)).write(eq(BaseEnrichmentBoltTest.sensorType), any(WriterConfiguration.class), anyList());
        }
        BulkWriterResponse response = new BulkWriterResponse();
        response.addAllSuccesses(messageIdList);
        when(bulkMessageWriter.write(eq(BaseEnrichmentBoltTest.sensorType), any(WriterConfiguration.class), eq(messageList))).thenReturn(response);
        bulkMessageWriterBolt.execute(tupleList.get(4));
        verify(bulkMessageWriter, times(1)).write(eq(BaseEnrichmentBoltTest.sensorType), any(WriterConfiguration.class), eq(messageList));
        tupleList.forEach(tuple -> verify(outputCollector, times(1)).ack(tuple));
        reset(outputCollector);
    }
    {
        doThrow(new Exception()).when(bulkMessageWriter).write(eq(BaseEnrichmentBoltTest.sensorType), any(WriterConfiguration.class), anyList());
        UnitTestHelper.setLog4jLevel(BulkWriterComponent.class, Level.FATAL);
        for (int i = 0; i < 5; i++) {
            bulkMessageWriterBolt.execute(tupleList.get(i));
        }
        UnitTestHelper.setLog4jLevel(BulkWriterComponent.class, Level.ERROR);
        tupleList.forEach(tuple -> verify(outputCollector, times(1)).ack(tuple));
        verify(outputCollector, times(5)).emit(eq(Constants.ERROR_STREAM), any(Values.class));
        verify(outputCollector, times(1)).reportError(any(Throwable.class));
    }
    Mockito.verifyNoMoreInteractions(outputCollector);
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) BeforeEach(org.junit.jupiter.api.BeforeEach) Mock(org.mockito.Mock) UnitTestHelper(org.apache.metron.test.utils.UnitTestHelper) HashMap(java.util.HashMap) MessageId(org.apache.metron.common.writer.MessageId) Mockito.argThat(org.mockito.Mockito.argThat) Mockito.spy(org.mockito.Mockito.spy) WriterConfiguration(org.apache.metron.common.configuration.writer.WriterConfiguration) ArrayList(java.util.ArrayList) MessageGetters(org.apache.metron.storm.common.message.MessageGetters) Mockito.doThrow(org.mockito.Mockito.doThrow) MockitoAnnotations(org.mockito.MockitoAnnotations) BaseEnrichmentBoltTest(org.apache.metron.test.bolt.BaseEnrichmentBoltTest) Values(org.apache.storm.tuple.Values) Tuple(org.apache.storm.tuple.Tuple) ParseException(org.json.simple.parser.ParseException) BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse) Map(java.util.Map) Level(org.apache.log4j.Level) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) BulkMessage(org.apache.metron.common.writer.BulkMessage) BulkMessageWriter(org.apache.metron.common.writer.BulkMessageWriter) FakeClock(org.apache.metron.common.system.FakeClock) BulkWriterComponent(org.apache.metron.writer.BulkWriterComponent) JSONParser(org.json.simple.parser.JSONParser) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) FileInputStream(java.io.FileInputStream) Constants(org.apache.metron.common.Constants) IndexingConfigurations(org.apache.metron.common.configuration.IndexingConfigurations) StandardCharsets(java.nio.charset.StandardCharsets) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) List(java.util.List) JSONObject(org.json.simple.JSONObject) Mockito.anyList(org.mockito.Mockito.anyList) Multiline(org.adrianwalker.multilinestring.Multiline) Mockito.any(org.mockito.Mockito.any) Mockito.reset(org.mockito.Mockito.reset) Mockito.eq(org.mockito.Mockito.eq) Mockito.mock(org.mockito.Mockito.mock) BulkWriterComponent(org.apache.metron.writer.BulkWriterComponent) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) FileInputStream(java.io.FileInputStream) ParseException(org.json.simple.parser.ParseException) BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse) BaseEnrichmentBoltTest(org.apache.metron.test.bolt.BaseEnrichmentBoltTest) Test(org.junit.jupiter.api.Test)

Aggregations

HashMap (java.util.HashMap)6 BulkMessageWriter (org.apache.metron.common.writer.BulkMessageWriter)6 JSONObject (org.json.simple.JSONObject)6 Test (org.junit.jupiter.api.Test)6 FileInputStream (java.io.FileInputStream)3 StandardCharsets (java.nio.charset.StandardCharsets)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 Multiline (org.adrianwalker.multilinestring.Multiline)3 Level (org.apache.log4j.Level)3 Constants (org.apache.metron.common.Constants)3 IndexingConfigurations (org.apache.metron.common.configuration.IndexingConfigurations)3 WriterConfiguration (org.apache.metron.common.configuration.writer.WriterConfiguration)3 FakeClock (org.apache.metron.common.system.FakeClock)3 BulkMessage (org.apache.metron.common.writer.BulkMessage)3 BulkWriterResponse (org.apache.metron.common.writer.BulkWriterResponse)3 MessageId (org.apache.metron.common.writer.MessageId)3 MessageGetters (org.apache.metron.storm.common.message.MessageGetters)3 BaseEnrichmentBoltTest (org.apache.metron.test.bolt.BaseEnrichmentBoltTest)3