Clover icon

Magnolia REST Services 2.1.3

  1. Project Clover database Fri Oct 25 2019 12:35:42 CEST
  2. Package info.magnolia.rest.service.command

File CommandEndpointTestCase.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

6
36
5
2
174
95
8
0.22
7.2
2.5
1.6

Classes

Class Line # Actions
CommandEndpointTestCase 77 33 0% 5 0
1.0100%
CommandEndpointTestCase.TestCommandEndpoint 158 3 0% 3 2
0.666666766.7%
 

Contributing tests

This file is covered by 10 tests. .

Source view

1    /**
2    * This file Copyright (c) 2015-2018 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
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    * Base test case for command endpoints.
76    */
 
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   
 
85  10 toggle @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    * Setup an endpoint with specified catalogName (which might be null), commandName, optional accessDefinition and
117    * roleName.
118    *
119    * @param endpointName The name for the endpoint node
120    * @param catalogName The catalog name (if null, property won't be set)
121    * @param commandName The command name
122    * @param withAccessDefinition Whether or not to add a node with an access definition
123    * @param roleName The role that should be used in the access definition
124    * @return The endpoint node
125    */
 
126  10 toggle protected Node setupEndpoint(String endpointName, String catalogName, String commandName, boolean withAccessDefinition, String roleName) throws RepositoryException {
127    // The endpoint observation node
128  10 final Node testEndpointNode = NodeUtil.createPath(configSession.getRootNode(), "/modules/yet-another-test/rest-endpoints", NodeTypes.ContentNode.NAME);
129   
130    // The actual endpoint node
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    // The enabled command
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    // Access definition node
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    * Test command endpoint.
157    */
 
158    protected class TestCommandEndpoint extends CommandEndpoint {
159   
 
160  8 toggle @Inject
161    public TestCommandEndpoint(CommandEndpointDefinition commandEndpointDefinition, CommandsManager commandsManager) {
162  8 super(commandEndpointDefinition, commandsManager);
163    }
164   
 
165  8 toggle @Override
166    public boolean isCommandExecutableByCurrentUser(String catalogName, String commandName) {
167  8 return super.isCommandExecutableByCurrentUser(catalogName, commandName);
168    }
169   
 
170  0 toggle public Object getNode(String param) throws RepositoryException {
171  0 return new Object[]{param};
172    }
173    }
174    }