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.assertFalse;
11  import static org.junit.Assert.assertNotNull;
12  import static org.junit.Assert.assertNull;
13  import static org.junit.Assert.assertSame;
14  import static org.junit.Assert.assertTrue;
15  import static org.junit.Assert.fail;
16  
17  import org.mule.api.MuleContext;
18  import org.mule.api.MuleException;
19  import org.mule.api.construct.FlowConstruct;
20  import org.mule.api.registry.MuleRegistry;
21  import org.mule.api.security.Authentication;
22  import org.mule.api.security.SecurityContext;
23  import org.mule.construct.SimpleFlowConstruct;
24  import org.mule.security.DefaultMuleAuthentication;
25  import org.mule.security.DefaultSecurityContextFactory;
26  import org.mule.security.MuleCredentials;
27  import org.mule.util.SerializationUtils;
28  
29  import java.util.Collections;
30  
31  import org.junit.Test;
32  import org.mockito.Mockito;
33  
34  public class DefaultMuleSessionTestCase
35  {
36  
37      @Test
38      public void create()
39      {
40          DefaultMuleSession session = new DefaultMuleSession(Mockito.mock(MuleContext.class));
41          assertCreate(session);
42          assertNull(session.getFlowConstruct());
43      }
44  
45      @Test
46      public void createWithFlowConstuct()
47      {
48          FlowConstruct flowConstruct = Mockito.mock(FlowConstruct.class);
49          DefaultMuleSession session = new DefaultMuleSession(flowConstruct, Mockito.mock(MuleContext.class));
50          assertCreate(session);
51          assertSame(flowConstruct, session.getFlowConstruct());
52      }
53  
54      protected void assertCreate(DefaultMuleSession session)
55      {
56          assertNotNull(session.getId());
57          assertNull(session.getSecurityContext());
58          assertNotNull(session.getPropertyNamesAsSet());
59          assertTrue(session.getPropertyNamesAsSet().isEmpty());
60          assertTrue(session.isValid());
61      }
62  
63      @Test
64      public void copy()
65          throws IllegalArgumentException, SecurityException, IllegalAccessException, NoSuchFieldException
66      {
67          FlowConstruct flowConstruct = Mockito.mock(FlowConstruct.class);
68          DefaultMuleSession original = new DefaultMuleSession(flowConstruct, Mockito.mock(MuleContext.class));
69          original.setValid(false);
70          original.setSecurityContext(Mockito.mock(SecurityContext.class));
71          original.setProperty("foo", "bar");
72  
73          DefaultMuleSession copy = new DefaultMuleSession(original, Mockito.mock(MuleContext.class));
74  
75          assertCopy(original, copy);
76  
77          assertSame(copy.getFlowConstruct(), original.getFlowConstruct());
78  
79          // properties are copied but a new map instance is used
80          assertSame(original.getProperty("foo"), copy.getProperty("foo"));
81          copy.setProperty("new", "bar");
82          assertNull(original.getProperty("new"));
83      }
84  
85      @Test
86      public void copyWithFlowConstruct()
87          throws IllegalArgumentException, SecurityException, IllegalAccessException, NoSuchFieldException
88      {
89          FlowConstruct originalFlow = Mockito.mock(FlowConstruct.class);
90          FlowConstruct newFlow = Mockito.mock(FlowConstruct.class);
91          DefaultMuleSession original = new DefaultMuleSession(originalFlow, Mockito.mock(MuleContext.class));
92          original.setValid(false);
93          original.setSecurityContext(Mockito.mock(SecurityContext.class));
94          original.setProperty("foo", "bar");
95  
96          DefaultMuleSession copy = new DefaultMuleSession(original, newFlow);
97  
98          assertCopy(original, copy);
99  
100         assertSame(copy.getFlowConstruct(), newFlow);
101 
102         // properties map is copied
103         assertSame(original.getProperty("foo"), copy.getProperty("foo"));
104         copy.setProperty("new", "bar");
105         assertNotNull(original.getProperty("new"));
106     }
107 
108     protected void assertCopy(DefaultMuleSession original, DefaultMuleSession copy)
109     {
110         assertSame(copy.getId(), original.getId());
111         assertSame(copy.isValid(), original.isValid());
112         assertSame(copy.getSecurityContext(), original.getSecurityContext());
113     }
114 
115     @Test
116     public void valid()
117     {
118         DefaultMuleSession session = new DefaultMuleSession(Mockito.mock(MuleContext.class));
119         assertTrue(session.isValid());
120         session.setValid(false);
121         assertFalse(session.isValid());
122         session.setValid(true);
123         assertTrue(session.isValid());
124     }
125 
126     @Test
127     public void propertiesCaseInsensitive()
128     {
129         DefaultMuleSession session = new DefaultMuleSession(Mockito.mock(MuleContext.class));
130         session.setProperty("key1", "value1");
131         assertSame("value1", session.getProperty("key1"));
132 
133         // properties are case-insenstive
134         session.setProperty("KEY1", "value2");
135         assertSame("value2", session.getProperty("key1"));
136     }
137 
138     @Test
139     public void propertiesCaseInsensitiveAfterCopy()
140     {
141         DefaultMuleSession original = new DefaultMuleSession(Mockito.mock(MuleContext.class));
142         DefaultMuleSession copy = new DefaultMuleSession(original, Mockito.mock(MuleContext.class));
143 
144         copy.setProperty("key1", "value1");
145         assertSame("value1", copy.getProperty("key1"));
146 
147         // properties are case-insenstive
148         copy.setProperty("KEY1", "value2");
149         assertSame("value2", copy.getProperty("key1"));
150     }
151 
152     @Test
153     public void merge()
154     {
155         DefaultMuleSession copy1 = new DefaultMuleSession(Mockito.mock(MuleContext.class));
156         DefaultMuleSession copy2 = new DefaultMuleSession(Mockito.mock(MuleContext.class));
157 
158         Object nonSerializableValue2 = new Object();
159         Object nonSerializableValue3 = new Object();
160 
161         copy1.setProperty("key1", "value1");
162         copy1.setProperty("key2", nonSerializableValue2);
163         copy1.setProperty("key3", nonSerializableValue3);
164         copy1.setProperty("key4", "value4");
165         copy1.setProperty("key5", "value5");
166         copy1.setProperty("key6", "value6");
167 
168         copy2.setProperty("key1", "value1");
169         copy2.setProperty("key2", "value2");
170         copy2.setProperty("KEY4", "value4");
171         copy2.setProperty("KEY5", "value5NEW");
172         copy2.setProperty("key7", "value7");
173 
174         int copy2PropertiesHashCode = copy2.getPropertyNamesAsSet().hashCode();
175 
176         copy1.merge(copy2);
177 
178         assertEquals(6, copy1.getPropertyNamesAsSet().size());
179         assertEquals("value1", copy1.getProperty("key1"));
180         assertEquals("value2", copy1.getProperty("key2"));
181         assertEquals(nonSerializableValue3, copy1.getProperty("key3"));
182         assertEquals("value4", copy1.getProperty("key4"));
183         assertEquals("value5NEW", copy1.getProperty("key5"));
184         assertNull(copy1.getProperty("key6"));
185         assertEquals("value7", copy1.getProperty("key7"));
186 
187         assertEquals(5, copy2.getPropertyNamesAsSet().size());
188         assertEquals(copy2PropertiesHashCode, copy2.getPropertyNamesAsSet().hashCode());
189     }
190 
191     @Test
192     public void serialization() throws MuleException
193     {
194         SimpleFlowConstruct flow = new SimpleFlowConstruct("flow", Mockito.mock(MuleContext.class));
195         DefaultMuleSession before = new DefaultMuleSession(flow, Mockito.mock(MuleContext.class));
196         before.setValid(false);
197         before.setSecurityContext(createTestAuthentication());
198         before.setProperty("foo", "bar");
199 
200         // Create mock muleContext
201         MuleContext muleContext = Mockito.mock(MuleContext.class);
202         MuleRegistry registry = Mockito.mock(MuleRegistry.class);
203         Mockito.when(muleContext.getRegistry()).thenReturn(registry);
204         Mockito.when(muleContext.getExecutionClassLoader()).thenReturn(getClass().getClassLoader());
205         Mockito.when(registry.lookupFlowConstruct("flow")).thenReturn(flow);
206 
207         // Serialize and then deserialize
208         DefaultMuleSession after = (DefaultMuleSession) SerializationUtils.deserialize(
209             SerializationUtils.serialize(before), muleContext);
210 
211         // assertions
212         assertEquals(before.getId(), after.getId());
213         assertEquals(before.isValid(), after.isValid());
214         assertEquals(before.getFlowConstruct(), after.getFlowConstruct());
215         assertEquals(before.getProperty("foo"), after.getProperty("foo"));
216         assertEquals(before.getSecurityContext().getAuthentication().getPrincipal(),
217             after.getSecurityContext().getAuthentication().getPrincipal());
218         assertEquals(before.getSecurityContext().getAuthentication().getProperties().get("key1"),
219             after.getSecurityContext().getAuthentication().getProperties().get("key1"));
220         assertEquals(before.getSecurityContext().getAuthentication().getCredentials(),
221             after.getSecurityContext().getAuthentication().getCredentials());
222         // assertEquals(before.getSecurityContext().getAuthentication().getEvent().getId(),
223         // after.getSecurityContext().getAuthentication().getEvent().getId());
224 
225         after.setProperty("new", "value");
226         assertNull(before.getProperty("new"));
227 
228     }
229 
230     @Test
231     @SuppressWarnings(value = {"deprecation"})
232     public void serializationWithNonSerializableProperty() throws MuleException
233     {
234         DefaultMuleSession before = new DefaultMuleSession(Mockito.mock(MuleContext.class));
235         before.setProperty("foo", new Object());
236 
237         try
238         {
239             // Serialize and then deserialize
240             SerializationUtils.deserialize(SerializationUtils.serialize(before), getClass().getClassLoader());
241 
242             fail("Exception expected");
243         }
244         catch (RuntimeException e)
245         {
246         }
247     }
248 
249     private SecurityContext createTestAuthentication()
250     {
251         Authentication auth = new DefaultMuleAuthentication(new MuleCredentials("dan", new char[]{'d', 'f'}));
252         auth.setProperties(Collections.singletonMap("key1", "value1"));
253         SecurityContext securityContext = new DefaultSecurityContextFactory().create(auth);
254         return securityContext;
255     }
256 }