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