View Javadoc

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