@Test public void retrieveTokenKeys_errorOccurs_throwsServiceException() throws IOException { String errorMessage = "useful error message"; when(httpClient.execute(any())).thenThrow(new IOException(errorMessage)); assertThatThrownBy(() -> cut.retrieveTokenKeys(TOKEN_ENDPOINT_URI)) .isInstanceOf(OAuth2ServiceException.class) .hasMessageContaining(errorMessage); }
@Override public String retrieveTokenKeys(URI tokenKeysEndpointUri) throws OAuth2ServiceException { Assertions.assertNotNull(tokenKeysEndpointUri, "Token key endpoint must not be null!"); HttpUriRequest request = new HttpGet(tokenKeysEndpointUri); try (CloseableHttpResponse response = httpClient.execute(request)) { String bodyAsString = HttpClientUtil.extractResponseBodyAsString(response); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { return bodyAsString; } else { throw OAuth2ServiceException.builder("Error retrieving token keys") .withUri(tokenKeysEndpointUri) .withStatusCode(statusCode) .withResponseBody(bodyAsString) .build(); } } catch (IOException e) { throw new OAuth2ServiceException("Error retrieving token keys: " + e.getMessage()); } }
DefaultOAuth2TokenKeyService implements OAuth2TokenKeyService { @Override public String retrieveTokenKeys(URI tokenKeysEndpointUri) throws OAuth2ServiceException { Assertions.assertNotNull(tokenKeysEndpointUri, "Token key endpoint must not be null!"); HttpUriRequest request = new HttpGet(tokenKeysEndpointUri); try (CloseableHttpResponse response = httpClient.execute(request)) { String bodyAsString = HttpClientUtil.extractResponseBodyAsString(response); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { return bodyAsString; } else { throw OAuth2ServiceException.builder("Error retrieving token keys") .withUri(tokenKeysEndpointUri) .withStatusCode(statusCode) .withResponseBody(bodyAsString) .build(); } } catch (IOException e) { throw new OAuth2ServiceException("Error retrieving token keys: " + e.getMessage()); } } }
DefaultOAuth2TokenKeyService implements OAuth2TokenKeyService { @Override public String retrieveTokenKeys(URI tokenKeysEndpointUri) throws OAuth2ServiceException { Assertions.assertNotNull(tokenKeysEndpointUri, "Token key endpoint must not be null!"); HttpUriRequest request = new HttpGet(tokenKeysEndpointUri); try (CloseableHttpResponse response = httpClient.execute(request)) { String bodyAsString = HttpClientUtil.extractResponseBodyAsString(response); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { return bodyAsString; } else { throw OAuth2ServiceException.builder("Error retrieving token keys") .withUri(tokenKeysEndpointUri) .withStatusCode(statusCode) .withResponseBody(bodyAsString) .build(); } } catch (IOException e) { throw new OAuth2ServiceException("Error retrieving token keys: " + e.getMessage()); } } DefaultOAuth2TokenKeyService();  DefaultOAuth2TokenKeyService(@Nonnull CloseableHttpClient httpClient); }
DefaultOAuth2TokenKeyService implements OAuth2TokenKeyService { @Override public String retrieveTokenKeys(URI tokenKeysEndpointUri) throws OAuth2ServiceException { Assertions.assertNotNull(tokenKeysEndpointUri, "Token key endpoint must not be null!"); HttpUriRequest request = new HttpGet(tokenKeysEndpointUri); try (CloseableHttpResponse response = httpClient.execute(request)) { String bodyAsString = HttpClientUtil.extractResponseBodyAsString(response); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { return bodyAsString; } else { throw OAuth2ServiceException.builder("Error retrieving token keys") .withUri(tokenKeysEndpointUri) .withStatusCode(statusCode) .withResponseBody(bodyAsString) .build(); } } catch (IOException e) { throw new OAuth2ServiceException("Error retrieving token keys: " + e.getMessage()); } } DefaultOAuth2TokenKeyService();  DefaultOAuth2TokenKeyService(@Nonnull CloseableHttpClient httpClient); @Override String retrieveTokenKeys(URI tokenKeysEndpointUri); }
DefaultOAuth2TokenKeyService implements OAuth2TokenKeyService { @Override public String retrieveTokenKeys(URI tokenKeysEndpointUri) throws OAuth2ServiceException { Assertions.assertNotNull(tokenKeysEndpointUri, "Token key endpoint must not be null!"); HttpUriRequest request = new HttpGet(tokenKeysEndpointUri); try (CloseableHttpResponse response = httpClient.execute(request)) { String bodyAsString = HttpClientUtil.extractResponseBodyAsString(response); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { return bodyAsString; } else { throw OAuth2ServiceException.builder("Error retrieving token keys") .withUri(tokenKeysEndpointUri) .withStatusCode(statusCode) .withResponseBody(bodyAsString) .build(); } } catch (IOException e) { throw new OAuth2ServiceException("Error retrieving token keys: " + e.getMessage()); } } DefaultOAuth2TokenKeyService();  DefaultOAuth2TokenKeyService(@Nonnull CloseableHttpClient httpClient); @Override String retrieveTokenKeys(URI tokenKeysEndpointUri);  }