Author: angela
Date: Thu Nov 16 16:41:53 2017
New Revision: 1815479

URL: http://svn.apache.org/viewvc?rev=1815479&view=rev
Log:
OAK-1588 : Create more tests/validation to LDAP integration
minor improvements in LdapProvider

Added:
    
jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/
    
jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/LdapIdentityPropertiesTest.java
   (with props)
    
jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/LdapIdentityProviderOsgiTest.java
   (with props)
    
jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/LdapProviderConfigTest.java
   (with props)
    
jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/PoolableUnboundConnectionFactoryTest.java
   (with props)
    
jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/UnboundLookupConnectionValidatorTest.java
   (with props)
Modified:
    jackrabbit/oak/trunk/oak-auth-ldap/pom.xml
    
jackrabbit/oak/trunk/oak-auth-ldap/src/main/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/LdapIdentityProvider.java

Modified: jackrabbit/oak/trunk/oak-auth-ldap/pom.xml
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-auth-ldap/pom.xml?rev=1815479&r1=1815478&r2=1815479&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-auth-ldap/pom.xml (original)
+++ jackrabbit/oak/trunk/oak-auth-ldap/pom.xml Thu Nov 16 16:41:53 2017
@@ -35,7 +35,7 @@
         <apacheds.test.version>2.0.0-M24</apacheds.test.version>
         <!-- enable execution of jacoco and set minimal line coverage -->
         <skip.coverage>false</skip.coverage>
-        <minimum.coverage>0.73</minimum.coverage>
+        <minimum.coverage>0.86</minimum.coverage>
     </properties>
 
     <build>
@@ -404,5 +404,16 @@
                 </exclusion>
             </exclusions>
         </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.testing.osgi-mock</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <version>2.11.0</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>

Modified: 
jackrabbit/oak/trunk/oak-auth-ldap/src/main/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/LdapIdentityProvider.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-auth-ldap/src/main/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/LdapIdentityProvider.java?rev=1815479&r1=1815478&r2=1815479&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-auth-ldap/src/main/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/LdapIdentityProvider.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-auth-ldap/src/main/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/LdapIdentityProvider.java
 Thu Nov 16 16:41:53 2017
@@ -258,9 +258,7 @@ public class LdapIdentityProvider implem
             } else {
                 return null;
             }
-        } catch (LdapException e) {
-            throw lookupFailedException(e, timer);
-        } catch (CursorException e) {
+        } catch (LdapException | CursorException e) {
             throw lookupFailedException(e, timer);
         } finally {
             disconnect(connection);
@@ -283,9 +281,7 @@ public class LdapIdentityProvider implem
             } else {
                 return null;
             }
-        } catch (LdapException e) {
-            throw lookupFailedException(e, timer);
-        } catch (CursorException e) {
+        } catch (LdapException | CursorException e) {
             throw lookupFailedException(e, timer);
         } finally {
             disconnect(connection);
@@ -311,9 +307,7 @@ public class LdapIdentityProvider implem
                     return null;
                 }
             };
-        } catch (LdapException e) {
-            throw lookupFailedException(e, null);
-        } catch (CursorException e) {
+        } catch (LdapException | CursorException e) {
             throw lookupFailedException(e, null);
         }
     }
@@ -337,9 +331,7 @@ public class LdapIdentityProvider implem
                     return null;
                 }
             };
-        } catch (LdapException e) {
-            throw lookupFailedException(e, null);
-        } catch (CursorException e) {
+        } catch (LdapException | CursorException e) {
             throw lookupFailedException(e, null);
         }
     }
@@ -693,11 +685,7 @@ public class LdapIdentityProvider implem
                     Entry entry = (Entry) page.get(pos);
                     findNextEntry();
                     return entry;
-                } catch (LdapException e) {
-                    log.error("Error while performing LDAP search", e);
-                } catch (CursorException e) {
-                    log.error("Error while performing LDAP search", e);
-                } catch (ExternalIdentityException e) {
+                } catch (LdapException | CursorException | 
ExternalIdentityException e) {
                     log.error("Error while performing LDAP search", e);
                 }
             }
@@ -851,7 +839,7 @@ public class LdapIdentityProvider implem
                 final Object propValue;
                 // for multivalue properties, store as collection
                 if (attr.size() > 1) {
-                    List<String> values = new ArrayList<String>();
+                    List<String> values = new ArrayList();
                     for (Value<?> value : attr) {
                         values.add(value.getString());
                     }

Added: 
jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/LdapIdentityPropertiesTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/LdapIdentityPropertiesTest.java?rev=1815479&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/LdapIdentityPropertiesTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/LdapIdentityPropertiesTest.java
 Thu Nov 16 16:41:53 2017
@@ -0,0 +1,127 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.jackrabbit.oak.security.authentication.ldap.impl;
+
+import com.google.common.collect.Maps;
+import java.util.Map;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+public class LdapIdentityPropertiesTest {
+
+    private LdapIdentityProperties properties = new LdapIdentityProperties();
+
+    @Before
+    public void before() {
+        properties.put("a", "value");
+        properties.put("A", "value2");
+    }
+
+    @Test
+    public void testConstructorWithInitialSize() {
+        LdapIdentityProperties props = new LdapIdentityProperties(5);
+        assertTrue(props.isEmpty());
+    }
+
+    @Test
+    public void testConstructorWithInitialSizeLoadFactor() {
+        LdapIdentityProperties props = new LdapIdentityProperties(5, 
Long.MAX_VALUE);
+        assertTrue(props.isEmpty());
+    }
+
+    @Test
+    public void testConstructorWithMap() {
+        Map m = Maps.newLinkedHashMap();
+        m.put("b", "v");
+        m.put("B", "v2");
+        LdapIdentityProperties props = new LdapIdentityProperties(m);
+        assertEquals(2, props.size());
+        assertEquals(props.get("b"), props.get("B"));
+    }
+
+    @Test
+    public void testContainsKey() {
+        assertTrue(properties.containsKey("a"));
+        assertTrue(properties.containsKey("A"));
+        assertFalse(properties.containsKey("c"));
+    }
+
+    @Test
+    public void testContainsKeyNull() {
+        assertFalse(properties.containsKey(null));
+    }
+
+    @Test
+    public void testPut() {
+        assertEquals(2, properties.size());
+        assertTrue(properties.containsKey("A"));
+        assertTrue(properties.containsKey("a"));
+    }
+
+    @Test
+    public void testPutNull() {
+        assertNull(properties.put(null, "value"));
+    }
+
+    @Test
+    public void testPutAll() {
+        Map m = Maps.newLinkedHashMap();
+        m.put("b", "v");
+        m.put("B", "v2");
+        properties.putAll(m);
+        assertEquals(4, properties.size());
+        assertEquals(properties.get("b"), properties.get("B"));
+    }
+
+    @Test
+    public void testGet() {
+        assertEquals(properties.get("a"), properties.get("A"));
+    }
+
+    @Test
+    public void testGetNull() {
+        assertNull(properties.get(null));
+    }
+
+    @Test
+    public void testRemove() {
+        assertEquals("value2", properties.remove("A"));
+        assertEquals(1, properties.size());
+        assertEquals("value", properties.remove("a"));
+        assertTrue(properties.isEmpty());
+    }
+
+    @Test
+    public void testClear() {
+        properties.clear();
+        assertTrue(properties.isEmpty());
+        assertFalse(properties.containsKey("a"));
+        assertFalse(properties.containsKey("A"));
+    }
+
+    @Test
+    public void testNonStringKey() {
+        properties.put("2", "value");
+        assertTrue(properties.containsKey(2));
+        assertFalse(properties.containsKey(3));
+    }
+}
\ No newline at end of file

Propchange: 
jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/LdapIdentityPropertiesTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/LdapIdentityProviderOsgiTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/LdapIdentityProviderOsgiTest.java?rev=1815479&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/LdapIdentityProviderOsgiTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/LdapIdentityProviderOsgiTest.java
 Thu Nov 16 16:41:53 2017
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.jackrabbit.oak.security.authentication.ldap.impl;
+
+import javax.jcr.GuestCredentials;
+
+import 
org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalIdentityException;
+import 
org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalIdentityRef;
+import org.apache.sling.testing.mock.osgi.junit.OsgiContext;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+public class LdapIdentityProviderOsgiTest {
+
+    @Rule
+    public final OsgiContext context = new OsgiContext();
+
+    private LdapIdentityProvider provider = new LdapIdentityProvider();
+
+    @Before
+    public void before() throws Exception {
+        context.registerInjectActivateService(provider);
+    }
+
+    @Test(expected = ExternalIdentityException.class)
+    public void testFromExternalIdentityRefForeign() throws Exception {
+        provider.fromExternalIdentityRef(new ExternalIdentityRef("id", 
"anotherName"));
+    }
+
+    @Test
+    public void testFromExternalIdentityRef() throws Exception {
+        assertEquals("id", provider.fromExternalIdentityRef(new 
ExternalIdentityRef("id", provider.getName())));
+    }
+
+    @Test
+    public void testGetName() {
+        assertEquals(LdapProviderConfig.PARAM_NAME_DEFAULT, 
provider.getName());
+    }
+
+    @Test
+    public void testAuthenticateOtherCredentials() throws Exception {
+        assertNull(provider.authenticate(new GuestCredentials()));
+    }
+
+    @Test
+    public void testGetIdentityForeingRef() throws Exception {
+        ExternalIdentityRef ref = new ExternalIdentityRef("id", "anotherName");
+        assertNull(provider.getIdentity(ref));
+    }
+
+    @Test
+    public void testGetDeclaredGroupRefsForeignRef() throws Exception {
+        ExternalIdentityRef ref = new ExternalIdentityRef("id", "anotherName");
+        assertTrue(provider.getDeclaredGroupRefs(ref).isEmpty());
+    }
+
+    @Test
+    public void testGetDeclaredMemberRefsForeignRef() throws Exception {
+        ExternalIdentityRef ref = new ExternalIdentityRef("id", "anotherName");
+        assertTrue(provider.getDeclaredMemberRefs(ref).isEmpty());
+    }
+
+    @Test(expected = ExternalIdentityException.class)
+    public void testGetUserMissingConnection() throws Exception {
+        provider.getUser("user");
+    }
+
+    @Test(expected = ExternalIdentityException.class)
+    public void testGetGroupMissingConnection() throws Exception {
+        provider.getGroup("gr");
+    }
+
+    @Test(expected = ExternalIdentityException.class)
+    public void testListGroupsMissingConnections() throws Exception {
+        provider.listGroups().hasNext();
+    }
+
+    @Test(expected = ExternalIdentityException.class)
+    public void testListUsersMissingConnections() throws Exception {
+        provider.listUsers().hasNext();
+    }
+}
\ No newline at end of file

Propchange: 
jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/LdapIdentityProviderOsgiTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/LdapProviderConfigTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/LdapProviderConfigTest.java?rev=1815479&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/LdapProviderConfigTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/LdapProviderConfigTest.java
 Thu Nov 16 16:41:53 2017
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.jackrabbit.oak.security.authentication.ldap.impl;
+
+import org.apache.jackrabbit.oak.spi.security.ConfigurationParameters;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class LdapProviderConfigTest {
+
+    @Test
+    public void testOfEmptyConfigurationParameters() {
+        LdapProviderConfig config = 
LdapProviderConfig.of(ConfigurationParameters.EMPTY);
+        assertEquals(LdapProviderConfig.PARAM_NAME_DEFAULT, config.getName());
+    }
+
+    @Test
+    public void testOfConfigurationParameters() {
+        LdapProviderConfig config = 
LdapProviderConfig.of(ConfigurationParameters.of(LdapProviderConfig.PARAM_NAME, 
"name"));
+        assertEquals("name", config.getName());
+    }
+
+    @Test
+    public void testOfConfigurationParametersIncludingSearchTimeout() {
+        LdapProviderConfig config = 
LdapProviderConfig.of(ConfigurationParameters.of(LdapProviderConfig.PARAM_SEARCH_TIMEOUT,
 25));
+        assertEquals(25, config.getSearchTimeout());
+    }
+
+}
\ No newline at end of file

Propchange: 
jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/LdapProviderConfigTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/PoolableUnboundConnectionFactoryTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/PoolableUnboundConnectionFactoryTest.java?rev=1815479&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/PoolableUnboundConnectionFactoryTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/PoolableUnboundConnectionFactoryTest.java
 Thu Nov 16 16:41:53 2017
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.jackrabbit.oak.security.authentication.ldap.impl;
+
+import org.apache.directory.ldap.client.api.LdapConnectionConfig;
+import org.apache.directory.ldap.client.api.LdapConnectionValidator;
+import org.apache.directory.ldap.client.api.LookupLdapConnectionValidator;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class PoolableUnboundConnectionFactoryTest {
+
+    PoolableUnboundConnectionFactory factory = new 
PoolableUnboundConnectionFactory(new LdapConnectionConfig());
+
+    @Test
+    public void testGetValidator() {
+        LdapConnectionValidator validator = factory.getValidator();
+        assertTrue(validator instanceof LookupLdapConnectionValidator);
+    }
+
+    @Test
+    public void testSetValidator() {
+        LdapConnectionValidator validator = 
Mockito.mock(LdapConnectionValidator.class);
+        factory.setValidator(validator);
+
+        assertEquals(validator, factory.getValidator());
+    }
+}
\ No newline at end of file

Propchange: 
jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/PoolableUnboundConnectionFactoryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/UnboundLookupConnectionValidatorTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/UnboundLookupConnectionValidatorTest.java?rev=1815479&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/UnboundLookupConnectionValidatorTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/UnboundLookupConnectionValidatorTest.java
 Thu Nov 16 16:41:53 2017
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.jackrabbit.oak.security.authentication.ldap.impl;
+
+import org.apache.directory.api.ldap.model.constants.SchemaConstants;
+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.name.Dn;
+import org.apache.directory.ldap.client.api.LdapConnection;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.when;
+
+public class UnboundLookupConnectionValidatorTest {
+
+    private UnboundLookupConnectionValidator validator = new 
UnboundLookupConnectionValidator();
+
+    @Test
+    public void testValidateThrowsException() throws Exception {
+        LdapConnection connection = Mockito.mock(LdapConnection.class);
+        doThrow(LdapException.class).when(connection).lookup(Dn.ROOT_DSE, 
SchemaConstants.NO_ATTRIBUTE);
+
+        assertFalse(validator.validate(connection));
+    }
+
+    @Test
+    public void testValidateNotConnected() {
+        LdapConnection connection = Mockito.mock(LdapConnection.class);
+        when(connection.isConnected()).thenReturn(false);
+
+        assertFalse(validator.validate(connection));
+    }
+
+    @Test
+    public void testValidateLookupReturnsNull() throws Exception {
+        LdapConnection connection = Mockito.mock(LdapConnection.class);
+        when(connection.isConnected()).thenReturn(true);
+        when(connection.lookup(Dn.ROOT_DSE, 
SchemaConstants.NO_ATTRIBUTE)).thenReturn(null);
+
+        assertFalse(validator.validate(connection));
+    }
+
+    @Test
+    public void testValidateNotConnectedLookupReturnsNull() throws Exception {
+        LdapConnection connection = Mockito.mock(LdapConnection.class);
+        when(connection.isConnected()).thenReturn(false);
+        when(connection.lookup(Dn.ROOT_DSE, 
SchemaConstants.NO_ATTRIBUTE)).thenReturn(null);
+
+        assertFalse(validator.validate(connection));
+    }
+
+    @Test
+    public void testValidate() throws Exception {
+        LdapConnection connection = Mockito.mock(LdapConnection.class);
+        when(connection.isConnected()).thenReturn(true);
+        when(connection.lookup(Dn.ROOT_DSE, 
SchemaConstants.NO_ATTRIBUTE)).thenReturn(Mockito.mock(Entry.class));
+
+        assertTrue(validator.validate(connection));
+    }
+}
\ No newline at end of file

Propchange: 
jackrabbit/oak/trunk/oak-auth-ldap/src/test/java/org/apache/jackrabbit/oak/security/authentication/ldap/impl/UnboundLookupConnectionValidatorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to