Skip to content

Commit 8611acd

Browse files
committed
Add some E2E testing
1 parent 09855f9 commit 8611acd

File tree

2 files changed

+100
-6
lines changed

2 files changed

+100
-6
lines changed

powertools-parameters/src/main/java/software/amazon/lambda/powertools/parameters/DynamoDBProvider.java

+11-6
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public class DynamoDBProvider extends BaseProvider {
2020
public DynamoDBProvider(CacheManager cacheManager, String tableName) {
2121
this(cacheManager, DynamoDbClient.builder()
2222
.httpClientBuilder(UrlConnectionHttpClient.builder())
23-
.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
24-
.region(Region.of(System.getenv(SdkSystemSetting.AWS_REGION.environmentVariable())))
23+
//.credentialsProvider(EnvironmentVariableCredentialsProvider.create())
24+
//.region(Region.of(System.getenv(SdkSystemSetting.AWS_REGION.environmentVariable())))
2525
.build(),
2626
tableName
2727
);
@@ -42,8 +42,14 @@ protected String getValue(String key) {
4242
.attributesToGet("val")
4343
.build());
4444

45-
// TODO
46-
return resp.item().values().stream().findFirst().get().s();
45+
// If we have an item at the key, we should be able to get a 'val' out of it. If not it's
46+
// exceptional.
47+
// If we don't have an item at the key, we should return null.
48+
if (resp.hasItem() && !resp.item().values().isEmpty()) {
49+
return resp.item().values().stream().findFirst().get().s();
50+
}
51+
52+
return null;
4753
}
4854

4955
@Override
@@ -52,8 +58,7 @@ protected Map<String, String> getMultipleValues(String path) {
5258
QueryResponse resp = client.query(QueryRequest.builder()
5359
.tableName(tableName)
5460
.keyConditionExpression("id = :v_id")
55-
.expressionAttributeValues(Collections.singletonMap("v_id", AttributeValue.fromS(path)))
56-
.attributesToGet("sk", "value")
61+
.expressionAttributeValues(Collections.singletonMap(":v_id", AttributeValue.fromS(path)))
5762
.build());
5863

5964
return resp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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

Comments
 (0)