|
| 1 | +package software.amazon.lambda.powertools.parameters; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Disabled; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | +import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient; |
| 6 | +import software.amazon.awssdk.services.dynamodb.DynamoDbClient; |
| 7 | +import software.amazon.awssdk.services.dynamodb.model.AttributeValue; |
| 8 | +import software.amazon.awssdk.services.dynamodb.model.PutItemRequest; |
| 9 | +import software.amazon.lambda.powertools.parameters.cache.CacheManager; |
| 10 | + |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +import static org.assertj.core.api.Assertions.assertThat; |
| 15 | + |
| 16 | +/** |
| 17 | + * This class provides simple end-to-end style testing of the DynamoDBProvider class. |
| 18 | + * It is ignored, for now, as it requires AWS access and that's not yet run as part |
| 19 | + * of our unit test suite in the cloud. |
| 20 | + * |
| 21 | + * The test is kept here for 1/ local development and 2/ in preparation for future |
| 22 | + * E2E tests running in the cloud CI. |
| 23 | + */ |
| 24 | +@Disabled |
| 25 | +public class DynamoDBProviderE2ETest { |
| 26 | + |
| 27 | + final String ParamsTestTable = "ddb-params-test"; |
| 28 | + final String MultiparamsTestTable = "ddb-multiparams-test"; |
| 29 | + private DynamoDbClient ddbClient; |
| 30 | + |
| 31 | + public DynamoDBProviderE2ETest() { |
| 32 | + // Create a DDB client to inject test data into our test tables |
| 33 | + ddbClient = DynamoDbClient.builder() |
| 34 | + .httpClientBuilder(UrlConnectionHttpClient.builder()) |
| 35 | + .build(); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void TestGetValue() { |
| 40 | + |
| 41 | + // Arrange |
| 42 | + HashMap<String, AttributeValue> testItem = new HashMap<String, AttributeValue>(); |
| 43 | + testItem.put("id", AttributeValue.fromS("test_param")); |
| 44 | + testItem.put("val", AttributeValue.fromS("the_value_is_hello!")); |
| 45 | + ddbClient.putItem(PutItemRequest.builder() |
| 46 | + .tableName(ParamsTestTable) |
| 47 | + .item(testItem) |
| 48 | + .build()); |
| 49 | + |
| 50 | + // Act |
| 51 | + DynamoDBProvider provider = new DynamoDBProvider(new CacheManager(), ParamsTestTable); |
| 52 | + |
| 53 | + // Assert |
| 54 | + String value = provider.getValue("test_param"); |
| 55 | + assertThat(value).isEqualTo("the_value_is_hello!"); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void TestGetValues() { |
| 60 | + |
| 61 | + // Arrange |
| 62 | + HashMap<String, AttributeValue> testItem = new HashMap<String, AttributeValue>(); |
| 63 | + testItem.put("id", AttributeValue.fromS("test_param")); |
| 64 | + testItem.put("sk", AttributeValue.fromS("test_param_part_1")); |
| 65 | + testItem.put("val", AttributeValue.fromS("the_value_is_hello!")); |
| 66 | + ddbClient.putItem(PutItemRequest.builder() |
| 67 | + .tableName(MultiparamsTestTable) |
| 68 | + .item(testItem) |
| 69 | + .build()); |
| 70 | + |
| 71 | + HashMap<String, AttributeValue> testItem2 = new HashMap<String, AttributeValue>(); |
| 72 | + testItem2.put("id", AttributeValue.fromS("test_param")); |
| 73 | + testItem2.put("sk", AttributeValue.fromS("test_param_part_2")); |
| 74 | + testItem2.put("val", AttributeValue.fromS("the_value_is_still_hello!")); |
| 75 | + ddbClient.putItem(PutItemRequest.builder() |
| 76 | + .tableName(MultiparamsTestTable) |
| 77 | + .item(testItem2) |
| 78 | + .build()); |
| 79 | + |
| 80 | + // Act |
| 81 | + DynamoDBProvider provider = new DynamoDBProvider(new CacheManager(), MultiparamsTestTable); |
| 82 | + Map<String, String> values = provider.getMultipleValues("test_param"); |
| 83 | + |
| 84 | + // Assert |
| 85 | + assertThat(values.size()).isEqualTo(2); |
| 86 | + assertThat(values.get("test_param_part_1")).isEqualTo("the_value_is_hello!"); |
| 87 | + assertThat(values.get("test_param_part_2")).isEqualTo("the_value_is_still_hello!"); |
| 88 | + } |
| 89 | +} |
0 commit comments