You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by lp...@apache.org on 2017/10/19 12:45:18 UTC

[44/46] ambari git commit: AMBARI-21307 Added unit test, minor fixes

AMBARI-21307 Added unit test, minor fixes


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

Branch: refs/heads/feature-branch-AMBARI-21307
Commit: 3f64c096256951bfe4f124161b8b9eb4baf8af86
Parents: e3676a7
Author: lpuskas <lp...@apache.org>
Authored: Wed Oct 18 15:07:28 2017 +0200
Committer: lpuskas <lp...@apache.org>
Committed: Thu Oct 19 14:42:03 2017 +0200

----------------------------------------------------------------------
 .../DefaultLdapAttributeDetectionService.java   |   2 +-
 .../ads/detectors/AttributeDetectorFactory.java |  21 +--
 ...efaultLdapAttributeDetectionServiceTest.java | 188 +++++++++++++++++++
 3 files changed, 198 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/3f64c096/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
index 25abce7..a9a9b53 100644
--- 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
@@ -118,7 +118,7 @@ public class DefaultLdapAttributeDetectionService implements LdapAttributeDetect
     try {
 
       LdapConnectionTemplate ldapConnectionTemplate = ldapConnectionTemplateFactory.create(ambariLdapConfiguration);
-      AttributeDetector<Entry> groupAttributeDetector = attributeDetectorFactory.groupAttributDetector();
+      AttributeDetector<Entry> groupAttributeDetector = attributeDetectorFactory.groupAttributeDetector();
 
       SearchRequest searchRequest = assembleGroupSearchRequest(ldapConnectionTemplate, ambariLdapConfiguration);
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/3f64c096/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/AttributeDetectorFactory.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/AttributeDetectorFactory.java b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/AttributeDetectorFactory.java
index 8155461..eba0bd9 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/AttributeDetectorFactory.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/ldap/service/ads/detectors/AttributeDetectorFactory.java
@@ -33,21 +33,18 @@ public class AttributeDetectorFactory {
   private static final Logger LOG = LoggerFactory.getLogger(AttributeDetectorFactory.class);
   private static final String USER_ATTRIBUTES_DETECTORS = "UserAttributesDetectors";
   private static final String GROUP_ATTRIBUTES_DETECTORS = "GroupAttributesDetectors";
-
-
-  /**
-   * The set of user attribute detectors, configured by GUICE (check the relevant guice module implementation)
-   */
-  @Inject
-  @Named(USER_ATTRIBUTES_DETECTORS)
-  private Set<AttributeDetector> userAttributeDetectors;
-
   /**
    * The set of group attribute detectors, configured by GUICE (check the relevant guice module implementation)
    */
   @Inject
   @Named(GROUP_ATTRIBUTES_DETECTORS)
   Set<AttributeDetector> groupAttributeDetectors;
+  /**
+   * The set of user attribute detectors, configured by GUICE (check the relevant guice module implementation)
+   */
+  @Inject
+  @Named(USER_ATTRIBUTES_DETECTORS)
+  private Set<AttributeDetector> userAttributeDetectors;
 
   @Inject
   public AttributeDetectorFactory() {
@@ -59,7 +56,7 @@ public class AttributeDetectorFactory {
    * @return the constructed ChainedAttributeDetector instance
    */
   public ChainedAttributeDetector userAttributDetector() {
-    LOG.info("Creating instance with user attribute detectors: [{}]", userAttributDetector());
+    LOG.info("Creating instance with user attribute detectors: [{}]", userAttributeDetectors);
     return new ChainedAttributeDetector(userAttributeDetectors);
   }
 
@@ -69,8 +66,8 @@ public class AttributeDetectorFactory {
    * @return the constructed ChainedAttributeDetector instance
    */
 
-  public ChainedAttributeDetector groupAttributDetector() {
-    LOG.info("Creating instance with group attribute detectors: [{}]", groupAttributDetector());
+  public ChainedAttributeDetector groupAttributeDetector() {
+    LOG.info("Creating instance with group attribute detectors: [{}]", groupAttributeDetectors);
     return new ChainedAttributeDetector(groupAttributeDetectors);
   }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/3f64c096/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..09dea1c
--- /dev/null
+++ b/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/ads/DefaultLdapAttributeDetectionServiceTest.java
@@ -0,0 +1,188 @@
+/*
+ * 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.List;
+import java.util.Map;
+
+import org.apache.ambari.server.ldap.domain.AmbariLdapConfigKeys;
+import org.apache.ambari.server.ldap.domain.AmbariLdapConfiguration;
+import org.apache.ambari.server.ldap.domain.AmbariLdapConfigurationFactory;
+import org.apache.ambari.server.ldap.domain.TestAmbariLdapConfigurationFactory;
+import org.apache.ambari.server.ldap.service.AmbariLdapException;
+import org.apache.ambari.server.ldap.service.ads.detectors.AttributeDetectorFactory;
+import org.apache.ambari.server.ldap.service.ads.detectors.ChainedAttributeDetector;
+import org.apache.ambari.server.ldap.service.ads.detectors.GroupMemberAttrDetector;
+import org.apache.ambari.server.ldap.service.ads.detectors.UserNameAttrDetector;
+import org.apache.directory.api.ldap.model.entry.DefaultEntry;
+import org.apache.directory.api.ldap.model.entry.Entry;
+import org.apache.directory.api.ldap.model.exception.LdapException;
+import org.apache.directory.api.ldap.model.message.SearchRequest;
+import org.apache.directory.api.ldap.model.message.SearchScope;
+import org.apache.directory.ldap.client.template.EntryMapper;
+import org.apache.directory.ldap.client.template.LdapConnectionTemplate;
+import org.easymock.EasyMock;
+import org.easymock.EasyMockRule;
+import org.easymock.EasyMockSupport;
+import org.easymock.Mock;
+import org.easymock.TestSubject;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
+
+public class DefaultLdapAttributeDetectionServiceTest extends EasyMockSupport {
+  @Rule
+  public EasyMockRule mocks = new EasyMockRule(this);
+
+  @Mock
+  private AttributeDetectorFactory attributeDetectorFactoryMock;
+
+  @Mock
+  private LdapConnectionTemplateFactory ldapConnectionTemplateFactoryMock;
+
+  @Mock
+  private LdapConnectionTemplate ldapConnectionTemplateMock;
+
+  @Mock
+  private SearchRequest searchRequestMock;
+
+
+  private AmbariLdapConfigurationFactory ldapConfigurationFactory = new TestAmbariLdapConfigurationFactory();
+
+  @TestSubject
+  private DefaultLdapAttributeDetectionService defaultLdapAttributeDetectionService = new DefaultLdapAttributeDetectionService();
+
+  @Before
+  public void before() {
+    resetAll();
+  }
+
+  @Test
+  @SuppressWarnings("unchecked")
+  public void shouldLdapUserAttributeDetection() throws Exception {
+    // GIVEN
+    Map<String, Object> configMap = Maps.newHashMap();
+    configMap.put(AmbariLdapConfigKeys.USER_SEARCH_BASE.key(), "dc=example,dc=com");
+    AmbariLdapConfiguration ldapConfiguration = ldapConfigurationFactory.createLdapConfiguration(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);
+
+    // WHEN
+    replayAll();
+    AmbariLdapConfiguration decorated = defaultLdapAttributeDetectionService.detectLdapUserAttributes(ldapConfiguration);
+
+    // THEN
+    Assert.assertNotNull(decorated);
+    Assert.assertEquals("N/A", ldapConfiguration.userNameAttribute());
+  }
+
+  @Test(expected = AmbariLdapException.class)
+  public void testShouldUserAttributeDetectionFailWhenLdapOerationFails() throws Exception {
+    // GIVEN
+    Map<String, Object> configMap = Maps.newHashMap();
+    configMap.put(AmbariLdapConfigKeys.USER_SEARCH_BASE.key(), "dc=example,dc=com");
+    AmbariLdapConfiguration ldapConfiguration = ldapConfigurationFactory.createLdapConfiguration(configMap);
+
+    EasyMock.expect(ldapConnectionTemplateFactoryMock.create(ldapConfiguration)).andThrow(new AmbariLdapException("Testing ..."));
+
+    // WHEN
+    replayAll();
+    AmbariLdapConfiguration decorated = defaultLdapAttributeDetectionService.detectLdapUserAttributes(ldapConfiguration);
+
+    // THEN
+    // exception is thrown
+
+  }
+
+
+  @Test
+  @SuppressWarnings("unchecked")
+  public void shouldLdapGroupAttributeDetection() throws Exception {
+    // GIVEN
+    Map<String, Object> configMap = Maps.newHashMap();
+    configMap.put(AmbariLdapConfigKeys.GROUP_SEARCH_BASE.key(), "dc=example,dc=com");
+    AmbariLdapConfiguration ldapConfiguration = ldapConfigurationFactory.createLdapConfiguration(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.groupAttributeDetector())
+      .andReturn(new ChainedAttributeDetector(Sets.newHashSet(new GroupMemberAttrDetector())));
+
+    EasyMock.expect(searchRequestMock.setSizeLimit(50)).andReturn(searchRequestMock);
+
+    // WHEN
+    replayAll();
+    AmbariLdapConfiguration decorated = defaultLdapAttributeDetectionService.detectLdapGroupAttributes(ldapConfiguration);
+
+    // THEN
+    Assert.assertNotNull(decorated);
+    Assert.assertEquals("N/A", ldapConfiguration.groupMemberAttribute());
+  }
+
+  @Test(expected = AmbariLdapException.class)
+  public void testShouldGroupAttributeDetectionFailWhenLdapOerationFails() throws Exception {
+    // GIVEN
+    Map<String, Object> configMap = Maps.newHashMap();
+    configMap.put(AmbariLdapConfigKeys.GROUP_SEARCH_BASE.key(), "dc=example,dc=com");
+    AmbariLdapConfiguration ldapConfiguration = ldapConfigurationFactory.createLdapConfiguration(configMap);
+
+    EasyMock.expect(ldapConnectionTemplateFactoryMock.create(ldapConfiguration)).andThrow(new AmbariLdapException("Testing ..."));
+
+    // WHEN
+    replayAll();
+    AmbariLdapConfiguration decorated = defaultLdapAttributeDetectionService.detectLdapGroupAttributes(ldapConfiguration);
+
+    // THEN
+    // exception is thrown
+
+  }
+
+
+  private EntryMapper<Entry> entryMapperMock() {
+    return new EntryMapper<Entry>() {
+      @Override
+      public Entry map(Entry entry) throws LdapException {
+        return null;
+      }
+    };
+  }
+
+}
\ No newline at end of file