|
20 | 20 | import com.amazonaws.services.secretsmanager.model.GetSecretValueRequest; |
21 | 21 | import com.amazonaws.services.secretsmanager.model.GetSecretValueResult; |
22 | 22 | import com.amazonaws.services.secretsmanager.model.ResourceNotFoundException; |
| 23 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 24 | +import org.junit.jupiter.api.BeforeEach; |
23 | 25 | import org.junit.jupiter.api.Test; |
| 26 | +import org.mockito.ArgumentCaptor; |
24 | 27 |
|
25 | 28 | import static org.assertj.core.api.Assertions.assertThat; |
26 | 29 | import static org.assertj.core.api.Assertions.assertThatThrownBy; |
|
30 | 33 |
|
31 | 34 | class AwsSecretsManagerPropertySourceTest { |
32 | 35 |
|
33 | | - private AWSSecretsManager client = mock(AWSSecretsManager.class); |
| 36 | + private AWSSecretsManager client; |
34 | 37 |
|
35 | | - private AwsSecretsManagerPropertySource propertySource = new AwsSecretsManagerPropertySource("/config/myservice", |
36 | | - client); |
| 38 | + private AwsSecretsManagerPropertySource propertySource; |
| 39 | + |
| 40 | + private ArgumentCaptor<GetSecretValueRequest> secretValueRequestArgumentCaptor; |
| 41 | + |
| 42 | + @BeforeEach |
| 43 | + void setUp() { |
| 44 | + client = mock(AWSSecretsManager.class); |
| 45 | + secretValueRequestArgumentCaptor = ArgumentCaptor.forClass(GetSecretValueRequest.class); |
| 46 | + propertySource = new AwsSecretsManagerPropertySource("/config/myservice", client); |
| 47 | + } |
37 | 48 |
|
38 | 49 | @Test |
39 | 50 | void shouldParseSecretValue() { |
40 | | - GetSecretValueResult secretValueResult = new GetSecretValueResult(); |
41 | | - secretValueResult.setSecretString("{\"key1\": \"value1\", \"key2\": \"value2\"}"); |
| 51 | + GetSecretValueResult secretValueResult = new GetSecretValueResult() |
| 52 | + .withSecretString("{\"key1\": \"value1\", \"key2\": \"value2\"}"); |
42 | 53 |
|
43 | | - when(client.getSecretValue(any(GetSecretValueRequest.class))).thenReturn(secretValueResult); |
| 54 | + when(client.getSecretValue(secretValueRequestArgumentCaptor.capture())).thenReturn(secretValueResult); |
44 | 55 |
|
45 | 56 | propertySource.init(); |
46 | 57 |
|
| 58 | + assertThat(secretValueRequestArgumentCaptor.getValue().getSecretId()).isEqualTo("/config/myservice"); |
47 | 59 | assertThat(propertySource.getPropertyNames()).containsExactly("key1", "key2"); |
48 | 60 | assertThat(propertySource.getProperty("key1")).isEqualTo("value1"); |
49 | 61 | assertThat(propertySource.getProperty("key2")).isEqualTo("value2"); |
50 | 62 | } |
51 | 63 |
|
| 64 | + @Test |
| 65 | + void shouldAppendPrefixIfPrefixConfigured() { |
| 66 | + propertySource = new AwsSecretsManagerPropertySource("/config/myservice2?prefix=service2.", client); |
| 67 | + GetSecretValueResult secretValueResult = new GetSecretValueResult() |
| 68 | + .withSecretString("{\"key1\": \"value1\", \"key2\": \"value2\"}"); |
| 69 | + |
| 70 | + when(client.getSecretValue(secretValueRequestArgumentCaptor.capture())).thenReturn(secretValueResult); |
| 71 | + |
| 72 | + propertySource.init(); |
| 73 | + |
| 74 | + assertThat(secretValueRequestArgumentCaptor.getValue().getSecretId()).isEqualTo("/config/myservice2"); |
| 75 | + assertThat(propertySource.getPropertyNames()).containsExactly("service2.key1", "service2.key2"); |
| 76 | + assertThat(propertySource.getProperty("service2.key1")).isEqualTo("value1"); |
| 77 | + assertThat(propertySource.getProperty("service2.key2")).isEqualTo("value2"); |
| 78 | + } |
| 79 | + |
52 | 80 | @Test |
53 | 81 | void throwsExceptionWhenSecretNotFound() { |
54 | 82 | when(client.getSecretValue(any(GetSecretValueRequest.class))) |
55 | 83 | .thenThrow(new ResourceNotFoundException("secret not found")); |
56 | 84 |
|
57 | | - assertThatThrownBy(() -> propertySource.init()).isInstanceOf(ResourceNotFoundException.class); |
| 85 | + assertThatThrownBy(() -> propertySource.init()).isInstanceOf(ResourceNotFoundException.class) |
| 86 | + .hasMessageContaining("secret not found"); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void throwsExceptionWhenSecretIsNotJsonSecret() { |
| 91 | + GetSecretValueResult secretValueResult = new GetSecretValueResult() |
| 92 | + .withSecretString("plain text secret string, not json secret"); |
| 93 | + when(client.getSecretValue(any(GetSecretValueRequest.class))).thenReturn(secretValueResult); |
| 94 | + |
| 95 | + assertThatThrownBy(() -> propertySource.init()).isInstanceOf(RuntimeException.class) |
| 96 | + .extracting(Throwable::getCause).isInstanceOf(JsonProcessingException.class); |
58 | 97 | } |
59 | 98 |
|
60 | 99 | } |
0 commit comments