### Question:
ClaimsParameterValidator implements AuthorizeRequestValidator { @Override public void validateRequest(OAuth2Request request) throws InvalidClientException, InvalidRequestException, RedirectUriMismatchException, UnsupportedResponseTypeException, ServerException, BadRequestException, InvalidScopeException, NotFoundException { final OAuth2ProviderSettings settings = providerSettingsFactory.get(request); final String claims = request.getParameter(OAuth2Constants.Custom.CLAIMS); if (!settings.getClaimsParameterSupported()) { return; } if (claims == null) { return; } final JSONObject claimsJson; try { claimsJson = new JSONObject(claims); } catch (JSONException e) { throw new BadRequestException("Invalid JSON in supplied claims parameter."); } JSONObject userinfoClaims = null; try { userinfoClaims = claimsJson.getJSONObject(OAuth2Constants.UserinfoEndpoint.USERINFO); } catch (Exception e) { } if (userinfoClaims != null) { String responseType = request.getParameter(OAuth2Constants.Params.RESPONSE_TYPE); if (responseType != null && responseType.trim().equals(OAuth2Constants.JWTTokenParams.ID_TOKEN)) { throw new BadRequestException("Must request an access token when providing " + "userinfo in claims parameter."); } } } @Inject  ClaimsParameterValidator(OAuth2ProviderSettingsFactory providerSettingsFactory); @Override void validateRequest(OAuth2Request request);  }### Answer:
@Test public void shouldValidateClaimsParameter() throws NotFoundException, BadRequestException, RedirectUriMismatchException, InvalidScopeException, InvalidRequestException, InvalidClientException, ServerException, UnsupportedResponseTypeException { OAuth2Request mockRequest = mock(OAuth2Request.class); OAuth2ProviderSettings mockProviderSettings = mock(OAuth2ProviderSettings.class); String responseTypes = "code token id_token"; given(mockProviderSettingsFactory.get(mockRequest)).willReturn(mockProviderSettings); given(mockProviderSettings.getClaimsParameterSupported()).willReturn(true); given(mockRequest.getParameter(OAuth2Constants.Custom.CLAIMS)).willReturn(validClaimsString); given(mockRequest.getParameter(OAuth2Constants.Params.RESPONSE_TYPE)).willReturn(responseTypes); claimsParameterValidator.validateRequest(mockRequest); }
@Test(expectedExceptions = BadRequestException.class) public void shouldErrorValidatingJson() throws NotFoundException, BadRequestException, RedirectUriMismatchException, InvalidScopeException, InvalidRequestException, InvalidClientException, ServerException, UnsupportedResponseTypeException { OAuth2Request mockRequest = mock(OAuth2Request.class); OAuth2ProviderSettings mockProviderSettings = mock(OAuth2ProviderSettings.class); String responseTypes = "id_token"; given(mockProviderSettingsFactory.get(mockRequest)).willReturn(mockProviderSettings); given(mockProviderSettings.getClaimsParameterSupported()).willReturn(true); given(mockRequest.getParameter(OAuth2Constants.Custom.CLAIMS)).willReturn(invalidClaimsString); given(mockRequest.getParameter(OAuth2Constants.Params.RESPONSE_TYPE)).willReturn(responseTypes); claimsParameterValidator.validateRequest(mockRequest); }
@Test(expectedExceptions = BadRequestException.class) public void shouldErrorValidatingResponseType() throws NotFoundException, BadRequestException, RedirectUriMismatchException, InvalidScopeException, InvalidRequestException, InvalidClientException, ServerException, UnsupportedResponseTypeException { OAuth2Request mockRequest = mock(OAuth2Request.class); OAuth2ProviderSettings mockProviderSettings = mock(OAuth2ProviderSettings.class); String responseTypes = "id_token"; given(mockProviderSettingsFactory.get(mockRequest)).willReturn(mockProviderSettings); given(mockProviderSettings.getClaimsParameterSupported()).willReturn(true); given(mockRequest.getParameter(OAuth2Constants.Custom.CLAIMS)).willReturn(validClaimsString); given(mockRequest.getParameter(OAuth2Constants.Params.RESPONSE_TYPE)).willReturn(responseTypes); claimsParameterValidator.validateRequest(mockRequest); }