-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathDynamoDBProviderTest.java
131 lines (106 loc) · 4.45 KB
/
DynamoDBProviderTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package software.amazon.lambda.powertools.parameters;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.*;
import software.amazon.lambda.powertools.parameters.cache.CacheManager;
import java.util.HashMap;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.MockitoAnnotations.openMocks;
public class DynamoDBProviderTest {
@Mock
DynamoDbClient client;
@Captor
ArgumentCaptor<GetItemRequest> getItemValueCaptor;
@Captor
ArgumentCaptor<QueryRequest> queryRequestCaptor;
private DynamoDBProvider provider;
private final String tableName = "ddb-test-table";
@BeforeEach
public void init() {
openMocks(this);
CacheManager cacheManager = new CacheManager();
provider = new DynamoDBProvider(cacheManager, client, tableName);
}
@Test
public void getValue() {
// Arrange
String key = "Key1";
String expectedValue = "Value1";
HashMap<String, AttributeValue> responseData = new HashMap<String, AttributeValue>();
responseData.put("id", AttributeValue.fromS(key));
responseData.put("val", AttributeValue.fromS(expectedValue));
GetItemResponse response = GetItemResponse.builder()
.item(responseData)
.build();
Mockito.when(client.getItem(getItemValueCaptor.capture())).thenReturn(response);
// Act
String value = provider.getValue(key);
// Assert
assertThat(value).isEqualTo(expectedValue);
assertThat(getItemValueCaptor.getValue().tableName()).isEqualTo(tableName);
assertThat(getItemValueCaptor.getValue().key().get("id").s()).isEqualTo(key);
}
@Test
public void getValueWithoutResultsReturnsNull() {
// Arrange
Mockito.when(client.getItem(getItemValueCaptor.capture())).thenReturn(GetItemResponse.builder()
.item(null)
.build());
// Act
String value = provider.getValue("key");
// Assert
assertThat(value).isEqualTo(null);
}
@Test
public void getValueWithMalformedRowThrows() {
// Arrange
String key = "Key1";
HashMap<String, AttributeValue> responseData = new HashMap<String, AttributeValue>();
responseData.put("id", AttributeValue.fromS(key));
responseData.put("not-val", AttributeValue.fromS("something"));
Mockito.when(client.getItem(getItemValueCaptor.capture())).thenReturn(GetItemResponse.builder()
.item(responseData)
.build());
// Act
Assertions.assertThrows(NullPointerException.class, () -> {
String value = provider.getValue(key);
});
}
@Test
public void getValues() {
// Arrange
String key = "Key1";
String subkey1 = "Subkey1";
String val1 = "Val1";
String subkey2 = "Subkey2";
String val2 = "Val2";
HashMap<String, AttributeValue> item1 = new HashMap<String, AttributeValue>();
item1.put("id", AttributeValue.fromS(key));
item1.put("sk", AttributeValue.fromS(subkey1));
item1.put("val", AttributeValue.fromS(val1));
HashMap<String, AttributeValue> item2 = new HashMap<String, AttributeValue>();
item2.put("id", AttributeValue.fromS(key));
item2.put("sk", AttributeValue.fromS(subkey2));
item2.put("val", AttributeValue.fromS(val2));
QueryResponse response = QueryResponse.builder()
.items(item1, item2)
.build();
Mockito.when(client.query(queryRequestCaptor.capture())).thenReturn(response);
// Act
Map<String, String> values = provider.getMultipleValues(key);
// Assert
assertThat(values.size()).isEqualTo(2);
assertThat(values.get(subkey1)).isEqualTo(val1);
assertThat(values.get(subkey2)).isEqualTo(val2);
assertThat(queryRequestCaptor.getValue().tableName()).isEqualTo(tableName);
assertThat(queryRequestCaptor.getValue().keyConditionExpression()).isEqualTo("id = :v_id");
assertThat(queryRequestCaptor.getValue().expressionAttributeValues().get(":v_id").s()).isEqualTo(key);
}
}