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

File RestDispatcherServletTest.java

 

Code metrics

0
44
9
2
183
112
9
0.2
4.89
4.5
1

Classes

Class Line # Actions
RestDispatcherServletTest 68 41 0% 6 0
1.0100%
RestDispatcherServletTest.TestResource 142 3 0% 3 2
0.666666766.7%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    /**
2    * This file Copyright (c) 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;
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.Set;
55    import java.util.stream.Stream;
56   
57    import javax.inject.Inject;
58    import javax.servlet.ServletConfig;
59    import javax.servlet.ServletContext;
60    import javax.servlet.ServletException;
61    import javax.ws.rs.GET;
62    import javax.ws.rs.Path;
63   
64    import org.junit.After;
65    import org.junit.Before;
66    import org.junit.Test;
67   
 
68    public class RestDispatcherServletTest {
69   
70    private RestDispatcherServlet restDispatcherServlet;
71    private EndpointDefinition definition1;
72    private EndpointDefinition definition3;
73   
 
74  2 toggle @Before
75    public void setUp() throws Exception {
76  2 definition1 = newEndpointDefinition("definition1", TestResource.class);
77    // This one doesn't have implementation class set, therefore it will fail the registration.
78  2 EndpointDefinition definition2 = newEndpointDefinition("definition2", null);
79  2 definition3 = newEndpointDefinition("definition3", TestResource.class);
80   
81  2 List<DefinitionProvider<EndpointDefinition>> providers = Stream.of(definition1, definition2, definition3)
82    .map(RestDispatcherServletTest::mockDefinitionProvider)
83    .collect(toList());
84   
85  2 EndpointDefinitionRegistry registry = mock(EndpointDefinitionRegistry.class);
86  2 given(registry.getAllProviders()).willReturn(providers);
87   
88  2 restDispatcherServlet = new RestDispatcherServlet(mock(RestIntegrationModule.class), registry, mock(EventBus.class), new MockComponentProvider());
89    }
90   
 
91  1 toggle @Test
92    public void shouldUnregisterExistedEndpointThenReregister() throws ServletException {
93    // GIVEN
94  1 ServletConfig servletConfig = mock(ServletConfig.class);
95  1 ServletContext servletContext = mock(ServletContext.class);
96  1 given(servletConfig.getServletContext()).willReturn(servletContext);
97   
98  1 EndpointDefinition endpointDefinition = newEndpointDefinition("definition", TestResource.class);
99  1 List<DefinitionProvider<EndpointDefinition>> providers = Stream.of(endpointDefinition)
100    .map(RestDispatcherServletTest::mockDefinitionProvider)
101    .collect(toList());
102   
103  1 EndpointDefinitionRegistry registry = mock(EndpointDefinitionRegistry.class);
104  1 given(registry.getAllProviders()).willReturn(providers);
105  1 restDispatcherServlet = new RestDispatcherServlet(mock(RestIntegrationModule.class), registry, mock(EventBus.class), new MockComponentProvider());
106   
107  1 restDispatcherServlet.init(servletConfig);
108  1 Object initEndpoint = restDispatcherServlet.getApplication().getSingletons().iterator().next();
109   
110  1 DefinitionProvider<EndpointDefinition> endpointDefinitionProvider = mockDefinitionProvider(endpointDefinition);
111  1 EndpointDefinitionRegistryEvent event = new EndpointDefinitionRegistryEvent(EndpointDefinitionRegistryEventType.REGISTERED, endpointDefinitionProvider);
112   
113    //WHEN
114  1 restDispatcherServlet.onEndpointRegistered(event);
115   
116    //THEN
117  1 Set<Object> endpoints = restDispatcherServlet.getApplication().getSingletons();
118  1 assertThat(endpoints, hasSize(1));
119  1 assertThat(endpoints, not(hasItem(initEndpoint)));
120    }
121   
 
122  1 toggle @Test
123    public void failureUponRegistrationOfOneEndpointDoesNotLeadToSkippingOthers() throws Exception {
124    // GIVEN
125  1 ServletConfig servletConfig = mock(ServletConfig.class);
126  1 ServletContext servletContext = mock(ServletContext.class);
127  1 given(servletConfig.getServletContext()).willReturn(servletContext);
128   
129    // WHEN
130  1 restDispatcherServlet.init(servletConfig);
131   
132    // THEN
133  1 List<EndpointDefinition> registeredEndpoints = restDispatcherServlet.getApplication().getSingletons().stream()
134    .map(o -> ((TestResource) o).getEndpointDefinition())
135    .collect(toList());
136   
137  1 assertThat(registeredEndpoints, hasSize(2));
138  1 assertThat(registeredEndpoints, containsInAnyOrder(definition1, definition3));
139    }
140   
141    @Path("/")
 
142    public class TestResource {
143    private final EndpointDefinition endpointDefinition;
144   
 
145  4 toggle @Inject
146    public TestResource(EndpointDefinition endpointDefinition) {
147  4 this.endpointDefinition = endpointDefinition;
148    }
149   
 
150  2 toggle EndpointDefinition getEndpointDefinition() {
151  2 return endpointDefinition;
152    }
153   
 
154  0 toggle @GET
155    public String get() {
156  0 return "test";
157    }
158   
159    }
160   
 
161  2 toggle @After
162    public void tearDown() throws Exception {
163  2 ComponentsTestUtil.clear();
164    }
165   
 
166  7 toggle private static EndpointDefinition newEndpointDefinition(String name, Class<?> endpointClass) {
167  7 ConfiguredEndpointDefinition endpointDefinition = new ConfiguredEndpointDefinition();
168  7 endpointDefinition.setName(name);
169  7 endpointDefinition.setImplementationClass(endpointClass);
170  7 return endpointDefinition;
171    }
172   
 
173  8 toggle private static DefinitionProvider<EndpointDefinition> mockDefinitionProvider(EndpointDefinition endpointDefinition) {
174  8 DefinitionProvider provider = mock(DefinitionProvider.class);
175  8 DefinitionMetadata metadata = DefinitionMetadataBuilder.usingNameAsId()
176    .name(endpointDefinition.getName())
177    .build();
178  8 when(provider.isValid()).thenReturn(true);
179  8 when(provider.getMetadata()).thenReturn(metadata);
180  8 when(provider.get()).thenReturn(endpointDefinition);
181  8 return provider;
182    }
183    }