public static UserInfo map(SecurityContext context) {
      Authentication authentication = context.getAuthentication();

      if (authentication instanceof JwtAuthenticationToken jwtToken) {
         Jwt jwt = jwtToken.getToken();

         String[] microcksGroups = new String[] {};
         if (jwt.hasClaim(MICROCKS_GROUPS_TOKEN_CLAIM)) {
            microcksGroups = jwt.getClaimAsStringList(MICROCKS_GROUPS_TOKEN_CLAIM).toArray(String[]::new);
         }

         // Create and return UserInfo.
         UserInfo userInfo = new UserInfo(jwt.getClaimAsString(NAME), jwt.getClaimAsString(PREFERRED_USERNAME),
               jwt.getClaimAsString(GIVEN_NAME), jwt.getClaimAsString(FAMILY_NAME), jwt.getClaimAsString(EMAIL),
               authentication.getAuthorities().stream()
                     .map(grantedAuthority -> grantedAuthority.getAuthority().replace("ROLE_", ""))
                     .toArray(String[]::new),
               microcksGroups);
         log.debug("Current user is: {}", userInfo);
         return userInfo;
      }
      return null;
   }
@Test
   void testMap() {
      // UserInfo mapper to test.
      KeycloakTokenToUserInfoMapper mapper = new KeycloakTokenToUserInfoMapper();

      // Prepare a Security Context.
      MicrocksJwtConverter converter = new MicrocksJwtConverter();
      Jwt jwt = null;

      try {
         JWT parsedJwt = JWTParser.parse(jwtBearer);
         jwt = MicrocksJwtConverterTest.createJwt(jwtBearer, parsedJwt);
      } catch (Exception e) {
         fail("Parsing Jwt bearer should not fail");
      }

      // Convert and assert granted authorities.
      JwtAuthenticationToken authenticationToken = converter.convert(jwt);
      SecurityContext context = new SecurityContextImpl(authenticationToken);

      // Execute and assert user data.
      UserInfo userInfo = mapper.map(context);

      assertEquals("Pastry Manager", userInfo.getName());
      assertEquals("pastry-manager", userInfo.getUsername());
      assertEquals("Pastry", userInfo.getGivenName());
      assertEquals("Manager", userInfo.getFamilyName());
      assertNull(userInfo.getEmail());

      assertEquals(2, userInfo.getRoles().length);
      assertTrue(Arrays.stream(userInfo.getRoles()).toList().contains("user"));
      assertTrue(Arrays.stream(userInfo.getRoles()).toList().contains("manager"));

      assertEquals(1, userInfo.getGroups().length);
      assertTrue(Arrays.stream(userInfo.getGroups()).toList().contains("/microcks/manager/pastry"));
   }