DittoPublicKeyProvider implements PublicKeyProvider { @Override public CompletableFuture<Optional<PublicKey>> getPublicKey(final String issuer, final String keyId) { argumentNotNull(issuer); argumentNotNull(keyId); return publicKeyCache.get(PublicKeyIdWithIssuer.of(keyId, issuer)); } private  DittoPublicKeyProvider(final JwtSubjectIssuersConfig jwtSubjectIssuersConfig,
            final HttpClientFacade httpClient,
            final CacheConfig publicKeysConfig,
            final String cacheName); static PublicKeyProvider of(final JwtSubjectIssuersConfig jwtSubjectIssuersConfig,
            final HttpClientFacade httpClient,
            final CacheConfig publicKeysCacheConfig,
            final String cacheName); @Override CompletableFuture<Optional<PublicKey>> getPublicKey(final String issuer, final String keyId);  }
@Test public void verifyThatKeyIsCached() throws InterruptedException, TimeoutException, ExecutionException { mockSuccessfulDiscoveryEndpointRequest(); mockSuccessfulPublicKeysRequest(); final Optional<PublicKey> publicKeyFromEndpoint = underTest.getPublicKey("google.com", KEY_ID).get(LATCH_TIMEOUT, TimeUnit.SECONDS); assertThat(publicKeyFromEndpoint).isNotEmpty(); verify(httpClientMock).createSingleHttpRequest(DISCOVERY_ENDPOINT_REQUEST); verify(httpClientMock).createSingleHttpRequest(PUBLIC_KEYS_REQUEST); Mockito.clearInvocations(httpClientMock); final Optional<PublicKey> publicKeyFromCache = underTest.getPublicKey("google.com", KEY_ID).get(LATCH_TIMEOUT, TimeUnit.SECONDS); assertThat(publicKeyFromCache).contains(publicKeyFromEndpoint.get()); assertThat(publicKeyFromCache).isNotEmpty(); verifyNoMoreInteractions(httpClientMock); }
@Test public void verifyThatKeyIsNotCachedOnErrorResponseFromDiscoveryEndpoint() { mockErrorDiscoveryEndpointRequest(); assertThatExceptionOfType(ExecutionException.class) .isThrownBy(() -> underTest.getPublicKey("google.com", KEY_ID).get(LATCH_TIMEOUT, TimeUnit.SECONDS)) .withCauseExactlyInstanceOf(GatewayAuthenticationProviderUnavailableException.class); verify(httpClientMock).createSingleHttpRequest(DISCOVERY_ENDPOINT_REQUEST); verify(httpClientMock, never()).createSingleHttpRequest(PUBLIC_KEYS_REQUEST); Mockito.clearInvocations(httpClientMock); assertThatExceptionOfType(ExecutionException.class) .isThrownBy(() -> underTest.getPublicKey("google.com", KEY_ID).get(LATCH_TIMEOUT, TimeUnit.SECONDS)) .withCauseExactlyInstanceOf(GatewayAuthenticationProviderUnavailableException.class); verify(httpClientMock).createSingleHttpRequest(DISCOVERY_ENDPOINT_REQUEST); verify(httpClientMock, never()).createSingleHttpRequest(PUBLIC_KEYS_REQUEST); }
@Test public void verifyThatKeyIsNotCachedIfResponseDoesNotContainKeyId() throws InterruptedException, ExecutionException, TimeoutException { mockSuccessfulDiscoveryEndpointRequest(); mockSuccessfulPublicKeysRequestWithoutMatchingKeyId(); assertThat(underTest.getPublicKey("google.com", KEY_ID).get(LATCH_TIMEOUT, TimeUnit.SECONDS)).isEmpty(); verify(httpClientMock).createSingleHttpRequest(DISCOVERY_ENDPOINT_REQUEST); verify(httpClientMock).createSingleHttpRequest(PUBLIC_KEYS_REQUEST); Mockito.clearInvocations(httpClientMock); assertThat(underTest.getPublicKey("google.com", KEY_ID).get(LATCH_TIMEOUT, TimeUnit.SECONDS)).isEmpty(); verify(httpClientMock).createSingleHttpRequest(DISCOVERY_ENDPOINT_REQUEST); verify(httpClientMock).createSingleHttpRequest(PUBLIC_KEYS_REQUEST); }