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