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

 

Code metrics

0
77
7
1
237
143
7
0.09
11
7
1

Classes

Class Line # Actions
ResourceActivationActionTest 76 77 0% 7 0
1.0100%
 

Contributing tests

This file is covered by 6 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.hamcrest.CoreMatchers.is;
37    import static org.hamcrest.MatcherAssert.assertThat;
38    import static org.hamcrest.Matchers.hasSize;
39    import static org.mockito.BDDMockito.given;
40    import static org.mockito.Matchers.*;
41    import static org.mockito.Mockito.*;
42   
43    import info.magnolia.cms.security.User;
44    import info.magnolia.commands.CommandsManager;
45    import info.magnolia.commands.chain.Command;
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.resourceloader.jcr.JcrResourceOrigin;
52    import info.magnolia.resources.app.workbench.ResourcesContainer;
53    import info.magnolia.test.ComponentsTestUtil;
54    import info.magnolia.test.MgnlTestCase;
55    import info.magnolia.test.mock.MockContext;
56    import info.magnolia.test.mock.jcr.SessionTestUtil;
57    import info.magnolia.ui.api.event.ContentChangedEvent;
58    import info.magnolia.ui.framework.action.AbstractCommandActionTest;
59    import info.magnolia.ui.framework.action.async.AsyncActionExecutor;
60    import info.magnolia.ui.framework.app.SubAppContextImpl;
61    import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
62   
63    import java.util.List;
64   
65    import javax.jcr.Node;
66    import javax.jcr.Session;
67   
68    import org.junit.Before;
69    import org.junit.Test;
70    import org.mockito.ArgumentCaptor;
71   
72    import com.vaadin.data.Item;
73    import com.vaadin.data.Property;
74    import com.vaadin.data.util.AbstractProperty;
75   
 
76    public class ResourceActivationActionTest extends MgnlTestCase {
77   
78    private ResourceActivationActionDefinition definition;
79    private Context context;
80    private ResourceActivationAction action;
81    private Session session;
82    private EventBus eventBus;
83   
 
84  6 toggle @Override
85    @Before
86    public void setUp() throws Exception {
87  6 super.setUp();
88  6 String website = "/foo.uuid=1\n" +
89    "/foo/bar.uuid=2\n" +
90    "/foo/bar/test.uuid=3";
91  6 session = SessionTestUtil.createSession(JcrResourceOrigin.RESOURCES_WORKSPACE, website);
92  6 ComponentsTestUtil.setImplementation(AsyncActionExecutor.class, AbstractCommandActionTest.DummyAsyncExecutor.class);
93   
94  6 CommandsManager commandsManager = mock(CommandsManager.class);
95  6 Command activationCommand = mock(Command.class);
96  6 eventBus = mock(EventBus.class);
97  6 SubAppContextImpl uiContext = mock(SubAppContextImpl.class);
98  6 SimpleTranslator i18n = mock(SimpleTranslator.class);
99  6 given(commandsManager.getCommand(eq(CommandsManager.DEFAULT_CATALOG), anyString())).willReturn(activationCommand);
100  6 given(commandsManager.getCommand(eq("workflow"), anyString())).willReturn(activationCommand);
101  6 given(commandsManager.getCommand(anyString())).willReturn(activationCommand);
102   
103  6 MockContext mockContext = (MockContext) MgnlContext.getInstance();
104  6 mockContext.addSession(JcrResourceOrigin.RESOURCES_WORKSPACE, session);
105   
106  6 User user = mock(User.class);
107  6 given(user.getName()).willReturn("ilgun");
108  6 mockContext.setUser(user);
109   
110  6 Item resourceItem = mock(Item.class);
111  6 Property property = mock(AbstractProperty.class);
112   
113  6 given(resourceItem.getItemProperty(ResourcesContainer.RESOURCE_PATH)).willReturn(property);
114  6 given(property.getValue()).willReturn("/foo/bar/test");
115   
116  6 context = mockContext;
117  6 definition = new ResourceActivationActionDefinition();
118  6 action = new ResourceActivationAction(definition, resourceItem, commandsManager, eventBus, uiContext, i18n, context);
119    }
120   
 
121  1 toggle @Test
122    public void unPublishSucceeds() throws Exception {
123    // GIVEN
124  1 definition.setCommand("deactivate");
125   
126    // WHEN
127  1 action.execute();
128   
129    // THEN
130  1 assertThat(!(Boolean) context.getAttribute("command_result"), is(true));
131    }
132   
 
133  1 toggle @Test
134    public void publishActionSucceeds() throws Exception {
135    // GIVEN
136  1 definition.setCommand("activate");
137   
138    // WHEN
139  1 action.execute();
140   
141    // THEN
142  1 assertThat(!(Boolean) context.getAttribute("command_result"), is(true));
143    }
144   
 
145  1 toggle @Test
146    public void buildParamsSetsRecursiveParameter() throws Exception {
147    // GIVEN
148  1 definition.setRecursive(true);
149   
150    // WHEN
151  1 action.execute();
152   
153    // THEN
154  1 assertThat((Boolean) action.getParams().get("recursive"), is(true));
155    }
156   
 
157  1 toggle @Test
158    public void publishMarkedAsDeletedResourceSucceeds() throws Exception {
159    // GIVEN
160  1 definition.setCommand("activate");
161  1 definition.setDeletionActivation(true);
162  1 Node foo = session.getRootNode().getNode("foo");
163  1 Node bar = foo.getNode("bar");
164  1 Node test = bar.getNode("test");
165   
166  1 foo.setProperty(NodeTypes.Activatable.ACTIVATION_STATUS, true);
167  1 foo.addMixin(NodeTypes.Deleted.NAME);
168   
169  1 bar.setProperty(NodeTypes.Activatable.ACTIVATION_STATUS, true);
170  1 bar.addMixin(NodeTypes.Deleted.NAME);
171   
172  1 test.setProperty(NodeTypes.Activatable.ACTIVATION_STATUS, true);
173  1 test.addMixin(NodeTypes.Deleted.NAME);
174   
175    // WHEN
176  1 action.execute();
177  1 List<JcrItemAdapter> items = action.getItems();
178   
179    // THEN
180  1 assertThat(!(Boolean) context.getAttribute("command_result"), is(true));
181  1 assertThat(items, hasSize(3));
182    }
183   
 
184  1 toggle @Test
185    public void publishMarkedAsDeletedResourceWillNotPublishAncestorsIfTheyAreNotDeleted() throws Exception {
186    // GIVEN
187  1 definition.setCommand("activate");
188  1 definition.setDeletionActivation(true);
189  1 Node foo = session.getRootNode().getNode("foo");
190  1 Node bar = foo.getNode("bar");
191  1 Node test = bar.getNode("test");
192   
193  1 foo.setProperty(NodeTypes.Activatable.ACTIVATION_STATUS, true);
194   
195  1 bar.setProperty(NodeTypes.Activatable.ACTIVATION_STATUS, true);
196   
197  1 test.setProperty(NodeTypes.Activatable.ACTIVATION_STATUS, true);
198  1 test.addMixin(NodeTypes.Deleted.NAME);
199   
200    // WHEN
201  1 action.execute();
202  1 List<JcrItemAdapter> items = action.getItems();
203   
204    // THEN
205  1 assertThat(!(Boolean) context.getAttribute("command_result"), is(true));
206  1 assertThat(items, hasSize(1));
207    }
208   
 
209  1 toggle @Test
210    @SuppressWarnings("unchecked")
211    public void eventWithCorrectPathIsFiredAfterPublication() throws Exception {
212    // GIVEN
213  1 definition.setCommand("activate");
214  1 definition.setDeletionActivation(true);
215  1 Node foo = session.getRootNode().getNode("foo");
216  1 Node bar = foo.getNode("bar");
217  1 Node test = bar.getNode("test");
218   
219  1 foo.setProperty(NodeTypes.Activatable.ACTIVATION_STATUS, true);
220  1 foo.addMixin(NodeTypes.Deleted.NAME);
221   
222  1 bar.setProperty(NodeTypes.Activatable.ACTIVATION_STATUS, true);
223  1 bar.addMixin(NodeTypes.Deleted.NAME);
224   
225  1 test.setProperty(NodeTypes.Activatable.ACTIVATION_STATUS, true);
226  1 test.addMixin(NodeTypes.Deleted.NAME);
227   
228    // WHEN
229  1 action.execute();
230   
231    // THEN
232  1 ArgumentCaptor<ContentChangedEvent> argumentCaptor = ArgumentCaptor.forClass(ContentChangedEvent.class);
233  1 verify(eventBus, atLeastOnce()).fireEvent(argumentCaptor.capture());
234  1 String result = ((List<String>) argumentCaptor.getValue().getItemId()).get(0);
235  1 assertThat(result, is("/foo/bar/test"));
236    }
237    }