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

File SwaggerRestDispatcherServletTest.java

 

Code metrics

0
46
3
1
171
92
3
0.07
15.33
3
1

Classes

Class Line # Actions
SwaggerRestDispatcherServletTest 76 46 0% 3 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /**
2    * This file Copyright (c) 2016-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;
35   
36    import static info.magnolia.rest.tools.SwaggerRestDispatcherServlet.SWAGGER;
37    import static org.hamcrest.CoreMatchers.*;
38    import static org.hamcrest.MatcherAssert.assertThat;
39    import static org.junit.Assert.assertNull;
40    import static org.mockito.Matchers.any;
41    import static org.mockito.Mockito.*;
42   
43    import info.magnolia.cms.util.CustomServletConfig;
44    import info.magnolia.config.registry.DefinitionMetadata;
45    import info.magnolia.config.registry.DefinitionProvider;
46    import info.magnolia.context.MgnlContext;
47    import info.magnolia.event.EventBus;
48    import info.magnolia.event.HandlerRegistration;
49    import info.magnolia.objectfactory.Components;
50    import info.magnolia.rest.EndpointDefinition;
51    import info.magnolia.rest.RestIntegrationModule;
52    import info.magnolia.rest.registry.ConfiguredEndpointDefinition;
53    import info.magnolia.rest.registry.EndpointDefinitionRegistry;
54    import info.magnolia.rest.registry.EndpointDefinitionRegistryEvent;
55    import info.magnolia.rest.registry.EndpointDefinitionRegistryEventHandler;
56    import info.magnolia.rest.registry.EndpointDefinitionRegistryEventType;
57    import info.magnolia.test.MgnlTestCase;
58    import info.magnolia.test.mock.MockWebContext;
59   
60    import java.util.HashMap;
61   
62    import javax.servlet.ServletConfig;
63    import javax.servlet.ServletContext;
64    import javax.servlet.http.HttpServletResponse;
65    import javax.ws.rs.HttpMethod;
66   
67    import org.junit.Test;
68   
69    import com.mockrunner.mock.web.MockHttpServletRequest;
70    import com.mockrunner.mock.web.MockHttpServletResponse;
71    import com.mockrunner.mock.web.MockServletContext;
72   
73    import io.swagger.jaxrs.listing.ApiListingResource;
74    import io.swagger.models.Swagger;
75   
 
76    public class SwaggerRestDispatcherServletTest extends MgnlTestCase {
77   
78    SwaggerRestDispatcherServlet swaggerRestDispatcherServlet;
79    ServletContext servletContext;
80   
 
81  1 toggle @Override
82    public void setUp() throws Exception {
83  1 super.setUp();
84   
85  1 RestIntegrationModule restIntegrationModule = mock(RestIntegrationModule.class);
86  1 EndpointDefinitionRegistry endpointDefinitionRegistry = mock(EndpointDefinitionRegistry.class);
87  1 EventBus eventBus = mock(EventBus.class);
88  1 HandlerRegistration handlerRegistration = mock(HandlerRegistration.class);
89  1 when(eventBus.addHandler(eq(EndpointDefinitionRegistryEvent.class), any(EndpointDefinitionRegistryEventHandler.class))).thenReturn(handlerRegistration);
90  1 RestToolsModule restToolsModule = mock(RestToolsModule.class);
91   
92  1 servletContext = spy(new MockServletContext());
93  1 ServletConfig servletConfig = new CustomServletConfig("test servlet", servletContext, new HashMap<String, String>());
94   
95  1 swaggerRestDispatcherServlet = new SwaggerRestDispatcherServlet(restIntegrationModule, endpointDefinitionRegistry, eventBus, restToolsModule, Components.getComponentProvider());
96  1 swaggerRestDispatcherServlet.init(servletConfig);
97    }
98   
 
99  1 toggle @Override
100    public void tearDown() throws Exception {
101  1 swaggerRestDispatcherServlet.destroy();
102    }
103   
 
104  1 toggle @Test
105    public void handleStaleSwaggerInServletContext() throws Exception {
106    // Stage 1: Init and make sure no swagger is in context yet
107    // GIVEN
108  1 final String endpointName = "turnMy$wagUp";
109   
110  1 final ConfiguredEndpointDefinition endpointDefinition = new ConfiguredEndpointDefinition();
111  1 endpointDefinition.setImplementationClass(ApiListingResource.class);
112  1 endpointDefinition.setName(endpointName);
113  1 endpointDefinition.setEnabled(true);
114   
115  1 final DefinitionMetadata endpointDefinitionMetadata = mock(DefinitionMetadata.class);
116  1 when(endpointDefinitionMetadata.getName()).thenReturn(endpointName);
117  1 when(endpointDefinitionMetadata.getReferenceId()).thenReturn(endpointName);
118   
119  1 final DefinitionProvider<EndpointDefinition> definitionDefinitionProvider = mock(DefinitionProvider.class);
120  1 when(definitionDefinitionProvider.get()).thenReturn(endpointDefinition);
121  1 when(definitionDefinitionProvider.getMetadata()).thenReturn(endpointDefinitionMetadata);
122   
123  1 final EndpointDefinitionRegistryEvent event = new EndpointDefinitionRegistryEvent(EndpointDefinitionRegistryEventType.REGISTERED, definitionDefinitionProvider);
124  1 assertThat(event.getEndpointDefinitionProvider(), notNullValue());
125   
126    // WHEN
127  1 swaggerRestDispatcherServlet.onEndpointRegistered(event);
128   
129    // THEN
130  1 assertNull(servletContext.getAttribute(SWAGGER));
131   
132    // Stage 2: Send a request and make sure a swagger ends up in context
133    // GIVEN
134  1 MockWebContext mockWebContext = new MockWebContext();
135  1 mockWebContext.setServletContext(servletContext);
136  1 MgnlContext.setInstance(mockWebContext);
137   
138  1 MockHttpServletRequest request = new MockHttpServletRequest();
139  1 request.setMethod(HttpMethod.GET);
140  1 request.setRequestURI("http://localhost:4321/swagger");
141  1 request.setRequestURL("http://localhost:4321/swagger");
142  1 request.setContextPath("/");
143  1 request.setContentType("text/json");
144  1 HttpServletResponse response = new MockHttpServletResponse();
145   
146    // WHEN
147  1 swaggerRestDispatcherServlet.service(request, response);
148   
149    // THEN
150  1 assertThat(servletContext.getAttribute(SWAGGER), instanceOf(Swagger.class));
151   
152    // Stage 3: Fire another consecutive request and make sure swagger isn't accidentally removed from context
153    // WHEN
154  1 swaggerRestDispatcherServlet.service(request, response);
155   
156    // THEN
157  1 assertThat(servletContext.getAttribute(SWAGGER), instanceOf(Swagger.class));
158   
159    // Stage 4: Re-register endpoint, fire another request and double check the number of context swagger "flushes"
160    // GIVEN
161  1 endpointDefinition.setEnabled(false);
162  1 EndpointDefinitionRegistryEvent unregisterEvent = new EndpointDefinitionRegistryEvent(EndpointDefinitionRegistryEventType.REREGISTERED, definitionDefinitionProvider);
163   
164    // WHEN
165  1 swaggerRestDispatcherServlet.onEndpointReregistered(unregisterEvent);
166  1 swaggerRestDispatcherServlet.service(request, response);
167   
168    // THEN
169  1 verify(servletContext, times(2)).removeAttribute(SWAGGER);
170    }
171    }