### Question:
AuthorizationCodeRequestValidatorImpl implements AuthorizationCodeRequestValidator { public void validateRequest(OAuth2Request request, ClientRegistration clientRegistration) throws InvalidRequestException, RedirectUriMismatchException, InvalidClientException { if (request.getParameter("scope") != null) { throw new InvalidRequestException("Scope parameter is not supported on an authorization code access_token "+ "exchange request. Scope parameter should be supplied to the authorize request."); } Reject.ifTrue(Utils.isEmpty(request.<String>getParameter("code")), "Missing parameter, 'code'"); Reject.ifTrue(Utils.isEmpty(request.<String>getParameter("redirect_uri")), "Missing parameter, 'redirect_uri'"); redirectUriValidator.validate(clientRegistration, request.<String>getParameter("redirect_uri")); } @Inject  AuthorizationCodeRequestValidatorImpl(final RedirectUriValidator redirectUriValidator); void validateRequest(OAuth2Request request, ClientRegistration clientRegistration);  }### Answer:
@Test public void shouldValidateValidRequest() throws RedirectUriMismatchException, InvalidClientException, InvalidRequestException { OAuth2Request request = mock(OAuth2Request.class); ClientRegistration clientRegistration = mock(ClientRegistration.class); given(request.getParameter("code")).willReturn("CODE"); given(request.getParameter("redirect_uri")).willReturn("REDIRECT_URI"); requestValidator.validateRequest(request, clientRegistration); verify(redirectUriValidator).validate(Matchers.<ClientRegistration>anyObject(), anyString()); }
@Test public void shouldValidateRequestWithEmptyClientId() throws InvalidClientException, InvalidRequestException, RedirectUriMismatchException { OAuth2Request request = mock(OAuth2Request.class); ClientRegistration clientRegistration = mock(ClientRegistration.class); given(request.getParameter("code")).willReturn(""); try { requestValidator.validateRequest(request, clientRegistration); fail(); } catch (IllegalArgumentException e) { assertEquals(e.getMessage(), "Missing parameter, 'code'"); } }
@Test public void shouldValidateRequestWithMissingClientId() throws InvalidClientException, InvalidRequestException, RedirectUriMismatchException { OAuth2Request request = mock(OAuth2Request.class); ClientRegistration clientRegistration = mock(ClientRegistration.class); try { requestValidator.validateRequest(request, clientRegistration); fail(); } catch (IllegalArgumentException e) { assertEquals(e.getMessage(), "Missing parameter, 'code'"); } }
@Test public void shouldValidateRequestWithEmptyResponseType() throws InvalidClientException, InvalidRequestException, RedirectUriMismatchException { OAuth2Request request = mock(OAuth2Request.class); ClientRegistration clientRegistration = mock(ClientRegistration.class); given(request.getParameter("code")).willReturn("CODE"); given(request.getParameter("redirect_uri")).willReturn(""); try { requestValidator.validateRequest(request, clientRegistration); fail(); } catch (IllegalArgumentException e) { assertEquals(e.getMessage(), "Missing parameter, 'redirect_uri'"); } }
@Test public void shouldValidateRequestWithMissingResponseType() throws InvalidClientException, InvalidRequestException, RedirectUriMismatchException { OAuth2Request request = mock(OAuth2Request.class); ClientRegistration clientRegistration = mock(ClientRegistration.class); given(request.getParameter("code")).willReturn("CODE"); try { requestValidator.validateRequest(request, clientRegistration); fail(); } catch (IllegalArgumentException e) { assertEquals(e.getMessage(), "Missing parameter, 'redirect_uri'"); } }
@Test (expectedExceptions = InvalidRequestException.class) public void shouldThrowInvalidRequestExceptionWhenScopeRequested() throws Exception { OAuth2Request request = mock(OAuth2Request.class); given(request.getParameter("scope")).willReturn("fred"); ClientRegistration clientRegistration = mock(ClientRegistration.class); requestValidator.validateRequest(request, clientRegistration); }