View Javadoc

1   /*
2    * $Id: DynamicEndpointParsingTestCase.java 20320 2010-11-24 15:03:31Z dfeist $
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  package org.mule.endpoint.outbound;
11  
12  import org.mule.MessageExchangePattern;
13  import org.mule.api.MuleEvent;
14  import org.mule.api.endpoint.EndpointException;
15  import org.mule.api.endpoint.MalformedEndpointException;
16  import org.mule.api.endpoint.OutboundEndpoint;
17  import org.mule.api.expression.RequiredValueException;
18  import org.mule.api.lifecycle.InitialisationException;
19  import org.mule.api.transport.DispatchException;
20  import org.mule.endpoint.DynamicOutboundEndpoint;
21  import org.mule.endpoint.EndpointURIEndpointBuilder;
22  import org.mule.endpoint.dynamic.NullConnector;
23  import org.mule.tck.AbstractMuleTestCase;
24  import org.mule.tck.testmodels.mule.TestConnector;
25  
26  
27  public class DynamicEndpointParsingTestCase extends AbstractMuleTestCase
28  {
29      public DynamicEndpointParsingTestCase()
30      {
31          setStartContext(true);
32      }
33  
34      public void testSingleExpression() throws Exception
35      {
36          OutboundEndpoint endpoint = createEndpoint("test://localhost:#[header:port]");
37  
38          assertTrue(endpoint instanceof DynamicOutboundEndpoint);
39  
40          assertTrue(endpoint.getConnector() instanceof NullConnector);
41  
42          MuleEvent event = getTestEvent("test");
43          event.getMessage().setOutboundProperty("port", 12345);
44  
45          endpoint.process(event);
46          assertTrue(endpoint.getConnector() instanceof TestConnector);
47  
48  
49      }
50  
51      public void testSingleMultiExpression() throws Exception
52      {
53          OutboundEndpoint endpoint = createEndpoint("test://#[header:host]:#[header:port]");
54  
55          assertTrue(endpoint instanceof DynamicOutboundEndpoint);
56  
57          assertTrue(endpoint.getConnector() instanceof NullConnector);
58  
59          MuleEvent event = getTestEvent("test");
60          event.getMessage().setOutboundProperty("port", 12345);
61          event.getMessage().setOutboundProperty("host", "localhost");
62  
63          endpoint.process(event);
64          assertTrue(endpoint.getConnector() instanceof TestConnector);
65      }
66  
67      public void testMissingExpressionResult() throws Exception
68      {
69          OutboundEndpoint endpoint = createEndpoint("test://#[header:host]:#[header:port]");
70  
71          assertTrue(endpoint instanceof DynamicOutboundEndpoint);
72  
73          assertTrue(endpoint.getConnector() instanceof NullConnector);
74  
75          MuleEvent event = getTestEvent("test");
76          event.getMessage().setOutboundProperty("port", 12345);
77  
78          try
79          {
80              endpoint.process(event);
81              fail("A required header is missing on the message");
82          }
83          catch (DispatchException e)
84          {
85              //expected
86              assertTrue(e.getCause() instanceof RequiredValueException);
87          }
88      }
89  
90      public void testExpressionInScheme() throws Exception
91      {
92          try
93          {
94              createEndpoint("#[header:scheme]://#[header:host]:#[header:port]");
95              fail("The scheme part of a dynamic endpoint cannot be an expression");
96          }
97          catch (MalformedEndpointException e)
98          {
99              //expected
100         }
101     }
102 
103     public void testMalformedEndpoint() throws Exception
104     {
105         try
106         {
107             createEndpoint("test://#[header:host:#[header:port]");
108             fail("The endpoint expressions are malformed");
109         }
110         catch (MalformedEndpointException e)
111         {
112             //expected
113         }
114     }
115 
116     public void testInboundEndpoint() throws Exception
117     {
118         //Dynamic inbound endpoints not allowed
119         EndpointURIEndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder("test://#[header:host]:#[header:port]", muleContext);
120         try
121         {
122             endpointBuilder.buildInboundEndpoint();
123             fail("Dynamic inbound endpoints not allowed");
124         }
125         catch (MalformedEndpointException e)
126         {
127             //expected
128         }
129     }
130 
131     public void testConnectorURIParam() throws Exception
132     {
133         TestConnector tc = new TestConnector(muleContext);
134         tc.setName("myTestConnector");
135         muleContext.getRegistry().registerConnector(tc);
136 
137         OutboundEndpoint endpoint = createEndpoint("test://#[header:host]:#[header:port]?connectorName=myTestConnector");
138 
139         assertTrue(endpoint instanceof DynamicOutboundEndpoint);
140 
141         assertTrue(endpoint.getConnector() instanceof NullConnector);
142 
143         MuleEvent event = getTestEvent("test");
144         event.getMessage().setOutboundProperty("port", 12345);
145         event.getMessage().setOutboundProperty("host", "localhost");
146 
147         endpoint.process(event);
148         assertTrue(endpoint.getConnector() instanceof TestConnector);
149         assertEquals("myTestConnector", endpoint.getConnector().getName());
150     }
151 
152     public void testMEPURIParam() throws Exception
153     {
154         OutboundEndpoint endpoint = createEndpoint("test://#[header:host]:#[header:port]");
155 
156         assertTrue(endpoint instanceof DynamicOutboundEndpoint);
157 
158         assertTrue(endpoint.getConnector() instanceof NullConnector);
159 
160         MuleEvent event = getTestEvent("test");
161         event.getMessage().setOutboundProperty("port", 12345);
162         event.getMessage().setOutboundProperty("host", "localhost");
163 
164         endpoint.process(event);
165         //The default for the Test connector is ONE_WAY
166         assertEquals(MessageExchangePattern.ONE_WAY, endpoint.getExchangePattern());
167 
168         //Now test set on the endpoint
169         endpoint = createEndpoint("test://#[header:host]:#[header:port]?exchangePattern=REQUEST_RESPONSE");
170 
171         assertTrue(endpoint instanceof DynamicOutboundEndpoint);
172 
173         assertTrue(endpoint.getConnector() instanceof NullConnector);
174 
175         event = getTestEvent("test");
176         event.getMessage().setOutboundProperty("port", 12345);
177         event.getMessage().setOutboundProperty("host", "localhost");
178 
179         endpoint.process(event);
180         assertEquals(MessageExchangePattern.REQUEST_RESPONSE, endpoint.getExchangePattern());
181     }
182 
183     protected OutboundEndpoint createEndpoint(String uri) throws EndpointException, InitialisationException
184     {
185         EndpointURIEndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder(uri, muleContext);
186 
187         return endpointBuilder.buildOutboundEndpoint();
188     }
189     
190 }