Skip to content

Instantly share code, notes, and snippets.

@philgebhardt
Created January 31, 2016 03:02
Show Gist options
  • Save philgebhardt/7baf33534df5bb3a26a7 to your computer and use it in GitHub Desktop.
Save philgebhardt/7baf33534df5bb3a26a7 to your computer and use it in GitHub Desktop.
Testing DynamoDB integration locally with Local DynamoDB and TestNG
package org.foo.app;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.dynamodbv2.local.main.CommandLineInput;
import com.amazonaws.services.dynamodbv2.local.main.ServerRunner;
import com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer;
import com.amazonaws.services.dynamodbv2.model.*;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.util.ArrayList;
/**
* Unit test for simple App.
*/
public class DynamoDBTest
{
DynamoDBProxyServer server = null;
/*
Sets up the Local DynamoDB
*/
@BeforeClass
public void setup() throws Exception {
server = ServerRunner.createServer(new CommandLineInput(new String[] {
"-port", "8888",
"-dbPath", "target/db",
"sharedDb"
}));
server.start();
AmazonDynamoDBClient dynamodb = new AmazonDynamoDBClient();
dynamodb.setEndpoint("http://localhost:8000");
}
/*
Shuts down the Local DynamoDB
*/
@AfterClass
public void shutdown() throws Exception {
// Stop the DynamoDB Local endpoint
if(server != null) {
server.stop();
}
}
/*
Creates "foo-table" on the Local DynamoDB
*/
@Test
public void test() throws InterruptedException {
DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient(
new ProfileCredentialsProvider()));
ArrayList<AttributeDefinition> attributeDefinitions= new ArrayList<AttributeDefinition>();
attributeDefinitions.add(new AttributeDefinition().withAttributeName("Id").withAttributeType("N"));
ArrayList<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
keySchema.add(new KeySchemaElement().withAttributeName("Id").withKeyType(KeyType.HASH));
CreateTableRequest request = new CreateTableRequest()
.withTableName("foo-table")
.withKeySchema(keySchema)
.withAttributeDefinitions(attributeDefinitions)
.withProvisionedThroughput(new ProvisionedThroughput()
.withReadCapacityUnits(5L)
.withWriteCapacityUnits(6L));
Table table = dynamoDB.createTable(request);
table.waitForActive();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment