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.v2; |
35 |
|
|
36 |
|
import static org.junit.Assert.*; |
37 |
|
import static org.mockito.Mockito.*; |
38 |
|
|
39 |
|
import info.magnolia.context.MgnlContext; |
40 |
|
import info.magnolia.objectfactory.Components; |
41 |
|
import info.magnolia.rest.service.command.CommandEndpointTestCase; |
42 |
|
import info.magnolia.rest.service.command.TestCommand; |
43 |
|
import info.magnolia.rest.service.command.definition.ConfiguredCommandDefinition; |
44 |
|
import info.magnolia.rest.service.command.definition.ConfiguredCommandEndpointDefinition; |
45 |
|
|
46 |
|
import java.util.Arrays; |
47 |
|
import java.util.HashMap; |
48 |
|
import java.util.Map; |
49 |
|
|
50 |
|
import javax.jcr.Node; |
51 |
|
import javax.ws.rs.core.Response; |
52 |
|
|
53 |
|
import org.apache.commons.lang3.StringUtils; |
54 |
|
import org.junit.Test; |
55 |
|
import org.mockito.invocation.InvocationOnMock; |
56 |
|
import org.mockito.stubbing.Answer; |
57 |
|
|
58 |
|
|
59 |
|
@link |
60 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (33) |
Complexity: 4 |
Complexity Density: 0.14 |
|
61 |
|
public class CommandEndpointTest extends CommandEndpointTestCase { |
62 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 1 |
Complexity Density: 0.07 |
1PASS
|
|
63 |
1 |
@Test... |
64 |
|
public void testCommandEndpointReturnsContextAttributesInResponse() throws Exception { |
65 |
|
|
66 |
1 |
when(commandsManager.getCommand("testCatalog", "testCommand")).thenReturn(new TestCommand()); |
67 |
1 |
when(commandsManager.executeCommand(any(TestCommand.class), anyMap())).thenAnswer(new Answer<Boolean>() { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
68 |
1 |
@Override... |
69 |
|
public Boolean answer(InvocationOnMock invocation) throws Throwable { |
70 |
1 |
return new TestCommand().execute(MgnlContext.getInstance()); |
71 |
|
} |
72 |
|
}); |
73 |
|
|
74 |
1 |
Node testEndpoint = setupEndpoint("test-endpoint", "testCatalog", "testCommand", true, null); |
75 |
|
|
76 |
1 |
ConfiguredCommandEndpointDefinition configuredCommandEndpointDefinition = (ConfiguredCommandEndpointDefinition) node2BeanProcessor.toBean(testEndpoint); |
77 |
1 |
ConfiguredCommandDefinition configuredCommandDefinition = (ConfiguredCommandDefinition) configuredCommandEndpointDefinition.getEnabledCommands().iterator().next(); |
78 |
1 |
configuredCommandDefinition.setContextParameters(Arrays.asList("foo", "bar")); |
79 |
1 |
configuredCommandEndpointDefinition.setImplementationClass(CommandEndpoint.class); |
80 |
1 |
CommandEndpoint endpoint = (CommandEndpoint) Components.newInstance(configuredCommandEndpointDefinition.getImplementationClass(), configuredCommandEndpointDefinition); |
81 |
|
|
82 |
|
|
83 |
1 |
Response response = endpoint.executeCommand("testCatalog", "testCommand", new HashMap<String, Object>()); |
84 |
|
|
85 |
|
|
86 |
1 |
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); |
87 |
1 |
Map<String, Object> entity = (Map<String, Object>) response.getEntity(); |
88 |
1 |
assertEquals(true, entity.get("success")); |
89 |
1 |
assertEquals("fooValue", entity.get("foo")); |
90 |
1 |
assertEquals("barValue", entity.get("bar")); |
91 |
|
} |
92 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
93 |
1 |
@Test... |
94 |
|
public void testCommandEndpointReturnsExceptionMessageAndStackTrace() throws Exception { |
95 |
|
|
96 |
1 |
when(commandsManager.getCommand("testCatalog", "testCommand")).thenReturn(new TestCommand()); |
97 |
1 |
when(commandsManager.executeCommand(any(TestCommand.class), anyMap())).thenAnswer(new Answer<Boolean>() { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
98 |
1 |
@Override... |
99 |
|
public Boolean answer(InvocationOnMock invocation) throws Throwable { |
100 |
1 |
return new TestCommand().execute(MgnlContext.getInstance()); |
101 |
|
} |
102 |
|
}); |
103 |
|
|
104 |
1 |
Node testEndpoint = setupEndpoint("test-endpoint", "testCatalog", "testCommand", true, null); |
105 |
|
|
106 |
1 |
ConfiguredCommandEndpointDefinition configuredCommandEndpointDefinition = (ConfiguredCommandEndpointDefinition) node2BeanProcessor.toBean(testEndpoint); |
107 |
1 |
configuredCommandEndpointDefinition.setImplementationClass(CommandEndpoint.class); |
108 |
1 |
CommandEndpoint endpoint = (CommandEndpoint) Components.newInstance(configuredCommandEndpointDefinition.getImplementationClass(), configuredCommandEndpointDefinition); |
109 |
|
|
110 |
1 |
MgnlContext.setAttribute("fail", true); |
111 |
|
|
112 |
1 |
Response response = endpoint.executeCommand("testCatalog", "testCommand", new HashMap<String, Object>()); |
113 |
|
|
114 |
|
|
115 |
1 |
assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus()); |
116 |
1 |
Map<String, Object> entity = (Map<String, Object>) response.getEntity(); |
117 |
1 |
assertEquals(false, entity.get("success")); |
118 |
1 |
assertEquals("Error", entity.get("exceptionMessage")); |
119 |
1 |
assertTrue(StringUtils.contains((String) entity.get("stackTrace"), "java.lang.Exception:")); |
120 |
|
} |
121 |
|
|
122 |
|
} |