-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #274 from CJSCommonPlatform/update-framework
Update framework to 11.0.0-M11
- Loading branch information
Showing
7 changed files
with
148 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...main/java/uk/gov/justice/services/healthcheck/healthchecks/SystemDatabaseHealthcheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package uk.gov.justice.services.healthcheck.healthchecks; | ||
|
||
import static java.lang.String.format; | ||
import static java.util.List.of; | ||
import static uk.gov.justice.services.healthcheck.api.HealthcheckResult.failure; | ||
|
||
import uk.gov.justice.services.healthcheck.api.Healthcheck; | ||
import uk.gov.justice.services.healthcheck.api.HealthcheckResult; | ||
import uk.gov.justice.services.healthcheck.database.TableChecker; | ||
import uk.gov.justice.services.jdbc.persistence.SystemJdbcDataSourceProvider; | ||
|
||
import java.sql.SQLException; | ||
import java.util.List; | ||
|
||
import javax.inject.Inject; | ||
import javax.sql.DataSource; | ||
|
||
import org.slf4j.Logger; | ||
|
||
public class SystemDatabaseHealthcheck implements Healthcheck { | ||
|
||
public static final String SYSTEM_DATABASE_HEALTHCHECK_NAME = "system-database-healthcheck"; | ||
|
||
public static final List<String> SYSTEM_DATABASE_TABLE_NAMES = of( | ||
"system_command_status", | ||
"stored_command"); | ||
|
||
@Inject | ||
private SystemJdbcDataSourceProvider systemJdbcDataSourceProvider; | ||
|
||
@Inject | ||
private TableChecker tableChecker; | ||
|
||
@Inject | ||
private Logger logger; | ||
|
||
@Override | ||
public String getHealthcheckName() { | ||
return SYSTEM_DATABASE_HEALTHCHECK_NAME; | ||
} | ||
|
||
@Override | ||
public String healthcheckDescription() { | ||
return "Checks connectivity to the system database and that all tables are available"; | ||
} | ||
|
||
@Override | ||
public HealthcheckResult runHealthcheck() { | ||
|
||
final DataSource jobStoreDataSource = systemJdbcDataSourceProvider.getDataSource(); | ||
|
||
try { | ||
return tableChecker.checkTables(SYSTEM_DATABASE_TABLE_NAMES, jobStoreDataSource); | ||
|
||
} catch (final SQLException e) { | ||
logger.error("Healthcheck for system database failed.", e); | ||
return failure(format("Exception thrown accessing system database. %s: %s", e.getClass().getName(), e.getMessage())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
.../java/uk/gov/justice/services/healthcheck/healthchecks/SystemDatabaseHealthcheckTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package uk.gov.justice.services.healthcheck.healthchecks; | ||
|
||
import static java.util.Optional.of; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
import static uk.gov.justice.services.healthcheck.healthchecks.SystemDatabaseHealthcheck.SYSTEM_DATABASE_TABLE_NAMES; | ||
|
||
import uk.gov.justice.services.healthcheck.api.HealthcheckResult; | ||
import uk.gov.justice.services.healthcheck.database.TableChecker; | ||
import uk.gov.justice.services.jdbc.persistence.SystemJdbcDataSourceProvider; | ||
|
||
import java.sql.SQLException; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
import org.slf4j.Logger; | ||
@RunWith(MockitoJUnitRunner.class) | ||
public class SystemDatabaseHealthcheckTest { | ||
|
||
@Mock | ||
private SystemJdbcDataSourceProvider systemJdbcDataSourceProvider; | ||
|
||
@Mock | ||
private TableChecker tableChecker; | ||
|
||
@Mock | ||
private Logger logger; | ||
|
||
@InjectMocks | ||
private SystemDatabaseHealthcheck systemDatabaseHealthcheck; | ||
|
||
@Test | ||
public void shouldReturnCorrectHealthcheckName() throws Exception { | ||
|
||
assertThat(systemDatabaseHealthcheck.getHealthcheckName(), is("system-database-healthcheck")); | ||
} | ||
|
||
@Test | ||
public void shouldReturnCorrectHealthcheckDescription() throws Exception { | ||
|
||
assertThat(systemDatabaseHealthcheck.healthcheckDescription(), is("Checks connectivity to the system database and that all tables are available")); | ||
} | ||
|
||
@Test | ||
public void shouldGetListOfExpectedTablesFromEventStoreAsHealthcheck() throws Exception { | ||
|
||
final DataSource systemDataSource = mock(DataSource.class); | ||
final HealthcheckResult healthcheckResult = mock(HealthcheckResult.class); | ||
|
||
when(systemJdbcDataSourceProvider.getDataSource()).thenReturn(systemDataSource); | ||
when(tableChecker.checkTables(SYSTEM_DATABASE_TABLE_NAMES, systemDataSource)).thenReturn(healthcheckResult); | ||
|
||
assertThat(systemDatabaseHealthcheck.runHealthcheck(), is(healthcheckResult)); | ||
} | ||
|
||
@Test | ||
public void shouldReturnHealthcheckFailureIfAccessingTheEventStoreThrowsSqlException() throws Exception { | ||
|
||
final SQLException sqlException = new SQLException("Oops"); | ||
final DataSource systemDataSource = mock(DataSource.class); | ||
|
||
when(systemJdbcDataSourceProvider.getDataSource()).thenReturn(systemDataSource); | ||
when(tableChecker.checkTables(SYSTEM_DATABASE_TABLE_NAMES, systemDataSource)).thenThrow(sqlException); | ||
|
||
final HealthcheckResult healthcheckResult = systemDatabaseHealthcheck.runHealthcheck(); | ||
|
||
assertThat(healthcheckResult.isPassed(), is(false)); | ||
assertThat(healthcheckResult.getErrorMessage().isPresent(), is(true)); | ||
assertThat(healthcheckResult.getErrorMessage(), is(of("Exception thrown accessing system database. java.sql.SQLException: Oops"))); | ||
|
||
verify(logger).error("Healthcheck for system database failed.", sqlException); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters