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; |
35 |
|
|
36 |
|
import static org.mockito.Mockito.mock; |
37 |
|
|
38 |
|
import info.magnolia.cms.security.MgnlGroupManager; |
39 |
|
import info.magnolia.cms.security.MgnlRoleManager; |
40 |
|
import info.magnolia.cms.security.MgnlUser; |
41 |
|
import info.magnolia.cms.security.SecuritySupport; |
42 |
|
import info.magnolia.cms.security.SecuritySupportImpl; |
43 |
|
import info.magnolia.commands.CommandsManager; |
44 |
|
import info.magnolia.context.MgnlContext; |
45 |
|
import info.magnolia.event.RecordingEventBus; |
46 |
|
import info.magnolia.jcr.node2bean.Node2BeanProcessor; |
47 |
|
import info.magnolia.jcr.util.NodeNameHelper; |
48 |
|
import info.magnolia.jcr.util.NodeTypes; |
49 |
|
import info.magnolia.jcr.util.NodeUtil; |
50 |
|
import info.magnolia.module.ModuleRegistry; |
51 |
|
import info.magnolia.objectfactory.Components; |
52 |
|
import info.magnolia.repository.RepositoryConstants; |
53 |
|
import info.magnolia.rest.registry.EndpointDefinitionRegistry; |
54 |
|
import info.magnolia.rest.service.command.definition.CommandDefinition; |
55 |
|
import info.magnolia.rest.service.command.definition.CommandEndpointDefinition; |
56 |
|
import info.magnolia.rest.service.command.definition.ConfiguredCommandDefinition; |
57 |
|
import info.magnolia.rest.service.command.definition.ConfiguredCommandEndpointDefinition; |
58 |
|
import info.magnolia.rest.service.command.v1.CommandEndpoint; |
59 |
|
import info.magnolia.test.ComponentsTestUtil; |
60 |
|
import info.magnolia.test.RepositoryTestCase; |
61 |
|
import info.magnolia.test.mock.MockContext; |
62 |
|
|
63 |
|
import java.util.ArrayList; |
64 |
|
import java.util.Collection; |
65 |
|
import java.util.Collections; |
66 |
|
|
67 |
|
import javax.inject.Inject; |
68 |
|
import javax.jcr.Node; |
69 |
|
import javax.jcr.RepositoryException; |
70 |
|
import javax.jcr.Session; |
71 |
|
|
72 |
|
import org.junit.Before; |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (41) |
Complexity: 5 |
Complexity Density: 0.15 |
|
77 |
|
public class CommandEndpointTestCase extends RepositoryTestCase { |
78 |
|
|
79 |
|
protected Session configSession; |
80 |
|
protected Node2BeanProcessor node2BeanProcessor; |
81 |
|
protected CommandsManager commandsManager; |
82 |
|
protected RecordingEventBus eventBus; |
83 |
|
protected EndpointDefinitionRegistry endpointDefinitionRegistry; |
84 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 1 |
Complexity Density: 0.06 |
|
85 |
10 |
@Override... |
86 |
|
@Before |
87 |
|
public void setUp() throws Exception { |
88 |
10 |
super.setUp(); |
89 |
10 |
configSession = MgnlContext.getJCRSession(RepositoryConstants.CONFIG); |
90 |
10 |
node2BeanProcessor = Components.getComponent(Node2BeanProcessor.class); |
91 |
10 |
commandsManager = mock(CommandsManager.class); |
92 |
|
|
93 |
10 |
ComponentsTestUtil.setImplementation(CommandDefinition.class, ConfiguredCommandDefinition.class); |
94 |
10 |
ComponentsTestUtil.setInstance(CommandsManager.class, commandsManager); |
95 |
|
|
96 |
10 |
final SecuritySupportImpl securitySupport = new SecuritySupportImpl(); |
97 |
10 |
securitySupport.setGroupManager(new MgnlGroupManager(mock(NodeNameHelper.class))); |
98 |
10 |
securitySupport.setRoleManager(new MgnlRoleManager(mock(NodeNameHelper.class))); |
99 |
10 |
ComponentsTestUtil.setInstance(SecuritySupport.class, securitySupport); |
100 |
|
|
101 |
10 |
ModuleRegistry moduleRegistry = mock(ModuleRegistry.class); |
102 |
|
|
103 |
10 |
eventBus = new RecordingEventBus(); |
104 |
10 |
endpointDefinitionRegistry = new EndpointDefinitionRegistry(moduleRegistry, eventBus); |
105 |
|
|
106 |
10 |
Collection<String> roles = new ArrayList<String>(); |
107 |
10 |
roles.add("superuser"); |
108 |
|
|
109 |
10 |
MockContext context = (MockContext) MgnlContext.getInstance(); |
110 |
10 |
context.setUser(new MgnlUser("superuser", "admin", Collections.EMPTY_LIST, roles, Collections.EMPTY_MAP, null, null, Collections.EMPTY_LIST, roles)); |
111 |
|
|
112 |
10 |
MgnlContext.setInstance(context); |
113 |
|
} |
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
@param |
120 |
|
@param |
121 |
|
@param |
122 |
|
@param |
123 |
|
@param |
124 |
|
@return |
125 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (21) |
Complexity: 4 |
Complexity Density: 0.27 |
|
126 |
10 |
protected Node setupEndpoint(String endpointName, String catalogName, String commandName, boolean withAccessDefinition, String roleName) throws RepositoryException {... |
127 |
|
|
128 |
10 |
final Node testEndpointNode = NodeUtil.createPath(configSession.getRootNode(), "/modules/yet-another-test/rest-endpoints", NodeTypes.ContentNode.NAME); |
129 |
|
|
130 |
|
|
131 |
10 |
final Node testEndpoint = NodeUtil.createPath(testEndpointNode, "/" + endpointName, NodeTypes.ContentNode.NAME); |
132 |
10 |
testEndpoint.setProperty("enabled", true); |
133 |
10 |
testEndpoint.setProperty("class", ConfiguredCommandEndpointDefinition.class.getName()); |
134 |
10 |
testEndpoint.setProperty("implementationClass", TestCommandEndpoint.class.getName()); |
135 |
|
|
136 |
10 |
final Node enabledCommands = NodeUtil.createPath(testEndpoint, "/enabledCommands", NodeTypes.ContentNode.NAME); |
137 |
|
|
138 |
|
|
139 |
10 |
final Node dummyCommand = NodeUtil.createPath(enabledCommands, "/" + commandName, NodeTypes.ContentNode.NAME); |
140 |
10 |
dummyCommand.setProperty("commandName", commandName); |
141 |
10 |
if (catalogName != null) { |
142 |
8 |
dummyCommand.setProperty("catalogName", catalogName); |
143 |
|
} |
144 |
|
|
145 |
|
|
146 |
10 |
if (withAccessDefinition) { |
147 |
9 |
String roleNameToUse = roleName == null ? "superuser" : roleName; |
148 |
9 |
final Node roles = NodeUtil.createPath(dummyCommand, "/access/roles", NodeTypes.ContentNode.NAME); |
149 |
9 |
roles.setProperty(roleNameToUse, roleNameToUse); |
150 |
|
} |
151 |
|
|
152 |
10 |
return testEndpoint; |
153 |
|
} |
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 3 |
Complexity Density: 1 |
|
158 |
|
protected class TestCommandEndpoint extends CommandEndpoint { |
159 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
160 |
8 |
@Inject... |
161 |
|
public TestCommandEndpoint(CommandEndpointDefinition commandEndpointDefinition, CommandsManager commandsManager) { |
162 |
8 |
super(commandEndpointDefinition, commandsManager); |
163 |
|
} |
164 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
165 |
8 |
@Override... |
166 |
|
public boolean isCommandExecutableByCurrentUser(String catalogName, String commandName) { |
167 |
8 |
return super.isCommandExecutableByCurrentUser(catalogName, commandName); |
168 |
|
} |
169 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
170 |
0 |
public Object getNode(String param) throws RepositoryException {... |
171 |
0 |
return new Object[]{param}; |
172 |
|
} |
173 |
|
} |
174 |
|
} |