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.Set; |
55 |
|
import java.util.stream.Stream; |
56 |
|
|
57 |
|
import javax.inject.Inject; |
58 |
|
import javax.servlet.ServletConfig; |
59 |
|
import javax.servlet.ServletContext; |
60 |
|
import javax.servlet.ServletException; |
61 |
|
import javax.ws.rs.GET; |
62 |
|
import javax.ws.rs.Path; |
63 |
|
|
64 |
|
import org.junit.After; |
65 |
|
import org.junit.Before; |
66 |
|
import org.junit.Test; |
67 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (47) |
Complexity: 6 |
Complexity Density: 0.15 |
|
68 |
|
public class RestDispatcherServletTest { |
69 |
|
|
70 |
|
private RestDispatcherServlet restDispatcherServlet; |
71 |
|
private EndpointDefinition definition1; |
72 |
|
private EndpointDefinition definition3; |
73 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
74 |
2 |
@Before... |
75 |
|
public void setUp() throws Exception { |
76 |
2 |
definition1 = newEndpointDefinition("definition1", TestResource.class); |
77 |
|
|
78 |
2 |
EndpointDefinition definition2 = newEndpointDefinition("definition2", null); |
79 |
2 |
definition3 = newEndpointDefinition("definition3", TestResource.class); |
80 |
|
|
81 |
2 |
List<DefinitionProvider<EndpointDefinition>> providers = Stream.of(definition1, definition2, definition3) |
82 |
|
.map(RestDispatcherServletTest::mockDefinitionProvider) |
83 |
|
.collect(toList()); |
84 |
|
|
85 |
2 |
EndpointDefinitionRegistry registry = mock(EndpointDefinitionRegistry.class); |
86 |
2 |
given(registry.getAllProviders()).willReturn(providers); |
87 |
|
|
88 |
2 |
restDispatcherServlet = new RestDispatcherServlet(mock(RestIntegrationModule.class), registry, mock(EventBus.class), new MockComponentProvider()); |
89 |
|
} |
90 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
91 |
1 |
@Test... |
92 |
|
public void shouldUnregisterExistedEndpointThenReregister() throws ServletException { |
93 |
|
|
94 |
1 |
ServletConfig servletConfig = mock(ServletConfig.class); |
95 |
1 |
ServletContext servletContext = mock(ServletContext.class); |
96 |
1 |
given(servletConfig.getServletContext()).willReturn(servletContext); |
97 |
|
|
98 |
1 |
EndpointDefinition endpointDefinition = newEndpointDefinition("definition", TestResource.class); |
99 |
1 |
List<DefinitionProvider<EndpointDefinition>> providers = Stream.of(endpointDefinition) |
100 |
|
.map(RestDispatcherServletTest::mockDefinitionProvider) |
101 |
|
.collect(toList()); |
102 |
|
|
103 |
1 |
EndpointDefinitionRegistry registry = mock(EndpointDefinitionRegistry.class); |
104 |
1 |
given(registry.getAllProviders()).willReturn(providers); |
105 |
1 |
restDispatcherServlet = new RestDispatcherServlet(mock(RestIntegrationModule.class), registry, mock(EventBus.class), new MockComponentProvider()); |
106 |
|
|
107 |
1 |
restDispatcherServlet.init(servletConfig); |
108 |
1 |
Object initEndpoint = restDispatcherServlet.getApplication().getSingletons().iterator().next(); |
109 |
|
|
110 |
1 |
DefinitionProvider<EndpointDefinition> endpointDefinitionProvider = mockDefinitionProvider(endpointDefinition); |
111 |
1 |
EndpointDefinitionRegistryEvent event = new EndpointDefinitionRegistryEvent(EndpointDefinitionRegistryEventType.REGISTERED, endpointDefinitionProvider); |
112 |
|
|
113 |
|
|
114 |
1 |
restDispatcherServlet.onEndpointRegistered(event); |
115 |
|
|
116 |
|
|
117 |
1 |
Set<Object> endpoints = restDispatcherServlet.getApplication().getSingletons(); |
118 |
1 |
assertThat(endpoints, hasSize(1)); |
119 |
1 |
assertThat(endpoints, not(hasItem(initEndpoint))); |
120 |
|
} |
121 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
122 |
1 |
@Test... |
123 |
|
public void failureUponRegistrationOfOneEndpointDoesNotLeadToSkippingOthers() throws Exception { |
124 |
|
|
125 |
1 |
ServletConfig servletConfig = mock(ServletConfig.class); |
126 |
1 |
ServletContext servletContext = mock(ServletContext.class); |
127 |
1 |
given(servletConfig.getServletContext()).willReturn(servletContext); |
128 |
|
|
129 |
|
|
130 |
1 |
restDispatcherServlet.init(servletConfig); |
131 |
|
|
132 |
|
|
133 |
1 |
List<EndpointDefinition> registeredEndpoints = restDispatcherServlet.getApplication().getSingletons().stream() |
134 |
|
.map(o -> ((TestResource) o).getEndpointDefinition()) |
135 |
|
.collect(toList()); |
136 |
|
|
137 |
1 |
assertThat(registeredEndpoints, hasSize(2)); |
138 |
1 |
assertThat(registeredEndpoints, containsInAnyOrder(definition1, definition3)); |
139 |
|
} |
140 |
|
|
141 |
|
@Path("/") |
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 3 |
Complexity Density: 1 |
|
142 |
|
public class TestResource { |
143 |
|
private final EndpointDefinition endpointDefinition; |
144 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
145 |
4 |
@Inject... |
146 |
|
public TestResource(EndpointDefinition endpointDefinition) { |
147 |
4 |
this.endpointDefinition = endpointDefinition; |
148 |
|
} |
149 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
150 |
2 |
EndpointDefinition getEndpointDefinition() {... |
151 |
2 |
return endpointDefinition; |
152 |
|
} |
153 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
154 |
0 |
@GET... |
155 |
|
public String get() { |
156 |
0 |
return "test"; |
157 |
|
} |
158 |
|
|
159 |
|
} |
160 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
161 |
2 |
@After... |
162 |
|
public void tearDown() throws Exception { |
163 |
2 |
ComponentsTestUtil.clear(); |
164 |
|
} |
165 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
166 |
7 |
private static EndpointDefinition newEndpointDefinition(String name, Class<?> endpointClass) {... |
167 |
7 |
ConfiguredEndpointDefinition endpointDefinition = new ConfiguredEndpointDefinition(); |
168 |
7 |
endpointDefinition.setName(name); |
169 |
7 |
endpointDefinition.setImplementationClass(endpointClass); |
170 |
7 |
return endpointDefinition; |
171 |
|
} |
172 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
173 |
8 |
private static DefinitionProvider<EndpointDefinition> mockDefinitionProvider(EndpointDefinition endpointDefinition) {... |
174 |
8 |
DefinitionProvider provider = mock(DefinitionProvider.class); |
175 |
8 |
DefinitionMetadata metadata = DefinitionMetadataBuilder.usingNameAsId() |
176 |
|
.name(endpointDefinition.getName()) |
177 |
|
.build(); |
178 |
8 |
when(provider.isValid()).thenReturn(true); |
179 |
8 |
when(provider.getMetadata()).thenReturn(metadata); |
180 |
8 |
when(provider.get()).thenReturn(endpointDefinition); |
181 |
8 |
return provider; |
182 |
|
} |
183 |
|
} |