Clover icon

Magnolia REST Tools 2.0-rc1

  1. Project Clover database Mon Oct 30 2017 16:31:56 CET
  2. Package info.magnolia.rest.tools.setup

File RestToolsModuleVersionHandlerTest.java

 

Code metrics

0
35
6
1
158
88
6
0.17
5.83
6
1

Classes

Class Line # Actions
RestToolsModuleVersionHandlerTest 70 35 0% 6 0
1.0100%
 

Contributing tests

This file is covered by 2 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.tools.setup;
35   
36    import static info.magnolia.test.hamcrest.NodeMatchers.*;
37    import static org.hamcrest.CoreMatchers.not;
38    import static org.junit.Assert.*;
39   
40    import info.magnolia.cms.security.MgnlRoleManager;
41    import info.magnolia.cms.security.Permission;
42    import info.magnolia.cms.security.SecuritySupport;
43    import info.magnolia.cms.security.SecuritySupportImpl;
44    import info.magnolia.context.MgnlContext;
45    import info.magnolia.jcr.util.NodeTypes;
46    import info.magnolia.jcr.util.NodeUtil;
47    import info.magnolia.module.ModuleVersionHandler;
48    import info.magnolia.module.ModuleVersionHandlerTestCase;
49    import info.magnolia.module.model.Version;
50    import info.magnolia.repository.RepositoryConstants;
51    import info.magnolia.rest.RestDispatcherServlet;
52    import info.magnolia.rest.tools.SwaggerRestDispatcherServlet;
53    import info.magnolia.test.ComponentsTestUtil;
54   
55    import java.util.Arrays;
56    import java.util.List;
57   
58    import javax.jcr.Node;
59    import javax.jcr.Session;
60   
61    import org.junit.Before;
62    import org.junit.Test;
63   
64    import io.swagger.jaxrs.listing.ApiListingResource;
65    import io.swagger.jaxrs.listing.SwaggerSerializers;
66   
67    /**
68    * Tests for {@link RestToolsModuleVersionHandler}.
69    */
 
70    public class RestToolsModuleVersionHandlerTest extends ModuleVersionHandlerTestCase {
71   
72    private static final String REST_DISPATCHER_SERVLET_PATH = "/server/filters/servlets/RestDispatcherServlet";
73    private static final String REST_INTEGRATION_MODULE_CONFIG_PATH = "/modules/rest-integration/config";
74   
75    private Session configSession;
76    private Session rolesSession;
77   
 
78  2 toggle @Override
79    protected String getModuleDescriptorPath() {
80  2 return "/META-INF/magnolia/rest-tools.xml";
81    }
82   
 
83  2 toggle @Override
84    protected List<String> getModuleDescriptorPathsForTests() {
85  2 return Arrays.asList(
86    "/META-INF/magnolia/core.xml"
87    );
88    }
89   
 
90  2 toggle @Override
91    protected ModuleVersionHandler newModuleVersionHandlerForTests() {
92  2 return new RestToolsModuleVersionHandler();
93    }
94   
 
95  2 toggle @Override
96    @Before
97    public void setUp() throws Exception {
98  2 super.setUp();
99   
100  2 configSession = MgnlContext.getJCRSession(RepositoryConstants.CONFIG);
101  2 rolesSession = MgnlContext.getJCRSession(RepositoryConstants.USER_ROLES);
102   
103    // for AddPermissionTask
104  2 final SecuritySupportImpl securitySupport = new SecuritySupportImpl();
105  2 securitySupport.setRoleManager(new MgnlRoleManager());
106  2 ComponentsTestUtil.setInstance(SecuritySupport.class, securitySupport);
107    }
108   
 
109  1 toggle @Test
110    public void basicInstall() throws Exception {
111    // GIVEN
112  1 Session session = MgnlContext.getJCRSession(RepositoryConstants.CONFIG);
113   
114  1 Node node = NodeUtil.createPath(session.getRootNode(), REST_DISPATCHER_SERVLET_PATH, NodeTypes.ContentNode.NAME);
115  1 node.setProperty("servletClass", RestDispatcherServlet.class.getName());
116   
117  1 NodeUtil.createPath(rolesSession.getRootNode(), "/rest-editor", NodeTypes.Role.NAME);
118   
119  1 NodeUtil.createPath(session.getRootNode(), REST_INTEGRATION_MODULE_CONFIG_PATH, NodeTypes.ContentNode.NAME);
120  1 NodeUtil.createPath(session.getRootNode(), REST_INTEGRATION_MODULE_CONFIG_PATH + "/additionalProviders", NodeTypes.ContentNode.NAME);
121   
122    // WHEN
123  1 executeUpdatesAsIfTheCurrentlyInstalledVersionWas(null);
124   
125    // THEN
126  1 assertThat(session.getNode(REST_DISPATCHER_SERVLET_PATH), hasProperty("servletClass", SwaggerRestDispatcherServlet.class.getName()));
127  1 assertEquals(1, rolesSession.getNode("/rest-editor/acl_uri").getNodes().getSize());
128  1 assertThat(session.getNode(REST_INTEGRATION_MODULE_CONFIG_PATH + "/additionalProviders/apiListingResource"), hasProperty("providerClass", ApiListingResource.class.getName()));
129  1 assertThat(session.getNode(REST_INTEGRATION_MODULE_CONFIG_PATH + "/additionalProviders/swaggerSerializers"), hasProperty("providerClass", SwaggerSerializers.class.getName()));
130    }
131   
 
132  1 toggle @Test
133    public void updateFrom1_1_2() throws Exception {
134    // GIVEN
135  1 setupConfigProperty("/modules/rest-integration/config/additionalProviders/apiDeclarationProvider", "providerClass", "com.whatever.it.was.Before");
136  1 setupConfigProperty("/modules/rest-integration/config/additionalProviders/resourceListingProvider", "providerClass", "com.whatever.it.was.Before");
137  1 setupConfigProperty("/modules/rest-tools/config", "apiVersion", "1.0.0");
138   
139  1 setupConfigProperty("/modules/rest-tools/apps/restTools/subApps/apiDocs", "url", "/.resources/swagger-ui/index.html");
140   
141  1 Node restRoleNode = NodeUtil.createPath(rolesSession.getRootNode(), "/rest", NodeTypes.Role.NAME);
142  1 Node legacyAcl = NodeUtil.createPath(restRoleNode, "acl_uri/02", NodeTypes.ContentNode.NAME);
143  1 legacyAcl.setProperty("path", "/.rest/api-docs*");
144  1 legacyAcl.setProperty("permissions", Permission.ALL);
145   
146    // WHEN
147  1 executeUpdatesAsIfTheCurrentlyInstalledVersionWas(Version.parseVersion("1.1.2"));
148   
149    // THEN
150  1 assertThat(configSession.getNode("/modules/rest-integration/config/additionalProviders/apiListingResource"), hasProperty("providerClass", ApiListingResource.class.getName()));
151  1 assertThat(configSession.getNode("/modules/rest-integration/config/additionalProviders/swaggerSerializers"), hasProperty("providerClass", SwaggerSerializers.class.getName()));
152  1 assertThat(configSession.getNode("/modules/rest-tools/config"), hasProperty("apiVersion", "1.5.4"));
153   
154  1 assertThat(rolesSession.getNode("/rest/acl_uri"), hasNode(hasProperty("path", "/.rest/swagger*")));
155  1 assertThat(rolesSession.getNode("/rest/acl_uri"), not(hasNode(hasProperty("path", "/.rest/api-docs*"))));
156  1 assertThat(configSession.getNode("/modules/rest-tools/apps/restTools/subApps/apiDocs"), hasProperty("url", "/.resources/rest-tools/webresources/swagger-ui/index.html"));
157    }
158    }