ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lpus...@apache.org
Subject [29/33] ambari git commit: AMBARI-21307 Added unit test for the LdapFacade
Date Fri, 13 Oct 2017 15:54:57 GMT
AMBARI-21307 Added unit test for the LdapFacade


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

Branch: refs/heads/feature-branch-AMBARI-21307
Commit: 2f059df32d74fbc32385838ff459eac481069647
Parents: 060b85c
Author: lpuskas <lpuskas@apache.org>
Authored: Thu Oct 12 16:53:31 2017 +0200
Committer: lpuskas <lpuskas@apache.org>
Committed: Fri Oct 13 17:20:53 2017 +0200

----------------------------------------------------------------------
 .../services/AmbariConfigurationService.java    |   2 +-
 .../ldap/domain/AmbariLdapConfiguration.java    |  22 ++
 .../server/ldap/service/AmbariLdapFacade.java   |  10 +-
 .../ldap/service/AmbariLdapFacadeTest.java      | 215 +++++++++++++++++++
 4 files changed, 245 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/2f059df3/ambari-server/src/main/java/org/apache/ambari/server/api/services/AmbariConfigurationService.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/AmbariConfigurationService.java
b/ambari-server/src/main/java/org/apache/ambari/server/api/services/AmbariConfigurationService.java
index 492509f..38ae766 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/AmbariConfigurationService.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/AmbariConfigurationService.java
@@ -150,7 +150,7 @@ public class AmbariConfigurationService extends BaseService {
   @PUT
   @Path("{configurationId}")
   @Produces(MediaType.TEXT_PLAIN)
-  @ApiOperation(value = "Updates ambari configuration resources - Not implemented yet",
+  @ApiOperation(value = "Updates ambari configuration resources ",
     nickname = "AmbariConfigurationService#updateAmbariConfiguration")
   @ApiImplicitParams({
     @ApiImplicitParam(dataType = AMBARI_CONFIGURATION_REQUEST_TYPE, paramType = PARAM_TYPE_BODY),

http://git-wip-us.apache.org/repos/asf/ambari/blob/2f059df3/ambari-server/src/main/java/org/apache/ambari/server/ldap/domain/AmbariLdapConfiguration.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/ldap/domain/AmbariLdapConfiguration.java
b/ambari-server/src/main/java/org/apache/ambari/server/ldap/domain/AmbariLdapConfiguration.java
index d071ebe..8b26cd3 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/ldap/domain/AmbariLdapConfiguration.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/ldap/domain/AmbariLdapConfiguration.java
@@ -19,6 +19,8 @@ import java.util.Map;
 
 import javax.inject.Inject;
 
+import org.apache.commons.lang3.builder.EqualsBuilder;
+import org.apache.commons.lang3.builder.HashCodeBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -174,4 +176,24 @@ public class AmbariLdapConfiguration {
   public String toString() {
     return configurationMap.toString();
   }
+
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) return true;
+
+    if (o == null || getClass() != o.getClass()) return false;
+
+    AmbariLdapConfiguration that = (AmbariLdapConfiguration) o;
+
+    return new EqualsBuilder()
+      .append(configurationMap, that.configurationMap)
+      .isEquals();
+  }
+
+  @Override
+  public int hashCode() {
+    return new HashCodeBuilder(17, 37)
+      .append(configurationMap)
+      .toHashCode();
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/2f059df3/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 c75b0c4..bbfbc8e 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
@@ -33,7 +33,7 @@ public class AmbariLdapFacade implements LdapFacade {
   /**
    * Additional parameters expected to be provided along with the configuration
    */
-  private enum Parameters {
+  protected enum Parameters {
     TEST_USER_NAME("ambari.ldap.test.user.name"),
     TEST_USER_PASSWORD("ambari.ldap.test.user.password");
 
@@ -43,7 +43,7 @@ public class AmbariLdapFacade implements LdapFacade {
       this.parameterKey = parameterKey;
     }
 
-    private String getParameterKey() {
+    public String getParameterKey() {
       return parameterKey;
     }
 
@@ -62,6 +62,7 @@ public class AmbariLdapFacade implements LdapFacade {
   @Override
   public void checkConnection(AmbariLdapConfiguration ambariLdapConfiguration) throws AmbariLdapException
{
     try {
+
       ldapConfigurationService.checkConnection(ambariLdapConfiguration);
       LOGGER.info("Validating LDAP connection related configuration: SUCCESS");
 
@@ -80,12 +81,15 @@ public class AmbariLdapFacade implements LdapFacade {
     LOGGER.info("Detecting LDAP configuration attributes ...");
 
     try {
-
+      LOGGER.info("Detecting user attributes ....");
       // decorate the configuration with detected user attributes
       ambariLdapConfiguration = ldapAttributeDetectionService.detectLdapUserAttributes(ambariLdapConfiguration);
 
+      LOGGER.info("Detecting group attributes ....");
       // decorate the configuration with detected group attributes
       ambariLdapConfiguration = ldapAttributeDetectionService.detectLdapGroupAttributes(ambariLdapConfiguration);
+
+      LOGGER.info("Attribute detection finished.");
       return ambariLdapConfiguration;
 
     } catch (Exception e) {

http://git-wip-us.apache.org/repos/asf/ambari/blob/2f059df3/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/AmbariLdapFacadeTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/AmbariLdapFacadeTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/AmbariLdapFacadeTest.java
new file mode 100644
index 0000000..8552f86
--- /dev/null
+++ b/ambari-server/src/test/java/org/apache/ambari/server/ldap/service/AmbariLdapFacadeTest.java
@@ -0,0 +1,215 @@
+/*
+ * 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.Map;
+import java.util.Set;
+
+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.TestAmbariAmbariLdapConfigurationFactory;
+import org.easymock.Capture;
+import org.easymock.EasyMock;
+import org.easymock.EasyMockRule;
+import org.easymock.EasyMockSupport;
+import org.easymock.Mock;
+import org.easymock.MockType;
+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.Maps;
+import com.google.common.collect.Sets;
+
+/**
+ * Unit test suite for the LdapFacade operations.
+ */
+public class AmbariLdapFacadeTest extends EasyMockSupport {
+
+  @Rule
+  public EasyMockRule mocks = new EasyMockRule(this);
+
+  @Mock(type = MockType.STRICT)
+  public LdapConfigurationService ldapConfigurationServiceMock;
+
+  @Mock(type = MockType.STRICT)
+  public LdapAttributeDetectionService ldapAttributeDetectionServiceMock;
+
+  private AmbariLdapConfigurationFactory ambariLdapConfigurationFactory;
+
+
+  @TestSubject
+  private LdapFacade ldapFacade = new AmbariLdapFacade();
+
+  private AmbariLdapConfiguration ambariLdapConfiguration;
+
+
+  private Capture<AmbariLdapConfiguration> ambariLdapConfigurationCapture;
+
+  @Before
+  public void before() {
+    ambariLdapConfigurationFactory = new TestAmbariAmbariLdapConfigurationFactory();
+    ambariLdapConfiguration = ambariLdapConfigurationFactory.createLdapConfiguration(Maps.newHashMap());
+    ambariLdapConfigurationCapture = Capture.newInstance();
+
+
+    resetAll();
+  }
+
+  /**
+   * Tests whether the facade method call delegates to the proper service call.
+   * The thest is success if the same instance is passed to the service.
+   *
+   * @throws Exception
+   */
+  @Test
+  public void testShouldConfigurationCheckDelegateToTheRightServiceCall() throws Exception
{
+    // GIVEN
+    // the mocks are set up
+    ldapConfigurationServiceMock.checkConnection(EasyMock.capture(ambariLdapConfigurationCapture));
+    replayAll();
+    // WHEN
+    // the facade method is called
+    ldapFacade.checkConnection(ambariLdapConfiguration);
+
+    // THEN
+    // the captured configuration instance is the same the facade method got called with
+    Assert.assertEquals("The configuration instance souldn't change before passing it to
the service",
+      ambariLdapConfiguration, ambariLdapConfigurationCapture.getValue());
+  }
+
+  @Test(expected = AmbariLdapException.class)
+  public void testShouldConfigurationCheckFailureResultInAmbariLdapException() throws Exception
{
+    // GIVEN
+    ldapConfigurationServiceMock.checkConnection(EasyMock.anyObject(AmbariLdapConfiguration.class));
+    EasyMock.expectLastCall().andThrow(new AmbariLdapException("Testing ..."));
+    replayAll();
+
+    // WHEN
+    ldapFacade.checkConnection(ambariLdapConfiguration);
+
+    // THEN
+    // exception is thrown
+
+  }
+
+  @Test
+  public void testShouldLdapAttributesCheckDelegateToTheRightServiceCalls() throws Exception
{
+    // GIVEN
+
+    Map<String, Object> parameters = Maps.newHashMap();
+    parameters.put(AmbariLdapFacade.Parameters.TEST_USER_NAME.getParameterKey(), "testUser");
+    parameters.put(AmbariLdapFacade.Parameters.TEST_USER_PASSWORD.getParameterKey(), "testPassword");
+
+
+    Capture<String> testUserCapture = Capture.newInstance();
+    Capture<String> testPasswordCapture = Capture.newInstance();
+    Capture<String> userDnCapture = Capture.newInstance();
+
+    EasyMock.expect(ldapConfigurationServiceMock.checkUserAttributes(EasyMock.capture(testUserCapture),
EasyMock.capture(testPasswordCapture),
+      EasyMock.capture(ambariLdapConfigurationCapture))).andReturn("userDn");
+
+    EasyMock.expect(ldapConfigurationServiceMock.checkGroupAttributes(EasyMock.capture(userDnCapture),
+      EasyMock.capture(ambariLdapConfigurationCapture))).andReturn(Sets.newHashSet("userGroup"));
+
+    replayAll();
+
+    // WHEN
+    Set<String> testUserGroups = ldapFacade.checkLdapAttributes(parameters, ambariLdapConfiguration);
+
+    // THEN
+    Assert.assertEquals("testUser", testUserCapture.getValue());
+    Assert.assertEquals("testPassword", testPasswordCapture.getValue());
+    Assert.assertEquals("userDn", userDnCapture.getValue());
+
+    Assert.assertTrue(testUserGroups.contains("userGroup"));
+
+  }
+
+  @Test(expected = AmbariLdapException.class)
+  public void testShouldAttributeCheckFailuresResultInAmbariLdapException() throws Exception
{
+    // GIVEN
+    Map<String, Object> parameters = Maps.newHashMap();
+    parameters.put(AmbariLdapFacade.Parameters.TEST_USER_NAME.getParameterKey(), "testUser");
+    parameters.put(AmbariLdapFacade.Parameters.TEST_USER_PASSWORD.getParameterKey(), "testPassword");
+
+    EasyMock.expect(ldapConfigurationServiceMock.checkUserAttributes(EasyMock.anyString(),
EasyMock.anyString(),
+      EasyMock.anyObject(AmbariLdapConfiguration.class))).andThrow(new AmbariLdapException("Testing
..."));
+
+    replayAll();
+
+    // WHEN
+    Set<String> testUserGroups = ldapFacade.checkLdapAttributes(parameters, ambariLdapConfiguration);
+    // THEN
+    // Exception is thrown
+  }
+
+  @Test
+  public void testShouldLdapAttributeDetectionDelegateToTheRightServiceCalls() throws Exception
{
+
+    // configuration map with user attributes detected
+    Map<String, Object> userConfigMap = Maps.newHashMap();
+    userConfigMap.put(AmbariLdapConfigKeys.USER_NAME_ATTRIBUTE.key(), "uid");
+    AmbariLdapConfiguration userAttrDecoratedConfig = ambariLdapConfigurationFactory.createLdapConfiguration(userConfigMap);
+
+    // configuration map with user+group attributes detected
+    Map<String, Object> groupConfigMap = Maps.newHashMap(userConfigMap);
+    groupConfigMap.put(AmbariLdapConfigKeys.GROUP_NAME_ATTRIBUTE.key(), "dn");
+    AmbariLdapConfiguration groupAttrDecoratedConfig = ambariLdapConfigurationFactory.createLdapConfiguration(groupConfigMap);
+
+    Capture<AmbariLdapConfiguration> userAttrDetectionConfigCapture = Capture.newInstance();
+    Capture<AmbariLdapConfiguration> groupAttrDetectionConfigCapture = Capture.newInstance();
+
+    // GIVEN
+    EasyMock.expect(ldapAttributeDetectionServiceMock.detectLdapUserAttributes(EasyMock.capture(userAttrDetectionConfigCapture)))
+      .andReturn(userAttrDecoratedConfig);
+
+    EasyMock.expect(ldapAttributeDetectionServiceMock.detectLdapGroupAttributes(EasyMock.capture(groupAttrDetectionConfigCapture)))
+      .andReturn(groupAttrDecoratedConfig);
+
+    replayAll();
+
+    // WHEN
+    AmbariLdapConfiguration detected = ldapFacade.detectAttributes(ambariLdapConfiguration);
+
+    // THEN
+    Assert.assertEquals("User attribute detection called with the wrong configuration", ambariLdapConfiguration,
+      userAttrDetectionConfigCapture.getValue());
+
+    Assert.assertEquals("Group attribute detection called with the wrong configuration",
userAttrDecoratedConfig,
+      groupAttrDetectionConfigCapture.getValue());
+
+    Assert.assertEquals("Attribute detection returned an invalid configuration", groupAttrDecoratedConfig,
detected);
+
+  }
+
+  @Test(expected = AmbariLdapException.class)
+  public void testShouldAttributeDetectionFailuresResultInAmbariLdapException() throws Exception
{
+    // GIVEN
+    EasyMock.expect(ldapAttributeDetectionServiceMock.detectLdapUserAttributes(EasyMock.anyObject(AmbariLdapConfiguration.class)))
+      .andThrow(new AmbariLdapException("Testing ..."));
+
+    replayAll();
+
+    // WHEN
+    ldapFacade.detectAttributes(ambariLdapConfiguration);
+
+    // THEN
+    // Exception is thrown
+  }
+}
\ No newline at end of file


Mime
View raw message