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  import org.mule.util.ArrayUtils;
11  
12  import java.io.IOException;
13  import java.util.List;
14  
15  import javax.activation.DataHandler;
16  
17  import org.junit.Test;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.assertTrue;
22  
23  public class MessageAttachmentsListExpressionEvaluatorTestCase extends AbstractAttachmentsTestCase
24  {
25      private MessageAttachmentsListExpressionEvaluator evaluator = new MessageAttachmentsListExpressionEvaluator();
26  
27      @Test
28      public void requiredKeysWithExistingAttachmentsShouldReturnAttachments() throws Exception
29      {
30          Object result = evaluator.evaluate("foo, baz", message);
31          assertTrue(result instanceof List);
32  
33          List<?> list = (List<?>) result;
34          assertEquals(2, list.size());
35          assertAttachmentAtIndexHasValue(0, "foovalue", list);
36          assertAttachmentAtIndexHasValue(1, "bazvalue", list);
37      }
38  
39      @Test
40      public void requiredKeysWithExistingAttachmentsViaExpressionManagerShouldReturnAttachments() throws Exception
41      {
42          Object result = muleContext.getExpressionManager().evaluate("#[attachments-list:foo, baz]", message);
43          assertTrue(result instanceof List);
44  
45          List<?> list = (List<?>) result;
46          assertEquals(2, list.size());
47          assertAttachmentAtIndexHasValue(0, "foovalue", list);
48          assertAttachmentAtIndexHasValue(1, "bazvalue", list);
49      }
50  
51      @Test(expected = RequiredValueException.class)
52      public void requiredKeysWithMissingAttachmentsShouldFail() throws Exception
53      {
54          evaluator.evaluate("nonexistent", message);
55      }
56  
57      @Test(expected = RequiredValueException.class)
58      public void requiredKeysWithMissingAttachmentsViaExpressionManagerShouldFail() throws Exception
59      {
60          muleContext.getExpressionManager().evaluate("#[attachments-list:nonexistent]", message);
61      }
62  
63      @Test
64      public void optionalKeysWithExistingAttachmentsShouldReturnAttachments() throws Exception
65      {
66          Object result = evaluator.evaluate("foo?, bar?", message);
67          assertTrue(result instanceof List);
68  
69          List<?> list = (List<?>) result;
70          assertEquals(2, list.size());
71          assertAttachmentAtIndexHasValue(0, "foovalue", list);
72          assertAttachmentAtIndexHasValue(1, "barvalue", list);
73      }
74  
75      @Test
76      public void optionalKeysWithExistingAttachmentsViaExpressionManagerShouldReturnAttachments() throws Exception
77      {
78          Object result = muleContext.getExpressionManager().evaluate("#[attachments-list:foo?, bar?]", message);
79          assertTrue(result instanceof List);
80  
81          List<?> list = (List<?>) result;
82          assertEquals(2, list.size());
83          assertAttachmentAtIndexHasValue(0, "foovalue", list);
84          assertAttachmentAtIndexHasValue(1, "barvalue", list);
85      }
86  
87      @Test
88      public void optionalKeysWithMissingAttachmentsShouldReturnEmptyList() throws Exception
89      {
90          Object result = evaluator.evaluate("nonexistent?", message);
91          assertTrue(result instanceof List);
92  
93          List<?> list = (List<?>) result;
94          assertEquals(0, list.size());
95      }
96  
97      @Test
98      public void optionalKeysWithMissingAttachmentsViaExpressionManagerShouldReturnEmptyList() throws Exception
99      {
100         Object result = muleContext.getExpressionManager().evaluate("#[attachments-list:nonexistent?]", message);
101         assertTrue(result instanceof List);
102 
103         List<?> list = (List<?>) result;
104         assertEquals(0, list.size());
105     }
106 
107     @Test
108     public void matchAllWildcardShouldReturnAllAttachments() throws Exception
109     {
110         Object result = evaluator.evaluate("*", message);
111         assertTrue(result instanceof List);
112 
113         List<?> list = (List<?>) result;
114         assertEquals(3, list.size());
115         assertAttachmentsContain(list, "foovalue", "bazvalue", "barvalue");
116     }
117 
118     @Test
119     public void matchAllWildcardViaExpressionManagerShouldReturnAllAttachments() throws Exception
120     {
121         Object result = muleContext.getExpressionManager().evaluate("#[attachments-list:*]", message);
122         assertTrue(result instanceof List);
123 
124         List<?> list = (List<?>) result;
125         assertEquals(3, list.size());
126         assertAttachmentsContain(list, "foovalue", "bazvalue", "barvalue");
127     }
128 
129     @Test
130     public void matchBeginningWildcardShouldReturnAttachments() throws Exception
131     {
132         Object result = evaluator.evaluate("ba*", message);
133         assertTrue(result instanceof List);
134 
135         List<?> list = (List<?>) result;
136         assertEquals(2, list.size());
137         assertAttachmentsContain(list, "bazvalue", "barvalue");
138     }
139 
140     @Test
141     public void matchBeginningWildcardViaExpressionManagerShouldReturnAttachments() throws Exception
142     {
143         Object result = muleContext.getExpressionManager().evaluate("#[attachments-list:ba*]", message);
144         assertTrue(result instanceof List);
145 
146         List<?> list = (List<?>) result;
147         assertEquals(2, list.size());
148         assertAttachmentsContain(list, "bazvalue", "barvalue");
149     }
150 
151     @Test
152     public void wildcardWithNoMatchShouldReturnEmptyList()
153     {
154         Object result = evaluator.evaluate("x*", message);
155         assertTrue(result instanceof List);
156 
157         List<?> list = (List<?>) result;
158         assertEquals(0, list.size());
159     }
160 
161     @Test
162     public void wildcardWithNoMatchViaExpressionManagerShouldReturnEmptyList()
163     {
164         Object result = muleContext.getExpressionManager().evaluate("#[attachments-list:x*]", message);
165         assertTrue(result instanceof List);
166 
167         List<?> list = (List<?>) result;
168         assertEquals(0, list.size());
169     }
170 
171     @Test
172     public void multipleWildcardsShouldReturnValues() throws Exception
173     {
174         Object result = evaluator.evaluate("ba*, f*", message);
175         assertTrue(result instanceof List);
176 
177         List<?> list = (List<?>) result;
178         assertEquals(3, list.size());
179         assertAttachmentsContain(list, "foovalue", "bazvalue", "barvalue");
180     }
181 
182     @Test
183     public void multipleWildcardsViaExpressionManagerShouldReturnValues() throws Exception
184     {
185         Object result = muleContext.getExpressionManager().evaluate("#[attachments-list:ba*, f*]", message);
186         assertTrue(result instanceof List);
187 
188         List<?> list = (List<?>) result;
189         assertEquals(3, list.size());
190         assertAttachmentsContain(list, "foovalue", "bazvalue", "barvalue");
191     }
192 
193     private void assertAttachmentAtIndexHasValue(int index, String expectedValue, List<?> list) throws IOException
194     {
195         Object attachment = list.get(index);
196         assertNotNull(attachment);
197         assertAttachmentValueEquals(expectedValue, attachment);
198     }
199 
200     private void assertAttachmentsContain(List<?> list, String... expected) throws IOException
201     {
202         for (Object object : list)
203         {
204             String attachmentString = attachmentToString((DataHandler) object);
205             assertTrue(ArrayUtils.contains(expected, attachmentString));
206         }
207     }
208 }