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.cache.integration;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleMessage;
11  import org.mule.api.client.MuleClient;
12  import org.mule.tck.junit4.FunctionalTestCase;
13  
14  import java.util.Map;
15  
16  import org.junit.Test;
17  
18  import static org.junit.Assert.assertEquals;
19  
20  /**
21   * Defines a contract test for {@link org.mule.cache.CachingStrategy}.
22   * <p/>
23   * Subclasses must provide a configuration of a cachingStrategy bean
24   */
25  public abstract class AbstractCachingStrategyTestCase extends FunctionalTestCase
26  {
27  
28      @Override
29      protected String getConfigResources()
30      {
31          return "org/mule/cache/integration/message-caching-config.xml," + getCachingStrategyConfigResource();
32      }
33  
34      /**
35       * @return comma separated list of the mule config file path's containing
36       *         the declaration of the caching strategy bean and any extra
37       *         needed configuration.
38       */
39      protected abstract String getCachingStrategyConfigResource();
40  
41      @Test
42      public void testCachesMessageRequestResponse() throws Exception
43      {
44          MuleClient client = muleContext.getClient();
45  
46          DefaultMuleMessage message1 = new DefaultMuleMessage("test1", (Map) null, muleContext);
47          DefaultMuleMessage message2 = new DefaultMuleMessage("test2", (Map) null, muleContext);
48  
49          MuleMessage msg = client.send("vm://testRequestResponse", message1);
50          assertEquals("0 Processed", msg.getPayload());
51  
52          msg = client.send("vm://testRequestResponse", message2);
53          assertEquals("1 Processed", msg.getPayload());
54  
55          // Checks that resending message 1 gets the response form the cache
56          msg = client.send("vm://testRequestResponse", message1);
57          assertEquals("0 Processed", msg.getPayload());
58      }
59  
60      @Test
61      public void testCachesMessageOneWay() throws Exception
62      {
63          MuleClient client = muleContext.getClient();
64  
65          DefaultMuleMessage message1 = new DefaultMuleMessage("test3", (Map) null, muleContext);
66          DefaultMuleMessage message2 = new DefaultMuleMessage("test4", (Map) null, muleContext);
67  
68          client.dispatch("vm://testOneWay", message1);
69          MuleMessage msg = client.request("vm://output", 5000);
70          assertEquals("0 Processed", msg.getPayload());
71  
72          client.dispatch("vm://testOneWay", message2);
73          msg = client.request("vm://output", 5000);
74          assertEquals("1 Processed", msg.getPayload());
75  
76          // Checks that resending message 1 gets the response form the cache
77          client.dispatch("vm://testOneWay", message1);
78          msg = client.request("vm://output", 5000);
79          assertEquals("0 Processed", msg.getPayload());
80      }
81  }