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; |
35 |
|
|
36 |
|
import static java.util.stream.Collectors.toList; |
37 |
|
import static org.hamcrest.MatcherAssert.assertThat; |
38 |
|
import static org.hamcrest.Matchers.*; |
39 |
|
import static org.mockito.BDDMockito.given; |
40 |
|
import static org.mockito.Mockito.*; |
41 |
|
|
42 |
|
import info.magnolia.config.registry.DefinitionMetadata; |
43 |
|
import info.magnolia.config.registry.DefinitionMetadataBuilder; |
44 |
|
import info.magnolia.config.registry.DefinitionProvider; |
45 |
|
import info.magnolia.event.EventBus; |
46 |
|
import info.magnolia.rest.registry.ConfiguredEndpointDefinition; |
47 |
|
import info.magnolia.rest.registry.EndpointDefinitionRegistry; |
48 |
|
import info.magnolia.rest.registry.EndpointDefinitionRegistryEvent; |
49 |
|
import info.magnolia.rest.registry.EndpointDefinitionRegistryEventType; |
50 |
|
import info.magnolia.test.ComponentsTestUtil; |
51 |
|
import info.magnolia.test.mock.MockComponentProvider; |
52 |
|
|
53 |
|
import java.util.List; |
54 |
|
import java.util.Map; |
55 |
|
import java.util.Set; |
56 |
|
import java.util.stream.Stream; |
57 |
|
|
58 |
|
import javax.inject.Inject; |
59 |
|
import javax.servlet.ServletConfig; |
60 |
|
import javax.servlet.ServletContext; |
61 |
|
import javax.servlet.ServletException; |
62 |
|
import javax.ws.rs.GET; |
63 |
|
import javax.ws.rs.Path; |
64 |
|
|
65 |
|
import org.jboss.resteasy.core.ResourceInvoker; |
66 |
|
import org.jboss.resteasy.core.ResourceMethodRegistry; |
67 |
|
import org.junit.After; |
68 |
|
import org.junit.Before; |
69 |
|
import org.junit.Test; |
70 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (83) |
Complexity: 9 |
Complexity Density: 0.12 |
|
71 |
|
public class RestDispatcherServletTest { |
72 |
|
|
73 |
|
private RestDispatcherServlet restDispatcherServlet; |
74 |
|
private EndpointDefinition definition1; |
75 |
|
private EndpointDefinition definition3; |
76 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
77 |
4 |
@Before... |
78 |
|
public void setUp() throws Exception { |
79 |
4 |
definition1 = newEndpointDefinition("definition1", TestResource.class); |
80 |
|
|
81 |
4 |
EndpointDefinition definition2 = newEndpointDefinition("definition2", null); |
82 |
4 |
definition3 = newEndpointDefinition("definition3", TestResource.class); |
83 |
|
|
84 |
4 |
List<DefinitionProvider<EndpointDefinition>> providers = Stream.of(definition1, definition2, definition3) |
85 |
|
.map(RestDispatcherServletTest::mockDefinitionProvider) |
86 |
|
.collect(toList()); |
87 |
|
|
88 |
4 |
EndpointDefinitionRegistry registry = mock(EndpointDefinitionRegistry.class); |
89 |
4 |
given(registry.getAllProviders()).willReturn(providers); |
90 |
|
|
91 |
4 |
restDispatcherServlet = new RestDispatcherServlet(mock(RestIntegrationModule.class), registry, mock(EventBus.class), new MockComponentProvider()); |
92 |
|
} |
93 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
94 |
1 |
@Test... |
95 |
|
public void shouldUnregisterExistedEndpointThenReregister() throws ServletException { |
96 |
|
|
97 |
1 |
ServletConfig servletConfig = mock(ServletConfig.class); |
98 |
1 |
ServletContext servletContext = mock(ServletContext.class); |
99 |
1 |
given(servletConfig.getServletContext()).willReturn(servletContext); |
100 |
|
|
101 |
1 |
EndpointDefinition endpointDefinition = newEndpointDefinition("definition", TestResource.class); |
102 |
1 |
List<DefinitionProvider<EndpointDefinition>> providers = Stream.of(endpointDefinition) |
103 |
|
.map(RestDispatcherServletTest::mockDefinitionProvider) |
104 |
|
.collect(toList()); |
105 |
|
|
106 |
1 |
EndpointDefinitionRegistry registry = mock(EndpointDefinitionRegistry.class); |
107 |
1 |
given(registry.getAllProviders()).willReturn(providers); |
108 |
1 |
restDispatcherServlet = new RestDispatcherServlet(mock(RestIntegrationModule.class), registry, mock(EventBus.class), new MockComponentProvider()); |
109 |
|
|
110 |
1 |
restDispatcherServlet.init(servletConfig); |
111 |
1 |
Object initEndpoint = restDispatcherServlet.getApplication().getSingletons().iterator().next(); |
112 |
|
|
113 |
1 |
DefinitionProvider<EndpointDefinition> endpointDefinitionProvider = mockDefinitionProvider(endpointDefinition); |
114 |
1 |
EndpointDefinitionRegistryEvent event = new EndpointDefinitionRegistryEvent(EndpointDefinitionRegistryEventType.REGISTERED, endpointDefinitionProvider); |
115 |
|
|
116 |
|
|
117 |
1 |
restDispatcherServlet.onEndpointRegistered(event); |
118 |
|
|
119 |
|
|
120 |
1 |
Set<Object> endpoints = restDispatcherServlet.getApplication().getSingletons(); |
121 |
1 |
assertThat(endpoints, hasSize(1)); |
122 |
1 |
assertThat(endpoints, not(hasItem(initEndpoint))); |
123 |
|
} |
124 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
125 |
1 |
@Test... |
126 |
|
public void failureUponRegistrationOfOneEndpointDoesNotLeadToSkippingOthers() throws Exception { |
127 |
|
|
128 |
1 |
ServletConfig servletConfig = mock(ServletConfig.class); |
129 |
1 |
ServletContext servletContext = mock(ServletContext.class); |
130 |
1 |
given(servletConfig.getServletContext()).willReturn(servletContext); |
131 |
|
|
132 |
|
|
133 |
1 |
restDispatcherServlet.init(servletConfig); |
134 |
|
|
135 |
|
|
136 |
1 |
List<EndpointDefinition> registeredEndpoints = restDispatcherServlet.getApplication().getSingletons().stream() |
137 |
|
.map(o -> ((TestResource) o).getEndpointDefinition()) |
138 |
|
.collect(toList()); |
139 |
|
|
140 |
1 |
assertThat(registeredEndpoints, hasSize(2)); |
141 |
1 |
assertThat(registeredEndpoints, containsInAnyOrder(definition1, definition3)); |
142 |
|
} |
143 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
144 |
1 |
@Test... |
145 |
|
public void registerMultipleEndpointsWithYamlPath() throws Exception { |
146 |
|
|
147 |
1 |
ServletConfig servletConfig = mock(ServletConfig.class); |
148 |
1 |
ServletContext servletContext = mock(ServletContext.class); |
149 |
1 |
given(servletConfig.getServletContext()).willReturn(servletContext); |
150 |
1 |
restDispatcherServlet.init(servletConfig); |
151 |
|
|
152 |
1 |
EndpointDefinition endpointDefinition1 = newEndpointDefinition("delivery/e1_v1", TestResourceMultipleEndpoint.class); |
153 |
1 |
DefinitionProvider<EndpointDefinition> provider1 = mockDefinitionProvider(endpointDefinition1); |
154 |
|
|
155 |
1 |
EndpointDefinition endpointDefinition2 = newEndpointDefinition("delivery/e2v1", TestResourceMultipleEndpoint.class); |
156 |
1 |
DefinitionProvider<EndpointDefinition> provider2 = mockDefinitionProvider(endpointDefinition2); |
157 |
|
|
158 |
|
|
159 |
1 |
restDispatcherServlet.registerEndpoint(provider1); |
160 |
1 |
restDispatcherServlet.registerEndpoint(provider2); |
161 |
|
|
162 |
|
|
163 |
1 |
List<EndpointDefinition> registeredEndpoints = restDispatcherServlet.getApplication().getSingletons().stream() |
164 |
|
.map(o -> ((TestResource) o).getEndpointDefinition()) |
165 |
|
.collect(toList()); |
166 |
|
|
167 |
1 |
assertThat(registeredEndpoints, hasSize(4)); |
168 |
1 |
assertThat(registeredEndpoints, containsInAnyOrder(definition1, definition3, endpointDefinition1, endpointDefinition2)); |
169 |
|
|
170 |
1 |
ResourceMethodRegistry registry = (ResourceMethodRegistry) restDispatcherServlet.getDispatcher().getRegistry(); |
171 |
1 |
final Map<String, List<ResourceInvoker>> bounded = registry.getBounded(); |
172 |
1 |
assertThat(bounded.keySet(), hasItems("delivery/e1/v1/", "delivery/e2v1/")); |
173 |
|
} |
174 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
175 |
1 |
@Test... |
176 |
|
public void registerMultipleEndpointsWithConfiguredPath() throws Exception { |
177 |
|
|
178 |
1 |
ServletConfig servletConfig = mock(ServletConfig.class); |
179 |
1 |
ServletContext servletContext = mock(ServletContext.class); |
180 |
1 |
given(servletConfig.getServletContext()).willReturn(servletContext); |
181 |
1 |
restDispatcherServlet.init(servletConfig); |
182 |
|
|
183 |
1 |
EndpointDefinition endpointDefinition1 = newEndpointDefinition("delivery/e1_v1", TestResourceMultipleEndpoint.class, "/test/e10/v2"); |
184 |
1 |
DefinitionProvider<EndpointDefinition> provider1 = mockDefinitionProvider(endpointDefinition1); |
185 |
|
|
186 |
|
|
187 |
1 |
restDispatcherServlet.registerEndpoint(provider1); |
188 |
|
|
189 |
|
|
190 |
1 |
List<EndpointDefinition> registeredEndpoints = restDispatcherServlet.getApplication().getSingletons().stream() |
191 |
|
.map(o -> ((TestResource) o).getEndpointDefinition()) |
192 |
|
.collect(toList()); |
193 |
|
|
194 |
1 |
assertThat(registeredEndpoints, hasSize(3)); |
195 |
1 |
assertThat(registeredEndpoints, containsInAnyOrder(definition1, definition3, endpointDefinition1)); |
196 |
|
|
197 |
1 |
ResourceMethodRegistry registry = (ResourceMethodRegistry) restDispatcherServlet.getDispatcher().getRegistry(); |
198 |
1 |
final Map<String, List<ResourceInvoker>> bounded = registry.getBounded(); |
199 |
1 |
assertThat(bounded.keySet(), hasItems("test/e10/v2/")); |
200 |
|
} |
201 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
202 |
16 |
private static EndpointDefinition newEndpointDefinition(String name, Class<?> endpointClass) {... |
203 |
16 |
ConfiguredEndpointDefinition endpointDefinition = new ConfiguredEndpointDefinition(); |
204 |
16 |
endpointDefinition.setName(name); |
205 |
16 |
endpointDefinition.setImplementationClass(endpointClass); |
206 |
16 |
return endpointDefinition; |
207 |
|
} |
208 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
209 |
1 |
private static EndpointDefinition newEndpointDefinition(String name, Class<?> endpointClass, String basePath) {... |
210 |
1 |
EndpointDefinition endpointDefinition = newEndpointDefinition(name, endpointClass); |
211 |
1 |
endpointDefinition = spy(endpointDefinition); |
212 |
1 |
when(endpointDefinition.getEndpointPath()).thenReturn(basePath); |
213 |
1 |
return endpointDefinition; |
214 |
|
} |
215 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
216 |
17 |
private static DefinitionProvider<EndpointDefinition> mockDefinitionProvider(EndpointDefinition endpointDefinition) {... |
217 |
17 |
DefinitionProvider provider = mock(DefinitionProvider.class); |
218 |
17 |
DefinitionMetadata metadata = DefinitionMetadataBuilder.usingNameAsId() |
219 |
|
.name(endpointDefinition.getName()) |
220 |
|
.build(); |
221 |
17 |
when(provider.isValid()).thenReturn(true); |
222 |
17 |
when(provider.getMetadata()).thenReturn(metadata); |
223 |
17 |
when(provider.get()).thenReturn(endpointDefinition); |
224 |
17 |
return provider; |
225 |
|
} |
226 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
227 |
4 |
@After... |
228 |
|
public void tearDown() throws Exception { |
229 |
4 |
ComponentsTestUtil.clear(); |
230 |
|
} |
231 |
|
|
232 |
|
@Path("/") |
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 3 |
Complexity Density: 1 |
|
233 |
|
public class TestResource { |
234 |
|
private final EndpointDefinition endpointDefinition; |
235 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
236 |
11 |
@Inject... |
237 |
|
public TestResource(EndpointDefinition endpointDefinition) { |
238 |
11 |
this.endpointDefinition = endpointDefinition; |
239 |
|
} |
240 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
241 |
0 |
@GET... |
242 |
|
public String get() { |
243 |
0 |
return "test"; |
244 |
|
} |
245 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
246 |
9 |
EndpointDefinition getEndpointDefinition() {... |
247 |
9 |
return endpointDefinition; |
248 |
|
} |
249 |
|
} |
250 |
|
|
251 |
|
@Path("/") |
252 |
|
@DynamicPath |
|
|
| 50% |
Uncovered Elements: 2 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
253 |
|
public class TestResourceMultipleEndpoint extends TestResource { |
254 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
255 |
3 |
@Inject... |
256 |
|
public TestResourceMultipleEndpoint(EndpointDefinition endpointDefinition) { |
257 |
3 |
super(endpointDefinition); |
258 |
|
} |
259 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
260 |
0 |
@GET... |
261 |
|
public String get() { |
262 |
0 |
return "multipleEndpointTest"; |
263 |
|
} |
264 |
|
} |
265 |
|
} |