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.transport.soap.axis.style;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.api.config.MuleProperties;
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.transport.soap.axis.NamedParameter;
15  import org.mule.transport.soap.axis.SoapMethod;
16  import org.mule.util.StringUtils;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import javax.xml.namespace.QName;
22  import javax.xml.rpc.ParameterMode;
23  
24  import org.apache.axis.client.Call;
25  import org.apache.axis.client.Service;
26  import org.junit.Rule;
27  import org.junit.Test;
28  
29  import static org.junit.Assert.assertEquals;
30  import static org.junit.Assert.assertNotNull;
31  
32  public class AxisMessageStyleServiceTestCase extends FunctionalTestCase
33  {
34      private static String expectedResult = "TEST RESPONSE";
35  
36      @Rule
37      public DynamicPort dynamicPort = new DynamicPort("port1");
38  
39      @Override
40      public String getConfigResources()
41      {
42          return "style/axis-mule-message-config.xml";
43      }
44  
45      protected String getServiceEndpoint()
46      {
47          return "http://localhost:" + dynamicPort.getNumber() + "/ServiceEntryPoint";
48      }
49  
50      @Test
51      public void testDocumentWithNamespace() throws Exception
52      {
53          doSoapRequest(new QName("http://muleumo.org", "document"), "axis:" + getServiceEndpoint(), false,
54              false, false);
55      }
56  
57      @Test
58      public void testDocumentWithQName() throws Exception
59      {
60          doSoapRequest(new QName("http://muleumo.org", "document"), "axis:" + getServiceEndpoint(), false,
61              false, true);
62      }
63  
64      @Test
65      public void testDocumentWithAxisApi() throws Exception
66      {
67          doSoapRequest(new QName("http://muleumo.org", "document"), getServiceEndpoint(), true, false, false);
68      }
69  
70      @Test
71      public void testDocumentWithSoapMethod() throws Exception
72      {
73          doSoapRequest(new QName("http://muleumo.org", "document"), "axis:" + getServiceEndpoint(), false,
74              true, false);
75      }
76  
77      @Test
78      public void testElementArrayWithSoapMethod() throws Exception
79      {
80          doSoapRequest(new QName("http://muleumo.org", "elementArray"), "axis:" + getServiceEndpoint(), false,
81              true, false);
82      }
83  
84      @Test
85      public void testElementArrayWithNamesapce() throws Exception
86      {
87          doSoapRequest(new QName("http://muleumo.org", "elementArray"), "axis:" + getServiceEndpoint(), false,
88              false, false);
89      }
90  
91      @Test
92      public void testElementArrayWithQName() throws Exception
93      {
94          doSoapRequest(new QName("http://muleumo.org", "elementArray"), "axis:" + getServiceEndpoint(), false,
95              false, true);
96      }
97  
98      @Test
99      public void testElementArrayWithAxisApi() throws Exception
100     {
101         doSoapRequest(new QName("http://muleumo.org", "elementArray"), getServiceEndpoint(), true, false,
102             false);
103     }
104 
105     @Test
106     public void testSoapBodyElementWithSoapMethod() throws Exception
107     {
108         doSoapRequest(new QName("http://muleumo.org", "soapBodyElement"), "axis:" + getServiceEndpoint(),
109             false, true, false);
110     }
111 
112     @Test
113     public void testSoapBodyElementWithNamesapce() throws Exception
114     {
115         doSoapRequest(new QName("http://muleumo.org", "soapBodyElement"), "axis:" + getServiceEndpoint(),
116             false, false, false);
117     }
118 
119     @Test
120     public void testSoapBodyElementWithQName() throws Exception
121     {
122         doSoapRequest(new QName("http://muleumo.org", "soapBodyElement"), "axis:" + getServiceEndpoint(),
123             false, false, true);
124     }
125 
126     @Test
127     public void testSoapBodyElementWithAxisApi() throws Exception
128     {
129         doSoapRequest(new QName("http://muleumo.org", "soapBodyElement"), getServiceEndpoint(), true, false,
130             false);
131     }
132 
133     // TODO does work , complains about generated namespace...TestNS1
134     // @Test
135     //public void testSoapRequestResponseWithSoapMethod() throws Exception {
136     // doSoapRequest(new QName("http://muleumo.org", "soapRequestResponse"), "axis:"
137     // + getServiceEndpoint(), false, true, false);
138     // }
139     //
140     // @Test
141     //public void testSoapRequestResponseWithNamesapce() throws Exception {
142     // doSoapRequest(new QName("http://muleumo.org", "soapRequestResponse"), "axis:"
143     // + getServiceEndpoint(), false, false, false);
144     // }
145     //
146     // @Test
147     //public void testSoapRequestResponseWithQName() throws Exception {
148     // doSoapRequest(new QName("http://muleumo.org", "soapRequestResponse"), "axis:"
149     // + getServiceEndpoint(), false, false, true);
150     // }
151 
152     @Test
153     public void testSoapRequestResponseWithAxisApi() throws Exception
154     {
155         doSoapRequest(new QName("http://muleumo.org", "soapRequestResponse"), getServiceEndpoint(), true,
156             false, false);
157     }
158 
159     protected void doSoapRequest(QName method,
160                                  String endpoint,
161                                  boolean useAxisApi,
162                                  boolean useSoapMethod,
163                                  boolean useQNameMethod) throws Exception
164     {
165         if (useAxisApi)
166         {
167             Service service = new Service();
168             Call call = (Call)service.createCall();
169             call.setTargetEndpointAddress(new java.net.URL(endpoint));
170             call.setOperationName(method);
171             String ret = (String)call.invoke(new Object[]{expectedResult});
172             assertNotNull(ret);
173             assertEquals(ret, expectedResult);
174         }
175         else
176         {
177 
178             // Now try with the MuleClient
179             MuleClient client = new MuleClient(muleContext);
180             Map props = new HashMap();
181             if (useSoapMethod)
182             {
183                 SoapMethod soapMethod = new SoapMethod(method);
184                 soapMethod.addNamedParameter(new QName(method.getNamespaceURI(), method.getLocalPart()),
185                     NamedParameter.XSD_STRING, ParameterMode.IN);
186                 props.put(MuleProperties.MULE_METHOD_PROPERTY, soapMethod);
187             }
188             else if (useQNameMethod)
189             {
190                 props.put(MuleProperties.MULE_METHOD_PROPERTY, method);
191             }
192             else
193             {
194                 endpoint += "?method=" + method.getLocalPart();
195                 if (StringUtils.isNotBlank(method.getNamespaceURI()))
196                 {
197                     props.put("methodNamespace", method.getNamespaceURI());
198                 }
199             }
200 
201             MuleMessage result = client.send(endpoint, expectedResult, props);
202             assertNotNull(result);
203             assertEquals(expectedResult, result.getPayloadAsString());
204         }
205     }
206 
207 }