Search in sources :

Example 11 with RawMessage

use of org.apache.metron.common.message.metadata.RawMessage in project metron by apache.

the class ParserBoltTest method shouldExecuteOnSuccessWithMultipleMessages.

@Test
public void shouldExecuteOnSuccessWithMultipleMessages() throws Exception {
    when(messageGetStrategy.get(t1)).thenReturn("originalMessage".getBytes(StandardCharsets.UTF_8));
    when(t1.getStringByField(FieldsConfiguration.TOPIC.getFieldName())).thenReturn("yafTopic");
    MockParserRunner mockParserRunner = new MockParserRunner(new HashSet<String>() {

        {
            add("yaf");
        }
    });
    ParserConfigurations parserConfigurations = new ParserConfigurations();
    parserConfigurations.updateSensorParserConfig("yaf", new SensorParserConfig());
    ParserBolt parserBolt = spy(new ParserBolt("zookeeperUrl", mockParserRunner, new HashMap<String, WriterHandler>() {

        {
            put("yaf", writerHandler);
        }
    }) {

        @Override
        public ParserConfigurations getConfigurations() {
            return parserConfigurations;
        }
    });
    parserBolt.setMessageGetStrategy(messageGetStrategy);
    parserBolt.setOutputCollector(outputCollector);
    parserBolt.setTopicToSensorMap(new HashMap<String, String>() {

        {
            put("yafTopic", "yaf");
        }
    });
    parserBolt.setAckTuplesPolicy(bulkWriterResponseHandler);
    List<BulkMessage<JSONObject>> messages = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        String messageId = String.format("messageId%s", i + 1);
        JSONObject message = new JSONObject();
        message.put(Constants.GUID, messageId);
        message.put("field", String.format("value%s", i + 1));
        messages.add(new BulkMessage<>(messageId, message));
    }
    mockParserRunner.setMessages(messages.stream().map(BulkMessage::getMessage).collect(Collectors.toList()));
    RawMessage expectedRawMessage = new RawMessage("originalMessage".getBytes(StandardCharsets.UTF_8), new HashMap<>());
    {
        // Verify the correct message is written and ack is handled
        parserBolt.execute(t1);
        assertEquals(expectedRawMessage, mockParserRunner.getRawMessage());
        InOrder inOrder = inOrder(bulkWriterResponseHandler, writerHandler);
        inOrder.verify(bulkWriterResponseHandler).addTupleMessageIds(t1, Arrays.asList("messageId1", "messageId2", "messageId3", "messageId4", "messageId5"));
        inOrder.verify(writerHandler, times(1)).write("yaf", messages.get(0), parserConfigurations);
        inOrder.verify(writerHandler, times(1)).write("yaf", messages.get(1), parserConfigurations);
        inOrder.verify(writerHandler, times(1)).write("yaf", messages.get(2), parserConfigurations);
        inOrder.verify(writerHandler, times(1)).write("yaf", messages.get(3), parserConfigurations);
        inOrder.verify(writerHandler, times(1)).write("yaf", messages.get(4), parserConfigurations);
    }
    verifyNoMoreInteractions(writerHandler, bulkWriterResponseHandler, outputCollector);
}
Also used : InOrder(org.mockito.InOrder) SensorParserConfig(org.apache.metron.common.configuration.SensorParserConfig) BulkMessage(org.apache.metron.common.writer.BulkMessage) JSONObject(org.json.simple.JSONObject) ParserConfigurations(org.apache.metron.common.configuration.ParserConfigurations) RawMessage(org.apache.metron.common.message.metadata.RawMessage) BaseBoltTest(org.apache.metron.test.bolt.BaseBoltTest) Test(org.junit.jupiter.api.Test)

Example 12 with RawMessage

use of org.apache.metron.common.message.metadata.RawMessage in project metron by apache.

the class ParserRunnerImplTest method shouldExecuteWithMasterThrowable.

@Test
public void shouldExecuteWithMasterThrowable() {
    parserRunner = spy(parserRunner);
    RawMessage rawMessage = new RawMessage("raw_message".getBytes(StandardCharsets.UTF_8), new HashMap<>());
    Throwable masterThrowable = mock(Throwable.class);
    MessageParserResult<JSONObject> messageParserResult = new DefaultMessageParserResult<>(masterThrowable);
    when(broParser.parseOptionalResult(rawMessage.getMessage())).thenReturn(Optional.of(messageParserResult));
    parserRunner.setSensorToParserComponentMap(new HashMap<String, ParserComponent>() {

        {
            put("bro", new ParserComponent(broParser, stellarFilter));
        }
    });
    ParserRunnerResults<JSONObject> parserRunnerResults = parserRunner.execute("bro", rawMessage, parserConfigurations);
    verify(parserRunner, times(0)).processMessage(any(), any(), any(), any(), any());
    MetronError expectedError = new MetronError().withErrorType(Constants.ErrorType.PARSER_ERROR).withThrowable(masterThrowable).withSensorType(Collections.singleton("bro")).addRawMessage(rawMessage.getMessage());
    assertEquals(1, parserRunnerResults.getErrors().size());
    assertTrue(parserRunnerResults.getErrors().contains(expectedError));
}
Also used : JSONObject(org.json.simple.JSONObject) MetronError(org.apache.metron.common.error.MetronError) RawMessage(org.apache.metron.common.message.metadata.RawMessage) Test(org.junit.jupiter.api.Test)

Example 13 with RawMessage

use of org.apache.metron.common.message.metadata.RawMessage in project metron by apache.

the class ParserRunnerImplTest method shouldNotOverwriteOriginalStringAddedByParser.

/**
 * This is only testing the processMessage method
 */
@Test
public void shouldNotOverwriteOriginalStringAddedByParser() {
    JSONObject inputMessage = new JSONObject();
    inputMessage.put("guid", "guid");
    inputMessage.put("ip_src_addr", "192.168.1.1");
    inputMessage.put("ip_dst_addr", "192.168.1.2");
    inputMessage.put(Fields.ORIGINAL.getName(), "original_string_added_by_parser");
    RawMessage rawMessage = new RawMessage("raw_message_for_testing".getBytes(StandardCharsets.UTF_8), new HashMap<>());
    JSONObject expectedOutput = new JSONObject();
    expectedOutput.put("guid", "guid");
    expectedOutput.put("source.type", "bro");
    expectedOutput.put("ip_src_addr", "192.168.1.1");
    expectedOutput.put("ip_dst_addr", "192.168.1.2");
    expectedOutput.put(Fields.ORIGINAL.getName(), "original_string_added_by_parser");
    when(stellarFilter.emit(expectedOutput, parserRunner.getStellarContext())).thenReturn(true);
    when(broParser.validate(expectedOutput)).thenReturn(true);
    parserRunner.setSensorToParserComponentMap(new HashMap<String, ParserComponent>() {

        {
            put("bro", new ParserComponent(broParser, stellarFilter));
        }
    });
    Optional<ParserRunnerImpl.ProcessResult> processResult = parserRunner.processMessage("bro", inputMessage, rawMessage, broParser, parserConfigurations);
    assertTrue(processResult.isPresent());
    assertFalse(processResult.get().isError());
    assertEquals(expectedOutput, processResult.get().getMessage());
}
Also used : JSONObject(org.json.simple.JSONObject) ProcessResult(org.apache.metron.parsers.ParserRunnerImpl.ProcessResult) RawMessage(org.apache.metron.common.message.metadata.RawMessage) Test(org.junit.jupiter.api.Test)

Example 14 with RawMessage

use of org.apache.metron.common.message.metadata.RawMessage in project metron by apache.

the class ParserRunnerImplTest method shouldReturnMetronErrorOnInvalidMessage.

@Test
public void shouldReturnMetronErrorOnInvalidMessage() {
    Map<String, Object> metadata = new HashMap<>();
    metadata.put("metron.metadata.topic", "bro");
    metadata.put("metron.metadata.partition", 0);
    metadata.put("metron.metadata.offset", 123);
    JSONObject inputMessage = new JSONObject();
    inputMessage.put("guid", "guid");
    RawMessage rawMessage = new RawMessage("raw_message".getBytes(StandardCharsets.UTF_8), metadata);
    JSONObject expectedOutput = new JSONObject();
    expectedOutput.put("guid", "guid");
    expectedOutput.put("source.type", "bro");
    expectedOutput.put(Fields.ORIGINAL.getName(), "raw_message");
    MetronError expectedMetronError = new MetronError().withErrorType(Constants.ErrorType.PARSER_INVALID).withSensorType(Collections.singleton("bro")).withMetadata(metadata).addRawMessage(inputMessage);
    when(stellarFilter.emit(expectedOutput, parserRunner.getStellarContext())).thenReturn(true);
    // This is the important switch. Not to be confused with field validators.
    when(broParser.validate(expectedOutput)).thenReturn(false);
    parserRunner.setSensorToParserComponentMap(new HashMap<String, ParserComponent>() {

        {
            put("bro", new ParserComponent(broParser, stellarFilter));
        }
    });
    Optional<ParserRunnerImpl.ProcessResult> processResult = parserRunner.processMessage("bro", inputMessage, rawMessage, broParser, parserConfigurations);
    assertTrue(processResult.isPresent());
    assertTrue(processResult.get().isError());
    assertEquals(expectedMetronError, processResult.get().getError());
}
Also used : JSONObject(org.json.simple.JSONObject) MetronError(org.apache.metron.common.error.MetronError) ProcessResult(org.apache.metron.parsers.ParserRunnerImpl.ProcessResult) JSONObject(org.json.simple.JSONObject) RawMessage(org.apache.metron.common.message.metadata.RawMessage) Test(org.junit.jupiter.api.Test)

Aggregations

RawMessage (org.apache.metron.common.message.metadata.RawMessage)14 JSONObject (org.json.simple.JSONObject)14 Test (org.junit.jupiter.api.Test)13 MetronError (org.apache.metron.common.error.MetronError)5 ProcessResult (org.apache.metron.parsers.ParserRunnerImpl.ProcessResult)5 ParserConfigurations (org.apache.metron.common.configuration.ParserConfigurations)4 SensorParserConfig (org.apache.metron.common.configuration.SensorParserConfig)4 Tuple (org.apache.storm.tuple.Tuple)4 BaseBoltTest (org.apache.metron.test.bolt.BaseBoltTest)3 BulkMessage (org.apache.metron.common.writer.BulkMessage)2 MetronErrorJSONMatcher (org.apache.metron.test.error.MetronErrorJSONMatcher)1 InOrder (org.mockito.InOrder)1