View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.construct;
8   
9   import org.mule.MessageExchangePattern;
10  import org.mule.api.MuleEvent;
11  import org.mule.api.endpoint.OutboundEndpoint;
12  import org.mule.api.transport.Connector;
13  import org.mule.routing.filters.PayloadTypeFilter;
14  import org.mule.tck.MuleTestUtils;
15  
16  import java.util.Collections;
17  
18  import org.junit.Test;
19  
20  import static org.junit.Assert.assertEquals;
21  
22  public class ValidatorTestCase extends AbstractFlowConstuctTestCase
23  {
24      private Validator validator;
25      protected Connector testConnector;
26  
27      @Override
28      protected void doSetUp() throws Exception
29      {
30          super.doSetUp();
31  
32          final OutboundEndpoint testOutboundEndpoint = MuleTestUtils.getTestOutboundEndpoint(
33              MessageExchangePattern.ONE_WAY, muleContext);
34          testConnector = testOutboundEndpoint.getConnector();
35          muleContext.getRegistry().registerConnector(testConnector);
36          testConnector.start();
37  
38          validator = new Validator("test-validator", muleContext, directInboundMessageSource,
39              testOutboundEndpoint, new PayloadTypeFilter(Integer.class), "#[string:GOOD:#[message:payload]]",
40              "#[string:BAD:#[message:payload]]");
41      }
42  
43      @Override
44      protected AbstractFlowConstruct getFlowConstruct() throws Exception
45      {
46          return validator;
47      }
48  
49      @Test
50      public void testAck() throws Exception
51      {
52          validator.initialise();
53          validator.start();
54          final MuleEvent response = directInboundMessageSource.process(MuleTestUtils.getTestInboundEvent(
55              Integer.valueOf(123), muleContext));
56  
57          assertEquals("GOOD:123", response.getMessageAsString());
58      }
59  
60      @Test
61      public void testNack() throws Exception
62      {
63          validator.initialise();
64          validator.start();
65          final MuleEvent response = directInboundMessageSource.process(MuleTestUtils.getTestInboundEvent(
66              "abc", muleContext));
67  
68          assertEquals("BAD:abc", response.getMessageAsString());
69      }
70  
71      @Test
72      @SuppressWarnings("unchecked")
73      public void testErrorWithoutExpression() throws Exception
74      {
75          final OutboundEndpoint failingOutboundEndpoint = MuleTestUtils.getTestOutboundEndpoint("failing-oe",
76              muleContext, "test://AlwaysFail", Collections.EMPTY_LIST, null, Collections.EMPTY_MAP,
77              testConnector);
78  
79          validator = new Validator("test-validator", muleContext, directInboundMessageSource,
80              failingOutboundEndpoint, new PayloadTypeFilter(Integer.class),
81              "#[string:GOOD:#[message:payload]]", "#[string:BAD:#[message:payload]]");
82  
83          testAck();
84      }
85  
86      @Test
87      public void testErrorWithExpression() throws Exception
88      {
89          final OutboundEndpoint failingOutboundEndpoint = MuleTestUtils.getTestOutboundEndpoint(
90              MessageExchangePattern.REQUEST_RESPONSE, muleContext, "test://AlwaysFail", testConnector);
91  
92          validator = new Validator("test-validator", muleContext, directInboundMessageSource,
93              failingOutboundEndpoint, new PayloadTypeFilter(Integer.class),
94              "#[string:GOOD:#[message:payload]]", "#[string:BAD:#[message:payload]]",
95              "#[string:ERROR:#[message:payload]]");
96  
97          validator.initialise();
98          validator.start();
99          final MuleEvent response = directInboundMessageSource.process(MuleTestUtils.getTestInboundEvent(123,
100             muleContext));
101 
102         assertEquals("ERROR:123", response.getMessageAsString());
103     }
104 }