1
2
3
4
5
6
7 package org.mule.test.integration.construct;
8
9 import org.mule.api.MuleEventContext;
10 import org.mule.api.MuleException;
11 import org.mule.module.client.MuleClient;
12 import org.mule.tck.functional.EventCallback;
13 import org.mule.tck.functional.FunctionalTestComponent;
14 import org.mule.tck.junit4.FunctionalTestCase;
15 import org.mule.util.concurrent.Latch;
16
17 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
18 import org.apache.commons.lang.math.RandomUtils;
19 import org.junit.Test;
20
21 import static org.junit.Assert.assertEquals;
22
23 public class ValidatorTestCase extends FunctionalTestCase
24 {
25 private MuleClient muleClient;
26
27 public ValidatorTestCase()
28 {
29 setDisposeContextPerClass(true);
30 }
31
32 @Override
33 protected void doSetUp() throws Exception
34 {
35 super.doSetUp();
36 muleClient = new MuleClient(muleContext);
37 }
38
39 @Override
40 protected String getConfigResources()
41 {
42 return "org/mule/test/integration/construct/validator-config.xml";
43 }
44
45 @Test
46 public void testChildFilter() throws Exception
47 {
48 doTestValidator("validator");
49 }
50
51 @Test
52 public void testFilterAndEndpointReferences() throws Exception
53 {
54 doTestValidator("validator-with-refs");
55 }
56
57 @Test
58 public void testChildEndpoints() throws Exception
59 {
60 doTestValidator("validator-with-child-endpoints");
61 }
62
63 @Test
64 public void testExceptionStrategy() throws Exception
65 {
66 doTestValidMessage("validator-with-exception-strategy");
67
68
69 assertEquals("Ka-boom!", muleClient.send("vm://validator-with-exception-strategy.in", "abc", null)
70 .getPayload());
71 }
72
73 @Test
74 public void testInheritance() throws Exception
75 {
76 doTestValidator("concrete-validator");
77 }
78
79 @Test
80 public void testDispatchError() throws Exception
81 {
82 doTestValidMessageError("dispatch-error");
83 }
84
85 private void doTestValidator(String serviceName) throws Exception
86 {
87 doTestValidMessage(serviceName);
88 doTestInvalidMessageNack(serviceName);
89 }
90
91 private void doTestValidMessage(String serviceName) throws MuleException, Exception, InterruptedException
92 {
93 final FunctionalTestComponent ftc = getFunctionalTestComponent("test-service");
94 final Latch latch = new Latch();
95 ftc.setEventCallback(new EventCallback()
96 {
97 public void eventReceived(MuleEventContext context, Object component) throws Exception
98 {
99 latch.countDown();
100 }
101 });
102
103 final Object validPayload = doTestValidMessageAck(serviceName);
104
105 latch.await(getTestTimeoutSecs(), TimeUnit.SECONDS);
106 assertEquals(1, ftc.getReceivedMessagesCount());
107 assertEquals(validPayload, ftc.getLastReceivedMessage());
108 ftc.initialise();
109 }
110
111 private Object doTestValidMessageAck(String serviceName) throws MuleException
112 {
113 final Integer payload = RandomUtils.nextInt();
114 assertEquals("GOOD:" + payload + "@" + serviceName, muleClient.send("vm://" + serviceName + ".in",
115 payload, null).getPayload());
116 return payload;
117 }
118
119 private Object doTestValidMessageError(String serviceName) throws MuleException
120 {
121 final Integer payload = RandomUtils.nextInt();
122 assertEquals("ERROR:" + payload + "@" + serviceName, muleClient.send("vm://" + serviceName + ".in",
123 payload, null).getPayload());
124 return payload;
125 }
126
127 private void doTestInvalidMessageNack(String serviceName) throws MuleException
128 {
129 assertEquals("BAD:abc@" + serviceName, muleClient.send("vm://" + serviceName + ".in", "abc", null)
130 .getPayload());
131 }
132 }