View Javadoc

1   /*
2    * $Id: CollectionMessageSplitterTestCase.java 22377 2011-07-11 12:41:42Z 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.routing;
12  
13  import org.mule.DefaultMuleEvent;
14  import org.mule.DefaultMuleMessage;
15  import org.mule.api.MuleEvent;
16  import org.mule.api.MuleException;
17  import org.mule.api.MuleMessage;
18  import org.mule.api.MuleSession;
19  import org.mule.api.config.MuleProperties;
20  import org.mule.api.processor.MessageProcessor;
21  import org.mule.api.service.Service;
22  import org.mule.routing.outbound.IteratorMessageSequence;
23  import org.mule.tck.junit4.AbstractMuleContextTestCase;
24  
25  import java.util.ArrayList;
26  import java.util.Arrays;
27  import java.util.Collections;
28  import java.util.HashMap;
29  import java.util.HashSet;
30  import java.util.List;
31  import java.util.Map;
32  import java.util.Set;
33  
34  import org.junit.Test;
35  
36  import static org.junit.Assert.assertEquals;
37  import static org.junit.Assert.assertNull;
38  import static org.junit.Assert.assertTrue;
39  import static org.mockito.Mockito.mock;
40  import static org.mockito.Mockito.times;
41  import static org.mockito.Mockito.verify;
42  import static org.mockito.Mockito.when;
43  
44  public class CollectionMessageSplitterTestCase extends AbstractMuleContextTestCase
45  {
46      private static final List<String> TEST_LIST = Arrays.asList("abc", "def", "ghi");
47  
48      public CollectionMessageSplitterTestCase()
49      {
50          setStartContext(true);
51      }
52  
53      /**
54       * Tests that a collection payload can be routed properly
55       */
56      @Test
57      public void testRouterCollection() throws Exception
58      {
59          assertRouted(TEST_LIST, 3, true);
60      }
61  
62      /**
63       * Tests that an iterable payload can be routed properly
64       */
65      @Test
66      @SuppressWarnings("unchecked")
67      public void testRouterIterable() throws Exception
68      {
69          Iterable<String> mock = mock(Iterable.class);
70          when(mock.iterator()).thenReturn(TEST_LIST.iterator());
71          assertRouted(mock, 3, false);
72          verify(mock, times(1)).iterator();
73      }
74  
75      /**
76       * Tests that an iterator payload can be routed properly
77       */
78      @Test
79      public void testRouterIterator() throws Exception
80      {
81          assertRouted(TEST_LIST.iterator(), 3, false) ;
82      }
83  
84      /**
85       * Tests that a message sequence payload can be routed properly
86       */
87      @Test
88      public void testRouterMesseageSequence() throws Exception
89      {
90          assertRouted(new IteratorMessageSequence<String>(TEST_LIST.iterator()), 3 , false);
91      }
92  
93      /**
94       *  Tests that an empty sequence can be routed properly
95       */
96      @Test
97      public void testEmptySequence() throws Exception
98      {
99          Object payload = Collections.emptySet();
100         Service fc = getTestService();
101         MuleSession session = getTestSession(fc, muleContext);
102         MuleMessage toSplit = new DefaultMuleMessage(payload, new HashMap<String, Object>(), new HashMap<String, Object>(), null, muleContext);
103         CollectionSplitter splitter = new CollectionSplitter();
104         splitter.setMuleContext(muleContext);
105         DefaultMuleEvent event = new DefaultMuleEvent(toSplit, getTestInboundEndpoint("ep"), session);
106         assertNull(splitter.process(event));
107     }
108 
109     private void assertRouted(Object payload, int count, boolean counted) throws Exception, MuleException
110     {
111         Service fc = getTestService();
112         MuleSession session = getTestSession(fc, muleContext);
113 
114         Map<String, Object> inboundProps = new HashMap<String, Object>();
115         inboundProps.put("inbound1", "1");
116         inboundProps.put("inbound2", 2);
117         inboundProps.put("inbound3", session);
118 
119         Map<String, Object> outboundProps = new HashMap<String, Object>();
120         outboundProps.put("outbound1", "3");
121         outboundProps.put("outbound2", 4);
122         outboundProps.put("outbound3", session);
123 
124         Map<String, Object> invocationProps = new HashMap<String, Object>();
125         invocationProps.put("invoke1", "5");
126         invocationProps.put("invoke2", 6);
127         invocationProps.put("invoke3", session);
128 
129         Set<Integer> expectedSequences = new HashSet<Integer>();
130         for (int i = 1; i <= count; i++)
131         {
132             expectedSequences.add(i);
133         }
134 
135         MuleMessage toSplit = new DefaultMuleMessage(payload, inboundProps, outboundProps, null, muleContext);
136         for (Map.Entry<String, Object> entry : invocationProps.entrySet())
137         {
138             toSplit.setInvocationProperty(entry.getKey(), entry.getValue());
139         }
140         CollectionSplitter splitter = new CollectionSplitter();
141         splitter.setMuleContext(muleContext);
142         Grabber grabber = new Grabber();
143         splitter.setListener(grabber);
144         DefaultMuleEvent event = new DefaultMuleEvent(toSplit, getTestInboundEndpoint("ep"), session);
145         splitter.process(event);
146         List<MuleMessage> splits =  grabber.getMessages();
147         assertEquals(count, splits.size());
148 
149         Set<Object> actualSequences = new HashSet<Object>();
150         for (MuleMessage msg : splits)
151         {
152             assertTrue(msg.getPayload() instanceof String);
153             assertEquals(counted ? count : -1, msg.getOutboundProperty(MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY));
154             actualSequences.add( msg.getOutboundProperty(MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY));
155             String str = (String) msg.getPayload();
156             assertTrue(TEST_LIST.contains(str));
157             for (String key : inboundProps.keySet())
158             {
159                 assertEquals(msg.getInboundProperty(key), inboundProps.get(key));
160             }
161             for (String key : outboundProps.keySet())
162             {
163                 assertEquals(msg.getOutboundProperty(key), outboundProps.get(key));
164             }
165             for (String key : invocationProps.keySet())
166             {
167                 assertEquals(msg.getInvocationProperty(key), invocationProps.get(key));
168             }
169         }
170         assertEquals(expectedSequences, actualSequences);
171     }
172 
173 
174     private static class Grabber implements MessageProcessor
175     {
176         private List<MuleMessage> messages = new ArrayList<MuleMessage>();
177 
178         @Override
179         public MuleEvent process(MuleEvent event) throws MuleException
180         {
181             messages.add(event.getMessage());
182             return null;
183         }
184 
185         public List<MuleMessage> getMessages()
186         {
187             return messages;
188         }
189     }
190 }