Clover icon

Magnolia Resources App Module 2.4.7

  1. Project Clover database Fri Sep 9 2016 16:27:56 CEST
  2. Package info.magnolia.resources.app.action

File RestoreResourceVersionActionTest.java

 

Code metrics

0
65
7
2
219
133
7
0.11
9.29
3.5
1

Classes

Class Line # Actions
RestoreResourceVersionActionTest 71 59 0% 5 0
1.0100%
RestoreResourceVersionActionTest.MockRestoreResourceVersionAction 202 6 0% 2 0
1.0100%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /**
2    * This file Copyright (c) 2015-2016 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.resources.app.action;
35   
36    import static org.junit.Assert.*;
37    import static org.mockito.BDDMockito.given;
38    import static org.mockito.Mockito.mock;
39   
40    import info.magnolia.cms.beans.config.VersionConfig;
41    import info.magnolia.cms.core.version.VersionManager;
42    import info.magnolia.context.Context;
43    import info.magnolia.context.MgnlContext;
44    import info.magnolia.event.EventBus;
45    import info.magnolia.i18nsystem.SimpleTranslator;
46    import info.magnolia.jcr.util.NodeTypes;
47    import info.magnolia.objectfactory.Components;
48    import info.magnolia.resourceloader.jcr.JcrResourceOrigin;
49    import info.magnolia.resources.app.workbench.ResourcesContainer;
50    import info.magnolia.test.RepositoryTestCase;
51    import info.magnolia.ui.api.app.AppContext;
52    import info.magnolia.ui.api.app.SubAppContext;
53    import info.magnolia.ui.api.context.UiContext;
54    import info.magnolia.ui.api.location.LocationController;
55    import info.magnolia.ui.dialog.formdialog.FormDialogPresenter;
56    import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
57   
58    import javax.jcr.Node;
59    import javax.jcr.Session;
60    import javax.jcr.version.Version;
61   
62    import org.junit.Before;
63    import org.junit.Test;
64   
65    import com.vaadin.data.Item;
66    import com.vaadin.data.Property;
67    import com.vaadin.data.util.AbstractProperty;
68    import com.vaadin.data.util.BeanItem;
69    import com.vaadin.ui.TextField;
70   
 
71    public class RestoreResourceVersionActionTest extends RepositoryTestCase {
72   
73    private static final String PROPERTY_NAME_VERSION_NAME = "versionName";
74    private static final String CREATED_VERSION_BEFORE_RESTORE = "ui-contentapp.actions.restoreVersion.comment.restore";
75   
76    private RestoreResourceVersionActionDefinition definition;
77    private FormDialogPresenter formDialogPresenter;
78    private LocationController locationController;
79    private VersionManager versionManager;
80    private VersionConfig versionConfig;
81    private AppContext appContext;
82    private SimpleTranslator i18n;
83    private UiContext uiContext;
84    private EventBus eventBus;
85    private Context context;
86    private Node node;
87   
 
88  3 toggle @Before
89    public void setUp() throws Exception {
90  3 super.setUp();
91  3 versionManager = Components.getComponent(VersionManager.class);
92  3 context = MgnlContext.getInstance();
93   
94  3 versionConfig = new VersionConfig();
95  3 definition = new RestoreResourceVersionActionDefinition();
96   
97  3 formDialogPresenter = mock(FormDialogPresenter.class);
98  3 uiContext = mock(SubAppContext.class);
99  3 eventBus = mock(EventBus.class);
100  3 i18n = mock(SimpleTranslator.class);
101  3 locationController = mock(LocationController.class);
102  3 appContext = mock(AppContext.class);
103  3 Item resourceItem = mock(Item.class);
104  3 Property property = mock(AbstractProperty.class);
105   
106  3 given(resourceItem.getItemProperty(ResourcesContainer.RESOURCE_PATH)).willReturn(property);
107  3 given(property.getValue()).willReturn("/test");
108   
109  3 Session session = MgnlContext.getJCRSession(JcrResourceOrigin.RESOURCES_WORKSPACE);
110  3 node = session.getRootNode().addNode("test", NodeTypes.Page.NAME);
111  3 NodeTypes.Created.set(node);
112  3 node.setProperty("mgnl:template", "home");
113  3 node.addNode("areaSubNode", NodeTypes.Area.NAME);
114  3 node.getNode("areaSubNode").setProperty("content", "version");
115  3 node.setProperty(ResourcesContainer.RESOURCE_PATH, "/test");
116  3 session.save();
117    }
118   
 
119  3 toggle @Override
120    protected String getRepositoryConfigFileName() {
121  3 return "test-resource-repositories.xml";
122    }
123   
 
124  1 toggle @Test
125    public void restoreVersion() throws Exception {
126    // GIVEN
127  1 versionManager.addVersion(node);
128   
129    // change test node before restore
130  1 node.setProperty("mgnl:template", "section");
131  1 node.getNode("areaSubNode").remove();
132  1 node.getSession().save();
133   
134  1 assertEquals("section", node.getProperty("mgnl:template").getString());
135  1 assertFalse(node.hasNode("areaSubNode"));
136   
137    // WHEN
138  1 MockRestoreResourceVersionAction action = new MockRestoreResourceVersionAction(definition, appContext, locationController, uiContext, formDialogPresenter, new JcrNodeAdapter(node), i18n, versionManager, eventBus, versionConfig, context);
139  1 action.execute();
140  1 action.getEditorCallback().onSuccess("");
141   
142    // THEN
143  1 assertEquals("home", node.getProperty("mgnl:template").getString());
144  1 assertTrue(node.hasNode("areaSubNode"));
145  1 assertEquals("version", node.getNode("areaSubNode").getProperty("content").getString());
146    }
147   
148   
 
149  1 toggle @Test
150    public void checkVersionCreatedBeforeRestore() throws Exception {
151    // GIVEN
152  1 versionManager.addVersion(node);
153   
154    // change test node before restore
155  1 node.setProperty("mgnl:template", "section");
156  1 node.getNode("areaSubNode").remove();
157  1 node.getSession().save();
158   
159  1 assertEquals(2, versionManager.getAllVersions(node).getSize());
160   
161  1 MockRestoreResourceVersionAction action = new MockRestoreResourceVersionAction(definition, appContext, locationController, uiContext, formDialogPresenter, new JcrNodeAdapter(node), i18n, versionManager, eventBus, versionConfig, context);
162  1 action.execute();
163   
164    // WHEN
165  1 action.getEditorCallback().onSuccess("");
166   
167    // THEN
168  1 assertEquals(3, versionManager.getAllVersions(node).getSize());
169  1 Version version = versionManager.getVersion(node, "1.1");
170  1 assertEquals(CREATED_VERSION_BEFORE_RESTORE, NodeTypes.Versionable.getComment(version));
171  1 assertEquals("section", version.getProperty("mgnl:template").getString());
172  1 assertFalse(version.hasNode("areaSubNode"));
173    }
174   
 
175  1 toggle @Test
176    public void doNotCreateVersionBeforeRestoreIfNotAllowed() throws Exception {
177    // GIVEN
178  1 versionManager.addVersion(node);
179   
180    // change test node before restore
181  1 node.setProperty("mgnl:template", "section");
182  1 node.getNode("areaSubNode").remove();
183  1 node.getSession().save();
184   
185  1 assertEquals(2, versionManager.getAllVersions(node).getSize());
186   
187  1 definition.setCreateVersionBeforeRestore(false);
188   
189  1 MockRestoreResourceVersionAction action = new MockRestoreResourceVersionAction(definition, appContext, locationController, uiContext, formDialogPresenter, new JcrNodeAdapter(node), i18n, versionManager, eventBus, versionConfig, context);
190  1 action.execute();
191   
192    // WHEN
193  1 action.getEditorCallback().onSuccess("");
194   
195    // THEN
196  1 assertEquals(2, versionManager.getAllVersions(node).getSize());
197    }
198   
199    /**
200    * Test class of {@link RestoreResourceVersionAction}.
201    */
 
202    private class MockRestoreResourceVersionAction extends RestoreResourceVersionAction {
203   
204    private String version = "1.0";
205   
 
206  3 toggle private MockRestoreResourceVersionAction(RestoreResourceVersionActionDefinition definition, AppContext appContext, LocationController locationController, UiContext uiContext, FormDialogPresenter formDialogPresenter, JcrNodeAdapter item, SimpleTranslator i18n, VersionManager versionManager, EventBus eventBus, VersionConfig versionConfig, Context context) {
207  3 super(definition, appContext, locationController, uiContext, formDialogPresenter, item, i18n, versionManager, eventBus, versionConfig, context);
208    }
209   
 
210  3 toggle @Override
211    protected BeanItem<?> getItem() {
212  3 BeanItem<?> item = super.getItem();
213  3 Property property = new TextField();
214  3 property.setValue(version);
215  3 item.addItemProperty(PROPERTY_NAME_VERSION_NAME, property);
216  3 return item;
217    }
218    }
219    }