DynamoDBStatusChecker implements StatusChecker { @Override public void check() throws StatusCheckException { DynamoDBClientAndCredentials clientAndCreds = null; try { DynamoDBClientFetcher dynamoDBClientFetcher = getDynamoDBClientFetcher(); clientAndCreds = dynamoDBClientFetcher.getDynamoDBClient(null, keyStore); AmazonDynamoDB amazonDynamoDB = clientAndCreds.getAmazonDynamoDB(); 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 { if (clientAndCreds != null) { try { if (clientAndCreds.getAmazonDynamoDB() != null) { clientAndCreds.getAmazonDynamoDB().shutdown(); } if (clientAndCreds.getAwsCredentialsProvider() != null) { clientAndCreds.getAwsCredentialsProvider().close(); } } catch (IOException ignored) { } } } } DynamoDBStatusChecker(String tableName, PrivateKeyStore keyStore); @Override void check();  }
@Test public void testCheck() throws StatusCheckException, IOException { String tableName = "testTable"; when(listTablesResult.getTableNames()).thenReturn(Collections.singletonList(tableName)); when(amazonDynamoDB.listTables()).thenReturn(listTablesResult); DynamoDBClientAndCredentials dynamoDBClientAndCredentials = new DynamoDBClientAndCredentials(amazonDynamoDB, awsCredentialsProvider); when(dynamoDBClientFetcher.getDynamoDBClient(any(), any())).thenReturn(dynamoDBClientAndCredentials); DynamoDBStatusCheckerTestClass dynamoDBStatusChecker = new DynamoDBStatusCheckerTestClass(tableName); dynamoDBStatusChecker.check(); Mockito.verify(amazonDynamoDB, times(1)).shutdown(); Mockito.verify(awsCredentialsProvider, times(1)).close(); }
@Test public void testCheckNoCredentialsProvider() throws StatusCheckException, IOException { String tableName = "testTable"; when(listTablesResult.getTableNames()).thenReturn(Collections.singletonList(tableName)); when(amazonDynamoDB.listTables()).thenReturn(listTablesResult); DynamoDBClientAndCredentials dynamoDBClientAndCredentials = new DynamoDBClientAndCredentials(amazonDynamoDB, null); when(dynamoDBClientFetcher.getDynamoDBClient(any(), any())).thenReturn(dynamoDBClientAndCredentials); DynamoDBStatusCheckerTestClass dynamoDBStatusChecker = new DynamoDBStatusCheckerTestClass(tableName); dynamoDBStatusChecker.check(); Mockito.verify(amazonDynamoDB, times(1)).shutdown(); Mockito.verify(awsCredentialsProvider, times(0)).close(); }
@Test public void testCheckNoDynamoDBClient() throws StatusCheckException, IOException { String tableName = "testTable"; when(listTablesResult.getTableNames()).thenReturn(Collections.singletonList(tableName)); DynamoDBClientAndCredentials dynamoDBClientAndCredentials = new DynamoDBClientAndCredentials(null, awsCredentialsProvider); when(dynamoDBClientFetcher.getDynamoDBClient(any(), any())).thenReturn(dynamoDBClientAndCredentials); DynamoDBStatusCheckerTestClass dynamoDBStatusChecker = new DynamoDBStatusCheckerTestClass(tableName); try { dynamoDBStatusChecker.check(); fail(); } catch (StatusCheckException ex) { assertEquals(500, ex.getCode()); Mockito.verify(amazonDynamoDB, times(0)).shutdown(); Mockito.verify(awsCredentialsProvider, times(1)).close(); } }
@Test public void testTableNotFound() throws IOException { String requestedTable = "requestedTable"; String tableNameInAws = "someExistingTable"; when(listTablesResult.getTableNames()).thenReturn(Collections.singletonList(tableNameInAws)); when(amazonDynamoDB.listTables()).thenReturn(listTablesResult); DynamoDBClientAndCredentials dynamoDBClientAndCredentials = new DynamoDBClientAndCredentials(amazonDynamoDB, awsCredentialsProvider); when(dynamoDBClientFetcher.getDynamoDBClient(any(), any())).thenReturn(dynamoDBClientAndCredentials); DynamoDBStatusCheckerTestClass dynamoDBStatusChecker = new DynamoDBStatusCheckerTestClass(requestedTable); try { dynamoDBStatusChecker.check(); fail(); } catch (StatusCheckException ex) { assertEquals("Table named " + requestedTable + " wasn't found in DynamoDB", ex.getMsg()); assertEquals(200, ex.getCode()); } Mockito.verify(amazonDynamoDB, times(1)).shutdown(); Mockito.verify(awsCredentialsProvider, times(1)).close(); }
@Test public void testClientNullTables() throws IOException { String tableName = "testTable"; when(amazonDynamoDB.listTables()).thenReturn(null); DynamoDBClientAndCredentials dynamoDBClientAndCredentials = new DynamoDBClientAndCredentials(amazonDynamoDB, awsCredentialsProvider); when(dynamoDBClientFetcher.getDynamoDBClient(any(), any())).thenReturn(dynamoDBClientAndCredentials); DynamoDBStatusCheckerTestClass dynamoDBStatusChecker = new DynamoDBStatusCheckerTestClass(tableName); try { dynamoDBStatusChecker.check(); fail(); } catch (StatusCheckException ex) { assertNull(ex.getMessage()); assertEquals(500, ex.getCode()); } Mockito.verify(amazonDynamoDB, times(1)).shutdown(); Mockito.verify(awsCredentialsProvider, times(1)).close(); }
@Test public void testClientNull() throws IOException { String tableName = "testTable"; when(dynamoDBClientFetcher.getDynamoDBClient(any(), any())).thenReturn(null); DynamoDBStatusCheckerTestClass dynamoDBStatusChecker = new DynamoDBStatusCheckerTestClass(tableName); try { dynamoDBStatusChecker.check(); fail(); } catch (StatusCheckException ex) { assertNull(ex.getMessage()); assertEquals(500, ex.getCode()); } Mockito.verify(amazonDynamoDB, times(0)).shutdown(); Mockito.verify(awsCredentialsProvider, times(0)).close(); }