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 CacheModuleVersionHandlerTest.java

 

Code metrics

0
114
17
1
340
220
17
0.15
6.71
17
1

Classes

Class Line # Actions
CacheModuleVersionHandlerTest 69 114 0% 17 0
1.0100%
 

Contributing tests

This file is covered by 11 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.module.cache.setup.CacheModuleVersionHandler.BROWSER_CACHE_POLICIES_CONTENT_TYPE_ALLOWED;
37    import static info.magnolia.test.hamcrest.NodeMatchers.*;
38    import static org.hamcrest.CoreMatchers.allOf;
39    import static org.hamcrest.CoreMatchers.not;
40    import static org.junit.Assert.*;
41   
42    import info.magnolia.context.MgnlContext;
43    import info.magnolia.importexport.BootstrapUtil;
44    import info.magnolia.jcr.util.NodeTypes;
45    import info.magnolia.jcr.util.NodeUtil;
46    import info.magnolia.module.InstallContext;
47    import info.magnolia.module.ModuleManagementException;
48    import info.magnolia.module.ModuleVersionHandler;
49    import info.magnolia.module.ModuleVersionHandlerTestCase;
50    import info.magnolia.module.cache.AbstractCacheModule;
51    import info.magnolia.module.cache.FlushAllListeningPolicy;
52    import info.magnolia.module.cache.factory.DelegatingCacheFactory;
53    import info.magnolia.module.model.Version;
54    import info.magnolia.repository.RepositoryConstants;
55    import info.magnolia.voting.voters.URIStartsWithVoter;
56   
57    import java.util.Arrays;
58    import java.util.List;
59   
60    import javax.jcr.ImportUUIDBehavior;
61    import javax.jcr.Node;
62    import javax.jcr.RepositoryException;
63    import javax.jcr.Session;
64   
65    import org.hamcrest.Matchers;
66    import org.junit.Before;
67    import org.junit.Test;
68   
 
69    public class CacheModuleVersionHandlerTest extends ModuleVersionHandlerTestCase {
70   
71    private Node denyVotersForCachePolicy;
72    private Session session;
73    private Node filterNode;
74    private Node moduleCfgNode;
75   
 
76  11 toggle @Override
77    protected String getModuleDescriptorPath() {
78  11 return "/META-INF/magnolia/cache.xml";
79    }
80   
 
81  11 toggle @Override
82    protected ModuleVersionHandler newModuleVersionHandlerForTests() {
83  11 return new CacheModuleVersionHandler();
84    }
85   
 
86  11 toggle @Override
87    protected List<String> getModuleDescriptorPathsForTests() {
88  11 return Arrays.asList(
89    "/META-INF/magnolia/core.xml"
90    );
91    }
92   
 
93  11 toggle @Override
94    @Before
95    public void setUp() throws Exception {
96  11 super.setUp();
97  11 session = MgnlContext.getJCRSession(RepositoryConstants.CONFIG);
98  11 denyVotersForCachePolicy = NodeUtil.createPath(session.getRootNode(), "/modules/cache/config/configurations/default/cachePolicy/voters/deny", NodeTypes.ContentNode.NAME);
99  11 filterNode = NodeUtil.createPath(session.getRootNode(), "/server/filters/cache", NodeTypes.ContentNode.NAME);
100  11 moduleCfgNode = NodeUtil.createPath(session.getRootNode(), "/modules/cache/config/", NodeTypes.ContentNode.NAME);
101  11 filterNode.setProperty("cacheConfigurationName", "default"); //needed for update to 5.3
102  11 filterNode.setProperty("class", "info.magnolia.module.cache.filter.CacheFilter"); // needed for update to 5.4
103  11 setupConfigNode("/modules/ui-admincentral/apps/cacheTools"); // needed for update to 5.4
104  11 setupConfigNode("/modules/cache/pages/cachePage"); // needed for update to 5.4
105  11 setupConfigNode("/modules/cache/config/cacheFactory/defaultCacheConfiguration");
106  11 setupConfigProperty("/modules/cache/config/cacheFactory", "class", "info.magnolia.module.cache.ehcache.EhCacheFactory");
107    }
108   
 
109  1 toggle @Test
110    public void addsUIDLPolicyIfNeeded() throws ModuleManagementException, RepositoryException {
111    // GIVEN
112  1 filterNode.setProperty("cacheConfigurationName", "default"); // Unrelated to current test but needed to update to 5.3
113  1 setupBrowserCacheForDotResourcesAsFor4_5();
114   
115    // WHEN
116  1 executeUpdatesAsIfTheCurrentlyInstalledVersionWas(Version.parseVersion("5.1.1"));
117   
118    // THEN
119  1 assertTrue(denyVotersForCachePolicy.hasNode("uidl"));
120  1 assertEquals(URIStartsWithVoter.class.getName(), denyVotersForCachePolicy.getNode("uidl").getProperty("class").getString());
121  1 assertEquals("true", denyVotersForCachePolicy.getNode("uidl").getProperty("enabled").getString());
122  1 assertEquals("2", denyVotersForCachePolicy.getNode("uidl").getProperty("level").getString());
123  1 assertEquals("/.magnolia/admincentral/UIDL", denyVotersForCachePolicy.getNode("uidl").getProperty("pattern").getString());
124    }
125   
 
126  1 toggle @Test
127    public void doesNotAddedUIDLPolicyIfAlreadyPresent() throws ModuleManagementException, RepositoryException {
128    // GIVEN
129  1 setupBrowserCacheForDotResourcesAsFor4_5();
130   
131  1 Node uidl = denyVotersForCachePolicy.addNode("uidl", NodeTypes.ContentNode.NAME);
132  1 uidl.setProperty("enabled", "false");
133  1 uidl.getSession().save();
134   
135    // WHEN
136  1 executeUpdatesAsIfTheCurrentlyInstalledVersionWas(Version.parseVersion("5.1.1"));
137   
138    // THEN
139  1 assertTrue(denyVotersForCachePolicy.hasNode("uidl"));
140  1 assertEquals("false", denyVotersForCachePolicy.getNode("uidl").getProperty("enabled").getString());
141  1 assertFalse(denyVotersForCachePolicy.getNode("uidl").hasProperty("class"));
142    }
143   
 
144  1 toggle @Test
145    public void cleanInstall() throws Exception {
146    // GIVEN
147  1 this.setupConfigNode("/modules/ui-admincentral/apps");
148  1 this.setupConfigNode("/modules/ui-admincentral/config");
149  1 this.setupConfigNode("/server/filters/gzip");
150  1 this.setupConfigNode("/server/filters/cache");
151  1 BootstrapUtil.bootstrap(new String[]{"/mgnl-bootstrap/cache/config.modules.cache.config.contentCaching.defaultPageCache.xml"}, ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW);
152  1 BootstrapUtil.bootstrap(new String[]{"/mgnl-bootstrap/cache/config.modules.cache.config.contentCaching.uuid-key-mapping.xml"}, ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW);
153   
154    // WHEN
155  1 executeUpdatesAsIfTheCurrentlyInstalledVersionWas(null);
156   
157    // THEN
158  1 assertThat(session.getRootNode(), hasNode(CacheModuleVersionHandler.CONFIGURATION_PATH + AbstractCacheModule.DEFAULT_CACHE_CONFIG + "/flushPolicy/policies/flushAll/" +
159    MigrateFlushListeningPolicyTask.EXCLUDED_WORKSPACES));
160  1 Node excludedWorkspaces = session.getRootNode().getNode(CacheModuleVersionHandler.CONFIGURATION_PATH + AbstractCacheModule.DEFAULT_CACHE_CONFIG + "/flushPolicy/policies/flushAll/" +
161    MigrateFlushListeningPolicyTask.EXCLUDED_WORKSPACES);
162  1 assertThat(excludedWorkspaces, hasProperty("magnolia-" + RepositoryConstants.VERSION_STORE, "magnolia-" + RepositoryConstants.VERSION_STORE));
163  1 assertThat(session.getNode("/modules/cache"), not(hasNode("virtualURIMapping")));
164  1 assertThat(session.getNode("/modules/cache/config/contentCaching/defaultPageCache/browserCachePolicy/policies/farFuture/voters/dotCacheExtension"), Matchers.allOf(
165    hasProperty("class", "info.magnolia.voting.voters.URIRegexVoter"),
166    hasProperty("pattern", ".*[~.]cache\\.[^\\.]*")
167    ));
168  1 Node policies = session.getNode("/modules/cache/config/contentCaching/defaultPageCache/browserCachePolicy/policies");
169  1 assertThat(policies, not(hasNode("never")));
170  1 assertThat(policies, hasNode("disableBrowserCacheInDevelopMode"));
171    }
172   
 
173  1 toggle @Test
174    public void testUpdateFrom525() throws Exception {
175    // GIVEN
176  1 this.setupConfigNode(CacheModuleVersionHandler.OBSOLETE_COMPRESSIBLE_CONFIGURATION_PATH);
177  1 setupVirtualUriAsFor4_5();
178  1 setupBrowserCacheForDotResourcesAsFor4_5();
179   
180    // WHEN
181  1 InstallContext installContext = executeUpdatesAsIfTheCurrentlyInstalledVersionWas(Version.parseVersion("5.2.5"));
182   
183    // THEN
184  1 assertThat(session.getRootNode(), not(hasNode(CacheModuleVersionHandler.OBSOLETE_COMPRESSIBLE_CONFIGURATION_PATH)));
185  1 this.assertNoMessages(installContext);
186  1 assertThat(session.getNode("/modules/cache"), not(hasNode("virtualURIMapping")));
187  1 assertThat(session.getNode("/modules/cache/config/contentCaching/defaultPageCache/browserCachePolicy/policies/farFuture/voters/dotCacheExtension"), Matchers.allOf(
188    hasProperty("class", "info.magnolia.voting.voters.URIRegexVoter"),
189    hasProperty("pattern", ".*[~.]cache\\.[^\\.]*")
190    ));
191    }
192   
 
193  1 toggle @Test
194    public void testUpdateFrom53() throws ModuleManagementException, RepositoryException {
195    // GIVEN
196  1 Node flushAll = NodeUtil.createPath(session.getRootNode(), "/modules/cache/config/configurations/default/flushPolicy/policies/flushAll/", NodeTypes.ContentNode.NAME);
197  1 flushAll.setProperty("class", FlushAllListeningPolicy.class.getName());
198  1 flushAll.addNode(MigrateFlushListeningPolicyTask.REPOSITORIES, NodeTypes.ContentNode.NAME)
199    .setProperty(RepositoryConstants.WEBSITE, RepositoryConstants.WEBSITE);
200  1 setupVirtualUriAsFor4_5();
201  1 setupBrowserCacheForDotResourcesAsFor4_5();
202  1 session.save();
203   
204    // WHEN
205  1 InstallContext installContext = executeUpdatesAsIfTheCurrentlyInstalledVersionWas(Version.parseVersion("5.3"));
206   
207    // THEN
208  1 assertThat(session.getRootNode(), hasNode(CacheModuleVersionHandler.CONFIGURATION_PATH + AbstractCacheModule.DEFAULT_CACHE_CONFIG + "/flushPolicy/policies/flushAll/" +
209    MigrateFlushListeningPolicyTask.EXCLUDED_WORKSPACES));
210  1 this.assertNoMessages(installContext);
211  1 assertThat(session.getNode("/modules/cache"), not(hasNode("virtualURIMapping")));
212  1 assertThat(session.getNode("/modules/cache/config/contentCaching/defaultPageCache/browserCachePolicy/policies/farFuture/voters/dotCacheExtension"), Matchers.allOf(
213    hasProperty("class", "info.magnolia.voting.voters.URIRegexVoter"),
214    hasProperty("pattern", ".*[~.]cache\\.[^\\.]*")
215    ));
216    }
217   
 
218  1 toggle @Test
219    public void reconfigurationsFor54() throws Exception {
220    // GIVEN
221  1 setupVirtualUriAsFor4_5();
222  1 setupBrowserCacheForDotResourcesAsFor4_5();
223   
224  1 filterNode.setProperty("cacheConfigurationName", "default");
225   
226  1 final Node cacheContent = NodeUtil.createPath(moduleCfgNode, "configurations/default/executors/store/cacheContent/", NodeTypes.ContentNode.NAME);
227  1 cacheContent.addNode("isCacheableVoters", NodeTypes.ContentNode.NAME);
228   
229    // WHEN
230  1 InstallContext installContext = executeUpdatesAsIfTheCurrentlyInstalledVersionWas(Version.parseVersion("5.2"));
231   
232    // THEN
233  1 assertThat(filterNode, hasProperty("defaultContentCachingConfigurationName", AbstractCacheModule.DEFAULT_CACHE_CONFIG)); // property is renamed as well its value
234   
235    // check a random node to assert the whole contentCaching moved, which we happen to have a the ready in this test
236  1 assertThat(denyVotersForCachePolicy.getPath(), Matchers.equalTo("/modules/cache/config/contentCaching/defaultPageCache/cachePolicy/shouldBypassVoters/deny"));
237   
238  1 assertThat(cacheContent, not(hasNode("isCacheableVoters")));
239  1 this.assertNoMessages(installContext);
240    }
241   
 
242  1 toggle @Test
243    public void updateTo54WithVirtualURICustomized() throws Exception {
244  1 setupBrowserCacheForDotResourcesAsFor4_5();
245  1 setupConfigProperty("/modules/cache/virtualURIMapping/tildeCacheRessources", "class", "info.magnolia.cms.beans.config.RegexpVirtualURIMapping");
246  1 setupConfigProperty("/modules/cache/virtualURIMapping/tildeCacheRessources", "fromURI", "blah");
247  1 setupConfigProperty("/modules/cache/virtualURIMapping/tildeCacheRessources", "toURI", "blu");
248   
249    // WHEN
250  1 executeUpdatesAsIfTheCurrentlyInstalledVersionWas(Version.parseVersion("5.3"));
251   
252    // THEN
253  1 assertThat(session.getNode("/modules/cache"), hasNode("virtualURIMapping"));
254  1 assertThat(session.getNode("/modules/cache/virtualURIMapping"), hasNode("tildeCacheRessources"));
255    }
256   
 
257  1 toggle @Test
258    public void testUpdateTo54InstallNewCacheToolsApp() throws Exception {
259    // GIVEN
260  1 setupBrowserCacheForDotResourcesAsFor4_5();
261   
262    // WHEN
263  1 executeUpdatesAsIfTheCurrentlyInstalledVersionWas(Version.parseVersion("5.3"));
264   
265    // THEN
266  1 assertFalse(session.itemExists("/modules/ui-admincentral/apps/cacheTools"));
267  1 assertFalse(session.itemExists("/modules/cache/pages/cachePage"));
268  1 assertFalse(session.itemExists("/modules/cache/pages"));
269    }
270   
 
271  1 toggle @Test
272    public void updateFrom551() throws Exception {
273    // GIVEN
274  1 Node policies = NodeUtil.createPath(session.getRootNode(), "/modules/cache/config/contentCaching/defaultPageCache/browserCachePolicy/policies", NodeTypes.ContentNode.NAME);
275  1 policies.addNode("anotherPolicy", NodeTypes.ContentNode.NAME);
276  1 policies.addNode("never", NodeTypes.ContentNode.NAME);
277   
278    // WHEN
279  1 executeUpdatesAsIfTheCurrentlyInstalledVersionWas(Version.parseVersion("5.5.1"));
280   
281    // THEN
282  1 assertThat(policies, not(hasNode("never")));
283  1 assertEquals("disableBrowserCacheInDevelopMode", policies.getNodes().nextNode().getName());
284    }
285   
 
286  1 toggle @Test
287    public void updateFrom554() throws Exception {
288    // GIVEN
289  1 final Node cacheFactory = NodeUtil.createPath(session.getRootNode(), "/modules/cache/config/cacheFactory/", NodeTypes.ContentNode.NAME);
290  1 cacheFactory.setProperty("class", "info.magnolia.module.cache.ehcache.EhCacheFactory");
291   
292    // WHEN
293  1 executeUpdatesAsIfTheCurrentlyInstalledVersionWas(Version.parseVersion("5.5.4"));
294   
295    // THEN
296  1 assertThat(session.getRootNode(), hasNode("modules/cache/config/cacheFactory/"));
297  1 assertThat(session.getNode("/modules/cache/config/cacheFactory/"), allOf(
298    hasProperty("class", DelegatingCacheFactory.class.getName()),
299    hasNode("delegateFactories/ehcache")
300    ));
301    }
302   
 
303  1 toggle @Test
304    public void updateFrom556() throws Exception {
305    // GIVEN
306  1 final Node compressionAllowed = NodeUtil.createPath(session.getRootNode(), "modules/cache/config/compression/voters/contentType/allowed", NodeTypes.ContentNode.NAME);
307  1 compressionAllowed.setProperty("1", "text/html");
308  1 compressionAllowed.setProperty("2", "application/x-javascript");
309  1 compressionAllowed.setProperty("3", "text/css");
310   
311  1 final Node cachingAllowed = NodeUtil.createPath(session.getRootNode(),
312    "modules/cache/config/contentCaching/defaultPageCache/browserCachePolicy/policies/resources/voters/contentType/allowed", NodeTypes.ContentNode.NAME);
313  1 cachingAllowed.setProperty("css", "text/css");
314  1 cachingAllowed.setProperty("javascript", "application/x-javascript");
315   
316    // WHEN
317  1 executeUpdatesAsIfTheCurrentlyInstalledVersionWas(Version.parseVersion("5.5.6"));
318   
319    // THEN
320  1 assertThat(session.getNode("/modules/cache/config/compression/voters/contentType/allowed"), hasProperty("2", "application/x-javascript"));
321  1 assertThat(session.getNode("/modules/cache/config/compression/voters/contentType/allowed"), hasProperty("4", "application/javascript"));
322  1 assertThat(session.getNode(BROWSER_CACHE_POLICIES_CONTENT_TYPE_ALLOWED),
323    hasProperty("javascript", "application/javascript"));
324  1 assertThat(session.getNode(BROWSER_CACHE_POLICIES_CONTENT_TYPE_ALLOWED),
325    hasProperty("x-javascript", "application/x-javascript"));
326    }
327   
 
328  3 toggle private void setupVirtualUriAsFor4_5() throws RepositoryException {
329  3 setupConfigProperty("/modules/cache/virtualURIMapping/tildeCacheRessources", "class", "info.magnolia.cms.beans.config.RegexpVirtualURIMapping");
330  3 setupConfigProperty("/modules/cache/virtualURIMapping/tildeCacheRessources", "fromURI", "(\\.*)\\.(\\d{4}-(?:\\d{2}-){5}\\d{3})\\.cache\\.(css|js)");
331  3 setupConfigProperty("/modules/cache/virtualURIMapping/tildeCacheRessources", "toURI", "$1~$2~cache.$3");
332    }
333   
 
334  7 toggle private void setupBrowserCacheForDotResourcesAsFor4_5() throws RepositoryException {
335    // /modules/cache/config/configurations gets renamed to contentCaching
336  7 setupConfigProperty("/modules/cache/config/configurations/default/browserCachePolicy/policies/farFuture/voters/dotCacheExtension", "class", "info.magnolia.voting.voters.URIRegexVoter");
337  7 setupConfigProperty("/modules/cache/config/configurations/default/browserCachePolicy/policies/farFuture/voters/dotCacheExtension", "pattern", ".*\\.cache\\.[^\\.]*");
338    }
339   
340    }