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.v2

File CommandEndpointTest.java

 

Code metrics

0
29
4
1
122
63
4
0.14
7.25
4
1

Classes

Class Line # Actions
CommandEndpointTest 61 29 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    /**
2    * This file Copyright (c) 2015-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.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    * Tests for {@link CommandEndpoint} v2.
60    */
 
61    public class CommandEndpointTest extends CommandEndpointTestCase {
62   
 
63  1 toggle @Test
64    public void testCommandEndpointReturnsContextAttributesInResponse() throws Exception {
65    // GIVEN
66  1 when(commandsManager.getCommand("testCatalog", "testCommand")).thenReturn(new TestCommand());
67  1 when(commandsManager.executeCommand(any(TestCommand.class), anyMap())).thenAnswer(new Answer<Boolean>() {
 
68  1 toggle @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    // WHEN
83  1 Response response = endpoint.executeCommand("testCatalog", "testCommand", new HashMap<String, Object>());
84   
85    // THEN
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   
 
93  1 toggle @Test
94    public void testCommandEndpointReturnsExceptionMessageAndStackTrace() throws Exception {
95    // GIVEN
96  1 when(commandsManager.getCommand("testCatalog", "testCommand")).thenReturn(new TestCommand());
97  1 when(commandsManager.executeCommand(any(TestCommand.class), anyMap())).thenAnswer(new Answer<Boolean>() {
 
98  1 toggle @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    // WHEN
112  1 Response response = endpoint.executeCommand("testCatalog", "testCommand", new HashMap<String, Object>());
113   
114    // THEN
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    }