Clover icon

Magnolia REST Services 2.0-alpha2

  1. Project Clover database Tue Oct 10 2017 14:50:59 CEST
  2. Package info.magnolia.rest.service.command.v1

File CommandEndpointTest.java

 

Code metrics

2
94
16
1
308
186
17
0.18
5.88
16
1.06

Classes

Class Line # Actions
CommandEndpointTest 79 94 0% 17 34
0.696428669.6%
 

Contributing tests

This file is covered by 8 tests. .

Source view

1    /**
2    * This file Copyright (c) 2013-2017 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.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    * Tests for {@link info.magnolia.rest.service.command.v1.CommandEndpoint}.
78    */
 
79    public class CommandEndpointTest extends CommandEndpointTestCase {
80   
 
81  8 toggle @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>() {
 
89  2 toggle @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() {
 
94  1 toggle @Override
95    public boolean execute(Context context) throws Exception {
96  1 throw new Exception("Exception from mock command");
97    }
98   
 
99  0 toggle @Override
100    public Command clone() throws CloneNotSupportedException {
101  0 return null;
102    }
103    };
104    else
105  1 return new Command() {
 
106  1 toggle @Override
107    public boolean execute(Context context) throws Exception {
108  1 return true;
109    }
110   
 
111  0 toggle @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>() {
 
119  2 toggle @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   
 
127  1 toggle @Test
128    public void testExecuteCommand() throws RepositoryException, Node2BeanException, IllegalAccessException, InstantiationException {
129    // GIVEN
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    // WHEN
136  1 Response response = endpoint.executeCommand("dummy", "dummy", new HashMap<String, Object>());
137   
138    // THEN
139  1 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
140    }
141   
 
142  1 toggle @Test
143    public void testExecuteCommandReturnsInternalServerErrorIfCommandFails() throws RepositoryException, Node2BeanException, IllegalAccessException, InstantiationException {
144    // GIVEN
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    // WHEN
151  1 Response response = endpoint.executeCommand("dummy", "failingCommand", new HashMap<String, Object>());
152   
153    // THEN
154  1 assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
155    }
156   
 
157  1 toggle @Test
158    public void testExecuteCommandReturnsForbiddenIfAccessToCommandIsNotAllowed() throws RepositoryException, Node2BeanException, IllegalAccessException, InstantiationException {
159    // GIVEN
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    // WHEN
166  1 Response response = endpoint.executeCommand("dummy", "dummy", new HashMap<String, Object>());
167   
168    // THEN
169  1 assertEquals(Response.Status.FORBIDDEN.getStatusCode(), response.getStatus());
170    }
171   
 
172  1 toggle @Test
173    public void testConfiguredCommandEndpointDefinitionWithAccessDefinition() throws RepositoryException, Node2BeanException, IllegalAccessException, InstantiationException {
174    // GIVEN
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    // WHEN
184  1 boolean isExecutable = endpoint.isCommandExecutableByCurrentUser("dummy", "dummy");
185   
186    // THEN
187  1 assertTrue("Dummy command should be executable!", isExecutable);
188    }
189   
 
190  1 toggle @Test
191    public void testConfiguredCommandEndpointDefinitionWithoutAccessDefinition() throws RepositoryException, Node2BeanException {
192    // GIVEN
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    // WHEN
202  1 boolean isExecutable = endpoint.isCommandExecutableByCurrentUser("dummy", "dummy");
203   
204    // THEN
205  1 assertFalse("Dummy command should not be executable!", isExecutable);
206    }
207   
 
208  1 toggle @Test
209    public void testConfiguredCommandEndpointDefinitionWithoutCatalogDefinition() throws RepositoryException, Node2BeanException {
210    // GIVEN
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    // WHEN
220  1 boolean isExecutable = endpoint.isCommandExecutableByCurrentUser(null, "dummy");
221   
222    // THEN
223  1 assertTrue("Dummy command should be executable!", isExecutable);
224    }
225   
 
226  1 toggle @Test
227    public void testConfiguredCommandEndpointDefinitionWithoutCatalogDefinitionRequestingACatalog() throws RepositoryException, Node2BeanException {
228    // GIVEN
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    // WHEN
238  1 boolean isExecutable = endpoint.isCommandExecutableByCurrentUser("dummy", "dummy");
239   
240    // THEN
241  1 assertFalse("Dummy command should not be executable!", isExecutable);
242    }
243   
 
244  1 toggle @Test
245    public void testConfiguredCommandEndpointDefinitionWithoutCommandName() throws RepositoryException, Node2BeanException {
246    // GIVEN
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    // WHEN
256  1 boolean isExecutable = endpoint.isCommandExecutableByCurrentUser(null, null);
257   
258    // THEN
259  1 assertFalse("Dummy command should not be executable!", isExecutable);
260    }
261   
 
262  0 toggle @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    // GIVEN
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    // The enabled command
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    // Access definition node
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    // WHEN
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    // THEN
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    }