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 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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (64) |
Complexity: 5 |
Complexity Density: 0.08 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (23) |
Complexity: 1 |
Complexity Density: 0.04 |
|
88 |
3 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
119 |
3 |
@Override... |
120 |
|
protected String getRepositoryConfigFileName() { |
121 |
3 |
return "test-resource-repositories.xml"; |
122 |
|
} |
123 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
124 |
1 |
@Test... |
125 |
|
public void restoreVersion() throws Exception { |
126 |
|
|
127 |
1 |
versionManager.addVersion(node); |
128 |
|
|
129 |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
149 |
1 |
@Test... |
150 |
|
public void checkVersionCreatedBeforeRestore() throws Exception { |
151 |
|
|
152 |
1 |
versionManager.addVersion(node); |
153 |
|
|
154 |
|
|
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 |
|
|
165 |
1 |
action.getEditorCallback().onSuccess(""); |
166 |
|
|
167 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
175 |
1 |
@Test... |
176 |
|
public void doNotCreateVersionBeforeRestoreIfNotAllowed() throws Exception { |
177 |
|
|
178 |
1 |
versionManager.addVersion(node); |
179 |
|
|
180 |
|
|
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 |
|
|
193 |
1 |
action.getEditorCallback().onSuccess(""); |
194 |
|
|
195 |
|
|
196 |
1 |
assertEquals(2, versionManager.getAllVersions(node).getSize()); |
197 |
|
} |
198 |
|
|
199 |
|
|
200 |
|
@link |
201 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
202 |
|
private class MockRestoreResourceVersionAction extends RestoreResourceVersionAction { |
203 |
|
|
204 |
|
private String version = "1.0"; |
205 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
206 |
3 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
210 |
3 |
@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 |
|
} |