View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.session;
8   
9   import static org.junit.Assert.assertEquals;
10  import static org.junit.Assert.assertNotNull;
11  import static org.junit.Assert.assertNotSame;
12  import static org.junit.Assert.assertNull;
13  
14  import org.mule.DefaultMuleEvent;
15  import org.mule.DefaultMuleMessage;
16  import org.mule.RequestContext;
17  import org.mule.api.MuleEvent;
18  import org.mule.api.MuleMessage;
19  import org.mule.api.endpoint.OutboundEndpoint;
20  import org.mule.api.transport.MessageDispatcher;
21  import org.mule.api.transport.PropertyScope;
22  import org.mule.construct.SimpleFlowConstruct;
23  import org.mule.tck.functional.FlowAssert;
24  import org.mule.transport.vm.VMMessageDispatcher;
25  
26  import org.junit.After;
27  import org.junit.Ignore;
28  import org.junit.Test;
29  
30  public class SessionPropertiesTestCase extends org.mule.tck.junit4.FunctionalTestCase
31  {
32  
33      @After
34      public void clearFlowAssertions()
35      {
36          FlowAssert.reset();
37      }
38  
39      @Test
40      public void setSessionPropertyUsingAPIGetInFlow() throws Exception
41      {
42          MuleMessage message = new DefaultMuleMessage("data", muleContext);
43          MuleEvent event = new DefaultMuleEvent(message, getTestInboundEndpoint(""), getTestSession(
44              getTestService(), muleContext));
45  
46          RequestContext.setEvent(event);
47          message.setProperty("key", "value", PropertyScope.SESSION);
48  
49          SimpleFlowConstruct flowA = (SimpleFlowConstruct) muleContext.getRegistry().lookupFlowConstruct("A");
50          MuleEvent result = flowA.process(event);
51  
52          assertEquals("value", result.getMessageAsString());
53      }
54  
55      @Test
56      public void setSessionPropertyInFlowGetUsingAPI() throws Exception
57      {
58          MuleMessage message = new DefaultMuleMessage("data", muleContext);
59          MuleEvent event = new DefaultMuleEvent(message, getTestInboundEndpoint(""), getTestSession(
60              getTestService(), muleContext));
61  
62          SimpleFlowConstruct flowA = (SimpleFlowConstruct) muleContext.getRegistry().lookupFlowConstruct("B");
63          MuleEvent result = flowA.process(event);
64  
65          assertEquals("value", result.getMessage().getProperty("key", PropertyScope.SESSION));
66      }
67  
68      @Test
69      @Ignore
70      public void propagateSessionPropertyOverTransportRequestResponse() throws Exception
71      {
72          MuleMessage message = new DefaultMuleMessage("data", muleContext);
73          MuleEvent event = new DefaultMuleEvent(message, getTestInboundEndpoint(""), getTestSession(
74              getTestService(), muleContext));
75  
76          RequestContext.setEvent(event);
77          Object nonSerializable = new Object();
78          message.setProperty("key", "value", PropertyScope.SESSION);
79          message.setProperty("keyNonSerializable", nonSerializable, PropertyScope.SESSION);
80  
81          SimpleFlowConstruct flowA = (SimpleFlowConstruct) muleContext.getRegistry().lookupFlowConstruct(
82              "RequestResponseSessionPropertySettingChain");
83          MuleEvent result = flowA.process(event);
84  
85          assertEquals("value", result.getMessage().getProperty("key", PropertyScope.SESSION));
86          assertEquals("value1", result.getMessage().getProperty("key1", PropertyScope.SESSION));
87          assertEquals("value2", result.getMessage().getProperty("key2", PropertyScope.SESSION));
88          assertEquals("value3", result.getMessage().getProperty("key3", PropertyScope.SESSION));
89          assertEquals("value4", result.getMessage().getProperty("key4", PropertyScope.SESSION));
90          assertEquals("value5", result.getMessage().getProperty("key5", PropertyScope.SESSION));
91          assertEquals(nonSerializable,
92              result.getMessage().getProperty("keyNonSerializable", PropertyScope.SESSION));
93      }
94  
95      @Test
96      @Ignore
97      public void propagateSessionPropertyOverTransportOneWay() throws Exception
98      {
99          MuleMessage message = new DefaultMuleMessage("data", muleContext);
100         MuleEvent event = new DefaultMuleEvent(message, getTestInboundEndpoint(""), getTestSession(
101             getTestService(), muleContext));
102 
103         RequestContext.setEvent(event);
104         Object nonSerializable = new Object();
105         message.setProperty("key", "value", PropertyScope.SESSION);
106         message.setProperty("keyNonSerializable", nonSerializable, PropertyScope.SESSION);
107 
108         SimpleFlowConstruct flowA = (SimpleFlowConstruct) muleContext.getRegistry().lookupFlowConstruct(
109             "OneWaySessionPropertySettingChain");
110         flowA.process(event);
111 
112         MuleMessage out = muleContext.getClient()
113             .request("vm://H-out?connector=VMConnector", RECEIVE_TIMEOUT);
114 
115         assertNotNull(out);
116         assertEquals("value", out.getProperty("key", PropertyScope.SESSION));
117         assertEquals("value1", out.getProperty("key1", PropertyScope.SESSION));
118         assertEquals("value2", out.getProperty("key2", PropertyScope.SESSION));
119         assertNull(out.getProperty("keyNonSerializable", PropertyScope.SESSION));
120     }
121 
122     @Test
123     @Ignore
124     public void nonSerializableSessionPropertyOneWayFlow() throws Exception
125     {
126         MuleMessage message = new DefaultMuleMessage("data", muleContext);
127         MuleEvent event = new DefaultMuleEvent(message, getTestInboundEndpoint(""), getTestSession(
128             getTestService(), muleContext));
129 
130         RequestContext.setEvent(event);
131         Object nonSerializable = new Object();
132         message.setProperty("keyNonSerializable", nonSerializable, PropertyScope.SESSION);
133 
134         SimpleFlowConstruct flow = (SimpleFlowConstruct) muleContext.getRegistry().lookupFlowConstruct(
135             "PassthroughFlow");
136         flow.process(event);
137 
138         MuleMessage out = muleContext.getClient().request("vm://PassthroughFlow-out?connector=VMConnector",
139             RECEIVE_TIMEOUT);
140 
141         assertNotNull(out);
142         // Mule 3.1 uses async without queues, so there are no issues with
143         // non-serializable
144         assertNotNull(out.getProperty("keyNonSerializable", PropertyScope.SESSION));
145     }
146 
147     /**
148      * When invoking a Flow directly session properties are preserved
149      */
150     @Test
151     public void flowRefSessionPropertyPropagation() throws Exception
152     {
153 
154         MuleMessage message = new DefaultMuleMessage("data", muleContext);
155         MuleEvent event = new DefaultMuleEvent(message, getTestInboundEndpoint(""), getTestSession(
156             getTestService(), muleContext));
157 
158         RequestContext.setEvent(event);
159         Object nonSerializable = new Object();
160         message.setProperty("keyNonSerializable", nonSerializable, PropertyScope.SESSION);
161         message.setProperty("key", "value", PropertyScope.SESSION);
162 
163         SimpleFlowConstruct flow = (SimpleFlowConstruct) muleContext.getRegistry().lookupFlowConstruct(
164             "FlowRefWithSessionProperties");
165         MuleEvent result = flow.process(event);
166 
167         assertNotSame(event.getSession(), result.getSession());
168 
169         assertNotNull(result);
170         assertEquals("value", result.getMessage().getProperty("key", PropertyScope.SESSION));
171         assertEquals("value1", result.getMessage().getProperty("key1", PropertyScope.SESSION));
172         assertEquals("value2", result.getMessage().getProperty("key2", PropertyScope.SESSION));
173         assertEquals(nonSerializable,
174             result.getMessage().getProperty("keyNonSerializable", PropertyScope.SESSION));
175 
176     }
177 
178     @Test
179     public void outboundEndpointSessionMerge() throws Exception
180     {
181         MuleMessage message = new DefaultMuleMessage("data", muleContext);
182         MuleEvent event = new DefaultMuleEvent(message, muleContext.getEndpointFactory().getOutboundEndpoint(
183             "addSessionPropertiesFlowEndpoint"), getTestSession(getTestService(), muleContext));
184 
185         Object nonSerializable = new Object();
186         event.getSession().setProperty("keyNonSerializable", nonSerializable);
187         event.getSession().setProperty("keyNonSerializable2", nonSerializable);
188         event.getSession().setProperty("key", "value");
189         event.getSession().setProperty("key2", "value2");
190 
191         MessageDispatcher dispatcher = new VMMessageDispatcher((OutboundEndpoint) event.getEndpoint());
192         MuleEvent result = dispatcher.process(event);
193 
194         assertNotNull(result);
195         assertNotSame(event, result);
196         assertEquals("val", result.getSession().getProperty("keyNonSerializable"));
197         assertEquals(nonSerializable, result.getSession().getProperty("keyNonSerializable2"));
198         assertEquals("value2NEW", result.getSession().getProperty("key2"));
199         assertEquals("value3", result.getSession().getProperty("key3"));
200         assertNull(result.getSession().getProperty("nonSerializableBean"));
201     }
202 
203     @Test
204     public void requestReplySessionMerge() throws Exception
205     {
206         MuleMessage message = new DefaultMuleMessage("data", muleContext);
207         MuleEvent event = new DefaultMuleEvent(message, getTestInboundEndpoint(""), getTestSession(
208             getTestService(), muleContext));
209 
210         Object nonSerializable = new Object();
211         event.getSession().setProperty("keyNonSerializable", nonSerializable);
212         event.getSession().setProperty("keyNonSerializable2", nonSerializable);
213         event.getSession().setProperty("key", "value");
214         event.getSession().setProperty("key2", "value2");
215 
216         MuleEvent result = ((SimpleFlowConstruct) muleContext.getRegistry().lookupFlowConstruct(
217             "requestResponseFlow")).process(event);
218 
219         assertNotNull(result);
220         assertNotSame(event, result);
221         assertEquals("val", result.getSession().getProperty("keyNonSerializable"));
222         assertEquals(nonSerializable, result.getSession().getProperty("keyNonSerializable2"));
223         assertEquals("value2NEW", result.getSession().getProperty("key2"));
224         assertEquals("value3", result.getSession().getProperty("key3"));
225         assertNull(result.getSession().getProperty("nonSerializableBean"));
226     }
227 
228     @Test
229     public void requestReplyNoSessionPropagationSessionMerge() throws Exception
230     {
231         MuleMessage message = new DefaultMuleMessage("data", muleContext);
232         MuleEvent event = new DefaultMuleEvent(message, getTestInboundEndpoint(""), getTestSession(
233             getTestService(), muleContext));
234 
235         Object nonSerializable = new Object();
236         event.getSession().setProperty("keyNonSerializable", nonSerializable);
237         event.getSession().setProperty("keyNonSerializable2", nonSerializable);
238         event.getSession().setProperty("key", "value");
239         event.getSession().setProperty("key2", "value2");
240 
241         MuleEvent result = ((SimpleFlowConstruct) muleContext.getRegistry().lookupFlowConstruct(
242             "requestResponseNoSessionPropagationFlow")).process(event);
243 
244         assertNotNull(result);
245         assertNotSame(event, result);
246         assertEquals(nonSerializable, result.getSession().getProperty("keyNonSerializable"));
247         assertEquals(nonSerializable, result.getSession().getProperty("keyNonSerializable2"));
248         assertEquals("value", result.getSession().getProperty("key"));
249         assertEquals("value2", result.getSession().getProperty("key2"));
250         assertNull(result.getSession().getProperty("nonSerializableBean"));
251     }
252 
253     @Test
254     public void defaultExceptionStrategy() throws Exception
255     {
256         testFlow("defaultExceptionStrategy");
257     }
258 
259     @Override
260     protected String getConfigResources()
261     {
262         return "org/mule/session/session-properties-config.xml";
263     }
264 
265 }