keycloak-memoizeit

Details

diff --git a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/util/LDAPRule.java b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/util/LDAPRule.java
new file mode 100644
index 0000000..5cebe8c
--- /dev/null
+++ b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/util/LDAPRule.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2016 Red Hat, Inc. and/or its affiliates
+ * and other contributors as indicated by the @author tags.
+ *
+ * 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.keycloak.testsuite.util;
+
+import java.util.Map;
+import java.util.Properties;
+
+import org.junit.rules.ExternalResource;
+import org.keycloak.util.ldap.LDAPEmbeddedServer;
+
+/**
+ * @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
+ */
+public class LDAPRule extends ExternalResource {
+
+    public static final String LDAP_CONNECTION_PROPERTIES_LOCATION = "classpath:ldap/ldap-connection.properties";
+
+    protected LDAPTestConfiguration ldapTestConfiguration;
+    protected LDAPEmbeddedServer ldapEmbeddedServer;
+
+    @Override
+    protected void before() throws Throwable {
+        String connectionPropsLocation = getConnectionPropertiesLocation();
+        ldapTestConfiguration = LDAPTestConfiguration.readConfiguration(connectionPropsLocation);
+
+        if (ldapTestConfiguration.isStartEmbeddedLdapServer()) {
+            ldapEmbeddedServer = createServer();
+            ldapEmbeddedServer.init();
+            ldapEmbeddedServer.start();
+        }
+    }
+
+    @Override
+    protected void after() {
+        try {
+            if (ldapEmbeddedServer != null) {
+                ldapEmbeddedServer.stop();
+                ldapEmbeddedServer = null;
+                ldapTestConfiguration = null;
+            }
+        } catch (Exception e) {
+            throw new RuntimeException("Error tearDown Embedded LDAP server.", e);
+        }
+    }
+
+    protected String getConnectionPropertiesLocation() {
+        return LDAP_CONNECTION_PROPERTIES_LOCATION;
+    }
+
+    protected LDAPEmbeddedServer createServer() {
+        Properties defaultProperties = new Properties();
+        defaultProperties.setProperty(LDAPEmbeddedServer.PROPERTY_DSF, LDAPEmbeddedServer.DSF_INMEMORY);
+        defaultProperties.setProperty(LDAPEmbeddedServer.PROPERTY_LDIF_FILE, "classpath:ldap/users.ldif");
+
+        return new LDAPEmbeddedServer(defaultProperties);
+    }
+
+    public Map<String, String> getConfig() {
+        return ldapTestConfiguration.getLDAPConfig();
+    }
+
+    public int getSleepTime() {
+        return ldapTestConfiguration.getSleepTime();
+    }
+}
diff --git a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/util/LDAPTestConfiguration.java b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/util/LDAPTestConfiguration.java
index f4a5d50..5540c39 100644
--- a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/util/LDAPTestConfiguration.java
+++ b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/util/LDAPTestConfiguration.java
@@ -20,13 +20,19 @@ package org.keycloak.testsuite.util;
 import static org.keycloak.testsuite.util.IOUtil.PROJECT_BUILD_DIRECTORY;
 
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.io.InputStream;
+import java.net.URL;
 import java.util.HashMap;
 import java.util.Map;
 
 import org.apache.commons.configuration.PropertiesConfiguration;
 import org.jboss.logging.Logger;
+import org.keycloak.common.constants.GenericConstants;
 import org.keycloak.common.constants.KerberosConstants;
+import org.keycloak.common.util.FindFile;
 import org.keycloak.models.LDAPConstants;
 import org.keycloak.models.UserFederationProvider;
 
@@ -37,7 +43,6 @@ public class LDAPTestConfiguration {
 
     private static final Logger log = Logger.getLogger(LDAPTestConfiguration.class);
 
-    private String connectionPropertiesLocation;
     private int sleepTime;
     private boolean startEmbeddedLdapServer = true;
     private Map<String, String> config;
@@ -95,8 +100,7 @@ public class LDAPTestConfiguration {
 
     public static LDAPTestConfiguration readConfiguration(String connectionPropertiesLocation) {
         LDAPTestConfiguration ldapTestConfiguration = new LDAPTestConfiguration();
-        ldapTestConfiguration.setConnectionPropertiesLocation(getResource(connectionPropertiesLocation));
-        ldapTestConfiguration.loadConnectionProperties();
+        ldapTestConfiguration.loadConnectionProperties(connectionPropertiesLocation);
         return ldapTestConfiguration;
     }
     
@@ -104,13 +108,28 @@ public class LDAPTestConfiguration {
         return new File(PROJECT_BUILD_DIRECTORY, "dependency/kerberos/" + resourceName).getAbsolutePath();
     }
 
-    protected void loadConnectionProperties() {
+    protected void loadConnectionProperties(String connectionPropertiesLocation) {
+        // TODO: Improve and possibly use FindFile
+        InputStream is;
+        try {
+            if (connectionPropertiesLocation.startsWith(GenericConstants.PROTOCOL_CLASSPATH)) {
+                String classPathLocation = connectionPropertiesLocation.replace(GenericConstants.PROTOCOL_CLASSPATH, "");
+                log.info("Reading LDAP configuration from classpath from: " + classPathLocation);
+                is = LDAPTestConfiguration.class.getClassLoader().getResourceAsStream(classPathLocation);
+            } else {
+                String file = getResource(connectionPropertiesLocation);
+                log.info("Reading LDAP configuration from: " + connectionPropertiesLocation);
+                is = new FileInputStream(file);
+            }
+        } catch (IOException ioe) {
+            throw new RuntimeException(ioe);
+        }
+
         PropertiesConfiguration p;
         try {
-            log.info("Reading LDAP configuration from: " + connectionPropertiesLocation);
             p = new PropertiesConfiguration();
             p.setDelimiterParsingDisabled(true);
-            p.load(connectionPropertiesLocation);
+            p.load(is);
         }
         catch (Exception e) {
             throw new RuntimeException(e);
@@ -139,10 +158,6 @@ public class LDAPTestConfiguration {
         return config;
     }
 
-    public void setConnectionPropertiesLocation(String connectionPropertiesLocation) {
-        this.connectionPropertiesLocation = connectionPropertiesLocation;
-    }
-
     public boolean isStartEmbeddedLdapServer() {
         return startEmbeddedLdapServer;
     }
diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/UserFederationLdapConnectionTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/UserFederationLdapConnectionTest.java
new file mode 100644
index 0000000..041d58c
--- /dev/null
+++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/admin/UserFederationLdapConnectionTest.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2016 Red Hat, Inc. and/or its affiliates
+ * and other contributors as indicated by the @author tags.
+ *
+ * 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.keycloak.testsuite.admin;
+
+import javax.ws.rs.core.Response;
+
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.keycloak.services.managers.LDAPConnectionTestManager;
+import org.keycloak.testsuite.Assert;
+import org.keycloak.testsuite.util.LDAPRule;
+
+/**
+ * @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
+ */
+public class UserFederationLdapConnectionTest extends AbstractAdminTest {
+
+    @ClassRule
+    public static LDAPRule ldapRule = new LDAPRule();
+
+    @Test
+    public void testLdapConnections1() {
+        // Unknown action
+        Response response = realm.testLDAPConnection("unknown", "ldap://localhost:10389", "foo", "bar", "false");
+        assertStatus(response, 400);
+
+        // Bad host
+        response = realm.testLDAPConnection(LDAPConnectionTestManager.TEST_CONNECTION, "ldap://localhostt:10389", "foo", "bar", "false");
+        assertStatus(response, 400);
+
+        // Connection success
+        response = realm.testLDAPConnection(LDAPConnectionTestManager.TEST_CONNECTION, "ldap://localhost:10389", "foo", "bar", "false");
+        assertStatus(response, 204);
+
+        // Bad authentication
+        response = realm.testLDAPConnection(LDAPConnectionTestManager.TEST_AUTHENTICATION, "ldap://localhost:10389", "foo", "bar", "false");
+        assertStatus(response, 400);
+
+        // Authentication success
+        response = realm.testLDAPConnection(LDAPConnectionTestManager.TEST_AUTHENTICATION, "ldap://localhost:10389", "uid=admin,ou=system", "secret", "false");
+        assertStatus(response, 204);
+
+    }
+
+    private void assertStatus(Response response, int status) {
+        Assert.assertEquals(status, response.getStatus());
+        response.close();
+    }
+}
diff --git a/testsuite/integration-arquillian/tests/base/src/test/resources/ldap/ldap-connection.properties b/testsuite/integration-arquillian/tests/base/src/test/resources/ldap/ldap-connection.properties
new file mode 100644
index 0000000..610312c
--- /dev/null
+++ b/testsuite/integration-arquillian/tests/base/src/test/resources/ldap/ldap-connection.properties
@@ -0,0 +1,26 @@
+#
+# Copyright 2016 Red Hat, Inc. and/or its affiliates
+# and other contributors as indicated by the @author tags.
+#
+# 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.
+#
+
+idm.test.ldap.connection.url=ldap\://localhost\:10389
+idm.test.ldap.base.dn=dc\=keycloak,dc\=org
+idm.test.ldap.user.dn.suffix=ou\=People,dc\=keycloak,dc\=org
+idm.test.ldap.start.embedded.ldap.server=true
+idm.test.ldap.bind.dn=uid\=admin,ou\=system
+idm.test.ldap.bind.credential=secret
+idm.test.ldap.connection.pooling=true
+idm.test.ldap.pagination=true
+idm.test.ldap.batch.size.for.sync=3
\ No newline at end of file