@Test public void testFindOne() throws Exception { AWSCredentials credentials = new BasicAWSCredentials("qwe", "qwe"); AmazonDynamoDB client = new AmazonDynamoDBClient(credentials); client.setEndpoint("http://localhost:3210"); DynamoDbClientFactory clientFactory = Mockito.mock(DynamoDbClientFactory.class); Mockito.when(clientFactory.client()).thenReturn(client); SQLite.setLibraryPath((this.getClass().getClassLoader().getResource("lib").getPath())); final String[] localArgs = {"-inMemory", "-port", "3210"}; DynamoDBProxyServer server = ServerRunner.createServerFromCommandLineArgs(localArgs); server.start(); log.info("tables: "); client.listTables().getTableNames().forEach(log::info); UUID uuid = UUID.randomUUID(); LeagueRecord record = LeagueRecord.builder() .id(uuid.toString()) .name("foosball singles") .teamSize(1).build(); LeagueDao instance = new LeagueDao(clientFactory); LeagueRecord created = instance.create(record); log.info("created league record:" + created.toString()); LeagueRecord actual = instance.findOne(uuid.toString()); Assert.assertEquals(record, actual); server.stop(); }
@Test public void testFindByType() throws Exception { AWSCredentials credentials = new BasicAWSCredentials("qwe", "qwe"); AmazonDynamoDB client = new AmazonDynamoDBClient(credentials); client.setEndpoint("http://localhost:3210"); DynamoDbClientFactory clientFactory = Mockito.mock(DynamoDbClientFactory.class); Mockito.when(clientFactory.client()).thenReturn(client); SQLite.setLibraryPath((this.getClass().getClassLoader().getResource("lib").getPath())); final String[] localArgs = {"-inMemory", "-port", "3210"}; DynamoDBProxyServer server = ServerRunner.createServerFromCommandLineArgs(localArgs); server.start(); log.info("tables: "); client.listTables().getTableNames().forEach(log::info); UUID uuid = UUID.randomUUID(); LeagueRecord record = LeagueRecord.builder() .id(uuid.toString()) .name("foosball singles") .gameType(GameType.FOOSBALL) .teamSize(1).build(); LeagueDao instance = new LeagueDao(clientFactory); LeagueRecord created = instance.create(record); log.info("created league record:" + created.toString()); List<LeagueRecord> actuals = instance.byType(record.getGameType()); LeagueRecord actual = actuals.get(0); log.info("expected: " + record.toString()); log.info("actual: " + actual.toString()); Assert.assertEquals("has same id", record.getId(), actual.getId()); Assert.assertNull("lookup does not have name data", actual.getName()); Assert.assertEquals("has same game type", record.getGameType(), actual.getGameType()); server.stop(); }
/** * You can use mvn to run DynamoDBLocalFixture, e.g. * <p> * $ mvn clean package * <p> * $ mvn exec:java -Dexec.mainClass="com.amazonaws.services.dynamodbv2.DynamoDBLocalFixture" \ * -Dexec.classpathScope="test" \ * -Dsqlite4java.library.path=target/dependencies * <p> * It's recommended to run "aws configure" one time before you run DynamoDBLocalFixture * * @param args - no args * @throws Exception */ public static void main(String[] args) throws Exception { AmazonDynamoDB dynamodb = null; try { // Create an in-memory and in-process instance of DynamoDB Local that skips HTTP dynamodb = DynamoDBEmbedded.create().amazonDynamoDB(); // use the DynamoDB API with DynamoDBEmbedded listTables(dynamodb.listTables(), "DynamoDB Embedded"); } finally { // Shutdown the thread pools in DynamoDB Local / Embedded if(dynamodb != null) { dynamodb.shutdown(); } } // Create an in-memory and in-process instance of DynamoDB Local that runs over HTTP final String[] localArgs = { "-inMemory" }; DynamoDBProxyServer server = null; try { server = ServerRunner.createServerFromCommandLineArgs(localArgs); server.start(); dynamodb = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration( // we can use any region here new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2")) .build(); // use the DynamoDB API over HTTP listTables(dynamodb.listTables(), "DynamoDB Local over HTTP"); } finally { // Stop the DynamoDB Local endpoint if(server != null) { server.stop(); } } }