1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (42) |
Complexity: 3 |
Complexity Density: 0.08 |
|
72 |
|
public class SwaggerRestDispatcherServletTest extends MgnlTestCase { |
73 |
|
|
74 |
|
SwaggerRestDispatcherServlet swaggerRestDispatcherServlet; |
75 |
|
ServletContext servletContext; |
76 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
|
77 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
95 |
1 |
@Override... |
96 |
|
public void tearDown() throws Exception { |
97 |
1 |
swaggerRestDispatcherServlet.destroy(); |
98 |
|
} |
99 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (27) |
Complexity: 1 |
Complexity Density: 0.04 |
1PASS
|
|
100 |
1 |
@Test... |
101 |
|
public void handleStaleSwaggerInServletContext() throws Exception { |
102 |
|
|
103 |
|
|
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 |
|
|
112 |
1 |
swaggerRestDispatcherServlet.onEndpointRegistered(event); |
113 |
|
|
114 |
|
|
115 |
1 |
assertNull(servletContext.getAttribute(SWAGGER)); |
116 |
|
|
117 |
|
|
118 |
|
|
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 |
|
|
132 |
1 |
swaggerRestDispatcherServlet.service(request, response); |
133 |
|
|
134 |
|
|
135 |
1 |
assertThat(servletContext.getAttribute(SWAGGER), instanceOf(Swagger.class)); |
136 |
|
|
137 |
|
|
138 |
|
|
139 |
1 |
swaggerRestDispatcherServlet.service(request, response); |
140 |
|
|
141 |
|
|
142 |
1 |
assertThat(servletContext.getAttribute(SWAGGER), instanceOf(Swagger.class)); |
143 |
|
|
144 |
|
|
145 |
|
|
146 |
1 |
endpointDefinition.setEnabled(false); |
147 |
1 |
EndpointDefinitionRegistryEvent unregisterEvent = new EndpointDefinitionRegistryEvent(EndpointDefinitionRegistryEventType.REREGISTERED, endpointDefinition); |
148 |
|
|
149 |
|
|
150 |
1 |
swaggerRestDispatcherServlet.onEndpointReregistered(unregisterEvent); |
151 |
1 |
swaggerRestDispatcherServlet.service(request, response); |
152 |
|
|
153 |
|
|
154 |
1 |
verify(servletContext, times(2)).removeAttribute(SWAGGER); |
155 |
|
} |
156 |
|
} |