View Javadoc

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