private List<JSONObject> doParse(List<String> messages) { // initialize HashSet<String> sensorTypes = new HashSet<>(); sensorTypes.add(sensorType); ParserRunnerImpl runner = new ParserRunnerImpl(sensorTypes); runner.init(() -> parserConfigurations, context); // parse each message List<ParserRunnerResults<JSONObject>> results = messages .stream() .map(str -> str.getBytes()) .map(bytes -> DEFAULT.get(emptyMap(), bytes, false, emptyMap())) .map(msg -> runner.execute(sensorType, msg, parserConfigurations)) .collect(Collectors.toList()); // aggregate both successes and errors into a list that can be returned List<JSONObject> successes = results .stream() .flatMap(result -> result.getMessages().stream()) .collect(Collectors.toList()); successCount += successes.size(); List<JSONObject> errors = results .stream() .flatMap(result -> result.getErrors().stream()) .map(err -> err.getJSONObject()) .collect(Collectors.toList()); errorCount += errors.size(); // return a list of both successes and errors successes.addAll(errors); return successes; }
return new ParserBolt(zookeeperUrl, new ParserRunnerImpl(new HashSet<>(sensorTypeToParserConfig.keySet())), writerConfigs);
@Test public void initShouldThrowExceptionOnMissingSensorParserConfig() { exception.expect(IllegalStateException.class); exception.expectMessage("Could not initialize parsers. Cannot find configuration for sensor test."); parserRunner = new ParserRunnerImpl(new HashSet<String>() {{ add("test"); }}); parserRunner.init(() -> parserConfigurations, mock(Context.class)); }
@Test public void executeShouldThrowExceptionOnMissingSensorParserConfig() { exception.expect(IllegalStateException.class); exception.expectMessage("Could not execute parser. Cannot find configuration for sensor test."); parserRunner = new ParserRunnerImpl(new HashSet<String>() {{ add("test"); }}); parserRunner.execute("test", mock(RawMessage.class), parserConfigurations); }
public ParserDriver(String sensorType, String parserConfig, String globalConfig) throws IOException { SensorParserConfig sensorParserConfig = SensorParserConfig.fromBytes(parserConfig.getBytes()); this.sensorType = sensorType == null ? sensorParserConfig.getSensorTopic() : sensorType; config = new ParserConfigurations(); config.updateSensorParserConfig(this.sensorType, SensorParserConfig.fromBytes(parserConfig.getBytes())); config.updateGlobalConfig(JSONUtils.INSTANCE.load(globalConfig, JSONUtils.MAP_SUPPLIER)); parserRunner = new ParserRunnerImpl(new HashSet<String>() {{ add(sensorType); }}); }
@Before public void setup() throws IOException { parserConfigurations = new ParserConfigurations(); SensorParserConfig broConfig = SensorParserConfig.fromBytes(broConfigString.getBytes()); SensorParserConfig snortConfig = SensorParserConfig.fromBytes(snortConfigString.getBytes()); parserConfigurations.updateSensorParserConfig("bro", broConfig); parserConfigurations.updateSensorParserConfig("snort", snortConfig); parserConfigurations.updateGlobalConfig(JSONUtils.INSTANCE.load(globalConfigString, JSONUtils.MAP_SUPPLIER)); parserRunner = new ParserRunnerImpl(new HashSet<>(Arrays.asList("bro", "snort"))); broParser = mock(MessageParser.class); snortParser = mock(MessageParser.class); stellarFilter = mock(StellarFilter.class); mockStatic(ReflectionUtils.class); mockStatic(Filters.class); when(ReflectionUtils.createInstance("org.apache.metron.parsers.bro.BasicBroParser")).thenReturn(broParser); when(ReflectionUtils.createInstance("org.apache.metron.parsers.snort.BasicSnortParser")).thenReturn(snortParser); when(Filters.get("org.apache.metron.parsers.filters.StellarFilter", broConfig.getParserConfig())) .thenReturn(stellarFilter); }