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

 

Code metrics

0
39
4
1
168
102
4
0.1
9.75
4
1

Classes

Class Line # Actions
UploadResourceActionTest 81 39 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 2 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.resourceloader.dummy.DummyResourceOrigin.Differentiator.qux;
37    import static info.magnolia.resources.app.workbench.ResourcesContainer.RESOURCE_PATH;
38    import static info.magnolia.test.hamcrest.NodeMatchers.*;
39    import static org.hamcrest.CoreMatchers.not;
40    import static org.junit.Assert.*;
41    import static org.mockito.BDDMockito.given;
42    import static org.mockito.Matchers.*;
43    import static org.mockito.Mockito.*;
44   
45    import info.magnolia.cms.beans.runtime.FileProperties;
46    import info.magnolia.context.MgnlContext;
47    import info.magnolia.context.SystemContext;
48    import info.magnolia.event.EventBus;
49    import info.magnolia.i18nsystem.SimpleTranslator;
50    import info.magnolia.resourceloader.ResourceOrigin;
51    import info.magnolia.resourceloader.dummy.DummyResourceOrigin;
52    import info.magnolia.resourceloader.jcr.JcrResourceOrigin;
53    import info.magnolia.resourceloader.jcr.JcrResourceOriginFactory;
54    import info.magnolia.resourceloader.layered.LayeredResourceOrigin;
55    import info.magnolia.resourceloader.layered.LayeredResourceOriginFactory;
56    import info.magnolia.resources.app.ResourcesContentConnector;
57    import info.magnolia.test.MgnlTestCase;
58    import info.magnolia.test.mock.MockContext;
59    import info.magnolia.test.mock.jcr.MockSession;
60    import info.magnolia.ui.api.context.UiContext;
61    import info.magnolia.ui.api.event.ContentChangedEvent;
62    import info.magnolia.ui.dialog.formdialog.FormDialogPresenter;
63    import info.magnolia.ui.dialog.formdialog.FormDialogPresenterFactory;
64    import info.magnolia.ui.form.EditorCallback;
65   
66    import javax.jcr.Binary;
67    import javax.jcr.Node;
68   
69    import org.apache.jackrabbit.JcrConstants;
70    import org.apache.jackrabbit.value.BinaryImpl;
71    import org.junit.Before;
72    import org.junit.Test;
73    import org.mockito.ArgumentCaptor;
74    import org.mockito.invocation.InvocationOnMock;
75    import org.mockito.stubbing.Answer;
76   
77    import com.google.inject.util.Providers;
78    import com.vaadin.data.Item;
79    import com.vaadin.data.util.ObjectProperty;
80   
 
81    public class UploadResourceActionTest extends MgnlTestCase {
82   
83    private static final String FILE_NAME = "test.png";
84   
85    private UploadResourceAction action;
86    private MockSession session;
87    private Item resourceItem;
88    private LayeredResourceOrigin origin;
89    private EventBus eventBus;
90    private ArgumentCaptor<ContentChangedEvent> argument = ArgumentCaptor.forClass(ContentChangedEvent.class);
91   
 
92  2 toggle @Override
93    @Before
94    public void setUp() throws Exception {
95  2 super.setUp();
96   
97  2 MockContext context = (MockContext) MgnlContext.getInstance();
98  2 session = new MockSession(JcrResourceOrigin.RESOURCES_WORKSPACE);
99  2 context.addSession(JcrResourceOrigin.RESOURCES_WORKSPACE, session);
100   
101  2 DummyResourceOrigin dummyOrigin = new DummyResourceOrigin(qux, null, null, new String[]{"/x", "/x/y"});
102  2 JcrResourceOrigin jcrResourceOrigin = new JcrResourceOriginFactory(Providers.<SystemContext>of(context)).create("jcr");
103  2 this.origin = new LayeredResourceOriginFactory().create("layeredOrigin", new ResourceOrigin[]{jcrResourceOrigin, dummyOrigin});
104   
105  2 resourceItem = mock(Item.class);
106  2 given(resourceItem.getItemProperty(JcrConstants.JCR_DATA)).willReturn(new ObjectProperty<Binary>(new BinaryImpl("blah".getBytes())));
107  2 given(resourceItem.getItemProperty(FileProperties.PROPERTY_CONTENTTYPE)).willReturn(new ObjectProperty<String>("image/png"));
108  2 given(resourceItem.getItemProperty(FileProperties.PROPERTY_FILENAME)).willReturn(new ObjectProperty<String>(FILE_NAME));
109   
110  2 ResourcesContentConnector contentConnector = new ResourcesContentConnector(origin, context);
111   
112  2 UploadResourceActionDefinition definition = new UploadResourceActionDefinition();
113  2 definition.setDialogName("uploadResource");
114   
115  2 FormDialogPresenter formDialogPresenter = mock(FormDialogPresenter.class);
116  2 doAnswer(new Answer() {
 
117  2 toggle @Override
118    public Object answer(InvocationOnMock inv) throws Throwable {
119  2 final Object[] args = inv.getArguments();
120  2 final EditorCallback callback = (EditorCallback) args[3];
121  2 callback.onSuccess("commit");
122  2 return null;
123    }
124    }).when(formDialogPresenter).start(any(Item.class), any(String.class), any(UiContext.class), any(EditorCallback.class));
125   
126  2 FormDialogPresenterFactory formDialogPresenterFactory = mock(FormDialogPresenterFactory.class);
127  2 given(formDialogPresenterFactory.createFormDialogPresenter(anyString())).willReturn(formDialogPresenter);
128   
129  2 eventBus = mock(EventBus.class);
130   
131  2 action = new UploadResourceAction(definition, resourceItem, contentConnector, formDialogPresenterFactory, mock(UiContext.class), mock(SimpleTranslator.class), eventBus, origin);
132    }
133   
 
134  1 toggle @Test
135    public void canUploadResourceToRoot() throws Exception {
136    // GIVEN
137  1 given(resourceItem.getItemProperty(RESOURCE_PATH)).willReturn(new ObjectProperty<>(origin.getByPath("/").getPath()));
138  1 Node root = session.getRootNode();
139  1 assertThat(root, not(hasNode(nodeName(FILE_NAME))));
140   
141    // WHEN
142  1 action.execute();
143   
144    // THEN
145  1 assertThat(root, hasNode(nodeName(FILE_NAME)));
146   
147  1 verify(eventBus).fireEvent(argument.capture());
148  1 assertTrue(argument.getValue().isItemContentChanged());
149    }
150   
 
151  1 toggle @Test
152    public void canCreateResourceIfItParentIsNotAvailableInJcr() throws Exception {
153    // GIVEN
154  1 given(resourceItem.getItemProperty(RESOURCE_PATH)).willReturn(new ObjectProperty<>(origin.getByPath("/x").getPath()));
155  1 Node root = session.getRootNode();
156  1 assertThat(root, not(hasNode(nodeName("x"))));
157   
158    // WHEN
159  1 action.execute();
160   
161    // THEN
162  1 assertThat(root, hasNode(nodeName("x")));
163  1 assertThat(root.getNode("x"), hasNode(nodeName(FILE_NAME)));
164   
165  1 verify(eventBus).fireEvent(argument.capture());
166  1 assertTrue(argument.getValue().isItemContentChanged());
167    }
168    }