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