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.module.cxf.issues;
8   
9   import static org.junit.Assert.assertEquals;
10  import static org.junit.Assert.assertTrue;
11  
12  import org.mule.DefaultMuleMessage;
13  import org.mule.api.MuleEventContext;
14  import org.mule.api.MuleException;
15  import org.mule.api.MuleMessage;
16  import org.mule.api.transport.PropertyScope;
17  import org.mule.module.client.MuleClient;
18  import org.mule.tck.functional.EventCallback;
19  import org.mule.tck.functional.FunctionalTestComponent;
20  import org.mule.tck.junit4.FunctionalTestCase;
21  import org.mule.tck.junit4.rule.DynamicPort;
22  import org.mule.util.concurrent.Latch;
23  
24  import javax.xml.namespace.QName;
25  
26  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
27  
28  import org.junit.Rule;
29  import org.junit.Test;
30  
31  public class ProxyMule6829TestCase extends FunctionalTestCase
32  {
33  
34      @Rule
35      public DynamicPort dynamicPort = new DynamicPort("port1");
36      
37      /* (non-Javadoc)
38       * @see org.mule.tck.junit4.FunctionalTestCase#getConfigResources()
39       */
40      @Override
41      protected String getConfigResources()
42      {
43          return "proxy-mule-6829.xml";
44      }
45      
46      private static class TestCxfEventCallback implements EventCallback {
47          
48          private Latch latch;
49          private String cxfOperationName;
50          
51          private TestCxfEventCallback(Latch latch) {
52              this.latch = latch;
53          }
54          
55          public void eventReceived(MuleEventContext context, Object component) throws Exception
56          {
57              QName cxfOperation = (QName)context.getMessage().getProperty("cxf_operation", PropertyScope.INVOCATION);
58              cxfOperationName = cxfOperation.getLocalPart();
59  
60              latch.countDown();
61          }
62          
63          public String getCxfOperationName()
64          {
65              return cxfOperationName;
66          }
67      }
68  
69      @Test
70      public void testProxyServerSoap11() throws Exception
71      {
72           final Latch latch = new Latch();
73           TestCxfEventCallback testCxfEventCallback = new TestCxfEventCallback(latch);
74           FunctionalTestComponent testComponent = (FunctionalTestComponent) getComponent("soap11Flow");
75           testComponent.setEventCallback(testCxfEventCallback);
76  
77           String msgEchoOperation1 = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:new=\"http://new.webservice.namespace\">"
78                   + "<soapenv:Header/>"
79                   + "  <soapenv:Body>"
80                   + "    <new:parameter1>hello world</new:parameter1>"
81                   + "  </soapenv:Body>"
82                   + "</soapenv:Envelope>";
83           
84           String msgEchoOperation2 = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:new=\"http://new.webservice.namespace\">"
85                   + "<soapenv:Header/>"
86                   + "  <soapenv:Body>"
87                   + "    <new:parameter2>hello world</new:parameter2>"
88                   + "  </soapenv:Body>"
89                   + "</soapenv:Envelope>";
90           
91           String soapOperation = "EchoOperation1";
92           MuleMessage response = executeSoap11Call(msgEchoOperation1, soapOperation);
93           assertTrue(latch.await(1000L, TimeUnit.MILLISECONDS));
94           String cxfOperationName = testCxfEventCallback.getCxfOperationName();
95           assertEquals(soapOperation, cxfOperationName);
96           assertTrue(response.getPayloadAsString().contains("<new:parameter1"));
97           assertTrue(response.getPayloadAsString().contains("hello world"));
98           
99           soapOperation = "EchoOperation2";
100          response = executeSoap11Call(msgEchoOperation2, soapOperation);
101          assertTrue(latch.await(1000L, TimeUnit.MILLISECONDS));
102          cxfOperationName = testCxfEventCallback.getCxfOperationName();
103          assertEquals(soapOperation, cxfOperationName);
104          assertTrue(response.getPayloadAsString().contains("<new:parameter2"));
105          assertTrue(response.getPayloadAsString().contains("hello world"));
106     }
107 
108     @Test
109     public void testProxyServerSoap12() throws Exception
110     {
111          final Latch latch = new Latch();
112          TestCxfEventCallback testCxfEventCallback = new TestCxfEventCallback(latch);
113          FunctionalTestComponent testComponent = (FunctionalTestComponent) getComponent("soap12Flow");
114          testComponent.setEventCallback(testCxfEventCallback);
115 
116          String msgEchoOperation1 = "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:new=\"http://new.webservice.namespace\">"
117                  + "<soap:Header/>"
118                  + "  <soap:Body>"
119                  + "    <new:parameter1>hello world</new:parameter1>"
120                  + "  </soap:Body>"
121                  + "</soap:Envelope>";
122          
123          String msgEchoOperation2 = "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:new=\"http://new.webservice.namespace\">"
124                  + "<soap:Header/>"
125                  + "  <soap:Body>"
126                  + "    <new:parameter2>hello world</new:parameter2>"
127                  + "  </soap:Body>"
128                  + "</soap:Envelope>";
129          
130          String soapOperation = "EchoOperation1";
131          MuleMessage response = executeSoap12Call(msgEchoOperation1, soapOperation);
132          assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
133          String cxfOperationName = testCxfEventCallback.getCxfOperationName();
134          assertEquals(soapOperation, cxfOperationName);
135          assertTrue(response.getPayloadAsString().contains("<new:parameter1"));
136          assertTrue(response.getPayloadAsString().contains("hello world"));
137          
138          soapOperation = "EchoOperation2";
139          response = executeSoap12Call(msgEchoOperation2, soapOperation);
140          assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
141          cxfOperationName = testCxfEventCallback.getCxfOperationName();
142          assertEquals(soapOperation, cxfOperationName);
143          assertTrue(response.getPayloadAsString().contains("<new:parameter2"));
144          assertTrue(response.getPayloadAsString().contains("hello world"));
145     }
146     
147     private MuleMessage executeSoap11Call(String msgString, String soapAction) throws MuleException {
148         MuleMessage msg = new DefaultMuleMessage(msgString, muleContext);
149         msg.setProperty("soapAction", soapAction, PropertyScope.OUTBOUND);
150 
151         MuleClient client = new MuleClient(muleContext);
152         return client.send("http://localhost:" + dynamicPort.getNumber() + "/EchoService11", msg, null);
153     }
154     
155     private MuleMessage executeSoap12Call(String msgString, String soapAction) throws MuleException {
156         MuleMessage msg = new DefaultMuleMessage(msgString, muleContext);
157         String contentType = "application/soap+xml;charset=UTF-8;action=\"" + soapAction + "\"";
158         msg.setProperty("Content-Type", contentType, PropertyScope.OUTBOUND);
159 
160         MuleClient client = new MuleClient(muleContext);
161         return client.send("http://localhost:" + dynamicPort.getNumber() + "/EchoService12", msg, null);
162     }
163 }
164 
165