DynamoDBManager { public static TableDescription createTable(final AmazonDynamoDB dynamoDB, final CreateTableRequest request) { try { final DescribeTableResult result = dynamoDB.describeTable(request.getTableName()); if (!request.getKeySchema().equals(result.getTable().getKeySchema())) { throw new IllegalStateException("Table " + request.getTableName() + " already exists and has an invalid schema"); } LOGGER.warning("Table " + request.getTableName() + " already exists"); return result.getTable(); } catch (final ResourceNotFoundException e) { return dynamoDB.createTable(request).getTableDescription(); } } 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 testCreateTableTableDoesNotExist() { final Collection<AttributeDefinition> ads = Arrays.asList(new AttributeDefinition("Hash", ScalarAttributeType.S)); final Collection<KeySchemaElement> kses = Arrays.asList(new KeySchemaElement("Hash", KeyType.HASH)); final TableDescription description = new TableDescription().withAttributeDefinitions(ads).withKeySchema(kses) .withTableName(tableName); final CreateTableResult cTR = new CreateTableResult().withTableDescription(description); EasyMock.expect(dynamoDB.describeTable(tableName)).andThrow(new ResourceNotFoundException(null)); final CreateTableRequest request = new CreateTableRequest().withAttributeDefinitions(ads).withKeySchema(kses) .withTableName(tableName); EasyMock.expect(dynamoDB.createTable(request)).andReturn(cTR); PowerMock.replayAll(); assertEquals(description, DynamoDBManager.createTable(dynamoDB, request)); PowerMock.verifyAll(); }
@Test public void testCreateTableTableAlreadyExistsCorrectKeySchema() { final Collection<AttributeDefinition> ads = Arrays.asList(new AttributeDefinition("Hash", ScalarAttributeType.S)); final Collection<KeySchemaElement> kses = Arrays.asList(new KeySchemaElement("Hash", KeyType.HASH)); final TableDescription description = new TableDescription().withAttributeDefinitions(ads).withKeySchema(kses) .withTableName(tableName); final DescribeTableResult result = new DescribeTableResult().withTable(description); EasyMock.expect(dynamoDB.describeTable(tableName)).andReturn(result); final CreateTableRequest request = new CreateTableRequest().withAttributeDefinitions(ads).withKeySchema(kses) .withTableName(tableName); PowerMock.replayAll(); assertEquals(description, DynamoDBManager.createTable(dynamoDB, request)); PowerMock.verifyAll(); }
@Test(expected = IllegalStateException.class) public void testCreateTableTableAlreadyExistsIncorrectKeySchema() { final Collection<AttributeDefinition> ads = Arrays.asList(new AttributeDefinition("Hash", ScalarAttributeType.S)); final Collection<KeySchemaElement> kses = Arrays.asList(new KeySchemaElement("Hash", KeyType.HASH)); final TableDescription description = new TableDescription().withAttributeDefinitions(ads).withKeySchema(kses) .withTableName(tableName); final DescribeTableResult result = new DescribeTableResult().withTable(description); EasyMock.expect(dynamoDB.describeTable(tableName)).andReturn(result); final Collection<AttributeDefinition> ads2 = Arrays.asList(new AttributeDefinition("Hash2", ScalarAttributeType.S)); final Collection<KeySchemaElement> kses2 = Arrays.asList(new KeySchemaElement("Hash2", KeyType.HASH)); final CreateTableRequest request = new CreateTableRequest().withAttributeDefinitions(ads2).withKeySchema(kses2) .withTableName(tableName); PowerMock.replayAll(); DynamoDBManager.createTable(dynamoDB, request); }