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