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.module.cxf;
8   
9   import static org.junit.Assert.assertEquals;
10  import static org.junit.Assert.assertFalse;
11  import static org.junit.Assert.assertNotNull;
12  import static org.junit.Assert.assertTrue;
13  
14  import org.mule.api.MuleEventContext;
15  import org.mule.api.MuleMessage;
16  import org.mule.module.client.MuleClient;
17  import org.mule.module.cxf.testmodels.AsyncService;
18  import org.mule.tck.functional.EventCallback;
19  import org.mule.tck.functional.FunctionalTestComponent;
20  import org.mule.tck.junit4.FunctionalTestCase;
21  import org.mule.tck.junit4.rule.DynamicPort;
22  import org.mule.transport.http.HttpConstants;
23  import org.mule.util.concurrent.Latch;
24  
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
29  import org.junit.Ignore;
30  import org.junit.Rule;
31  import org.junit.Test;
32  
33  
34  public class ProxyTestCase extends FunctionalTestCase
35  {
36      String msg = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
37          + "<soap:Body><test xmlns=\"http://foo\"> foo </test>" + "</soap:Body>" + "</soap:Envelope>";
38  
39      String doGoogleSearch = "<urn:doGoogleSearch xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:urn=\"urn:GoogleSearch\">";
40  
41      String msgWithComment = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
42          + "<!-- comment 1 -->"
43          + "<soap:Header>"
44          + "<!-- comment 2 -->"
45          + "</soap:Header>"
46          + "<!-- comment 3 -->"
47          + "<soap:Body>"
48          + "<!-- comment 4 -->"
49          + doGoogleSearch
50          + "<!-- this comment breaks it -->"
51          + "<key>1</key>"
52          + "<!-- comment 5 -->"
53          + "<q>a</q>"
54          + "<start>0</start>"
55          + "<maxResults>1</maxResults>"
56          + "<filter>false</filter>"
57          + "<restrict>a</restrict>"
58          + "<safeSearch>true</safeSearch>"
59          + "<lr>a</lr>"
60          + "<ie>b</ie>"
61          + "<oe>c</oe>"
62          + "</urn:doGoogleSearch>"
63          + "<!-- comment 6 -->"
64          + "</soap:Body>"
65          + "<!-- comment 7 -->"
66          + "</soap:Envelope>";
67  
68      @Rule
69      public DynamicPort dynamicPort = new DynamicPort("port1");
70  
71      @Override
72      protected String getConfigResources()
73      {
74          return "proxy-conf.xml";
75      }
76  
77     @Test
78     public void testServerWithEcho() throws Exception
79     {
80          MuleClient client = new MuleClient(muleContext);
81          MuleMessage result = client.send("http://localhost:" + dynamicPort.getNumber() + "/services/Echo", msg, null);
82          String resString = result.getPayloadAsString();
83          assertTrue(resString.indexOf("<test xmlns=\"http://foo\"> foo </test>") != -1);
84     }
85  
86     @Test
87     public void testServerClientProxy() throws Exception
88     {
89          String msg = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
90                       + "<soap:Body> <foo xmlns=\"http://foo\"></foo>" + "</soap:Body>" + "</soap:Envelope>";
91  
92          MuleClient client = new MuleClient(muleContext);
93          MuleMessage result = client.send("http://localhost:" + dynamicPort.getNumber() + "/services/proxy", msg, null);
94          String resString = result.getPayloadAsString();
95  
96          assertTrue(resString.indexOf("<foo xmlns=\"http://foo\"") != -1);
97     }
98  
99  
100    @Test
101    public void testProxyBodyValidation() throws Exception
102    {
103         doTestProxyValidation("http://localhost:" + dynamicPort.getNumber() + "/services/proxyBodyWithValidation");
104    }
105 
106    @Test
107    public void testProxyBodyValidationWithExternalSchema() throws Exception
108    {
109         doTestProxyValidation("http://localhost:" + dynamicPort.getNumber() + "/services/proxyBodyWithValidationAndSchemas");
110    }
111 
112    @Test
113    public void testProxyEnvelopeValidation() throws Exception
114    {
115         doTestProxyValidation("http://localhost:" + dynamicPort.getNumber() + "/services/proxyEnvelopeWithValidation");
116    }
117 
118    public void doTestProxyValidation(String url) throws Exception
119    {
120         MuleClient client = new MuleClient(muleContext);
121         MuleMessage result = client.send(url, msg, null);
122         String resString = result.getPayloadAsString();
123         assertTrue(resString.indexOf("Schema validation error on message") != -1);
124 
125         String valid =
126             "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
127             + "<soap:Body> " +
128                     "<echo xmlns=\"http://www.muleumo.org\">" +
129                     "  <echo>test</echo>" +
130                     "</echo>"
131             + "</soap:Body>"
132             + "</soap:Envelope>";
133         result = client.send(url, valid, null);
134         resString = result.getPayloadAsString();
135         assertTrue(resString.contains("<echoResponse xmlns=\"http://www.muleumo.org\">"));
136    }
137 
138    @Test
139    public void testServerClientProxyWithWsdl() throws Exception
140    {
141         final Latch latch = new Latch();
142         ((FunctionalTestComponent) getComponent("serverClientProxyWithWsdl")).setEventCallback(new EventCallback()
143         {
144 
145             public void eventReceived(MuleEventContext context, Object component) throws Exception
146             {
147                 latch.countDown();
148             }
149         });
150 
151         String msg = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
152                      + "<soap:Body> <test xmlns=\"http://foo\"></test>" + "</soap:Body>" + "</soap:Envelope>";
153 
154         MuleClient client = new MuleClient(muleContext);
155         MuleMessage result = client.send("http://localhost:" + dynamicPort.getNumber() + "/services/proxyWithWsdl", msg, null);
156         String resString = result.getPayloadAsString();
157         assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
158         assertTrue(resString.indexOf("<test xmlns=\"http://foo\"") != -1);
159    }
160 
161    @Test
162    public void testServerClientProxyWithWsdl2() throws Exception
163    {
164         final Latch latch = new Latch();
165         ((FunctionalTestComponent) getComponent("serverClientProxyWithWsdl2")).setEventCallback(new EventCallback()
166         {
167 
168             public void eventReceived(MuleEventContext context, Object component) throws Exception
169             {
170                 latch.countDown();
171             }
172         });
173 
174         String msg = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
175                      + "<soap:Body> <test xmlns=\"http://foo\"></test>" + "</soap:Body>" + "</soap:Envelope>";
176 
177         MuleClient client = new MuleClient(muleContext);
178         MuleMessage result = client.send("http://localhost:" + dynamicPort.getNumber() + "/services/proxyWithWsdl2", msg, null);
179         String resString = result.getPayloadAsString();
180         assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
181         assertTrue(resString.indexOf("<test xmlns=\"http://foo\"") != -1);
182    }
183 
184    @Test
185    public void testServerClientProxyWithTransform() throws Exception
186    {
187         String msg = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
188                      + "<soap:Body> <test xmlns=\"http://foo\"></test>" + "</soap:Body>" + "</soap:Envelope>";
189 
190         MuleClient client = new MuleClient(muleContext);
191         MuleMessage result = client.send("http://localhost:" + dynamicPort.getNumber() + "/services/proxyWithTransform", msg, null);
192         String resString = result.getPayloadAsString();
193         System.out.println(resString);
194         assertTrue(resString.indexOf("<transformed xmlns=\"http://foo\">") != -1);
195    }
196 
197    @Test
198    public void testProxyWithDatabinding() throws Exception
199    {
200         String msg = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
201             + "<soap:Body><greetMe xmlns=\"http://apache.org/hello_world_soap_http/types\"><requestType>Dan</requestType></greetMe>" +
202                     "</soap:Body>" + "</soap:Envelope>";
203 
204         MuleClient client = new MuleClient(muleContext);
205         MuleMessage result = client.send("http://localhost:" + dynamicPort.getNumber() + "/services/greeter-databinding-proxy", msg, null);
206         String resString = result.getPayloadAsString();
207         assertTrue(resString.indexOf("greetMeResponse") != -1);
208    }
209 
210    @Test
211    public void testProxyWithFault() throws Exception
212    {
213         String msg = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
214             + "<soap:Body><invalid xmlns=\"http://apache.org/hello_world_soap_http/types\"><requestType>Dan</requestType></invalid>" +
215                     "</soap:Body>" + "</soap:Envelope>";
216 
217         MuleClient client = new MuleClient(muleContext);
218         MuleMessage result = client.send("http://localhost:" + dynamicPort.getNumber() + "/services/greeter-proxy", msg, null);
219         String resString = result.getPayloadAsString();
220 
221         assertFalse("Status code should not be 'OK' when the proxied endpoint returns a fault",
222                     String.valueOf(HttpConstants.SC_OK).equals(result.getOutboundProperty("http.status")));
223 
224         assertTrue(resString.indexOf("Fault") != -1);
225    }
226 
227    @Test
228    public void testProxyWithIntermediateTransform() throws Exception
229    {
230         String msg = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
231             + "<soap:Body><greetMe xmlns=\"http://apache.org/hello_world_soap_http/types\"><requestType>Dan</requestType></greetMe>" +
232                     "</soap:Body>" + "</soap:Envelope>";
233 
234         MuleClient client = new MuleClient(muleContext);
235         MuleMessage result = client.send("http://localhost:" + dynamicPort.getNumber() + "/services/transform-proxy", msg, null);
236         String resString = result.getPayloadAsString();
237         assertTrue(resString.indexOf("greetMeResponse") != -1);
238    }
239 
240    @Test
241    public void testSoapActionRouting() throws Exception
242    {
243         String msg = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
244             + "<soap:Body> <test xmlns=\"http://foo\"></test>" + "</soap:Body>" + "</soap:Envelope>";
245 
246         Map<String, Object> props = new HashMap<String, Object>();
247         props.put("SOAPAction", "http://acme.com/transform");
248 
249         MuleClient client = new MuleClient(muleContext);
250         MuleMessage result = client.send("http://localhost:" + dynamicPort.getNumber() + "/services/routeBasedOnSoapAction", msg, props);
251         String resString = result.getPayloadAsString();
252         System.out.println(resString);
253         assertTrue(resString.indexOf("<transformed xmlns=\"http://foo\">") != -1);
254    }
255 
256    @Test
257    public void testOneWaySend() throws Exception
258    {
259         MuleClient client = new MuleClient(muleContext);
260         MuleMessage result = client.send("http://localhost:" + dynamicPort.getNumber() + "/services/oneway",
261             prepareOneWayTestMessage(), prepareOneWayTestProperties());
262         assertEquals("", result.getPayloadAsString());
263 
264         AsyncService component = (AsyncService) getComponent("asyncService");
265         assertTrue(component.getLatch().await(10000, TimeUnit.MILLISECONDS));
266    }
267 
268    @Test
269    public void testOneWayDispatch() throws Exception
270    {
271         new MuleClient(muleContext).dispatch("http://localhost:" + dynamicPort.getNumber() + "/services/oneway", prepareOneWayTestMessage(),
272             prepareOneWayTestProperties());
273 
274         AsyncService component = (AsyncService) getComponent("asyncService");
275         assertTrue(component.getLatch().await(10000, TimeUnit.MILLISECONDS));
276    }
277 
278    @Test
279    public void testOneWayDispatchWithException() throws Exception
280    {
281         MuleClient client = new MuleClient(muleContext);
282         client.dispatch("http://localhost:" + dynamicPort.getNumber() + "/services/onewayexception", prepareOneWayTestMessage(),
283             prepareOneWayTestProperties());
284 
285         MuleMessage resp = client.request("vm://exception", RECEIVE_TIMEOUT);
286         assertNotNull(resp);
287    }
288 
289    /**
290      * MULE-4549 ReversibleXMLStreamReader chokes on comments with ClassCastException
291      * @throws Exception
292      */
293    @Test
294    public void testProxyWithCommentInRequest() throws Exception
295    {
296         MuleClient client = new MuleClient(muleContext);
297         MuleMessage result = client.send("http://localhost:" + dynamicPort.getNumber() + "/services/envelope-proxy", msgWithComment, null);
298         String resString = result.getPayloadAsString();
299         assertTrue(resString.contains(doGoogleSearch));
300    }
301 
302    /**
303      * MULE-6188: ReversibleXMLStreamReader throw NPE after reset because current event is null.
304      * @throws Exception
305      */
306    @Test
307    public void testProxyEnvelopeWithXsltTransformation() throws Exception
308    {
309         MuleClient client = new MuleClient(muleContext);
310         MuleMessage result = client.send("http://localhost:" + dynamicPort.getNumber() + "/services/envelope-xslt-proxy", msg, null);
311         assertTrue(result.getPayloadAsString().contains(msg));
312    }
313 
314 
315    @Test
316    public void testProxyCDATA() throws Exception
317    {
318         String msg="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:sup=\"http://support.cxf.module.mule.org/\">\n" +
319                    "<soapenv:Header/>\n" +
320                    "<soapenv:Body>\n" +
321                    "<sup:invoke>\n" +
322                    "<soapenv:Envelope>\n" +
323                    "<soapenv:Header/>\n" +
324                    "<soapenv:Body>\n" +
325                    "<sup:invoke>\n" +
326                    "<Request>\n" +
327                    "<servicePayload><![CDATA[<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Header/><soapenv:Body><int:test/></soapenv:Body></soapenv:Envelope>]]></servicePayload>\n" +
328                    "</Request>\n" +
329                    "</sup:invoke>\n" +
330                    "</soapenv:Body>\n" +
331                    "</soapenv:Envelope>\n" +
332                    "</sup:invoke>\n" +
333                    "</soapenv:Body>\n" +
334                    "</soapenv:Envelope>";
335 
336         MuleClient client = new MuleClient(muleContext);
337         MuleMessage result = client.send("http://localhost:" + dynamicPort.getNumber() + "/services/CDATAService", msg, null);
338         assertNotNull(result);
339         assertTrue(result.getPayloadAsString().contains("![CDATA["));
340    }
341 
342    /** MULE-6159: Proxy service fails when WSDL has faults **/
343    @Test
344    public void testProxyWithSoapFault() throws Exception
345    {
346         MuleClient client = new MuleClient(muleContext);
347 
348         String proxyFaultMsg = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
349             + "<soap:Body><greetMe xmlns=\"http://apache.org/hello_world_fault/types\"><requestType>Dan</requestType></greetMe>" +
350                     "</soap:Body>" + "</soap:Envelope>";
351 
352         MuleMessage result = client.send("http://localhost:" + dynamicPort.getNumber() + "/proxyFault", proxyFaultMsg, null);
353         String resString = result.getPayloadAsString();
354         assertTrue(resString.contains("ERROR"));
355    }
356 
357     protected String prepareOneWayTestMessage()
358     {
359         return "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "<soap:Body>"
360                + "<ns:send xmlns:ns=\"http://testmodels.cxf.module.mule.org/\"><text>hello</text></ns:send>"
361                + "</soap:Body>" + "</soap:Envelope>";
362     }
363     
364     protected Map prepareOneWayTestProperties()
365     {
366         Map<String, Object> props = new HashMap<String, Object>();
367         props.put("SOAPAction", "http://acme.com/oneway");
368         return props;
369     }
370 
371 }