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.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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (49) |
Complexity: 3 |
Complexity Density: 0.07 |
|
76 |
|
public class SwaggerRestDispatcherServletTest extends MgnlTestCase { |
77 |
|
|
78 |
|
SwaggerRestDispatcherServlet swaggerRestDispatcherServlet; |
79 |
|
ServletContext servletContext; |
80 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
|
81 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
99 |
1 |
@Override... |
100 |
|
public void tearDown() throws Exception { |
101 |
1 |
swaggerRestDispatcherServlet.destroy(); |
102 |
|
} |
103 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (34) |
Complexity: 1 |
Complexity Density: 0.03 |
1PASS
|
|
104 |
1 |
@Test... |
105 |
|
public void handleStaleSwaggerInServletContext() throws Exception { |
106 |
|
|
107 |
|
|
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 |
|
|
127 |
1 |
swaggerRestDispatcherServlet.onEndpointRegistered(event); |
128 |
|
|
129 |
|
|
130 |
1 |
assertNull(servletContext.getAttribute(SWAGGER)); |
131 |
|
|
132 |
|
|
133 |
|
|
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 |
|
|
147 |
1 |
swaggerRestDispatcherServlet.service(request, response); |
148 |
|
|
149 |
|
|
150 |
1 |
assertThat(servletContext.getAttribute(SWAGGER), instanceOf(Swagger.class)); |
151 |
|
|
152 |
|
|
153 |
|
|
154 |
1 |
swaggerRestDispatcherServlet.service(request, response); |
155 |
|
|
156 |
|
|
157 |
1 |
assertThat(servletContext.getAttribute(SWAGGER), instanceOf(Swagger.class)); |
158 |
|
|
159 |
|
|
160 |
|
|
161 |
1 |
endpointDefinition.setEnabled(false); |
162 |
1 |
EndpointDefinitionRegistryEvent unregisterEvent = new EndpointDefinitionRegistryEvent(EndpointDefinitionRegistryEventType.REREGISTERED, definitionDefinitionProvider); |
163 |
|
|
164 |
|
|
165 |
1 |
swaggerRestDispatcherServlet.onEndpointReregistered(unregisterEvent); |
166 |
1 |
swaggerRestDispatcherServlet.service(request, response); |
167 |
|
|
168 |
|
|
169 |
1 |
verify(servletContext, times(2)).removeAttribute(SWAGGER); |
170 |
|
} |
171 |
|
} |