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.setup

File MigrateCacheFilterConfigurationTaskTest.java

 

Code metrics

0
35
5
2
141
76
5
0.14
7
2.5
1

Classes

Class Line # Actions
MigrateCacheFilterConfigurationTaskTest 63 34 0% 4 0
1.0100%
MigrateCacheFilterConfigurationTaskTest.DummyCacheFilter 135 1 0% 1 2
0.00%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /**
2    * This file Copyright (c) 2015-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.setup;
35   
36    import static info.magnolia.test.hamcrest.NodeMatchers.hasProperty;
37    import static org.hamcrest.CoreMatchers.not;
38    import static org.junit.Assert.assertThat;
39    import static org.mockito.Mockito.*;
40   
41    import info.magnolia.context.WebContext;
42    import info.magnolia.jcr.util.NodeTypes;
43    import info.magnolia.jcr.util.NodeUtil;
44    import info.magnolia.module.InstallContext;
45    import info.magnolia.module.cache.AbstractCacheModule;
46    import info.magnolia.module.cache.CacheModule;
47    import info.magnolia.module.cache.cachepolicy.result.CachePolicyResultProvider;
48    import info.magnolia.module.cache.filter.CacheFilter;
49    import info.magnolia.module.cache.mbean.CacheMonitor;
50    import info.magnolia.repository.RepositoryConstants;
51    import info.magnolia.test.mock.jcr.MockNode;
52   
53    import javax.inject.Provider;
54    import javax.jcr.Node;
55    import javax.jcr.Session;
56   
57    import org.junit.Before;
58    import org.junit.Test;
59   
60    /**
61    * Test class for {@link MigrateCacheFilterConfigurationTask}.
62    */
 
63    public class MigrateCacheFilterConfigurationTaskTest {
64   
65    private InstallContext context;
66   
67    private MigrateCacheFilterConfigurationTask task;
68   
69    private Node cacheFilters;
70   
 
71  3 toggle @Before
72    public void setUp() throws Exception {
73  3 context = mock(InstallContext.class);
74  3 task = new MigrateCacheFilterConfigurationTask();
75  3 Session jcrSession = mock(Session.class);
76  3 when(context.getJCRSession(RepositoryConstants.CONFIG)).thenReturn(jcrSession);
77  3 Node rootNode = new MockNode();
78  3 when(jcrSession.getRootNode()).thenReturn(rootNode);
79  3 Node filters = NodeUtil.createPath(jcrSession.getRootNode(), "/server/filters", NodeTypes.ContentNode.NAME);
80  3 when(jcrSession.nodeExists("/server/filters")).thenReturn(true);
81  3 when(jcrSession.getNode("/server/filters")).thenReturn(filters);
82  3 cacheFilters = filters.addNode("cache");
83    }
84   
 
85  1 toggle @Test
86    public void testOneFilterSetup() throws Exception {
87    //GIVEN
88  1 cacheFilters.setProperty("cacheConfigurationName", "default");
89  1 cacheFilters.setProperty("class", "info.magnolia.module.cache.filter.CacheFilter");
90   
91    //WHEN
92  1 task.execute(context);
93   
94    //THEN
95  1 assertThat(cacheFilters, hasProperty("defaultContentCachingConfigurationName", AbstractCacheModule.DEFAULT_CACHE_CONFIG));
96    }
97   
 
98  1 toggle @Test
99    public void testMultiFilterSetup() throws Exception {
100  1 Node filter1 = cacheFilters.addNode("filter1", NodeTypes.ContentNode.NAME);
101  1 filter1.setProperty("cacheConfigurationName", "default");
102  1 filter1.setProperty("class", "info.magnolia.module.cache.filter.CacheFilter");
103  1 Node filter2 = cacheFilters.addNode("filter2", NodeTypes.ContentNode.NAME);
104  1 filter2.setProperty("cacheConfigurationName", "non-default");
105  1 filter2.setProperty("class", "info.magnolia.module.cache.filter.CacheFilter");
106  1 Node filter3 = cacheFilters.addNode("filter3", NodeTypes.ContentNode.NAME);
107  1 filter3.setProperty(AbstractCacheModule.DEFAULT_CACHE_CONFIG, AbstractCacheModule.DEFAULT_CACHE_CONFIG);
108  1 filter3.setProperty("class", "info.magnolia.module.cache.filter.CacheFilter");
109  1 Node filter4 = cacheFilters.addNode("filter4", NodeTypes.ContentNode.NAME);
110  1 filter4.setProperty("class", "info.magnolia.module.cache.filter.DummyFilter");
111   
112    // WHEN
113  1 task.execute(context);
114   
115    // THEN
116  1 assertThat(filter1, hasProperty("defaultContentCachingConfigurationName", AbstractCacheModule.DEFAULT_CACHE_CONFIG));
117  1 assertThat(filter2, hasProperty("defaultContentCachingConfigurationName", "non-default"));
118  1 assertThat(filter3, hasProperty("defaultContentCachingConfigurationName", AbstractCacheModule.DEFAULT_CACHE_CONFIG));
119  1 assertThat(filter4, not(hasProperty("defaultContentCachingConfigurationName", AbstractCacheModule.DEFAULT_CACHE_CONFIG)));
120    }
121   
 
122  1 toggle @Test
123    public void testCustomFilterSetup() throws Exception {
124    //GIVEN
125  1 cacheFilters.setProperty("cacheConfigurationName", "custom");
126  1 cacheFilters.setProperty("class", "info.magnolia.module.cache.setup.MigrateCacheFilterConfigurationTaskTest$DummyCacheFilter");
127   
128    //WHEN
129  1 task.execute(context);
130   
131    //THEN
132  1 assertThat(cacheFilters, hasProperty("defaultContentCachingConfigurationName", "custom"));
133    }
134   
 
135    private class DummyCacheFilter extends CacheFilter {
136   
 
137  0 toggle public DummyCacheFilter(Provider<WebContext> webContextProvider, CacheModule cacheModule, CacheMonitor cacheMonitor, Provider<CachePolicyResultProvider> cachePolicyResultProvider) {
138  0 super(webContextProvider, cacheModule, cacheMonitor, cachePolicyResultProvider);
139    }
140    }
141    }