### Question:
InfraUserRolesLookupStrategy { public Set<String> getUserRolesFromPrincipal(Map<String, Set<String>> usersVsRoles, Principal principal) { if (principal instanceof AuthenticationToken) { AuthenticationToken authenticationToken = (AuthenticationToken) principal; KerberosName kerberosName = new KerberosName(authenticationToken.getName()); Set<String> rolesResult = usersVsRoles.get(String.format("%s@%s", kerberosName.getServiceName(), kerberosName.getRealm())); if (CollectionUtils.isEmpty(rolesResult)) { rolesResult = usersVsRoles.get(principal.getName()); } return rolesResult; } else { return usersVsRoles.get(principal.getName()); } }  Set<String> getUserRolesFromPrincipal(Map<String, Set<String>> usersVsRoles, Principal principal);  }### Answer:
@Test public void testLookupRolesForPrincipalName() { Map<String, Set<String>> usersVsRoles = generateUserRolesMap(); AuthenticationToken principal = new AuthenticationToken( "logsearch", "logsearch/c6401.ambari.apache.org@EXAMPLE.COM", "kerberos"); Set<String> result = underTest.getUserRolesFromPrincipal(usersVsRoles, principal); assertTrue(result.contains("logsearch_user")); assertTrue(result.contains("ranger_user")); assertFalse(result.contains("admin")); }
@Test public void testLookupRolesForNonKerberosPrincipalName() { Map<String, Set<String>> usersVsRoles = generateUserRolesMap(); BasicUserPrincipal principal = new BasicUserPrincipal("infra-solr"); Set<String> result = underTest.getUserRolesFromPrincipal(usersVsRoles, principal); assertTrue(result.contains("admin")); assertTrue(result.contains("logsearch_user")); }
@Test public void testLookupRolesWithNonKerberosPrincipalWithoutRoles() { Map<String, Set<String>> usersVsRoles = generateUserRolesMap(); BasicUserPrincipal principal = new BasicUserPrincipal("unknownuser"); Set<String> result = underTest.getUserRolesFromPrincipal(usersVsRoles, principal); assertTrue(result.isEmpty()); }