Search in sources :

Example 51 with SensorParserConfig

use of org.apache.metron.common.configuration.SensorParserConfig in project metron by apache.

the class ParserBoltTest method shouldThrowExceptionOnFailedExecute.

@Test
public void shouldThrowExceptionOnFailedExecute() {
    when(messageGetStrategy.get(t1)).thenReturn("originalMessage".getBytes(StandardCharsets.UTF_8));
    when(t1.getStringByField(FieldsConfiguration.TOPIC.getFieldName())).thenReturn("yafTopic");
    ParserConfigurations parserConfigurations = new ParserConfigurations();
    parserConfigurations.updateSensorParserConfig("yaf", new SensorParserConfig());
    doThrow(new IllegalStateException("parserRunner.execute failed")).when(parserRunner).execute(eq("yaf"), any(), eq(parserConfigurations));
    ParserBolt parserBolt = new ParserBolt("zookeeperUrl", parserRunner, 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");
        }
    });
    MetronError error = new MetronError().withErrorType(Constants.ErrorType.PARSER_ERROR).withThrowable(new IllegalStateException("parserRunner.execute failed")).withSensorType(Collections.singleton("yaf")).addRawMessage("originalMessage".getBytes(StandardCharsets.UTF_8));
    parserBolt.execute(t1);
    verify(outputCollector, times(1)).emit(eq(Constants.ERROR_STREAM), argThat(new MetronErrorJSONMatcher(error.getJSONObject())));
    verify(outputCollector, times(1)).reportError(any(IllegalStateException.class));
    verify(outputCollector, times(1)).ack(t1);
}
Also used : MetronErrorJSONMatcher(org.apache.metron.test.error.MetronErrorJSONMatcher) MetronError(org.apache.metron.common.error.MetronError) ParserConfigurations(org.apache.metron.common.configuration.ParserConfigurations) SensorParserConfig(org.apache.metron.common.configuration.SensorParserConfig) BaseBoltTest(org.apache.metron.test.bolt.BaseBoltTest) Test(org.junit.jupiter.api.Test)

Example 52 with SensorParserConfig

use of org.apache.metron.common.configuration.SensorParserConfig in project metron by apache.

the class ParserBoltTest method shouldExecuteOnSuccess.

@Test
public void shouldExecuteOnSuccess() 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);
    JSONObject message = new JSONObject();
    message.put(Constants.GUID, "messageId");
    message.put("field", "value");
    mockParserRunner.setMessages(Collections.singletonList(message));
    RawMessage expectedRawMessage = new RawMessage("originalMessage".getBytes(StandardCharsets.UTF_8), new HashMap<>());
    {
        parserBolt.execute(t1);
        assertEquals(expectedRawMessage, mockParserRunner.getRawMessage());
        verify(bulkWriterResponseHandler).addTupleMessageIds(t1, Collections.singletonList("messageId"));
        verify(writerHandler, times(1)).write("yaf", new BulkMessage<>("messageId", message), parserConfigurations);
    }
}
Also used : 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 53 with SensorParserConfig

use of org.apache.metron.common.configuration.SensorParserConfig in project metron by apache.

the class ParserBoltTest method shouldExecuteOnError.

@Test
public void shouldExecuteOnError() 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");
        }
    });
    mockParserRunner.setInvalid(true);
    ParserConfigurations parserConfigurations = new ParserConfigurations();
    parserConfigurations.updateSensorParserConfig("yaf", new SensorParserConfig());
    ParserBolt parserBolt = 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");
        }
    });
    JSONObject message = new JSONObject();
    message.put("field", "value");
    mockParserRunner.setMessages(Collections.singletonList(message));
    RawMessage expectedRawMessage = new RawMessage("originalMessage".getBytes(StandardCharsets.UTF_8), new HashMap<>());
    MetronError error = new MetronError().withErrorType(Constants.ErrorType.PARSER_INVALID).withSensorType(Collections.singleton("yaf")).addRawMessage(message);
    parserBolt.execute(t1);
    assertEquals(expectedRawMessage, mockParserRunner.getRawMessage());
    verify(outputCollector, times(1)).emit(eq(Constants.ERROR_STREAM), argThat(new MetronErrorJSONMatcher(error.getJSONObject())));
    verify(outputCollector, times(1)).ack(t1);
}
Also used : MetronErrorJSONMatcher(org.apache.metron.test.error.MetronErrorJSONMatcher) MetronError(org.apache.metron.common.error.MetronError) SensorParserConfig(org.apache.metron.common.configuration.SensorParserConfig) 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 54 with SensorParserConfig

use of org.apache.metron.common.configuration.SensorParserConfig in project metron by apache.

the class ParserTopologyBuilderTest method shouldCreateWriterConfigWithWriterClassName.

@Test
public void shouldCreateWriterConfigWithWriterClassName() {
    SensorParserConfig yafConfig = new SensorParserConfig();
    yafConfig.setSensorTopic("yaf");
    yafConfig.setWriterClassName("org.apache.metron.writer.NoopWriter");
    when(configs.getSensorParserConfig("yaf")).thenReturn(yafConfig);
    Map<String, SensorParserConfig> sensorTypeToParserConfig = new HashMap<String, SensorParserConfig>() {

        {
            put("yaf", yafConfig);
        }
    };
    Map<String, WriterHandler> writerConfigs = ParserTopologyBuilder.createWriterConfigs("zookeeperUrl", Optional.of("brokerUrl"), sensorTypeToParserConfig, Optional.of("securityProtocol"), configs, Optional.empty());
    assertEquals(1, writerConfigs.size());
    assertTrue(writerConfigs.get("yaf").getBulkMessageWriter() instanceof NoopWriter);
}
Also used : NoopWriter(org.apache.metron.writer.NoopWriter) HashMap(java.util.HashMap) WriterHandler(org.apache.metron.parsers.bolt.WriterHandler) SensorParserConfig(org.apache.metron.common.configuration.SensorParserConfig) Test(org.junit.jupiter.api.Test)

Example 55 with SensorParserConfig

use of org.apache.metron.common.configuration.SensorParserConfig in project metron by apache.

the class ParserTopologyBuilderTest method shouldCreateWriterConfigWithSuppliedOutputTopic.

@Test
public void shouldCreateWriterConfigWithSuppliedOutputTopic() {
    SensorParserConfig snortConfig = new SensorParserConfig();
    snortConfig.setSensorTopic("snort");
    when(configs.getSensorParserConfig("snort")).thenReturn(snortConfig);
    Map<String, SensorParserConfig> sensorTypeToParserConfig = new HashMap<String, SensorParserConfig>() {

        {
            put("snort", snortConfig);
        }
    };
    Map<String, WriterHandler> writerConfigs = ParserTopologyBuilder.createWriterConfigs("zookeeperUrl", Optional.of("brokerUrl"), sensorTypeToParserConfig, Optional.of("securityProtocol"), configs, Optional.of("supplied_topic"));
    assertEquals(1, writerConfigs.size());
    // Can't directly verify against mocks because this is all static. However, knowing that we have a KafkaWriter
    // and the appropriate topic lets us know we've created the underlying config.
    BulkMessageWriter writer = writerConfigs.get("snort").getBulkMessageWriter();
    assertTrue(writer instanceof KafkaWriter);
    assertEquals("supplied_topic", ((KafkaWriter) writer).getKafkaTopic(new JSONObject()).get());
}
Also used : JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) BulkMessageWriter(org.apache.metron.common.writer.BulkMessageWriter) WriterHandler(org.apache.metron.parsers.bolt.WriterHandler) KafkaWriter(org.apache.metron.writer.kafka.KafkaWriter) SensorParserConfig(org.apache.metron.common.configuration.SensorParserConfig) Test(org.junit.jupiter.api.Test)

Aggregations

SensorParserConfig (org.apache.metron.common.configuration.SensorParserConfig)97 Test (org.junit.jupiter.api.Test)74 JSONObject (org.json.simple.JSONObject)40 FieldTransformer (org.apache.metron.common.configuration.FieldTransformer)20 ParserConfigurations (org.apache.metron.common.configuration.ParserConfigurations)19 HashMap (java.util.HashMap)15 Config (org.apache.storm.Config)7 BaseBoltTest (org.apache.metron.test.bolt.BaseBoltTest)6 File (java.io.File)5 Map (java.util.Map)5 CuratorFramework (org.apache.curator.framework.CuratorFramework)5 WriterHandler (org.apache.metron.parsers.bolt.WriterHandler)5 IOException (java.io.IOException)4 SensorParserGroup (org.apache.metron.common.configuration.SensorParserGroup)4 MetronError (org.apache.metron.common.error.MetronError)4 ImmutableList (com.google.common.collect.ImmutableList)3 List (java.util.List)3 CommandLine (org.apache.commons.cli.CommandLine)3 ParseException (org.apache.commons.cli.ParseException)3 PosixParser (org.apache.commons.cli.PosixParser)3