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

File RestDispatcherServletTest.java

 

Code metrics

0
79
14
3
265
172
14
0.18
5.64
4.67
1

Classes

Class Line # Actions
RestDispatcherServletTest 71 74 0% 9 0
1.0100%
RestDispatcherServletTest.TestResource 233 3 0% 3 2
0.666666766.7%
RestDispatcherServletTest.TestResourceMultipleEndpoint 253 2 0% 2 2
0.550%
 

Contributing tests

This file is covered by 4 tests. .

Source view

1    /**
2    * This file Copyright (c) 2017-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;
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.Map;
55    import java.util.Set;
56    import java.util.stream.Stream;
57   
58    import javax.inject.Inject;
59    import javax.servlet.ServletConfig;
60    import javax.servlet.ServletContext;
61    import javax.servlet.ServletException;
62    import javax.ws.rs.GET;
63    import javax.ws.rs.Path;
64   
65    import org.jboss.resteasy.core.ResourceInvoker;
66    import org.jboss.resteasy.core.ResourceMethodRegistry;
67    import org.junit.After;
68    import org.junit.Before;
69    import org.junit.Test;
70   
 
71    public class RestDispatcherServletTest {
72   
73    private RestDispatcherServlet restDispatcherServlet;
74    private EndpointDefinition definition1;
75    private EndpointDefinition definition3;
76   
 
77  4 toggle @Before
78    public void setUp() throws Exception {
79  4 definition1 = newEndpointDefinition("definition1", TestResource.class);
80    // This one doesn't have implementation class set, therefore it will fail the registration.
81  4 EndpointDefinition definition2 = newEndpointDefinition("definition2", null);
82  4 definition3 = newEndpointDefinition("definition3", TestResource.class);
83   
84  4 List<DefinitionProvider<EndpointDefinition>> providers = Stream.of(definition1, definition2, definition3)
85    .map(RestDispatcherServletTest::mockDefinitionProvider)
86    .collect(toList());
87   
88  4 EndpointDefinitionRegistry registry = mock(EndpointDefinitionRegistry.class);
89  4 given(registry.getAllProviders()).willReturn(providers);
90   
91  4 restDispatcherServlet = new RestDispatcherServlet(mock(RestIntegrationModule.class), registry, mock(EventBus.class), new MockComponentProvider());
92    }
93   
 
94  1 toggle @Test
95    public void shouldUnregisterExistedEndpointThenReregister() throws ServletException {
96    // GIVEN
97  1 ServletConfig servletConfig = mock(ServletConfig.class);
98  1 ServletContext servletContext = mock(ServletContext.class);
99  1 given(servletConfig.getServletContext()).willReturn(servletContext);
100   
101  1 EndpointDefinition endpointDefinition = newEndpointDefinition("definition", TestResource.class);
102  1 List<DefinitionProvider<EndpointDefinition>> providers = Stream.of(endpointDefinition)
103    .map(RestDispatcherServletTest::mockDefinitionProvider)
104    .collect(toList());
105   
106  1 EndpointDefinitionRegistry registry = mock(EndpointDefinitionRegistry.class);
107  1 given(registry.getAllProviders()).willReturn(providers);
108  1 restDispatcherServlet = new RestDispatcherServlet(mock(RestIntegrationModule.class), registry, mock(EventBus.class), new MockComponentProvider());
109   
110  1 restDispatcherServlet.init(servletConfig);
111  1 Object initEndpoint = restDispatcherServlet.getApplication().getSingletons().iterator().next();
112   
113  1 DefinitionProvider<EndpointDefinition> endpointDefinitionProvider = mockDefinitionProvider(endpointDefinition);
114  1 EndpointDefinitionRegistryEvent event = new EndpointDefinitionRegistryEvent(EndpointDefinitionRegistryEventType.REGISTERED, endpointDefinitionProvider);
115   
116    //WHEN
117  1 restDispatcherServlet.onEndpointRegistered(event);
118   
119    //THEN
120  1 Set<Object> endpoints = restDispatcherServlet.getApplication().getSingletons();
121  1 assertThat(endpoints, hasSize(1));
122  1 assertThat(endpoints, not(hasItem(initEndpoint)));
123    }
124   
 
125  1 toggle @Test
126    public void failureUponRegistrationOfOneEndpointDoesNotLeadToSkippingOthers() throws Exception {
127    // GIVEN
128  1 ServletConfig servletConfig = mock(ServletConfig.class);
129  1 ServletContext servletContext = mock(ServletContext.class);
130  1 given(servletConfig.getServletContext()).willReturn(servletContext);
131   
132    // WHEN
133  1 restDispatcherServlet.init(servletConfig);
134   
135    // THEN
136  1 List<EndpointDefinition> registeredEndpoints = restDispatcherServlet.getApplication().getSingletons().stream()
137    .map(o -> ((TestResource) o).getEndpointDefinition())
138    .collect(toList());
139   
140  1 assertThat(registeredEndpoints, hasSize(2));
141  1 assertThat(registeredEndpoints, containsInAnyOrder(definition1, definition3));
142    }
143   
 
144  1 toggle @Test
145    public void registerMultipleEndpointsWithYamlPath() throws Exception {
146    // GIVEN
147  1 ServletConfig servletConfig = mock(ServletConfig.class);
148  1 ServletContext servletContext = mock(ServletContext.class);
149  1 given(servletConfig.getServletContext()).willReturn(servletContext);
150  1 restDispatcherServlet.init(servletConfig);
151   
152  1 EndpointDefinition endpointDefinition1 = newEndpointDefinition("delivery/e1_v1", TestResourceMultipleEndpoint.class);
153  1 DefinitionProvider<EndpointDefinition> provider1 = mockDefinitionProvider(endpointDefinition1);
154   
155  1 EndpointDefinition endpointDefinition2 = newEndpointDefinition("delivery/e2v1", TestResourceMultipleEndpoint.class);
156  1 DefinitionProvider<EndpointDefinition> provider2 = mockDefinitionProvider(endpointDefinition2);
157   
158    // WHEN
159  1 restDispatcherServlet.registerEndpoint(provider1);
160  1 restDispatcherServlet.registerEndpoint(provider2);
161   
162    // THEN
163  1 List<EndpointDefinition> registeredEndpoints = restDispatcherServlet.getApplication().getSingletons().stream()
164    .map(o -> ((TestResource) o).getEndpointDefinition())
165    .collect(toList());
166   
167  1 assertThat(registeredEndpoints, hasSize(4));
168  1 assertThat(registeredEndpoints, containsInAnyOrder(definition1, definition3, endpointDefinition1, endpointDefinition2));
169   
170  1 ResourceMethodRegistry registry = (ResourceMethodRegistry) restDispatcherServlet.getDispatcher().getRegistry();
171  1 final Map<String, List<ResourceInvoker>> bounded = registry.getBounded();
172  1 assertThat(bounded.keySet(), hasItems("delivery/e1/v1/", "delivery/e2v1/"));
173    }
174   
 
175  1 toggle @Test
176    public void registerMultipleEndpointsWithConfiguredPath() throws Exception {
177    // GIVEN
178  1 ServletConfig servletConfig = mock(ServletConfig.class);
179  1 ServletContext servletContext = mock(ServletContext.class);
180  1 given(servletConfig.getServletContext()).willReturn(servletContext);
181  1 restDispatcherServlet.init(servletConfig);
182   
183  1 EndpointDefinition endpointDefinition1 = newEndpointDefinition("delivery/e1_v1", TestResourceMultipleEndpoint.class, "/test/e10/v2");
184  1 DefinitionProvider<EndpointDefinition> provider1 = mockDefinitionProvider(endpointDefinition1);
185   
186    // WHEN
187  1 restDispatcherServlet.registerEndpoint(provider1);
188   
189    // THEN
190  1 List<EndpointDefinition> registeredEndpoints = restDispatcherServlet.getApplication().getSingletons().stream()
191    .map(o -> ((TestResource) o).getEndpointDefinition())
192    .collect(toList());
193   
194  1 assertThat(registeredEndpoints, hasSize(3));
195  1 assertThat(registeredEndpoints, containsInAnyOrder(definition1, definition3, endpointDefinition1));
196   
197  1 ResourceMethodRegistry registry = (ResourceMethodRegistry) restDispatcherServlet.getDispatcher().getRegistry();
198  1 final Map<String, List<ResourceInvoker>> bounded = registry.getBounded();
199  1 assertThat(bounded.keySet(), hasItems("test/e10/v2/"));
200    }
201   
 
202  16 toggle private static EndpointDefinition newEndpointDefinition(String name, Class<?> endpointClass) {
203  16 ConfiguredEndpointDefinition endpointDefinition = new ConfiguredEndpointDefinition();
204  16 endpointDefinition.setName(name);
205  16 endpointDefinition.setImplementationClass(endpointClass);
206  16 return endpointDefinition;
207    }
208   
 
209  1 toggle private static EndpointDefinition newEndpointDefinition(String name, Class<?> endpointClass, String basePath) {
210  1 EndpointDefinition endpointDefinition = newEndpointDefinition(name, endpointClass);
211  1 endpointDefinition = spy(endpointDefinition);
212  1 when(endpointDefinition.getEndpointPath()).thenReturn(basePath);
213  1 return endpointDefinition;
214    }
215   
 
216  17 toggle private static DefinitionProvider<EndpointDefinition> mockDefinitionProvider(EndpointDefinition endpointDefinition) {
217  17 DefinitionProvider provider = mock(DefinitionProvider.class);
218  17 DefinitionMetadata metadata = DefinitionMetadataBuilder.usingNameAsId()
219    .name(endpointDefinition.getName())
220    .build();
221  17 when(provider.isValid()).thenReturn(true);
222  17 when(provider.getMetadata()).thenReturn(metadata);
223  17 when(provider.get()).thenReturn(endpointDefinition);
224  17 return provider;
225    }
226   
 
227  4 toggle @After
228    public void tearDown() throws Exception {
229  4 ComponentsTestUtil.clear();
230    }
231   
232    @Path("/")
 
233    public class TestResource {
234    private final EndpointDefinition endpointDefinition;
235   
 
236  11 toggle @Inject
237    public TestResource(EndpointDefinition endpointDefinition) {
238  11 this.endpointDefinition = endpointDefinition;
239    }
240   
 
241  0 toggle @GET
242    public String get() {
243  0 return "test";
244    }
245   
 
246  9 toggle EndpointDefinition getEndpointDefinition() {
247  9 return endpointDefinition;
248    }
249    }
250   
251    @Path("/")
252    @DynamicPath
 
253    public class TestResourceMultipleEndpoint extends TestResource {
254   
 
255  3 toggle @Inject
256    public TestResourceMultipleEndpoint(EndpointDefinition endpointDefinition) {
257  3 super(endpointDefinition);
258    }
259   
 
260  0 toggle @GET
261    public String get() {
262  0 return "multipleEndpointTest";
263    }
264    }
265    }