View Javadoc

1   /*
2    * $Id: InboundMessageLossFlowTestCase.java 22552 2011-07-25 07:18:19Z claude.mamo $
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.transport.jdbc.reliability;
12  
13  import static org.junit.Assert.assertEquals;
14  
15  import org.mule.tck.probe.Probe;
16  
17  import java.util.Arrays;
18  import java.util.Collection;
19  
20  import org.apache.commons.dbutils.handlers.ArrayHandler;
21  import org.junit.Test;
22  import org.junit.runners.Parameterized.Parameters;
23  
24  
25  /**
26   * Verify that no inbound messages are lost when exceptions occur.  
27   * The message must either make it all the way to the SEDA queue (in the case of 
28   * an asynchronous inbound endpoint), or be restored/rolled back at the source.
29   * 
30   * In the case of JDBC, this will cause the ACK query to not be executed and therefore 
31   * the source data will still be present the next time the database is polled.
32   */
33  public class InboundMessageLossFlowTestCase extends InboundMessageLossTestCase
34  {
35      public InboundMessageLossFlowTestCase(ConfigVariant variant, String configResources)
36      {
37          super(variant, configResources);     
38      }
39  
40      @Parameters
41      public static Collection<Object[]> parameters()
42      {
43          return Arrays.asList(new Object[][]{            
44              {ConfigVariant.FLOW, "reliability/jdbc-connector.xml, reliability/inbound-message-loss-flow.xml"}
45          });
46      }          
47  
48      @Override
49      @Test
50      public void testTransformerException() throws Exception
51      {
52          assertEquals(1, qr.update(jdbcConnector.getConnection(), 
53              "INSERT INTO TEST(TYPE, DATA, ACK, RESULT) VALUES (2, '" + TEST_MESSAGE + "', NULL, NULL)"));
54  
55          prober.check(new Probe()
56          {
57              @Override
58              public boolean isSatisfied()
59              {
60                  try
61                  {
62                      Object[] queryResult = (Object[]) qr.query(jdbcConnector.getConnection(), 
63                          "SELECT DATA FROM TEST WHERE TYPE = 2 AND ACK IS NULL", new ArrayHandler());
64                      // Exception occurs after the SEDA queue for an asynchronous request, so from the client's
65                      // perspective, the message has been delivered successfully.
66                      // Note that this behavior is different from services because the exception occurs before
67                      // the SEDA queue for services.
68                      return (queryResult == null);
69                  }
70                  catch (Exception e)
71                  {
72                      throw new RuntimeException(e);
73                  }
74              }
75  
76              @Override
77              public String describeFailure()
78              {
79                  return "Row should be acknowledged (marked read)";
80              }
81          });
82      }
83      
84      @Override
85      @Test
86      public void testRouterException() throws Exception
87      {
88          assertEquals(1, qr.update(jdbcConnector.getConnection(), 
89              "INSERT INTO TEST(TYPE, DATA, ACK, RESULT) VALUES (3, '" + TEST_MESSAGE + "', NULL, NULL)"));
90  
91          prober.check(new Probe()
92          {
93              @Override
94              public boolean isSatisfied()
95              {
96                  try
97                  {
98                      Object[] queryResult = (Object[]) qr.query(jdbcConnector.getConnection(), 
99                          "SELECT DATA FROM TEST WHERE TYPE = 3 AND ACK IS NULL", new ArrayHandler());
100                     // Exception occurs after the SEDA queue for an asynchronous request, so from the client's
101                     // perspective, the message has been delivered successfully.
102                     // Note that this behavior is different from services because the exception occurs before
103                     // the SEDA queue for services.
104                     return (queryResult == null);
105                 }
106                 catch (Exception e)
107                 {
108                     throw new RuntimeException(e);
109                 }
110             }
111 
112             @Override
113             public String describeFailure()
114             {
115                 return "Row should be acknowledged (marked read)";
116             }
117         });
118     }    
119 }
120