View Javadoc

1   /*
2    * $Id$
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.construct;
12  
13  import java.util.Collections;
14  
15  import org.mule.MessageExchangePattern;
16  import org.mule.api.MuleEvent;
17  import org.mule.api.endpoint.OutboundEndpoint;
18  import org.mule.api.transport.Connector;
19  import org.mule.routing.filters.PayloadTypeFilter;
20  import org.mule.tck.MuleTestUtils;
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      public void testAck() throws Exception
50      {
51          validator.initialise();
52          validator.start();
53          final MuleEvent response = directInboundMessageSource.process(MuleTestUtils.getTestInboundEvent(
54              Integer.valueOf(123), muleContext));
55  
56          assertEquals("GOOD:123", response.getMessageAsString());
57      }
58  
59      public void testNack() throws Exception
60      {
61          validator.initialise();
62          validator.start();
63          final MuleEvent response = directInboundMessageSource.process(MuleTestUtils.getTestInboundEvent(
64              "abc", muleContext));
65  
66          assertEquals("BAD:abc", response.getMessageAsString());
67      }
68  
69      @SuppressWarnings("unchecked")
70      public void testErrorWithoutExpression() throws Exception
71      {
72          final OutboundEndpoint failingOutboundEndpoint = MuleTestUtils.getTestOutboundEndpoint("failing-oe",
73              muleContext, "test://AlwaysFail", Collections.EMPTY_LIST, null, Collections.EMPTY_MAP,
74              testConnector);
75  
76          validator = new Validator("test-validator", muleContext, directInboundMessageSource,
77              failingOutboundEndpoint, new PayloadTypeFilter(Integer.class),
78              "#[string:GOOD:#[message:payload]]", "#[string:BAD:#[message:payload]]");
79  
80          testAck();
81      }
82  
83      public void testErrorWithExpression() throws Exception
84      {
85          final OutboundEndpoint failingOutboundEndpoint = MuleTestUtils.getTestOutboundEndpoint(
86              MessageExchangePattern.REQUEST_RESPONSE, muleContext, "test://AlwaysFail", testConnector);
87  
88          validator = new Validator("test-validator", muleContext, directInboundMessageSource,
89              failingOutboundEndpoint, new PayloadTypeFilter(Integer.class),
90              "#[string:GOOD:#[message:payload]]", "#[string:BAD:#[message:payload]]",
91              "#[string:ERROR:#[message:payload]]");
92  
93          validator.initialise();
94          validator.start();
95          final MuleEvent response = directInboundMessageSource.process(MuleTestUtils.getTestInboundEvent(123,
96              muleContext));
97  
98          assertEquals("ERROR:123", response.getMessageAsString());
99      }
100 }