### Question:
AWSKeyVaultService implements KeyVaultService<AWSSetSecretData, AWSGetSecretData> { @Override public String getSecret(AWSGetSecretData getSecretData) { GetSecretValueRequest getSecretValueRequest = GetSecretValueRequest.builder() .secretId(getSecretData.getSecretName()) .build(); GetSecretValueResponse secretValueResponse; try { secretValueResponse = secretsManager.getSecretValue(getSecretValueRequest); } catch (ResourceNotFoundException e) { throw new VaultSecretNotFoundException( "The requested secret '" + getSecretData.getSecretName() + "' was not found in AWS Secrets Manager"); } catch (InvalidRequestException | InvalidParameterException e) { throw new AWSSecretsManagerException(e); } if (secretValueResponse != null && secretValueResponse.secretString() != null) { return secretValueResponse.secretString(); } throw new VaultSecretNotFoundException( "The requested secret '" + getSecretData.getSecretName() + "' was not found in AWS Secrets Manager"); } AWSKeyVaultService(SecretsManagerClient secretsManager); @Override String getSecret(AWSGetSecretData getSecretData); @Override Object setSecret(AWSSetSecretData setSecretData);  }### Answer:
@Test public void getSecret() { String secretName = "name"; AWSGetSecretData getSecretData = mock(AWSGetSecretData.class); when(getSecretData.getSecretName()).thenReturn(secretName); GetSecretValueResponse secretValueResponse = GetSecretValueResponse.builder().secretString("secret").build(); when(secretsManager.getSecretValue(Mockito.any(GetSecretValueRequest.class))).thenReturn(secretValueResponse); assertThat(keyVaultService.getSecret(getSecretData)).isEqualTo("secret"); }
@Test public void getSecretThrowsExceptionIfSecretReturnedIsNull() { String secretName = "secret"; AWSGetSecretData getSecretData = mock(AWSGetSecretData.class); when(getSecretData.getSecretName()).thenReturn(secretName); Throwable throwable = catchThrowable(() -> keyVaultService.getSecret(getSecretData)); assertThat(throwable).isInstanceOf(VaultSecretNotFoundException.class); assertThat(throwable) .hasMessageContaining("The requested secret '" + secretName + "' was not found in AWS Secrets Manager"); }
@Test public void getSecretThrowsExceptionIfKeyNotFoundInVault() { String secretName = "secret"; AWSGetSecretData getSecretData = mock(AWSGetSecretData.class); when(getSecretData.getSecretName()).thenReturn(secretName); when(secretsManager.getSecretValue(Mockito.any(GetSecretValueRequest.class))) .thenThrow(ResourceNotFoundException.builder().build()); Throwable throwable = catchThrowable(() -> keyVaultService.getSecret(getSecretData)); assertThat(throwable).isInstanceOf(VaultSecretNotFoundException.class); assertThat(throwable) .hasMessageContaining("The requested secret '" + secretName + "' was not found in AWS Secrets Manager"); }
@Test public void getSecretThrowsExceptionIfAWSException() { String secretName = "secret"; AWSGetSecretData getSecretData = mock(AWSGetSecretData.class); when(getSecretData.getSecretName()).thenReturn(secretName); when(secretsManager.getSecretValue(Mockito.any(GetSecretValueRequest.class))) .thenThrow(InvalidParameterException.builder().build()); Throwable throwable = catchThrowable(() -> keyVaultService.getSecret(getSecretData)); assertThat(throwable).isInstanceOf(AWSSecretsManagerException.class); }