### Question:
DefaultLdapAttributeDetectionService implements LdapAttributeDetectionService { @Override public AmbariLdapConfiguration detectLdapUserAttributes(AmbariLdapConfiguration ambariLdapConfiguration) throws AmbariLdapException { LOG.info("Detecting LDAP user attributes ..."); if (Strings.isEmpty(ambariLdapConfiguration.userSearchBase())) { LOG.warn("No user search base provided"); return ambariLdapConfiguration; } try { LdapConnectionTemplate ldapConnectionTemplate = ldapConnectionTemplateFactory.create(ambariLdapConfiguration); AttributeDetector<Entry> userAttributeDetector = attributeDetectorFactory.userAttributDetector(); SearchRequest searchRequest = assembleUserSearchRequest(ldapConnectionTemplate, ambariLdapConfiguration); List<Entry> entries = ldapConnectionTemplate.search(searchRequest, getEntryMapper()); for (Entry entry : entries) { LOG.info("Collecting user attribute information from the sample entry with dn: [{}]", entry.getDn()); userAttributeDetector.collect(entry); } Map<String, String> detectedUserAttributes = userAttributeDetector.detect(); setDetectedAttributes(ambariLdapConfiguration, detectedUserAttributes); LOG.info("Decorated ambari ldap config : [{}]", ambariLdapConfiguration); } catch (Exception e) { LOG.error("Ldap operation failed while detecting user attributes", e); throw new AmbariLdapException(e); } return ambariLdapConfiguration; } @Inject  DefaultLdapAttributeDetectionService(); @Override AmbariLdapConfiguration detectLdapUserAttributes(AmbariLdapConfiguration ambariLdapConfiguration); @Override AmbariLdapConfiguration detectLdapGroupAttributes(AmbariLdapConfiguration ambariLdapConfiguration); EntryMapper<Entry> getEntryMapper();  }### Answer:
@Test @SuppressWarnings("unchecked") public void shouldLdapUserAttributeDetection() throws Exception { Map<String, String> configMap = Maps.newHashMap(); configMap.put(AmbariServerConfigurationKey.USER_SEARCH_BASE.key(), "dc=example,dc=com"); AmbariLdapConfiguration ldapConfiguration = new AmbariLdapConfiguration(configMap); List<Object> entryList = Lists.newArrayList(new DefaultEntry("uid=gauss")); EasyMock.expect(ldapConnectionTemplateFactoryMock.create(ldapConfiguration)).andReturn(ldapConnectionTemplateMock); EasyMock.expect(ldapConnectionTemplateMock.search(EasyMock.anyObject(SearchRequest.class), EasyMock.anyObject(entryMapperMock().getClass()))) .andReturn(entryList); EasyMock.expect(ldapConnectionTemplateMock.newSearchRequest(EasyMock.anyString(), EasyMock.anyString(), EasyMock.anyObject(SearchScope.class))).andReturn(searchRequestMock); EasyMock.expect(attributeDetectorFactoryMock.userAttributDetector()) .andReturn(new ChainedAttributeDetector(Sets.newHashSet(new UserNameAttrDetector()))); EasyMock.expect(searchRequestMock.setSizeLimit(50)).andReturn(searchRequestMock); replayAll(); AmbariLdapConfiguration decorated = defaultLdapAttributeDetectionService.detectLdapUserAttributes(ldapConfiguration); Assert.assertNotNull(decorated); Assert.assertEquals("N/A", ldapConfiguration.userNameAttribute()); }
@Test(expected = AmbariLdapException.class) public void testShouldUserAttributeDetectionFailWhenLdapOerationFails() throws Exception { Map<String, String> configMap = Maps.newHashMap(); configMap.put(AmbariServerConfigurationKey.USER_SEARCH_BASE.key(), "dc=example,dc=com"); AmbariLdapConfiguration ldapConfiguration = new AmbariLdapConfiguration(configMap); EasyMock.expect(ldapConnectionTemplateFactoryMock.create(ldapConfiguration)).andThrow(new AmbariLdapException("Testing ...")); replayAll(); AmbariLdapConfiguration decorated = defaultLdapAttributeDetectionService.detectLdapUserAttributes(ldapConfiguration); }