View Javadoc

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