View Javadoc

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