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

 

Code metrics

0
52
7
1
197
120
7
0.13
7.43
7
1

Classes

Class Line # Actions
RestoreResourcePreviousVersionActionTest 75 52 0% 7 0
1.0100%
 

Contributing tests

This file is covered by 4 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 info.magnolia.test.hamcrest.ExceptionMatcher.strictlyInstanceOf;
37    import static info.magnolia.test.hamcrest.ExecutionMatcher.throwsAnException;
38    import static org.hamcrest.CoreMatchers.is;
39    import static org.hamcrest.MatcherAssert.assertThat;
40    import static org.mockito.BDDMockito.given;
41    import static org.mockito.Mockito.*;
42   
43    import info.magnolia.cms.core.version.VersionManager;
44    import info.magnolia.commands.CommandsManager;
45    import info.magnolia.commands.impl.RestorePreviousVersionCommand;
46    import info.magnolia.context.Context;
47    import info.magnolia.context.MgnlContext;
48    import info.magnolia.event.EventBus;
49    import info.magnolia.i18nsystem.SimpleTranslator;
50    import info.magnolia.jcr.util.NodeTypes;
51    import info.magnolia.objectfactory.Components;
52    import info.magnolia.resourceloader.jcr.JcrResourceOrigin;
53    import info.magnolia.resources.app.workbench.ResourcesContainer;
54    import info.magnolia.test.ComponentsTestUtil;
55    import info.magnolia.test.RepositoryTestCase;
56    import info.magnolia.test.hamcrest.Execution;
57    import info.magnolia.ui.api.context.UiContext;
58    import info.magnolia.ui.api.event.ContentChangedEvent;
59    import info.magnolia.ui.framework.action.async.AsyncActionExecutor;
60    import info.magnolia.ui.vaadin.integration.jcr.AbstractJcrNodeAdapter;
61    import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
62   
63    import javax.jcr.Node;
64    import javax.jcr.RepositoryException;
65    import javax.jcr.Session;
66   
67    import org.junit.Before;
68    import org.junit.Test;
69    import org.mockito.ArgumentCaptor;
70   
71    import com.vaadin.data.Item;
72    import com.vaadin.data.Property;
73    import com.vaadin.data.util.AbstractProperty;
74   
 
75    public class RestoreResourcePreviousVersionActionTest extends RepositoryTestCase {
76   
77    String PROPERTY_NAME_VERSION_NAME = "versionName";
78    String CREATED_VERSION_BEFORE_RESTORE = "ui-contentapp.actions.restoreVersion.comment.restore";
79   
80    private Node node;
81    private SimpleTranslator i18n;
82    private UiContext uiContext;
83    private EventBus eventBus;
84    private RestoreResourcePreviousVersionActionDefinition definition;
85    private Context context;
86    private CommandsManager commandsManager;
87   
 
88  4 toggle @Override
89    @Before
90    public void setUp() throws Exception {
91  4 super.setUp();
92  4 ComponentsTestUtil.setImplementation(AsyncActionExecutor.class, mock(AsyncActionExecutor.class).getClass());
93   
94  4 context = MgnlContext.getInstance();
95  4 definition = new RestoreResourcePreviousVersionActionDefinition();
96  4 definition.setAsynchronous(false);
97   
98  4 uiContext = mock(UiContext.class);
99  4 eventBus = mock(EventBus.class);
100  4 i18n = mock(SimpleTranslator.class);
101  4 commandsManager = mock(CommandsManager.class);
102  4 Item resourceItem = mock(Item.class);
103  4 Property property = mock(AbstractProperty.class);
104   
105  4 when(commandsManager.getCommand(anyString(), anyString())).thenReturn(new RestorePreviousVersionCommand(Components.getComponent(VersionManager.class)));
106   
107  4 given(resourceItem.getItemProperty(ResourcesContainer.RESOURCE_PATH)).willReturn(property);
108  4 given(property.getValue()).willReturn("/test");
109   
110  4 Session session = MgnlContext.getJCRSession(JcrResourceOrigin.RESOURCES_WORKSPACE);
111  4 node = session.getRootNode().addNode("test", NodeTypes.Page.NAME);
112  4 NodeTypes.Created.set(node);
113  4 node.setProperty("mgnl:template", "home");
114  4 node.addNode("areaSubNode", NodeTypes.Area.NAME);
115  4 node.getNode("areaSubNode").setProperty("content", "version");
116  4 node.setProperty(ResourcesContainer.RESOURCE_PATH, "/test");
117  4 session.save();
118    }
119   
 
120  4 toggle @Override
121    protected String getRepositoryConfigFileName() {
122  4 return "test-resource-repositories.xml";
123    }
124   
 
125  1 toggle @Test
126    public void execute() throws Exception {
127    // GIVEN
128  1 VersionManager versionManager = VersionManager.getInstance();
129  1 versionManager.addVersion(node);
130   
131  1 AbstractJcrNodeAdapter item = new JcrNodeAdapter(node);
132  1 RestoreResourcePreviousVersionAction action = new RestoreResourcePreviousVersionAction(definition, item, commandsManager, eventBus, uiContext, i18n, context);
133   
134    // WHEN
135  1 action.execute();
136   
137    // THEN
138  1 assertThat(versionManager.getBaseVersion(node).getName(), is("1.0"));
139    }
140   
 
141  1 toggle @Test
142    public void executeNoVersion() throws Exception {
143    // GIVEN
144  1 final VersionManager versionManager = VersionManager.getInstance();
145   
146  1 AbstractJcrNodeAdapter item = new JcrNodeAdapter(node);
147  1 RestoreResourcePreviousVersionAction action = new RestoreResourcePreviousVersionAction(definition, item, commandsManager, eventBus, uiContext, i18n, context);
148   
149    // WHEN
150  1 action.execute();
151   
152    // THEN
153  1 assertThat(new Execution() {
 
154  1 toggle @Override
155    public void evaluate() throws Exception {
156  1 versionManager.getBaseVersion(node);
157    }
158    }, throwsAnException(strictlyInstanceOf(RepositoryException.class).withMessage("Node /test was never versioned")));
159    }
160   
 
161  1 toggle @Test
162    public void executeThreeVersion() throws Exception {
163    // GIVEN
164  1 VersionManager versionManager = VersionManager.getInstance();
165  1 versionManager.addVersion(node);
166  1 versionManager.addVersion(node);
167  1 versionManager.addVersion(node);
168   
169  1 AbstractJcrNodeAdapter item = new JcrNodeAdapter(node);
170  1 RestoreResourcePreviousVersionAction action = new RestoreResourcePreviousVersionAction(definition, item, commandsManager, eventBus, uiContext, i18n, context);
171   
172    // WHEN
173  1 action.execute();
174   
175    // THEN
176  1 assertThat(versionManager.getBaseVersion(node).getName(), is("1.2"));
177    }
178   
 
179  1 toggle @Test
180    public void eventWithCorrectPathIsFiredAfterRestoreOfPreviousVersion() throws Exception {
181    // GIVEN
182  1 VersionManager versionManager = VersionManager.getInstance();
183  1 versionManager.addVersion(node);
184   
185  1 AbstractJcrNodeAdapter item = new JcrNodeAdapter(node);
186  1 RestoreResourcePreviousVersionAction action = new RestoreResourcePreviousVersionAction(definition, item, commandsManager, eventBus, uiContext, i18n, context);
187   
188    // WHEN
189  1 action.execute();
190   
191    // THEN
192  1 ArgumentCaptor<ContentChangedEvent> argumentCaptor = ArgumentCaptor.forClass(ContentChangedEvent.class);
193  1 verify(eventBus, atLeastOnce()).fireEvent(argumentCaptor.capture());
194  1 String result = (String) argumentCaptor.getValue().getItemId();
195  1 assertThat(result, is("/test"));
196    }
197    }