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.messaging.meps;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.api.config.MuleProperties;
11  import org.mule.module.client.MuleClient;
12  import org.mule.tck.junit4.FunctionalTestCase;
13  import org.mule.tck.util.WebServiceOnlineCheck;
14  import org.mule.transport.http.HttpConstants;
15  
16  import java.util.HashMap;
17  import java.util.Map;
18  
19  import org.junit.Ignore;
20  import org.junit.Test;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertNull;
25  import static org.junit.Assert.assertTrue;
26  
27  @Ignore("MULE-6926: flaky test (Relies on an external server which can go offline after isDisabledInThisEnvironment is executed)")
28  public class MessagePropertiesPropagationTestCase extends FunctionalTestCase
29  {
30  
31      @Override
32      protected boolean isFailOnTimeout()
33      {
34          // Do not fail test case upon timeout because this probably just means
35          // that the 3rd-party web service is off-line.
36          return false;
37      }
38      
39      /**
40       * If a simple call to the web service indicates that it is not responding properly,
41       * we disable the test case so as to not report a test failure which has nothing to do
42       * with Mule.
43       */
44      @Override
45      protected boolean isDisabledInThisEnvironment()
46      {
47          return !WebServiceOnlineCheck.isWebServiceOnline();
48      }
49  
50      @Override
51      protected String getConfigResources()
52      {
53          return "org/mule/test/integration/messaging/meps/message-properties-propagation.xml";
54      }
55  
56      /**
57       * As per EE-1613, the Correlation-related properties _should_ be propagated to the response message by default.
58       */
59      @Test
60      public void testPropagatedPropertiesWithHttpTransport() throws Exception
61      {
62          MuleClient client = new MuleClient(muleContext);
63          
64          Map<String, Object> props = new HashMap<String, Object>();
65          props.put("Content-Type", "application/x-www-form-urlencoded");
66          props.put(MuleProperties.MULE_CORRELATION_ID_PROPERTY, "TestID");
67          props.put(MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY, "TestGroupSize");
68          props.put(MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY, "TestSequence");
69          
70          MuleMessage response = client.send("vm://httpService1", "symbol=IBM", props);
71          assertNotNull(response);
72          checkPayLoad(response.getPayloadAsString());
73          assertEquals("TestID", response.getOutboundProperty(MuleProperties.MULE_CORRELATION_ID_PROPERTY));
74          assertEquals("TestGroupSize", response.getOutboundProperty(MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY));
75          assertEquals("TestSequence", response.getOutboundProperty(MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY));
76      }
77  
78      /**
79       * As per EE-1613, the Correlation-related properties _should_ be propagated to the response message by default.
80       */
81      @Test
82      public void testPropagatedPropertiesWithCxfTransport() throws Exception
83      {
84          MuleClient client = new MuleClient(muleContext);
85  
86          Map<String, Object> props = new HashMap<String, Object>();
87          props.put(MuleProperties.MULE_CORRELATION_ID_PROPERTY, "TestID");
88          props.put(MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY, "TestGroupSize");
89          props.put(MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY, "TestSequence");
90  
91          MuleMessage response = client.send("vm://cxfService1", "IBM", props);
92          assertNotNull(response);
93          checkPayLoad(response.getPayloadAsString());
94          assertEquals("TestID", response.getOutboundProperty(MuleProperties.MULE_CORRELATION_ID_PROPERTY));
95          assertEquals("TestGroupSize", response.getOutboundProperty(MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY));
96          assertEquals("TestSequence", response.getOutboundProperty(MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY));
97      }
98  
99      /**
100      * As per EE-1611/MULE-4302, custom properties and non-Correlation-related properties _should not_ be propagated
101      * to the response message by default.
102      */
103     @Test
104     public void testNotPropagatedPropertiesWithHttpTransport() throws Exception
105     {
106         MuleClient client = new MuleClient(muleContext);
107 
108         Map<String, Object> props = new HashMap<String, Object>();
109         props.put("Content-Type", "application/x-www-form-urlencoded");
110         props.put("some", "thing");
111         props.put("other", "stuff");
112         props.put(HttpConstants.HEADER_CONTENT_TYPE, "text/bizarre;charset=utf-16");
113 
114         MuleMessage response = client.send("vm://httpService1", "symbol=IBM", props);
115         assertNotNull(response);
116         assertNull(response.getInboundProperty("some"));
117         assertNull(response.getInboundProperty("other"));
118         assertEquals("text/plain; charset=utf-8", response.getInboundProperty(HttpConstants.HEADER_CONTENT_TYPE));
119         assertEquals("utf-8", response.getEncoding());
120     }
121 
122     /**
123      * As per EE-1611/MULE-4302, custom properties and non-Correlation-related properties _should not_ be propagated
124      * to the response message by default.
125      */
126     @Test
127     public void testNotPropagatedPropertiesWithCxfTransport() throws Exception
128     {
129         MuleClient client = new MuleClient(muleContext);
130 
131         Map<String, Object> props = new HashMap<String, Object>();
132         props.put("some", "thing");
133         props.put("other", "stuff");
134         props.put(HttpConstants.HEADER_CONTENT_TYPE, "text/bizarre;charset=utf-16");
135 
136         MuleMessage response = client.send("vm://cxfService1", "IBM", props);
137         assertNotNull(response);
138         assertNull(response.getInboundProperty("some"));
139         assertNull(response.getInboundProperty("other"));
140         assertEquals("text/xml; charset=utf-8", response.getInboundProperty(HttpConstants.HEADER_CONTENT_TYPE));
141         assertEquals("utf-8", response.getEncoding());
142     }
143 
144     /**
145      * Force the properties to be propagated to the response message.
146      */
147     @Test
148     public void testForcePropagatedPropertiesWithHttpTransport() throws Exception
149     {
150         MuleClient client = new MuleClient(muleContext);
151 
152         Map<String, Object> props = new HashMap<String, Object>();
153         props.put("Content-Type", "application/x-www-form-urlencoded");
154         props.put("some", "thing");
155         props.put("other", "stuff");
156 
157         MuleMessage response = client.send("vm://httpService2", "symbol=IBM", props);
158         assertNotNull(response);
159         checkPayLoad(response.getPayloadAsString());
160         assertEquals("thing", response.getInboundProperty("some"));
161         assertEquals("stuff", response.getInboundProperty("other"));
162     }
163 
164     /**
165      * Force the properties to be propagated to the response message.
166      * MULE-4986
167      */
168     public void xtestForcePropagatedPropertiesWithCxfTransport() throws Exception
169     {
170         MuleClient client = new MuleClient(muleContext);
171 
172         Map<String, Object> props = new HashMap<String, Object>();
173         props.put("some", "thing");
174         props.put("other", "stuff");
175 
176         MuleMessage response = client.send("vm://cxfService2", "symbol=IBM", props);
177         assertNotNull(response);
178         checkPayLoad(response.getPayloadAsString());
179         assertEquals("thing", response.getOutboundProperty("some"));
180         assertEquals("stuff", response.getOutboundProperty("other"));
181     }
182     
183     private void checkPayLoad(String payload)
184     {
185         assertNotNull("payload is null", payload);
186         assertTrue("payload does not contain 'PreviousClose': " + payload, payload.contains("PreviousClose"));
187     }
188 }