Clover icon

Magnolia REST Services 2.1.3

  1. Project Clover database Fri Oct 25 2019 12:35:42 CEST
  2. Package info.magnolia.rest.service.property.v1

File PropertyEndpointTest.java

 

Code metrics

0
71
23
1
331
165
23
0.32
3.09
23
1

Classes

Class Line # Actions
PropertyEndpointTest 62 71 0% 23 0
1.0100%
 

Contributing tests

This file is covered by 22 tests. .

Source view

1    /**
2    * This file Copyright (c) 2013-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.service.property.v1;
35   
36    import static org.junit.Assert.*;
37   
38    import info.magnolia.repository.RepositoryConstants;
39    import info.magnolia.rest.service.node.v1.RepositoryProperty;
40    import info.magnolia.rest.service.property.definition.ConfiguredPropertyEndpointDefinition;
41    import info.magnolia.rest.service.property.definition.PropertyEndpointDefinition;
42    import info.magnolia.test.MgnlTestCase;
43    import info.magnolia.test.mock.MockUtil;
44    import info.magnolia.test.mock.jcr.MockSession;
45    import info.magnolia.test.mock.jcr.MockValue;
46    import info.magnolia.test.mock.jcr.SessionTestUtil;
47   
48    import java.util.Arrays;
49    import java.util.Collections;
50   
51    import javax.jcr.PropertyType;
52    import javax.jcr.RepositoryException;
53    import javax.jcr.Value;
54    import javax.ws.rs.core.Response;
55   
56    import org.junit.Before;
57    import org.junit.Test;
58   
59    /**
60    * Test case for {@link PropertyEndpoint}.
61    */
 
62    public class PropertyEndpointTest extends MgnlTestCase {
63   
64    private PropertyEndpoint<PropertyEndpointDefinition> endpoint;
65    private MockSession session;
66   
 
67  22 toggle @Before
68    public void setUp() throws Exception {
69  22 super.setUp();
70  22 endpoint = new PropertyEndpoint<PropertyEndpointDefinition>(new ConfiguredPropertyEndpointDefinition());
71   
72  22 session = SessionTestUtil.createSession(RepositoryConstants.WEBSITE,
73    "/start",
74    "/start.property=propertyValue");
75   
76  22 session.getNode("start").setProperty("multiValueProperty", new Value[]{new MockValue("value1"), new MockValue("value2")});
77   
78  22 MockUtil.setSessionAndHierarchyManager(session);
79    }
80   
81    // GET
82   
 
83  1 toggle @Test
84    public void testGetReturnsPropertyValue() throws RepositoryException {
85   
86    // WHEN
87  1 Response response = endpoint.readProperty(RepositoryConstants.WEBSITE, "/start/property");
88   
89    // THEN
90  1 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
91  1 RepositoryProperty property = (RepositoryProperty) response.getEntity();
92  1 assertEquals(1, property.getValues().size());
93  1 assertEquals("propertyValue", property.getValues().get(0));
94    }
95   
 
96  1 toggle @Test
97    public void testGetMultiValuedProperty() throws RepositoryException {
98   
99    // WHEN
100  1 Response response = endpoint.readProperty(RepositoryConstants.WEBSITE, "/start/multiValueProperty");
101   
102    // THEN
103  1 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
104  1 RepositoryProperty property = (RepositoryProperty) response.getEntity();
105  1 assertEquals(2, property.getValues().size());
106  1 assertEquals("value1", property.getValues().get(0));
107  1 assertEquals("value2", property.getValues().get(1));
108    }
109   
 
110  1 toggle @Test
111    public void testGetReturnsNotFoundForNonExistingProperty() throws RepositoryException {
112   
113    // WHEN
114  1 Response response = endpoint.readProperty(RepositoryConstants.WEBSITE, "/start/nonExistingProperty");
115   
116    // THEN
117  1 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
118    }
119   
120    // PUT
121   
 
122  1 toggle @Test
123    public void testCreateProperty() throws RepositoryException {
124   
125    // WHEN
126  1 Response response = endpoint.createProperty(RepositoryConstants.WEBSITE, "/start", "newProperty", Arrays.asList("newValue"), PropertyType.TYPENAME_STRING, false);
127   
128    // THEN
129  1 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
130  1 assertEquals("newValue", session.getProperty("/start/newProperty").getString());
131    }
132   
 
133  1 toggle @Test
134    public void testCreatePropertyOnRootNode() throws RepositoryException {
135   
136    // WHEN
137  1 Response response = endpoint.createProperty(RepositoryConstants.WEBSITE, "/", "newProperty", Arrays.asList("newValue"), PropertyType.TYPENAME_STRING, false);
138   
139    // THEN
140  1 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
141  1 assertEquals("newValue", session.getProperty("/newProperty").getString());
142    }
143   
 
144  1 toggle @Test
145    public void testCreatePropertyOnRootNodePassingEmptyStringAsPath() throws RepositoryException {
146   
147    // WHEN
148  1 Response response = endpoint.createProperty(RepositoryConstants.WEBSITE, "", "newProperty", Arrays.asList("newValue"), PropertyType.TYPENAME_STRING, false);
149   
150    // THEN
151  1 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
152  1 assertEquals("newValue", session.getProperty("/newProperty").getString());
153    }
154   
 
155  1 toggle @Test
156    public void testCreateMultiValueProperty() throws RepositoryException {
157   
158    // WHEN
159  1 Response response = endpoint.createProperty(RepositoryConstants.WEBSITE, "/start", "newProperty", Arrays.asList("value1", "value2"), PropertyType.TYPENAME_STRING, true);
160   
161    // THEN
162  1 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
163  1 assertTrue(session.getProperty("/start/newProperty").isMultiple());
164  1 assertEquals("value1", session.getProperty("/start/newProperty").getValues()[0].getString());
165  1 assertEquals("value2", session.getProperty("/start/newProperty").getValues()[1].getString());
166    }
167   
 
168  1 toggle @Test
169    public void testCreateReturnsBadRequestIfNoValuesGiven() throws RepositoryException {
170   
171    // WHEN
172  1 Response response = endpoint.createProperty(RepositoryConstants.WEBSITE, "/start", "newProperty", Collections.<String>emptyList(), PropertyType.TYPENAME_STRING, false);
173   
174    // THEN
175  1 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
176    }
177   
 
178  1 toggle @Test
179    public void testCreateReturnsBadRequestIfMultipleValuesGivenButMultipleFlagIsFalse() throws RepositoryException {
180   
181    // WHEN
182  1 Response response = endpoint.createProperty(RepositoryConstants.WEBSITE, "/start", "newProperty", Arrays.asList("value1", "value2"), PropertyType.TYPENAME_STRING, false);
183   
184    // THEN
185  1 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
186    }
187   
 
188  1 toggle @Test
189    public void testCreateReturnsBadRequestWhenTypeIsUnknown() throws RepositoryException {
190   
191    // WHEN
192  1 Response response = endpoint.createProperty(RepositoryConstants.WEBSITE, "/start", "newProperty", Arrays.asList("newValue"), "UnknownType", false);
193   
194    // THEN
195  1 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
196    }
197   
 
198  1 toggle @Test
199    public void testCreateReturnsNotFoundWhenNodeDoesntExist() throws RepositoryException {
200   
201    // WHEN
202  1 Response response = endpoint.createProperty(RepositoryConstants.WEBSITE, "/nodeThatDoesntExist", "newProperty", Arrays.asList("newValue"), PropertyType.TYPENAME_STRING, false);
203   
204    // THEN
205  1 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
206    }
207   
 
208  1 toggle @Test
209    public void testCreateReturnsBadRequestIfPropertyAlreadyExists() throws RepositoryException {
210   
211    // WHEN
212  1 Response response = endpoint.createProperty(RepositoryConstants.WEBSITE, "/start", "property", Arrays.asList("newValue"), PropertyType.TYPENAME_STRING, false);
213   
214    // THEN
215  1 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
216    }
217   
218    // POST
219   
 
220  1 toggle @Test
221    public void testUpdateProperty() throws RepositoryException {
222   
223    // WHEN
224  1 Response response = endpoint.updateProperty(RepositoryConstants.WEBSITE, "/start/property", Arrays.asList("newValue"), PropertyType.TYPENAME_STRING, false);
225   
226    // THEN
227  1 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
228  1 assertEquals("newValue", session.getProperty("/start/property").getString());
229    }
230   
 
231  1 toggle @Test
232    public void testUpdatePropertyOnRootNode() throws RepositoryException {
233   
234  1 session.getRootNode().setProperty("newProperty", "oldValue");
235   
236    // WHEN
237  1 Response response = endpoint.updateProperty(RepositoryConstants.WEBSITE, "/newProperty", Arrays.asList("newValue"), PropertyType.TYPENAME_STRING, false);
238   
239    // THEN
240  1 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
241  1 assertEquals("newValue", session.getProperty("/newProperty").getString());
242    }
243   
 
244  1 toggle @Test
245    public void testUpdateSingleValuePropertyWithMultiValue() throws RepositoryException {
246   
247    // WHEN
248  1 Response response = endpoint.updateProperty(RepositoryConstants.WEBSITE, "/start/property", Arrays.asList("value1", "value2"), PropertyType.TYPENAME_STRING, true);
249   
250    // THEN
251  1 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
252  1 assertTrue(session.getProperty("/start/property").isMultiple());
253  1 assertEquals("value1", session.getProperty("/start/property").getValues()[0].getString());
254  1 assertEquals("value2", session.getProperty("/start/property").getValues()[1].getString());
255    }
256   
 
257  1 toggle @Test
258    public void testUpdateMultiValuePropertyWithSingleProperty() throws RepositoryException {
259   
260    // WHEN
261  1 Response response = endpoint.updateProperty(RepositoryConstants.WEBSITE, "/start/multiValueProperty", Arrays.asList("newValue"), PropertyType.TYPENAME_STRING, false);
262   
263    // THEN
264  1 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
265  1 assertFalse(session.getProperty("/start/multiValueProperty").isMultiple());
266  1 assertEquals("newValue", session.getProperty("/start/multiValueProperty").getString());
267    }
268   
 
269  1 toggle @Test
270    public void testUpdateReturnsBadRequestIfNoValuesGiven() throws RepositoryException {
271   
272    // WHEN
273  1 Response response = endpoint.updateProperty(RepositoryConstants.WEBSITE, "/start/property", Collections.<String>emptyList(), PropertyType.TYPENAME_STRING, false);
274   
275    // THEN
276  1 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
277    }
278   
 
279  1 toggle @Test
280    public void testUpdateReturnsBadRequestIfMultipleValuesGivenButMultipleFlagIsFalse() throws RepositoryException {
281   
282    // WHEN
283  1 Response response = endpoint.updateProperty(RepositoryConstants.WEBSITE, "/start/property", Arrays.asList("value1", "value2"), PropertyType.TYPENAME_STRING, false);
284   
285    // THEN
286  1 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
287    }
288   
 
289  1 toggle @Test
290    public void testUpdateReturnsBadRequestWhenTypeIsUnknown() throws RepositoryException {
291   
292    // WHEN
293  1 Response response = endpoint.updateProperty(RepositoryConstants.WEBSITE, "/start/property", Arrays.asList("newValue"), "UnknownType", false);
294   
295    // THEN
296  1 assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
297    }
298   
 
299  1 toggle @Test
300    public void testUpdateReturnsNotFoundIfPropertyDoesntExist() throws RepositoryException {
301   
302    // WHEN
303  1 Response response = endpoint.updateProperty(RepositoryConstants.WEBSITE, "/start/propertyThatDoesntExist", Arrays.asList("newValue"), PropertyType.TYPENAME_STRING, false);
304   
305    // THEN
306  1 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
307    }
308   
309    // DELETE
310   
 
311  1 toggle @Test
312    public void testDeleteReturnsNotFoundForNonExistingProperty() throws RepositoryException {
313   
314    // WHEN
315  1 Response response = endpoint.deleteProperty(RepositoryConstants.WEBSITE, "/start/nonExistingProperty");
316   
317    // THEN
318  1 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
319    }
320   
 
321  1 toggle @Test
322    public void testDeleteProperty() throws RepositoryException {
323   
324    // WHEN
325  1 Response response = endpoint.deleteProperty(RepositoryConstants.WEBSITE, "/start/property");
326   
327    // THEN
328  1 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
329  1 assertFalse(session.propertyExists("/start/property"));
330    }
331    }