Clover icon

Magnolia REST Content Delivery 2.1

  1. Project Clover database Fri Mar 16 2018 18:21:08 CET
  2. Package info.magnolia.rest.delivery.jcr.v2

File JcrDeliveryEndpointTest.java

 

Code metrics

0
37
6
1
174
108
6
0.16
6.17
6
1

Classes

Class Line # Actions
JcrDeliveryEndpointTest 82 37 0% 6 0
1.0100%
 

Contributing tests

This file is covered by 3 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.delivery.jcr.v2;
35   
36    import static info.magnolia.rest.RestTestAccessManager.ANONYMOUS;
37    import static info.magnolia.test.hamcrest.ExceptionMatcher.instanceOf;
38    import static info.magnolia.test.hamcrest.ExecutionMatcher.throwsAnException;
39    import static info.magnolia.test.hamcrest.NodeMatchers.*;
40    import static info.magnolia.test.hamcrest.NodeMatchers.hasProperty;
41    import static org.hamcrest.Matchers.*;
42    import static org.junit.Assert.*;
43    import static org.mockito.Mockito.*;
44   
45    import info.magnolia.cms.security.User;
46    import info.magnolia.cms.util.ClasspathResourcesUtil;
47    import info.magnolia.context.DefaultRepositoryStrategy;
48    import info.magnolia.context.MgnlContext;
49    import info.magnolia.context.UserContext;
50    import info.magnolia.jcr.util.NodeTypes;
51    import info.magnolia.jcr.util.NodeUtil;
52    import info.magnolia.jcr.util.PropertiesImportExport;
53    import info.magnolia.objectfactory.Components;
54    import info.magnolia.repository.RepositoryConstants;
55    import info.magnolia.rest.reference.ReferenceContext;
56    import info.magnolia.test.ComponentsTestUtil;
57    import info.magnolia.test.RepositoryTestCase;
58    import info.magnolia.test.mock.MockWebContext;
59   
60    import java.io.InputStream;
61    import java.util.ArrayList;
62    import java.util.Arrays;
63    import java.util.List;
64   
65    import javax.jcr.Node;
66    import javax.jcr.NodeIterator;
67    import javax.jcr.PathNotFoundException;
68    import javax.jcr.RepositoryException;
69    import javax.jcr.Session;
70    import javax.ws.rs.core.MediaType;
71    import javax.ws.rs.core.MultivaluedHashMap;
72    import javax.ws.rs.core.MultivaluedMap;
73    import javax.ws.rs.core.UriInfo;
74    import javax.ws.rs.ext.Providers;
75   
76    import org.junit.Before;
77    import org.junit.Test;
78    import org.mockito.InjectMocks;
79    import org.mockito.Mock;
80    import org.mockito.MockitoAnnotations;
81   
 
82    public class JcrDeliveryEndpointTest extends RepositoryTestCase {
83   
84    private Session session;
85    private ConfiguredJcrDeliveryEndpointDefinition websiteParameters;
86    private MultivaluedMap<String, String> queryParameters;
87   
88    @InjectMocks
89    private JcrDeliveryEndpoint endpoint;
90   
91    @Mock
92    private Providers providers;
93   
94    @Mock
95    private UriInfo uriInfo;
96   
 
97  3 toggle @Before
98    public void setUp() throws Exception {
99  3 super.setUp();
100  3 session = MgnlContext.getJCRSession(RepositoryConstants.WEBSITE);
101  3 new PropertiesImportExport().createNodes(session.getRootNode(), getClass().getClassLoader().getResourceAsStream("travel-test-data.properties"));
102  3 session.save();
103   
104  3 websiteParameters = new ConfiguredJcrDeliveryEndpointDefinition();
105  3 websiteParameters.setWorkspace(RepositoryConstants.WEBSITE);
106  3 endpoint = new JcrDeliveryEndpoint(websiteParameters, Components.getComponentProvider());
107   
108  3 MockitoAnnotations.initMocks(this);
109  3 queryParameters = new MultivaluedHashMap<>();
110  3 when(uriInfo.getQueryParameters()).thenReturn(queryParameters);
111  3 when(providers.getContextResolver(ReferenceContext.class, MediaType.WILDCARD_TYPE)).thenReturn(type -> mock(ReferenceContext.class));
112    }
113   
 
114  3 toggle @Override
115    protected InputStream getJackrabbitRepositoryConfigFileStream() throws Exception {
116  3 return ClasspathResourcesUtil.getResource("jackrabbit-memory-search.xml").openStream();
117    }
118   
 
119  1 toggle @Test
120    public void readNode() throws Exception {
121    // WHEN
122  1 Node node = endpoint.readNode("/travel/about");
123   
124    // THEN
125  1 assertNotNull(node);
126  1 assertEquals("about", node.getName());
127  1 assertThat(node, hasProperty("title"));
128  1 assertThat(node, hasProperty(NodeTypes.LastModified.NAME));
129  1 assertThat(node, not(hasNode("footer")));
130    }
131   
 
132  1 toggle @Test
133    public void queryNodes() throws Exception {
134    // GIVEN
135  1 websiteParameters.setNodeTypes(Arrays.asList(NodeTypes.Page.NAME));
136   
137    // WHEN
138  1 NodeIterator nodeIterator = endpoint.queryNodes("", "", 0L, 10L).getResults();
139   
140    // THEN
141  1 assertNotNull(nodeIterator);
142  1 assertEquals(3, nodeIterator.getSize());
143  1 List<Node> nodes = NodeUtil.asList(NodeUtil.asIterable(nodeIterator));
144  1 List<String> nodeNames = new ArrayList<>((int) nodeIterator.getSize());
145  1 for (Node node : nodes) {
146  3 nodeNames.add(node.getName());
147    }
148  1 assertThat(nodeNames, containsInAnyOrder("about", "company", "travel"));
149    }
150   
 
151  1 toggle @Test
152    public void readingNodeWithoutBypassingAclsThrowsException() throws Exception {
153    // GIVEN
154  1 setUpContextForAnonymous();
155   
156    // WHEN
157  1 assertThat(() -> endpoint.readNode("/travel/about"),
158    throwsAnException(instanceOf(PathNotFoundException.class)));
159    }
160   
 
161  1 toggle private void setUpContextForAnonymous() throws RepositoryException {
162  1 MockWebContext context = new MockWebContext();
163  1 ComponentsTestUtil.setInstance(UserContext.class, mock(UserContext.class));
164  1 DefaultRepositoryStrategy defaultRepositoryStrategy = Components.newInstance(DefaultRepositoryStrategy.class);
165  1 context.setRepositoryStrategy(defaultRepositoryStrategy);
166   
167    // RestTestAccessManager only does a primitive check on userId to simulate no read permission for anonymous
168  1 User user = mock(User.class);
169  1 when(user.getName()).thenReturn(ANONYMOUS);
170  1 context.setUser(user);
171   
172  1 MgnlContext.setInstance(context);
173    }
174    }