View Javadoc

1   /*
2    * $Id: IdempotencyTestCase.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  package org.mule.transport.jdbc.functional;
11  
12  import static org.junit.Assert.assertNotNull;
13  
14  import org.mule.DefaultMuleMessage;
15  import org.mule.api.MuleMessage;
16  import org.mule.api.client.MuleClient;
17  import org.mule.api.transport.PropertyScope;
18  import org.mule.tck.junit4.FunctionalTestCase;
19  import org.mule.util.DateUtils;
20  
21  import java.util.Arrays;
22  import java.util.Calendar;
23  import java.util.Collection;
24  
25  import org.apache.commons.dbutils.QueryRunner;
26  import org.junit.Test;
27  import org.junit.runners.Parameterized.Parameters;
28  
29  public class IdempotencyTestCase extends AbstractJdbcFunctionalTestCase
30  {
31  
32      public IdempotencyTestCase(ConfigVariant variant, String configResources)
33      {
34          super(variant, configResources);
35      }
36  
37      @Parameters
38      public static Collection<Object[]> parameters()
39      {
40          return Arrays.asList(new Object[][]{            
41              {ConfigVariant.FLOW, AbstractJdbcFunctionalTestCase.getConfig() + ",jdbc-store.xml"}
42          });
43      }      
44      
45      @Test
46      public void testIdempotencySequential() throws Exception
47      {
48          MuleClient client = muleContext.getClient();
49  
50          int id = Integer.valueOf(DateUtils.formatTimeStamp(Calendar.getInstance().getTime(), "hhmmss"));
51          String valueExpression = DateUtils.formatTimeStamp(Calendar.getInstance().getTime(), "ssmmhh");
52  
53          MuleMessage message = new DefaultMuleMessage("hi", muleContext);
54          message.setProperty("originalId", id, PropertyScope.OUTBOUND);
55          message.setProperty("valueId", valueExpression, PropertyScope.OUTBOUND);
56          client.dispatch("vm://in", message);
57  
58          MuleMessage requestMessage = client.request("vm://forProcessing", FunctionalTestCase.RECEIVE_TIMEOUT);
59          assertNotNull(requestMessage);
60  
61          message = new DefaultMuleMessage("hi", muleContext);
62          message.setProperty("originalId", id, PropertyScope.OUTBOUND);
63          message.setProperty("valueId", valueExpression, PropertyScope.OUTBOUND);
64          client.send("vm://in", message);
65  
66          requestMessage = client.request("vm://duplicated", FunctionalTestCase.RECEIVE_TIMEOUT);
67          assertNotNull(requestMessage);
68      }
69  
70      @Override
71      protected void createTable() throws Exception
72      {
73          super.createTable();
74          QueryRunner qr = jdbcConnector.getQueryRunner();
75          qr.update(jdbcConnector.getConnection(), "CREATE TABLE IDS(K VARCHAR(255) NOT NULL PRIMARY KEY, VALUE VARCHAR(255))");
76      }
77  
78      @Override
79      protected void deleteTable() throws Exception
80      {
81          super.deleteTable();
82          QueryRunner qr = jdbcConnector.getQueryRunner();
83          qr.update(jdbcConnector.getConnection(), "DELETE FROM IDS");
84      }
85  }