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.config;
8   
9   import org.mule.api.MuleEvent;
10  import org.mule.api.MuleMessage;
11  import org.mule.api.routing.filter.Filter;
12  import org.mule.cache.CachingMessageProcessor;
13  import org.mule.cache.CachingStrategy;
14  import org.mule.cache.ObjectStoreCachingStrategy;
15  import org.mule.cache.keygenerator.ExpressionKeyGenerator;
16  import org.mule.cache.keygenerator.KeyGenerator;
17  import org.mule.cache.responsegenerator.ResponseGenerator;
18  import org.mule.construct.SimpleFlowConstruct;
19  import org.mule.tck.junit4.FunctionalTestCase;
20  
21  import java.io.NotSerializableException;
22  import java.io.Serializable;
23  
24  import org.junit.Test;
25  
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.assertNotNull;
28  import static org.junit.Assert.assertTrue;
29  
30  public class CachingStrategyConfigTestCase extends FunctionalTestCase
31  {
32  
33      public CachingStrategyConfigTestCase()
34      {
35          setDisposeContextPerClass(true);
36      }
37  
38      @Override
39      protected String getConfigResources()
40      {
41          return "org/mule/cache/config/caching-strategy-config.xml";
42      }
43  
44      @Test
45      public void testKeyGenerationExpressionConfig() throws Exception
46      {
47          String cacheFlow = "CacheRouterWithkeyGenerationExpression";
48          CachingMessageProcessor cacheMessageProcessor = getCachingMessageProcessorFromFlow(cacheFlow);
49  
50          CachingStrategy cachingStrategy = cacheMessageProcessor.getCachingStrategy();
51          assertTrue(cachingStrategy instanceof ObjectStoreCachingStrategy);
52          ObjectStoreCachingStrategy objectStoreCachingStrategy = (ObjectStoreCachingStrategy) cachingStrategy;
53  
54          assertTrue(objectStoreCachingStrategy.getKeyGenerator() instanceof ExpressionKeyGenerator);
55          ExpressionKeyGenerator keyGenerator = (ExpressionKeyGenerator) objectStoreCachingStrategy.getKeyGenerator();
56          assertEquals("#[payload]", keyGenerator.getExpression());
57      }
58  
59      @Test
60      public void testKeyGeneratorConfig() throws Exception
61      {
62          String cacheFlow = "CacheRouterWithKeyGenerator";
63          CachingMessageProcessor cacheMessageProcessor = getCachingMessageProcessorFromFlow(cacheFlow);
64  
65          CachingStrategy cachingStrategy = cacheMessageProcessor.getCachingStrategy();
66          assertTrue(cachingStrategy instanceof ObjectStoreCachingStrategy);
67          ObjectStoreCachingStrategy objectStoreCachingStrategy = (ObjectStoreCachingStrategy) cachingStrategy;
68  
69          assertTrue(objectStoreCachingStrategy.getKeyGenerator() instanceof TestKeyGenerator);
70      }
71  
72      @Test
73      public void testResponseGeneratorConfig() throws Exception
74      {
75          String cacheFlow = "CacheRouterWithResponseGenerator";
76          CachingMessageProcessor cacheMessageProcessor = getCachingMessageProcessorFromFlow(cacheFlow);
77  
78          CachingStrategy cachingStrategy = cacheMessageProcessor.getCachingStrategy();
79          assertTrue(cachingStrategy instanceof ObjectStoreCachingStrategy);
80          ObjectStoreCachingStrategy objectStoreCachingStrategy = (ObjectStoreCachingStrategy) cachingStrategy;
81  
82          assertTrue(objectStoreCachingStrategy.getResponseGenerator() instanceof TestResponseGenerator);
83      }
84  
85      @Test
86      public void testConsumableFilterConfig() throws Exception
87      {
88          String cacheFlow = "CacheRouterWithConsumableFilter";
89          CachingMessageProcessor cacheMessageProcessor = getCachingMessageProcessorFromFlow(cacheFlow);
90  
91          CachingStrategy cachingStrategy = cacheMessageProcessor.getCachingStrategy();
92          assertTrue(cachingStrategy instanceof ObjectStoreCachingStrategy);
93          ObjectStoreCachingStrategy objectStoreCachingStrategy = (ObjectStoreCachingStrategy) cachingStrategy;
94  
95          assertTrue(objectStoreCachingStrategy.getConsumableFilter() instanceof TestConsumableFilter);
96      }
97  
98      private CachingMessageProcessor getCachingMessageProcessorFromFlow(String cacheFlow)
99      {
100         SimpleFlowConstruct flow = (SimpleFlowConstruct) muleContext.getRegistry().get(cacheFlow);
101         assertNotNull(flow);
102         return (CachingMessageProcessor) flow.getMessageProcessors().get(0);
103     }
104 
105     public static class TestKeyGenerator implements KeyGenerator
106     {
107 
108         public TestKeyGenerator()
109         {
110 
111         }
112 
113         public Serializable generateKey(MuleEvent event) throws NotSerializableException
114         {
115             return "theKey";
116         }
117     }
118 
119     public static class TestResponseGenerator implements ResponseGenerator
120     {
121 
122         public MuleEvent create(MuleEvent request, MuleEvent cachedResponse)
123         {
124             return cachedResponse;
125         }
126     }
127 
128     public static class TestConsumableFilter implements Filter
129     {
130 
131         public boolean accept(MuleMessage muleMessage)
132         {
133             return false;
134         }
135     }
136 }