Skip to content

Commit

Permalink
BAEL-681 Configuring DynamoDB locally for integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
knightdna committed Feb 14, 2018
1 parent 4f6988b commit b8f4338
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 4 deletions.
79 changes: 79 additions & 0 deletions persistence-modules/spring-data-dynamodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
<spring-data-dynamodb.version>4.4.1</spring-data-dynamodb.version>
<aws-java-sdk-dynamodb.version>1.11.64</aws-java-sdk-dynamodb.version>
<bootstrap.version>3.3.7-1</bootstrap.version>
<sqlite4java.version>1.0.392</sqlite4java.version>
<dynamodb.version>1.11.106</dynamodb.version>
<dynamodblocal.version>1.11.86</dynamodblocal.version>
<dynamodblocal.repository.url>https://s3.eu-central-1.amazonaws.com/dynamodb-local-frankfurt/release</dynamodblocal.repository.url>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -103,6 +107,57 @@
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
<!-- ================================================== -->
<!-- DynamoDBLocal dependencies -->
<!-- ================================================== -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>DynamoDBLocal</artifactId>
<version>${dynamodblocal.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.almworks.sqlite4java</groupId>
<artifactId>sqlite4java</artifactId>
<version>${sqlite4java.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.almworks.sqlite4java</groupId>
<artifactId>sqlite4java-win32-x86</artifactId>
<version>${sqlite4java.version}</version>
<type>dll</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.almworks.sqlite4java</groupId>
<artifactId>sqlite4java-win32-x64</artifactId>
<version>${sqlite4java.version}</version>
<type>dll</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.almworks.sqlite4java</groupId>
<artifactId>libsqlite4java-osx</artifactId>
<version>${sqlite4java.version}</version>
<type>dylib</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.almworks.sqlite4java</groupId>
<artifactId>libsqlite4java-linux-i386</artifactId>
<version>${sqlite4java.version}</version>
<type>so</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.almworks.sqlite4java</groupId>
<artifactId>libsqlite4java-linux-amd64</artifactId>
<version>${sqlite4java.version}</version>
<type>so</type>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand All @@ -120,6 +175,25 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>test-compile</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeScope>test</includeScope>
<includeTypes>so,dll,dylib</includeTypes>
<outputDirectory>${project.basedir}/native-libs</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

</plugins>

Expand All @@ -142,6 +216,11 @@
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>dynamodb-local</id>
<name>DynamoDB Local Release Repository</name>
<url>${dynamodblocal.repository.url}</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import com.baeldung.Application;
import com.baeldung.spring.data.dynamodb.model.ProductInfo;
import com.baeldung.spring.data.dynamodb.repositories.ProductInfoRepository;
import com.baeldung.spring.data.dynamodb.repository.rule.LocalDynamoDBCreationRule;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -30,6 +31,9 @@
@TestPropertySource(properties = { "amazon.dynamodb.endpoint=http://localhost:8000/", "amazon.aws.accesskey=test1", "amazon.aws.secretkey=test231" })
public class ProductInfoRepositoryIntegrationTest {

@ClassRule
public static LocalDynamoDBCreationRule dynamoDB = new LocalDynamoDBCreationRule();

private DynamoDBMapper dynamoDBMapper;

@Autowired
Expand All @@ -42,7 +46,6 @@ public class ProductInfoRepositoryIntegrationTest {
private static final String EXPECTED_PRICE = "50";

@Before
@Ignore // TODO Remove Ignore annotations when running locally with Local DynamoDB instance
public void setup() throws Exception {

try {
Expand All @@ -57,12 +60,11 @@ public void setup() throws Exception {
// Do nothing, table already created
}

// TODO How to handle different environments. i.e. AVOID deleting all entries in ProductInfoion table
// TODO How to handle different environments. i.e. AVOID deleting all entries in ProductInfo on table
dynamoDBMapper.batchDelete((List<ProductInfo>) repository.findAll());
}

@Test
@Ignore // TODO Remove Ignore annotations when running locally with Local DynamoDB instance
public void givenItemWithExpectedCost_whenRunFindAll_thenItemIsFound() {

ProductInfo productInfo = new ProductInfo(EXPECTED_COST, EXPECTED_PRICE);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.baeldung.spring.data.dynamodb.repository.rule;

import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.local.main.ServerRunner;
import com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer;
import org.junit.rules.ExternalResource;
import org.springframework.beans.factory.annotation.Autowired;

public class LocalDynamoDBCreationRule extends ExternalResource {

protected DynamoDBProxyServer server;
protected AmazonDynamoDB amazonDynamoDB;

@Override
protected void before() throws Exception {
System.setProperty("sqlite4java.library.path", "native-libs");
String port = "8000";
this.server = ServerRunner.createServerFromCommandLineArgs(new String[]{"-inMemory", "-port", port});
server.start();
}

@Override
protected void after() {
try {
server.stop();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Autowired
public void setAmazonDynamoDB(AmazonDynamoDB amazonDynamoDB) {
this.amazonDynamoDB = amazonDynamoDB;
}

}

0 comments on commit b8f4338

Please sign in to comment.