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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations