StatusLogger extends PeriodicStatusReporter { @Override public void initialize(Properties properties, QueryableStatusAggregator queryableStatusAggregator) { this.queryableStatusAggregator = queryableStatusAggregator; String periodString = properties.getProperty(REPORT_PERIOD_KEY); if (periodString == null) { throw new IllegalStateException(REPORT_PERIOD_KEY + " is null but it is required. Please configure it."); } try { setPeriod(Integer.parseInt(periodString)); } catch (NumberFormatException e) { throw new IllegalStateException(REPORT_PERIOD_KEY + " is not a valid number.", e); } String loglevelString = properties.getProperty(LOGGING_LEVEL_KEY); if (loglevelString == null) { throw new IllegalStateException(LOGGING_LEVEL_KEY + " is null but it is required. Please configure it."); } try { logLevel = LogLevel.valueOf(loglevelString.toUpperCase()); } catch (IllegalArgumentException e) { throw new IllegalStateException("Value set for " + LOGGING_LEVEL_KEY + " is not a valid log level."); } if (LogLevel.FATAL.equals(logLevel)){ throw new IllegalStateException("Cannot log status at the FATAL level. Please configure " + LOGGING_LEVEL_KEY + " to another value."); } statusQuery = properties.getProperty(QUERY_KEY); if (statusQuery == null) { throw new IllegalStateException(QUERY_KEY + " is null but it is required. Please configure it."); } reportRunner = new ReportStatusRunner(); }  @Override void initialize(Properties properties,  QueryableStatusAggregator queryableStatusAggregator); static final String LOGGER_STATUS_REPORTER_PROPERTY_PREFIX; static final String REPORT_PERIOD_KEY; static final String LOGGING_LEVEL_KEY; static final String QUERY_KEY; }
@Test(expected = IllegalStateException.class) public void testFailedInitDueToFatalLogLevel(){ Properties properties = new Properties(); properties.setProperty(REPORT_PERIOD_KEY, "100"); properties.setProperty(LOGGING_LEVEL_KEY, LogLevel.FATAL.name()); properties.setProperty(QUERY_KEY, MOCK_QUERY); statusLogger.initialize(properties, queryableStatusAggregator); }
@Test(expected = IllegalStateException.class) public void testFailedInitDueToNoPeriod(){ Properties properties = new Properties(); properties.setProperty(LOGGING_LEVEL_KEY, LogLevel.INFO.name()); properties.setProperty(QUERY_KEY, MOCK_QUERY); statusLogger.initialize(properties, queryableStatusAggregator); }
@Test(expected = IllegalStateException.class) public void testFailedInitDueToNoQuery(){ Properties properties = new Properties(); properties.setProperty(REPORT_PERIOD_KEY, "100"); properties.setProperty(LOGGING_LEVEL_KEY, LogLevel.INFO.name()); statusLogger.initialize(properties, queryableStatusAggregator); }
@Test public void TestTrace() { statusLogger.initialize(getProperties(LogLevel.TRACE), queryableStatusAggregator); statusLogger.start(); verify(logger, Mockito.timeout(300).atLeastOnce()).info(MOCK_STATUS, (Throwable) null); }
@Test public void TestDebug() { statusLogger.initialize(getProperties(LogLevel.DEBUG), queryableStatusAggregator); statusLogger.start(); verify(logger, Mockito.timeout(300).atLeastOnce()).debug(MOCK_STATUS, (Throwable) null); }
@Test public void TestInfo() { statusLogger.initialize(getProperties(LogLevel.INFO), queryableStatusAggregator); statusLogger.start(); verify(logger, Mockito.timeout(300).atLeastOnce()).info(MOCK_STATUS, (Throwable) null); }
@Test public void TestWarn() { statusLogger.initialize(getProperties(LogLevel.WARN), queryableStatusAggregator); statusLogger.start(); verify(logger, Mockito.timeout(300).atLeastOnce()).warn(MOCK_STATUS, (Throwable) null); }
@Test public void TestError() { statusLogger.initialize(getProperties(LogLevel.ERROR), queryableStatusAggregator); statusLogger.start(); verify(logger, Mockito.timeout(300).atLeastOnce()).error(MOCK_STATUS, (Throwable) null); }
@Test public void TestTraceException() throws IOException { Properties properties = new Properties(); properties.setProperty(REPORT_PERIOD_KEY, "100"); properties.setProperty(LOGGING_LEVEL_KEY, LogLevel.TRACE.name()); properties.setProperty(QUERY_KEY, MOCK_QUERY); IOException ioException = new IOException("This is an expected test exception"); Mockito.when(queryableStatusAggregator.statusReport(MOCK_QUERY)).thenThrow(ioException); statusLogger.initialize(properties, queryableStatusAggregator); statusLogger.start(); verify(logger, Mockito.timeout(300).atLeastOnce()).trace(ENCOUNTERED_IO_EXCEPTION, ioException); }
@Test public void TestDebugException() throws IOException { IOException ioException = new IOException("This is an expected test exception"); Mockito.when(queryableStatusAggregator.statusReport(MOCK_QUERY)).thenThrow(ioException); statusLogger.initialize(getProperties(LogLevel.DEBUG), queryableStatusAggregator); statusLogger.start(); verify(logger, Mockito.timeout(300).atLeastOnce()).debug(ENCOUNTERED_IO_EXCEPTION, ioException); }
@Test public void TestInfoException() throws IOException { IOException ioException = new IOException("This is an expected test exception"); Mockito.when(queryableStatusAggregator.statusReport(MOCK_QUERY)).thenThrow(ioException); statusLogger.initialize(getProperties(LogLevel.INFO), queryableStatusAggregator); statusLogger.start(); verify(logger, Mockito.timeout(300).atLeastOnce()).info(ENCOUNTERED_IO_EXCEPTION, ioException); }
@Test public void TestWarnException() throws IOException { IOException ioException = new IOException("This is an expected test exception"); Mockito.when(queryableStatusAggregator.statusReport(MOCK_QUERY)).thenThrow(ioException); statusLogger.initialize(getProperties(LogLevel.WARN), queryableStatusAggregator); statusLogger.start(); verify(logger, Mockito.timeout(300).atLeastOnce()).warn(ENCOUNTERED_IO_EXCEPTION, ioException); }
@Test public void TestErrorException() throws IOException { IOException ioException = new IOException("This is an expected test exception"); Mockito.when(queryableStatusAggregator.statusReport(MOCK_QUERY)).thenThrow(ioException); statusLogger.initialize(getProperties(LogLevel.ERROR), queryableStatusAggregator); statusLogger.start(); verify(logger, Mockito.timeout(300).atLeastOnce()).error(ENCOUNTERED_IO_EXCEPTION, ioException); }