### Question:
AzureKeyVaultClientCredentials extends KeyVaultCredentials { @Override public String doAuthenticate(String authorization, String resource, String scope) { if(clientId == null || clientSecret == null) { throw new AzureCredentialNotSetException(AZURE_CLIENT_ID + " and " + AZURE_CLIENT_SECRET + " environment variables must be set"); } try { if(Objects.isNull(authenticationContext)) { this.authenticationContext = new AuthenticationContext(authorization, false, executorService); } ClientCredential credential = new ClientCredential(clientId, clientSecret); return authenticationContext.acquireToken(resource, credential, null).get().getAccessToken(); } catch (ExecutionException | MalformedURLException ex) { throw new RuntimeException(ex); } catch(InterruptedException ex) { LOGGER.warn("Key vault executor executorService interrupted"); throw new RuntimeException(ex); } } AzureKeyVaultClientCredentials(String clientId, String clientSecret, ExecutorService executorService); @Override String doAuthenticate(String authorization, String resource, String scope); @PreDestroy void onDestroy();  }### Answer:
@Test public void getCredentialsUsingInjectedParameters() throws Exception { Future<AuthenticationResult> response = mock(Future.class); AuthenticationResult authenticationResult = new AuthenticationResult(null,"accessToken",null,0L,null,null,false); when(response.get()).thenReturn(authenticationResult); when(authenticationContext.acquireToken(anyString(), any(ClientCredential.class), any())).thenReturn(response); String result = credentials.doAuthenticate("auth","resource",null); assertThat(result).isEqualTo("accessToken"); verify(authenticationContext).acquireToken(anyString(), any(ClientCredential.class), any()); Mockito.verifyNoMoreInteractions(authenticationContext); }
@Test(expected = RuntimeException.class) public void ifFutureAbortsThenExecutionExceptionThrownAsRuntimeException() throws ExecutionException, InterruptedException { Future<AuthenticationResult> response = mock(Future.class); when(response.get()).thenThrow(ExecutionException.class); when(authenticationContext.acquireToken(anyString(), any(ClientCredential.class), any())).thenReturn(response); credentials.doAuthenticate("auth", "resource", null); }
@Test(expected = RuntimeException.class) public void threadInterruptedExceptionIsCaughtAndLogged() throws ExecutionException, InterruptedException { Future<AuthenticationResult> response = mock(Future.class); when(response.get()).thenThrow(InterruptedException.class); when(authenticationContext.acquireToken(anyString(), any(ClientCredential.class), any())).thenReturn(response); credentials.doAuthenticate("auth", "resource", null); }
@Test public void nullClientIdThrowsRuntimeException() { credentials = new AzureKeyVaultClientCredentials(null, "secret", executorService); String goodUrl = "https: final Throwable ex = catchThrowable(() -> credentials.doAuthenticate(goodUrl, null, null)); assertThat(ex).isInstanceOf(RuntimeException.class) .hasMessageContaining(AZURE_CLIENT_ID + " and " + AZURE_CLIENT_SECRET + " environment variables must be set"); }
@Test public void nullClientSecretThrowsRuntimeException() { credentials = new AzureKeyVaultClientCredentials("id", null, executorService); String goodUrl = "https: final Throwable ex = catchThrowable(() -> credentials.doAuthenticate(goodUrl, null, null)); assertThat(ex).isInstanceOf(RuntimeException.class) .hasMessageContaining(AZURE_CLIENT_ID + " and " + AZURE_CLIENT_SECRET + " environment variables must be set"); }
@Test public void nullClientIdAndSecretThrowsRuntimeException() { credentials = new AzureKeyVaultClientCredentials(null, null, executorService); String goodUrl = "https: final Throwable ex = catchThrowable(() -> credentials.doAuthenticate(goodUrl, null, null)); assertThat(ex).isInstanceOf(RuntimeException.class) .hasMessageContaining(AZURE_CLIENT_ID + " and " + AZURE_CLIENT_SECRET + " environment variables must be set"); }