1
2
3
4
5
6
7
8
9
10
11 package org.mule.test.integration.routing;
12
13 import org.mule.api.context.notification.EndpointMessageNotificationListener;
14 import org.mule.api.context.notification.ServerNotification;
15 import org.mule.context.notification.EndpointMessageNotification;
16 import org.mule.module.client.MuleClient;
17 import org.mule.tck.AbstractServiceAndFlowTestCase;
18 import org.mule.tck.functional.FunctionalTestNotification;
19 import org.mule.tck.functional.FunctionalTestNotificationListener;
20 import org.mule.util.concurrent.Latch;
21
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.concurrent.TimeUnit;
25 import java.util.concurrent.atomic.AtomicInteger;
26
27 import org.apache.commons.lang.SerializationUtils;
28 import org.junit.Test;
29 import org.junit.runners.Parameterized.Parameters;
30
31 import static org.junit.Assert.assertEquals;
32 import static org.junit.Assert.assertTrue;
33
34 public class MessageChunkingTestCase extends AbstractServiceAndFlowTestCase
35 {
36 @Parameters
37 public static Collection<Object[]> parameters()
38 {
39 return Arrays.asList(new Object[][]{
40 {ConfigVariant.SERVICE, "org/mule/test/integration/routing/message-chunking-service.xml"},
41 {ConfigVariant.FLOW, "org/mule/test/integration/routing/message-chunking-flow.xml"}});
42 }
43
44 public MessageChunkingTestCase(ConfigVariant variant, String configResources)
45 {
46 super(variant, configResources);
47 }
48
49 @Test
50 public void testMessageChunkingWithEvenSplit() throws Exception
51 {
52 doMessageChunking("0123456789", 5);
53 }
54
55 @Test
56 public void testMessageChunkingWithOddSplit() throws Exception
57 {
58 doMessageChunking("01234567890", 6);
59 }
60
61 @Test
62 public void testMessageChunkingWith100Splits() throws Exception
63 {
64 doMessageChunking(
65 "0123456789012345678901234567890123456789012345678901234567890123456789"
66 + "01234567890123456789012345678901234567890123456789012345678901234567890123456789"
67 + "01234567890123456789012345678901234567890123456789", 100);
68 }
69
70 @Test
71 public void testMessageChunkingOneChunk() throws Exception
72 {
73 doMessageChunking("x", 1);
74 }
75
76 @Test
77 public void testMessageChunkingObject() throws Exception
78 {
79 final AtomicInteger messagePartsCount = new AtomicInteger(0);
80 final Latch chunkingReceiverLatch = new Latch();
81 final SimpleSerializableObject simpleSerializableObject = new SimpleSerializableObject("Test String",
82 true, 99);
83
84
85 final int parts = (int) Math.ceil((SerializationUtils.serialize(simpleSerializableObject).length / (double) 2));
86
87
88 muleContext.registerListener(new FunctionalTestNotificationListener()
89 {
90 @Override
91 public void onNotification(ServerNotification notification)
92 {
93
94
95 assertEquals("ChunkingObjectReceiver", notification.getResourceIdentifier());
96
97 Object reply = ((FunctionalTestNotification) notification).getEventContext()
98 .getMessage()
99 .getPayload();
100
101 assertTrue(reply instanceof SimpleSerializableObject);
102 SimpleSerializableObject replySimpleSerializableObject = (SimpleSerializableObject) reply;
103
104 assertEquals(simpleSerializableObject.b, replySimpleSerializableObject.b);
105 assertEquals(simpleSerializableObject.i, replySimpleSerializableObject.i);
106 assertEquals(simpleSerializableObject.s, replySimpleSerializableObject.s);
107 chunkingReceiverLatch.countDown();
108 }
109 }, "ChunkingObjectReceiver");
110
111
112
113 muleContext.registerListener(new EndpointMessageNotificationListener<EndpointMessageNotification>()
114 {
115 @Override
116 public void onNotification(EndpointMessageNotification notification)
117 {
118 if (notification.getAction() == EndpointMessageNotification.MESSAGE_RECEIVED)
119 {
120 messagePartsCount.getAndIncrement();
121 }
122 assertEquals("ChunkingObjectReceiver", notification.getResourceIdentifier());
123 }
124 }, "ChunkingObjectReceiver");
125
126 MuleClient client = new MuleClient(muleContext);
127 client.dispatch("vm://inbound.object.channel", simpleSerializableObject, null);
128
129 assertTrue(chunkingReceiverLatch.await(20L, TimeUnit.SECONDS));
130
131 assertEquals(parts, messagePartsCount.get());
132 }
133
134 protected void doMessageChunking(final String data, int partsCount) throws Exception
135 {
136 final AtomicInteger messagePartsCount = new AtomicInteger(0);
137 final Latch chunkingReceiverLatch = new Latch();
138
139
140 muleContext.registerListener(new FunctionalTestNotificationListener()
141 {
142 @Override
143 public void onNotification(ServerNotification notification)
144 {
145
146
147 assertEquals("ChunkingReceiver", notification.getResourceIdentifier());
148
149
150 Object reply = ((FunctionalTestNotification) notification).getReplyMessage();
151 assertEquals(data + " Received", reply);
152 chunkingReceiverLatch.countDown();
153 }
154 }, "ChunkingReceiver");
155
156
157
158 muleContext.registerListener(new EndpointMessageNotificationListener<EndpointMessageNotification>()
159 {
160 @Override
161 public void onNotification(EndpointMessageNotification notification)
162 {
163 if (notification.getAction() == EndpointMessageNotification.MESSAGE_RECEIVED)
164 {
165 messagePartsCount.getAndIncrement();
166 }
167 assertEquals("ChunkingReceiver", notification.getResourceIdentifier());
168 }
169 }, "ChunkingReceiver");
170
171 MuleClient client = new MuleClient(muleContext);
172 client.dispatch("vm://inbound.channel", data, null);
173
174 assertTrue(chunkingReceiverLatch.await(20L, TimeUnit.SECONDS));
175
176 assertEquals(partsCount, messagePartsCount.get());
177 }
178 }