1
2
3
4
5
6
7
8
9
10
11 package org.mule.test.integration.routing.outbound;
12
13 import org.mule.api.MuleException;
14 import org.mule.api.MuleMessage;
15 import org.mule.api.construct.FlowConstruct;
16 import org.mule.api.endpoint.OutboundEndpoint;
17 import org.mule.api.processor.MessageProcessor;
18 import org.mule.api.processor.MessageProcessorChain;
19 import org.mule.api.routing.RoutePathNotFoundException;
20 import org.mule.api.routing.filter.Filter;
21 import org.mule.construct.Flow;
22 import org.mule.routing.ChoiceRouter;
23 import org.mule.routing.MessageProcessorFilterPair;
24 import org.mule.routing.filters.ExpressionFilter;
25 import org.mule.routing.filters.RegExFilter;
26 import org.mule.tck.junit4.FunctionalTestCase;
27
28 import org.junit.Test;
29
30 import static org.junit.Assert.assertEquals;
31 import static org.junit.Assert.assertNotNull;
32 import static org.junit.Assert.assertTrue;
33
34 public class ChoiceRouterTestCase extends FunctionalTestCase
35 {
36 private static final String WITH_DEFAULT_ROUTE_CHANNEL = "vm://with-default-route.in";
37 private static final String WITHOUT_DEFAULT_ROUTE_CHANNEL = "vm://without-default-route.in";
38
39 public ChoiceRouterTestCase()
40 {
41 setDisposeContextPerClass(true);
42 }
43
44 @Override
45 protected String getConfigResources()
46 {
47 return "org/mule/test/integration/routing/outbound/choice-router-test.xml";
48 }
49
50
51
52
53 @Test
54 public void filterReferenceShouldCreateFilterWithRegexFilterAndOutboundEndpointChain()
55 {
56 ChoiceRouter router = findChoiceRouterInFlow("without-default-route");
57 MessageProcessorFilterPair pair = router.getConditionalMessageProcessors().get(0);
58 assertIsRegExFilterWithPattern(pair.getFilter(), "apple");
59 assertMessageChainIsOutboundEndpoint(pair.getMessageProcessor(), "vm://fruit-channel.in");
60 }
61
62
63
64
65 @Test
66 public void embeddedFilterShouldCreatePairWithFilterAndOtherConfiguredMPsAsChain()
67 {
68 ChoiceRouter router = findChoiceRouterInFlow("with-default-route");
69
70 MessageProcessorFilterPair firstPair = router.getConditionalMessageProcessors().get(0);
71 assertIsRegExFilterWithPattern(firstPair.getFilter(), "apple");
72 assertMessageChainIsOutboundEndpoint(firstPair.getMessageProcessor(), "vm://fruit-channel.in");
73
74 MessageProcessorFilterPair secondPair = router.getConditionalMessageProcessors().get(1);
75 assertIsRegExFilterWithPattern(secondPair.getFilter(), "turnip");
76 assertMessageChainIsOutboundEndpoint(secondPair.getMessageProcessor(), "vm://veggie-channel.in");
77
78 MessageProcessorFilterPair thirdPair = router.getConditionalMessageProcessors().get(2);
79 assertIsExpressionFilterWithExpressionAndEvaluator(thirdPair.getFilter(), ".*berry", "regex");
80 assertMessageChainIsOutboundEndpoint(thirdPair.getMessageProcessor(), "vm://fruit-channel.in");
81 }
82
83 @Test
84 public void sendToInvalidRouteWithoutDefaultRouteShouldThrowException() throws Exception
85 {
86 MuleMessage result = muleContext.getClient().send(WITHOUT_DEFAULT_ROUTE_CHANNEL, "bad", null);
87 assertNotNull(result);
88 assertNotNull("should have got a MuleException", result.getExceptionPayload());
89 assertNotNull(result.getExceptionPayload().getException() instanceof MuleException);
90 assertNotNull(result.getExceptionPayload().getRootException() instanceof RoutePathNotFoundException);
91 }
92
93 @Test
94 public void sendToValidRouteShouldReturnValidResult() throws Exception
95 {
96 MuleMessage result = muleContext.getClient().send(WITHOUT_DEFAULT_ROUTE_CHANNEL, "apple", null);
97 assertEquals("apple:fruit:fruit", result.getPayloadAsString());
98 }
99
100 @Test
101 public void sendToAppleRouteShouldHitFruitService() throws Exception
102 {
103 MuleMessage result = muleContext.getClient().send(WITH_DEFAULT_ROUTE_CHANNEL, "apple", null);
104 assertEquals("apple:fruit:fruit", result.getPayloadAsString());
105 }
106
107 @Test
108 public void sendToTurnipRouteShouldHitVeggieService() throws Exception
109 {
110 MuleMessage result = muleContext.getClient().send(WITH_DEFAULT_ROUTE_CHANNEL, "turnip", null);
111 assertEquals("turnip:veggie:veggie", result.getPayloadAsString());
112 }
113
114 @Test
115 public void sendToBlueberryRouteShouldHitFruitService() throws Exception
116 {
117 MuleMessage result = muleContext.getClient().send(WITH_DEFAULT_ROUTE_CHANNEL, "blueberry", null);
118 assertEquals("blueberry:fruit:fruit", result.getPayloadAsString());
119 }
120
121 @Test
122 public void sendToInvalidRouteShouldHitDefaultRoute() throws Exception
123 {
124 MuleMessage result = muleContext.getClient().send(WITH_DEFAULT_ROUTE_CHANNEL, "car", null);
125 assertEquals("car:default:default", result.getPayloadAsString());
126 }
127
128 private ChoiceRouter findChoiceRouterInFlow(String flowName)
129 {
130 Flow flow = lookupFlow(flowName);
131 return getChoiceRouter(flow);
132 }
133
134 private Flow lookupFlow(String flowConstructName)
135 {
136 FlowConstruct flow = muleContext.getRegistry().lookupFlowConstruct(flowConstructName);
137 assertNotNull(flow);
138 assertTrue(flow instanceof Flow);
139 return (Flow) flow;
140 }
141
142 private ChoiceRouter getChoiceRouter(Flow flowConstruct)
143 {
144 MessageProcessor routerMessageProcessor = flowConstruct.getMessageProcessors().get(0);
145 assertTrue(routerMessageProcessor instanceof ChoiceRouter);
146 return (ChoiceRouter) routerMessageProcessor;
147 }
148
149 private void assertIsRegExFilterWithPattern(Filter filter, String pattern)
150 {
151 assertTrue(filter instanceof RegExFilter);
152
153 RegExFilter regExFilter = (RegExFilter) filter;
154 assertEquals(regExFilter.getPattern(), pattern);
155 }
156
157 private void assertIsExpressionFilterWithExpressionAndEvaluator(Filter filter, String expression, String evaluator)
158 {
159 assertTrue(filter instanceof ExpressionFilter);
160
161 ExpressionFilter expressionFilter = (ExpressionFilter) filter;
162 assertEquals(expression, expressionFilter.getExpression());
163 assertEquals(evaluator, expressionFilter.getEvaluator());
164 }
165
166 private void assertMessageChainIsOutboundEndpoint(MessageProcessor processor, String expectedAddress)
167 {
168 assertTrue(processor instanceof MessageProcessorChain);
169 MessageProcessorChain chain = (MessageProcessorChain) processor;
170
171 MessageProcessor firstInChain = chain.getMessageProcessors().get(0);
172 assertTrue(firstInChain instanceof OutboundEndpoint);
173 OutboundEndpoint endpoint = (OutboundEndpoint) firstInChain;
174
175 String address = endpoint.getAddress();
176 assertEquals(expectedAddress, address);
177 }
178 }