### Question:
KeycloakAuthenticationProcessingFilter extends AbstractAuthenticationProcessingFilter implements ApplicationContextAware { @Override public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException { log.debug("Attempting Keycloak authentication"); HttpFacade facade = new SimpleHttpFacade(request, response); KeycloakDeployment deployment = adapterDeploymentContext.resolveDeployment(facade); deployment.setDelegateBearerErrorResponseSending(true); AdapterTokenStore tokenStore = adapterTokenStoreFactory.createAdapterTokenStore(deployment, request, response); RequestAuthenticator authenticator = requestAuthenticatorFactory.createRequestAuthenticator(facade, request, deployment, tokenStore, -1); AuthOutcome result = authenticator.authenticate(); log.debug("Auth outcome: {}", result); if (AuthOutcome.FAILED.equals(result)) { AuthChallenge challenge = authenticator.getChallenge(); if (challenge != null) { challenge.challenge(facade); } throw new KeycloakAuthenticationException("Invalid authorization header, see WWW-Authenticate header for details"); } if (AuthOutcome.NOT_ATTEMPTED.equals(result)) { AuthChallenge challenge = authenticator.getChallenge(); if (challenge != null) { challenge.challenge(facade); } if (deployment.isBearerOnly()) { throw new KeycloakAuthenticationException("Authorization header not found, see WWW-Authenticate header"); } else { return null; } } else if (AuthOutcome.AUTHENTICATED.equals(result)) { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); Assert.notNull(authentication, "Authentication SecurityContextHolder was null"); return authenticationManager.authenticate(authentication); } else { AuthChallenge challenge = authenticator.getChallenge(); if (challenge != null) { challenge.challenge(facade); } return null; } } KeycloakAuthenticationProcessingFilter(AuthenticationManager authenticationManager);  KeycloakAuthenticationProcessingFilter(AuthenticationManager authenticationManager, RequestMatcher
            requiresAuthenticationRequestMatcher); @Override void afterPropertiesSet(); @Override Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response); @Override void setApplicationContext(ApplicationContext applicationContext); void setAdapterTokenStoreFactory(AdapterTokenStoreFactory adapterTokenStoreFactory); @Override final void setAllowSessionCreation(boolean allowSessionCreation); @Override final void setContinueChainBeforeSuccessfulAuthentication(boolean continueChainBeforeSuccessfulAuthentication); void setRequestAuthenticatorFactory(RequestAuthenticatorFactory requestAuthenticatorFactory); static final String AUTHORIZATION_HEADER; static final RequestMatcher DEFAULT_REQUEST_MATCHER; }### Answer:
@Test public void testAttemptAuthenticationExpectRedirect() throws Exception { when(keycloakDeployment.getAuthUrl()).thenReturn(KeycloakUriBuilder.fromUri("http: when(keycloakDeployment.getResourceName()).thenReturn("resource-name"); when(keycloakDeployment.getStateCookieName()).thenReturn("kc-cookie"); when(keycloakDeployment.getSslRequired()).thenReturn(SslRequired.NONE); when(keycloakDeployment.isBearerOnly()).thenReturn(Boolean.FALSE); filter.attemptAuthentication(request, response); verify(response).setStatus(302); verify(response).setHeader(eq("Location"), startsWith("http: }
@Test(expected = KeycloakAuthenticationException.class) public void testAttemptAuthenticationWithInvalidToken() throws Exception { request.addHeader("Authorization", "Bearer xxx"); filter.attemptAuthentication(request, response); }
@Test(expected = KeycloakAuthenticationException.class) public void testAttemptAuthenticationWithInvalidTokenBearerOnly() throws Exception { when(keycloakDeployment.isBearerOnly()).thenReturn(Boolean.TRUE); request.addHeader("Authorization", "Bearer xxx"); filter.attemptAuthentication(request, response); }