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.construct;
8   
9   import org.mule.api.MuleException;
10  import org.mule.api.MuleMessage;
11  import org.mule.module.client.MuleClient;
12  import org.mule.tck.junit4.FunctionalTestCase;
13  import org.mule.tck.junit4.rule.DynamicPort;
14  import org.mule.test.integration.tck.WeatherForecaster;
15  
16  import org.junit.ClassRule;
17  import org.junit.Rule;
18  import org.junit.Test;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertNotNull;
22  import static org.junit.Assert.assertTrue;
23  
24  public class WSProxyTestCase extends FunctionalTestCase
25  {
26      @Rule
27      public DynamicPort port1 = new DynamicPort("port1");
28  
29      @Rule
30      public DynamicPort port2 = new DynamicPort("port2");
31  
32      @Rule
33      public DynamicPort port3 = new DynamicPort("port3");
34  
35      private MuleClient muleClient;
36  
37      @Override
38      protected String getConfigResources()
39      {
40          return "org/mule/test/integration/construct/ws-proxy-config.xml";
41      }
42  
43      @Override
44      protected void doSetUp() throws Exception
45      {
46          super.doSetUp();
47          muleClient = new MuleClient(muleContext);
48      }
49  
50      @Test
51      public void testDynamicWsdl() throws Exception
52      {
53          testWsdlAndWebServiceRequests(0);
54      }
55  
56      @Test
57      public void testFileContentsWsdl() throws Exception
58      {
59          testWsdlAndWebServiceRequests(1);
60      }
61  
62      @Test
63      public void testStaticUriWsdl() throws Exception
64      {
65          testWsdlAndWebServiceRequests(2);
66      }
67  
68      @Test
69      public void testGlobalEndpoints() throws Exception
70      {
71          testWsdlAndWebServiceRequests(3);
72      }
73  
74      @Test
75      public void testTransformers() throws Exception
76      {
77          testWsdlAndWebServiceRequests(4);
78      }
79  
80      @Test
81      public void testExceptionStrategy() throws Exception
82      {
83          testWsdlAndWebServiceRequests(5);
84      }
85  
86      @Test
87      public void testInheritance() throws Exception
88      {
89          testWsdlAndWebServiceRequests(6);
90      }
91  
92      @Test
93      public void testEndpointChildren() throws Exception
94      {
95          testWsdlAndWebServiceRequests(7);
96      }
97  
98      @Test
99      public void testInheritanceAndEndpointChildren() throws Exception
100     {
101         testWsdlAndWebServiceRequests(8);
102     }
103 
104     @Test
105     public void testExpressionEndpoint() throws Exception
106     {
107         testWsdlAndWebServiceRequests(9);
108     }
109 
110     @Test
111     public void testResponsePropertiesPropagation() throws Exception
112     {
113         MuleMessage reply = performWebServiceRequest(10);
114         // Test if Content-Encoding is present as an inbound property because HTTP response headers are translated
115         // into inbound properties when the response is transformed into the MuleMessage.
116         assertNotNull(reply.getInboundProperty("Content-Encoding"));
117         assertEquals(reply.getInboundProperty("Content-Encoding"), "gzip");
118     }
119 
120     private void testWsdlAndWebServiceRequests(int proxyId) throws Exception
121     {
122         testWsdlRequest(proxyId);
123         testWebServiceRequest(proxyId);
124     }
125 
126     private void testWsdlRequest(int proxyId) throws Exception
127     {
128         final String wsdl = muleClient.request(
129                 "http://localhost:" + port1.getNumber() + "/weather-forecast/" + proxyId + "?wsdl",
130                 getTestTimeoutSecs() * 1000L).getPayloadAsString();
131         assertTrue(wsdl.contains("GetWeatherByZipCode"));
132     }
133 
134     private void testWebServiceRequest(int proxyId) throws Exception
135     {
136         final String weatherForecast = performWebServiceRequest(proxyId).getPayloadAsString();
137         assertEquals(new WeatherForecaster().getByZipCode("95050"), weatherForecast);
138     }
139 
140     /**
141      * Performs a request to the web-service.
142      * @param proxyId The proxy id.
143      * @return The {@link org.mule.api.MuleMessage} with the web-service response.
144      * @throws org.mule.api.MuleException If there is a problem with the request.
145      */
146     private MuleMessage performWebServiceRequest(final int proxyId) throws MuleException
147     {
148         return muleClient.send("wsdl-cxf:http://localhost:" + port1.getNumber() + "/weather-forecast/" + proxyId
149                                + "?wsdl&method=GetWeatherByZipCode", "95050", null, getTestTimeoutSecs() * 1000);
150     }
151 }