### Question:
KeycloakSecurityContextClientRequestInterceptor implements ClientHttpRequestInterceptor { protected KeycloakSecurityContext getKeycloakSecurityContext() { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); Principal principal = attributes.getRequest().getUserPrincipal(); if (principal == null) { throw new IllegalStateException("Cannot set authorization header because there is no authenticated principal"); } if (!(principal instanceof KeycloakPrincipal)) { throw new IllegalStateException( String.format( "Cannot set authorization header because the principal type %s does not provide the KeycloakSecurityContext", principal.getClass())); } return ((KeycloakPrincipal) principal).getKeycloakSecurityContext(); }  @Override ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution clientHttpRequestExecution);  }### Answer:
@Test public void testGetKeycloakSecurityContext() throws Exception { KeycloakSecurityContext context = factory.getKeycloakSecurityContext(); assertNotNull(context); assertEquals(keycloakSecurityContext, context); }
@Test(expected = IllegalStateException.class) public void testGetKeycloakSecurityContextInvalidPrincipal() throws Exception { servletRequest.setUserPrincipal(new MarkerPrincipal()); factory.getKeycloakSecurityContext(); }
@Test(expected = IllegalStateException.class) public void testGetKeycloakSecurityContextNullAuthentication() throws Exception { servletRequest.setUserPrincipal(null); factory.getKeycloakSecurityContext(); }