Clover icon

Magnolia REST Integration 1.1

  1. Project Clover database Thu Aug 13 2015 19:08:49 CEST
  2. Package info.magnolia.rest.registry

File ConfiguredEndpointDefinitionManagerTest.java

 

Code metrics

0
49
5
1
166
90
5
0.1
9.8
5
1

Classes

Class Line # Actions
ConfiguredEndpointDefinitionManagerTest 62 49 0% 5 0
1.0100%
 

Contributing tests

This file is covered by 4 tests. .

Source view

1    /**
2    * This file Copyright (c) 2013-2015 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 org.junit.Assert.*;
37    import static org.mockito.Mockito.mock;
38   
39    import info.magnolia.context.MgnlContext;
40    import info.magnolia.event.RecordingEventBus;
41    import info.magnolia.jcr.util.NodeTypes;
42    import info.magnolia.jcr.util.NodeUtil;
43    import info.magnolia.module.ModuleRegistry;
44    import info.magnolia.registry.RegistrationException;
45    import info.magnolia.repository.RepositoryConstants;
46    import info.magnolia.rest.EndpointDefinition;
47    import info.magnolia.test.RepositoryTestCase;
48   
49    import java.util.ArrayList;
50    import java.util.List;
51   
52    import javax.jcr.Node;
53    import javax.jcr.RepositoryException;
54    import javax.jcr.Session;
55   
56    import org.junit.Before;
57    import org.junit.Test;
58   
59    /**
60    * Tests for {@link ConfiguredEndpointDefinitionManager}.
61    */
 
62    public class ConfiguredEndpointDefinitionManagerTest extends RepositoryTestCase {
63   
64    private ConfiguredEndpointDefinitionManager configuredEndpointDefinitionManager;
65    private EndpointDefinitionRegistry endpointDefinitionRegistry;
66    private Session configSession;
67    private List<Node> nodes;
68    private RecordingEventBus eventBus;
69   
 
70  4 toggle @Before
71    public void setUp() throws Exception {
72  4 super.setUp();
73   
74  4 configSession = MgnlContext.getJCRSession(RepositoryConstants.CONFIG);
75   
76  4 ModuleRegistry moduleRegistry = mock(ModuleRegistry.class);
77   
78  4 eventBus = new RecordingEventBus();
79  4 endpointDefinitionRegistry = new EndpointDefinitionRegistry(eventBus);
80  4 configuredEndpointDefinitionManager = new ConfiguredEndpointDefinitionManager(moduleRegistry, endpointDefinitionRegistry);
81   
82  4 final Node testEndpointNode = NodeUtil.createPath(configSession.getRootNode(), "/modules/test/rest-endpoints", NodeTypes.ContentNode.NAME);
83  4 final Node testEndpoint = NodeUtil.createPath(testEndpointNode, "/test", NodeTypes.ContentNode.NAME);
84  4 testEndpoint.setProperty("enabled", true);
85  4 testEndpoint.setProperty("class", ConfiguredEndpointDefinition.class.getName());
86  4 testEndpoint.setProperty("implementationClass", TestEndpoint.class.getName());
87  4 configSession.save();
88   
89  4 nodes = new ArrayList<Node>();
90  4 nodes.add(testEndpointNode);
91    }
92   
 
93  1 toggle @Test
94    public void testGetEndpointDefinition() throws RepositoryException, RegistrationException {
95    // GIVEN
96   
97    // WHEN
98  1 configuredEndpointDefinitionManager.reload(nodes);
99   
100    // THEN
101  1 EndpointDefinition endpointDefinition = endpointDefinitionRegistry.getEndpointDefinition("test");
102   
103  1 assertEquals("test", endpointDefinition.getName());
104  1 assertEquals(TestEndpoint.class.getName(), endpointDefinition.getImplementationClass().getName());
105    }
106   
 
107  1 toggle @Test
108    public void testSendsRegisteredEvent() throws RepositoryException {
109   
110    // WHEN
111  1 configuredEndpointDefinitionManager.reload(nodes);
112   
113    // THEN
114  1 assertEquals(1, eventBus.getSentEvents().size());
115  1 EndpointDefinitionRegistryEvent event = (EndpointDefinitionRegistryEvent) eventBus.getSentEvents().get(0);
116  1 assertEquals(EndpointDefinitionRegistryEventType.REGISTERED, event.getType());
117  1 assertEquals("test", event.getEndpointDefinition().getName());
118  1 assertTrue(event.getEndpointDefinition().isEnabled());
119  1 assertEquals(TestEndpoint.class, event.getEndpointDefinition().getImplementationClass());
120    }
121   
 
122  1 toggle @Test
123    public void testSendsRegisteredEventForDisabledEndpoint() throws RepositoryException {
124    // GIVEN
125  1 final Node testEndpointNode = NodeUtil.createPath(configSession.getRootNode(), "/modules/yet-another-test/rest-endpoints", NodeTypes.ContentNode.NAME);
126  1 final Node testEndpoint = NodeUtil.createPath(testEndpointNode, "/test-2", NodeTypes.ContentNode.NAME);
127  1 testEndpoint.setProperty("enabled", false);
128  1 testEndpoint.setProperty("class", ConfiguredEndpointDefinition.class.getName());
129  1 testEndpoint.setProperty("implementationClass", TestEndpoint.class.getName());
130  1 configSession.save();
131   
132  1 List<Node> nodes = new ArrayList<Node>();
133  1 nodes.add(testEndpointNode);
134   
135    // WHEN
136  1 configuredEndpointDefinitionManager.reload(nodes);
137   
138    // THEN
139  1 assertEquals(1, eventBus.getSentEvents().size());
140  1 EndpointDefinitionRegistryEvent event = (EndpointDefinitionRegistryEvent) eventBus.getSentEvents().get(0);
141  1 assertEquals(EndpointDefinitionRegistryEventType.REGISTERED, event.getType());
142  1 assertEquals("test-2", event.getEndpointDefinition().getName());
143  1 assertFalse(event.getEndpointDefinition().isEnabled());
144  1 assertEquals(TestEndpoint.class, event.getEndpointDefinition().getImplementationClass());
145    }
146   
 
147  1 toggle @Test
148    public void testIgnoresIncompleteEndpointDefinition() throws RepositoryException {
149    // GIVEN
150  1 final Node testEndpointNode = NodeUtil.createPath(configSession.getRootNode(), "/modules/yet-another-test/rest-endpoints", NodeTypes.ContentNode.NAME);
151  1 final Node testEndpoint = NodeUtil.createPath(testEndpointNode, "/test-2", NodeTypes.ContentNode.NAME);
152  1 testEndpoint.setProperty("enabled", true);
153  1 testEndpoint.setProperty("class", ConfiguredEndpointDefinition.class.getName());
154  1 configSession.save();
155   
156  1 List<Node> nodes = new ArrayList<Node>();
157  1 nodes.add(testEndpointNode);
158   
159    // WHEN
160  1 configuredEndpointDefinitionManager.reload(nodes);
161   
162    // THEN
163    // Should be empty, as endpoint was not yet fully configured
164  1 assertTrue(eventBus.isEmpty());
165    }
166    }