View Javadoc

1   /*
2    * $Id: MessageChunkingTestCase.java 22422 2011-07-15 08:22:16Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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          // find number of chunks
85          final int parts = (int) Math.ceil((SerializationUtils.serialize(simpleSerializableObject).length / (double) 2));
86  
87          // Listen to events fired by the ChunkingReceiver service
88          muleContext.registerListener(new FunctionalTestNotificationListener()
89          {
90              @Override
91              public void onNotification(ServerNotification notification)
92              {
93                  // Not strictly necessary to test for this as when we register the
94                  // listener we supply the ComponentName as the subscription filter
95                  assertEquals("ChunkingObjectReceiver", notification.getResourceIdentifier());
96                  // Test that we have received all chunks in the correct order
97                  Object reply = ((FunctionalTestNotification) notification).getEventContext()
98                      .getMessage()
99                      .getPayload();
100                 // Check if Object is of Correct Type
101                 assertTrue(reply instanceof SimpleSerializableObject);
102                 SimpleSerializableObject replySimpleSerializableObject = (SimpleSerializableObject) reply;
103                 // Check that Contents are Identical
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         // Listen to Message Notifications on the Chunking receiver so we can
112         // determine how many message parts have been received
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         // Wait for the message to be received and tested (in the listener above)
129         assertTrue(chunkingReceiverLatch.await(20L, TimeUnit.SECONDS));
130         // Ensure we processed expected number of message parts
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         // Listen to events fired by the ChunkingReceiver service
140         muleContext.registerListener(new FunctionalTestNotificationListener()
141         {
142             @Override
143             public void onNotification(ServerNotification notification)
144             {
145                 // Not strictly necessary to test for this as when we register the
146                 // listener we supply the ComponentName as the subscription filter
147                 assertEquals("ChunkingReceiver", notification.getResourceIdentifier());
148 
149                 // Test that we have received all chunks in the correct order
150                 Object reply = ((FunctionalTestNotification) notification).getReplyMessage();
151                 assertEquals(data + " Received", reply);
152                 chunkingReceiverLatch.countDown();
153             }
154         }, "ChunkingReceiver");
155 
156         // Listen to Message Notifications on the Chunking receiver so we can
157         // determine how many message parts have been received
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         // Wait for the message to be received and tested (in the listener above)
174         assertTrue(chunkingReceiverLatch.await(20L, TimeUnit.SECONDS));
175         // Ensure we processed expected number of message parts
176         assertEquals(partsCount, messagePartsCount.get());
177     }
178 }