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.transformers.simple;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleMessage;
11  import org.mule.api.transport.PropertyScope;
12  import org.mule.tck.junit4.AbstractMuleContextTestCase;
13  import org.mule.transformer.simple.MessagePropertiesTransformer;
14  
15  import java.util.Collections;
16  import java.util.HashMap;
17  import java.util.Map;
18  
19  import org.junit.Test;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertNull;
23  
24  public class MessagePropertiesTransformerScopesTestCase extends AbstractMuleContextTestCase
25  {
26  
27      @Test
28      public void testPropertyScopes() throws Exception
29      {
30          MuleMessage msg = new DefaultMuleMessage("message", muleContext);
31          
32          // Add properties to scope
33          
34          MessagePropertiesTransformer add = new MessagePropertiesTransformer();
35          add.setScope(PropertyScope.INVOCATION);
36          Map<String, Object> addProps = new HashMap<String, Object>();
37          addProps.put("foo", "bar");
38          addProps.put("foo2", "baz");
39          add.setAddProperties(addProps);
40          add.setMuleContext(muleContext);
41          add.initialise();
42  
43          msg = (DefaultMuleMessage) add.transform(msg, (String)null);
44  
45          assertEquals("bar", msg.getInvocationProperty("foo"));
46          assertNull(msg.getOutboundProperty("foo"));
47          assertNull(msg.getSessionProperty("foo"));
48  
49          // Remove property from the wrong scope
50          
51          MessagePropertiesTransformer deleteWrongScope = new MessagePropertiesTransformer();
52          deleteWrongScope.setScope(PropertyScope.OUTBOUND);
53          deleteWrongScope.setDeleteProperties("foo");
54          deleteWrongScope.setMuleContext(muleContext);
55          deleteWrongScope.initialise();
56  
57          msg = (DefaultMuleMessage) deleteWrongScope.transform(msg, (String)null);
58          assertEquals("bar", msg.getInvocationProperty("foo"));
59  
60          // Remove property from the correct scope
61          
62          MessagePropertiesTransformer delete = new MessagePropertiesTransformer();
63          delete.setScope(PropertyScope.INVOCATION);
64          delete.setDeleteProperties(Collections.singletonList("foo"));
65          delete.setMuleContext(muleContext);
66          delete.initialise();
67  
68          msg = (DefaultMuleMessage) delete.transform(msg, (String)null);
69          assertNull(msg.getInvocationProperty("foo"));
70          assertEquals("baz", msg.getInvocationProperty("foo2"));
71      }
72  }