### Question:
AzureKeyVaultService implements KeyVaultService<AzureSetSecretData, AzureGetSecretData> { @Override public String getSecret(AzureGetSecretData azureGetSecretData) { SecretBundle secretBundle; if (azureGetSecretData.getSecretVersion() != null) { secretBundle = azureKeyVaultClientDelegate.getSecret(vaultUrl, azureGetSecretData.getSecretName(), azureGetSecretData.getSecretVersion()); } else { secretBundle = azureKeyVaultClientDelegate.getSecret(vaultUrl, azureGetSecretData.getSecretName()); } if (secretBundle == null) { throw new VaultSecretNotFoundException("Azure Key Vault secret " + azureGetSecretData.getSecretName() + " was not found in vault " + vaultUrl); } return secretBundle.value(); } AzureKeyVaultService(KeyVaultConfig keyVaultConfig, AzureKeyVaultClientDelegate azureKeyVaultClientDelegate);   AzureKeyVaultService(String vaultUrl, AzureKeyVaultClientDelegate azureKeyVaultClientDelegate); @Override String getSecret(AzureGetSecretData azureGetSecretData); @Override Object setSecret(AzureSetSecretData azureSetSecretData);  }### Answer:
@Test public void getSecretGetsLatestVersionOfSecretIfNoVersionProvided() { String secretName = "name"; AzureGetSecretData getSecretData = mock(AzureGetSecretData.class); when(getSecretData.getSecretName()).thenReturn(secretName); when(getSecretData.getSecretVersion()).thenReturn(null); SecretBundle secretBundle = mock(SecretBundle.class); when(azureKeyVaultClientDelegate.getSecret(anyString(), anyString())).thenReturn(secretBundle); when(secretBundle.value()).thenReturn("value"); keyVaultService.getSecret(getSecretData); verify(azureKeyVaultClientDelegate).getSecret(vaultUrl, secretName); }
@Test public void getSecretGetsSpecificVersionOfSecretIfVersionProvided() { String secretName = "name"; String secretVersion = "version"; AzureGetSecretData getSecretData = mock(AzureGetSecretData.class); when(getSecretData.getSecretName()).thenReturn(secretName); when(getSecretData.getSecretVersion()).thenReturn(secretVersion); SecretBundle secretBundle = mock(SecretBundle.class); when(azureKeyVaultClientDelegate.getSecret(anyString(), anyString(), anyString())).thenReturn(secretBundle); when(secretBundle.value()).thenReturn("value"); keyVaultService.getSecret(getSecretData); verify(azureKeyVaultClientDelegate).getSecret(vaultUrl, secretName, secretVersion); }
@Test public void getSecretThrowsExceptionIfKeyNotFoundInVault() { when(azureKeyVaultClientDelegate.getSecret(anyString(), anyString())).thenReturn(null); AzureKeyVaultService azureKeyVaultService = new AzureKeyVaultService(keyVaultConfig, azureKeyVaultClientDelegate); String secretName = "secret"; AzureGetSecretData getSecretData = mock(AzureGetSecretData.class); when(getSecretData.getSecretName()).thenReturn(secretName); Throwable throwable = catchThrowable(() -> azureKeyVaultService.getSecret(getSecretData)); assertThat(throwable).isInstanceOf(VaultSecretNotFoundException.class); assertThat(throwable).hasMessageContaining("Azure Key Vault secret " + secretName + " was not found in vault " + vaultUrl); }