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.routing;
8   
9   import org.mule.api.MuleEvent;
10  import org.mule.api.MuleException;
11  import org.mule.api.processor.MessageProcessor;
12  import org.mule.api.transport.PropertyScope;
13  import org.mule.tck.junit4.AbstractMuleContextTestCase;
14  
15  import java.util.ArrayList;
16  import java.util.HashMap;
17  import java.util.List;
18  import java.util.Map;
19  
20  import org.junit.Test;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertTrue;
24  
25  public class MapSplitterTestCase extends AbstractMuleContextTestCase
26  {
27      private MapSplitter mapSplitter;
28      private List<String> splitPayloads = new ArrayList<String>();
29      private List<String> splitKeyProperties = new ArrayList<String>();
30  
31      @Override
32      protected void doSetUp() throws Exception
33      {
34          super.doSetUp();
35          mapSplitter = new MapSplitter();
36          mapSplitter.setMuleContext(muleContext);
37          mapSplitter.setListener(new MessageProcessor()
38          {
39              public MuleEvent process(MuleEvent event) throws MuleException
40              {
41                  splitPayloads.add(event.getMessageAsString());
42                  splitKeyProperties.add((String) event.getMessage().getProperty(MapSplitter.MAP_ENTRY_KEY,
43                      PropertyScope.INVOCATION));
44                  return event;
45              }
46          });
47      }
48  
49      @Test
50      public void testSplit() throws Exception
51      {
52          Map<String, Object> testMap = new HashMap<String, Object>();
53          testMap.put("1", "one");
54          testMap.put("2", "two");
55          testMap.put("3", "three");
56  
57          mapSplitter.process(getTestEvent(testMap));
58  
59          assertEquals(3, splitPayloads.size());
60          assertTrue(splitPayloads.contains("one"));
61          assertTrue(splitPayloads.contains("two"));
62          assertTrue(splitPayloads.contains("three"));
63  
64          assertEquals(3, splitPayloads.size());
65          assertEquals("1", splitKeyProperties.get(splitPayloads.indexOf("one")));
66          assertEquals("2", splitKeyProperties.get(splitPayloads.indexOf("two")));
67          assertEquals("3", splitKeyProperties.get(splitPayloads.indexOf("three")));
68      }
69  }