Skip to content

Commit b8f4338

Browse files
committed
BAEL-681 Configuring DynamoDB locally for integration tests
1 parent 4f6988b commit b8f4338

3 files changed

Lines changed: 121 additions & 4 deletions

File tree

‎persistence-modules/spring-data-dynamodb/pom.xml‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
<spring-data-dynamodb.version>4.4.1</spring-data-dynamodb.version>
2424
<aws-java-sdk-dynamodb.version>1.11.64</aws-java-sdk-dynamodb.version>
2525
<bootstrap.version>3.3.7-1</bootstrap.version>
26+
<sqlite4java.version>1.0.392</sqlite4java.version>
27+
<dynamodb.version>1.11.106</dynamodb.version>
28+
<dynamodblocal.version>1.11.86</dynamodblocal.version>
29+
<dynamodblocal.repository.url>https://s3.eu-central-1.amazonaws.com/dynamodb-local-frankfurt/release</dynamodblocal.repository.url>
2630
</properties>
2731

2832
<dependencyManagement>
@@ -103,6 +107,57 @@
103107
<artifactId>httpclient</artifactId>
104108
<version>${httpclient.version}</version>
105109
</dependency>
110+
<!-- ================================================== -->
111+
<!-- DynamoDBLocal dependencies -->
112+
<!-- ================================================== -->
113+
<dependency>
114+
<groupId>com.amazonaws</groupId>
115+
<artifactId>DynamoDBLocal</artifactId>
116+
<version>${dynamodblocal.version}</version>
117+
<scope>test</scope>
118+
</dependency>
119+
<dependency>
120+
<groupId>com.almworks.sqlite4java</groupId>
121+
<artifactId>sqlite4java</artifactId>
122+
<version>${sqlite4java.version}</version>
123+
<scope>test</scope>
124+
</dependency>
125+
<dependency>
126+
<groupId>com.almworks.sqlite4java</groupId>
127+
<artifactId>sqlite4java-win32-x86</artifactId>
128+
<version>${sqlite4java.version}</version>
129+
<type>dll</type>
130+
<scope>test</scope>
131+
</dependency>
132+
<dependency>
133+
<groupId>com.almworks.sqlite4java</groupId>
134+
<artifactId>sqlite4java-win32-x64</artifactId>
135+
<version>${sqlite4java.version}</version>
136+
<type>dll</type>
137+
<scope>test</scope>
138+
</dependency>
139+
<dependency>
140+
<groupId>com.almworks.sqlite4java</groupId>
141+
<artifactId>libsqlite4java-osx</artifactId>
142+
<version>${sqlite4java.version}</version>
143+
<type>dylib</type>
144+
<scope>test</scope>
145+
</dependency>
146+
<dependency>
147+
<groupId>com.almworks.sqlite4java</groupId>
148+
<artifactId>libsqlite4java-linux-i386</artifactId>
149+
<version>${sqlite4java.version}</version>
150+
<type>so</type>
151+
<scope>test</scope>
152+
</dependency>
153+
<dependency>
154+
<groupId>com.almworks.sqlite4java</groupId>
155+
<artifactId>libsqlite4java-linux-amd64</artifactId>
156+
<version>${sqlite4java.version}</version>
157+
<type>so</type>
158+
<scope>test</scope>
159+
</dependency>
160+
106161
</dependencies>
107162

108163
<build>
@@ -120,6 +175,25 @@
120175
<groupId>org.apache.maven.plugins</groupId>
121176
<artifactId>maven-war-plugin</artifactId>
122177
</plugin>
178+
<plugin>
179+
<groupId>org.apache.maven.plugins</groupId>
180+
<artifactId>maven-dependency-plugin</artifactId>
181+
<version>2.10</version>
182+
<executions>
183+
<execution>
184+
<id>copy-dependencies</id>
185+
<phase>test-compile</phase>
186+
<goals>
187+
<goal>copy-dependencies</goal>
188+
</goals>
189+
<configuration>
190+
<includeScope>test</includeScope>
191+
<includeTypes>so,dll,dylib</includeTypes>
192+
<outputDirectory>${project.basedir}/native-libs</outputDirectory>
193+
</configuration>
194+
</execution>
195+
</executions>
196+
</plugin>
123197

124198
</plugins>
125199

@@ -142,6 +216,11 @@
142216
<enabled>false</enabled>
143217
</snapshots>
144218
</repository>
219+
<repository>
220+
<id>dynamodb-local</id>
221+
<name>DynamoDB Local Release Repository</name>
222+
<url>${dynamodblocal.repository.url}</url>
223+
</repository>
145224
</repositories>
146225
<pluginRepositories>
147226
<pluginRepository>

‎persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
import com.baeldung.Application;
99
import com.baeldung.spring.data.dynamodb.model.ProductInfo;
1010
import com.baeldung.spring.data.dynamodb.repositories.ProductInfoRepository;
11+
import com.baeldung.spring.data.dynamodb.repository.rule.LocalDynamoDBCreationRule;
1112
import org.junit.Before;
12-
import org.junit.Ignore;
13+
import org.junit.ClassRule;
1314
import org.junit.Test;
1415
import org.junit.runner.RunWith;
1516
import org.springframework.beans.factory.annotation.Autowired;
@@ -30,6 +31,9 @@
3031
@TestPropertySource(properties = { "amazon.dynamodb.endpoint=http://localhost:8000/", "amazon.aws.accesskey=test1", "amazon.aws.secretkey=test231" })
3132
public class ProductInfoRepositoryIntegrationTest {
3233

34+
@ClassRule
35+
public static LocalDynamoDBCreationRule dynamoDB = new LocalDynamoDBCreationRule();
36+
3337
private DynamoDBMapper dynamoDBMapper;
3438

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

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

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

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

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

6870
ProductInfo productInfo = new ProductInfo(EXPECTED_COST, EXPECTED_PRICE);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.spring.data.dynamodb.repository.rule;
2+
3+
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
4+
import com.amazonaws.services.dynamodbv2.local.main.ServerRunner;
5+
import com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer;
6+
import org.junit.rules.ExternalResource;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
9+
public class LocalDynamoDBCreationRule extends ExternalResource {
10+
11+
protected DynamoDBProxyServer server;
12+
protected AmazonDynamoDB amazonDynamoDB;
13+
14+
@Override
15+
protected void before() throws Exception {
16+
System.setProperty("sqlite4java.library.path", "native-libs");
17+
String port = "8000";
18+
this.server = ServerRunner.createServerFromCommandLineArgs(new String[]{"-inMemory", "-port", port});
19+
server.start();
20+
}
21+
22+
@Override
23+
protected void after() {
24+
try {
25+
server.stop();
26+
} catch (Exception e) {
27+
throw new RuntimeException(e);
28+
}
29+
}
30+
31+
@Autowired
32+
public void setAmazonDynamoDB(AmazonDynamoDB amazonDynamoDB) {
33+
this.amazonDynamoDB = amazonDynamoDB;
34+
}
35+
36+
}

0 commit comments

Comments
 (0)