ParserRunnerImpl implements ParserRunner<JSONObject>, Serializable { @Override public ParserRunnerResults<JSONObject> execute(String sensorType, RawMessage rawMessage, ParserConfigurations parserConfigurations) { DefaultParserRunnerResults parserRunnerResults = new DefaultParserRunnerResults(); SensorParserConfig sensorParserConfig = parserConfigurations.getSensorParserConfig(sensorType); if (sensorParserConfig != null) { MessageParser<JSONObject> parser = sensorToParserComponentMap.get(sensorType).getMessageParser(); Optional<MessageParserResult<JSONObject>> optionalMessageParserResult = parser.parseOptionalResult(rawMessage.getMessage()); if (optionalMessageParserResult.isPresent()) { MessageParserResult<JSONObject> messageParserResult = optionalMessageParserResult.get(); messageParserResult.getMessages().forEach(message -> { Optional<ProcessResult> processResult = processMessage(sensorType, message, rawMessage, parser, parserConfigurations); if (processResult.isPresent()) { if (processResult.get().isError()) { parserRunnerResults.addError(processResult.get().getError()); } else { parserRunnerResults.addMessage(processResult.get().getMessage()); } } }); messageParserResult.getMasterThrowable().ifPresent(throwable -> parserRunnerResults.addError(new MetronError() .withErrorType(Constants.ErrorType.PARSER_ERROR) .withThrowable(throwable) .withSensorType(Collections.singleton(sensorType)) .withMetadata(rawMessage.getMetadata()) .addRawMessage(rawMessage.getMessage()))); parserRunnerResults.addErrors(messageParserResult.getMessageThrowables().entrySet().stream().map(entry -> new MetronError() .withErrorType(Constants.ErrorType.PARSER_ERROR) .withThrowable(entry.getValue()) .withSensorType(Collections.singleton(sensorType)) .withMetadata(rawMessage.getMetadata()) .addRawMessage(entry.getKey())).collect(Collectors.toList())); } } else { throw new IllegalStateException(String.format("Could not execute parser. Cannot find configuration for sensor %s.", sensorType)); } return parserRunnerResults; } ParserRunnerImpl(HashSet<String> sensorTypes); Map<String, ParserComponent> getSensorToParserComponentMap(); void setSensorToParserComponentMap(Map<String, ParserComponent> sensorToParserComponentMap); Context getStellarContext(); @Override Set<String> getSensorTypes(); @Override void init(Supplier<ParserConfigurations> parserConfigSupplier, Context stellarContext); @Override ParserRunnerResults<JSONObject> execute(String sensorType, RawMessage rawMessage, ParserConfigurations parserConfigurations);  }
@Test public void executeShouldThrowExceptionOnMissingSensorParserConfig() { parserRunner = new ParserRunnerImpl(new HashSet<String>() {{ add("test"); }}); IllegalStateException e = assertThrows(IllegalStateException.class, () -> parserRunner.execute("test", mock(RawMessage.class), parserConfigurations)); assertEquals("Could not execute parser. Cannot find configuration for sensor test.", e.getMessage()); }