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.test.integration.routing;
8   
9   
10  import org.mule.api.context.notification.EndpointMessageNotificationListener;
11  import org.mule.api.context.notification.ServerNotification;
12  import org.mule.context.notification.EndpointMessageNotification;
13  import org.mule.module.client.MuleClient;
14  import org.mule.tck.functional.FunctionalTestNotification;
15  import org.mule.tck.functional.FunctionalTestNotificationListener;
16  import org.mule.tck.junit4.FunctionalTestCase;
17  import org.mule.util.concurrent.Latch;
18  
19  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
20  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
21  import org.apache.commons.lang.SerializationUtils;
22  import org.junit.Test;
23  
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertTrue;
26  
27  public class MessageChunkingTestCase extends FunctionalTestCase
28  {
29      @Override
30      protected String getConfigResources()
31      {
32          return "org/mule/test/integration/routing/message-chunking.xml";
33      }
34  
35      @Test
36      public void testMessageChunkingWithEvenSplit() throws Exception
37      {
38          doMessageChunking("0123456789", 5);
39      }
40  
41      @Test
42      public void testMessageChunkingWithOddSplit() throws Exception
43      {
44          doMessageChunking("01234567890", 6);
45      }
46  
47      @Test
48      public void testMessageChunkingWith100Splits() throws Exception
49      {
50          doMessageChunking("0123456789012345678901234567890123456789012345678901234567890123456789"
51                            + "01234567890123456789012345678901234567890123456789012345678901234567890123456789"
52                            + "01234567890123456789012345678901234567890123456789", 100);
53      }
54  
55      @Test
56      public void testMessageChunkingOneChunk() throws Exception
57      {
58          doMessageChunking("x", 1);
59      }
60  
61      @Test
62      public void testMessageChunkingObject() throws Exception
63      {
64          final AtomicInteger messagePartsCount = new AtomicInteger(0);
65          final Latch chunkingReceiverLatch = new Latch();
66          final SimpleSerializableObject simpleSerializableObject = new SimpleSerializableObject("Test String", true, 99);
67  
68          // find number of chunks
69          final int parts = (int) Math.ceil((SerializationUtils.serialize(simpleSerializableObject).length / (double) 2));
70  
71          // Listen to events fired by the ChunkingReceiver service
72          muleContext.registerListener(new FunctionalTestNotificationListener()
73          {
74              public void onNotification(ServerNotification notification)
75              {
76                  // Not strictly necessary to test for this as when we register the
77                  // listener we supply the ComponentName as the subscription filter
78                  assertEquals("ChunkingObjectReceiver", notification.getResourceIdentifier());
79                  // Test that we have received all chunks in the correct order
80                  Object reply = ((FunctionalTestNotification) notification).getEventContext().getMessage().getPayload();
81                  // Check if Object is of Correct Type
82                  assertTrue(reply instanceof SimpleSerializableObject);
83                  SimpleSerializableObject replySimpleSerializableObject = (SimpleSerializableObject) reply;
84                  // Check that Contents are Identical
85                  assertEquals(simpleSerializableObject.b, replySimpleSerializableObject.b);
86                  assertEquals(simpleSerializableObject.i, replySimpleSerializableObject.i);
87                  assertEquals(simpleSerializableObject.s, replySimpleSerializableObject.s);
88                  chunkingReceiverLatch.countDown();
89              }
90          }, "ChunkingObjectReceiver");
91  
92          // Listen to Message Notifications on the Chunking receiver so we can
93          // determine how many message parts have been received
94          muleContext.registerListener(new EndpointMessageNotificationListener<EndpointMessageNotification>()
95          {
96              public void onNotification(EndpointMessageNotification notification)
97              {
98                  if (notification.getAction() == EndpointMessageNotification.MESSAGE_RECEIVED)
99                  {
100                     messagePartsCount.getAndIncrement();
101                 }
102                 assertEquals("ChunkingObjectReceiver", notification.getResourceIdentifier());
103             }
104         }, "ChunkingObjectReceiver");
105 
106         MuleClient client = new MuleClient(muleContext);
107         client.dispatch("vm://inbound.object.channel", simpleSerializableObject, null);
108         // Wait for the message to be received and tested (in the listener above)
109         assertTrue(chunkingReceiverLatch.await(20L, TimeUnit.SECONDS));
110         // Ensure we processed expected number of message parts
111         assertEquals(parts, messagePartsCount.get());
112     }
113 
114     protected void doMessageChunking(final String data, int partsCount) throws Exception
115     {
116         final AtomicInteger messagePartsCount = new AtomicInteger(0);
117         final Latch chunkingReceiverLatch = new Latch();
118 
119         // Listen to events fired by the ChunkingReceiver service
120         muleContext.registerListener(new FunctionalTestNotificationListener()
121         {
122             public void onNotification(ServerNotification notification)
123             {
124                 // Not strictly necessary to test for this as when we register the
125                 // listener we supply the ComponentName as the subscription filter
126                 assertEquals("ChunkingReceiver", notification.getResourceIdentifier());
127                 
128                 // Test that we have received all chunks in the correct order
129                 Object reply = ((FunctionalTestNotification) notification).getReplyMessage();
130                 assertEquals(data + " Received", reply);
131                 chunkingReceiverLatch.countDown();
132             }
133         }, "ChunkingReceiver");
134 
135         // Listen to Message Notifications on the Chunking receiver so we can
136         // determine how many message parts have been received
137         muleContext.registerListener(new EndpointMessageNotificationListener<EndpointMessageNotification>()
138         {
139             public void onNotification(EndpointMessageNotification notification)
140             {
141                 if (notification.getAction() == EndpointMessageNotification.MESSAGE_RECEIVED)
142                 {
143                     messagePartsCount.getAndIncrement();
144                 }
145                 assertEquals("ChunkingReceiver", notification.getResourceIdentifier());
146             }
147         }, "ChunkingReceiver");
148 
149         MuleClient client = new MuleClient(muleContext);
150         client.dispatch("vm://inbound.channel", data, null);
151         // Wait for the message to be received and tested (in the listener above)
152         assertTrue(chunkingReceiverLatch.await(20L, TimeUnit.SECONDS));
153         // Ensure we processed expected number of message parts
154         assertEquals(partsCount, messagePartsCount.get());
155     }
156 }