Clover icon

Magnolia REST Integration 2.1

  1. Project Clover database Fri Mar 16 2018 18:18:02 CET
  2. Package info.magnolia.rest

File RestJerseyDispatcherServletTest.java

 

Code metrics

0
72
14
3
254
165
14
0.19
5.14
4.67
1

Classes

Class Line # Actions
RestJerseyDispatcherServletTest 71 67 0% 9 0
1.0100%
RestJerseyDispatcherServletTest.TestJerseyResource 222 3 0% 3 2
0.666666766.7%
RestJerseyDispatcherServletTest.TestJerseyResourceMultipleEndpoint 242 2 0% 2 2
0.550%
 

Contributing tests

This file is covered by 4 tests. .

Source view

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