BAEL-681 Configuring DynamoDB locally for integration tests

This commit is contained in:
Diaz Novandi
2018-02-12 22:56:48 +01:00
parent 4f6988b134
commit b8f433859e
3 changed files with 121 additions and 4 deletions

View File

@@ -8,8 +8,9 @@ import com.amazonaws.services.dynamodbv2.model.ResourceInUseException;
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;
@@ -30,6 +31,9 @@ import static org.junit.Assert.assertTrue;
@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
@@ -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 {
@@ -57,12 +60,11 @@ public class ProductInfoRepositoryIntegrationTest {
// 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);

View File

@@ -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;
}
}