Search in sources :

Example 1 with StatusCheckException

use of com.yahoo.athenz.common.server.status.StatusCheckException in project athenz by yahoo.

the class DynamoDBStatusChecker method check.

@Override
public void check() throws StatusCheckException {
    DynamoDBClientAndCredentials clientAndCreds = null;
    try {
        // Get DynamoDB client and temp credentials (if required)
        DynamoDBClientFetcher dynamoDBClientFetcher = getDynamoDBClientFetcher();
        clientAndCreds = dynamoDBClientFetcher.getDynamoDBClient(null, keyStore);
        AmazonDynamoDB amazonDynamoDB = clientAndCreds.getAmazonDynamoDB();
        // Get list of tables and verify our table appears
        boolean tableFound = amazonDynamoDB.listTables().getTableNames().stream().anyMatch(fetchedTableName -> fetchedTableName.equals(tableName));
        if (!tableFound) {
            throw new StatusCheckException(HttpStatus.SC_OK, "Table named " + tableName + " wasn't found in DynamoDB");
        }
    } catch (StatusCheckException ex) {
        throw ex;
    } catch (Throwable ex) {
        throw new StatusCheckException(ex);
    } finally {
        // Close resources
        if (clientAndCreds != null) {
            try {
                if (clientAndCreds.getAmazonDynamoDB() != null) {
                    clientAndCreds.getAmazonDynamoDB().shutdown();
                }
                if (clientAndCreds.getAwsCredentialsProvider() != null) {
                    clientAndCreds.getAwsCredentialsProvider().close();
                }
            } catch (IOException ignored) {
            }
        }
    }
}
Also used : StatusCheckException(com.yahoo.athenz.common.server.status.StatusCheckException) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) IOException(java.io.IOException)

Example 2 with StatusCheckException

use of com.yahoo.athenz.common.server.status.StatusCheckException in project athenz by yahoo.

the class JDBCCertRecordStoreStatusCheckerTest method testCheckNoDBConnection.

@Test
public void testCheckNoDBConnection() {
    JDBCCertRecordStore jdbcCertRecordStore = Mockito.mock(JDBCCertRecordStore.class);
    Mockito.when(jdbcCertRecordStore.getConnection()).thenThrow(new ResourceException(503));
    JDBCCertRecordStoreStatusChecker jdbcCertRecordStoreStatusChecker = new JDBCCertRecordStoreStatusChecker(jdbcCertRecordStore);
    try {
        jdbcCertRecordStoreStatusChecker.check();
        fail();
    } catch (StatusCheckException ex) {
        assertEquals(500, ex.getCode());
        assertEquals("ResourceException (503): {code: 503, message: \"Service Unavailable\"}", ex.getMsg());
    }
    Mockito.verify(jdbcCertRecordStore, Mockito.times(1)).getConnection();
}
Also used : StatusCheckException(com.yahoo.athenz.common.server.status.StatusCheckException) ResourceException(com.yahoo.athenz.zts.ResourceException) Test(org.testng.annotations.Test)

Aggregations

StatusCheckException (com.yahoo.athenz.common.server.status.StatusCheckException)2 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)1 ResourceException (com.yahoo.athenz.zts.ResourceException)1 IOException (java.io.IOException)1 Test (org.testng.annotations.Test)1