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.registry; |
35 |
|
|
36 |
|
import static info.magnolia.config.registry.DefinitionProvider.Problem.SeverityType.MAJOR; |
37 |
|
import static info.magnolia.rest.registry.EndpointDefinitionRegistryEventType.*; |
38 |
|
import static info.magnolia.test.hamcrest.ExceptionMatcher.instanceOf; |
39 |
|
import static info.magnolia.test.hamcrest.ExecutionMatcher.throwsAnException; |
40 |
|
import static java.util.Collections.*; |
41 |
|
import static org.hamcrest.CoreMatchers.equalTo; |
42 |
|
import static org.hamcrest.Matchers.contains; |
43 |
|
import static org.hamcrest.Matchers.*; |
44 |
|
import static org.junit.Assert.*; |
45 |
|
import static org.mockito.Matchers.any; |
46 |
|
import static org.mockito.Mockito.*; |
47 |
|
|
48 |
|
import info.magnolia.config.registry.DefinitionMetadata; |
49 |
|
import info.magnolia.config.registry.DefinitionMetadataBuilder; |
50 |
|
import info.magnolia.config.registry.DefinitionProvider; |
51 |
|
import info.magnolia.config.registry.Registry; |
52 |
|
import info.magnolia.config.registry.decoration.DefinitionDecorator; |
53 |
|
import info.magnolia.event.RecordingEventBus; |
54 |
|
import info.magnolia.module.ModuleRegistry; |
55 |
|
import info.magnolia.registry.RegistrationException; |
56 |
|
import info.magnolia.rest.EndpointDefinition; |
57 |
|
|
58 |
|
import java.util.ArrayList; |
59 |
|
import java.util.Arrays; |
60 |
|
import java.util.Collections; |
61 |
|
import java.util.List; |
62 |
|
import java.util.Set; |
63 |
|
import java.util.stream.Collectors; |
64 |
|
|
65 |
|
import org.junit.Before; |
66 |
|
import org.junit.Test; |
67 |
|
|
|
|
| 97.8% |
Uncovered Elements: 3 (138) |
Complexity: 16 |
Complexity Density: 0.14 |
|
68 |
|
public class EndpointDefinitionRegistryTest { |
69 |
|
private EndpointDefinitionRegistry registry; |
70 |
|
private RecordingEventBus eventBus; |
71 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
|
72 |
13 |
@Before... |
73 |
|
public void setUp() throws Exception { |
74 |
13 |
ModuleRegistry moduleRegistry = mock(ModuleRegistry.class); |
75 |
13 |
eventBus = new RecordingEventBus(); |
76 |
13 |
registry = new EndpointDefinitionRegistry(moduleRegistry, eventBus); |
77 |
|
|
78 |
13 |
EndpointDefinition endpoint1 = newEndpointDefinition("endpoint1", true, Integer.class); |
79 |
13 |
EndpointDefinition endpoint2 = newEndpointDefinition("endpoint2", true, Double.class); |
80 |
13 |
EndpointDefinition empty = mock(EndpointDefinition.class); |
81 |
13 |
registry.register(mockDefinitionProvider("endpoint1", endpoint1)); |
82 |
13 |
registry.register(mockDefinitionProvider("endpoint2", endpoint2)); |
83 |
13 |
DefinitionProvider<EndpointDefinition> invalidProvider = mockDefinitionProvider("empty", empty); |
84 |
13 |
when(invalidProvider.isValid()).thenReturn(false); |
85 |
13 |
registry.register(invalidProvider); |
86 |
|
} |
87 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
1PASS
|
|
88 |
|
@Test... |
89 |
|
public void getAllDefinitions() throws Exception { |
90 |
|
|
91 |
|
int size = registry.getAllDefinitions().size(); |
92 |
|
|
93 |
|
|
94 |
|
assertEquals(2, size); |
95 |
|
} |
96 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
1PASS
|
|
97 |
|
@Test... |
98 |
|
public void getProvider() throws Exception { |
99 |
|
|
100 |
|
DefinitionProvider<EndpointDefinition> provider = registry.getProvider("endpoint1"); |
101 |
|
|
102 |
|
|
103 |
|
assertTrue(provider.isValid()); |
104 |
|
assertThat(provider.get().getName(), equalTo("endpoint1")); |
105 |
|
} |
106 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
1PASS
|
|
107 |
|
@Test... |
108 |
|
public void getNonExistingProvider() throws Exception { |
109 |
|
assertThat(() -> { |
110 |
|
registry.getProvider("endpoint99"); |
111 |
|
}, throwsAnException(instanceOf(Registry.NoSuchDefinitionException.class))); |
112 |
|
} |
113 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
1PASS
|
|
114 |
|
@Test... |
115 |
|
public void getInvalidProvider() throws Exception { |
116 |
|
DefinitionProvider<EndpointDefinition> provider = registry.getProvider("empty"); |
117 |
|
assertFalse(provider.isValid()); |
118 |
|
} |
119 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
1PASS
|
|
120 |
|
@Test... |
121 |
|
@Deprecated |
122 |
|
public void getExistingWithDeprecatedApi() throws Exception { |
123 |
|
|
124 |
|
EndpointDefinition res = registry.getEndpointDefinition("endpoint1"); |
125 |
|
|
126 |
|
|
127 |
|
assertNotNull(res); |
128 |
|
assertThat(res.getName(), equalTo("endpoint1")); |
129 |
|
} |
130 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
1PASS
|
|
131 |
|
@Test... |
132 |
|
@Deprecated |
133 |
|
public void getNonExistingWithDeprecatedApi() throws Exception { |
134 |
|
assertThat(() -> { |
135 |
|
registry.getEndpointDefinition("endpoint99"); |
136 |
|
}, throwsAnException(instanceOf(RegistrationException.class))); |
137 |
|
} |
138 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
139 |
1 |
@Test... |
140 |
|
public void unregisterAndRegisterWhenEmpty() throws Exception { |
141 |
|
|
142 |
1 |
registry.unregisterAndRegister(registry.getAllMetadata(), emptyList()); |
143 |
1 |
DefinitionProvider<EndpointDefinition> provider = mockDefinitionProvider("endpoint3", newEndpointDefinition("endpoint3", true)); |
144 |
1 |
DefinitionMetadata metadata3 = provider.getMetadata(); |
145 |
1 |
eventBus.getSentEvents().clear(); |
146 |
|
|
147 |
|
|
148 |
1 |
Set<DefinitionMetadata> registeredIds = registry.unregisterAndRegister(emptyList(), singletonList(provider)); |
149 |
|
|
150 |
|
|
151 |
1 |
assertThat(registeredIds, contains(metadata3)); |
152 |
1 |
assertEquals(1, eventBus.getSentEvents().size()); |
153 |
1 |
EndpointDefinitionRegistryEvent event = (EndpointDefinitionRegistryEvent) eventBus.getEvent(); |
154 |
1 |
assertEquals(REGISTERED, event.getType()); |
155 |
1 |
assertEquals("endpoint3", event.getEndpointName()); |
156 |
1 |
assertEquals(provider, event.getEndpointDefinitionProvider()); |
157 |
|
} |
158 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
159 |
1 |
@Test... |
160 |
|
public void register() throws Exception { |
161 |
|
|
162 |
1 |
DefinitionProvider<EndpointDefinition> provider = mockDefinitionProvider("endpoint4", newEndpointDefinition("endpoint4", true)); |
163 |
1 |
eventBus.getSentEvents().clear(); |
164 |
|
|
165 |
|
|
166 |
1 |
registry.register(provider); |
167 |
|
|
168 |
|
|
169 |
1 |
assertTrue(registry.getProvider("endpoint4").isValid()); |
170 |
1 |
assertEquals(1, eventBus.getSentEvents().size()); |
171 |
1 |
EndpointDefinitionRegistryEvent event = (EndpointDefinitionRegistryEvent) eventBus.getEvent(); |
172 |
1 |
assertEquals(REGISTERED, event.getType()); |
173 |
1 |
assertEquals("endpoint4", event.getEndpointName()); |
174 |
1 |
assertEquals(provider, event.getEndpointDefinitionProvider()); |
175 |
|
} |
176 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
177 |
1 |
@Test... |
178 |
|
public void registeringSameNameEndpointJustTakesTheFirstEndpoint() throws Exception { |
179 |
|
|
180 |
1 |
eventBus.getSentEvents().clear(); |
181 |
1 |
registry = new EndpointDefinitionRegistry(mock(ModuleRegistry.class), eventBus); |
182 |
1 |
DefinitionProvider<EndpointDefinition> provider = mockDefinitionProvider("endpoint", newEndpointDefinition("endpoint", true), "moduleA"); |
183 |
1 |
registry.register(provider); |
184 |
|
|
185 |
|
|
186 |
1 |
DefinitionProvider<EndpointDefinition> sameNameProvider = mockDefinitionProvider("endpoint", newEndpointDefinition("endpoint", true), "moduleB"); |
187 |
1 |
registry.register(sameNameProvider); |
188 |
|
|
189 |
|
|
190 |
1 |
assertTrue(registry.getProvider("endpoint").isValid()); |
191 |
1 |
EndpointDefinitionRegistryEvent event = (EndpointDefinitionRegistryEvent) eventBus.getEvent(); |
192 |
1 |
assertEquals("moduleA", event.getEndpointDefinitionProvider().getMetadata().getModule()); |
193 |
|
} |
194 |
|
|
|
|
| 92.3% |
Uncovered Elements: 2 (26) |
Complexity: 3 |
Complexity Density: 0.14 |
1PASS
|
|
195 |
1 |
@Test... |
196 |
|
public void firedEventContainsDecoratedProvider() throws Exception { |
197 |
|
|
198 |
1 |
eventBus.getSentEvents().clear(); |
199 |
1 |
DefinitionDecorator<EndpointDefinition> decorator = mock(DefinitionDecorator.class); |
200 |
1 |
final DefinitionProvider[] providers = new DefinitionProvider[] { null }; |
201 |
1 |
doAnswer(invocation -> { |
202 |
10 |
DefinitionProvider provider = invocation.getArgumentAt(0, DefinitionProvider.class); |
203 |
10 |
boolean applicable = provider != null && provider.getMetadata() != null |
204 |
|
&& "endpoint1".equals(provider.getMetadata().getReferenceId()); |
205 |
10 |
if (applicable) { |
206 |
4 |
providers[0] = provider; |
207 |
|
} |
208 |
10 |
return applicable; |
209 |
|
}).when(decorator).appliesTo(any(DefinitionProvider.class)); |
210 |
|
|
211 |
1 |
doAnswer(invocation -> { |
212 |
2 |
DefinitionProvider provider = invocation.getArgumentAt(0, DefinitionProvider.class); |
213 |
2 |
if (provider == providers[0]) { |
214 |
2 |
when(provider.getDecorators()).thenReturn(Arrays.asList(mock(DefinitionDecorator.class))); |
215 |
2 |
return provider; |
216 |
|
} |
217 |
0 |
return null; |
218 |
|
}).when(decorator).decorate(any(DefinitionProvider.class)); |
219 |
|
|
220 |
1 |
registry.addDecorator(decorator); |
221 |
|
|
222 |
|
|
223 |
1 |
eventBus.getSentEvents().clear(); |
224 |
1 |
EndpointDefinition updatedEndpoint = newEndpointDefinition("endpoint1", false, Integer.class); |
225 |
1 |
DefinitionProvider provider = mockDefinitionProvider("endpoint1", updatedEndpoint); |
226 |
1 |
registry.register(provider); |
227 |
|
|
228 |
|
|
229 |
1 |
EndpointDefinitionRegistryEvent event = (EndpointDefinitionRegistryEvent) eventBus.getEvent(); |
230 |
1 |
assertFalse(event.getEndpointDefinitionProvider().getDecorators().isEmpty()); |
231 |
|
} |
232 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
233 |
1 |
@Test... |
234 |
|
public void unregister() throws Exception { |
235 |
|
|
236 |
1 |
DefinitionProvider<EndpointDefinition> providerToRemove = registry.getProvider("endpoint2"); |
237 |
1 |
eventBus.getSentEvents().clear(); |
238 |
|
|
239 |
|
|
240 |
1 |
registry.unregisterAndRegister(singletonList(providerToRemove.getMetadata()), Collections.emptyList()); |
241 |
|
|
242 |
|
|
243 |
1 |
assertThat(() -> { |
244 |
1 |
registry.getProvider("endpoint2"); |
245 |
|
}, throwsAnException(instanceOf(Registry.NoSuchDefinitionException.class))); |
246 |
1 |
assertEquals(1, eventBus.getSentEvents().size()); |
247 |
1 |
EndpointDefinitionRegistryEvent event = (EndpointDefinitionRegistryEvent) eventBus.getEvent(); |
248 |
1 |
assertEquals(UNREGISTERED, event.getType()); |
249 |
1 |
assertEquals("endpoint2", event.getEndpointName()); |
250 |
|
} |
251 |
|
|
|
|
| 95.5% |
Uncovered Elements: 1 (22) |
Complexity: 3 |
Complexity Density: 0.17 |
1PASS
|
|
252 |
1 |
@Test... |
253 |
|
public void decoratorsTriggerRegistryEvents() throws Exception { |
254 |
|
|
255 |
1 |
eventBus.getSentEvents().clear(); |
256 |
1 |
DefinitionDecorator<EndpointDefinition> decorator = mock(DefinitionDecorator.class); |
257 |
1 |
final DefinitionProvider[] providers = new DefinitionProvider[]{null}; |
258 |
1 |
doAnswer(invocation -> { |
259 |
6 |
DefinitionProvider provider = invocation.getArgumentAt(0, DefinitionProvider.class); |
260 |
6 |
boolean applicable = provider != null && provider.getMetadata() != null |
261 |
|
&& "endpoint1".equals(provider.getMetadata().getReferenceId()); |
262 |
6 |
if (applicable) { |
263 |
2 |
providers[0] = provider; |
264 |
|
} |
265 |
6 |
return applicable; |
266 |
|
}).when(decorator).appliesTo(any(DefinitionProvider.class)); |
267 |
|
|
268 |
1 |
doAnswer(invocation -> { |
269 |
1 |
DefinitionProvider provider = invocation.getArgumentAt(0, DefinitionProvider.class); |
270 |
1 |
return provider == providers[0] ? provider : null; |
271 |
|
}).when(decorator).decorate(any(DefinitionProvider.class)); |
272 |
|
|
273 |
|
|
274 |
1 |
registry.addDecorator(decorator); |
275 |
|
|
276 |
|
|
277 |
1 |
assertThat(eventBus.getSentEvents(), hasSize(1)); |
278 |
1 |
EndpointDefinitionRegistryEvent event = (EndpointDefinitionRegistryEvent) eventBus.getEvent(); |
279 |
1 |
assertThat(event.getType(), equalTo(REREGISTERED)); |
280 |
1 |
assertThat(event.getEndpointName(), equalTo("endpoint1")); |
281 |
1 |
verify(decorator).decorate(providers[0]); |
282 |
|
} |
283 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
284 |
4 |
private static EndpointDefinition newEndpointDefinition(String name, boolean enabled) {... |
285 |
4 |
ConfiguredEndpointDefinition endpointDefinition = new ConfiguredEndpointDefinition(); |
286 |
4 |
endpointDefinition.setName(name); |
287 |
4 |
endpointDefinition.setEnabled(enabled); |
288 |
4 |
return endpointDefinition; |
289 |
|
} |
290 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
291 |
1 |
@Test... |
292 |
|
public void registerAndConflictedEndpointShouldRaiseMajorProblem() throws Exception { |
293 |
|
|
294 |
1 |
DefinitionProvider<EndpointDefinition> provider = mockDefinitionProvider("endpoint10", newEndpointDefinition("endpoint10", true, String.class)); |
295 |
1 |
when(provider.getProblems()).thenReturn(new ArrayList<>()); |
296 |
1 |
registry.register(provider); |
297 |
1 |
DefinitionProvider<EndpointDefinition> duplicatedProvider = mockDefinitionProvider("endpoint11", newEndpointDefinition("endpoint11", true, String.class)); |
298 |
1 |
when(duplicatedProvider.getProblems()).thenReturn(new ArrayList<>()); |
299 |
|
|
300 |
|
|
301 |
1 |
registry.register(duplicatedProvider); |
302 |
|
|
303 |
|
|
304 |
1 |
List<DefinitionProvider<EndpointDefinition>> providers = registry.getAllProviders().stream() |
305 |
|
.filter(p -> String.class.equals(p.get().getImplementationClass())) |
306 |
|
.collect(Collectors.toList()); |
307 |
|
|
308 |
1 |
assertThat(providers, hasSize(2)); |
309 |
|
|
310 |
1 |
DefinitionProvider<EndpointDefinition> target = providers.stream() |
311 |
|
.filter(p -> p.getProblems().size() > 0) |
312 |
|
.findFirst().orElse(null); |
313 |
|
|
314 |
1 |
assertThat(target.getProblems(), hasSize(1)); |
315 |
1 |
DefinitionProvider.Problem problem = target.getProblems().iterator().next(); |
316 |
1 |
assertEquals(MAJOR, problem.getSeverityType()); |
317 |
1 |
assertEquals("Conflict endpoint definitions.", problem.getTitle()); |
318 |
|
} |
319 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
320 |
44 |
private static DefinitionProvider<EndpointDefinition> mockDefinitionProvider(String endpointName, EndpointDefinition endpointDefinition) {... |
321 |
44 |
return mockDefinitionProvider(endpointName, endpointDefinition, "module"); |
322 |
|
} |
323 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
324 |
46 |
private static DefinitionProvider<EndpointDefinition> mockDefinitionProvider(String endpointName, EndpointDefinition endpointDefinition, String module) {... |
325 |
46 |
DefinitionProvider provider = mock(DefinitionProvider.class); |
326 |
46 |
DefinitionMetadata metadata = DefinitionMetadataBuilder.usingNameAsId() |
327 |
|
.type(EndpointDefinitionRegistry.TYPE) |
328 |
|
.module(module) |
329 |
|
.name(endpointName) |
330 |
|
.location("/" + module + "/restEndpoints/" + endpointName) |
331 |
|
.relativeLocation(endpointName) |
332 |
|
.build(); |
333 |
46 |
when(provider.isValid()).thenReturn(true); |
334 |
46 |
when(provider.getMetadata()).thenReturn(metadata); |
335 |
46 |
when(provider.get()).thenReturn(endpointDefinition); |
336 |
46 |
return provider; |
337 |
|
} |
338 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
339 |
29 |
private EndpointDefinition newEndpointDefinition(String name, boolean enabled, Class<?> clazz) {... |
340 |
29 |
ConfiguredEndpointDefinition endpointDefinition = new ConfiguredEndpointDefinition(); |
341 |
29 |
endpointDefinition.setName(name); |
342 |
29 |
endpointDefinition.setEnabled(enabled); |
343 |
29 |
endpointDefinition.setImplementationClass(clazz); |
344 |
29 |
return endpointDefinition; |
345 |
|
} |
346 |
|
} |