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.ws.construct;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.MessageExchangePattern;
11  import org.mule.api.MuleEvent;
12  import org.mule.api.MuleException;
13  import org.mule.api.MuleMessage;
14  import org.mule.api.endpoint.InboundEndpoint;
15  import org.mule.api.endpoint.OutboundEndpoint;
16  import org.mule.api.lifecycle.InitialisationException;
17  import org.mule.api.transport.Connector;
18  import org.mule.api.transport.MessageRequester;
19  import org.mule.api.transport.PropertyScope;
20  import org.mule.construct.AbstractFlowConstruct;
21  import org.mule.construct.AbstractFlowConstuctTestCase;
22  import org.mule.tck.MuleTestUtils;
23  import org.mule.transport.AbstractMessageRequester;
24  import org.mule.transport.AbstractMessageRequesterFactory;
25  
26  import java.net.InetAddress;
27  
28  import org.junit.Test;
29  
30  import static org.junit.Assert.assertEquals;
31  
32  public abstract class AbstractWSProxyTestCase extends AbstractFlowConstuctTestCase
33  {
34      protected Connector testConnector;
35      private WSProxy wsProxy;
36  
37      @Override
38      protected void doSetUp() throws Exception
39      {
40          super.doSetUp();
41  
42          final OutboundEndpoint testOutboundEndpoint = MuleTestUtils.getTestOutboundEndpoint(
43              MessageExchangePattern.REQUEST_RESPONSE, muleContext);
44          testConnector = testOutboundEndpoint.getConnector();
45          muleContext.getRegistry().registerConnector(testConnector);
46          testConnector.start();
47  
48          wsProxy = newWSProxy(testOutboundEndpoint);
49      }
50  
51      protected abstract WSProxy newWSProxy(OutboundEndpoint testOutboundEndpoint) throws Exception;
52  
53      @Override
54      protected AbstractFlowConstruct getFlowConstruct() throws Exception
55      {
56          return wsProxy;
57      }
58  
59      private void startWsProxy() throws InitialisationException, MuleException
60      {
61          wsProxy.initialise();
62          wsProxy.start();
63      }
64  
65      @Test
66      public void testProcessNonHttpRequest() throws Exception
67      {
68          startWsProxy();
69  
70          final MuleEvent response = directInboundMessageSource.process(MuleTestUtils.getTestInboundEvent(
71              "hello", muleContext));
72  
73          assertEquals("hello", response.getMessageAsString());
74      }
75  
76      @Test
77      public void testProcessHttpWsdlRequest() throws Exception
78      {
79          startWsProxy();
80  
81          testConnector.setRequesterFactory(new AbstractMessageRequesterFactory()
82          {
83              @Override
84              public MessageRequester create(InboundEndpoint endpoint) throws MuleException
85              {
86                  return new AbstractMessageRequester(endpoint)
87                  {
88                      @Override
89                      protected MuleMessage doRequest(long timeout) throws Exception
90                      {
91                          assertEquals("test://test?wsdl", endpoint.getEndpointURI().toString());
92                          return new DefaultMuleMessage("fake_wsdl localhost", muleContext);
93                      }
94                  };
95              }
96          });
97  
98          final MuleEvent event = MuleTestUtils.getTestInboundEvent("hello", muleContext);
99          event.getMessage().setProperty("http.request", "test://foo?WSDL", PropertyScope.INBOUND);
100         final MuleEvent response = directInboundMessageSource.process(event);
101 
102         assertEquals("fake_wsdl " + InetAddress.getLocalHost().getHostName(), response.getMessageAsString());
103     }
104 
105     @Test
106     public void testProcessHttpServiceRequest() throws Exception
107     {
108         startWsProxy();
109         final MuleEvent event = MuleTestUtils.getTestInboundEvent("hello", muleContext);
110         event.getMessage().setProperty("http.request", "http://foo", PropertyScope.INBOUND);
111         final MuleEvent response = directInboundMessageSource.process(event);
112 
113         assertEquals("hello", response.getMessageAsString());
114     }
115 }