View Javadoc

1   /*
2    * $Id: ProxyTestCase.java 20576 2010-12-09 21:37:23Z dzapata $
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.module.cxf;
12  
13  import org.mule.api.MuleEventContext;
14  import org.mule.api.MuleMessage;
15  import org.mule.module.client.MuleClient;
16  import org.mule.module.cxf.testmodels.AsyncService;
17  import org.mule.tck.FunctionalTestCase;
18  import org.mule.tck.functional.EventCallback;
19  import org.mule.tck.functional.FunctionalTestComponent;
20  import org.mule.transport.http.HttpConstants;
21  import org.mule.util.concurrent.Latch;
22  
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
27  
28  public class ProxyTestCase extends FunctionalTestCase
29  {
30      String msg = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
31          + "<soap:Body><test xmlns=\"http://foo\"> foo </test>" + "</soap:Body>" + "</soap:Envelope>";
32  
33      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\">";
34  
35      String msgWithComment = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
36          + "<!-- comment 1 -->"
37          + "<soap:Header>"
38          + "<!-- comment 2 -->"
39          + "</soap:Header>"
40          + "<!-- comment 3 -->"
41          + "<soap:Body>"
42          + "<!-- comment 4 -->"
43          + doGoogleSearch
44          + "<!-- this comment breaks it -->"
45          + "<key>1</key>"
46          + "<!-- comment 5 -->"
47          + "<q>a</q>"
48          + "<start>0</start>"
49          + "<maxResults>1</maxResults>"
50          + "<filter>false</filter>"
51          + "<restrict>a</restrict>"
52          + "<safeSearch>true</safeSearch>"
53          + "<lr>a</lr>"
54          + "<ie>b</ie>"
55          + "<oe>c</oe>"
56          + "</urn:doGoogleSearch>"
57          + "<!-- comment 6 -->"
58          + "</soap:Body>"
59          + "<!-- comment 7 -->"
60          + "</soap:Envelope>";
61  
62      public void testServerWithEcho() throws Exception
63      {
64          MuleClient client = new MuleClient(muleContext);
65          MuleMessage result = client.send("http://localhost:63081/services/Echo", msg, null);
66          String resString = result.getPayloadAsString();
67  //        System.out.println(resString);
68          assertTrue(resString.indexOf("<test xmlns=\"http://foo\"> foo </test>") != -1);
69      }
70  
71      public void testServerClientProxy() throws Exception
72      {
73          String msg = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
74                       + "<soap:Body> <foo xmlns=\"http://foo\"></foo>" + "</soap:Body>" + "</soap:Envelope>";
75  
76          MuleClient client = new MuleClient(muleContext);
77          MuleMessage result = client.send("http://localhost:63081/services/proxy", msg, null);
78          String resString = result.getPayloadAsString();
79  
80          assertTrue(resString.indexOf("<foo xmlns=\"http://foo\"") != -1);
81      }
82  
83      public void testProxyBodyValidation() throws Exception
84      {
85          doTestProxyValidation("http://localhost:63081/services/proxyBodyWithValidation");
86      }
87  
88      public void testProxyEnvelopeValidation() throws Exception
89      {
90          doTestProxyValidation("http://localhost:63081/services/proxyEnvelopeWithValidation");
91      }
92      
93      public void doTestProxyValidation(String url) throws Exception
94      {
95          MuleClient client = new MuleClient(muleContext);
96          MuleMessage result = client.send(url, msg, null);
97          String resString = result.getPayloadAsString();
98          assertTrue(resString.indexOf("Schema validation error on message") != -1);
99          
100         String valid = 
101             "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
102             + "<soap:Body> " +
103                     "<echo xmlns=\"http://www.muleumo.org\">" +
104                     "  <echo>test</echo>" +
105                     "</echo>" 
106             + "</soap:Body>" 
107             + "</soap:Envelope>";
108         result = client.send(url, valid, null);
109         resString = result.getPayloadAsString();
110         assertTrue(resString.contains("<echoResponse xmlns=\"http://www.muleumo.org\">"));
111     }
112     
113     public void testServerClientProxyWithWsdl() throws Exception
114     {
115         final Latch latch = new Latch();
116         ((FunctionalTestComponent) getComponent("serverClientProxyWithWsdl")).setEventCallback(new EventCallback()
117         {
118 
119             public void eventReceived(MuleEventContext context, Object component) throws Exception
120             {
121                 latch.countDown();
122             }
123         });
124 
125         String msg = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
126                      + "<soap:Body> <test xmlns=\"http://foo\"></test>" + "</soap:Body>" + "</soap:Envelope>";
127 
128         MuleClient client = new MuleClient(muleContext);
129         MuleMessage result = client.send("http://localhost:63081/services/proxyWithWsdl", msg, null);
130         String resString = result.getPayloadAsString();
131         assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
132         assertTrue(resString.indexOf("<test xmlns=\"http://foo\"") != -1);
133     }
134 
135     public void testServerClientProxyWithWsdl2() throws Exception
136     {
137         final Latch latch = new Latch();
138         ((FunctionalTestComponent) getComponent("serverClientProxyWithWsdl2")).setEventCallback(new EventCallback()
139         {
140 
141             public void eventReceived(MuleEventContext context, Object component) throws Exception
142             {
143                 latch.countDown();
144             }
145         });
146 
147         String msg = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
148                      + "<soap:Body> <test xmlns=\"http://foo\"></test>" + "</soap:Body>" + "</soap:Envelope>";
149 
150         MuleClient client = new MuleClient(muleContext);
151         MuleMessage result = client.send("http://localhost:63081/services/proxyWithWsdl2", msg, null);
152         String resString = result.getPayloadAsString();
153         assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
154         assertTrue(resString.indexOf("<test xmlns=\"http://foo\"") != -1);
155     }
156 
157     public void testServerClientProxyWithTransform() throws Exception
158     {
159         String msg = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
160                      + "<soap:Body> <test xmlns=\"http://foo\"></test>" + "</soap:Body>" + "</soap:Envelope>";
161 
162         MuleClient client = new MuleClient(muleContext);
163         MuleMessage result = client.send("http://localhost:63081/services/proxyWithTransform", msg, null);
164         String resString = result.getPayloadAsString();
165         System.out.println(resString);
166         assertTrue(resString.indexOf("<transformed xmlns=\"http://foo\">") != -1);
167     }
168 
169     public void testProxyWithDatabinding() throws Exception 
170     {
171         String msg = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
172             + "<soap:Body><greetMe xmlns=\"http://apache.org/hello_world_soap_http/types\"><requestType>Dan</requestType></greetMe>" +
173                     "</soap:Body>" + "</soap:Envelope>";
174 
175         MuleClient client = new MuleClient(muleContext);
176         MuleMessage result = client.send("http://localhost:63081/services/greeter-databinding-proxy", msg, null);
177         String resString = result.getPayloadAsString();
178         assertTrue(resString.indexOf("greetMeResponse") != -1);
179     }
180 
181     public void testProxyWithFault() throws Exception 
182     {
183         String msg = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
184             + "<soap:Body><invalid xmlns=\"http://apache.org/hello_world_soap_http/types\"><requestType>Dan</requestType></invalid>" +
185                     "</soap:Body>" + "</soap:Envelope>";
186 
187         MuleClient client = new MuleClient(muleContext);
188         MuleMessage result = client.send("http://localhost:63081/services/greeter-proxy", msg, null);
189         String resString = result.getPayloadAsString();
190 
191         assertFalse("Status code should not be 'OK' when the proxied endpoint returns a fault",
192                     String.valueOf(HttpConstants.SC_OK).equals(result.getOutboundProperty("http.status")));
193 
194         assertTrue(resString.indexOf("Fault") != -1);
195     }
196 
197     public void testProxyWithIntermediateTransform() throws Exception 
198     {
199         String msg = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
200             + "<soap:Body><greetMe xmlns=\"http://apache.org/hello_world_soap_http/types\"><requestType>Dan</requestType></greetMe>" +
201                     "</soap:Body>" + "</soap:Envelope>";
202 
203         MuleClient client = new MuleClient(muleContext);
204         MuleMessage result = client.send("http://localhost:63081/services/transform-proxy", msg, null);
205         String resString = result.getPayloadAsString();
206         assertTrue(resString.indexOf("greetMeResponse") != -1);
207     }
208 
209     public void testSoapActionRouting() throws Exception 
210     {
211         String msg = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
212             + "<soap:Body> <test xmlns=\"http://foo\"></test>" + "</soap:Body>" + "</soap:Envelope>";
213 
214         Map<String, Object> props = new HashMap<String, Object>();
215         props.put("SOAPAction", "http://acme.com/transform");
216 
217         MuleClient client = new MuleClient(muleContext);
218         MuleMessage result = client.send("http://localhost:63081/services/routeBasedOnSoapAction", msg, props);
219         String resString = result.getPayloadAsString();
220         System.out.println(resString);
221         assertTrue(resString.indexOf("<transformed xmlns=\"http://foo\">") != -1);
222     }
223 
224     public void testOneWaySend() throws Exception
225     {
226         MuleClient client = new MuleClient(muleContext);
227         MuleMessage result = client.send("http://localhost:63081/services/oneway",
228             prepareOneWayTestMessage(), prepareOneWayTestProperties());
229         assertEquals("", result.getPayloadAsString());
230 
231         AsyncService component = (AsyncService) getComponent("asyncService");
232         assertTrue(component.getLatch().await(10000, TimeUnit.MILLISECONDS));
233     }
234 
235     public void testOneWayDispatch() throws Exception
236     {
237         new MuleClient(muleContext).dispatch("http://localhost:63081/services/oneway", prepareOneWayTestMessage(),
238             prepareOneWayTestProperties());
239 
240         AsyncService component = (AsyncService) getComponent("asyncService");
241         assertTrue(component.getLatch().await(10000, TimeUnit.MILLISECONDS));
242     }
243 
244     /**
245      * MULE-4549 ReversibleXMLStreamReader chokes on comments with ClassCastException
246      * @throws Exception
247      */
248     public void testProxyWithCommentInRequest() throws Exception
249     {
250         MuleClient client = new MuleClient(muleContext);
251         MuleMessage result = client.send("http://localhost:63081/services/envelope-proxy", msgWithComment, null);
252         String resString = result.getPayloadAsString();
253         assertTrue(resString.contains(doGoogleSearch));
254     }
255     
256     protected String prepareOneWayTestMessage()
257     {
258         return "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "<soap:Body>"
259                + "<ns:send xmlns:ns=\"http://testmodels.cxf.module.mule.org/\"><text>hello</text></ns:send>"
260                + "</soap:Body>" + "</soap:Envelope>";
261     }
262 
263     protected Map prepareOneWayTestProperties()
264     {
265         Map<String, Object> props = new HashMap<String, Object>();
266         props.put("SOAPAction", "http://acme.com/oneway");
267         return props;
268     }
269     
270     protected String getConfigResources()
271     {
272         return "proxy-conf.xml";
273     }
274 
275 }