View Javadoc

1   /*
2    * $Id: ProxyTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
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> <test xmlns=\"http://foo\"></test>" + "</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          System.out.println(resString);
80          assertTrue(resString.indexOf("<test xmlns=\"http://foo\"") != -1);
81      }
82  
83      public void testServerClientProxyWithWsdl() throws Exception
84      {
85          final Latch latch = new Latch();
86          ((FunctionalTestComponent) getComponent("serverClientProxyWithWsdl")).setEventCallback(new EventCallback()
87          {
88  
89              public void eventReceived(MuleEventContext context, Object component) throws Exception
90              {
91                  latch.countDown();
92              }
93          });
94  
95          String msg = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
96                       + "<soap:Body> <test xmlns=\"http://foo\"></test>" + "</soap:Body>" + "</soap:Envelope>";
97  
98          MuleClient client = new MuleClient(muleContext);
99          MuleMessage result = client.send("http://localhost:63081/services/proxyWithWsdl", msg, null);
100         String resString = result.getPayloadAsString();
101         assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
102         assertTrue(resString.indexOf("<test xmlns=\"http://foo\"") != -1);
103     }
104 
105     public void testServerClientProxyWithWsdl2() throws Exception
106     {
107         final Latch latch = new Latch();
108         ((FunctionalTestComponent) getComponent("serverClientProxyWithWsdl2")).setEventCallback(new EventCallback()
109         {
110 
111             public void eventReceived(MuleEventContext context, Object component) throws Exception
112             {
113                 latch.countDown();
114             }
115         });
116 
117         String msg = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
118                      + "<soap:Body> <test xmlns=\"http://foo\"></test>" + "</soap:Body>" + "</soap:Envelope>";
119 
120         MuleClient client = new MuleClient(muleContext);
121         MuleMessage result = client.send("http://localhost:63081/services/proxyWithWsdl2", msg, null);
122         String resString = result.getPayloadAsString();
123         assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
124         assertTrue(resString.indexOf("<test xmlns=\"http://foo\"") != -1);
125     }
126 
127     public void testServerClientProxyWithTransform() throws Exception
128     {
129         String msg = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
130                      + "<soap:Body> <test xmlns=\"http://foo\"></test>" + "</soap:Body>" + "</soap:Envelope>";
131 
132         MuleClient client = new MuleClient(muleContext);
133         MuleMessage result = client.send("http://localhost:63081/services/proxyWithTransform", msg, null);
134         String resString = result.getPayloadAsString();
135         System.out.println(resString);
136         assertTrue(resString.indexOf("<transformed xmlns=\"http://foo\">") != -1);
137     }
138 
139     public void testProxyWithDatabinding() throws Exception 
140     {
141         String msg = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
142             + "<soap:Body><greetMe xmlns=\"http://apache.org/hello_world_soap_http/types\"><requestType>Dan</requestType></greetMe>" +
143                     "</soap:Body>" + "</soap:Envelope>";
144 
145         MuleClient client = new MuleClient(muleContext);
146         MuleMessage result = client.send("http://localhost:63081/services/greeter-databinding-proxy", msg, null);
147         String resString = result.getPayloadAsString();
148         assertTrue(resString.indexOf("greetMeResponse") != -1);
149     }
150 
151     public void testProxyWithFault() throws Exception 
152     {
153         String msg = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
154             + "<soap:Body><invalid xmlns=\"http://apache.org/hello_world_soap_http/types\"><requestType>Dan</requestType></invalid>" +
155                     "</soap:Body>" + "</soap:Envelope>";
156 
157         MuleClient client = new MuleClient(muleContext);
158         MuleMessage result = client.send("http://localhost:63081/services/greeter-proxy", msg, null);
159         String resString = result.getPayloadAsString();
160 
161         assertFalse("Status code should not be 'OK' when the proxied endpoint returns a fault",
162                     String.valueOf(HttpConstants.SC_OK).equals(result.getOutboundProperty("http.status")));
163 
164         assertTrue(resString.indexOf("Fault") != -1);
165     }
166 
167     public void testProxyWithIntermediateTransform() throws Exception 
168     {
169         String msg = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
170             + "<soap:Body><greetMe xmlns=\"http://apache.org/hello_world_soap_http/types\"><requestType>Dan</requestType></greetMe>" +
171                     "</soap:Body>" + "</soap:Envelope>";
172 
173         MuleClient client = new MuleClient(muleContext);
174         MuleMessage result = client.send("http://localhost:63081/services/transform-proxy", msg, null);
175         String resString = result.getPayloadAsString();
176         assertTrue(resString.indexOf("greetMeResponse") != -1);
177     }
178 
179     public void testSoapActionRouting() throws Exception 
180     {
181         String msg = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
182             + "<soap:Body> <test xmlns=\"http://foo\"></test>" + "</soap:Body>" + "</soap:Envelope>";
183 
184         Map<String, Object> props = new HashMap<String, Object>();
185         props.put("SOAPAction", "http://acme.com/transform");
186 
187         MuleClient client = new MuleClient(muleContext);
188         MuleMessage result = client.send("http://localhost:63081/services/routeBasedOnSoapAction", msg, props);
189         String resString = result.getPayloadAsString();
190         System.out.println(resString);
191         assertTrue(resString.indexOf("<transformed xmlns=\"http://foo\">") != -1);
192     }
193 
194     public void testOneWaySend() throws Exception
195     {
196         MuleClient client = new MuleClient(muleContext);
197         MuleMessage result = client.send("http://localhost:63081/services/oneway",
198             prepareOneWayTestMessage(), prepareOneWayTestProperties());
199         assertEquals("", result.getPayloadAsString());
200 
201         AsyncService component = (AsyncService) getComponent("asyncService");
202         assertTrue(component.getLatch().await(10000, TimeUnit.MILLISECONDS));
203     }
204 
205     public void testOneWayDispatch() throws Exception
206     {
207         new MuleClient(muleContext).dispatch("http://localhost:63081/services/oneway", prepareOneWayTestMessage(),
208             prepareOneWayTestProperties());
209 
210         AsyncService component = (AsyncService) getComponent("asyncService");
211         assertTrue(component.getLatch().await(10000, TimeUnit.MILLISECONDS));
212     }
213 
214     /**
215      * MULE-4549 ReversibleXMLStreamReader chokes on comments with ClassCastException
216      * @throws Exception
217      */
218     public void testProxyWithCommentInRequest() throws Exception
219     {
220         MuleClient client = new MuleClient(muleContext);
221         MuleMessage result = client.send("http://localhost:63081/services/envelope-proxy", msgWithComment, null);
222         String resString = result.getPayloadAsString();
223         assertTrue(resString.contains(doGoogleSearch));
224     }
225     
226     protected String prepareOneWayTestMessage()
227     {
228         return "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "<soap:Body>"
229                + "<send xmlns=\"http://testmodels.cxf.transport.mule.org\"><text>hello</text></send>"
230                + "</soap:Body>" + "</soap:Envelope>";
231     }
232 
233     protected Map prepareOneWayTestProperties()
234     {
235         Map<String, Object> props = new HashMap<String, Object>();
236         props.put("SOAPAction", "http://acme.com/oneway");
237         return props;
238     }
239     
240     protected String getConfigResources()
241     {
242         return "proxy-conf.xml";
243     }
244 
245 }