|
20 | 20 | import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient; |
21 | 21 | import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest; |
22 | 22 | import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueResponse; |
| 23 | +import software.amazon.awssdk.services.secretsmanager.model.PutSecretValueRequest; |
| 24 | +import software.amazon.awssdk.services.secretsmanager.model.PutSecretValueResponse; |
23 | 25 |
|
24 | 26 | import java.util.Map; |
25 | 27 | import java.util.UUID; |
26 | 28 |
|
27 | 29 | import static org.hamcrest.CoreMatchers.equalTo; |
28 | 30 | import static org.hamcrest.MatcherAssert.assertThat; |
| 31 | +import static org.mockito.ArgumentMatchers.any; |
29 | 32 | import static org.mockito.ArgumentMatchers.eq; |
30 | 33 | import static org.mockito.Mockito.never; |
31 | 34 | import static org.mockito.Mockito.times; |
@@ -61,6 +64,12 @@ class AwsSecretsSupplierLazyLoadTest { |
61 | 64 | @Mock |
62 | 65 | private GetSecretValueResponse getSecretValueResponse; |
63 | 66 |
|
| 67 | + @Mock |
| 68 | + private PutSecretValueRequest putSecretValueRequest; |
| 69 | + |
| 70 | + @Mock |
| 71 | + private PutSecretValueResponse putSecretValueResponse; |
| 72 | + |
64 | 73 | @Mock |
65 | 74 | private AwsCredentialsSupplier awsCredentialsSupplier; |
66 | 75 |
|
@@ -131,4 +140,37 @@ void testSecretWithSkipValidationOnStartFalse_LoadsAtConstruction() throws JsonP |
131 | 140 | verify(secretsManagerClient, times(1)).getSecretValue(eq(getSecretValueRequest)); |
132 | 141 | assertThat(value, equalTo(testValue)); |
133 | 142 | } |
| 143 | + |
| 144 | + @Test |
| 145 | + void testUpdateValue_withSkipValidationOnStart_loadsSecretBeforeUpdate() throws JsonProcessingException { |
| 146 | + // Given: Secret configured with skip_validation_on_start=true |
| 147 | + when(awsSecretPluginConfig.getAwsSecretManagerConfigurationMap()).thenReturn( |
| 148 | + Map.of(testSecretId, awsSecretManagerConfiguration) |
| 149 | + ); |
| 150 | + when(awsSecretManagerConfiguration.isSkipValidationOnStart()).thenReturn(true); |
| 151 | + when(awsSecretManagerConfiguration.createSecretManagerClient(awsCredentialsSupplier)).thenReturn(secretsManagerClient); |
| 152 | + when(awsSecretManagerConfiguration.createGetSecretValueRequest()).thenReturn(getSecretValueRequest); |
| 153 | + when(secretValueDecoder.decode(eq(getSecretValueResponse))).thenReturn(objectMapper.writeValueAsString( |
| 154 | + Map.of(testKey, testValue) |
| 155 | + )); |
| 156 | + when(secretsManagerClient.getSecretValue(eq(getSecretValueRequest))).thenReturn(getSecretValueResponse); |
| 157 | + when(awsSecretManagerConfiguration.putSecretValueRequest(any())).thenReturn(putSecretValueRequest); |
| 158 | + when(secretsManagerClient.putSecretValue(eq(putSecretValueRequest))).thenReturn(putSecretValueResponse); |
| 159 | + final String newVersionId = UUID.randomUUID().toString(); |
| 160 | + when(putSecretValueResponse.versionId()).thenReturn(newVersionId); |
| 161 | + |
| 162 | + final AwsSecretsSupplier supplier = new AwsSecretsSupplier( |
| 163 | + secretValueDecoder, awsSecretPluginConfig, objectMapper, awsCredentialsSupplier |
| 164 | + ); |
| 165 | + |
| 166 | + // Then: Secret is NOT retrieved at construction time |
| 167 | + verify(secretsManagerClient, never()).getSecretValue(eq(getSecretValueRequest)); |
| 168 | + |
| 169 | + // When: updateValue is called before any retrieveValue |
| 170 | + final String versionId = supplier.updateValue(testSecretId, testKey, "newValue"); |
| 171 | + |
| 172 | + // Then: Secret was loaded on-demand and update succeeded |
| 173 | + verify(secretsManagerClient, times(1)).getSecretValue(eq(getSecretValueRequest)); |
| 174 | + assertThat(versionId, equalTo(newVersionId)); |
| 175 | + } |
134 | 176 | } |
0 commit comments