### Question:
AmbariProxiedUserDetailsService implements UserDetailsService { boolean validateGroup(AmbariTProxyConfiguration tProxyConfiguration, String proxyUserName, UserEntity userEntity) { String allowedGroups = tProxyConfiguration.getAllowedGroups(proxyUserName); if (StringUtils.isNotEmpty(allowedGroups)) { Set<String> groupSpecs = Arrays.stream(allowedGroups.split("\\s*,\\s*")) .map(s -> s.trim().toLowerCase()) .collect(toSet()); if (groupSpecs.contains("*")) { return true; } else { Set<MemberEntity> memberEntities = userEntity.getMemberEntities(); if (memberEntities != null) { for (MemberEntity memberEntity : memberEntities) { GroupEntity group = memberEntity.getGroup(); if (group != null) { String groupName = group.getGroupName(); if (StringUtils.isNotEmpty(groupName) && groupSpecs.contains(groupName.toLowerCase())) { return true; } } } } } } return false; } AmbariProxiedUserDetailsService(Configuration configuration, Users users); @Override UserDetails loadUserByUsername(String proxiedUserName); UserDetails loadProxiedUser(String proxiedUserName,
                                     String proxyUserName,
                                     TrustedProxyAuthenticationDetails trustedProxyAuthenticationDetails);  }### Answer:
@Test public void testValidateGroup() { AmbariProxiedUserDetailsService service = new AmbariProxiedUserDetailsService(createNiceMock(Configuration.class), createNiceMock(Users.class)); AmbariTProxyConfiguration tproxyConfigration = createMock(AmbariTProxyConfiguration.class); expect(tproxyConfigration.getAllowedGroups("proxyUser")).andReturn("*").once(); expect(tproxyConfigration.getAllowedGroups("proxyUser")).andReturn("validGroup").once(); expect(tproxyConfigration.getAllowedGroups("proxyUser")).andReturn("validgroup").once(); expect(tproxyConfigration.getAllowedGroups("proxyUser")).andReturn("validGroup, *").once(); expect(tproxyConfigration.getAllowedGroups("proxyUser")).andReturn("").once(); expect(tproxyConfigration.getAllowedGroups("proxyUser")).andReturn(null).once(); expect(tproxyConfigration.getAllowedGroups("proxyUser")).andReturn("notValidGroup").once(); Set<MemberEntity> memberEntities = new HashSet<>(); memberEntities.add(createMockMemberEntity("validGroup")); memberEntities.add(createMockMemberEntity("users")); memberEntities.add(createMockMemberEntity(null)); MemberEntity memberEntity = createMock(MemberEntity.class); expect(memberEntity.getGroup()).andReturn(null).anyTimes(); memberEntities.add(memberEntity); UserEntity userEntity = createMock(UserEntity.class); expect(userEntity.getMemberEntities()).andReturn(memberEntities).anyTimes(); replayAll(); Assert.assertTrue(service.validateGroup(tproxyConfigration, "proxyUser", userEntity)); Assert.assertTrue(service.validateGroup(tproxyConfigration, "proxyUser", userEntity)); Assert.assertTrue(service.validateGroup(tproxyConfigration, "proxyUser", userEntity)); Assert.assertTrue(service.validateGroup(tproxyConfigration, "proxyUser", userEntity)); Assert.assertFalse(service.validateGroup(tproxyConfigration, "proxyUser", userEntity)); Assert.assertFalse(service.validateGroup(tproxyConfigration, "proxyUser", userEntity)); Assert.assertFalse(service.validateGroup(tproxyConfigration, "proxyUser", userEntity)); verifyAll(); }