Clover icon

Magnolia REST Tools 1.2.2

  1. Project Clover database Thu Jun 29 2017 13:59:54 CEST
  2. Package info.magnolia.rest.tools

File SwaggerRestDispatcherServletTest.java

 

Code metrics

0
39
3
1
156
81
3
0.08
13
3
1

Classes

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