1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (43) |
Complexity: 4 |
Complexity Density: 0.1 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (20) |
Complexity: 1 |
Complexity Density: 0.05 |
|
92 |
2 |
@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() { |
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
117 |
2 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
134 |
1 |
@Test... |
135 |
|
public void canUploadResourceToRoot() throws Exception { |
136 |
|
|
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 |
|
|
142 |
1 |
action.execute(); |
143 |
|
|
144 |
|
|
145 |
1 |
assertThat(root, hasNode(nodeName(FILE_NAME))); |
146 |
|
|
147 |
1 |
verify(eventBus).fireEvent(argument.capture()); |
148 |
1 |
assertTrue(argument.getValue().isItemContentChanged()); |
149 |
|
} |
150 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
151 |
1 |
@Test... |
152 |
|
public void canCreateResourceIfItParentIsNotAvailableInJcr() throws Exception { |
153 |
|
|
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 |
|
|
159 |
1 |
action.execute(); |
160 |
|
|
161 |
|
|
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 |
|
} |