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