### Question:
KeycloakClientRequestFactory extends HttpComponentsClientHttpRequestFactory implements ClientHttpRequestFactory { protected KeycloakSecurityContext getKeycloakSecurityContext() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); KeycloakAuthenticationToken token; KeycloakSecurityContext context; if (authentication == null) { throw new IllegalStateException("Cannot set authorization header because there is no authenticated principal"); } if (!KeycloakAuthenticationToken.class.isAssignableFrom(authentication.getClass())) { throw new IllegalStateException( String.format( "Cannot set authorization header because Authentication is of type %s but %s is required", authentication.getClass(), KeycloakAuthenticationToken.class) ); } token = (KeycloakAuthenticationToken) authentication; context = token.getAccount().getKeycloakSecurityContext(); return context; } KeycloakClientRequestFactory();  static final String AUTHORIZATION_HEADER; }### Answer:
@Test public void testGetKeycloakSecurityContext() throws Exception { KeycloakSecurityContext context = factory.getKeycloakSecurityContext(); assertNotNull(context); assertEquals(keycloakSecurityContext, context); }
@Test(expected = IllegalStateException.class) public void testGetKeycloakSecurityContextInvalidAuthentication() throws Exception { SecurityContextHolder.getContext().setAuthentication( new PreAuthenticatedAuthenticationToken("foo", "bar", Collections.singleton(new KeycloakRole("baz")))); factory.getKeycloakSecurityContext(); }
@Test(expected = IllegalStateException.class) public void testGetKeycloakSecurityContextNullAuthentication() throws Exception { SecurityContextHolder.clearContext(); factory.getKeycloakSecurityContext(); }