View Javadoc

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