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.api;
8   
9   import org.mule.api.processor.MessageProcessorChain;
10  import org.mule.api.processor.policy.AroundPolicy;
11  import org.mule.api.processor.policy.PolicyInvocation;
12  import org.mule.processor.chain.DefaultMessageProcessorChain;
13  import org.mule.tck.junit4.AbstractMuleContextTestCase;
14  import org.mule.transformer.simple.StringAppendTransformer;
15  
16  import org.junit.Test;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertNotNull;
20  import static org.junit.Assert.assertSame;
21  import static org.junit.Assert.fail;
22  
23  public class PolicyTestCase extends AbstractMuleContextTestCase
24  {
25  
26      @Test
27      public void testSinglePolicy() throws Exception
28      {
29          AroundPolicy ap = new TestPolicy("test around policy");
30  
31          // this is our regular chain that should get a policy applied
32          MessageProcessorChain chain = DefaultMessageProcessorChain.from(
33                                                               new StringAppendTransformer("first"),
34                                                               new StringAppendTransformer(" second"));
35          initialiseObject(chain);
36  
37          // test registration
38          assertEquals("No policies should have been registered.", 0, chain.getPolicies().list().size());
39          chain.getPolicies().add(ap);
40          assertSame("Policy has not been registered.", ap, chain.getPolicies().list().iterator().next());
41  
42          System.out.println(chain);
43  
44          // invoke
45          final MuleEvent result = chain.process(getTestEvent("payload "));
46          assertNotNull(result);
47          final MuleMessage message = result.getMessage();
48          assertNotNull(message);
49          assertEquals("payload {before} first second {after}", message.getPayload());
50  
51          // test cleanup
52          final AroundPolicy policy = chain.getPolicies().remove(ap.getName());
53          assertSame("Wrong policy returned?", ap, policy);
54          assertEquals("No policies should have been registered.", 0, chain.getPolicies().list().size());
55      }
56  
57      @Test
58      public void testMultiplePolicies() throws Exception
59      {
60  
61          // this is our regular chain that should get a policy applied
62          MessageProcessorChain chain = DefaultMessageProcessorChain.from(
63                                                              new StringAppendTransformer("first"),
64                                                              new StringAppendTransformer(" second"));
65          initialiseObject(chain);
66  
67          // test registration
68          assertEquals("No policies should have been registered.", 0, chain.getPolicies().list().size());
69          AroundPolicy policy1 = new TestPolicy("test around policy 1");
70          chain.getPolicies().add(policy1);
71          // add another policy
72          final TestPolicy policy2 = new TestPolicy("test around policy 2");
73          chain.getPolicies().add(policy2);
74          assertEquals("Wrong policies count.", 2, chain.getPolicies().list().size());
75  
76          System.out.println(chain);
77  
78          // invoke
79          final MuleEvent result = chain.process(getTestEvent("payload "));
80          assertNotNull(result);
81          final MuleMessage message = result.getMessage();
82          assertNotNull(message);
83          assertEquals("payload {before} {before} first second {after} {after}", message.getPayload());
84  
85          // test cleanup
86          final AroundPolicy policy = chain.getPolicies().remove(policy1.getName());
87          assertSame("Wrong policy returned?", policy1, policy);
88          chain.getPolicies().remove(policy2.getName());
89          assertEquals("No policies should have been registered.", 0, chain.getPolicies().list().size());
90      }
91  
92      @Test
93      public void testDuplicateName() throws Exception
94      {
95          MessageProcessorChain chain = DefaultMessageProcessorChain.from();
96          chain.getPolicies().add(new TestPolicy("test"));
97          try
98          {
99              chain.getPolicies().add(new TestPolicy("test"));
100             fail("Should've thrown an exception, no duplicates allowed");
101         }
102         catch (IllegalArgumentException e)
103         {
104             System.out.println(e);
105             // expected
106         }
107     }
108 
109     private static class TestPolicy implements AroundPolicy
110     {
111 
112         private String name;
113 
114         public TestPolicy(final String name)
115         {
116             this.name = name;
117         }
118 
119         public MuleEvent invoke(PolicyInvocation invocation) throws MuleException
120         {
121             // mutates the event directly, thus we can safely ignore the return object
122             new StringAppendTransformer("{before} ").process(invocation.getEvent());
123             final MuleEvent result = invocation.proceed();
124             //throw new DefaultMuleException("test");
125             new StringAppendTransformer(" {after}").process(invocation.getEvent());
126             return result;
127         }
128 
129         public String getName()
130         {
131             return name;
132         }
133     }
134 }