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