Author: ningjiang
Date: Tue Aug  3 09:28:51 2010
New Revision: 981788

URL: http://svn.apache.org/viewvc?rev=981788&view=rev
Log:
CAMEL-3006 Added an OSGi test for camel shiro component

Added:
    
camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/shiro/
    
camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/shiro/ShiroAuthenticationTest.java
   (with props)
    
camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/shiro/
    
camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/shiro/securityconfig.ini
Modified:
    camel/trunk/tests/camel-itest-osgi/pom.xml

Modified: camel/trunk/tests/camel-itest-osgi/pom.xml
URL: 
http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest-osgi/pom.xml?rev=981788&r1=981787&r2=981788&view=diff
==============================================================================
--- camel/trunk/tests/camel-itest-osgi/pom.xml (original)
+++ camel/trunk/tests/camel-itest-osgi/pom.xml Tue Aug  3 09:28:51 2010
@@ -99,6 +99,11 @@
       <artifactId>camel-protobuf</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.camel</groupId>
+      <artifactId>camel-shiro</artifactId>
+      <scope>test</scope>
+    </dependency>
        <dependency>
          <groupId>org.apache.camel.tests</groupId>
          <artifactId>org.apache.camel.tests.mock-javamail_1.7</artifactId>

Added: 
camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/shiro/ShiroAuthenticationTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/shiro/ShiroAuthenticationTest.java?rev=981788&view=auto
==============================================================================
--- 
camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/shiro/ShiroAuthenticationTest.java
 (added)
+++ 
camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/shiro/ShiroAuthenticationTest.java
 Tue Aug  3 09:28:51 2010
@@ -0,0 +1,158 @@
+/**
+ * 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.camel.itest.osgi.shiro;
+
+import javax.naming.AuthenticationException;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.component.shiro.security.ShiroSecurityPolicy;
+import org.apache.camel.component.shiro.security.ShiroSecurityToken;
+import org.apache.camel.component.shiro.security.ShiroSecurityTokenInjector;
+import org.apache.camel.itest.osgi.OSGiIntegrationTestSupport;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.shiro.authc.IncorrectCredentialsException;
+import org.apache.shiro.authc.LockedAccountException;
+import org.apache.shiro.authc.UnknownAccountException;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.Configuration;
+import org.ops4j.pax.exam.junit.JUnit4TestRunner;
+
+import static org.ops4j.pax.exam.CoreOptions.felix;
+import static org.ops4j.pax.exam.CoreOptions.maven;
+import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
+import static org.ops4j.pax.exam.CoreOptions.options;
+import static org.ops4j.pax.exam.CoreOptions.wrappedBundle;
+import static org.ops4j.pax.exam.container.def.PaxRunnerOptions.profile;
+import static org.ops4j.pax.exam.container.def.PaxRunnerOptions.scanFeatures;
+import static 
org.ops4j.pax.exam.container.def.PaxRunnerOptions.workingDirectory;
+
+...@runwith(JUnit4TestRunner.class)
+public class ShiroAuthenticationTest extends OSGiIntegrationTestSupport {
+    
+    @EndpointInject(uri = "mock:success")
+    protected MockEndpoint successEndpoint;
+
+    @EndpointInject(uri = "mock:authenticationException")
+    protected MockEndpoint failureEndpoint;
+
+    private byte[] passPhrase = {
+        (byte) 0x08, (byte) 0x09, (byte) 0x0A, (byte) 0x0B,
+        (byte) 0x0C, (byte) 0x0D, (byte) 0x0E, (byte) 0x0F,
+        (byte) 0x10, (byte) 0x11, (byte) 0x12, (byte) 0x13,
+        (byte) 0x14, (byte) 0x15, (byte) 0x16, (byte) 0x17};    
+    
+    @Test
+    public void testShiroAuthenticationFailure() throws Exception {        
+        //Incorrect password
+        ShiroSecurityToken shiroSecurityToken = new 
ShiroSecurityToken("ringo", "stirr");
+        TestShiroSecurityTokenInjector shiroSecurityTokenInjector = new 
TestShiroSecurityTokenInjector(shiroSecurityToken, passPhrase);
+        
+        successEndpoint.expectedMessageCount(0);
+        failureEndpoint.expectedMessageCount(1);
+        
+        template.send("direct:secureEndpoint", shiroSecurityTokenInjector);
+        
+        successEndpoint.assertIsSatisfied();
+        failureEndpoint.assertIsSatisfied();
+    }
+    
+    @Test
+    public void testSuccessfulShiroAuthenticationWithNoAuthorization() throws 
Exception {        
+        //Incorrect password
+        ShiroSecurityToken shiroSecurityToken = new 
ShiroSecurityToken("ringo", "starr");
+        TestShiroSecurityTokenInjector shiroSecurityTokenInjector = new 
TestShiroSecurityTokenInjector(shiroSecurityToken, passPhrase);
+        
+        successEndpoint.expectedMessageCount(1);
+        failureEndpoint.expectedMessageCount(0);
+        
+        template.send("direct:secureEndpoint", shiroSecurityTokenInjector);
+        
+        successEndpoint.assertIsSatisfied();
+        failureEndpoint.assertIsSatisfied();
+    }
+    
+    @Configuration
+    public static Option[] configure() {
+        Option[] options = options(
+            // install the spring dm profile            
+            profile("spring.dm").version("1.2.0"), 
+            // this is how you set the default log level when using pax 
logging (logProfile)
+            
org.ops4j.pax.exam.CoreOptions.systemProperty("org.ops4j.pax.logging.DefaultServiceLog.level").value("INFO"),
+            
+            // using the features to install the camel components             
+            scanFeatures(getCamelKarafFeatureUrl(),                         
+                          "camel-core", "camel-spring", "camel-test"),
+            // add the camel-shior bundles
+            
wrappedBundle(maven().groupId("org.apache.shiro").artifactId("shiro-core").version("1.0.0-incubating")),
+                                        
+            
mavenBundle().groupId("org.apache.camel").artifactId("camel-shiro").versionAsInProject(),
+                          
+            workingDirectory("target/paxrunner/"),
+
+            felix());
+        
+        return options;
+    }
+
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+         
+        return new RouteBuilder() {
+            public void configure() {
+                // need to update the CCL as it could be no defined
+                
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
+                
+                final ShiroSecurityPolicy securityPolicy = new 
ShiroSecurityPolicy("classpath:/org/apache/camel/itest/osgi/shiro/securityconfig.ini",
 passPhrase);
+                
+                onException(UnknownAccountException.class).
+                    to("mock:authenticationException");
+                onException(IncorrectCredentialsException.class).
+                    to("mock:authenticationException");
+                onException(LockedAccountException.class).
+                    to("mock:authenticationException");
+                onException(AuthenticationException.class).
+                    to("mock:authenticationException");
+                
+                from("direct:secureEndpoint").
+                    to("log:incoming payload").
+                    policy(securityPolicy).
+                    to("mock:success");
+            }
+        };
+    }
+
+    
+    private class TestShiroSecurityTokenInjector extends 
ShiroSecurityTokenInjector {
+
+        public TestShiroSecurityTokenInjector(
+                ShiroSecurityToken shiroSecurityToken, byte[] bytes) {
+            super(shiroSecurityToken, bytes);
+        }
+        
+        public void process(Exchange exchange) throws Exception {
+            exchange.getIn().setHeader("SHIRO_SECURITY_TOKEN", encrypt());
+            exchange.getIn().setBody("Beatle Mania");
+        }
+    }
+    
+}

Propchange: 
camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/shiro/ShiroAuthenticationTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/shiro/ShiroAuthenticationTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: 
camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/shiro/securityconfig.ini
URL: 
http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/shiro/securityconfig.ini?rev=981788&view=auto
==============================================================================
--- 
camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/shiro/securityconfig.ini
 (added)
+++ 
camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/shiro/securityconfig.ini
 Tue Aug  3 09:28:51 2010
@@ -0,0 +1,36 @@
+#
+# 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.
+#
+
+[users]
+# user 'ringo' with password 'starr' and the 'hero' role
+ringo = starr, sec-level1
+george = harrison, sec-level2
+john = lennon, sec-level3
+paul = mccartney, sec-level3
+
+[roles]
+# 'sec-level3' role has all permissions, indicated by the wildcard '*'
+sec-level3 = *
+
+# The 'sec-level2' role can do anything with access of permission readonly (*) 
to help
+sec-level2 = zone1:*
+
+# The 'sec-level1' role can do anything with access of permission readonly   
+sec-level1 = zone1:readonly:*
+


Reply via email to