Clover icon

Magnolia Module Cache 5.5.9

  1. Project Clover database Mon Nov 25 2019 16:46:50 CET
  2. Package info.magnolia.module.cache.cachepolicy

File DefaultTest.java

 

Code metrics

0
95
14
2
293
174
14
0.15
6.79
7
1

Classes

Class Line # Actions
DefaultTest 68 94 0% 13 5
0.9532710395.3%
DefaultTest.DummyCacheKeyGererator 280 1 0% 1 0
1.0100%
 

Contributing tests

This file is covered by 10 tests. .

Source view

1    /**
2    * This file Copyright (c) 2011-2018 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.module.cache.cachepolicy;
35   
36    import static org.hamcrest.Matchers.equalTo;
37    import static org.junit.Assert.*;
38    import static org.mockito.Matchers.anyObject;
39    import static org.mockito.Mockito.*;
40   
41    import info.magnolia.cms.core.AggregationState;
42    import info.magnolia.cms.core.Channel;
43    import info.magnolia.cms.security.User;
44    import info.magnolia.context.MgnlContext;
45    import info.magnolia.context.WebContext;
46    import info.magnolia.module.cache.AbstractCacheTest;
47    import info.magnolia.module.cache.Cache;
48    import info.magnolia.module.cache.CachePolicyResult;
49    import info.magnolia.module.cache.FlushPolicy;
50    import info.magnolia.module.cache.cachekey.CacheKeyGenerator;
51    import info.magnolia.module.cache.cachekey.DefaultCacheKey;
52    import info.magnolia.module.cache.cachepolicy.instructor.CacheInstructor;
53    import info.magnolia.module.cache.filter.UncacheableEntry;
54    import info.magnolia.objectfactory.guice.GuiceUtils;
55    import info.magnolia.test.ComponentsTestUtil;
56   
57    import java.util.Arrays;
58    import java.util.HashMap;
59    import java.util.Locale;
60    import java.util.Map;
61   
62    import javax.servlet.http.HttpServletRequest;
63   
64    import org.junit.After;
65    import org.junit.Before;
66    import org.junit.Test;
67   
 
68    public class DefaultTest extends AbstractCacheTest {
69   
70    private Default policy;
71    private AggregationState aggregationState;
72    private WebContext webCtx;
73    private HttpServletRequest request;
74    private Map<String, String[]> params;
75    private final CacheInstructor cacheInstructor = new CacheInstructor();
76   
 
77  10 toggle @Before
78    public void setUp() throws Exception {
79  10 super.setUp();
80  10 webCtx = mock(WebContext.class);
81  10 request = mock(HttpServletRequest.class);
82  10 aggregationState = new AggregationState();
83  10 MgnlContext.setInstance(webCtx);
84   
85  10 Default defaultPolicy = new Default(null, GuiceUtils.providerForInstance(cacheInstructor));
86  10 defaultPolicy.setRefreshOnNoCacheRequests(true);
87  10 policy = defaultPolicy;
88   
89  10 Boolean isSecure = false;
90  10 String uri = "localhost";
91  10 String serverName = "test";
92  10 params = new HashMap();
93   
94  10 when(webCtx.getContextPath()).thenReturn(uri);
95  10 when(webCtx.getRequest()).thenReturn(request);
96  10 when(webCtx.getRequest()).thenReturn(request);
97  10 User user = mock(User.class);
98  10 when(webCtx.getUser()).thenReturn(user);
99   
100  10 when(request.getServerName()).thenReturn(serverName);
101  10 when(request.isSecure()).thenReturn(isSecure);
102  10 when(request.getMethod()).thenReturn("GET");
103  10 when(request.getParameterMap()).thenReturn(params);
104   
105  10 aggregationState.setCharacterEncoding("UTF-8");
106  10 aggregationState.setLocale(Locale.ENGLISH);
107   
108  10 final Channel channel = new Channel();
109  10 channel.setName("mobile");
110  10 aggregationState.setChannel(channel);
111  10 aggregationState.setOriginalURI(uri);
112    }
113   
 
114  1 toggle @Test
115    public void testRetrieveDefaultCacheKey() {
116    // GIVEN
117  1 params.put("testkey", new String[]{"testvalue"});
118   
119    // WHEN
120  1 final DefaultCacheKey cacheKey = (DefaultCacheKey) policy.retrieveCacheKey(aggregationState);
121   
122    // THEN
123  1 assertThat(cacheKey.getUri(), equalTo("localhost"));
124  1 assertThat(cacheKey.getServerName(), equalTo("test"));
125  1 assertThat(cacheKey.getLocale(), equalTo("en"));
126  1 assertThat(cacheKey.getChannel(), equalTo("mobile"));
127  1 assertThat(cacheKey.getParams().toString(), equalTo("{testkey=testvalue}"));
128  1 assertThat(cacheKey.getIsSecured(), equalTo(false));
129  1 assertThat(cacheKey.getMethod(), equalTo("get"));
130   
131    }
132   
 
133  1 toggle @Test
134    public void testRetrieveDefaultCacheKeyWhenRequestParamHasMoreValuesDefined() {
135    // GIVEN
136  1 params.put("bTestKey", new String[]{"bTestValue1", "bTestValue2", "bTestValue3"});
137  1 params.put("aTestKey", new String[]{"aTestValue"});
138   
139    // WHEN
140  1 final DefaultCacheKey cacheKey = (DefaultCacheKey) policy.retrieveCacheKey(aggregationState);
141   
142    // THEN
143  1 assertThat(cacheKey.getUri(), equalTo("localhost"));
144  1 assertThat(cacheKey.getServerName(), equalTo("test"));
145  1 assertThat(cacheKey.getLocale(), equalTo("en"));
146  1 assertThat(cacheKey.getChannel(), equalTo("mobile"));
147  1 assertThat(cacheKey.getParams().toString(), equalTo("{aTestKey=aTestValue, bTestKey=bTestValue1;bTestValue2;bTestValue3}"));
148  1 assertThat(cacheKey.getIsSecured(), equalTo(false));
149  1 assertThat(cacheKey.getMethod(), equalTo("get"));
150    }
151   
 
152  1 toggle @Test
153    public void testSemicolonIsEscapedInParameterValues() {
154    // GIVEN
155  1 params.put("bTestKey", new String[]{";bTestValue1;", "bTestVal;ue2", "bTestValue3"});
156  1 params.put("aTestKey", new String[]{"aTestValue"});
157   
158    // WHEN
159  1 final DefaultCacheKey cacheKey = (DefaultCacheKey) policy.retrieveCacheKey(aggregationState);
160   
161    // THEN
162  1 assertThat(cacheKey.getParams().toString(), equalTo("{aTestKey=aTestValue, bTestKey=;;bTestValue1;;;bTestVal;;ue2;bTestValue3}"));
163    }
164   
 
165  1 toggle @Test
166    public void testSemicolonIsEscapedInParameterValues1() {
167    // GIVEN
168  1 params.put("bTestKey", new String[]{";bTestValue1;", "bTestVal;ue2", "bTestValue3"});
169  1 params.put("aTestKey", new String[]{"aTestValue"});
170   
171    // WHEN
172  1 policy.retrieveCacheKey(aggregationState);
173   
174    // THEN
175  1 assertEquals("[aTestValue]", Arrays.asList(params.get("aTestKey")).toString());
176  1 assertEquals("[;bTestValue1;, bTestVal;ue2, bTestValue3]", Arrays.asList(params.get("bTestKey")).toString());
177    }
178   
 
179  1 toggle @Test
180    public void testDontSortParameterValuesInPlace() {
181    // GIVEN
182  1 params.put("key", new String[]{"1", "3", "2"});
183   
184    // WHEN
185  1 policy.retrieveCacheKey(aggregationState);
186   
187    // THEN
188  1 assertEquals("Params values in request should remain unchanged", "[1, 3, 2]", Arrays.asList(params.get("key")).toString());
189    }
190   
 
191  1 toggle @Test
192    public void testRefreshWithCacheControlHeader() {
193    // GIVEN
194  1 Cache cache = mock(Cache.class);
195  1 FlushPolicy flushPolicy = mock(FlushPolicy.class);
196   
197  1 when(request.getHeader("Cache-Control")).thenReturn("no-cache");
198  1 when(cache.get((Object) anyObject())).thenReturn("cached-key");
199   
200    // WHEN
201  1 CachePolicyResult result = policy.shouldCache(cache, aggregationState, flushPolicy);
202   
203    // THEN
204  1 assertEquals("store", result.getBehaviour().toString());
205  1 assertEquals(null, result.getCachedEntry());
206    }
207   
 
208  1 toggle @Test
209    public void testRefreshWithPragmaHeader() {
210    // GIVEN
211  1 Cache cache = mock(Cache.class);
212  1 FlushPolicy flushPolicy = mock(FlushPolicy.class);
213   
214  1 when(request.getHeader("Pragma")).thenReturn("no-cache");
215  1 when(cache.get((Object) anyObject())).thenReturn("cached-key");
216   
217    // WHEN
218  1 CachePolicyResult result = policy.shouldCache(cache, aggregationState, flushPolicy);
219   
220    // THEN
221  1 assertEquals("store", result.getBehaviour().toString());
222  1 assertEquals(null, result.getCachedEntry());
223    }
224   
 
225  0 toggle public void testStore() {
226    // GIVEN
227  0 Cache cache = mock(Cache.class);
228   
229    // WHEN
230  0 CachePolicyResult result = policy.shouldCache(cache, aggregationState, null);
231   
232    // THEN
233  0 assertEquals(CachePolicyResult.store, result.getBehaviour());
234  0 assertNull(result.getCachedEntry());
235    }
236   
 
237  1 toggle @Test
238    public void testUseCache() {
239    // GIVEN
240  1 Cache cache = mock(Cache.class);
241  1 Object value = "someValue";
242  1 when(cache.get(anyObject())).thenReturn(value);
243    // WHEN
244  1 CachePolicyResult result = policy.shouldCache(cache, aggregationState, null);
245   
246    // THEN
247  1 assertEquals(CachePolicyResult.useCache, result.getBehaviour());
248  1 assertNotNull(result.getCachedEntry());
249    }
250   
 
251  1 toggle @Test
252    public void testBypass() {
253    // GIVEN
254  1 Cache cache = mock(Cache.class);
255  1 Object value = mock(UncacheableEntry.class);
256  1 when(cache.get(anyObject())).thenReturn(value);
257    // WHEN
258  1 CachePolicyResult result = policy.shouldCache(cache, aggregationState, null);
259   
260    // THEN
261  1 assertEquals(CachePolicyResult.bypass, result.getBehaviour());
262  1 assertNull(result.getCachedEntry());
263    }
264   
 
265  1 toggle @Test
266    public void testCustomCacheKeyGenerator() {
267    // GIVEN
268  1 cacheInstructor.setKeyGenerator(new DummyCacheKeyGererator());
269   
270    // WHEN
271  1 Object key = policy.retrieveCacheKey(aggregationState);
272   
273    // THEN
274  1 assertThat(key.toString(), equalTo("key"));
275   
276    //AFTER
277  1 ComponentsTestUtil.clear();
278    }
279   
 
280    public static class DummyCacheKeyGererator implements CacheKeyGenerator<String> {
281   
 
282  1 toggle @Override
283    public String generateKey(AggregationState aggregationState) {
284  1 return "key";
285    }
286    }
287   
 
288  10 toggle @After
289    public void tearDown() throws Exception {
290  10 super.tearDown();
291  10 MgnlContext.setInstance(null);
292    }
293    }