CertificateCredentials extends TokenCredentials implements AzureTokenCredentials { @Override public String getToken(String resource) throws IOException { AuthenticationResult authenticationResult = this.tokens.get(resource); if(authenticationResult == null || authenticationResult.getExpiresOnDate().before(new Date())) { authenticationResult = this.acquireAccessToken(resource); } return authenticationResult.getAccessToken(); } CertificateCredentials(String domain, AzureEnvironment environment,
                                  AsymmetricKeyCredentialFactory credentialFactory); @Override String getDomain(); @Override AzureEnvironment getEnvironment(); @Override String getToken(String resource); void applyCredentialsFilter(OkHttpClient.Builder clientBuilder);  }
@Test void testValidToken() throws NoSuchFieldException, IllegalAccessException, IOException { String resource = "resource"; String accessToken = "accessToken"; Field tokensField = CertificateCredentials.class.getDeclaredField("tokens"); tokensField.setAccessible(true); CertificateCredentials credentials = new CertificateCredentials(domain, environment, mock(AsymmetricKeyCredentialFactory.class)); Map<String, AuthenticationResult> tokens = (Map<String, AuthenticationResult>)tokensField.get(credentials); AuthenticationResult authenticationResult = new AuthenticationResult( "accessTokenType", accessToken, "refreshToken", 1000, "idToken", null, false); tokens.put(resource, authenticationResult); String token = credentials.getToken(resource); Assertions.assertEquals(accessToken, token); }