Clover icon

Magnolia REST Integration 2.1.5

  1. Project Clover database Tue Jan 28 2020 16:36:47 CET
  2. Package info.magnolia.rest.registry

File EndpointDefinitionRegistryTest.java

 

Code metrics

8
118
12
1
346
239
16
0.14
9.83
12
1.33
12.7% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
EndpointDefinitionRegistryTest 68 118 12.7% 16 3
0.978260997.8%
 

Contributing tests

This file is covered by 13 tests. .

Source view

1    /**
2    * This file Copyright (c) 2013-2018 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
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   
 
68    public class EndpointDefinitionRegistryTest {
69    private EndpointDefinitionRegistry registry;
70    private RecordingEventBus eventBus;
71   
 
72  13 toggle @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   
 
88    toggle @Test
89    public void getAllDefinitions() throws Exception {
90    // WHEN
91    int size = registry.getAllDefinitions().size();
92   
93    // THEN
94    assertEquals(2, size);
95    }
96   
 
97    toggle @Test
98    public void getProvider() throws Exception {
99    // WHEN
100    DefinitionProvider<EndpointDefinition> provider = registry.getProvider("endpoint1");
101   
102    // THEN
103    assertTrue(provider.isValid());
104    assertThat(provider.get().getName(), equalTo("endpoint1"));
105    }
106   
 
107    toggle @Test
108    public void getNonExistingProvider() throws Exception {
109    assertThat(() -> {
110    registry.getProvider("endpoint99");
111    }, throwsAnException(instanceOf(Registry.NoSuchDefinitionException.class)));
112    }
113   
 
114    toggle @Test
115    public void getInvalidProvider() throws Exception {
116    DefinitionProvider<EndpointDefinition> provider = registry.getProvider("empty");
117    assertFalse(provider.isValid());
118    }
119   
 
120    toggle @Test
121    @Deprecated
122    public void getExistingWithDeprecatedApi() throws Exception {
123    // WHEN
124    EndpointDefinition res = registry.getEndpointDefinition("endpoint1");
125   
126    // THEN
127    assertNotNull(res);
128    assertThat(res.getName(), equalTo("endpoint1"));
129    }
130   
 
131    toggle @Test
132    @Deprecated
133    public void getNonExistingWithDeprecatedApi() throws Exception {
134    assertThat(() -> {
135    registry.getEndpointDefinition("endpoint99");
136    }, throwsAnException(instanceOf(RegistrationException.class)));
137    }
138   
 
139  1 toggle @Test
140    public void unregisterAndRegisterWhenEmpty() throws Exception {
141    // GIVEN
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    // WHEN
148  1 Set<DefinitionMetadata> registeredIds = registry.unregisterAndRegister(emptyList(), singletonList(provider));
149   
150    // THEN
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   
 
159  1 toggle @Test
160    public void register() throws Exception {
161    // GIVEN
162  1 DefinitionProvider<EndpointDefinition> provider = mockDefinitionProvider("endpoint4", newEndpointDefinition("endpoint4", true));
163  1 eventBus.getSentEvents().clear();
164   
165    // WHEN
166  1 registry.register(provider);
167   
168    // THEN
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   
 
177  1 toggle @Test
178    public void registeringSameNameEndpointJustTakesTheFirstEndpoint() throws Exception {
179    // GIVEN
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    // WHEN
186  1 DefinitionProvider<EndpointDefinition> sameNameProvider = mockDefinitionProvider("endpoint", newEndpointDefinition("endpoint", true), "moduleB");
187  1 registry.register(sameNameProvider);
188   
189    // THEN
190  1 assertTrue(registry.getProvider("endpoint").isValid());
191  1 EndpointDefinitionRegistryEvent event = (EndpointDefinitionRegistryEvent) eventBus.getEvent();
192  1 assertEquals("moduleA", event.getEndpointDefinitionProvider().getMetadata().getModule());
193    }
194   
 
195  1 toggle @Test
196    public void firedEventContainsDecoratedProvider() throws Exception {
197    // GIVEN
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    // WHEN
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    // THEN
229  1 EndpointDefinitionRegistryEvent event = (EndpointDefinitionRegistryEvent) eventBus.getEvent();
230  1 assertFalse(event.getEndpointDefinitionProvider().getDecorators().isEmpty());
231    }
232   
 
233  1 toggle @Test
234    public void unregister() throws Exception {
235    // GIVEN
236  1 DefinitionProvider<EndpointDefinition> providerToRemove = registry.getProvider("endpoint2");
237  1 eventBus.getSentEvents().clear();
238   
239    // WHEN
240  1 registry.unregisterAndRegister(singletonList(providerToRemove.getMetadata()), Collections.emptyList());
241   
242    // THEN
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   
 
252  1 toggle @Test
253    public void decoratorsTriggerRegistryEvents() throws Exception {
254    // GIVEN
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    // WHEN
274  1 registry.addDecorator(decorator);
275   
276    // THEN
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   
 
284  4 toggle 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   
 
291  1 toggle @Test
292    public void registerAndConflictedEndpointShouldRaiseMajorProblem() throws Exception {
293    // GIVEN
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    // WHEN
301  1 registry.register(duplicatedProvider);
302   
303    // THEN
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   
 
320  44 toggle private static DefinitionProvider<EndpointDefinition> mockDefinitionProvider(String endpointName, EndpointDefinition endpointDefinition) {
321  44 return mockDefinitionProvider(endpointName, endpointDefinition, "module");
322    }
323   
 
324  46 toggle 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   
 
339  29 toggle 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    }