Login operation test. Works only from maven

git-svn-id: https://svn.apache.org/repos/asf/karaf/sandbox/webconsole/trunk@1165243 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/core/pom.xml b/core/pom.xml
index 5de5abc..d8834f5 100644
--- a/core/pom.xml
+++ b/core/pom.xml
@@ -61,6 +61,26 @@
             <artifactId>org.apache.karaf.jaas.modules</artifactId>
             <version>${karaf.version}</version>
         </dependency>
+
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>servlet-api</artifactId>
+            <version>2.5</version>
+            <scope>provided</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.8.2</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-log4j12</artifactId>
+            <version>${slf4j.version}</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>
@@ -105,6 +125,19 @@
                     </instructions>
                 </configuration>
             </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-surefire-plugin</artifactId>
+                <version>2.9</version>
+                <configuration>
+                    <systemProperties>
+                        <property>
+                            <name>java.security.auth.login.config</name>
+                            <value>${basedir}/src/test/resources/jaas.conf</value>
+                        </property>
+                    </systemProperties>
+                </configuration>
+            </plugin>
         </plugins>
     </build>
 </project>
diff --git a/core/src/test/java/org/apache/karaf/webconsole/core/LoginTest.java b/core/src/test/java/org/apache/karaf/webconsole/core/LoginTest.java
new file mode 100644
index 0000000..6d100d2
--- /dev/null
+++ b/core/src/test/java/org/apache/karaf/webconsole/core/LoginTest.java
@@ -0,0 +1,83 @@
+/*
+ * 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.karaf.webconsole.core;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.karaf.webconsole.core.brand.DefaultBrandProvider;
+import org.apache.karaf.webconsole.core.dashboard.DashboardPage;
+import org.apache.karaf.webconsole.core.internal.WebConsoleApplication;
+import org.apache.karaf.webconsole.core.page.LoginPage;
+import org.apache.wicket.authorization.UnauthorizedInstantiationException;
+import org.apache.wicket.protocol.http.WebApplication;
+import org.apache.wicket.util.tester.FormTester;
+import org.apache.wicket.util.tester.WicketTester;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.BlockJUnit4ClassRunner;
+
+/**
+ * Test login operation and role verification.
+ */
+@RunWith(BlockJUnit4ClassRunner.class)
+public class LoginTest {
+
+    /**
+     * Application instance.
+     */
+    private WebApplication application;
+
+    @Before
+    public void setUp() {
+        application = new WebConsoleApplication();
+
+        final Map<String, Object> values = new HashMap<String, Object>();
+        values.put("brandProvider", new DefaultBrandProvider());
+
+        application.addComponentInstantiationListener(new TestInjector(values));
+    }
+
+    @Test
+    public void testSuccessLogin() throws Exception {
+        WicketTester tester = new WicketTester(application);
+        tester.startPage(LoginPage.class);
+        //tester.debugComponentTrees();
+        FormTester form = tester.newFormTester("signIn:signInForm");
+
+        form.setValue("username", "rootadmin");
+        form.setValue("password", "admin");
+        form.submit();
+
+        tester.assertNoErrorMessage();
+        tester.assertRenderedPage(DashboardPage.class);
+    }
+
+    @Test(expected = UnauthorizedInstantiationException.class)
+    public void testWrongRoleLogin() throws Exception {
+        WicketTester tester = new WicketTester(application);
+        tester.startPage(LoginPage.class);
+
+        FormTester form = tester.newFormTester("signIn:signInForm");
+
+        form.setValue("username", "someuser");
+        form.setValue("password", "user");
+        form.submit();
+    }
+
+}
diff --git a/core/src/test/java/org/apache/karaf/webconsole/core/TestInjector.java b/core/src/test/java/org/apache/karaf/webconsole/core/TestInjector.java
new file mode 100644
index 0000000..cbef675
--- /dev/null
+++ b/core/src/test/java/org/apache/karaf/webconsole/core/TestInjector.java
@@ -0,0 +1,96 @@
+/*
+ * 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.karaf.webconsole.core;
+
+import java.io.Serializable;
+import java.lang.reflect.Field;
+import java.util.Map;
+
+import net.sf.cglib.proxy.Enhancer;
+
+import org.apache.wicket.Component;
+import org.apache.wicket.application.IComponentInstantiationListener;
+import org.ops4j.pax.wicket.api.PaxWicketBean;
+import org.ops4j.pax.wicket.internal.injection.AbstractPaxWicketInjector;
+import org.ops4j.pax.wicket.internal.injection.ComponentProxy;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * {@link IComponentInstantiationListener} which injects given values to fields
+ * annotated with {@link PaxWicketBean}.
+ */
+public class TestInjector extends AbstractPaxWicketInjector implements IComponentInstantiationListener {
+
+    /**
+     * Logger.
+     */
+    private Logger logger = LoggerFactory.getLogger(TestInjector.class);
+
+    /**
+     * Values to inject.
+     */
+    private final Map<String, Object> values;
+
+    public TestInjector(Map<String, Object> values) {
+        this.values = values;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void onInstantiation(Component component) {
+
+        for (Field field : getFields(component.getClass())) {
+            // iterate over fields and check if there is values to inject 
+            String name = field.getName();
+            Class<?> type = field.getType();
+
+            if (values.containsKey(name)) {
+                // we have value..
+                Object value = values.get(name);
+
+                // create instance proxy, just like pax-wicket does
+                if (type.isAssignableFrom(value.getClass())){
+                    Enhancer enhancer = new Enhancer();
+                    enhancer.setSuperclass(value.getClass());
+                    if (!(value instanceof Serializable)) {
+                        logger.warn("Value for field {}.{} is not serializable. Adding Serializable interface for test, result in production may vary",
+                            field.getDeclaringClass(), name);
+                        enhancer.setInterfaces(new Class[] {Serializable.class});
+                    }
+                    enhancer.setCallback(new ComponentProxy("test", null));
+                    setField(component, field, enhancer.create());
+                } else {
+                    logger.error("Type mismath for field {}.{}. Expected {} but {} given", new Object[] {
+                        field.getDeclaringClass().getName(), name, field.getType().getName(), value.getClass().getName()
+                    });
+                    System.err.println("Unknown type " + field.getType());
+                }
+            } else {
+                logger.warn("No value provided for field {}.{}", field.getDeclaringClass().getName(), name);
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void inject(Object toInject, Class<?> toHandle) {
+        throw new UnsupportedOperationException("inject operation should never be executed");
+    }
+}
\ No newline at end of file
diff --git a/core/src/test/resources/jaas.conf b/core/src/test/resources/jaas.conf
new file mode 100644
index 0000000..2230446
--- /dev/null
+++ b/core/src/test/resources/jaas.conf
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+karaf {
+	org.apache.karaf.jaas.modules.properties.PropertiesLoginModule required
+	users="src/test/resources/users.properties";
+};
\ No newline at end of file
diff --git a/core/src/test/resources/users.properties b/core/src/test/resources/users.properties
new file mode 100644
index 0000000..a78473e
--- /dev/null
+++ b/core/src/test/resources/users.properties
@@ -0,0 +1,16 @@
+# 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.
+rootadmin=admin,admin
+someuser=user,user
\ No newline at end of file