1
2
3
4
5
6
7
8
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.tck.junit4.AbstractMuleContextTestCase;
23
24 import org.junit.Test;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
30 import static org.junit.Assert.fail;
31
32 public class DynamicEndpointParsingTestCase extends AbstractMuleContextTestCase
33 {
34
35 public DynamicEndpointParsingTestCase()
36 {
37 setStartContext(true);
38 }
39
40 @Test
41 public void testDynamicUriIsProperlyResolvedOnEvent() throws Exception
42 {
43 OutboundEndpoint endpoint = createRequestResponseEndpoint("test://localhost:#[header:port]");
44 assertTrue(endpoint instanceof DynamicOutboundEndpoint);
45
46 MuleEvent event = getTestEvent("test");
47 event.getMessage().setOutboundProperty("port", 12345);
48
49 MuleEvent response = endpoint.process(event);
50
51 assertEquals("test://localhost:12345", response.getMessageSourceURI().toString());
52 }
53
54 @Test
55 public void testMissingExpressionResult() throws Exception
56 {
57 OutboundEndpoint endpoint = createRequestResponseEndpoint("test://#[header:host]:#[header:port]");
58
59 assertTrue(endpoint instanceof DynamicOutboundEndpoint);
60
61 MuleEvent event = getTestEvent("test");
62 event.getMessage().setOutboundProperty("port", 12345);
63
64 try
65 {
66 endpoint.process(event);
67 fail("A required header is missing on the message");
68 }
69 catch (DispatchException expected)
70 {
71 assertTrue(expected.getCause() instanceof RequiredValueException);
72 }
73 }
74
75 @Test(expected = MalformedEndpointException.class)
76 public void testExpressionInSchemeIsForbidden() throws Exception
77 {
78 createRequestResponseEndpoint("#[header:scheme]://#[header:host]:#[header:port]");
79 }
80
81 @Test(expected = MalformedEndpointException.class)
82 public void testMalformedExpressionInUriIsDetected() throws Exception
83 {
84 createRequestResponseEndpoint("test://#[header:host:#[header:port]");
85 }
86
87 @Test(expected = MalformedEndpointException.class)
88 public void testDynamicInboundEndpointNotAllowed() throws Exception
89 {
90 EndpointURIEndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder("test://#[header:host]:#[header:port]", muleContext);
91 endpointBuilder.buildInboundEndpoint();
92 }
93
94 @Test
95 public void testMEPOverridingInUri() throws Exception
96 {
97 OutboundEndpoint endpoint = createEndpoint("test://#[header:host]:#[header:port]", MessageExchangePattern.ONE_WAY);
98
99 assertTrue(endpoint instanceof DynamicOutboundEndpoint);
100
101 MuleEvent event = getTestEvent("test");
102 event.getMessage().setOutboundProperty("port", 12345);
103 event.getMessage().setOutboundProperty("host", "localhost");
104
105 MuleEvent response = endpoint.process(event);
106 assertNull(response);
107
108
109 endpoint = createRequestResponseEndpoint("test://#[header:host]:#[header:port]?exchangePattern=REQUEST_RESPONSE");
110
111 assertTrue(endpoint instanceof DynamicOutboundEndpoint);
112
113 event = getTestEvent("test");
114 event.getMessage().setOutboundProperty("port", 12345);
115 event.getMessage().setOutboundProperty("host", "localhost");
116
117 response = endpoint.process(event);
118 assertNotNull(response);
119 assertEquals(MessageExchangePattern.REQUEST_RESPONSE, endpoint.getExchangePattern());
120 }
121
122 protected OutboundEndpoint createRequestResponseEndpoint(String uri) throws EndpointException, InitialisationException
123 {
124 return createEndpoint(uri, MessageExchangePattern.REQUEST_RESPONSE);
125 }
126
127 private OutboundEndpoint createEndpoint(String uri, MessageExchangePattern exchangePattern) throws EndpointException, InitialisationException
128 {
129 EndpointURIEndpointBuilder endpointBuilder = new EndpointURIEndpointBuilder(uri, muleContext);
130 endpointBuilder.setExchangePattern(exchangePattern);
131
132 return endpointBuilder.buildOutboundEndpoint();
133 }
134
135 }