View Javadoc

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