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.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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (59) |
Complexity: 7 |
Complexity Density: 0.13 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (22) |
Complexity: 1 |
Complexity Density: 0.05 |
|
88 |
4 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
120 |
4 |
@Override... |
121 |
|
protected String getRepositoryConfigFileName() { |
122 |
4 |
return "test-resource-repositories.xml"; |
123 |
|
} |
124 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
125 |
1 |
@Test... |
126 |
|
public void execute() throws Exception { |
127 |
|
|
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 |
|
|
135 |
1 |
action.execute(); |
136 |
|
|
137 |
|
|
138 |
1 |
assertThat(versionManager.getBaseVersion(node).getName(), is("1.0")); |
139 |
|
} |
140 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
141 |
1 |
@Test... |
142 |
|
public void executeNoVersion() throws Exception { |
143 |
|
|
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 |
|
|
150 |
1 |
action.execute(); |
151 |
|
|
152 |
|
|
153 |
1 |
assertThat(new Execution() { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
154 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
161 |
1 |
@Test... |
162 |
|
public void executeThreeVersion() throws Exception { |
163 |
|
|
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 |
|
|
173 |
1 |
action.execute(); |
174 |
|
|
175 |
|
|
176 |
1 |
assertThat(versionManager.getBaseVersion(node).getName(), is("1.2")); |
177 |
|
} |
178 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
179 |
1 |
@Test... |
180 |
|
public void eventWithCorrectPathIsFiredAfterRestoreOfPreviousVersion() throws Exception { |
181 |
|
|
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 |
|
|
189 |
1 |
action.execute(); |
190 |
|
|
191 |
|
|
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 |
|
} |