DynamoDBManager { public static void waitForTableToBecomeActive(final AmazonDynamoDB dynamoDB, final String tableName) { int numTries = 0; TableStatus currentState; try { currentState = getTableStatus(dynamoDB, tableName); } catch (final ResourceNotFoundException e) { throw new IllegalStateException("Table " + tableName + " does not exist"); } while (numTries < RETRY_COUNT) { try { Thread.sleep(RETRY_DELAY); } catch (final InterruptedException e) { LOGGER.severe(e.getMessage()); } currentState = getTableStatus(dynamoDB, tableName); numTries++; LOGGER.info("Table " + tableName + " is in " + currentState + " state"); switch (currentState) { case ACTIVE: return; case DELETING: throw new IllegalStateException("Table " + tableName + " has DELETING status and will never become active"); case UPDATING: break; case CREATING: break; default: throw new IllegalStateException("Unknown table status: " + currentState); } } final DescribeTableResult result = dynamoDB.describeTable(tableName); throw new IllegalStateException("Table " + tableName + " never went ACTIVE" + result); } private  DynamoDBManager(); static TableDescription createTable(final AmazonDynamoDB dynamoDB, final CreateTableRequest request); static boolean doesTableExist(final AmazonDynamoDB dynamoDB, final String tableName); static TableStatus getTableStatus(final AmazonDynamoDB dynamoDB, final String tableName); static void waitForTableToBecomeActive(final AmazonDynamoDB dynamoDB, final String tableName);  }
@Test public void testWaitForTableToBecomeActiveAlreadyActive() { final TableDescription table = new TableDescription(); final DescribeTableResult result = new DescribeTableResult().withTable(table); table.setTableStatus(TableStatus.ACTIVE); dynamoDB.describeTable(tableName); PowerMock.expectLastCall().andReturn(result); PowerMock.expectLastCall().andReturn(result); PowerMock.replayAll(); DynamoDBManager.waitForTableToBecomeActive(dynamoDB, tableName); }
@Test public void testWaitForTableToBecomeActiveCreatingThenActive() { final TableDescription table1 = new TableDescription(); table1.setTableStatus(TableStatus.CREATING); final DescribeTableResult result1 = new DescribeTableResult().withTable(table1); final TableDescription table2 = new TableDescription(); table2.setTableStatus(TableStatus.ACTIVE); final DescribeTableResult result2 = new DescribeTableResult().withTable(table2); EasyMock.expect(dynamoDB.describeTable(tableName)).andReturn(result1); EasyMock.expect(dynamoDB.describeTable(tableName)).andReturn(result2); PowerMock.replayAll(); DynamoDBManager.waitForTableToBecomeActive(dynamoDB, tableName); }
@Test(expected = IllegalStateException.class) public void testWaitForTableToBecomeActiveDeleting() { final TableDescription table = new TableDescription().withTableStatus(TableStatus.DELETING); final DescribeTableResult result = new DescribeTableResult().withTable(table); PowerMock.expectLastCall().andReturn(result); PowerMock.replayAll(); DynamoDBManager.waitForTableToBecomeActive(dynamoDB, tableName); }
@Test(expected = IllegalStateException.class) public void testWaitForTableToBecomeActiveNeverGoingActive() { final TableDescription table = new TableDescription(); final DescribeTableResult result = new DescribeTableResult().withTable(table); table.setTableStatus(TableStatus.CREATING); EasyMock.expect(dynamoDB.describeTable(tableName)).andReturn(result).anyTimes(); PowerMock.replayAll(); DynamoDBManager.waitForTableToBecomeActive(dynamoDB, tableName); }
@Test(expected = IllegalStateException.class) public void testWaitForTableToBecomeActiveTableDoesNotExist() { EasyMock.expect(dynamoDB.describeTable(tableName)).andThrow(new ResourceNotFoundException(null)); PowerMock.replayAll(); DynamoDBManager.waitForTableToBecomeActive(dynamoDB, tableName); }
@Test public void testWaitForTableToBecomeActiveUpdatingThenActive() { final TableDescription table1 = new TableDescription(); table1.setTableStatus(TableStatus.UPDATING); final DescribeTableResult result1 = new DescribeTableResult().withTable(table1); final TableDescription table2 = new TableDescription(); table2.setTableStatus(TableStatus.ACTIVE); final DescribeTableResult result2 = new DescribeTableResult().withTable(table2); EasyMock.expect(dynamoDB.describeTable(tableName)).andReturn(result1); EasyMock.expect(dynamoDB.describeTable(tableName)).andReturn(result2); PowerMock.replayAll(); DynamoDBManager.waitForTableToBecomeActive(dynamoDB, tableName); }