View Javadoc

1   /*
2    * $Id: WSProxyPathBugTestCase.java 22556 2011-07-25 21:01:33Z mike.schilling $
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.junit.Test;
14  import org.mockito.Mock;
15  import org.mockito.MockitoAnnotations;
16  import org.mule.MessageExchangePattern;
17  import org.mule.api.MuleEvent;
18  import org.mule.api.MuleMessage;
19  import org.mule.api.endpoint.EndpointBuilder;
20  import org.mule.api.endpoint.InboundEndpoint;
21  import org.mule.api.endpoint.OutboundEndpoint;
22  import org.mule.api.transport.PropertyScope;
23  import org.mule.construct.AbstractFlowConstruct;
24  import org.mule.construct.AbstractFlowConstuctTestCase;
25  import org.mule.tck.MuleTestUtils;
26  
27  import java.net.URI;
28  import java.util.Collections;
29  
30  import static org.mockito.Mockito.when;
31  import static org.junit.Assert.assertEquals;
32  
33  
34  /**
35   * Test WSDL rewriting.  MULE-5520
36   */
37  public class WSProxyPathBugTestCase extends AbstractFlowConstuctTestCase
38  {
39      private static final String INBOUND_ADDRESS = "test://myhost:8090/weather-forecast";
40      private static final String OUTBOUND_ADDRESS = "test://ws.acme.com:6090/weather-forecast";
41      private static final String WSDL_LOCATION_SAME_PATH = OUTBOUND_ADDRESS;
42      private static final String WSDL_LOCATION_DIFFERENT_PATH = "test://ws.acme.com:6090/wsdl/weather-forecast";
43  
44      private WSProxy proxy;
45      private MuleEvent inboundEvent;
46  
47      @Mock
48      private EndpointBuilder mockEndpointBuilder;
49      @Mock
50      private InboundEndpoint mockWsdlEndpoint;
51      @Mock
52      private MuleMessage mockMuleMessage;
53  
54      private OutboundEndpoint testOutboundEndpoint;
55  
56      @Override
57      protected void doSetUp() throws Exception
58      {
59          super.doSetUp();
60          MockitoAnnotations.initMocks(this);
61          when(mockMuleMessage.getPayloadAsString()).thenReturn(OUTBOUND_ADDRESS);
62          inboundEvent = getTestEvent(null, getTestInboundEndpoint("test-inbound", INBOUND_ADDRESS));
63          when(mockWsdlEndpoint.request(inboundEvent.getTimeout())).thenReturn(mockMuleMessage);
64          inboundEvent.getMessage().setProperty("http.request", INBOUND_ADDRESS + "?wsdl", PropertyScope.INBOUND);
65          when(mockEndpointBuilder.buildInboundEndpoint()).thenReturn(mockWsdlEndpoint);
66          testOutboundEndpoint = MuleTestUtils.getTestOutboundEndpoint(
67              MessageExchangePattern.REQUEST_RESPONSE, muleContext, OUTBOUND_ADDRESS, null);
68          proxy = new WSProxy("test-proxy", muleContext, directInboundMessageSource, testOutboundEndpoint,
69                              Collections.EMPTY_LIST, Collections.EMPTY_LIST);
70  
71      }
72  
73      @Test
74      public void testWithSamePath() throws Exception
75      {
76          assertOnUriRewrite(WSDL_LOCATION_SAME_PATH);
77      }
78  
79      @Test
80      public void testWithDifferentPath() throws Exception
81      {
82          assertOnUriRewrite(WSDL_LOCATION_DIFFERENT_PATH);
83      }
84  
85      private void assertOnUriRewrite(String wsdlLocation) throws Exception
86      {
87          setUpProxy(wsdlLocation);
88          MuleEvent response = directInboundMessageSource.process(inboundEvent);
89          assertEquals(INBOUND_ADDRESS, response.getMessageAsString());
90      }
91  
92  
93      private void setUpProxy(String wsdlLocation) throws Exception
94      {
95          muleContext.getRegistry().registerEndpointBuilder(wsdlLocation, mockEndpointBuilder);
96          proxy = new WSProxy("test-proxy", muleContext, directInboundMessageSource,
97              testOutboundEndpoint, Collections.EMPTY_LIST, Collections.EMPTY_LIST, new URI(wsdlLocation));
98          proxy.initialise();
99          proxy.start();
100     }
101 
102     @Override
103     protected AbstractFlowConstruct getFlowConstruct()
104     {
105         return proxy;
106     }
107 }