### Question:
PasswordCredentialsRequestValidatorImpl implements PasswordCredentialsRequestValidator { public void validateRequest(OAuth2Request request, ClientRegistration clientRegistration) { Reject.ifTrue(Utils.isEmpty(request.<String>getParameter("username")), "Missing parameter, 'username'"); Reject.ifTrue(Utils.isEmpty(request.<String>getParameter("password")), "Missing parameter, 'password'"); }  void validateRequest(OAuth2Request request, ClientRegistration clientRegistration);  }### Answer:
@Test public void shouldValidateValidRequest() { OAuth2Request request = mock(OAuth2Request.class); ClientRegistration clientRegistration = mock(ClientRegistration.class); given(request.getParameter("username")).willReturn("USERNAME"); given(request.getParameter("password")).willReturn("PASSWORD"); requestValidator.validateRequest(request, clientRegistration); }
@Test public void shouldValidateRequestWithEmptyUsername() { OAuth2Request request = mock(OAuth2Request.class); ClientRegistration clientRegistration = mock(ClientRegistration.class); given(request.getParameter("username")).willReturn(""); try { requestValidator.validateRequest(request, clientRegistration); fail(); } catch (IllegalArgumentException e) { assertEquals(e.getMessage(), "Missing parameter, 'username'"); } }
@Test public void shouldValidateRequestWithMissingUsername() { OAuth2Request request = mock(OAuth2Request.class); ClientRegistration clientRegistration = mock(ClientRegistration.class); try { requestValidator.validateRequest(request, clientRegistration); fail(); } catch (IllegalArgumentException e) { assertEquals(e.getMessage(), "Missing parameter, 'username'"); } }
@Test public void shouldValidateRequestWithEmptyPassword() { OAuth2Request request = mock(OAuth2Request.class); ClientRegistration clientRegistration = mock(ClientRegistration.class); given(request.getParameter("username")).willReturn("USERNAME"); given(request.getParameter("password")).willReturn(""); try { requestValidator.validateRequest(request, clientRegistration); fail(); } catch (IllegalArgumentException e) { assertEquals(e.getMessage(), "Missing parameter, 'password'"); } }
@Test public void shouldValidateRequestWithMissingPassword() { OAuth2Request request = mock(OAuth2Request.class); ClientRegistration clientRegistration = mock(ClientRegistration.class); given(request.getParameter("username")).willReturn("USERNAME"); try { requestValidator.validateRequest(request, clientRegistration); fail(); } catch (IllegalArgumentException e) { assertEquals(e.getMessage(), "Missing parameter, 'password'"); } }