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;
8   
9   import org.mule.DefaultMuleEvent;
10  import org.mule.DefaultMuleMessage;
11  import org.mule.api.MuleEvent;
12  import org.mule.api.MuleEventContext;
13  import org.mule.api.MuleException;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.MuleSession;
16  import org.mule.api.lifecycle.Callable;
17  import org.mule.api.service.Service;
18  import org.mule.service.ServiceCompositeMessageSource;
19  import org.mule.session.DefaultMuleSession;
20  import org.mule.tck.junit4.FunctionalTestCase;
21  import org.mule.tck.testmodels.fruit.Apple;
22  import org.mule.transformer.AbstractMessageAwareTransformer;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.io.OutputStream;
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  import javax.activation.DataHandler;
31  import javax.activation.DataSource;
32  
33  import org.junit.Test;
34  
35  import static org.junit.Assert.assertEquals;
36  import static org.junit.Assert.assertNotNull;
37  import static org.junit.Assert.assertTrue;
38  
39  public class EventMetaDataPropagationTestCase extends FunctionalTestCase
40  {
41  
42      @Override
43      protected String getConfigResources()
44      {
45          return "org/mule/test/integration/event-metadata-propagation-config.xml";
46      }
47  
48      @Test
49      public void testEventMetaDataPropagation() throws MuleException
50      {
51          Service service = muleContext.getRegistry().lookupService("component1");
52          MuleSession session = new DefaultMuleSession(service, muleContext);
53          MuleEvent event = new DefaultMuleEvent(new DefaultMuleMessage("Test MuleEvent", muleContext),
54              ((ServiceCompositeMessageSource) service.getMessageSource()).getEndpoints().get(0), session);
55          service.sendEvent(event);
56      }
57  
58      public static class DummyComponent implements Callable
59      {
60          public Object onCall(MuleEventContext context) throws Exception
61          {
62              if ("component1".equals(context.getFlowConstruct().getName()))
63              {
64                  Map<String, Object> props = new HashMap<String, Object>();
65                  props.put("stringParam", "param1");
66                  props.put("objectParam", new Apple());
67                  props.put("doubleParam", 12345.6);
68                  props.put("integerParam", 12345);
69                  props.put("longParam", (long) 123456789);
70                  props.put("booleanParam", Boolean.TRUE);
71                  MuleMessage msg = new DefaultMuleMessage(context.getMessageAsString(), props, muleContext);
72                  msg.addAttachment("test1", new DataHandler(new DataSource()
73                  {
74                      public InputStream getInputStream() throws IOException
75                      {
76                          return null;
77                      }
78  
79                      public OutputStream getOutputStream() throws IOException
80                      {
81                          return null;
82                      }
83  
84                      public String getContentType()
85                      {
86                          return "text/plain";
87                      }
88  
89                      public String getName()
90                      {
91                          return "test1";
92                      }
93                  }));
94                  return msg;
95              }
96              else
97              {
98                  MuleMessage msg = context.getMessage();
99                  assertEquals("param1", msg.getInboundProperty("stringParam"));
100                 final Object o = msg.getInboundProperty("objectParam");
101                 assertTrue(o instanceof Apple);
102                 assertEquals(12345.6, 12345.6, msg.<Double>getInboundProperty("doubleParam", 0d));
103                 assertEquals(12345, msg.<Integer>getInboundProperty("integerParam", 0).intValue());
104                 assertEquals(123456789, msg.<Long>getInboundProperty("longParam", 0L).longValue());
105                 assertEquals(Boolean.TRUE, msg.getInboundProperty("booleanParam", Boolean.FALSE));
106                 assertNotNull(msg.getInboundAttachment("test1"));
107             }
108             return null;
109         }
110     }
111 
112     /**
113      * Extend AbstractMessageAwareTransformer, even though it's deprecated, to ensure that it
114      * keeps working for compatibility with older user-written transformers.
115      */
116     @SuppressWarnings("deprecation")
117     public static class DummyTransformer extends AbstractMessageAwareTransformer
118     {
119         @Override
120         public Object transform(MuleMessage msg, String outputEncoding)
121         {
122             assertEquals("param1", msg.getOutboundProperty("stringParam"));
123             final Object o = msg.getOutboundProperty("objectParam");
124             assertTrue(o instanceof Apple);
125             assertEquals(12345.6, 12345.6, msg.<Double>getOutboundProperty("doubleParam", 0d));
126             assertEquals(12345, msg.<Integer>getOutboundProperty("integerParam", 0).intValue());
127             assertEquals(123456789, msg.<Long>getOutboundProperty("longParam", 0L).longValue());
128             assertEquals(Boolean.TRUE, msg.getOutboundProperty("booleanParam", Boolean.FALSE));
129             return msg;
130         }
131     }
132 }