### Question:
ODLJndiLdapRealm extends DefaultLdapRealm { protected Set<String> getRoleNamesForUser(final String username, final LdapContext ldapContext) throws NamingException { final Set<String> roleNames = new LinkedHashSet<>(); final SearchControls searchControls = createSearchControls(); LOG.debug("Asking the configured LDAP about which groups uid=\"{}\" belongs to using " + "searchBase=\"{}\" ldapAttributeForComparison=\"{}\"", username, searchBase, ldapAttributeForComparison); final NamingEnumeration<SearchResult> answer = ldapContext .search(searchBase, String.format("%s=%s", UID, username), searchControls); while (answer.hasMoreElements()) { final SearchResult searchResult = answer.next(); final Attributes attrs = searchResult.getAttributes(); if (attrs != null) { final NamingEnumeration<? extends Attribute> ae = attrs.getAll(); while (ae.hasMore()) { final Attribute attr = ae.next(); LOG.debug("LDAP returned \"{}\" attribute for \"{}\"", attr.getID(), username); if (attr.getID().equals(ldapAttributeForComparison)) { final Collection<String> groupNamesExtractedFromLdap = LdapUtils.getAllAttributeValues(attr); final Map<String, Set<String>> groupsToRoles = this.GROUPS_TO_ROLES_MAPPING_STRATEGY .mapGroupsToRoles(groupNamesExtractedFromLdap, ROLE_NAMES_DELIMITER, groupRolesMap); final Collection<String> roleNamesFromLdapGroups; if (groupRolesMap != null) { roleNamesFromLdapGroups = new HashSet<>(); for (Set<String> roles : groupsToRoles.values()) { roleNamesFromLdapGroups.addAll(roles); } if (LOG.isDebugEnabled()) { for (Entry<String, Set<String>> entry : groupsToRoles.entrySet()) { LOG.debug("Mapped the \"{}\" LDAP group to \"{}\" ODL role for \"{}\"", entry.getKey(), entry.getValue(), username); } } } else { LOG.debug("Since groupRolesMap was unspecified, no mapping is attempted so " + "the role names are set to the extracted group names"); roleNamesFromLdapGroups = groupNamesExtractedFromLdap; if (LOG.isDebugEnabled()) { for (String group : groupNamesExtractedFromLdap) { LOG.debug("Mapped the \"{}\" LDAP group to \"{}\" ODL role for \"{}\"", group, group, username); } } } roleNames.addAll(roleNamesFromLdapGroups); } } } } return roleNames; } ODLJndiLdapRealm(); static String getUsername(AuthenticationToken token); static AuthorizationInfo buildAuthorizationInfo(final Set<String> roleNames); @Override String getUserDnSuffix(); void setSearchBase(final String searchBase); void setLdapAttributeForComparison(final String ldapAttributeForComparison); void setGroupRolesMap(final Map<String, String> groupRolesMap);  }### Answer:
@Test public void testGetRoleNamesForUser() throws NamingException { ODLJndiLdapRealm ldapRealm = new ODLJndiLdapRealm(); LdapContext ldapContext = mock(LdapContext.class); when( ldapContext.search((String) any(), (String) any(), any())).thenReturn(new TestNamingEnumeration()); Set<String> roles = ldapRealm.getRoleNamesForUser("testuser", ldapContext); assertFalse(roles.isEmpty()); assertTrue(roles.iterator().next().equals("engineering")); }