View Javadoc

1   /*
2    * $Id: AbstractSoapFunctionalTestCase.java 20321 2010-11-24 15:21:24Z dfeist $
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;
12  
13  import org.mule.api.MuleMessage;
14  import org.mule.api.transport.DispatchException;
15  import org.mule.module.client.MuleClient;
16  import org.mule.tck.DynamicPortTestCase;
17  import org.mule.tck.testmodels.services.Person;
18  import org.mule.transport.NullPayload;
19  import org.mule.transport.http.HttpConnector;
20  import org.mule.transport.http.HttpConstants;
21  
22  import java.util.ArrayList;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  public abstract class AbstractSoapFunctionalTestCase extends DynamicPortTestCase
28  {
29  
30      protected abstract String getRequestResponseEndpoint();
31  
32      protected abstract String getReceiveEndpoint();
33  
34      protected abstract String getReceiveComplexEndpoint();
35  
36      protected abstract String getSendReceiveComplexEndpoint1();
37  
38      protected abstract String getSendReceiveComplexEndpoint2();
39  
40      protected abstract String getReceiveComplexCollectionEndpoint();
41  
42      protected abstract String getDispatchAsyncComplexEndpoint1();
43  
44      protected abstract String getDispatchAsyncComplexEndpoint2();
45  
46      protected abstract String getTestExceptionEndpoint();
47  
48      protected abstract String getWsdlEndpoint();
49  
50      public void testRequestResponse() throws Throwable
51      {
52          MuleClient client = new MuleClient(muleContext);
53          List<Object> results = new ArrayList<Object>();
54          int number = 1;
55          Map<String, Object> props = new HashMap<String, Object>();
56          for (int i = 0; i < number; i++)
57          {
58              props.put("X-Message-Number", String.valueOf(i));
59              MuleMessage msg = client.send(getRequestResponseEndpoint(), "Message " + i, props);
60              assertNotNull(msg);
61              results.add(msg.getPayload());
62          }
63  
64          assertEquals(number, results.size());
65          for (int i = 0; i < number; i++)
66          {
67              assertEquals("Message " + i, results.get(i).toString());
68          }
69      }
70  
71      public void testRequest() throws Throwable
72      {
73          MuleClient client = new MuleClient(muleContext);
74          MuleMessage result = client.request(getReceiveEndpoint(), 0);
75          assertNotNull(result);
76          assertNotNull(result.getPayload());
77          assertTrue(result.getPayload().toString().length() > 0);
78      }
79  
80      public void testReceiveComplex() throws Throwable
81      {
82          MuleClient client = new MuleClient(muleContext);
83          MuleMessage result = client.request(getReceiveComplexEndpoint(), 0);
84          assertNotNull(result);
85          assertTrue(result.getPayload() instanceof Person);
86          assertEquals("Fred", ((Person)result.getPayload()).getFirstName());
87          assertEquals("Flintstone", ((Person)result.getPayload()).getLastName());
88  
89          result = client.request(getReceiveComplexEndpoint(), 0);
90          assertNotNull(result);
91          assertTrue(result.getPayload() instanceof Person);
92          assertEquals("Fred", ((Person)result.getPayload()).getFirstName());
93          assertEquals("Flintstone", ((Person)result.getPayload()).getLastName());
94      }
95  
96      public void testSendAndReceiveComplex() throws Throwable
97      {
98          MuleClient client = new MuleClient(muleContext);
99          MuleMessage result = client.send(getSendReceiveComplexEndpoint1(), new Person("Dino", "Flintstone"), null);
100         assertEquals(NullPayload.getInstance(), result.getPayload());
101 
102         result = client.request(getSendReceiveComplexEndpoint2(), 0);
103         assertNotNull(result);
104         
105         assertTrue(result.getPayload() instanceof Person);
106         assertEquals("Dino", ((Person)result.getPayload()).getFirstName());
107         assertEquals("Flintstone", ((Person)result.getPayload()).getLastName());
108     }
109 
110     public void testReceiveComplexCollection() throws Throwable
111     {
112         MuleClient client = new MuleClient(muleContext);
113         MuleMessage result = client.request(getReceiveComplexCollectionEndpoint(), 0);
114         assertNotNull(result);
115         assertTrue(result.getPayload() instanceof Person[]);
116         assertEquals(3, ((Person[])result.getPayload()).length);
117     }
118 
119     public void testDispatchAsyncComplex() throws Throwable
120     {
121         MuleClient client = new MuleClient(muleContext);
122 
123         //TODO MULE-4951 Dispatch no longer works (fails with class cast exception, probably need to configure AXIS.OneWay)
124         //switching to send() does work
125         client.send(getDispatchAsyncComplexEndpoint1(), new Person("Betty", "Rubble"), null);
126 
127         // lets get our newly added person
128         MuleMessage result = client.request(getDispatchAsyncComplexEndpoint2(), RECEIVE_TIMEOUT);
129         assertNotNull(result);
130         assertTrue("Did not receive a Person but: " + result.getPayload().getClass(),
131             result.getPayload() instanceof Person);
132         assertEquals("Betty", ((Person)result.getPayload()).getFirstName());
133         assertEquals("Rubble", ((Person)result.getPayload()).getLastName());
134     }
135 
136     public void testException() throws Exception
137     {
138         MuleClient client = new MuleClient(muleContext);
139         
140         try
141         {
142             client.send(getTestExceptionEndpoint(), new Person("Ross", "Mason"), null);
143             fail("A nested Fault should have been raised");
144         }
145         catch (DispatchException e)
146         {
147             assertTrue(e.getCause() instanceof Exception);
148         }
149     }
150 
151     public void testLocationUrlInWSDL() throws Exception
152     {
153         Map<String, Object> props = new HashMap<String, Object>();
154         props.put(HttpConnector.HTTP_METHOD_PROPERTY, "GET");
155         MuleClient client = new MuleClient(muleContext);
156         MuleMessage result = client.send(getWsdlEndpoint(), null, props);
157         assertNotNull(result);
158         if (logger.isDebugEnabled())
159         {
160             logger.debug(result.getPayloadAsString());
161         }
162 
163         String location = getWsdlEndpoint();
164         location = location.substring(0, location.length() - 5);
165         if (location.endsWith("/"))
166         {
167             location = location.substring(0, location.length() - 1);
168         }
169 
170         if (logger.isDebugEnabled())
171         {
172             logger.debug(result.getPayloadAsString());
173         }
174         System.out.println(result.getPayloadAsString());
175         if (result.getPayloadAsString().indexOf("location=\"" + location) == -1)
176         {
177             assertTrue(result.getPayloadAsString().indexOf("location='" + location) > -1);
178         }
179         else
180         {
181             assertTrue(result.getPayloadAsString().indexOf("location=\"" + location) > -1);
182         }
183 
184         assertTrue(result.getInboundProperty(HttpConstants.HEADER_CONTENT_TYPE, "").startsWith("text/xml"));
185     }
186 
187 }