### Question:
DefaultLdapConfigurationService implements LdapConfigurationService { @Override public Set<String> checkGroupAttributes(String userDn, AmbariLdapConfiguration ambariLdapConfiguration) throws AmbariLdapException { List<String> groups = Lists.newArrayList(); try { LOGGER.info("Checking group attributes for user dn: [{}] ...", userDn); String filter = FilterBuilder.and( FilterBuilder.equal(SchemaConstants.OBJECT_CLASS_AT, ambariLdapConfiguration.groupObjectClass()), FilterBuilder.equal(ambariLdapConfiguration.groupMemberAttribute(), userDn) ).toString(); LOGGER.info("Searching for the groups the user dn: [{}] is member of using the search filter: [{}]", userDn, filter); LdapConnectionTemplate ldapConnectionTemplate = ldapConnectionTemplateFactory.create(ambariLdapConfiguration); SearchRequest searchRequest = ldapConnectionTemplate.newSearchRequest(new Dn(ambariLdapConfiguration.groupSearchBase()), filter, SearchScope.SUBTREE); searchRequest.addAttributes(ambariLdapConfiguration.groupMemberAttribute(), ambariLdapConfiguration.groupNameAttribute()); groups = ldapConnectionTemplate.search(searchRequest, getGroupNameEntryMapper(ambariLdapConfiguration)); if (groups == null || groups.isEmpty()) { LOGGER.info("No groups found for the user dn. Group attributes configuration is incomplete"); throw new AmbariLdapException("Group attribute ldap configuration is incomplete"); } LOGGER.info("Group attribute configuration check succeeded."); } catch (Exception e) { LOGGER.error("User attributes validation failed.", e); throw new AmbariLdapException(e.getMessage(), e); } return new HashSet<>(groups); } @Inject  DefaultLdapConfigurationService(); @Override void checkConnection(AmbariLdapConfiguration ambariLdapConfiguration); @Override String checkUserAttributes(String testUserName, String testPassword, AmbariLdapConfiguration ambariLdapConfiguration); @Override Set<String> checkGroupAttributes(String userDn, AmbariLdapConfiguration ambariLdapConfiguration);  }### Answer:
@Test public void testShouldGroupAttributeConfigurationCheckSucceedWhenGroupForUserDnIsFound() throws Exception { Map<String, String> configMap = groupConfigObjectMap(); SearchRequest sr = new SearchRequestImpl(); AmbariLdapConfiguration ambariLdapConfiguration = new AmbariLdapConfiguration(configMap); EasyMock.expect(ldapConnectionTemplateFactory.create(ambariLdapConfiguration)).andReturn(ldapConnectionTemplateMock); EasyMock.expect(ldapConnectionTemplateMock.newSearchRequest(EasyMock.anyObject(Dn.class), EasyMock.anyString(), EasyMock.anyObject(SearchScope.class))).andReturn(sr); EasyMock.expect(ldapConnectionTemplateMock.search(EasyMock.anyObject(SearchRequest.class), EasyMock.anyObject(EntryMapper.class))) .andReturn(Lists.newArrayList("userGroup")); replayAll(); Set<String> userGroups = ldapConfigurationService.checkGroupAttributes("userDn", ambariLdapConfiguration); Assert.assertNotNull("No groups found", userGroups); }
@Test(expected = AmbariLdapException.class) public void testShouldGroupAttributeConfigurationCheckFailWhenNoGroupsForUserDnFound() throws Exception { Map<String, String> configMap = groupConfigObjectMap(); SearchRequest sr = new SearchRequestImpl(); AmbariLdapConfiguration ambariLdapConfiguration = new AmbariLdapConfiguration(configMap); EasyMock.expect(ldapConnectionTemplateFactory.create(ambariLdapConfiguration)).andReturn(ldapConnectionTemplateMock); EasyMock.expect(ldapConnectionTemplateMock.newSearchRequest(EasyMock.anyObject(Dn.class), EasyMock.anyString(), EasyMock.anyObject(SearchScope.class))).andReturn(sr); EasyMock.expect(ldapConnectionTemplateMock.search(EasyMock.anyObject(SearchRequest.class), EasyMock.anyObject(EntryMapper.class))) .andReturn(Lists.newArrayList()); replayAll(); Set<String> userGroups = ldapConfigurationService.checkGroupAttributes("userDn", ambariLdapConfiguration); Assert.assertNotNull("No groups found", userGroups); }