Author: hiranya
Date: Mon Aug  5 23:53:59 2013
New Revision: 1510820

URL: http://svn.apache.org/r1510820
Log:
Adding test cases for session dispatchers

Added:
    
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/endpoints/dispatch/
    
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/endpoints/dispatch/HttpSessionDispatcherTest.java
    
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/endpoints/dispatch/SimpleClientSessionDispatcherTest.java
Modified:
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/endpoints/dispatch/AbstractDispatcher.java
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/endpoints/dispatch/SALSessions.java

Modified: 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/endpoints/dispatch/AbstractDispatcher.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/endpoints/dispatch/AbstractDispatcher.java?rev=1510820&r1=1510819&r2=1510820&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/endpoints/dispatch/AbstractDispatcher.java
 (original)
+++ 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/endpoints/dispatch/AbstractDispatcher.java
 Mon Aug  5 23:53:59 2013
@@ -36,7 +36,6 @@ import java.util.Map;
 public abstract class AbstractDispatcher implements Dispatcher {
 
     protected Log log;
-    private final static String TRANSPORT_HEADERS = "TRANSPORT_HEADERS";
 
     protected AbstractDispatcher() {
         log = LogFactory.getLog(this.getClass());
@@ -149,7 +148,7 @@ public abstract class AbstractDispatcher
         org.apache.axis2.context.MessageContext axis2MessageContext =
                 ((Axis2MessageContext) synCtx).getAxis2MessageContext();
 
-        Object o = axis2MessageContext.getProperty(TRANSPORT_HEADERS);
+        Object o = 
axis2MessageContext.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
         if (o != null && o instanceof Map) {
             return (Map) o;
         }

Modified: 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/endpoints/dispatch/SALSessions.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/endpoints/dispatch/SALSessions.java?rev=1510820&r1=1510819&r2=1510820&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/endpoints/dispatch/SALSessions.java
 (original)
+++ 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/endpoints/dispatch/SALSessions.java
 Mon Aug  5 23:53:59 2013
@@ -131,7 +131,7 @@ public class SALSessions {
 
         if (sessionID == null || "".equals(sessionID)) {
             if (log.isDebugEnabled()) {
-                log.debug("Cannot find session ID .Returing null");
+                log.debug("Cannot find session ID. Returning null.");
             }
             return;
         }
@@ -524,7 +524,7 @@ public class SALSessions {
 
     /*
      * Factory method to create a session information using given endpoint 
list,
-     * session id and other informations
+     * session id and other information
      */
     private SessionInformation createSessionInformation(MessageContext synCtx,
                                                         String id, 
List<Endpoint> endpoints) {
@@ -580,7 +580,7 @@ public class SALSessions {
 
     /*
      * Factory method to create a session information using a given member 
node ,
-     * session id and other informations
+     * session id and other information
      */
     private SessionInformation createSessionInformation(MessageContext synCtx,
                                                         String id, Member 
currentMember) {

Added: 
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/endpoints/dispatch/HttpSessionDispatcherTest.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/endpoints/dispatch/HttpSessionDispatcherTest.java?rev=1510820&view=auto
==============================================================================
--- 
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/endpoints/dispatch/HttpSessionDispatcherTest.java
 (added)
+++ 
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/endpoints/dispatch/HttpSessionDispatcherTest.java
 Mon Aug  5 23:53:59 2013
@@ -0,0 +1,94 @@
+/*
+ *  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.synapse.endpoints.dispatch;
+
+import junit.framework.TestCase;
+import org.apache.axis2.clustering.Member;
+import org.apache.axis2.context.ConfigurationContext;
+import org.apache.axis2.engine.AxisConfiguration;
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.SynapseConstants;
+import org.apache.synapse.core.axis2.Axis2MessageContext;
+import org.apache.synapse.endpoints.AddressEndpoint;
+import org.apache.synapse.endpoints.Endpoint;
+import org.apache.synapse.mediators.TestUtils;
+
+import java.util.*;
+
+public class HttpSessionDispatcherTest extends TestCase {
+
+    public void testHttpSessionDispatcher() throws Exception {
+        AxisConfiguration axisConfiguration = new AxisConfiguration();
+        ConfigurationContext configContext = new 
ConfigurationContext(axisConfiguration);
+        SALSessions.getInstance().initialize(true, configContext);
+
+        List<Endpoint> endpoints = new ArrayList<Endpoint>();
+        AddressEndpoint endpoint = new AddressEndpoint();
+        endpoint.setName("ep1");
+        endpoints.add(endpoint);
+
+        Dispatcher dispatcher = new HttpSessionDispatcher();
+        assertTrue(dispatcher.isServerInitiatedSession());
+
+        // test session creation
+        String clientId = "JSESSIONID=760764CB72E96A7221506823748CF2AE";
+        dispatcher.updateSession(getMessageContext(clientId + "; Path=/", 
true, endpoints));
+        assertNotNull(SALSessions.getInstance().getSession(clientId));
+
+        // test session query
+        SessionInformation session = 
dispatcher.getSession(getMessageContext(clientId, false,
+                endpoints));
+        assertNotNull(session);
+        assertNotNull(session.getMember());
+
+        // test session removal
+        dispatcher.unbind(getMessageContext(clientId, false, endpoints));
+        session = dispatcher.getSession(getMessageContext(clientId, false, 
endpoints));
+        assertNull(session);
+        assertNull(SALSessions.getInstance().getSession(clientId));
+
+        SALSessions.getInstance().reset();
+    }
+
+    private MessageContext getMessageContext(String clientId, boolean response,
+                                             List<Endpoint> endpoints) throws 
Exception {
+        MessageContext msgContext = 
TestUtils.createLightweightSynapseMessageContext("<test/>");
+        
msgContext.setProperty(SynapseConstants.PROP_SAL_ENDPOINT_ENDPOINT_LIST, 
endpoints);
+        Member member = new Member("127.0.0.1", 9000);
+        Properties props = new Properties();
+        Map<String,String> hosts = new HashMap<String, String>();
+        hosts.put("127.0.0.1", "foo");
+        props.put(HttpSessionDispatcher.HOSTS, hosts);
+        member.setProperties(props);
+        
msgContext.setProperty(SynapseConstants.PROP_SAL_ENDPOINT_CURRENT_MEMBER, 
member);
+        org.apache.axis2.context.MessageContext axis2MsgContext =
+                ((Axis2MessageContext) msgContext).getAxis2MessageContext();
+        Map<String,String> headers = new HashMap<String, String>();
+        if (response) {
+            headers.put("Set-Cookie", clientId);
+            msgContext.setResponse(true);
+        } else {
+            headers.put("Cookie", clientId);
+            headers.put("Host", "127.0.0.1");
+        }
+        
axis2MsgContext.setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS,
 headers);
+        return msgContext;
+    }
+}

Added: 
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/endpoints/dispatch/SimpleClientSessionDispatcherTest.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/endpoints/dispatch/SimpleClientSessionDispatcherTest.java?rev=1510820&view=auto
==============================================================================
--- 
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/endpoints/dispatch/SimpleClientSessionDispatcherTest.java
 (added)
+++ 
synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/endpoints/dispatch/SimpleClientSessionDispatcherTest.java
 Mon Aug  5 23:53:59 2013
@@ -0,0 +1,84 @@
+/*
+ *  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.synapse.endpoints.dispatch;
+
+import junit.framework.TestCase;
+import org.apache.axis2.context.ConfigurationContext;
+import org.apache.axis2.engine.AxisConfiguration;
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.SynapseConstants;
+import org.apache.synapse.core.axis2.Axis2MessageContext;
+import org.apache.synapse.endpoints.AddressEndpoint;
+import org.apache.synapse.endpoints.Endpoint;
+import org.apache.synapse.mediators.TestUtils;
+
+import javax.xml.namespace.QName;
+import java.util.ArrayList;
+import java.util.List;
+
+public class SimpleClientSessionDispatcherTest extends TestCase {
+
+    private static final QName CLIENT_ID= new 
QName("http://ws.apache.org/ns/synapse";,
+            "ClientID", "syn");
+
+    public void testClientSessionDispatcher() throws Exception {
+        AxisConfiguration axisConfiguration = new AxisConfiguration();
+        ConfigurationContext configContext = new 
ConfigurationContext(axisConfiguration);
+        SALSessions.getInstance().initialize(true, configContext);
+
+        List<Endpoint> endpoints = new ArrayList<Endpoint>();
+        AddressEndpoint endpoint = new AddressEndpoint();
+        endpoint.setName("ep1");
+        endpoints.add(endpoint);
+
+        Dispatcher dispatcher = new SimpleClientSessionDispatcher();
+        assertFalse(dispatcher.isServerInitiatedSession());
+
+        // test session creation
+        String clientId = "client0001";
+        dispatcher.updateSession(getMessageContext(clientId, endpoints));
+        assertNotNull(SALSessions.getInstance().getSession(clientId));
+
+        // test session query
+        SessionInformation session = 
dispatcher.getSession(getMessageContext(clientId,
+                endpoints));
+        assertNotNull(session);
+        assertEquals(session.getEndpointList().get(0).getName(), "ep1");
+
+        // test session removal
+        dispatcher.unbind(getMessageContext(clientId, endpoints));
+        session = dispatcher.getSession(getMessageContext(clientId, 
endpoints));
+        assertNull(session);
+        assertNull(SALSessions.getInstance().getSession(clientId));
+
+        SALSessions.getInstance().reset();
+    }
+
+    private MessageContext getMessageContext(String clientId,
+                                             List<Endpoint> endpoints) throws 
Exception {
+        MessageContext msgContext = 
TestUtils.createLightweightSynapseMessageContext("<test/>");
+        
msgContext.setProperty(SynapseConstants.PROP_SAL_ENDPOINT_ENDPOINT_LIST, 
endpoints);
+        org.apache.axis2.context.MessageContext axis2MsgContext =
+                ((Axis2MessageContext) msgContext).getAxis2MessageContext();
+        TestUtils.addSOAPHeaderBlock(axis2MsgContext, 
SimpleClientSessionDispatcherTest.CLIENT_ID,
+                clientId);
+        return msgContext;
+    }
+}


Reply via email to