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.api.expression.RequiredValueException;
10  
11  import org.junit.Test;
12  
13  import static org.junit.Assert.assertNull;
14  
15  public class MessageAttachmentExpressionEvaluatorTestCase extends AbstractAttachmentsTestCase
16  {
17      private MessageAttachmentExpressionEvaluator evaluator = new MessageAttachmentExpressionEvaluator();
18  
19      @Test
20      public void requiredKeyWithExistingAttachmentShouldReturnAttachment() throws Exception
21      {
22          Object result = evaluator.evaluate("foo", message);
23          assertAttachmentValueEquals("foovalue", result);
24      }
25  
26      @Test
27      public void requiredKeyWithExistingAttachmentViaExpressionManagerShouldReturnAttachment() throws Exception
28      {
29          Object result = muleContext.getExpressionManager().evaluate("#[attachment:foo]", message);
30          assertAttachmentValueEquals("foovalue", result);
31      }
32  
33      @Test(expected = RequiredValueException.class)
34      public void requiredKeyWithMissingAttachmentShouldFail()
35      {
36          evaluator.evaluate("nonexistent", message);
37      }
38  
39      @Test(expected = RequiredValueException.class)
40      public void requiredKeyWithMissingAttachmentViaExpressionManagerShouldFail()
41      {
42          muleContext.getExpressionManager().evaluate("#[attachment:nonexistent]", message);
43      }
44  
45      @Test
46      public void optionalKeyWithExistingValueShouldReturnAttachment() throws Exception
47      {
48          Object result = evaluator.evaluate("foo?", message);
49          assertAttachmentValueEquals("foovalue", result);
50      }
51  
52      @Test
53      public void optionalKeyWithExistingValueViaExpressionManagerShouldReturnAttachment() throws Exception
54      {
55          Object result = muleContext.getExpressionManager().evaluate("#[attachment:foo?]", message);
56          assertAttachmentValueEquals("foovalue", result);
57      }
58  
59      @Test
60      public void optionalKeyWithMissingValueShouldReturnNull()
61      {
62          Object result = evaluator.evaluate("nonexistent?", message);
63          assertNull(result);
64      }
65  
66      @Test
67      public void optionalKeyWithMissingValueViaExpressionManagerShouldReturnNull()
68      {
69          Object result = muleContext.getExpressionManager().evaluate("#[attachment:nonexistent?]", message);
70          assertNull(result);
71      }
72  }