View Javadoc

1   /*
2    * $Id: ValidatorTestCase.java 22377 2011-07-11 12:41:42Z dirk.olmes $
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 org.mule.MessageExchangePattern;
14  import org.mule.api.MuleEvent;
15  import org.mule.api.endpoint.OutboundEndpoint;
16  import org.mule.api.transport.Connector;
17  import org.mule.routing.filters.PayloadTypeFilter;
18  import org.mule.tck.MuleTestUtils;
19  
20  import java.util.Collections;
21  
22  import org.junit.Test;
23  
24  import static org.junit.Assert.assertEquals;
25  
26  public class ValidatorTestCase extends AbstractFlowConstuctTestCase
27  {
28      private Validator validator;
29      protected Connector testConnector;
30  
31      @Override
32      protected void doSetUp() throws Exception
33      {
34          super.doSetUp();
35  
36          final OutboundEndpoint testOutboundEndpoint = MuleTestUtils.getTestOutboundEndpoint(
37              MessageExchangePattern.ONE_WAY, muleContext);
38          testConnector = testOutboundEndpoint.getConnector();
39          muleContext.getRegistry().registerConnector(testConnector);
40          testConnector.start();
41  
42          validator = new Validator("test-validator", muleContext, directInboundMessageSource,
43              testOutboundEndpoint, new PayloadTypeFilter(Integer.class), "#[string:GOOD:#[message:payload]]",
44              "#[string:BAD:#[message:payload]]");
45      }
46  
47      @Override
48      protected AbstractFlowConstruct getFlowConstruct() throws Exception
49      {
50          return validator;
51      }
52  
53      @Test
54      public void testAck() throws Exception
55      {
56          validator.initialise();
57          validator.start();
58          final MuleEvent response = directInboundMessageSource.process(MuleTestUtils.getTestEvent(
59              Integer.valueOf(123), muleContext));
60  
61          assertEquals("GOOD:123", response.getMessageAsString());
62      }
63  
64      @Test
65      public void testNack() throws Exception
66      {
67          validator.initialise();
68          validator.start();
69          final MuleEvent response = directInboundMessageSource.process(MuleTestUtils.getTestEvent(
70              "abc", muleContext));
71  
72          assertEquals("BAD:abc", response.getMessageAsString());
73      }
74  
75      @Test
76      @SuppressWarnings("unchecked")
77      public void testErrorWithoutExpression() throws Exception
78      {
79          final OutboundEndpoint failingOutboundEndpoint = MuleTestUtils.getTestOutboundEndpoint("failing-oe",
80              muleContext, "test://AlwaysFail", Collections.EMPTY_LIST, null, Collections.EMPTY_MAP,
81              testConnector);
82  
83          validator = new Validator("test-validator", muleContext, directInboundMessageSource,
84              failingOutboundEndpoint, new PayloadTypeFilter(Integer.class),
85              "#[string:GOOD:#[message:payload]]", "#[string:BAD:#[message:payload]]");
86  
87          testAck();
88      }
89  
90      @Test
91      public void testErrorWithExpression() throws Exception
92      {
93          final OutboundEndpoint failingOutboundEndpoint = MuleTestUtils.getTestOutboundEndpoint(
94              MessageExchangePattern.REQUEST_RESPONSE, muleContext, "test://AlwaysFail", testConnector);
95  
96          validator = new Validator("test-validator", muleContext, directInboundMessageSource,
97              failingOutboundEndpoint, new PayloadTypeFilter(Integer.class),
98              "#[string:GOOD:#[message:payload]]", "#[string:BAD:#[message:payload]]",
99              "#[string:ERROR:#[message:payload]]");
100 
101         validator.initialise();
102         validator.start();
103         final MuleEvent response = directInboundMessageSource.process(MuleTestUtils.getTestEvent(123,
104             muleContext));
105 
106         assertEquals("ERROR:123", response.getMessageAsString());
107     }
108 }