|
| 1 | +package com.baeldung.dynamodb; |
| 2 | + |
| 3 | +import com.amazonaws.auth.BasicAWSCredentials; |
| 4 | +import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; |
| 5 | +import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient; |
| 6 | +import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper; |
| 7 | +import com.amazonaws.services.dynamodbv2.model.CreateTableRequest; |
| 8 | +import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput; |
| 9 | +import com.amazonaws.services.dynamodbv2.model.ResourceInUseException; |
| 10 | +import com.baeldung.dynamodb.entity.ProductInfo; |
| 11 | +import com.baeldung.dynamodb.repository.ProductInfoRepository; |
| 12 | +import com.baeldung.dynamodb.rule.LocalDbCreationRule; |
| 13 | +import org.junit.Before; |
| 14 | +import org.junit.BeforeClass; |
| 15 | +import org.junit.ClassRule; |
| 16 | +import org.junit.Test; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.InputStream; |
| 20 | +import java.net.URISyntaxException; |
| 21 | +import java.nio.file.Files; |
| 22 | +import java.nio.file.Path; |
| 23 | +import java.nio.file.Paths; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Optional; |
| 26 | +import java.util.Properties; |
| 27 | + |
| 28 | +import static org.hamcrest.Matchers.greaterThan; |
| 29 | +import static org.hamcrest.core.Is.is; |
| 30 | +import static org.hamcrest.core.IsEqual.equalTo; |
| 31 | +import static org.junit.Assert.assertThat; |
| 32 | + |
| 33 | +public class ProductInfoRepositoryIntegrationTest { |
| 34 | + |
| 35 | + @ClassRule |
| 36 | + public static LocalDbCreationRule dynamoDB = new LocalDbCreationRule(); |
| 37 | + |
| 38 | + private static DynamoDBMapper dynamoDBMapper; |
| 39 | + private static AmazonDynamoDB amazonDynamoDB; |
| 40 | + |
| 41 | + private ProductInfoRepository repository; |
| 42 | + |
| 43 | + private static final String DYNAMODB_ENDPOINT = "amazon.dynamodb.endpoint"; |
| 44 | + private static final String AWS_ACCESSKEY = "amazon.aws.accesskey"; |
| 45 | + private static final String AWS_SECRETKEY = "amazon.aws.secretkey"; |
| 46 | + |
| 47 | + private static final String EXPECTED_COST = "20"; |
| 48 | + private static final String EXPECTED_PRICE = "50"; |
| 49 | + |
| 50 | + @BeforeClass |
| 51 | + public static void setupClass() { |
| 52 | + Properties testProperties = loadFromFileInClasspath("test.properties") |
| 53 | + .filter(properties -> !isEmpty(properties.getProperty(AWS_ACCESSKEY))) |
| 54 | + .filter(properties -> !isEmpty(properties.getProperty(AWS_SECRETKEY))) |
| 55 | + .filter(properties -> !isEmpty(properties.getProperty(DYNAMODB_ENDPOINT))) |
| 56 | + .orElseThrow(() -> new RuntimeException("Unable to get all of the required test property values")); |
| 57 | + |
| 58 | + String amazonAWSAccessKey = testProperties.getProperty(AWS_ACCESSKEY); |
| 59 | + String amazonAWSSecretKey = testProperties.getProperty(AWS_SECRETKEY); |
| 60 | + String amazonDynamoDBEndpoint = testProperties.getProperty(DYNAMODB_ENDPOINT); |
| 61 | + |
| 62 | + amazonDynamoDB = new AmazonDynamoDBClient(new BasicAWSCredentials(amazonAWSAccessKey, amazonAWSSecretKey)); |
| 63 | + amazonDynamoDB.setEndpoint(amazonDynamoDBEndpoint); |
| 64 | + dynamoDBMapper = new DynamoDBMapper(amazonDynamoDB); |
| 65 | + } |
| 66 | + |
| 67 | + @Before |
| 68 | + public void setup() { |
| 69 | + try { |
| 70 | + repository = new ProductInfoRepository(); |
| 71 | + repository.setMapper(dynamoDBMapper); |
| 72 | + |
| 73 | + CreateTableRequest tableRequest = dynamoDBMapper.generateCreateTableRequest(ProductInfo.class); |
| 74 | + |
| 75 | + tableRequest.setProvisionedThroughput(new ProvisionedThroughput(1L, 1L)); |
| 76 | + |
| 77 | + amazonDynamoDB.createTable(tableRequest); |
| 78 | + } catch (ResourceInUseException e) { |
| 79 | + // Do nothing, table already created |
| 80 | + } |
| 81 | + |
| 82 | + // TODO How to handle different environments. i.e. AVOID deleting all entries in ProductInfo on table |
| 83 | + dynamoDBMapper.batchDelete((List<ProductInfo>) repository.findAll()); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void givenItemWithExpectedCost_whenRunFindAll_thenItemIsFound() { |
| 88 | + |
| 89 | + ProductInfo productInfo = new ProductInfo(EXPECTED_COST, EXPECTED_PRICE); |
| 90 | + repository.save(productInfo); |
| 91 | + |
| 92 | + List<ProductInfo> result = (List<ProductInfo>) repository.findAll(); |
| 93 | + assertThat(result.size(), is(greaterThan(0))); |
| 94 | + assertThat(result.get(0).getCost(), is(equalTo(EXPECTED_COST))); |
| 95 | + } |
| 96 | + |
| 97 | + private static boolean isEmpty(String inputString) { |
| 98 | + return inputString == null || "".equals(inputString); |
| 99 | + } |
| 100 | + |
| 101 | + private static Optional<Properties> loadFromFileInClasspath(String fileName) { |
| 102 | + InputStream stream = null; |
| 103 | + try { |
| 104 | + Properties config = new Properties(); |
| 105 | + Path configLocation = Paths.get(ClassLoader.getSystemResource(fileName).toURI()); |
| 106 | + stream = Files.newInputStream(configLocation); |
| 107 | + config.load(stream); |
| 108 | + return Optional.of(config); |
| 109 | + } catch (Exception e) { |
| 110 | + return Optional.empty(); |
| 111 | + } finally { |
| 112 | + if (stream != null) { |
| 113 | + try { |
| 114 | + stream.close(); |
| 115 | + } catch (IOException e) { |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments