AMBARI-21307 added javadocs, organized and cleaned the code

Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/4ad492b4
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/4ad492b4
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/4ad492b4

Branch: refs/heads/feature-branch-AMBARI-21307
Commit: 4ad492b4a160e86a9e853aee9be063f8ab04619d
Parents: 953a98f
Author: lpuskas <lpus...@apache.org>
Authored: Wed Sep 13 15:27:39 2017 +0200
Committer: lpuskas <lpus...@apache.org>
Committed: Thu Oct 12 19:25:51 2017 +0200

----------------------------------------------------------------------
 .../server/ldap/LdapConfigurationFactory.java   |  13 +
 .../server/ldap/LdapConfigurationService.java   |  60 -----
 .../apache/ambari/server/ldap/LdapModule.java   |   6 +-
 .../server/ldap/service/AmbariLdapFacade.java   |   9 +-
 .../server/ldap/service/AttributeDetector.java  |   3 +-
 .../ldap/service/LdapConfigurationService.java  |  60 +++++
 .../ambari/server/ldap/service/LdapFacade.java  |  10 +-
 .../ads/DefaultAttributeDetectionService.java   | 238 -------------------
 .../DefaultLdapAttributeDetectionService.java   | 238 +++++++++++++++++++
 .../ads/DefaultLdapConfigurationService.java    |  17 +-
 .../detectors/UserGroupMemberAttrDetector.java  |   1 -
 .../DefaultAttributeDetectionServiceTest.java   | 108 ---------
 ...efaultLdapAttributeDetectionServiceTest.java | 108 +++++++++
 .../DefaultLdapConfigurationServiceTest.java    |   2 +-
 14 files changed, 453 insertions(+), 420 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/4ad492b4/ambari-server/src/main/java/org/apache/ambari/server/ldap/LdapConfigurationFactory.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/ldap/LdapConfigurationFactory.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/ldap/LdapConfigurationFactory.java
index 57cdf6e..67ac092 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/ldap/LdapConfigurationFactory.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/ldap/LdapConfigurationFactory.java
@@ -16,6 +16,19 @@ package org.apache.ambari.server.ldap;
 
 import java.util.Map;
 
+/**
+ * Factory interface for AmbariLdapConfiguration instances.
+ * It's registered as a factory in the GUICE context ( so no implementations 
required)
+ *
+ * To be extended with other factory methods upon needs.
+ */
 public interface LdapConfigurationFactory {
+
+  /**
+   * Creates an AmbariLdapConfiguration instance with the provided map of 
configuration settings.
+   *
+   * @param configuration a map where keys are the configuration properties 
and values are the configuration values
+   * @return an AmbariLdapConfiguration instance
+   */
   AmbariLdapConfiguration createLdapConfiguration(Map<String, Object> 
configuration);
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ambari/blob/4ad492b4/ambari-server/src/main/java/org/apache/ambari/server/ldap/LdapConfigurationService.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/ldap/LdapConfigurationService.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/ldap/LdapConfigurationService.java
deleted file mode 100644
index 69a641f..0000000
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/ldap/LdapConfigurationService.java
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.ldap;
-
-import java.util.Set;
-
-import org.apache.ambari.server.AmbariException;
-import org.apache.ambari.server.ldap.service.AmbariLdapException;
-import org.apache.directory.ldap.client.api.LdapConnection;
-
-/**
- * Collection of operations for validating ldap configuration.
- * It's intended to decouple implementations using different libraries.
- */
-public interface LdapConfigurationService {
-
-  /**
-   * Tests the connection based on the provided configuration.
-   *
-   * @param ldapConnection connection instance
-   * @param configuration  the ambari ldap configuration instance
-   * @throws AmbariLdapException if the connection is not possible
-   */
-  void checkConnection(LdapConnection ldapConnection, AmbariLdapConfiguration 
configuration) throws AmbariLdapException;
-
-
-  /**
-   * Implements LDAP user related configuration settings validation logic.
-   * Implementers communicate with the LDAP server (search, bind) to validate 
attributes in the provided configuration
-   * instance
-   *
-   * @param ldapConnection connection instance used to connect to the LDAP 
server
-   * @param testUserName   the test username
-   * @param testPassword   the test password
-   * @param configuration  the available ldap configuration
-   * @return The DN of the found user entry
-   * @throws AmbariException if the connection couldn't be estabilisheds
-   */
-  String checkUserAttributes(LdapConnection ldapConnection, String 
testUserName, String testPassword, AmbariLdapConfiguration configuration) 
throws AmbariLdapException;
-
-  /**
-   * Checks whether the group related LDAP attributes in the configuration are 
correct.
-   *
-   * @throws AmbariException if the attributes are not valid
-   */
-  Set<String> checkGroupAttributes(LdapConnection ldapConnection, String 
userDn, AmbariLdapConfiguration ambariLdapConfiguration) throws 
AmbariLdapException;
-
-}

http://git-wip-us.apache.org/repos/asf/ambari/blob/4ad492b4/ambari-server/src/main/java/org/apache/ambari/server/ldap/LdapModule.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/ldap/LdapModule.java 
b/ambari-server/src/main/java/org/apache/ambari/server/ldap/LdapModule.java
index 5d6a698..0be69d4 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/ldap/LdapModule.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/ldap/LdapModule.java
@@ -18,9 +18,10 @@ package org.apache.ambari.server.ldap;
 import org.apache.ambari.server.ldap.service.AmbariLdapConfigurationProvider;
 import org.apache.ambari.server.ldap.service.AmbariLdapFacade;
 import org.apache.ambari.server.ldap.service.LdapAttributeDetectionService;
+import org.apache.ambari.server.ldap.service.LdapConfigurationService;
 import org.apache.ambari.server.ldap.service.LdapConnectionService;
 import org.apache.ambari.server.ldap.service.LdapFacade;
-import 
org.apache.ambari.server.ldap.service.ads.DefaultAttributeDetectionService;
+import 
org.apache.ambari.server.ldap.service.ads.DefaultLdapAttributeDetectionService;
 import 
org.apache.ambari.server.ldap.service.ads.DefaultLdapConfigurationService;
 import org.apache.ambari.server.ldap.service.ads.DefaultLdapConnectionService;
 
@@ -37,8 +38,9 @@ public class LdapModule extends AbstractModule {
     bind(LdapFacade.class).to(AmbariLdapFacade.class);
     
bind(LdapConfigurationService.class).to(DefaultLdapConfigurationService.class);
     bind(LdapConnectionService.class).to(DefaultLdapConnectionService.class);
-    
bind(LdapAttributeDetectionService.class).to(DefaultAttributeDetectionService.class);
+    
bind(LdapAttributeDetectionService.class).to(DefaultLdapAttributeDetectionService.class);
 
+    // this binding requires the JPA module!
     
bind(AmbariLdapConfiguration.class).toProvider(AmbariLdapConfigurationProvider.class);
 
     install(new FactoryModuleBuilder().build(LdapConfigurationFactory.class));

http://git-wip-us.apache.org/repos/asf/ambari/blob/4ad492b4/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/AmbariLdapFacade.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/AmbariLdapFacade.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/AmbariLdapFacade.java
index 719bb7b..689a072 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/AmbariLdapFacade.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/AmbariLdapFacade.java
@@ -22,7 +22,6 @@ import javax.inject.Inject;
 import javax.inject.Singleton;
 
 import org.apache.ambari.server.ldap.AmbariLdapConfiguration;
-import org.apache.ambari.server.ldap.LdapConfigurationService;
 import org.apache.directory.ldap.client.api.LdapConnection;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -32,9 +31,12 @@ public class AmbariLdapFacade implements LdapFacade {
 
   private static final Logger LOGGER = 
LoggerFactory.getLogger(AmbariLdapFacade.class);
 
+  /**
+   * Additional parameters expected to be provided along with the configuration
+   */
   private enum Parameters {
-    TEST_USER_NAME("ldap.test.user.name"),
-    TEST_USER_PASSWORD("ldap.test.user.password");
+    TEST_USER_NAME("ambari.ldap.test.user.name"),
+    TEST_USER_PASSWORD("ambari.ldap.test.user.password");
 
     private String parameterKey;
 
@@ -68,6 +70,7 @@ public class AmbariLdapFacade implements LdapFacade {
 
       LOGGER.info("Validating LDAP connection related configuration based on: 
{}", ambariLdapConfiguration);
       connection = 
ldapConnectionService.getBoundLdapConnection(ambariLdapConfiguration);
+
       ldapConfigurationService.checkConnection(connection, 
ambariLdapConfiguration);
       LOGGER.info("Validating LDAP connection related configuration: SUCCESS");
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/4ad492b4/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/AttributeDetector.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/AttributeDetector.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/AttributeDetector.java
index 3e20a6a..1b28148 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/AttributeDetector.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/AttributeDetector.java
@@ -16,7 +16,8 @@ package org.apache.ambari.server.ldap.service;
 
 /**
  * Operations for detecting LDAP related settings.
- * Attributes and values are detected based on a sample set of results 
returned from a search
+ * The basis for the attribute or value detection is a set of entries returned 
by a search operation.
+ * Individual attribute detector implementations are responsible for detecting 
a specific set of attributes or values
  */
 public interface AttributeDetector<T> {
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/4ad492b4/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/LdapConfigurationService.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/LdapConfigurationService.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/LdapConfigurationService.java
new file mode 100644
index 0000000..06566cc
--- /dev/null
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/LdapConfigurationService.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.server.ldap.service;
+
+import java.util.Set;
+
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.ldap.AmbariLdapConfiguration;
+import org.apache.directory.ldap.client.api.LdapConnection;
+
+/**
+ * Collection of operations for validating ldap configuration.
+ * It's intended to decouple implementations using different libraries.
+ */
+public interface LdapConfigurationService {
+
+  /**
+   * Tests the connection based on the provided configuration.
+   *
+   * @param ldapConnection connection instance
+   * @param configuration  the ambari ldap configuration instance
+   * @throws AmbariLdapException if the connection is not possible
+   */
+  void checkConnection(LdapConnection ldapConnection, AmbariLdapConfiguration 
configuration) throws AmbariLdapException;
+
+
+  /**
+   * Implements LDAP user related configuration settings validation logic.
+   * Implementers communicate with the LDAP server (search, bind) to validate 
attributes in the provided configuration
+   * instance
+   *
+   * @param ldapConnection connection instance used to connect to the LDAP 
server
+   * @param testUserName   the test username
+   * @param testPassword   the test password
+   * @param configuration  the available ldap configuration
+   * @return The DN of the found user entry
+   * @throws AmbariException if the connection couldn't be estabilisheds
+   */
+  String checkUserAttributes(LdapConnection ldapConnection, String 
testUserName, String testPassword, AmbariLdapConfiguration configuration) 
throws AmbariLdapException;
+
+  /**
+   * Checks whether the group related LDAP attributes in the configuration are 
correct.
+   *
+   * @throws AmbariException if the attributes are not valid
+   */
+  Set<String> checkGroupAttributes(LdapConnection ldapConnection, String 
userDn, AmbariLdapConfiguration ambariLdapConfiguration) throws 
AmbariLdapException;
+
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/4ad492b4/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/LdapFacade.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/LdapFacade.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/LdapFacade.java
index 6060d7f..3ae913c 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/LdapFacade.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/LdapFacade.java
@@ -22,6 +22,9 @@ import org.apache.ambari.server.ldap.AmbariLdapConfiguration;
 /**
  * The contract defining all the operations required by the application when 
communicating with an arbitrary LDAP server.
  * This interface is intended to decouple LDAP specific details from the 
application.
+ *
+ * Any operation that requires interaction with an LDAP server from within 
Ambari should go through this interface.
+ * (LDAP)
  */
 public interface LdapFacade {
 
@@ -35,9 +38,12 @@ public interface LdapFacade {
 
 
   /**
-   * Runs the user and group attribute detection algorithms
+   * Runs the user and group attribute detection algorithms.
+   * The method is not intended to be used as a coniguration factory, the 
returned instance may not be suitable for use.
    *
-   * @param ambariLdapConfiguration
+   * @param ambariLdapConfiguration partially filled configuration instance to 
be extended with detected properties
+   * @return a configuration instance, with properties filled with potentially 
correct values
+   * @throws AmbariLdapException
    */
   AmbariLdapConfiguration detectAttributes(AmbariLdapConfiguration 
ambariLdapConfiguration) throws AmbariLdapException;
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/4ad492b4/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultAttributeDetectionService.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultAttributeDetectionService.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultAttributeDetectionService.java
deleted file mode 100644
index e5254b5..0000000
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultAttributeDetectionService.java
+++ /dev/null
@@ -1,238 +0,0 @@
-/*
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.ldap.service.ads;
-
-import javax.inject.Inject;
-import javax.inject.Singleton;
-
-import org.apache.ambari.server.ldap.AmbariLdapConfiguration;
-import org.apache.ambari.server.ldap.service.AmbariLdapException;
-import org.apache.ambari.server.ldap.service.LdapAttributeDetectionService;
-import 
org.apache.ambari.server.ldap.service.ads.detectors.GroupMemberAttrDetector;
-import 
org.apache.ambari.server.ldap.service.ads.detectors.GroupNameAttrDetector;
-import 
org.apache.ambari.server.ldap.service.ads.detectors.GroupObjectClassDetector;
-import 
org.apache.ambari.server.ldap.service.ads.detectors.UserGroupMemberAttrDetector;
-import 
org.apache.ambari.server.ldap.service.ads.detectors.UserNameAttrDetector;
-import 
org.apache.ambari.server.ldap.service.ads.detectors.UserObjectClassDetector;
-import org.apache.directory.api.ldap.model.cursor.SearchCursor;
-import org.apache.directory.api.ldap.model.entry.Entry;
-import org.apache.directory.api.ldap.model.message.Response;
-import org.apache.directory.api.ldap.model.message.SearchRequest;
-import org.apache.directory.api.ldap.model.message.SearchRequestImpl;
-import org.apache.directory.api.ldap.model.message.SearchResultEntry;
-import org.apache.directory.api.ldap.model.message.SearchScope;
-import org.apache.directory.api.ldap.model.name.Dn;
-import org.apache.directory.api.util.Strings;
-import org.apache.directory.ldap.client.api.LdapConnection;
-import org.apache.directory.ldap.client.api.search.FilterBuilder;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-@Singleton
-public class DefaultAttributeDetectionService implements 
LdapAttributeDetectionService<LdapConnection> {
-
-  private static final Logger LOGGER = 
LoggerFactory.getLogger(DefaultAttributeDetectionService.class);
-  private static final int SAMPLE_RESULT_SIZE = 50;
-
-
-  @Inject
-  private UserNameAttrDetector userNameAttrDetector;
-
-  @Inject
-  private UserObjectClassDetector userObjectClassDetector;
-
-  @Inject
-  private UserGroupMemberAttrDetector userGroupMemberAttrDetector;
-
-  @Inject
-  private GroupNameAttrDetector groupNameAttrDetector;
-
-  @Inject
-  private GroupObjectClassDetector groupObjectClassDetector;
-
-  @Inject
-  private GroupMemberAttrDetector groupMemberAttrDetector;
-
-  @Inject
-  public DefaultAttributeDetectionService() {
-  }
-
-  @Override
-  public AmbariLdapConfiguration detectLdapUserAttributes(LdapConnection 
connection, AmbariLdapConfiguration ambariLdapConfiguration) {
-    LOGGER.info("Detecting LDAP user attributes ...");
-
-    // perform a search using the user search base
-    if (Strings.isEmpty(ambariLdapConfiguration.userSearchBase())) {
-      LOGGER.warn("No user search base provided");
-      return ambariLdapConfiguration;
-    }
-
-    SearchCursor searchCursor = null;
-
-    try {
-
-      SearchRequest searchRequest = 
assembleUserSearchRequest(ambariLdapConfiguration);
-
-      // do the search
-      searchCursor = connection.search(searchRequest);
-
-      int processedUserCnt = 0;
-
-      while (searchCursor.next()) {
-
-        if (processedUserCnt >= SAMPLE_RESULT_SIZE) {
-          LOGGER.debug("The maximum count of results for attribute detection 
has exceeded. Quit user attribute detection.");
-          break;
-        }
-
-        Response response = searchCursor.get();
-        // process the SearchResultEntry
-
-        if (response instanceof SearchResultEntry) {
-          Entry resultEntry = ((SearchResultEntry) response).getEntry();
-          LOGGER.info("Processing sample entry with dn: [{}]", 
resultEntry.getDn());
-
-          userNameAttrDetector.collect(resultEntry);
-          userObjectClassDetector.collect(resultEntry);
-          userGroupMemberAttrDetector.collect(resultEntry);
-
-          processedUserCnt++;
-        }
-      }
-
-      
ambariLdapConfiguration.setValueFor(AmbariLdapConfiguration.AmbariLdapConfig.USER_NAME_ATTRIBUTE,
 userNameAttrDetector.detect());
-      
ambariLdapConfiguration.setValueFor(AmbariLdapConfiguration.AmbariLdapConfig.USER_OBJECT_CLASS,
 userObjectClassDetector.detect());
-      
ambariLdapConfiguration.setValueFor(AmbariLdapConfiguration.AmbariLdapConfig.USER_GROUP_MEMBER_ATTRIBUTE,
 userGroupMemberAttrDetector.detect());
-
-      LOGGER.info("Decorated ambari ldap config : [{}]", 
ambariLdapConfiguration);
-
-    } catch (Exception e) {
-
-      LOGGER.error("Ldap operation failed", e);
-    } finally {
-      // housekeeping
-      if (null != searchCursor) {
-        searchCursor.close();
-      }
-    }
-
-    return ambariLdapConfiguration;
-  }
-
-
-  @Override
-  public AmbariLdapConfiguration detectLdapGroupAttributes(LdapConnection 
connection, AmbariLdapConfiguration ambariLdapConfiguration) {
-    LOGGER.info("Detecting LDAP group attributes ...");
-
-    // perform a search using the user search base
-    if (Strings.isEmpty(ambariLdapConfiguration.groupSearchBase())) {
-      LOGGER.warn("No group search base provided");
-      return ambariLdapConfiguration;
-    }
-
-    SearchCursor searchCursor = null;
-
-    try {
-      // todo should the bind operation be done in the facade?
-      connection.bind(ambariLdapConfiguration.bindDn(), 
ambariLdapConfiguration.bindPassword());
-
-      SearchRequest searchRequest = 
assembleGroupSearchRequest(ambariLdapConfiguration);
-
-      // do the search
-      searchCursor = connection.search(searchRequest);
-
-      int processedGroupCnt = 0;
-
-      while (searchCursor.next()) {
-
-        if (processedGroupCnt >= SAMPLE_RESULT_SIZE) {
-          LOGGER.debug("The maximum number of results for attribute detection 
has exceeded. Quit  detection.");
-          break;
-        }
-
-        Response response = searchCursor.get();
-        // process the SearchResultEntry
-
-        if (response instanceof SearchResultEntry) {
-          Entry resultEntry = ((SearchResultEntry) response).getEntry();
-          LOGGER.info("Processing sample entry with dn: [{}]", 
resultEntry.getDn());
-
-          groupNameAttrDetector.collect(resultEntry);
-          groupObjectClassDetector.collect(resultEntry);
-          groupMemberAttrDetector.collect(resultEntry);
-
-          processedGroupCnt++;
-        }
-      }
-
-      
ambariLdapConfiguration.setValueFor(AmbariLdapConfiguration.AmbariLdapConfig.GROUP_NAME_ATTRIBUTE,
 groupNameAttrDetector.detect());
-      
ambariLdapConfiguration.setValueFor(AmbariLdapConfiguration.AmbariLdapConfig.GROUP_OBJECT_CLASS,
 groupObjectClassDetector.detect());
-      
ambariLdapConfiguration.setValueFor(AmbariLdapConfiguration.AmbariLdapConfig.GROUP_MEMBER_ATTRIBUTE,
 groupMemberAttrDetector.detect());
-
-      LOGGER.info("Decorated ambari ldap config : [{}]", 
ambariLdapConfiguration);
-
-    } catch (Exception e) {
-
-      LOGGER.error("Ldap operation failed", e);
-    } finally {
-      // housekeeping
-      if (null != searchCursor) {
-        searchCursor.close();
-      }
-    }
-
-    return ambariLdapConfiguration;
-  }
-
-  private SearchRequest assembleUserSearchRequest(AmbariLdapConfiguration 
ambariLdapConfiguration) throws AmbariLdapException {
-    try {
-
-      SearchRequest req = new SearchRequestImpl();
-      req.setScope(SearchScope.SUBTREE);
-      req.addAttributes("*");
-      req.setTimeLimit(0);
-      req.setBase(new Dn(ambariLdapConfiguration.userSearchBase()));
-      // the filter must be set!
-      
req.setFilter(FilterBuilder.present(ambariLdapConfiguration.dnAttribute()).toString());
-
-      return req;
-
-    } catch (Exception e) {
-      LOGGER.error("Could not assemble ldap search request", e);
-      throw new AmbariLdapException(e);
-    }
-  }
-
-  private SearchRequest assembleGroupSearchRequest(AmbariLdapConfiguration 
ambariLdapConfiguration) throws AmbariLdapException {
-    try {
-
-      SearchRequest req = new SearchRequestImpl();
-      req.setScope(SearchScope.SUBTREE);
-      req.addAttributes("*");
-      req.setTimeLimit(0);
-      req.setBase(new Dn(ambariLdapConfiguration.groupSearchBase()));
-      // the filter must be set!
-      
req.setFilter(FilterBuilder.present(ambariLdapConfiguration.dnAttribute()).toString());
-
-      return req;
-
-    } catch (Exception e) {
-      LOGGER.error("Could not assemble ldap search request", e);
-      throw new AmbariLdapException(e);
-    }
-  }
-
-
-}

http://git-wip-us.apache.org/repos/asf/ambari/blob/4ad492b4/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapAttributeDetectionService.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapAttributeDetectionService.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapAttributeDetectionService.java
new file mode 100644
index 0000000..b499b65
--- /dev/null
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapAttributeDetectionService.java
@@ -0,0 +1,238 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.server.ldap.service.ads;
+
+import javax.inject.Inject;
+import javax.inject.Singleton;
+
+import org.apache.ambari.server.ldap.AmbariLdapConfiguration;
+import org.apache.ambari.server.ldap.service.AmbariLdapException;
+import org.apache.ambari.server.ldap.service.LdapAttributeDetectionService;
+import 
org.apache.ambari.server.ldap.service.ads.detectors.GroupMemberAttrDetector;
+import 
org.apache.ambari.server.ldap.service.ads.detectors.GroupNameAttrDetector;
+import 
org.apache.ambari.server.ldap.service.ads.detectors.GroupObjectClassDetector;
+import 
org.apache.ambari.server.ldap.service.ads.detectors.UserGroupMemberAttrDetector;
+import 
org.apache.ambari.server.ldap.service.ads.detectors.UserNameAttrDetector;
+import 
org.apache.ambari.server.ldap.service.ads.detectors.UserObjectClassDetector;
+import org.apache.directory.api.ldap.model.cursor.SearchCursor;
+import org.apache.directory.api.ldap.model.entry.Entry;
+import org.apache.directory.api.ldap.model.message.Response;
+import org.apache.directory.api.ldap.model.message.SearchRequest;
+import org.apache.directory.api.ldap.model.message.SearchRequestImpl;
+import org.apache.directory.api.ldap.model.message.SearchResultEntry;
+import org.apache.directory.api.ldap.model.message.SearchScope;
+import org.apache.directory.api.ldap.model.name.Dn;
+import org.apache.directory.api.util.Strings;
+import org.apache.directory.ldap.client.api.LdapConnection;
+import org.apache.directory.ldap.client.api.search.FilterBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Singleton
+public class DefaultLdapAttributeDetectionService implements 
LdapAttributeDetectionService<LdapConnection> {
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(DefaultLdapAttributeDetectionService.class);
+  private static final int SAMPLE_RESULT_SIZE = 50;
+
+
+  @Inject
+  private UserNameAttrDetector userNameAttrDetector;
+
+  @Inject
+  private UserObjectClassDetector userObjectClassDetector;
+
+  @Inject
+  private UserGroupMemberAttrDetector userGroupMemberAttrDetector;
+
+  @Inject
+  private GroupNameAttrDetector groupNameAttrDetector;
+
+  @Inject
+  private GroupObjectClassDetector groupObjectClassDetector;
+
+  @Inject
+  private GroupMemberAttrDetector groupMemberAttrDetector;
+
+  @Inject
+  public DefaultLdapAttributeDetectionService() {
+  }
+
+  @Override
+  public AmbariLdapConfiguration detectLdapUserAttributes(LdapConnection 
connection, AmbariLdapConfiguration ambariLdapConfiguration) {
+    LOGGER.info("Detecting LDAP user attributes ...");
+
+    // perform a search using the user search base
+    if (Strings.isEmpty(ambariLdapConfiguration.userSearchBase())) {
+      LOGGER.warn("No user search base provided");
+      return ambariLdapConfiguration;
+    }
+
+    SearchCursor searchCursor = null;
+
+    try {
+
+      SearchRequest searchRequest = 
assembleUserSearchRequest(ambariLdapConfiguration);
+
+      // do the search
+      searchCursor = connection.search(searchRequest);
+
+      int processedUserCnt = 0;
+
+      while (searchCursor.next()) {
+
+        if (processedUserCnt >= SAMPLE_RESULT_SIZE) {
+          LOGGER.debug("The maximum count of results for attribute detection 
has exceeded. Quit user attribute detection.");
+          break;
+        }
+
+        Response response = searchCursor.get();
+        // process the SearchResultEntry
+
+        if (response instanceof SearchResultEntry) {
+          Entry resultEntry = ((SearchResultEntry) response).getEntry();
+          LOGGER.info("Processing sample entry with dn: [{}]", 
resultEntry.getDn());
+
+          userNameAttrDetector.collect(resultEntry);
+          userObjectClassDetector.collect(resultEntry);
+          userGroupMemberAttrDetector.collect(resultEntry);
+
+          processedUserCnt++;
+        }
+      }
+
+      
ambariLdapConfiguration.setValueFor(AmbariLdapConfiguration.AmbariLdapConfig.USER_NAME_ATTRIBUTE,
 userNameAttrDetector.detect());
+      
ambariLdapConfiguration.setValueFor(AmbariLdapConfiguration.AmbariLdapConfig.USER_OBJECT_CLASS,
 userObjectClassDetector.detect());
+      
ambariLdapConfiguration.setValueFor(AmbariLdapConfiguration.AmbariLdapConfig.USER_GROUP_MEMBER_ATTRIBUTE,
 userGroupMemberAttrDetector.detect());
+
+      LOGGER.info("Decorated ambari ldap config : [{}]", 
ambariLdapConfiguration);
+
+    } catch (Exception e) {
+
+      LOGGER.error("Ldap operation failed", e);
+    } finally {
+      // housekeeping
+      if (null != searchCursor) {
+        searchCursor.close();
+      }
+    }
+
+    return ambariLdapConfiguration;
+  }
+
+
+  @Override
+  public AmbariLdapConfiguration detectLdapGroupAttributes(LdapConnection 
connection, AmbariLdapConfiguration ambariLdapConfiguration) {
+    LOGGER.info("Detecting LDAP group attributes ...");
+
+    // perform a search using the user search base
+    if (Strings.isEmpty(ambariLdapConfiguration.groupSearchBase())) {
+      LOGGER.warn("No group search base provided");
+      return ambariLdapConfiguration;
+    }
+
+    SearchCursor searchCursor = null;
+
+    try {
+      // todo should the bind operation be done in the facade?
+      connection.bind(ambariLdapConfiguration.bindDn(), 
ambariLdapConfiguration.bindPassword());
+
+      SearchRequest searchRequest = 
assembleGroupSearchRequest(ambariLdapConfiguration);
+
+      // do the search
+      searchCursor = connection.search(searchRequest);
+
+      int processedGroupCnt = 0;
+
+      while (searchCursor.next()) {
+
+        if (processedGroupCnt >= SAMPLE_RESULT_SIZE) {
+          LOGGER.debug("The maximum number of results for attribute detection 
has exceeded. Quit  detection.");
+          break;
+        }
+
+        Response response = searchCursor.get();
+        // process the SearchResultEntry
+
+        if (response instanceof SearchResultEntry) {
+          Entry resultEntry = ((SearchResultEntry) response).getEntry();
+          LOGGER.info("Processing sample entry with dn: [{}]", 
resultEntry.getDn());
+
+          groupNameAttrDetector.collect(resultEntry);
+          groupObjectClassDetector.collect(resultEntry);
+          groupMemberAttrDetector.collect(resultEntry);
+
+          processedGroupCnt++;
+        }
+      }
+
+      
ambariLdapConfiguration.setValueFor(AmbariLdapConfiguration.AmbariLdapConfig.GROUP_NAME_ATTRIBUTE,
 groupNameAttrDetector.detect());
+      
ambariLdapConfiguration.setValueFor(AmbariLdapConfiguration.AmbariLdapConfig.GROUP_OBJECT_CLASS,
 groupObjectClassDetector.detect());
+      
ambariLdapConfiguration.setValueFor(AmbariLdapConfiguration.AmbariLdapConfig.GROUP_MEMBER_ATTRIBUTE,
 groupMemberAttrDetector.detect());
+
+      LOGGER.info("Decorated ambari ldap config : [{}]", 
ambariLdapConfiguration);
+
+    } catch (Exception e) {
+
+      LOGGER.error("Ldap operation failed", e);
+    } finally {
+      // housekeeping
+      if (null != searchCursor) {
+        searchCursor.close();
+      }
+    }
+
+    return ambariLdapConfiguration;
+  }
+
+  private SearchRequest assembleUserSearchRequest(AmbariLdapConfiguration 
ambariLdapConfiguration) throws AmbariLdapException {
+    try {
+
+      SearchRequest req = new SearchRequestImpl();
+      req.setScope(SearchScope.SUBTREE);
+      req.addAttributes("*");
+      req.setTimeLimit(0);
+      req.setBase(new Dn(ambariLdapConfiguration.userSearchBase()));
+      // the filter must be set!
+      
req.setFilter(FilterBuilder.present(ambariLdapConfiguration.dnAttribute()).toString());
+
+      return req;
+
+    } catch (Exception e) {
+      LOGGER.error("Could not assemble ldap search request", e);
+      throw new AmbariLdapException(e);
+    }
+  }
+
+  private SearchRequest assembleGroupSearchRequest(AmbariLdapConfiguration 
ambariLdapConfiguration) throws AmbariLdapException {
+    try {
+
+      SearchRequest req = new SearchRequestImpl();
+      req.setScope(SearchScope.SUBTREE);
+      req.addAttributes("*");
+      req.setTimeLimit(0);
+      req.setBase(new Dn(ambariLdapConfiguration.groupSearchBase()));
+      // the filter must be set!
+      
req.setFilter(FilterBuilder.present(ambariLdapConfiguration.dnAttribute()).toString());
+
+      return req;
+
+    } catch (Exception e) {
+      LOGGER.error("Could not assemble ldap search request", e);
+      throw new AmbariLdapException(e);
+    }
+  }
+
+
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/4ad492b4/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConfigurationService.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConfigurationService.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConfigurationService.java
index c90b5ac..0a309cd 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConfigurationService.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConfigurationService.java
@@ -21,8 +21,8 @@ import javax.inject.Inject;
 import javax.inject.Singleton;
 
 import org.apache.ambari.server.ldap.AmbariLdapConfiguration;
-import org.apache.ambari.server.ldap.LdapConfigurationService;
 import org.apache.ambari.server.ldap.service.AmbariLdapException;
+import org.apache.ambari.server.ldap.service.LdapConfigurationService;
 import 
org.apache.directory.api.ldap.codec.decorators.SearchResultEntryDecorator;
 import org.apache.directory.api.ldap.model.constants.SchemaConstants;
 import org.apache.directory.api.ldap.model.cursor.EntryCursor;
@@ -63,7 +63,6 @@ public class DefaultLdapConfigurationService implements 
LdapConfigurationService
 
   }
 
-
   /**
    * Checks the user attributes provided in the configuration instance by 
issuing a search for a (known) test user in the LDAP.
    * Attributes are considered correct if there is at least one entry found.
@@ -73,7 +72,7 @@ public class DefaultLdapConfigurationService implements 
LdapConfigurationService
    * @param ldapConnection          connection instance used to connect to the 
LDAP server
    * @param testUserName            the test username
    * @param testPassword            the test password
-   * @param ambariLdapConfiguration configuration instance holding ldap 
configuration details
+   * @param ambariLdapConfiguration the available LDAP configuration to be 
validated
    * @return the DN of the test user
    * @throws AmbariLdapException if an error occurs
    */
@@ -122,7 +121,16 @@ public class DefaultLdapConfigurationService implements 
LdapConfigurationService
     return userDn;
   }
 
-
+  /**
+   * Checks whether the provided group related settings are correct.
+   * The algorithm implemented in this method per
+   *
+   * @param ldapConnection          a connecion instance bound to an LDAP 
server
+   * @param userDn                  a user DN to check
+   * @param ambariLdapConfiguration the available LDAP configuration to be 
validated
+   * @return
+   * @throws AmbariLdapException
+   */
   @Override
   public Set<String> checkGroupAttributes(LdapConnection ldapConnection, 
String userDn, AmbariLdapConfiguration ambariLdapConfiguration) throws 
AmbariLdapException {
     SearchCursor searchCursor = null;
@@ -144,6 +152,7 @@ public class DefaultLdapConfigurationService implements 
LdapConfigurationService
       searchRequest.setFilter(filter);
       searchRequest.setBase(new Dn(ambariLdapConfiguration.groupSearchBase()));
       searchRequest.setScope(SearchScope.SUBTREE);
+      // attributes to be returned
       
searchRequest.addAttributes(ambariLdapConfiguration.groupMemberAttribute(), 
ambariLdapConfiguration.groupNameAttribute());
 
       // perform the search

http://git-wip-us.apache.org/repos/asf/ambari/blob/4ad492b4/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/UserGroupMemberAttrDetector.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/UserGroupMemberAttrDetector.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/UserGroupMemberAttrDetector.java
index 913c2b6..a7f03dd 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/UserGroupMemberAttrDetector.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/UserGroupMemberAttrDetector.java
@@ -19,7 +19,6 @@ import javax.inject.Inject;
 import org.apache.directory.api.ldap.model.entry.Entry;
 
 public class UserGroupMemberAttrDetector extends 
OccurranceAndWeightBasedDetector {
-  private static String[] userGroupMemAttrValues = {"memberOf", "ismemberOf"};
 
   private enum UserGroupMemberAttr {
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/4ad492b4/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/DefaultAttributeDetectionServiceTest.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/DefaultAttributeDetectionServiceTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/DefaultAttributeDetectionServiceTest.java
deleted file mode 100644
index 9b03b86..0000000
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/DefaultAttributeDetectionServiceTest.java
+++ /dev/null
@@ -1,108 +0,0 @@
-/*
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ambari.server.ldap.service.ads;
-
-
-import java.util.Map;
-
-import org.apache.ambari.server.ldap.AmbariLdapConfiguration;
-import org.apache.ambari.server.ldap.service.LdapConnectionService;
-import org.apache.directory.api.ldap.model.constants.SchemaConstants;
-import org.apache.directory.ldap.client.api.LdapConnection;
-import org.easymock.EasyMockRule;
-import org.easymock.TestSubject;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.google.common.collect.Maps;
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
-
-public class DefaultAttributeDetectionServiceTest {
-
-  private static final Logger LOGGER = 
LoggerFactory.getLogger(DefaultAttributeDetectionServiceTest.class);
-
-  @Rule
-  public EasyMockRule mocks = new EasyMockRule(this);
-
-  private AmbariLdapConfiguration testLdapConfiguration;
-  private LdapConnection connection;
-
-  @TestSubject
-  private DefaultAttributeDetectionService attributeDetectionService = new 
DefaultAttributeDetectionService();
-
-  @Before
-  public void before() {
-
-    Map<String, Object> initialProps = Maps.newHashMap();
-    initialProps.put(AmbariLdapConfiguration.AmbariLdapConfig.BIND_DN.key(), 
"");
-    testLdapConfiguration = new AmbariLdapConfiguration(initialProps);
-  }
-
-  @Test
-  public void 
testShouldUserNameAttributeBeDetectedWhenSearchReturnsValidUsers() throws 
Exception {
-    // GIVEN
-// a set of entries returned from the LDAP search
-
-    // WHEN
-    AmbariLdapConfiguration ambariLdapConfiguration = 
attributeDetectionService.detectLdapUserAttributes(connection, 
testLdapConfiguration);
-
-    // THEN
-    Assert.assertNotNull(ambariLdapConfiguration);
-    Assert.assertEquals("The username attribute is not the expected", "uid", 
ambariLdapConfiguration.userNameAttribute());
-
-  }
-
-
-  @Test
-  public void functionalTest() throws Exception {
-    // GIVEN
-    AmbariLdapConfiguration ambariLdapConfiguration = new 
AmbariLdapConfiguration(getTestPropertiesMap());
-    LdapConnectionService connectionService = new 
DefaultLdapConnectionService();
-    LdapConnection ldapConnection = 
connectionService.createLdapConnection(ambariLdapConfiguration);
-
-    // WHEN
-    AmbariLdapConfiguration config = 
attributeDetectionService.detectLdapUserAttributes(ldapConnection, 
ambariLdapConfiguration);
-    config = 
attributeDetectionService.detectLdapGroupAttributes(ldapConnection, 
ambariLdapConfiguration);
-
-    Gson gson = new GsonBuilder().create();
-    LOGGER.info(gson.toJson(config));
-
-    // THEN
-    ldapConnection.close();
-
-  }
-
-  private Map<String, Object> getTestPropertiesMap() {
-    Map<String, Object> ldapPropsMap = Maps.newHashMap();
-
-    
ldapPropsMap.put(AmbariLdapConfiguration.AmbariLdapConfig.ANONYMOUS_BIND.key(), 
"true");
-    
ldapPropsMap.put(AmbariLdapConfiguration.AmbariLdapConfig.SERVER_HOST.key(), 
"ldap.forumsys.com");
-    
ldapPropsMap.put(AmbariLdapConfiguration.AmbariLdapConfig.SERVER_PORT.key(), 
"389");
-    ldapPropsMap.put(AmbariLdapConfiguration.AmbariLdapConfig.BIND_DN.key(), 
"cn=read-only-admin,dc=example,dc=com");
-    
ldapPropsMap.put(AmbariLdapConfiguration.AmbariLdapConfig.BIND_PASSWORD.key(), 
"password");
-    
ldapPropsMap.put(AmbariLdapConfiguration.AmbariLdapConfig.DN_ATTRIBUTE.key(), 
SchemaConstants.CN_AT);
-
-    
ldapPropsMap.put(AmbariLdapConfiguration.AmbariLdapConfig.USER_SEARCH_BASE.key(),
 "dc=example,dc=com");
-    
ldapPropsMap.put(AmbariLdapConfiguration.AmbariLdapConfig.GROUP_SEARCH_BASE.key(),
 "dc=example,dc=com");
-
-    return ldapPropsMap;
-
-  }
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ambari/blob/4ad492b4/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapAttributeDetectionServiceTest.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapAttributeDetectionServiceTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapAttributeDetectionServiceTest.java
new file mode 100644
index 0000000..3d335bf
--- /dev/null
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapAttributeDetectionServiceTest.java
@@ -0,0 +1,108 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.server.ldap.service.ads;
+
+
+import java.util.Map;
+
+import org.apache.ambari.server.ldap.AmbariLdapConfiguration;
+import org.apache.ambari.server.ldap.service.LdapConnectionService;
+import org.apache.directory.api.ldap.model.constants.SchemaConstants;
+import org.apache.directory.ldap.client.api.LdapConnection;
+import org.easymock.EasyMockRule;
+import org.easymock.TestSubject;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.Maps;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+
+public class DefaultLdapAttributeDetectionServiceTest {
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(DefaultLdapAttributeDetectionServiceTest.class);
+
+  @Rule
+  public EasyMockRule mocks = new EasyMockRule(this);
+
+  private AmbariLdapConfiguration testLdapConfiguration;
+  private LdapConnection connection;
+
+  @TestSubject
+  private DefaultLdapAttributeDetectionService attributeDetectionService = new 
DefaultLdapAttributeDetectionService();
+
+  @Before
+  public void before() {
+
+    Map<String, Object> initialProps = Maps.newHashMap();
+    initialProps.put(AmbariLdapConfiguration.AmbariLdapConfig.BIND_DN.key(), 
"");
+    testLdapConfiguration = new AmbariLdapConfiguration(initialProps);
+  }
+
+  @Test
+  public void 
testShouldUserNameAttributeBeDetectedWhenSearchReturnsValidUsers() throws 
Exception {
+    // GIVEN
+// a set of entries returned from the LDAP search
+
+    // WHEN
+    AmbariLdapConfiguration ambariLdapConfiguration = 
attributeDetectionService.detectLdapUserAttributes(connection, 
testLdapConfiguration);
+
+    // THEN
+    Assert.assertNotNull(ambariLdapConfiguration);
+    Assert.assertEquals("The username attribute is not the expected", "uid", 
ambariLdapConfiguration.userNameAttribute());
+
+  }
+
+
+  @Test
+  public void functionalTest() throws Exception {
+    // GIVEN
+    AmbariLdapConfiguration ambariLdapConfiguration = new 
AmbariLdapConfiguration(getTestPropertiesMap());
+    LdapConnectionService connectionService = new 
DefaultLdapConnectionService();
+    LdapConnection ldapConnection = 
connectionService.createLdapConnection(ambariLdapConfiguration);
+
+    // WHEN
+    AmbariLdapConfiguration config = 
attributeDetectionService.detectLdapUserAttributes(ldapConnection, 
ambariLdapConfiguration);
+    config = 
attributeDetectionService.detectLdapGroupAttributes(ldapConnection, 
ambariLdapConfiguration);
+
+    Gson gson = new GsonBuilder().create();
+    LOGGER.info(gson.toJson(config));
+
+    // THEN
+    ldapConnection.close();
+
+  }
+
+  private Map<String, Object> getTestPropertiesMap() {
+    Map<String, Object> ldapPropsMap = Maps.newHashMap();
+
+    
ldapPropsMap.put(AmbariLdapConfiguration.AmbariLdapConfig.ANONYMOUS_BIND.key(), 
"true");
+    
ldapPropsMap.put(AmbariLdapConfiguration.AmbariLdapConfig.SERVER_HOST.key(), 
"ldap.forumsys.com");
+    
ldapPropsMap.put(AmbariLdapConfiguration.AmbariLdapConfig.SERVER_PORT.key(), 
"389");
+    ldapPropsMap.put(AmbariLdapConfiguration.AmbariLdapConfig.BIND_DN.key(), 
"cn=read-only-admin,dc=example,dc=com");
+    
ldapPropsMap.put(AmbariLdapConfiguration.AmbariLdapConfig.BIND_PASSWORD.key(), 
"password");
+    
ldapPropsMap.put(AmbariLdapConfiguration.AmbariLdapConfig.DN_ATTRIBUTE.key(), 
SchemaConstants.CN_AT);
+
+    
ldapPropsMap.put(AmbariLdapConfiguration.AmbariLdapConfig.USER_SEARCH_BASE.key(),
 "dc=example,dc=com");
+    
ldapPropsMap.put(AmbariLdapConfiguration.AmbariLdapConfig.GROUP_SEARCH_BASE.key(),
 "dc=example,dc=com");
+
+    return ldapPropsMap;
+
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ambari/blob/4ad492b4/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConfigurationServiceTest.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConfigurationServiceTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConfigurationServiceTest.java
index 1e69012..1ba6110 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConfigurationServiceTest.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapConfigurationServiceTest.java
@@ -19,7 +19,7 @@ import static org.junit.Assert.assertNotNull;
 import java.util.Map;
 
 import org.apache.ambari.server.ldap.AmbariLdapConfiguration;
-import org.apache.ambari.server.ldap.LdapConfigurationService;
+import org.apache.ambari.server.ldap.service.LdapConfigurationService;
 import org.apache.ambari.server.ldap.service.LdapConnectionService;
 import org.apache.directory.api.ldap.model.constants.SchemaConstants;
 import org.apache.directory.api.ldap.model.cursor.EntryCursor;

Reply via email to