Search in sources :

Example 1 with FakeClock

use of org.apache.metron.common.system.FakeClock in project metron by apache.

the class BulkMessageWriterBoltTest method testFlushOnTickTuple.

@Test
public void testFlushOnTickTuple() throws Exception {
    FakeClock clock = new FakeClock();
    BulkMessageWriterBolt bulkMessageWriterBolt = new BulkMessageWriterBolt("zookeeperUrl").withBulkMessageWriter(bulkMessageWriter).withMessageGetter(MessageGetters.JSON_FROM_FIELD.name()).withMessageGetterField("message");
    bulkMessageWriterBolt.setCuratorFramework(client);
    bulkMessageWriterBolt.setZKCache(cache);
    bulkMessageWriterBolt.getConfigurations().updateSensorIndexingConfig(sensorType, new FileInputStream(sampleSensorIndexingConfigPath));
    bulkMessageWriterBolt.declareOutputFields(declarer);
    verify(declarer, times(1)).declareStream(eq("error"), argThat(new FieldsMatcher("message")));
    Map stormConf = new HashMap();
    when(bulkMessageWriter.getName()).thenReturn("elasticsearch");
    bulkMessageWriterBolt.prepare(stormConf, topologyContext, outputCollector, clock);
    verify(bulkMessageWriter, times(1)).init(eq(stormConf), any(TopologyContext.class), any(WriterConfiguration.class));
    int batchTimeout = bulkMessageWriterBolt.getDefaultBatchTimeout();
    assertEquals(14, batchTimeout);
    tupleList = new ArrayList<>();
    messageList = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        when(tuple.getValueByField("message")).thenReturn(fullMessageList.get(i));
        tupleList.add(tuple);
        messageList.add(fullMessageList.get(i));
        bulkMessageWriterBolt.execute(tuple);
        verify(bulkMessageWriter, times(0)).write(eq(sensorType), any(WriterConfiguration.class), eq(tupleList), eq(messageList));
    }
    when(tuple.getValueByField("message")).thenReturn(null);
    // mark the tuple as a TickTuple, part 1 of 2
    when(tuple.getSourceComponent()).thenReturn("__system");
    // mark the tuple as a TickTuple, part 2 of 2
    when(tuple.getSourceStreamId()).thenReturn("__tick");
    BulkWriterResponse response = new BulkWriterResponse();
    response.addAllSuccesses(tupleList);
    when(bulkMessageWriter.write(eq(sensorType), any(WriterConfiguration.class), eq(tupleList), argThat(new MessageListMatcher(messageList)))).thenReturn(response);
    clock.advanceToSeconds(2);
    bulkMessageWriterBolt.execute(tuple);
    verify(bulkMessageWriter, times(0)).write(eq(sensorType), any(WriterConfiguration.class), eq(tupleList), argThat(new MessageListMatcher(messageList)));
    // 1 tick
    verify(outputCollector, times(1)).ack(tuple);
    clock.advanceToSeconds(9);
    bulkMessageWriterBolt.execute(tuple);
    verify(bulkMessageWriter, times(1)).write(eq(sensorType), any(WriterConfiguration.class), eq(tupleList), argThat(new MessageListMatcher(messageList)));
    assertEquals(3, tupleList.size());
    // 3 messages + 2nd tick
    verify(outputCollector, times(5)).ack(tuple);
}
Also used : BulkMessageWriterBolt(org.apache.metron.writer.bolt.BulkMessageWriterBolt) HashMap(java.util.HashMap) FakeClock(org.apache.metron.common.system.FakeClock) WriterConfiguration(org.apache.metron.common.configuration.writer.WriterConfiguration) TopologyContext(org.apache.storm.task.TopologyContext) HashMap(java.util.HashMap) Map(java.util.Map) FileInputStream(java.io.FileInputStream) BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse) BaseEnrichmentBoltTest(org.apache.metron.test.bolt.BaseEnrichmentBoltTest) Test(org.junit.Test)

Example 2 with FakeClock

use of org.apache.metron.common.system.FakeClock in project metron by apache.

the class BulkMessageWriterBoltTest method testFlushOnBatchTimeout.

@Test
public void testFlushOnBatchTimeout() throws Exception {
    FakeClock clock = new FakeClock();
    BulkMessageWriterBolt bulkMessageWriterBolt = new BulkMessageWriterBolt("zookeeperUrl").withBulkMessageWriter(bulkMessageWriter).withMessageGetter(MessageGetters.JSON_FROM_FIELD.name()).withMessageGetterField("message").withBatchTimeoutDivisor(3);
    bulkMessageWriterBolt.setCuratorFramework(client);
    bulkMessageWriterBolt.setZKCache(cache);
    bulkMessageWriterBolt.getConfigurations().updateSensorIndexingConfig(sensorType, new FileInputStream(sampleSensorIndexingConfigPath));
    bulkMessageWriterBolt.declareOutputFields(declarer);
    verify(declarer, times(1)).declareStream(eq("error"), argThat(new FieldsMatcher("message")));
    Map stormConf = new HashMap();
    when(bulkMessageWriter.getName()).thenReturn("elasticsearch");
    bulkMessageWriterBolt.prepare(stormConf, topologyContext, outputCollector, clock);
    verify(bulkMessageWriter, times(1)).init(eq(stormConf), any(TopologyContext.class), any(WriterConfiguration.class));
    int batchTimeout = bulkMessageWriterBolt.getDefaultBatchTimeout();
    assertEquals(4, batchTimeout);
    tupleList = new ArrayList<>();
    messageList = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        when(tuple.getValueByField("message")).thenReturn(fullMessageList.get(i));
        tupleList.add(tuple);
        messageList.add(fullMessageList.get(i));
        bulkMessageWriterBolt.execute(tuple);
        verify(bulkMessageWriter, times(0)).write(eq(sensorType), any(WriterConfiguration.class), eq(tupleList), eq(messageList));
    }
    clock.elapseSeconds(5);
    when(tuple.getValueByField("message")).thenReturn(fullMessageList.get(3));
    tupleList.add(tuple);
    messageList.add(fullMessageList.get(3));
    BulkWriterResponse response = new BulkWriterResponse();
    response.addAllSuccesses(tupleList);
    when(bulkMessageWriter.write(eq(sensorType), any(WriterConfiguration.class), eq(tupleList), argThat(new MessageListMatcher(messageList)))).thenReturn(response);
    bulkMessageWriterBolt.execute(tuple);
    verify(bulkMessageWriter, times(1)).write(eq(sensorType), any(WriterConfiguration.class), eq(tupleList), argThat(new MessageListMatcher(messageList)));
    verify(outputCollector, times(4)).ack(tuple);
}
Also used : BulkMessageWriterBolt(org.apache.metron.writer.bolt.BulkMessageWriterBolt) HashMap(java.util.HashMap) FakeClock(org.apache.metron.common.system.FakeClock) WriterConfiguration(org.apache.metron.common.configuration.writer.WriterConfiguration) TopologyContext(org.apache.storm.task.TopologyContext) HashMap(java.util.HashMap) Map(java.util.Map) FileInputStream(java.io.FileInputStream) BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse) BaseEnrichmentBoltTest(org.apache.metron.test.bolt.BaseEnrichmentBoltTest) Test(org.junit.Test)

Example 3 with FakeClock

use of org.apache.metron.common.system.FakeClock in project metron by apache.

the class BulkMessageWriterBoltTest method testFlushOnBatchTimeout.

@Test
public void testFlushOnBatchTimeout() throws Exception {
    FakeClock clock = new FakeClock();
    bulkMessageWriterBolt = bulkMessageWriterBolt.withBatchTimeoutDivisor(3);
    bulkMessageWriterBolt.setCuratorFramework(client);
    bulkMessageWriterBolt.setZKCache(cache);
    bulkMessageWriterBolt.getConfigurations().updateSensorIndexingConfig(BaseEnrichmentBoltTest.sensorType, new FileInputStream("../" + BaseEnrichmentBoltTest.sampleSensorIndexingConfigPath));
    {
        bulkMessageWriterBolt.declareOutputFields(declarer);
        verify(declarer, times(1)).declareStream(eq("error"), argThat(new FieldsMatcher("message")));
    }
    {
        Map stormConf = new HashMap();
        when(bulkMessageWriter.getName()).thenReturn("elasticsearch");
        bulkMessageWriterBolt.prepare(stormConf, topologyContext, outputCollector, clock);
        verify(bulkMessageWriter, times(1)).init(eq(stormConf), any(WriterConfiguration.class));
    }
    {
        int batchTimeout = bulkMessageWriterBolt.getMaxBatchTimeout();
        assertEquals(4, batchTimeout);
        for (int i = 0; i < 4; i++) {
            bulkMessageWriterBolt.execute(tupleList.get(i));
            verify(bulkMessageWriter, times(0)).write(eq(BaseEnrichmentBoltTest.sensorType), any(WriterConfiguration.class), any(List.class));
        }
        clock.elapseSeconds(5);
        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));
    }
    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) HashMap(java.util.HashMap) FakeClock(org.apache.metron.common.system.FakeClock) HashMap(java.util.HashMap) Map(java.util.Map) FileInputStream(java.io.FileInputStream) BulkWriterResponse(org.apache.metron.common.writer.BulkWriterResponse) BaseEnrichmentBoltTest(org.apache.metron.test.bolt.BaseEnrichmentBoltTest) Test(org.junit.jupiter.api.Test)

Example 4 with FakeClock

use of org.apache.metron.common.system.FakeClock in project metron by apache.

the class AlertsUIServiceImplTest method setUp.

@BeforeEach
public void setUp() {
    kafkaService = mock(KafkaService.class);
    environment = mock(Environment.class);
    userSettingsClient = mock(UserSettingsClient.class);
    alertsUIService = new AlertsUIServiceImpl(kafkaService, environment, userSettingsClient);
    // use a fake clock for testing
    clock = new FakeClock();
    clock.elapseSeconds(1000);
    alertsUIService.setClock(clock);
    // assume user1 is logged in for tests
    Authentication authentication = Mockito.mock(Authentication.class);
    UserDetails userDetails = Mockito.mock(UserDetails.class);
    when(authentication.getPrincipal()).thenReturn(userDetails);
    when(userDetails.getUsername()).thenReturn(user1);
    SecurityContextHolder.getContext().setAuthentication(authentication);
}
Also used : KafkaService(org.apache.metron.rest.service.KafkaService) UserDetails(org.springframework.security.core.userdetails.UserDetails) UserSettingsClient(org.apache.metron.rest.user.UserSettingsClient) FakeClock(org.apache.metron.common.system.FakeClock) Authentication(org.springframework.security.core.Authentication) Environment(org.springframework.core.env.Environment) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with FakeClock

use of org.apache.metron.common.system.FakeClock in project metron by apache.

the class BulkMessageWriterBoltTest method testMessageInvalid.

/**
 * If an invalid message is sent to indexing, the message should be handled as an error
 * and the topology should continue processing.
 */
@Test
public void testMessageInvalid() throws Exception {
    FakeClock clock = new FakeClock();
    // setup the bolt
    BulkMessageWriterBolt<IndexingConfigurations> bolt = new BulkMessageWriterBolt<IndexingConfigurations>("zookeeperUrl", "INDEXING").withBulkMessageWriter(bulkMessageWriter).withMessageGetter(MessageGetters.JSON_FROM_POSITION.name()).withMessageGetterField("message");
    bolt.setCuratorFramework(client);
    bolt.setZKCache(cache);
    bolt.getConfigurations().updateSensorIndexingConfig(BaseEnrichmentBoltTest.sensorType, new FileInputStream("../" + BaseEnrichmentBoltTest.sampleSensorIndexingConfigPath));
    // initialize the bolt
    bolt.declareOutputFields(declarer);
    Map stormConf = new HashMap();
    bolt.prepare(stormConf, topologyContext, outputCollector, clock);
    // execute a tuple that contains an invalid message
    byte[] invalidJSON = "this is not valid JSON".getBytes(StandardCharsets.UTF_8);
    when(tuple.getBinary(0)).thenReturn(invalidJSON);
    bolt.execute(tuple);
    // the tuple should be handled as an error and ack'd
    verify(outputCollector, times(1)).emit(eq(Constants.ERROR_STREAM), any());
    verify(outputCollector, times(1)).ack(tuple);
    verify(outputCollector, times(1)).reportError(any(Throwable.class));
    Mockito.verifyNoMoreInteractions(outputCollector);
}
Also used : HashMap(java.util.HashMap) FakeClock(org.apache.metron.common.system.FakeClock) IndexingConfigurations(org.apache.metron.common.configuration.IndexingConfigurations) HashMap(java.util.HashMap) Map(java.util.Map) FileInputStream(java.io.FileInputStream) BaseEnrichmentBoltTest(org.apache.metron.test.bolt.BaseEnrichmentBoltTest) Test(org.junit.jupiter.api.Test)

Aggregations

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