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.rest.service.command.v1; |
35 |
|
|
36 |
|
import static org.junit.Assert.*; |
37 |
|
import static org.mockito.Matchers.any; |
38 |
|
import static org.mockito.Matchers.anyString; |
39 |
|
import static org.mockito.Mockito.*; |
40 |
|
|
41 |
|
import info.magnolia.cms.security.operations.AccessDefinition; |
42 |
|
import info.magnolia.commands.chain.Command; |
43 |
|
import info.magnolia.commands.chain.Context; |
44 |
|
import info.magnolia.config.source.jcr.JcrConfigurationSource; |
45 |
|
import info.magnolia.jcr.node2bean.Node2BeanException; |
46 |
|
import info.magnolia.jcr.util.NodeTypes; |
47 |
|
import info.magnolia.jcr.util.NodeUtil; |
48 |
|
import info.magnolia.module.ModuleRegistry; |
49 |
|
import info.magnolia.objectfactory.Components; |
50 |
|
import info.magnolia.rest.EndpointDefinition; |
51 |
|
import info.magnolia.rest.registry.EndpointDefinitionRegistryEvent; |
52 |
|
import info.magnolia.rest.service.command.CommandEndpointTestCase; |
53 |
|
import info.magnolia.rest.service.command.definition.CommandDefinition; |
54 |
|
import info.magnolia.rest.service.command.definition.ConfiguredCommandEndpointDefinition; |
55 |
|
import info.magnolia.test.ComponentsTestUtil; |
56 |
|
import info.magnolia.transformer.BeanTypeResolver; |
57 |
|
|
58 |
|
import java.util.ArrayList; |
59 |
|
import java.util.HashMap; |
60 |
|
import java.util.List; |
61 |
|
import java.util.Map; |
62 |
|
|
63 |
|
import javax.jcr.Node; |
64 |
|
import javax.jcr.RepositoryException; |
65 |
|
import javax.ws.rs.core.Response; |
66 |
|
|
67 |
|
import org.apache.jackrabbit.commons.predicate.Predicate; |
68 |
|
import org.junit.Before; |
69 |
|
import org.junit.Ignore; |
70 |
|
import org.junit.Test; |
71 |
|
import org.mockito.invocation.InvocationOnMock; |
72 |
|
import org.mockito.stubbing.Answer; |
73 |
|
|
74 |
|
import com.google.common.collect.Sets; |
75 |
|
|
76 |
|
|
77 |
|
@link |
78 |
|
|
|
|
| 69.6% |
Uncovered Elements: 34 (112) |
Complexity: 17 |
Complexity Density: 0.18 |
|
79 |
|
public class CommandEndpointTest extends CommandEndpointTestCase { |
80 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
81 |
8 |
@Override... |
82 |
|
@Before |
83 |
|
public void setUp() throws Exception { |
84 |
8 |
super.setUp(); |
85 |
8 |
BeanTypeResolver beanTypeResolver = new BeanTypeResolver(); |
86 |
8 |
ComponentsTestUtil.setInstance(BeanTypeResolver.class, beanTypeResolver); |
87 |
|
|
88 |
8 |
when(commandsManager.getCommand(anyString(), anyString())).thenAnswer(new Answer<Command>() { |
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
89 |
2 |
@Override... |
90 |
|
public Command answer(InvocationOnMock invocation) throws Throwable { |
91 |
2 |
String commandName = (String) invocation.getArguments()[1]; |
92 |
2 |
if (commandName.equals("failingCommand")) |
93 |
1 |
return new Command() { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
94 |
1 |
@Override... |
95 |
|
public boolean execute(Context context) throws Exception { |
96 |
1 |
throw new Exception("Exception from mock command"); |
97 |
|
} |
98 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
99 |
0 |
@Override... |
100 |
|
public Command clone() throws CloneNotSupportedException { |
101 |
0 |
return null; |
102 |
|
} |
103 |
|
}; |
104 |
|
else |
105 |
1 |
return new Command() { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
106 |
1 |
@Override... |
107 |
|
public boolean execute(Context context) throws Exception { |
108 |
1 |
return true; |
109 |
|
} |
110 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
111 |
0 |
@Override... |
112 |
|
public Command clone() throws CloneNotSupportedException { |
113 |
0 |
return null; |
114 |
|
} |
115 |
|
}; |
116 |
|
} |
117 |
|
}); |
118 |
8 |
when(commandsManager.executeCommand(any(Command.class), any(Map.class))).thenAnswer(new Answer<Boolean>() { |
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
119 |
2 |
@Override... |
120 |
|
public Boolean answer(InvocationOnMock invocation) throws Throwable { |
121 |
2 |
Command command = (Command) invocation.getArguments()[0]; |
122 |
2 |
return command.execute(null); |
123 |
|
} |
124 |
|
}); |
125 |
|
} |
126 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
127 |
1 |
@Test... |
128 |
|
public void testExecuteCommand() throws RepositoryException, Node2BeanException, IllegalAccessException, InstantiationException { |
129 |
|
|
130 |
1 |
Node testEndpoint = setupEndpoint("test-endpoint", "dummy", "dummy", true, null); |
131 |
|
|
132 |
1 |
ConfiguredCommandEndpointDefinition configuredCommandEndpointDefinition = (ConfiguredCommandEndpointDefinition) node2BeanProcessor.toBean(testEndpoint); |
133 |
1 |
CommandEndpoint endpoint = (CommandEndpoint) Components.newInstance(configuredCommandEndpointDefinition.getImplementationClass(), configuredCommandEndpointDefinition); |
134 |
|
|
135 |
|
|
136 |
1 |
Response response = endpoint.executeCommand("dummy", "dummy", new HashMap<String, Object>()); |
137 |
|
|
138 |
|
|
139 |
1 |
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); |
140 |
|
} |
141 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
142 |
1 |
@Test... |
143 |
|
public void testExecuteCommandReturnsInternalServerErrorIfCommandFails() throws RepositoryException, Node2BeanException, IllegalAccessException, InstantiationException { |
144 |
|
|
145 |
1 |
Node testEndpoint = setupEndpoint("test-endpoint", "dummy", "failingCommand", true, null); |
146 |
|
|
147 |
1 |
ConfiguredCommandEndpointDefinition configuredCommandEndpointDefinition = (ConfiguredCommandEndpointDefinition) node2BeanProcessor.toBean(testEndpoint); |
148 |
1 |
CommandEndpoint endpoint = (CommandEndpoint) Components.newInstance(configuredCommandEndpointDefinition.getImplementationClass(), configuredCommandEndpointDefinition); |
149 |
|
|
150 |
|
|
151 |
1 |
Response response = endpoint.executeCommand("dummy", "failingCommand", new HashMap<String, Object>()); |
152 |
|
|
153 |
|
|
154 |
1 |
assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus()); |
155 |
|
} |
156 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
157 |
1 |
@Test... |
158 |
|
public void testExecuteCommandReturnsForbiddenIfAccessToCommandIsNotAllowed() throws RepositoryException, Node2BeanException, IllegalAccessException, InstantiationException { |
159 |
|
|
160 |
1 |
Node testEndpoint = setupEndpoint("test-endpoint", "dummy", "failingCommand", true, "bogus-role"); |
161 |
|
|
162 |
1 |
ConfiguredCommandEndpointDefinition configuredCommandEndpointDefinition = (ConfiguredCommandEndpointDefinition) node2BeanProcessor.toBean(testEndpoint); |
163 |
1 |
CommandEndpoint endpoint = (CommandEndpoint) Components.newInstance(configuredCommandEndpointDefinition.getImplementationClass(), configuredCommandEndpointDefinition); |
164 |
|
|
165 |
|
|
166 |
1 |
Response response = endpoint.executeCommand("dummy", "dummy", new HashMap<String, Object>()); |
167 |
|
|
168 |
|
|
169 |
1 |
assertEquals(Response.Status.FORBIDDEN.getStatusCode(), response.getStatus()); |
170 |
|
} |
171 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
172 |
1 |
@Test... |
173 |
|
public void testConfiguredCommandEndpointDefinitionWithAccessDefinition() throws RepositoryException, Node2BeanException, IllegalAccessException, InstantiationException { |
174 |
|
|
175 |
1 |
Node testEndpoint = setupEndpoint("test-endpoint-1", "dummy", "dummy", true, null); |
176 |
1 |
testEndpoint.setProperty("implementationClass", TestCommandEndpoint.class.getName()); |
177 |
|
|
178 |
1 |
ConfiguredCommandEndpointDefinition configuredCommandEndpointDefinition = (ConfiguredCommandEndpointDefinition) node2BeanProcessor.toBean(testEndpoint); |
179 |
1 |
TestCommandEndpoint endpoint = (TestCommandEndpoint) Components.newInstance(configuredCommandEndpointDefinition.getImplementationClass(), configuredCommandEndpointDefinition); |
180 |
|
|
181 |
1 |
assertTrue(endpoint.getEndpointDefinition().isEnabled()); |
182 |
|
|
183 |
|
|
184 |
1 |
boolean isExecutable = endpoint.isCommandExecutableByCurrentUser("dummy", "dummy"); |
185 |
|
|
186 |
|
|
187 |
1 |
assertTrue("Dummy command should be executable!", isExecutable); |
188 |
|
} |
189 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
190 |
1 |
@Test... |
191 |
|
public void testConfiguredCommandEndpointDefinitionWithoutAccessDefinition() throws RepositoryException, Node2BeanException { |
192 |
|
|
193 |
1 |
Node testEndpoint = setupEndpoint("test-endpoint-2", "dummy", "dummy", false, null); |
194 |
1 |
testEndpoint.setProperty("implementationClass", TestCommandEndpoint.class.getName()); |
195 |
|
|
196 |
1 |
ConfiguredCommandEndpointDefinition configuredCommandEndpointDefinition = (ConfiguredCommandEndpointDefinition) node2BeanProcessor.toBean(testEndpoint); |
197 |
1 |
TestCommandEndpoint endpoint = (TestCommandEndpoint) Components.newInstance(configuredCommandEndpointDefinition.getImplementationClass(), configuredCommandEndpointDefinition); |
198 |
|
|
199 |
1 |
assertTrue(endpoint.getEndpointDefinition().isEnabled()); |
200 |
|
|
201 |
|
|
202 |
1 |
boolean isExecutable = endpoint.isCommandExecutableByCurrentUser("dummy", "dummy"); |
203 |
|
|
204 |
|
|
205 |
1 |
assertFalse("Dummy command should not be executable!", isExecutable); |
206 |
|
} |
207 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
208 |
1 |
@Test... |
209 |
|
public void testConfiguredCommandEndpointDefinitionWithoutCatalogDefinition() throws RepositoryException, Node2BeanException { |
210 |
|
|
211 |
1 |
Node testEndpoint = setupEndpoint("test-endpoint-3", null, "dummy", true, null); |
212 |
1 |
testEndpoint.setProperty("implementationClass", TestCommandEndpoint.class.getName()); |
213 |
|
|
214 |
1 |
ConfiguredCommandEndpointDefinition configuredCommandEndpointDefinition = (ConfiguredCommandEndpointDefinition) node2BeanProcessor.toBean(testEndpoint); |
215 |
1 |
TestCommandEndpoint endpoint = (TestCommandEndpoint) Components.newInstance(configuredCommandEndpointDefinition.getImplementationClass(), configuredCommandEndpointDefinition); |
216 |
|
|
217 |
1 |
assertTrue(endpoint.getEndpointDefinition().isEnabled()); |
218 |
|
|
219 |
|
|
220 |
1 |
boolean isExecutable = endpoint.isCommandExecutableByCurrentUser(null, "dummy"); |
221 |
|
|
222 |
|
|
223 |
1 |
assertTrue("Dummy command should be executable!", isExecutable); |
224 |
|
} |
225 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
226 |
1 |
@Test... |
227 |
|
public void testConfiguredCommandEndpointDefinitionWithoutCatalogDefinitionRequestingACatalog() throws RepositoryException, Node2BeanException { |
228 |
|
|
229 |
1 |
Node testEndpoint = setupEndpoint("test-endpoint-4", null, "dummy", true, null); |
230 |
1 |
testEndpoint.setProperty("implementationClass", TestCommandEndpoint.class.getName()); |
231 |
|
|
232 |
1 |
ConfiguredCommandEndpointDefinition configuredCommandEndpointDefinition = (ConfiguredCommandEndpointDefinition) node2BeanProcessor.toBean(testEndpoint); |
233 |
1 |
TestCommandEndpoint endpoint = (TestCommandEndpoint) Components.newInstance(configuredCommandEndpointDefinition.getImplementationClass(), configuredCommandEndpointDefinition); |
234 |
|
|
235 |
1 |
assertTrue(endpoint.getEndpointDefinition().isEnabled()); |
236 |
|
|
237 |
|
|
238 |
1 |
boolean isExecutable = endpoint.isCommandExecutableByCurrentUser("dummy", "dummy"); |
239 |
|
|
240 |
|
|
241 |
1 |
assertFalse("Dummy command should not be executable!", isExecutable); |
242 |
|
} |
243 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
244 |
1 |
@Test... |
245 |
|
public void testConfiguredCommandEndpointDefinitionWithoutCommandName() throws RepositoryException, Node2BeanException { |
246 |
|
|
247 |
1 |
Node testEndpoint = setupEndpoint("test-endpoint-5", "dummy", "dummy", true, null); |
248 |
1 |
testEndpoint.setProperty("implementationClass", TestCommandEndpoint.class.getName()); |
249 |
|
|
250 |
1 |
ConfiguredCommandEndpointDefinition configuredCommandEndpointDefinition = (ConfiguredCommandEndpointDefinition) node2BeanProcessor.toBean(testEndpoint); |
251 |
1 |
TestCommandEndpoint endpoint = (TestCommandEndpoint) Components.newInstance(configuredCommandEndpointDefinition.getImplementationClass(), configuredCommandEndpointDefinition); |
252 |
|
|
253 |
1 |
assertTrue(endpoint.getEndpointDefinition().isEnabled()); |
254 |
|
|
255 |
|
|
256 |
1 |
boolean isExecutable = endpoint.isCommandExecutableByCurrentUser(null, null); |
257 |
|
|
258 |
|
|
259 |
1 |
assertFalse("Dummy command should not be executable!", isExecutable); |
260 |
|
} |
261 |
|
|
|
|
| 0% |
Uncovered Elements: 29 (29) |
Complexity: 1 |
Complexity Density: 0.03 |
1PASS
|
|
262 |
0 |
@Test... |
263 |
|
@Ignore("Cross-testing an endpoint registration from JCR; EndpointDefinitionRegistry is well tested already, and nothing command-specific") |
264 |
|
public void testCommandEndpointRegistration() throws Exception { |
265 |
|
|
266 |
0 |
final Node testEndpointNode = NodeUtil.createPath(configSession.getRootNode(), "/modules/yet-another-test/rest-endpoints", NodeTypes.ContentNode.NAME); |
267 |
0 |
final Node testEndpoint = NodeUtil.createPath(testEndpointNode, "/test-3", NodeTypes.ContentNode.NAME); |
268 |
0 |
testEndpoint.setProperty("enabled", true); |
269 |
0 |
testEndpoint.setProperty("class", ConfiguredCommandEndpointDefinition.class.getName()); |
270 |
0 |
testEndpoint.setProperty("implementationClass", TestCommandEndpoint.class.getName()); |
271 |
|
|
272 |
0 |
final Node enabledCommands = NodeUtil.createPath(testEndpoint, "/enabledCommands", NodeTypes.ContentNode.NAME); |
273 |
|
|
274 |
|
|
275 |
0 |
final Node dummyCommand = NodeUtil.createPath(enabledCommands, "/dummy", NodeTypes.ContentNode.NAME); |
276 |
0 |
dummyCommand.setProperty("commandName", "dummy"); |
277 |
0 |
dummyCommand.setProperty("catalogName", "dummy"); |
278 |
|
|
279 |
|
|
280 |
0 |
final Node roles = NodeUtil.createPath(dummyCommand, "/access/roles", NodeTypes.ContentNode.NAME); |
281 |
0 |
roles.setProperty("superuser", "superuser"); |
282 |
|
|
283 |
0 |
List<Node> nodes = new ArrayList<Node>(); |
284 |
0 |
nodes.add(testEndpointNode); |
285 |
|
|
286 |
|
|
287 |
0 |
ModuleRegistry moduleRegistry = mock(ModuleRegistry.class); |
288 |
0 |
when(moduleRegistry.getModuleNames()).thenReturn(Sets.newHashSet("yet-another-test")); |
289 |
0 |
JcrConfigurationSource<EndpointDefinition> jcrConfigurationSource = new JcrConfigurationSource<>(endpointDefinitionRegistry, "rest-endpoints", Predicate.TRUE, node2BeanProcessor, moduleRegistry); |
290 |
0 |
jcrConfigurationSource.start(); |
291 |
|
|
292 |
|
|
293 |
0 |
assertEquals(1, eventBus.getSentEvents().size()); |
294 |
0 |
EndpointDefinitionRegistryEvent event = (EndpointDefinitionRegistryEvent) eventBus.getSentEvents().get(0); |
295 |
0 |
ConfiguredCommandEndpointDefinition endpointDefinition = (ConfiguredCommandEndpointDefinition) event.getEndpointDefinition(); |
296 |
0 |
assertEquals("test-3", endpointDefinition.getName()); |
297 |
0 |
assertTrue(endpointDefinition.isEnabled()); |
298 |
0 |
assertEquals(TestCommandEndpoint.class, endpointDefinition.getImplementationClass()); |
299 |
|
|
300 |
0 |
CommandDefinition commandDefinition = endpointDefinition.getEnabledCommands().iterator().next(); |
301 |
0 |
AccessDefinition accessDefinition = commandDefinition.getAccess(); |
302 |
|
|
303 |
0 |
assertFalse(endpointDefinition.getEnabledCommands().isEmpty()); |
304 |
0 |
assertEquals("dummy", commandDefinition.getCommandName()); |
305 |
0 |
assertEquals("dummy", commandDefinition.getCatalogName()); |
306 |
0 |
assertTrue(accessDefinition.getRoles().contains("superuser")); |
307 |
|
} |
308 |
|
} |