JooqHealthCheck extends HealthCheck { @Override protected Result check() throws Exception { try { databaseAgnostic(dataSource).selectOne().execute(); } catch (DataAccessException | SQLException e) { switch (onFailure) { case LOG_ONLY: logger.warn("Unhealthy connection to database."); break; case RETURN_UNHEALTHY: return Result.unhealthy("Unhealthy connection to database."); } } return Result.healthy(); } JooqHealthCheck(ManagedDataSource dataSource, OnFailure onFailure);   }
@Test public void reportsHealthy() throws Exception { JooqHealthCheck healthCheck = new JooqHealthCheck(dataSource, LOG_ONLY); assertThat(healthCheck.check().isHealthy()).isTrue(); healthCheck = new JooqHealthCheck(dataSource, RETURN_UNHEALTHY); assertThat(healthCheck.check().isHealthy()).isTrue(); }
@Test public void reportsUnhealthy() throws Exception { Connection connection; try (Connection c = dataSource.getConnection()) { connection = c; } when(managedDataSource.getConnection()).thenReturn(connection); JooqHealthCheck healthCheck = new JooqHealthCheck(managedDataSource, RETURN_UNHEALTHY); HealthCheck.Result result = healthCheck.check(); assertThat(result.isHealthy()).isFalse(); assertThat(result.getMessage()).isEqualTo("Unhealthy connection to database."); }
@Test public void reportsHealthyWhenLogOnlyIsEnabled() throws Exception { Connection connection; try (Connection c = dataSource.getConnection()) { connection = c; } when(managedDataSource.getConnection()).thenReturn(connection); JooqHealthCheck healthCheck = new JooqHealthCheck(managedDataSource, LOG_ONLY); assertThat(healthCheck.check().isHealthy()).isTrue(); }