1
2
3
4
5
6
7 package org.mule.endpoint.outbound;
8
9 import org.mule.MessageExchangePattern;
10 import org.mule.api.MuleEvent;
11 import org.mule.api.endpoint.EndpointException;
12 import org.mule.api.endpoint.MalformedEndpointException;
13 import org.mule.api.endpoint.OutboundEndpoint;
14 import org.mule.api.expression.RequiredValueException;
15 import org.mule.api.lifecycle.InitialisationException;
16 import org.mule.api.transport.DispatchException;
17 import org.mule.endpoint.DynamicOutboundEndpoint;
18 import org.mule.endpoint.EndpointURIEndpointBuilder;
19 import org.mule.tck.junit4.AbstractMuleContextTestCase;
20 import org.mule.tck.testmodels.mule.TestConnector;
21
22 import org.junit.Test;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
29
30 public class DynamicEndpointParsingTestCase extends AbstractMuleContextTestCase
31 {
32
33 public DynamicEndpointParsingTestCase()
34 {
35 setStartContext(true);
36 }
37
38 @Test
39 public void testDynamicUriIsProperlyResolvedOnEvent() throws Exception
40 {
41 OutboundEndpoint endpoint = createRequestResponseEndpoint("test://localhost:#[header:port]");
42 assertTrue(endpoint instanceof DynamicOutboundEndpoint);
43
44 MuleEvent event = getTestEvent("test");
45 event.getMessage().setOutboundProperty("port", 12345);
46
47 MuleEvent response = endpoint.process(event);
48 assertEquals("test://localhost:12345", response.getEndpoint().getEndpointURI().toString());
49 }
50
51 @Test
52 public void testMissingExpressionResult() throws Exception
53 {
54 OutboundEndpoint endpoint = createRequestResponseEndpoint("test://#[header:host]:#[header:port]");
55
56 assertTrue(endpoint instanceof DynamicOutboundEndpoint);
57
58 MuleEvent event = getTestEvent("test");
59 event.getMessage().setOutboundProperty("port", 12345);
60
61 try
62 {
63 endpoint.process(event);
64 fail("A required header is missing on the message");
65 }
66 catch (DispatchException expected)
67 {
68 assertTrue(expected.getCause() instanceof RequiredValueException);
69 }
70 }
71
72 @Test(expected = MalformedEndpointException.class)
73 public void testExpressionInSchemeIsForbidden() throws Exception
74 {
75 createRequestResponseEndpoint("#[header:scheme]://#[header:host]:#[header:port]");
76 }
77
78 @Test(expected = MalformedEndpointException.class)
79 public void testMalformedExpressionInUriIsDetected() throws Exception
80 {
81 createRequestResponseEndpoint("test://#[header:host:#[header:port]");
82 }
83
84 @Test(expected = MalformedEndpointException.class)
85 public void testDynamicInboundEndpointNotAllowed() throws Exception
86 {
87 EndpointURIEndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder("test://#[header:host]:#[header:port]", muleContext);
88 endpointBuilder.buildInboundEndpoint();
89 }
90
91 @Test
92 public void testConnectorURIParam() throws Exception
93 {
94 TestConnector tc = new TestConnector(muleContext);
95 tc.setName("myTestConnector");
96 muleContext.getRegistry().registerConnector(tc);
97
98 OutboundEndpoint endpoint = createRequestResponseEndpoint("test://#[header:host]:#[header:port]?connectorName=myTestConnector");
99
100 MuleEvent event = getTestEvent("test");
101 event.getMessage().setOutboundProperty("port", 12345);
102 event.getMessage().setOutboundProperty("host", "localhost");
103
104 MuleEvent response = endpoint.process(event);
105 assertEquals("test://localhost:12345?connectorName=myTestConnector", response.getEndpoint().getEndpointURI().toString());
106 assertEquals("myTestConnector", response.getEndpoint().getConnector().getName());
107 }
108
109 @Test
110 public void testMEPOverridingInUri() throws Exception
111 {
112 OutboundEndpoint endpoint = createEndpoint("test://#[header:host]:#[header:port]", MessageExchangePattern.ONE_WAY);
113
114 assertTrue(endpoint instanceof DynamicOutboundEndpoint);
115
116 MuleEvent event = getTestEvent("test");
117 event.getMessage().setOutboundProperty("port", 12345);
118 event.getMessage().setOutboundProperty("host", "localhost");
119
120 MuleEvent response = endpoint.process(event);
121 assertNull(response);
122
123
124 endpoint = createRequestResponseEndpoint("test://#[header:host]:#[header:port]?exchangePattern=REQUEST_RESPONSE");
125
126 assertTrue(endpoint instanceof DynamicOutboundEndpoint);
127
128 event = getTestEvent("test");
129 event.getMessage().setOutboundProperty("port", 12345);
130 event.getMessage().setOutboundProperty("host", "localhost");
131
132 response = endpoint.process(event);
133 assertNotNull(response);
134 assertEquals(MessageExchangePattern.REQUEST_RESPONSE, endpoint.getExchangePattern());
135 }
136
137 protected OutboundEndpoint createRequestResponseEndpoint(String uri) throws EndpointException, InitialisationException
138 {
139 return createEndpoint(uri, MessageExchangePattern.REQUEST_RESPONSE);
140 }
141
142 private OutboundEndpoint createEndpoint(String uri, MessageExchangePattern exchangePattern) throws EndpointException, InitialisationException
143 {
144 EndpointURIEndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder(uri, muleContext);
145 endpointBuilder.setExchangePattern(exchangePattern);
146
147 return endpointBuilder.buildOutboundEndpoint();
148 }
149
150 }