Clover icon

Magnolia Resources Module 2.4.2

  1. Project Clover database Fri Nov 6 2015 16:15:26 CET
  2. Package info.magnolia.module.resources.setup

File TemplateMigrationTaskTest.java

 

Code metrics

10
133
19
1
410
243
25
0.19
7
19
1.32

Classes

Class Line # Actions
TemplateMigrationTaskTest 78 133 0% 25 3
0.981481598.1%
 

Contributing tests

This file is covered by 13 tests. .

Source view

1    /**
2    * This file Copyright (c) 2015 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.resources.setup;
35   
36    import static info.magnolia.module.resources.setup.TemplateMigrationTask.*;
37    import static info.magnolia.resourceloader.dummy.DummyResourceOrigin.Differentiator.qux;
38    import static info.magnolia.test.hamcrest.ExceptionMatcher.strictlyInstanceOf;
39    import static info.magnolia.test.hamcrest.ExecutionMatcher.*;
40    import static info.magnolia.test.hamcrest.NodeMatchers.hasProperty;
41    import static org.hamcrest.CoreMatchers.*;
42    import static org.junit.Assert.*;
43    import static org.mockito.Mockito.*;
44   
45    import info.magnolia.context.MgnlContext;
46    import info.magnolia.context.SystemContext;
47    import info.magnolia.jcr.util.NodeTypes;
48    import info.magnolia.jcr.util.NodeUtil;
49    import info.magnolia.module.InstallContext;
50    import info.magnolia.module.delta.Task;
51    import info.magnolia.module.delta.TaskExecutionException;
52    import info.magnolia.repository.RepositoryManager;
53    import info.magnolia.resourceloader.ResourceOrigin;
54    import info.magnolia.resourceloader.dummy.DummyResourceOrigin;
55    import info.magnolia.resourceloader.jcr.JcrResourceOrigin;
56    import info.magnolia.resourceloader.jcr.JcrResourceOriginFactory;
57    import info.magnolia.resourceloader.layered.LayeredResourceOrigin;
58    import info.magnolia.resourceloader.layered.LayeredResourceOriginFactory;
59    import info.magnolia.test.ComponentsTestUtil;
60    import info.magnolia.test.hamcrest.Execution;
61    import info.magnolia.test.mock.MockContext;
62    import info.magnolia.test.mock.jcr.MockSession;
63   
64    import java.util.Calendar;
65   
66    import javax.jcr.Node;
67    import javax.jcr.RepositoryException;
68    import javax.jcr.Session;
69   
70    import org.junit.Before;
71    import org.junit.Test;
72   
73    import com.google.inject.util.Providers;
74   
75    /**
76    * {@link info.magnolia.test.RepositoryTestCase}s for {@link TemplateMigrationTask}.
77    */
 
78    public class TemplateMigrationTaskTest {
79   
80    private TemplateMigrationTask task;
81    private InstallContext installContext;
82    private Session templateSession;
83    private Session resourceSession;
84    private LayeredResourceOrigin origin;
85   
86    private MockContext context;
87    private RepositoryManager repositoryManager;
88   
 
89  13 toggle @Before
90    public void setUp() throws Exception {
91  13 context = new MockContext();
92   
93  13 templateSession = new MockSession(TemplateMigrationTask.TEMPLATES_WORKSPACE);
94  13 resourceSession = new MockSession(JcrResourceOrigin.RESOURCES_WORKSPACE);
95  13 context.addSession(TemplateMigrationTask.TEMPLATES_WORKSPACE, templateSession);
96  13 context.addSession(JcrResourceOrigin.RESOURCES_WORKSPACE, resourceSession);
97   
98  13 installContext = mock(InstallContext.class);
99  13 when(installContext.getJCRSession(TemplateMigrationTask.TEMPLATES_WORKSPACE)).thenReturn(templateSession);
100  13 when(installContext.getJCRSession(JcrResourceOrigin.RESOURCES_WORKSPACE)).thenReturn(resourceSession);
101   
102    // Origins
103  13 JcrResourceOrigin jcrResourceOrigin = new JcrResourceOriginFactory(Providers.<SystemContext>of(context)).create("jcr");
104  13 origin = new LayeredResourceOriginFactory().create("layeredOrigin", new ResourceOrigin[]{jcrResourceOrigin});
105   
106  13 ComponentsTestUtil.setInstance(ResourceOrigin.class, origin);
107   
108  13 repositoryManager = mock(RepositoryManager.class);
109  13 when(repositoryManager.hasWorkspace(eq(TemplateMigrationTask.TEMPLATES_WORKSPACE))).thenReturn(true);
110  13 ComponentsTestUtil.setInstance(RepositoryManager.class, repositoryManager);
111   
112  13 task = new TemplateMigrationTask();
113    }
114   
 
115  1 toggle @Test
116    public void templateThatIsNotEnabledWillNotBeHandled() throws Exception {
117  1 origin = mock(LayeredResourceOrigin.class);
118  1 ComponentsTestUtil.setInstance(ResourceOrigin.class, origin);
119   
120    // GIVEN
121  1 final Node testTemplateNode = createLegacyTemplateNode(templateSession.getRootNode(), "test-template", true, false, "some_text");
122  1 final String testTemplateNodePath = testTemplateNode.getPath();
123   
124  1 NodeUtil.createPath(resourceSession.getRootNode(), "test-template.ftl", NodeTypes.Content.NAME);
125   
126    // WHEN
127  1 task.doExecute(installContext);
128   
129    // THEN
130  1 assertTrue("We want to make sure that nothing gets deleted", templateSession.itemExists(testTemplateNodePath));
131  1 assertThat("We want to make sure that the task does NOT copy/migrate the template node",
132    resourceSession.getNode("/test-template.ftl"), not(hasProperty(PROPERTY_AUTO_IMPORT)));
133   
134    // Also verify no interaction with origin
135  1 verify(origin, never()).hasPath(anyString());
136  1 verify(origin, never()).getByPath(anyString());
137    }
138   
 
139  1 toggle @Test
140    public void templateWillBeDeletedWhenAutoImportTrueAndResourceExists() throws Exception {
141    // GIVEN
142  1 final Node testTemplateNode = createLegacyTemplateNode(templateSession.getRootNode(), "test-template", true, true, "some_text");
143  1 final String testTemplateNodePath = testTemplateNode.getPath();
144   
145  1 NodeUtil.createPath(resourceSession.getRootNode(), "test-template.ftl", NodeTypes.Content.NAME);
146   
147    // WHEN
148  1 task.doExecute(installContext);
149   
150    // THEN
151  1 assertTrue("We want to make sure that nothing gets deleted", templateSession.itemExists(testTemplateNodePath));
152  1 assertThat("We want to make sure that the task does NOT copy/migrate the template node",
153    resourceSession.getNode("/test-template.ftl"), not(hasProperty(PROPERTY_AUTO_IMPORT)));
154    }
155   
 
156  1 toggle @Test
157    public void templateWillBeDeletedWhenAutoImportFalseAndResourceExistsWithSameContent() throws Exception {
158    // GIVEN
159  1 final Node testTemplateNode = createLegacyTemplateNode(templateSession.getRootNode(), "test-template", false, true, "some_text");
160  1 final String testTemplateNodePath = testTemplateNode.getPath();
161   
162  1 final Node resourcesTestTemplateNode = NodeUtil.createPath(resourceSession.getRootNode(), "test-template.ftl", NodeTypes.Content.NAME);
163  1 resourcesTestTemplateNode.setProperty(PROPERTY_TEXT, "some_text");
164   
165    // WHEN
166  1 task.doExecute(installContext);
167   
168    // THEN
169  1 assertTrue("We want to make sure that nothing gets deleted", templateSession.itemExists(testTemplateNodePath));
170  1 assertThat("We want to make sure that the task does NOT copy/migrate the template node",
171    resourceSession.getNode("/test-template.ftl"), not(hasProperty(PROPERTY_AUTO_IMPORT)));
172    }
173   
 
174  1 toggle @Test
175    public void taskWillFailWhenAutoImportFalseAndResourceExistsWithDifferentContent() throws Exception {
176    // GIVEN
177  1 createLegacyTemplateNode(templateSession.getRootNode(), "test-template", false, true, "some_text");
178   
179  1 final Node resourcesTestTemplateNode = NodeUtil.createPath(resourceSession.getRootNode(), "test-template.ftl", NodeTypes.Content.NAME);
180  1 resourcesTestTemplateNode.setProperty(PROPERTY_TEXT, "other_text");
181   
182    // WHEN
183    // THEN
184  1 assertThat(new Execution() {
 
185  1 toggle @Override
186    public void evaluate() throws Exception {
187  1 task.doExecute(installContext);
188    }
189    }, throwsAnException(strictlyInstanceOf(TaskExecutionException.class).withMessage(containsString("Cannot proceed with template migration"))));
190    }
191   
 
192  1 toggle @Test
193    public void taskWillNotFailWhenRepositoryExceptionIsEncounteredWhileGettingSession() throws Exception {
194    // GIVEN
195  1 when(installContext.getJCRSession(TemplateMigrationTask.TEMPLATES_WORKSPACE)).thenThrow(new RepositoryException("This exception is thrown on purpose"));
196   
197    // WHEN
198    // THEN
199  1 assertThat(new Execution() {
 
200  1 toggle @Override
201    public void evaluate() throws Exception {
202  1 task.doExecute(installContext);
203    }
204    }, throwsNothing());
205   
206  1 verify(installContext).info("Could not get session for workspace 'templates'. Not running template migration task.");
207    }
208   
 
209  1 toggle @Test
210    public void taskWillNotFailWhenNoTemplatesWorkspaceEncountered() throws Exception {
211    // GIVEN
212  1 when(repositoryManager.hasWorkspace(TemplateMigrationTask.TEMPLATES_WORKSPACE)).thenReturn(false);
213   
214    // WHEN
215    // THEN
216  1 assertThat(new Execution() {
 
217  1 toggle @Override
218    public void evaluate() throws Exception {
219  1 task.doExecute(installContext);
220    }
221    }, throwsNothing());
222   
223  1 verify(installContext).info("Inplace-templating might not be installed. Not running template migration task.");
224    }
225   
 
226  1 toggle @Test
227    public void templateIsMigratedWhenAutoImportTrueAndNoResourceExist() throws Exception {
228    // GIVEN
229  1 final Node testTemplateNode = createLegacyTemplateNode(templateSession.getRootNode(), "test-template", true, true, "some_text");
230  1 final String testTemplateNodePath = testTemplateNode.getPath();
231  1 final String newTemplateNodePath = testTemplateNodePath + ".ftl";
232   
233    // WHEN
234  1 task.doExecute(installContext);
235   
236    // THEN
237  1 assertTrue(resourceSession.itemExists(newTemplateNodePath));
238  1 assertTemplateResource(resourceSession.getNode(newTemplateNodePath), true, "some_text");
239   
240  1 assertTrue("We want to make sure that nothing gets deleted", templateSession.itemExists(testTemplateNodePath));
241    }
242   
 
243  1 toggle @Test
244    public void templateIsMigratedWhenAutoImportFalseAndNoResourceExist() throws Exception {
245    // GIVEN
246  1 final Node testTemplateNode = createLegacyTemplateNode(templateSession.getRootNode(), "test-template", false, true, "some_other_text");
247  1 final String testTemplateNodePath = testTemplateNode.getPath();
248  1 final String newTemplateNodePath = testTemplateNodePath + ".ftl";
249   
250    // WHEN
251  1 task.doExecute(installContext);
252   
253    // THEN
254  1 assertTrue(resourceSession.itemExists(newTemplateNodePath));
255  1 assertTemplateResource(resourceSession.getNode(newTemplateNodePath), false, "some_other_text");
256   
257  1 assertTrue("We want to make sure that nothing gets deleted", templateSession.itemExists(testTemplateNodePath));
258    }
259   
 
260  1 toggle @Test
261    public void templateIsMigratedWhenAutoImportFalseAndResourceInOtherOriginExist() throws Exception {
262  1 MgnlContext.setInstance(context);
263   
264    // GIVEN
265  1 final Node testTemplateNode = createLegacyTemplateNode(templateSession.getRootNode(), "test-template", false, true, "some_text");
266  1 final String testTemplateNodePath = testTemplateNode.getPath();
267  1 final String newTemplateNodePath = testTemplateNodePath + ".ftl";
268   
269    // Adding the resource to another origin (but the jcr one) should trigger migration
270  1 final DummyResourceOrigin dummyOrigin = new DummyResourceOrigin(qux, null, null, new String[]{newTemplateNodePath});
271  1 final JcrResourceOrigin jcrResourceOrigin = new JcrResourceOriginFactory(Providers.of((SystemContext) MgnlContext.getInstance())).create("jcr");
272  1 origin = new LayeredResourceOriginFactory().create("layeredOrigin", new ResourceOrigin[]{dummyOrigin, jcrResourceOrigin});
273   
274  1 ComponentsTestUtil.setInstance(ResourceOrigin.class, origin);
275   
276    // WHEN
277  1 task.doExecute(installContext);
278   
279    // THEN
280  1 assertTrue(resourceSession.itemExists(newTemplateNodePath));
281  1 assertTemplateResource(resourceSession.getNode(newTemplateNodePath), false, "some_text");
282   
283  1 assertTrue("We want to make sure that nothing gets deleted", templateSession.itemExists(testTemplateNodePath));
284    }
285   
 
286  1 toggle @Test
287    public void templateIsMigratedWhenAutoImportTrueAndNoResourceExistAndTemplateIsInFolderThatDoesNotExistInDest() throws Exception {
288    // GIVEN
289  1 final Node folderNode = NodeUtil.createPath(templateSession.getRootNode(), "folder", NodeTypes.Folder.NAME);
290  1 final Node testTemplateNode = createLegacyTemplateNode(folderNode, "test-template", true, true, "some_text");
291  1 final String testTemplateNodePath = testTemplateNode.getPath();
292  1 final String newTemplateNodePath = testTemplateNodePath + ".ftl";
293   
294    // WHEN
295  1 task.doExecute(installContext);
296   
297    // THEN
298  1 assertTrue(resourceSession.itemExists(newTemplateNodePath));
299  1 assertTemplateResource(resourceSession.getNode(newTemplateNodePath), true, "some_text");
300   
301  1 assertTrue(templateSession.itemExists("/folder"));
302  1 assertTrue(templateSession.itemExists(testTemplateNodePath));
303    }
304   
 
305  1 toggle @Test
306    public void templateIsMigratedAndActivationStatusIsNotCopied() throws Exception {
307    // GIVEN
308  1 final Calendar calendar = Calendar.getInstance();
309  1 final Node testTemplateNode = createLegacyTemplateNode(templateSession.getRootNode(), "test-template", null, true, null);
310  1 testTemplateNode.setProperty(NodeTypes.Activatable.ACTIVATION_STATUS, true);
311  1 testTemplateNode.setProperty(NodeTypes.Activatable.LAST_ACTIVATED, calendar);
312  1 testTemplateNode.setProperty(NodeTypes.LastModified.LAST_MODIFIED, calendar);
313  1 testTemplateNode.setProperty(NodeTypes.LastModified.LAST_MODIFIED_BY, "yet-other-guy");
314  1 testTemplateNode.setProperty(NodeTypes.Created.CREATED, calendar);
315  1 testTemplateNode.setProperty(NodeTypes.Created.CREATED_BY, "this-other-guy");
316   
317  1 final String newTemplateNodePath = testTemplateNode.getPath() + ".ftl";
318   
319  1 assertThat(NodeTypes.Activatable.getActivationStatus(testTemplateNode), is(NodeTypes.Activatable.ACTIVATION_STATUS_ACTIVATED));
320   
321  1 Thread.sleep(1); // Sleep for one millisecond
322   
323    // WHEN
324  1 task.doExecute(installContext);
325   
326    // THEN
327  1 assertTrue(resourceSession.itemExists(newTemplateNodePath));
328  1 assertThat(resourceSession.getNode(newTemplateNodePath), not(hasProperty(NodeTypes.Activatable.ACTIVATION_STATUS)));
329  1 assertThat(resourceSession.getNode(newTemplateNodePath), not(hasProperty(NodeTypes.Activatable.LAST_ACTIVATED)));
330  1 assertThat(resourceSession.getNode(newTemplateNodePath), not(hasProperty(NodeTypes.Activatable.LAST_ACTIVATED_BY)));
331   
332  1 assertThat(resourceSession.getNode(newTemplateNodePath), hasProperty(NodeTypes.Created.CREATED, calendar.getTime()));
333  1 assertThat(resourceSession.getNode(newTemplateNodePath), hasProperty(NodeTypes.Created.CREATED_BY, "this-other-guy"));
334   
335    // A mocked session will actually not add the last modified (by) property
336    // So we simply make sure the existing ones are not migrated
337  1 assertThat(resourceSession.getNode(newTemplateNodePath), not(hasProperty(NodeTypes.LastModified.LAST_MODIFIED)));
338  1 assertThat(resourceSession.getNode(newTemplateNodePath), not(hasProperty(NodeTypes.LastModified.LAST_MODIFIED_BY)));
339    }
340   
 
341  1 toggle @Test
342    public void makeSureAdditionalTaskIsExecutedWhenMigrationPasses() throws Exception {
343    // GIVEN
344  1 final Task onSuccessTask = mock(Task.class);
345  1 task = new TemplateMigrationTask(onSuccessTask);
346   
347    // WHEN
348  1 task.execute(installContext);
349   
350    // THEN
351  1 verify(onSuccessTask, times(1)).execute(installContext);
352    }
353   
 
354  1 toggle @Test
355    public void makeSureAdditionalTaskIsNotExecutedWhenMigrationIsNotPassing() throws Exception {
356    // GIVEN
357    // We create a template with contents that are different to the one in resources workspace
358  1 createLegacyTemplateNode(templateSession.getRootNode(), "test-template", false, true, "some_text");
359   
360  1 final Node resourcesTestTemplateNode = NodeUtil.createPath(resourceSession.getRootNode(), "test-template.ftl", NodeTypes.Content.NAME);
361  1 resourcesTestTemplateNode.setProperty(PROPERTY_TEXT, "other_text");
362   
363  1 final Task onSuccessTask = mock(Task.class);
364  1 task = new TemplateMigrationTask(onSuccessTask);
365   
366    // WHEN
367  1 try {
368  1 task.execute(installContext);
369    } catch (TaskExecutionException e) {
370    // We ignore the error, we just want to make sure the additional on-success-task is not executed
371    }
372   
373    // THEN
374  1 verify(onSuccessTask, never()).execute(installContext);
375    }
376   
377    /**
378    * Creates a legacy template with given values for their respective properties.
379    */
 
380  10 toggle private Node createLegacyTemplateNode(final Node parent, final String relativePath, final Boolean autoImport, final Boolean enabled, final String text) throws RepositoryException {
381  10 final Node templateNode = NodeUtil.createPath(parent, relativePath, NodeTypes.Content.NAME);
382  10 if (autoImport != null) {
383  9 templateNode.setProperty(PROPERTY_AUTO_IMPORT, autoImport);
384    }
385  10 if (enabled != null) {
386  10 templateNode.setProperty(PROPERTY_ENABLED, enabled);
387    }
388  10 if (text != null) {
389  9 templateNode.setProperty(PROPERTY_TEXT, text);
390    }
391  10 return templateNode;
392    }
393   
394    /**
395    * Assert that new resource in jcr has expected properties.
396    */
 
397  4 toggle private void assertTemplateResource(final Node newTemplateNode, final Boolean autoImport, final String text) throws RepositoryException {
398  4 if (text != null) {
399  4 assertThat(newTemplateNode, hasProperty(PROPERTY_TEXT));
400  4 assertThat(newTemplateNode.getProperty(PROPERTY_TEXT).getString(), is(text));
401    }
402  4 if (autoImport != null) {
403  4 assertThat(newTemplateNode, hasProperty(PROPERTY_AUTO_IMPORT));
404  4 assertThat(newTemplateNode.getProperty(PROPERTY_AUTO_IMPORT).getBoolean(), is(autoImport));
405    }
406    // The enabled property should never be migrated
407  4 assertThat(newTemplateNode, not(hasProperty(PROPERTY_ENABLED)));
408    }
409   
410    }