@Test public void retrieveTokenKeys_badResponse_throwsException() { String errorMessage = "useful error message"; mockResponse(errorMessage, HttpStatus.BAD_REQUEST); assertThatThrownBy(() -> cut.retrieveTokenKeys(TOKEN_KEYS_ENDPOINT_URI)) .isInstanceOf(OAuth2ServiceException.class) .hasMessageContaining(TOKEN_KEYS_ENDPOINT_URI.toString()) .hasMessageContaining(String.valueOf(HttpStatus.BAD_REQUEST.value())) .hasMessageContaining(errorMessage); }
@Override public String retrieveTokenKeys(URI tokenKeysEndpointUri) throws OAuth2ServiceException { Assertions.assertNotNull(tokenKeysEndpointUri, "Token key endpoint must not be null!"); try { ResponseEntity<String> response = restOperations.getForEntity(tokenKeysEndpointUri, String.class); if (HttpStatus.OK.value() == response.getStatusCode().value()) { return response.getBody(); } else { throw OAuth2ServiceException.builder("Error retrieving token keys") .withUri(tokenKeysEndpointUri) .withStatusCode(response.getStatusCodeValue()) .withResponseBody(response.getBody()) .build(); } } catch (HttpClientErrorException ex) { throw OAuth2ServiceException.builder("Error retrieving token keys") .withUri(tokenKeysEndpointUri) .withStatusCode(ex.getStatusCode().value()) .withResponseBody(ex.getResponseBodyAsString()) .build(); } catch (Exception e) { throw OAuth2ServiceException.builder("Unexpected error retrieving token keys: " + e.getMessage()) .withUri(tokenKeysEndpointUri) .build(); } }
SpringOAuth2TokenKeyService implements OAuth2TokenKeyService { @Override public String retrieveTokenKeys(URI tokenKeysEndpointUri) throws OAuth2ServiceException { Assertions.assertNotNull(tokenKeysEndpointUri, "Token key endpoint must not be null!"); try { ResponseEntity<String> response = restOperations.getForEntity(tokenKeysEndpointUri, String.class); if (HttpStatus.OK.value() == response.getStatusCode().value()) { return response.getBody(); } else { throw OAuth2ServiceException.builder("Error retrieving token keys") .withUri(tokenKeysEndpointUri) .withStatusCode(response.getStatusCodeValue()) .withResponseBody(response.getBody()) .build(); } } catch (HttpClientErrorException ex) { throw OAuth2ServiceException.builder("Error retrieving token keys") .withUri(tokenKeysEndpointUri) .withStatusCode(ex.getStatusCode().value()) .withResponseBody(ex.getResponseBodyAsString()) .build(); } catch (Exception e) { throw OAuth2ServiceException.builder("Unexpected error retrieving token keys: " + e.getMessage()) .withUri(tokenKeysEndpointUri) .build(); } } }
SpringOAuth2TokenKeyService implements OAuth2TokenKeyService { @Override public String retrieveTokenKeys(URI tokenKeysEndpointUri) throws OAuth2ServiceException { Assertions.assertNotNull(tokenKeysEndpointUri, "Token key endpoint must not be null!"); try { ResponseEntity<String> response = restOperations.getForEntity(tokenKeysEndpointUri, String.class); if (HttpStatus.OK.value() == response.getStatusCode().value()) { return response.getBody(); } else { throw OAuth2ServiceException.builder("Error retrieving token keys") .withUri(tokenKeysEndpointUri) .withStatusCode(response.getStatusCodeValue()) .withResponseBody(response.getBody()) .build(); } } catch (HttpClientErrorException ex) { throw OAuth2ServiceException.builder("Error retrieving token keys") .withUri(tokenKeysEndpointUri) .withStatusCode(ex.getStatusCode().value()) .withResponseBody(ex.getResponseBodyAsString()) .build(); } catch (Exception e) { throw OAuth2ServiceException.builder("Unexpected error retrieving token keys: " + e.getMessage()) .withUri(tokenKeysEndpointUri) .build(); } } SpringOAuth2TokenKeyService(@Nonnull RestOperations restOperations); }
SpringOAuth2TokenKeyService implements OAuth2TokenKeyService { @Override public String retrieveTokenKeys(URI tokenKeysEndpointUri) throws OAuth2ServiceException { Assertions.assertNotNull(tokenKeysEndpointUri, "Token key endpoint must not be null!"); try { ResponseEntity<String> response = restOperations.getForEntity(tokenKeysEndpointUri, String.class); if (HttpStatus.OK.value() == response.getStatusCode().value()) { return response.getBody(); } else { throw OAuth2ServiceException.builder("Error retrieving token keys") .withUri(tokenKeysEndpointUri) .withStatusCode(response.getStatusCodeValue()) .withResponseBody(response.getBody()) .build(); } } catch (HttpClientErrorException ex) { throw OAuth2ServiceException.builder("Error retrieving token keys") .withUri(tokenKeysEndpointUri) .withStatusCode(ex.getStatusCode().value()) .withResponseBody(ex.getResponseBodyAsString()) .build(); } catch (Exception e) { throw OAuth2ServiceException.builder("Unexpected error retrieving token keys: " + e.getMessage()) .withUri(tokenKeysEndpointUri) .build(); } } SpringOAuth2TokenKeyService(@Nonnull RestOperations restOperations); @Override String retrieveTokenKeys(URI tokenKeysEndpointUri); }
SpringOAuth2TokenKeyService implements OAuth2TokenKeyService { @Override public String retrieveTokenKeys(URI tokenKeysEndpointUri) throws OAuth2ServiceException { Assertions.assertNotNull(tokenKeysEndpointUri, "Token key endpoint must not be null!"); try { ResponseEntity<String> response = restOperations.getForEntity(tokenKeysEndpointUri, String.class); if (HttpStatus.OK.value() == response.getStatusCode().value()) { return response.getBody(); } else { throw OAuth2ServiceException.builder("Error retrieving token keys") .withUri(tokenKeysEndpointUri) .withStatusCode(response.getStatusCodeValue()) .withResponseBody(response.getBody()) .build(); } } catch (HttpClientErrorException ex) { throw OAuth2ServiceException.builder("Error retrieving token keys") .withUri(tokenKeysEndpointUri) .withStatusCode(ex.getStatusCode().value()) .withResponseBody(ex.getResponseBodyAsString()) .build(); } catch (Exception e) { throw OAuth2ServiceException.builder("Unexpected error retrieving token keys: " + e.getMessage()) .withUri(tokenKeysEndpointUri) .build(); } } SpringOAuth2TokenKeyService(@Nonnull RestOperations restOperations); @Override String retrieveTokenKeys(URI tokenKeysEndpointUri);  }