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.expression;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.RequestContext;
11  import org.mule.api.MuleEvent;
12  import org.mule.api.MuleException;
13  import org.mule.api.MuleMessage;
14  import org.mule.api.expression.ExpressionManager;
15  import org.mule.api.expression.ExpressionRuntimeException;
16  import org.mule.api.processor.MessageProcessor;
17  import org.mule.api.transport.PropertyScope;
18  import org.mule.tck.junit4.AbstractMuleContextTestCase;
19  
20  import org.junit.Test;
21  
22  import static org.junit.Assert.assertEquals;
23  
24  public class MessageProcessorExpressionEvaluatorTestCase extends AbstractMuleContextTestCase
25  {
26  
27      private ExpressionManager expressionManager;
28  
29      @Override
30      protected void doSetUp() throws Exception
31      {
32          super.doSetUp();
33          expressionManager = muleContext.getExpressionManager();
34          RequestContext.setEvent(getTestEvent(""));
35  
36          muleContext.getRegistry().registerObject("processor", new MessageProcessor()
37          {
38              public MuleEvent process(MuleEvent event) throws MuleException
39              {
40                  event.getMessage().setPayload(event.getMessageAsString() + "123");
41                  return event;
42              }
43          });
44      }
45  
46      @Test
47      public void testNameOnly() throws Exception
48      {
49          MessageProcessorExpressionEvaluator evaluator = new MessageProcessorExpressionEvaluator();
50          assertEquals("0123",
51              ((MuleMessage) evaluator.evaluate("processor", createTestMessage())).getPayloadAsString());
52      }
53  
54      @Test
55      public void testNameOnlyExpressionManager() throws ExpressionRuntimeException, Exception
56      {
57          assertEquals("0123", ((MuleMessage) expressionManager.evaluate("#[process:processor]",
58              createTestMessage())).getPayloadAsString());
59      }
60  
61      @Test
62      public void testNestedPayloadExpression() throws Exception
63      {
64          MessageProcessorExpressionEvaluator evaluator = new MessageProcessorExpressionEvaluator();
65          assertEquals("0123",
66              ((MuleMessage) evaluator.evaluate("processor:payload", createTestMessage())).getPayloadAsString());
67      }
68  
69      @Test
70      public void testNestedPayloadExpressionExpressionManager() throws ExpressionRuntimeException, Exception
71      {
72          assertEquals("0123", ((MuleMessage) expressionManager.evaluate("#[process:processor:#[payload]]",
73              createTestMessage())).getPayloadAsString());
74      }
75  
76      @Test
77      public void testNestedHeaderExpression() throws Exception
78      {
79          MessageProcessorExpressionEvaluator evaluator = new MessageProcessorExpressionEvaluator();
80          assertEquals("value123", ((MuleMessage) evaluator.evaluate("processor:header:one",
81              createTestMessage())).getPayloadAsString());
82      }
83  
84      @Test
85      public void testNestedHeaderExpressionExpressionManager() throws ExpressionRuntimeException, Exception
86      {
87          assertEquals("value123", ((MuleMessage) expressionManager.evaluate(
88              "#[process:processor:#[header:one]]", createTestMessage())).getPayloadAsString());
89      }
90  
91      private MuleMessage createTestMessage()
92      {
93          MuleMessage message = new DefaultMuleMessage("0", muleContext);
94          message.setProperty("one", "value", PropertyScope.OUTBOUND);
95          return message;
96      }
97  
98  }