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