@Override
  public Set<String> getAssociatedRoles() {
    Subject subject = org.apache.shiro.SecurityUtils.getSubject();
    Set<String> roles = new HashSet<>();
    Map<String, String> allRoles = null;

    if (subject.isAuthenticated()) {
      Collection<Realm> realmsList = getRealmsList();
      for (Realm realm : realmsList) {
        String name = realm.getClass().getName();
        if (INI_REALM.equals(name)) {
          allRoles = ((IniRealm) realm).getIni().get("roles");
          break;
        } else if (LDAP_REALM.equals(name)) {
          try {
            AuthorizationInfo auth =
                ((LdapRealm) realm)
                    .queryForAuthorizationInfo(
                        new SimplePrincipalCollection(subject.getPrincipal(), realm.getName()),
                        ((LdapRealm) realm).getContextFactory());
            if (auth != null) {
              roles = new HashSet<>(auth.getRoles());
            }
          } catch (NamingException e) {
            LOGGER.error("Can't fetch roles", e);
          }
          break;
        } else if (ACTIVE_DIRECTORY_GROUP_REALM.equals(name)) {
          allRoles = ((ActiveDirectoryGroupRealm) realm).getListRoles();
          break;
        } else if (realm instanceof KnoxJwtRealm) {
          roles = ((KnoxJwtRealm) realm).mapGroupPrincipals(getPrincipal());
          break;
        }
      }
      if (allRoles != null) {
        for (Map.Entry<String, String> pair : allRoles.entrySet()) {
          if (subject.hasRole(pair.getKey())) {
            roles.add(pair.getKey());
          }
        }
      }
    }
    return roles;
  }
@Test
  void testKnoxGetRoles() {
    setupPrincipalName("test");

    KnoxJwtRealm realm = spy(new KnoxJwtRealm());
    LifecycleUtils.init(realm);
    Set<String> testRoles = new HashSet<String>();
    testRoles.add("role1");
    testRoles.add("role2");

    when(realm.mapGroupPrincipals("test")).thenReturn(testRoles);

    DefaultSecurityManager securityManager = new DefaultSecurityManager(realm);
    ThreadContext.bind(securityManager);

    Set<String> roles = shiroSecurityService.getAssociatedRoles();
    assertEquals(testRoles, roles);
  }