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 EndpointDefinitionRegistryTest.java

 

Code metrics

0
46
8
1
171
89
8
0.17
5.75
8
1

Classes

Class Line # Actions
EndpointDefinitionRegistryTest 63 46 0% 8 0
1.0100%
 

Contributing tests

This file is covered by 5 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   
38    import info.magnolia.event.RecordingEventBus;
39    import info.magnolia.jcr.node2bean.Node2BeanException;
40    import info.magnolia.jcr.node2bean.Node2BeanProcessor;
41    import info.magnolia.jcr.node2bean.impl.Node2BeanProcessorImpl;
42    import info.magnolia.jcr.node2bean.impl.Node2BeanTransformerImpl;
43    import info.magnolia.jcr.node2bean.impl.TypeMappingImpl;
44    import info.magnolia.registry.RegistrationException;
45    import info.magnolia.rest.EndpointDefinition;
46    import info.magnolia.test.ComponentsTestUtil;
47    import info.magnolia.test.MgnlTestCase;
48    import info.magnolia.test.mock.jcr.MockNode;
49   
50    import java.util.ArrayList;
51    import java.util.Arrays;
52    import java.util.HashSet;
53    import java.util.Set;
54   
55    import javax.jcr.RepositoryException;
56   
57    import org.junit.Before;
58    import org.junit.Test;
59   
60    /**
61    * Main test class for {@link EndpointDefinitionRegistry}.
62    */
 
63    public class EndpointDefinitionRegistryTest extends MgnlTestCase {
64   
65    private EndpointDefinitionRegistry endpointDefinitionRegistry;
66    private RecordingEventBus eventBus;
67   
 
68  5 toggle @Before
69    public void setUp() throws Exception {
70  5 eventBus = new RecordingEventBus();
71  5 endpointDefinitionRegistry = new EndpointDefinitionRegistry(eventBus);
72   
73  5 TypeMappingImpl typeMapping = new TypeMappingImpl();
74  5 Node2BeanTransformerImpl transformer = new Node2BeanTransformerImpl();
75  5 ComponentsTestUtil.setInstance(Node2BeanProcessor.class, new Node2BeanProcessorImpl(typeMapping, transformer));
76  5 ComponentsTestUtil.setImplementation(EndpointDefinition.class, ConfiguredEndpointDefinition.class);
77    }
78   
 
79  1 toggle @Test
80    public void testGetEndpointDefinition() throws Node2BeanException, RepositoryException, RegistrationException {
81    // GIVEN
82  1 final String endpoint1 = "endpoint1";
83  1 EndpointDefinitionProvider endpointDefinitionProvider = createEndpointDefinitionProvider(endpoint1, true);
84   
85  1 endpointDefinitionRegistry.unregisterAndRegister(new HashSet<String>(), Arrays.asList(endpointDefinitionProvider));
86   
87    // WHEN
88  1 EndpointDefinition definition = endpointDefinitionRegistry.getEndpointDefinition(endpoint1);
89   
90    // THEN
91  1 assertNotNull(definition);
92  1 assertEquals(endpoint1, definition.getName());
93    }
94   
 
95  1 toggle @Test(expected = RegistrationException.class)
96    public void testGetEndpointDefinitionThrowsExceptionWhenEndpointNotFound() throws Node2BeanException, RepositoryException, RegistrationException {
97    // GIVEN
98  1 final String endpoint2 = "endpoint2";
99  1 EndpointDefinitionProvider endpointDefinitionProvider = createEndpointDefinitionProvider(endpoint2, true);
100   
101  1 endpointDefinitionRegistry.unregisterAndRegister(new HashSet<String>(), Arrays.asList(endpointDefinitionProvider));
102   
103    // WHEN
104  1 endpointDefinitionRegistry.getEndpointDefinition("xx");
105    }
106   
 
107  1 toggle @Test
108    public void testUnregisterAndRegisterWhenEmpty() throws Node2BeanException, RepositoryException, RegistrationException {
109    // GIVEN
110  1 String endpoint3 = "endpoint3";
111  1 EndpointDefinitionProvider endpointDefinitionProvider = createEndpointDefinitionProvider(endpoint3, true);
112   
113    // WHEN
114  1 Set<String> registeredNames = endpointDefinitionRegistry.unregisterAndRegister(new HashSet<String>(), Arrays.asList(endpointDefinitionProvider));
115   
116    // THEN
117    // endpointDefinitionProvider should be registered.
118  1 assertNotNull(registeredNames);
119  1 assertEquals(1, registeredNames.size());
120  1 assertEquals(endpoint3, registeredNames.toArray()[0]);
121  1 assertEquals(1, eventBus.getSentEvents().size());
122  1 assertEquals("endpoint3", ((EndpointDefinitionRegistryEvent) eventBus.getEvent()).getEndpointDefinition().getName());
123    }
124   
 
125  1 toggle @Test
126    public void testRegister() throws RepositoryException, Node2BeanException, RegistrationException {
127    // GIVEN
128  1 final String endpoint4 = "endpoint4";
129  1 EndpointDefinitionProvider provider = createEndpointDefinitionProvider(endpoint4, true);
130   
131  1 assertFalse(endpointDefinitionRegistry.isEndpointDefinitionRegistered(endpoint4));
132   
133    // WHEN
134  1 endpointDefinitionRegistry.unregisterAndRegister(new HashSet<String>(), Arrays.asList(provider));
135   
136    // THEN
137  1 assertTrue(endpointDefinitionRegistry.isEndpointDefinitionRegistered(endpoint4));
138  1 assertEquals(1, eventBus.getSentEvents().size());
139  1 assertEquals("endpoint4", ((EndpointDefinitionRegistryEvent) eventBus.getEvent()).getEndpointDefinition().getName());
140    }
141   
 
142  1 toggle @Test
143    public void testUnregister() throws RepositoryException, Node2BeanException, RegistrationException {
144    // GIVEN
145  1 final String endpoint5 = "endpoint5";
146  1 EndpointDefinitionProvider provider = createEndpointDefinitionProvider(endpoint5, true);
147   
148  1 assertFalse(endpointDefinitionRegistry.isEndpointDefinitionRegistered(endpoint5));
149   
150  1 endpointDefinitionRegistry.unregisterAndRegister(new HashSet<String>(), Arrays.asList(provider));
151  1 eventBus.getSentEvents().clear();
152   
153  1 assertTrue(endpointDefinitionRegistry.isEndpointDefinitionRegistered(endpoint5));
154   
155    // WHEN
 
156  1 toggle endpointDefinitionRegistry.unregisterAndRegister(new HashSet<String>() {{ add(endpoint5); }}, new ArrayList<EndpointDefinitionProvider>());
157   
158    // THEN
159  1 assertFalse(endpointDefinitionRegistry.isEndpointDefinitionRegistered(endpoint5));
160  1 assertEquals(1, eventBus.getSentEvents().size());
161  1 assertEquals("endpoint5", ((EndpointDefinitionRegistryEvent) eventBus.getEvent()).getEndpointName());
162    }
163   
 
164  5 toggle private EndpointDefinitionProvider createEndpointDefinitionProvider(String name, boolean enabled) throws RepositoryException, Node2BeanException {
165  5 MockNode node = new MockNode("name");
166  5 node.setProperty("name", name);
167  5 node.setProperty("enabled", enabled);
168  5 return new ConfiguredEndpointDefinitionProvider(name, node);
169    }
170   
171    }