Clover icon

Magnolia REST Integration 2.0-rc1

  1. Project Clover database Mon Oct 30 2017 16:27:09 CET
  2. Package info.magnolia.rest.registry

File EndpointDefinitionRegistryTest.java

 

Code metrics

0
50
6
1
208
133
6
0.12
8.33
6
1
26.3% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
EndpointDefinitionRegistryTest 60 50 26.3% 6 0
1.0100%
 

Contributing tests

This file is covered by 9 tests. .

Source view

1    /**
2    * This file Copyright (c) 2013-2017 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.rest.registry.EndpointDefinitionRegistryEventType.*;
37    import static info.magnolia.test.hamcrest.ExceptionMatcher.instanceOf;
38    import static info.magnolia.test.hamcrest.ExecutionMatcher.throwsAnException;
39    import static java.util.Collections.*;
40    import static org.hamcrest.CoreMatchers.equalTo;
41    import static org.hamcrest.Matchers.contains;
42    import static org.junit.Assert.*;
43    import static org.mockito.Mockito.*;
44   
45    import info.magnolia.config.registry.DefinitionMetadata;
46    import info.magnolia.config.registry.DefinitionMetadataBuilder;
47    import info.magnolia.config.registry.DefinitionProvider;
48    import info.magnolia.config.registry.Registry;
49    import info.magnolia.event.RecordingEventBus;
50    import info.magnolia.module.ModuleRegistry;
51    import info.magnolia.registry.RegistrationException;
52    import info.magnolia.rest.EndpointDefinition;
53   
54    import java.util.Collections;
55    import java.util.Set;
56   
57    import org.junit.Before;
58    import org.junit.Test;
59   
 
60    public class EndpointDefinitionRegistryTest {
61    private EndpointDefinitionRegistry registry;
62    private RecordingEventBus eventBus;
63   
 
64  9 toggle @Before
65    public void setUp() throws Exception {
66  9 ModuleRegistry moduleRegistry = mock(ModuleRegistry.class);
67  9 eventBus = new RecordingEventBus();
68  9 registry = new EndpointDefinitionRegistry(moduleRegistry, eventBus);
69   
70  9 EndpointDefinition endpoint1 = newEndpointDefinition("endpoint1", true);
71  9 EndpointDefinition endpoint2 = newEndpointDefinition("endpoint2", true);
72  9 EndpointDefinition empty = mock(EndpointDefinition.class);
73  9 registry.register(mockDefinitionProvider("endpoint1", endpoint1));
74  9 registry.register(mockDefinitionProvider("endpoint2", endpoint2));
75  9 DefinitionProvider<EndpointDefinition> invalidProvider = mockDefinitionProvider("empty", empty);
76  9 when(invalidProvider.isValid()).thenReturn(false);
77  9 registry.register(invalidProvider);
78    }
79   
 
80    toggle @Test
81    public void getAllDefinitions() throws Exception {
82    // WHEN
83    int size = registry.getAllDefinitions().size();
84   
85    // THEN
86    assertEquals(2, size);
87    }
88   
 
89    toggle @Test
90    public void getProvider() throws Exception {
91    // WHEN
92    DefinitionProvider<EndpointDefinition> provider = registry.getProvider("endpoint1");
93   
94    // THEN
95    assertTrue(provider.isValid());
96    assertThat(provider.get().getName(), equalTo("endpoint1"));
97    }
98   
 
99    toggle @Test
100    public void getNonExistingProvider() throws Exception {
101    assertThat(() -> {
102    registry.getProvider("endpoint99");
103    }, throwsAnException(instanceOf(Registry.NoSuchDefinitionException.class)));
104    }
105   
 
106    toggle @Test
107    public void getInvalidProvider() throws Exception {
108    DefinitionProvider<EndpointDefinition> provider = registry.getProvider("empty");
109    assertFalse(provider.isValid());
110    }
111   
 
112    toggle @Test
113    @Deprecated
114    public void getExistingWithDeprecatedApi() throws Exception {
115    // WHEN
116    EndpointDefinition res = registry.getEndpointDefinition("endpoint1");
117   
118    // THEN
119    assertNotNull(res);
120    assertThat(res.getName(), equalTo("endpoint1"));
121    }
122   
 
123    toggle @Test
124    @Deprecated
125    public void getNonExistingWithDeprecatedApi() throws Exception {
126    assertThat(() -> {
127    registry.getEndpointDefinition("endpoint99");
128    }, throwsAnException(instanceOf(RegistrationException.class)));
129    }
130   
 
131  1 toggle @Test
132    public void unregisterAndRegisterWhenEmpty() throws Exception {
133    // GIVEN
134  1 registry.unregisterAndRegister(registry.getAllMetadata(), emptyList());
135  1 DefinitionProvider<EndpointDefinition> provider = mockDefinitionProvider("endpoint3", newEndpointDefinition("endpoint3", true));
136  1 DefinitionMetadata metadata3 = provider.getMetadata();
137  1 eventBus.getSentEvents().clear();
138   
139    // WHEN
140  1 Set<DefinitionMetadata> registeredIds = registry.unregisterAndRegister(emptyList(), singletonList(provider));
141   
142    // THEN
143  1 assertThat(registeredIds, contains(metadata3));
144  1 assertEquals(1, eventBus.getSentEvents().size());
145  1 EndpointDefinitionRegistryEvent event = (EndpointDefinitionRegistryEvent) eventBus.getEvent();
146  1 assertEquals(REGISTERED, event.getType());
147  1 assertEquals("endpoint3", event.getEndpointName());
148  1 assertEquals(provider, event.getEndpointDefinitionProvider());
149    }
150   
 
151  1 toggle @Test
152    public void register() throws Exception {
153    // GIVEN
154  1 DefinitionProvider<EndpointDefinition> provider = mockDefinitionProvider("endpoint4", newEndpointDefinition("endpoint4", true));
155  1 eventBus.getSentEvents().clear();
156   
157    // WHEN
158  1 registry.register(provider);
159   
160    // THEN
161  1 assertTrue(registry.getProvider("endpoint4").isValid());
162  1 assertEquals(1, eventBus.getSentEvents().size());
163  1 EndpointDefinitionRegistryEvent event = (EndpointDefinitionRegistryEvent) eventBus.getEvent();
164  1 assertEquals(REGISTERED, event.getType());
165  1 assertEquals("endpoint4", event.getEndpointName());
166  1 assertEquals(provider, event.getEndpointDefinitionProvider());
167    }
168   
 
169  1 toggle @Test
170    public void unregister() throws Exception {
171    // GIVEN
172  1 DefinitionProvider<EndpointDefinition> providerToRemove = registry.getProvider("endpoint2");
173  1 eventBus.getSentEvents().clear();
174   
175    // WHEN
176  1 registry.unregisterAndRegister(singletonList(providerToRemove.getMetadata()), Collections.emptyList());
177   
178    // THEN
179  1 assertThat(() -> {
180  1 registry.getProvider("endpoint2");
181    }, throwsAnException(instanceOf(Registry.NoSuchDefinitionException.class)));
182  1 assertEquals(1, eventBus.getSentEvents().size());
183  1 EndpointDefinitionRegistryEvent event = (EndpointDefinitionRegistryEvent) eventBus.getEvent();
184  1 assertEquals(UNREGISTERED, event.getType());
185  1 assertEquals("endpoint2", event.getEndpointName());
186    }
187   
 
188  20 toggle private static EndpointDefinition newEndpointDefinition(String name, boolean enabled) {
189  20 ConfiguredEndpointDefinition endpointDefinition = new ConfiguredEndpointDefinition();
190  20 endpointDefinition.setName(name);
191  20 endpointDefinition.setEnabled(enabled);
192  20 return endpointDefinition;
193    }
194   
 
195  29 toggle private static DefinitionProvider<EndpointDefinition> mockDefinitionProvider(String endpointName, EndpointDefinition endpointDefinition) {
196  29 DefinitionProvider provider = mock(DefinitionProvider.class);
197  29 DefinitionMetadata metadata = DefinitionMetadataBuilder.usingNameAsId()
198    .type(EndpointDefinitionRegistry.TYPE)
199    .module("module")
200    .name(endpointName)
201    .relativeLocation("test/" + endpointName)
202    .build();
203  29 when(provider.isValid()).thenReturn(true);
204  29 when(provider.getMetadata()).thenReturn(metadata);
205  29 when(provider.get()).thenReturn(endpointDefinition);
206  29 return provider;
207    }
208    }