### Question:
InfraKerberosHostValidator { public boolean validate(Principal principal, Map<String, Set<String>> userVsHosts, Map<String, String> userVsHostRegex) { if (principal instanceof AuthenticationToken) { AuthenticationToken authenticationToken = (AuthenticationToken) principal; KerberosName kerberosName = new KerberosName(authenticationToken.getName()); String hostname = kerberosName.getHostName(); String serviceUserName = kerberosName.getServiceName(); if (MapUtils.isNotEmpty(userVsHostRegex)) { String regex = userVsHostRegex.get(serviceUserName); return hostname.matches(regex); } if (MapUtils.isNotEmpty(userVsHosts)) { Set<String> hosts = userVsHosts.get(serviceUserName); if (CollectionUtils.isNotEmpty(hosts)) { return hosts.contains(hostname); } } } return true; }  boolean validate(Principal principal, Map<String, Set<String>> userVsHosts, Map<String, String> userVsHostRegex);  }### Answer:
@Test public void testValidateHosts() { Map<String, Set<String>> userHostsMap = generateUserHostMap("c6401.ambari.apache.org"); boolean result = underTest.validate(principal, userHostsMap, new HashMap<String, String>()); assertTrue(result); }
@Test public void testValidateHostsValid() { Map<String, Set<String>> userHostsMap = generateUserHostMap("c6402.ambari.apache.org"); boolean result = underTest.validate(principal, userHostsMap, new HashMap<String, String>()); assertFalse(result); }
@Test public void testValidateHostRegex() { Map<String, String> userHostRegex = generateRegexMap("c\\d+.*.apache.org"); boolean result = underTest.validate(principal, new HashMap<String, Set<String>>(), userHostRegex); assertTrue(result); }
@Test public void testValidateHostRegexInvalid() { Map<String, String> userHostRegex = generateRegexMap("c\\d+.*.org.apache"); boolean result = underTest.validate(principal, new HashMap<String, Set<String>>(), userHostRegex); assertFalse(result); }
@Test public void testPrecedence() { Map<String, Set<String>> userHostsMap = generateUserHostMap("c6402.ambari.apache.org"); Map<String, String> userHostRegex = generateRegexMap("c\\d+.*.apache.org"); boolean result = underTest.validate(principal, userHostsMap, userHostRegex); assertTrue(result); }