View Javadoc

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