1
2
3
4
5
6
7 package org.mule.cache;
8
9 import org.mule.api.MuleEvent;
10 import org.mule.api.MuleMessage;
11 import org.mule.api.processor.MessageProcessor;
12 import org.mule.api.routing.filter.Filter;
13 import org.mule.api.store.ObjectStore;
14 import org.mule.cache.keygenerator.KeyGenerator;
15 import org.mule.cache.responsegenerator.ResponseGenerator;
16 import org.mule.routing.filters.AcceptAllFilter;
17 import org.mule.tck.junit4.AbstractMuleTestCase;
18
19 import java.io.Serializable;
20
21 import org.junit.Test;
22 import org.mockito.Matchers;
23 import org.mockito.Mockito;
24
25 import static org.junit.Assert.assertSame;
26 import static org.mockito.Matchers.any;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31
32 public class ObjectStoreCachingStrategyTestCase extends AbstractMuleTestCase
33 {
34
35 private static final String OBJECT_KEY = "key";
36
37 @Test
38 public void testCacheMiss() throws Exception
39 {
40 MuleEvent mockEvent = mock(MuleEvent.class);
41 final MuleEvent mockResponse = mock(MuleEvent.class);
42
43 KeyGenerator keyGenerator = mock(KeyGenerator.class);
44 when(keyGenerator.generateKey(mockEvent)).thenReturn(OBJECT_KEY);
45
46 ObjectStore<MuleEvent> objectStore = mock(ObjectStore.class);
47 when(objectStore.retrieve(OBJECT_KEY)).thenReturn(null);
48
49 Filter consumablePayloadFilter = mock(Filter.class);
50 when(consumablePayloadFilter.accept(Mockito.<MuleMessage>any())).thenReturn(true);
51
52 ObjectStoreCachingStrategy cachingStrategy = new ObjectStoreCachingStrategy();
53 cachingStrategy.setKeyGenerator(keyGenerator);
54 cachingStrategy.setStore(objectStore);
55 cachingStrategy.setConsumableFilter(consumablePayloadFilter);
56
57 MessageProcessor cachedMessageProcessor = mock(MessageProcessor.class);
58 when(cachedMessageProcessor.process(mockEvent)).thenReturn(mockResponse);
59 MuleEvent response = cachingStrategy.process(mockEvent, cachedMessageProcessor);
60
61 assertSame(mockResponse, response);
62 Mockito.verify(objectStore, Mockito.times(1)).store(OBJECT_KEY, mockResponse);
63 }
64
65 @Test
66 public void testCacheHit() throws Exception
67 {
68 MuleEvent mockEvent = mock(MuleEvent.class);
69 final MuleEvent mockResponse = mock(MuleEvent.class);
70
71 KeyGenerator keyGenerator = mock(KeyGenerator.class);
72 when(keyGenerator.generateKey(mockEvent)).thenReturn(OBJECT_KEY);
73
74 ResponseGenerator responseGenerator = mock(ResponseGenerator.class);
75 when(responseGenerator.create(mockEvent, mockResponse)).thenReturn(mockResponse);
76
77 ObjectStore<MuleEvent> objectStore = mock(ObjectStore.class);
78 when(objectStore.retrieve(OBJECT_KEY)).thenReturn(mockResponse);
79
80 ObjectStoreCachingStrategy cachingStrategy = new ObjectStoreCachingStrategy();
81 cachingStrategy.setKeyGenerator(keyGenerator);
82 cachingStrategy.setStore(objectStore);
83 cachingStrategy.setResponseGenerator(responseGenerator);
84 cachingStrategy.setConsumableFilter(new AcceptAllFilter());
85
86 MessageProcessor cachedMessageProcessor = mock(MessageProcessor.class);
87 MuleEvent response = cachingStrategy.process(mockEvent, cachedMessageProcessor);
88
89 assertSame(mockResponse, response);
90 Mockito.verify(objectStore, Mockito.times(0)).store(OBJECT_KEY, mockResponse);
91 verify(cachedMessageProcessor, times(0)).process(mockEvent);
92 }
93
94 @Test
95 public void testConsumableRequestPayloadIsNotCached() throws Exception
96 {
97 MuleEvent mockEvent = mock(MuleEvent.class);
98 final MuleEvent mockResponse = mock(MuleEvent.class);
99
100 ObjectStore<MuleEvent> objectStore = mock(ObjectStore.class);
101
102 Filter consumablePayloadFilter = mock(Filter.class);
103 when(consumablePayloadFilter.accept(Mockito.<MuleMessage>any())).thenReturn(false);
104
105 ObjectStoreCachingStrategy cachingStrategy = new ObjectStoreCachingStrategy();
106 cachingStrategy.setStore(objectStore);
107 cachingStrategy.setConsumableFilter(consumablePayloadFilter);
108
109 MessageProcessor cachedMessageProcessor = mock(MessageProcessor.class);
110 when(cachedMessageProcessor.process(mockEvent)).thenReturn(mockResponse);
111 MuleEvent response = cachingStrategy.process(mockEvent, cachedMessageProcessor);
112
113 assertSame(mockResponse, response);
114 Mockito.verify(objectStore, Mockito.times(0)).retrieve(mockEvent);
115 }
116
117 @Test
118 public void testConsumableResponsePayloadIsNotCached() throws Exception
119 {
120 MuleEvent mockEvent = mock(MuleEvent.class);
121 final MuleEvent mockResponse = mock(MuleEvent.class);
122
123 KeyGenerator keyGenerator = mock(KeyGenerator.class);
124 when(keyGenerator.generateKey(mockEvent)).thenReturn(OBJECT_KEY);
125
126 ResponseGenerator responseGenerator = mock(ResponseGenerator.class);
127 when(responseGenerator.create(mockEvent, mockResponse)).thenReturn(mockResponse);
128
129 ObjectStore<MuleEvent> objectStore = mock(ObjectStore.class);
130
131 Filter consumablePayloadFilter = mock(Filter.class);
132 when(consumablePayloadFilter.accept(Mockito.<MuleMessage>any())).thenReturn(true).thenReturn(false);
133
134 ObjectStoreCachingStrategy cachingStrategy = new ObjectStoreCachingStrategy();
135 cachingStrategy.setKeyGenerator(keyGenerator);
136 cachingStrategy.setResponseGenerator(responseGenerator);
137 cachingStrategy.setStore(objectStore);
138 cachingStrategy.setConsumableFilter(consumablePayloadFilter);
139
140 MessageProcessor cachedMessageProcessor = mock(MessageProcessor.class);
141 when(cachedMessageProcessor.process(mockEvent)).thenReturn(mockResponse);
142 MuleEvent response = cachingStrategy.process(mockEvent, cachedMessageProcessor);
143
144
145 assertSame(mockResponse, response);
146 Mockito.verify(objectStore, Mockito.times(0)).store(OBJECT_KEY, mockResponse);
147 }
148
149 @Test
150 public void testIgnoresKeyGeneratorError() throws Exception
151 {
152 MuleEvent mockEvent = mock(MuleEvent.class);
153 final MuleEvent mockResponse = mock(MuleEvent.class);
154
155 KeyGenerator keyGenerator = mock(KeyGenerator.class);
156 when(keyGenerator.generateKey(mockEvent)).thenThrow(new IllegalArgumentException());
157
158 Filter consumablePayloadFilter = mock(Filter.class);
159 when(consumablePayloadFilter.accept(Mockito.<MuleMessage>any())).thenReturn(true);
160
161 ObjectStore<MuleEvent> objectStore = mock(ObjectStore.class);
162
163 ObjectStoreCachingStrategy cachingStrategy = new ObjectStoreCachingStrategy();
164 cachingStrategy.setKeyGenerator(keyGenerator);
165 cachingStrategy.setStore(objectStore);
166 cachingStrategy.setConsumableFilter(consumablePayloadFilter);
167
168 MessageProcessor cachedMessageProcessor = mock(MessageProcessor.class);
169 when(cachedMessageProcessor.process(mockEvent)).thenReturn(mockResponse);
170 MuleEvent response = cachingStrategy.process(mockEvent, cachedMessageProcessor);
171
172 assertSame(mockResponse, response);
173 verify(cachedMessageProcessor, times(1)).process(mockEvent);
174 Mockito.verify(objectStore, Mockito.times(0)).retrieve(any(Serializable.class));
175 Mockito.verify(objectStore, Mockito.times(0)).store(any(Serializable.class), Matchers.<MuleEvent>any(MuleEvent.class));
176 }
177 }