1
2
3
4
5
6
7 package org.mule.routing;
8
9 import org.mule.DefaultMuleEvent;
10 import org.mule.DefaultMuleMessage;
11 import org.mule.api.MuleEvent;
12 import org.mule.api.MuleException;
13 import org.mule.api.MuleMessage;
14 import org.mule.api.MuleSession;
15 import org.mule.api.processor.MessageProcessor;
16 import org.mule.api.service.Service;
17 import org.mule.tck.junit4.AbstractMuleContextTestCase;
18
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.junit.Test;
26
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertTrue;
29
30 public class CollectionMessageSplitterTestCase extends AbstractMuleContextTestCase
31 {
32 public CollectionMessageSplitterTestCase()
33 {
34 setStartContext(true);
35 }
36
37 @Test
38 public void testSpliterMultipleParts() throws Exception
39 {
40 Service fc = getTestService();
41 MuleSession session = getTestSession(fc, muleContext);
42
43 Map<String, Object> inboundProps = new HashMap();
44 inboundProps.put("inbound1", "1");
45 inboundProps.put("inbound2", 2);
46 inboundProps.put("inbound3", session);
47
48 Map<String, Object> outboundProps = new HashMap();
49 inboundProps.put("outbound1", "3");
50 inboundProps.put("outbound2", 4);
51 inboundProps.put("outbound3", session);
52
53 Map<String, Object> invocationProps = new HashMap();
54 inboundProps.put("invoke1", "5");
55 inboundProps.put("invoke2", 6);
56 inboundProps.put("invoke3", session);
57
58 List<String> payload = Arrays.asList("abc", "def", "ghi");
59 MuleMessage toSplit = new DefaultMuleMessage(payload, inboundProps, outboundProps, null, muleContext);
60 for (Map.Entry<String, Object> entry : invocationProps.entrySet())
61 {
62 toSplit.setInvocationProperty(entry.getKey(), entry.getValue());
63 }
64 CollectionSplitter splitter = new CollectionSplitter();
65 splitter.setMuleContext(muleContext);
66 Grabber grabber = new Grabber();
67 splitter.setListener(grabber);
68 DefaultMuleEvent event = new DefaultMuleEvent(toSplit, getTestOutboundEndpoint("ep"), session);
69 splitter.process(event);
70 List<MuleMessage> splits = grabber.getMessages();
71 assertEquals(3, splits.size());
72 assertSplitPart(inboundProps, outboundProps, invocationProps, payload, splits);
73 }
74
75 @Test
76 public void testSpliterSinglePart() throws Exception
77 {
78 Service fc = getTestService();
79 MuleSession session = getTestSession(fc, muleContext);
80
81 Map<String, Object> inboundProps = new HashMap();
82 inboundProps.put("inbound1", "1");
83
84 Map<String, Object> outboundProps = new HashMap();
85 inboundProps.put("outbound1", "2");
86
87 Map<String, Object> invocationProps = new HashMap();
88 inboundProps.put("invoke1", "3");
89
90 List<String> payload = Arrays.asList("abc");
91 MuleMessage toSplit = new DefaultMuleMessage(payload, inboundProps, outboundProps, null, muleContext);
92 for (Map.Entry<String, Object> entry : invocationProps.entrySet())
93 {
94 toSplit.setInvocationProperty(entry.getKey(), entry.getValue());
95 }
96 CollectionSplitter splitter = new CollectionSplitter();
97 splitter.setMuleContext(muleContext);
98 Grabber grabber = new Grabber();
99 splitter.setListener(grabber);
100 DefaultMuleEvent event = new DefaultMuleEvent(toSplit, getTestOutboundEndpoint("ep"), session);
101 splitter.process(event);
102 List<MuleMessage> splits = grabber.getMessages();
103 assertEquals(1, splits.size());
104 assertSplitPart(inboundProps, outboundProps, invocationProps, payload, splits);
105 }
106
107 protected void assertSplitPart(Map<String, Object> inboundProps,
108 Map<String, Object> outboundProps,
109 Map<String, Object> invocationProps,
110 List<String> payload,
111 List<MuleMessage> splits)
112 {
113 for (MuleMessage msg : splits)
114 {
115 assertTrue(msg.getPayload() instanceof String);
116 String str = (String) msg.getPayload();
117 assertTrue(payload.contains(str));
118 for (String key : inboundProps.keySet())
119 {
120 assertEquals(msg.getInboundProperty(key), inboundProps.get(key));
121 }
122 for (String key : outboundProps.keySet())
123 {
124 assertEquals(msg.getOutboundProperty(key), outboundProps.get(key));
125 }
126 for (String key : invocationProps.keySet())
127 {
128 assertEquals(msg.getInvocationProperty(key), invocationProps.get(key));
129 }
130 }
131 }
132
133
134 static class Grabber implements MessageProcessor
135 {
136 private List<MuleMessage> messages = new ArrayList();
137
138 public MuleEvent process(MuleEvent event) throws MuleException
139 {
140 messages.add(event.getMessage());
141 return null;
142 }
143
144 public List<MuleMessage> getMessages()
145 {
146 return messages;
147 }
148 }
149 }