View Javadoc

1   /*
2    * $Id: EventMetaDataPropagationTestCase.java 22421 2011-07-15 05:05:06Z 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;
12  
13  import org.mule.DefaultMuleEvent;
14  import org.mule.DefaultMuleMessage;
15  import org.mule.api.MuleEvent;
16  import org.mule.api.MuleEventContext;
17  import org.mule.api.MuleException;
18  import org.mule.api.MuleMessage;
19  import org.mule.api.MuleSession;
20  import org.mule.api.endpoint.InboundEndpoint;
21  import org.mule.api.lifecycle.Callable;
22  import org.mule.api.service.Service;
23  import org.mule.construct.Flow;
24  import org.mule.service.ServiceCompositeMessageSource;
25  import org.mule.session.DefaultMuleSession;
26  import org.mule.tck.AbstractServiceAndFlowTestCase;
27  import org.mule.tck.testmodels.fruit.Apple;
28  import org.mule.transformer.AbstractMessageAwareTransformer;
29  
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.io.OutputStream;
33  import java.util.Arrays;
34  import java.util.Collection;
35  import java.util.HashMap;
36  import java.util.Map;
37  
38  import javax.activation.DataHandler;
39  import javax.activation.DataSource;
40  
41  import org.junit.Test;
42  import org.junit.runners.Parameterized.Parameters;
43  
44  import static org.junit.Assert.assertEquals;
45  import static org.junit.Assert.assertNotNull;
46  import static org.junit.Assert.assertTrue;
47  
48  public class EventMetaDataPropagationTestCase extends AbstractServiceAndFlowTestCase
49  {
50      @Parameters
51      public static Collection<Object[]> parameters()
52      {
53          return Arrays.asList(new Object[][]{
54              {ConfigVariant.SERVICE, "org/mule/test/integration/event-metadata-propagation-config-service.xml"},
55              {ConfigVariant.FLOW, "org/mule/test/integration/event-metadata-propagation-config-flow.xml"}});
56      }
57  
58      public EventMetaDataPropagationTestCase(ConfigVariant variant, String configResources)
59      {
60          super(variant, configResources);
61      }
62  
63      @Test
64      public void testEventMetaDataPropagation() throws MuleException
65      {
66          if (variant.equals(ConfigVariant.FLOW))
67          {
68              Flow flow = muleContext.getRegistry().lookupObject("component1");
69              MuleSession session = new DefaultMuleSession(flow, muleContext);
70              MuleEvent event = new DefaultMuleEvent(new DefaultMuleMessage("Test MuleEvent", muleContext),
71                  ((InboundEndpoint) flow.getMessageSource()), session);
72              flow.process(event);
73          }
74          else
75          {
76              Service service = muleContext.getRegistry().lookupService("component1");
77              MuleSession session = new DefaultMuleSession(service, muleContext);
78              MuleEvent event = new DefaultMuleEvent(new DefaultMuleMessage("Test MuleEvent", muleContext),
79                  ((ServiceCompositeMessageSource) service.getMessageSource()).getEndpoints().get(0), session);
80              service.sendEvent(event);
81          }
82      }
83  
84      public static class DummyComponent implements Callable
85      {
86          @Override
87          public Object onCall(MuleEventContext context) throws Exception
88          {
89              if ("component1".equals(context.getFlowConstruct().getName()))
90              {
91                  Map<String, Object> props = new HashMap<String, Object>();
92                  props.put("stringParam", "param1");
93                  props.put("objectParam", new Apple());
94                  props.put("doubleParam", 12345.6);
95                  props.put("integerParam", 12345);
96                  props.put("longParam", (long) 123456789);
97                  props.put("booleanParam", Boolean.TRUE);
98                  MuleMessage msg = new DefaultMuleMessage(context.getMessageAsString(), props, muleContext);
99                  msg.addAttachment("test1", new DataHandler(new DataSource()
100                 {
101                     @Override
102                     public InputStream getInputStream() throws IOException
103                     {
104                         return null;
105                     }
106 
107                     @Override
108                     public OutputStream getOutputStream() throws IOException
109                     {
110                         return null;
111                     }
112 
113                     @Override
114                     public String getContentType()
115                     {
116                         return "text/plain";
117                     }
118 
119                     @Override
120                     public String getName()
121                     {
122                         return "test1";
123                     }
124                 }));
125                 return msg;
126             }
127             else
128             {
129                 MuleMessage msg = context.getMessage();
130                 assertEquals("param1", msg.getInboundProperty("stringParam"));
131                 final Object o = msg.getInboundProperty("objectParam");
132                 assertTrue(o instanceof Apple);
133                 assertEquals(12345.6, 12345.6, msg.<Double> getInboundProperty("doubleParam", 0d));
134                 assertEquals(12345, msg.<Integer> getInboundProperty("integerParam", 0).intValue());
135                 assertEquals(123456789, msg.<Long> getInboundProperty("longParam", 0L).longValue());
136                 assertEquals(Boolean.TRUE, msg.getInboundProperty("booleanParam", Boolean.FALSE));
137                 assertNotNull(msg.getInboundAttachment("test1"));
138             }
139             return null;
140         }
141     }
142 
143     /**
144      * Extend AbstractMessageAwareTransformer, even though it's deprecated, to ensure
145      * that it keeps working for compatibility with older user-written transformers.
146      */
147     @SuppressWarnings("deprecation")
148     public static class DummyTransformer extends AbstractMessageAwareTransformer
149     {
150         @Override
151         public Object transform(MuleMessage msg, String outputEncoding)
152         {
153             assertEquals("param1", msg.getOutboundProperty("stringParam"));
154             final Object o = msg.getOutboundProperty("objectParam");
155             assertTrue(o instanceof Apple);
156             assertEquals(12345.6, 12345.6, msg.<Double> getOutboundProperty("doubleParam", 0d));
157             assertEquals(12345, msg.<Integer> getOutboundProperty("integerParam", 0).intValue());
158             assertEquals(123456789, msg.<Long> getOutboundProperty("longParam", 0L).longValue());
159             assertEquals(Boolean.TRUE, msg.getOutboundProperty("booleanParam", Boolean.FALSE));
160             return msg;
161         }
162     }
163 }