### Question:
HashicorpKeyVaultService implements KeyVaultService<HashicorpSetSecretData, HashicorpGetSecretData> { @Override public String getSecret(HashicorpGetSecretData hashicorpGetSecretData) { KeyValueOperationsDelegate keyValueOperationsDelegate = keyValueOperationsDelegateFactory.create(hashicorpGetSecretData.getSecretEngineName()); Versioned<Map<String, Object>> versionedResponse = keyValueOperationsDelegate.get(hashicorpGetSecretData); if (versionedResponse == null || !versionedResponse.hasData()) { throw new HashicorpVaultException("No data found at " + hashicorpGetSecretData.getSecretEngineName() + "/" + hashicorpGetSecretData.getSecretName()); } if (!versionedResponse.getData().containsKey(hashicorpGetSecretData.getValueId())) { throw new HashicorpVaultException("No value with id " + hashicorpGetSecretData.getValueId() + " found at " + hashicorpGetSecretData.getSecretEngineName() + "/" + hashicorpGetSecretData.getSecretName()); } return versionedResponse.getData().get(hashicorpGetSecretData.getValueId()).toString(); } HashicorpKeyVaultService(KeyValueOperationsDelegateFactory keyValueOperationsDelegateFactory); @Override String getSecret(HashicorpGetSecretData hashicorpGetSecretData); @Override Object setSecret(HashicorpSetSecretData hashicorpSetSecretData);  }### Answer:
@Test public void getSecret() { HashicorpGetSecretData getSecretData = mock(HashicorpGetSecretData.class); when(getSecretData.getSecretEngineName()).thenReturn("secretEngine"); when(getSecretData.getSecretName()).thenReturn("secretName"); when(getSecretData.getValueId()).thenReturn("keyId"); Versioned versionedResponse = mock(Versioned.class); when(delegate.get(any(HashicorpGetSecretData.class))).thenReturn(versionedResponse); when(versionedResponse.hasData()).thenReturn(true); Map responseData = mock(Map.class); when(versionedResponse.getData()).thenReturn(responseData); when(responseData.containsKey("keyId")).thenReturn(true); String keyValue = "keyvalue"; when(responseData.get("keyId")).thenReturn(keyValue); String result = keyVaultService.getSecret(getSecretData); assertThat(result).isEqualTo(keyValue); }
@Test public void getSecretThrowsExceptionIfNullRetrievedFromVault() { HashicorpGetSecretData getSecretData = new HashicorpGetSecretData("engine", "secretName", "id", 0); when(delegate.get(getSecretData)).thenReturn(null); Throwable ex = catchThrowable(() -> keyVaultService.getSecret(getSecretData)); assertThat(ex).isExactlyInstanceOf(HashicorpVaultException.class); assertThat(ex).hasMessage("No data found at engine/secretName"); }
@Test public void getSecretThrowsExceptionIfNoDataRetrievedFromVault() { HashicorpGetSecretData getSecretData = new HashicorpGetSecretData("engine", "secretName", "id", 0); Versioned versionedResponse = mock(Versioned.class); when(versionedResponse.hasData()).thenReturn(false); when(delegate.get(getSecretData)).thenReturn(versionedResponse); Throwable ex = catchThrowable(() -> keyVaultService.getSecret(getSecretData)); assertThat(ex).isExactlyInstanceOf(HashicorpVaultException.class); assertThat(ex).hasMessage("No data found at engine/secretName"); }
@Test public void getSecretThrowsExceptionIfValueNotFoundForGivenId() { HashicorpGetSecretData getSecretData = new HashicorpGetSecretData("engine", "secretName", "id", 0); Versioned versionedResponse = mock(Versioned.class); when(versionedResponse.hasData()).thenReturn(true); Map responseData = mock(Map.class); when(versionedResponse.getData()).thenReturn(responseData); when(responseData.containsKey("id")).thenReturn(false); when(delegate.get(getSecretData)).thenReturn(versionedResponse); Throwable ex = catchThrowable(() -> keyVaultService.getSecret(getSecretData)); assertThat(ex).isExactlyInstanceOf(HashicorpVaultException.class); assertThat(ex).hasMessage("No value with id id found at engine/secretName"); }