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; }
@Test public void shouldThrowExceptionOnEmptyParserSupplier() { exception.expect(IllegalStateException.class); exception.expectMessage("A parser config supplier must be set before initializing the ParserRunner."); parserRunner.init(null, null); }
@Test public void shouldThrowExceptionOnEmptyStellarContext() { exception.expect(IllegalStateException.class); exception.expectMessage("A stellar context must be set before initializing the ParserRunner."); parserRunner.init(() -> parserConfigurations, null); }
@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)); }
verify(parserRunner, times(1)).init(any(Supplier.class), eq(stellarContext)); verify(yafConfig, times(1)).init(); Map<String, String> topicToSensorMap = parserBolt.getTopicToSensorMap();
Map<String, Object> snortParserConfig = parserConfigurations.getSensorParserConfig("snort").getParserConfig(); parserRunner.init(() -> parserConfigurations, stellarContext);