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.node.v1

File RepositoryMarshallerTest.java

 

Code metrics

2
187
18
1
427
269
19
0.1
10.39
18
1.06

Classes

Class Line # Actions
RepositoryMarshallerTest 66 187 0% 19 1
0.995169199.5%
 

Contributing tests

This file is covered by 17 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.node.v1;
35   
36    import static org.junit.Assert.*;
37    import static org.junit.Assert.assertEquals;
38   
39    import info.magnolia.jcr.util.NodeTypes;
40    import info.magnolia.test.mock.jcr.MockNode;
41    import info.magnolia.test.mock.jcr.MockSession;
42   
43    import java.io.IOException;
44    import java.text.DateFormat;
45    import java.text.ParseException;
46    import java.text.SimpleDateFormat;
47    import java.util.ArrayList;
48    import java.util.Arrays;
49    import java.util.Calendar;
50    import java.util.Collections;
51    import java.util.List;
52   
53    import javax.jcr.Node;
54    import javax.jcr.PropertyType;
55    import javax.jcr.RepositoryException;
56    import javax.jcr.Value;
57    import javax.jcr.ValueFactory;
58   
59    import org.apache.commons.codec.binary.Base64;
60    import org.apache.commons.io.IOUtils;
61    import org.junit.Test;
62   
63    /**
64    * Test case for {@link RepositoryMarshaller}.
65    */
 
66    public class RepositoryMarshallerTest {
67   
68    private final RepositoryMarshaller marshaller = new RepositoryMarshaller();
69   
 
70  1 toggle @Test
71    public void testMarshallNode() throws Exception {
72    // GIVEN
73  1 final String rootNodeName = "root";
74  1 final Node node = new MockNode(rootNodeName);
75  1 node.setProperty("first", "1");
76  1 node.setProperty("second", "2");
77   
78    // WHEN
79  1 final RepositoryNode result = marshaller.marshallNode(node);
80   
81    // THEN
82  1 assertEquals(rootNodeName, result.getName());
83  1 assertEquals(2, result.getProperties().size());
84  1 assertHasProperty(result, "first");
85  1 assertHasProperty(result, "second");
86    }
87   
 
88  1 toggle @Test
89    public void testMarshallNodeExcludingMetadata() throws Exception {
90    // GIVEN
91  1 final String rootNodeName = "root";
92  1 final Node node = new MockNode(rootNodeName);
93  1 NodeTypes.LastModified.update(node, "username", Calendar.getInstance());
94   
95    // WHEN
96  1 final RepositoryNode result = marshaller.marshallNode(node, 0, null, false);
97   
98    // THEN
99  1 assertEquals(rootNodeName, result.getName());
100  1 assertTrue(result.getProperties().isEmpty());
101    }
102   
 
103  1 toggle @Test
104    public void testMarshallNodeIncludingMetadata() throws Exception {
105    // GIVEN
106  1 final String rootNodeName = "root";
107  1 final Node node = new MockNode(rootNodeName);
108  1 NodeTypes.LastModified.update(node, "username", Calendar.getInstance());
109   
110    // WHEN
111  1 final RepositoryNode result = marshaller.marshallNode(node, 0, null, true);
112   
113    // THEN
114  1 assertEquals(rootNodeName, result.getName());
115  1 assertEquals(2, result.getProperties().size());
116  1 assertHasProperty(result, NodeTypes.LastModified.LAST_MODIFIED);
117  1 assertHasProperty(result, NodeTypes.LastModified.LAST_MODIFIED_BY);
118    }
119   
 
120  1 toggle @Test
121    public void testMarshallNodeWithNullExcludeString() throws Exception {
122    // GIVEN
123  1 final String rootNodeName = "root";
124  1 final Node node = new MockNode(rootNodeName);
125  1 node.setProperty("first", "1");
126  1 node.setProperty("second", "2");
127   
128    // WHEN
129  1 final RepositoryNode result = marshaller.marshallNode(node, 0, null, false);
130   
131    // THEN
132  1 assertEquals(rootNodeName, result.getName());
133  1 assertEquals(2, result.getProperties().size());
134  1 assertHasProperty(result, "first");
135  1 assertHasProperty(result, "second");
136    }
137   
 
138  1 toggle @Test
139    public void testMarshallNodeWithMultiValuedProperty() throws Exception {
140    // GIVEN
141  1 final String rootNodeName = "root";
142  1 final Node node = new MockNode(rootNodeName);
143  1 final String[] multiValues = new String[]{"one", "two", "three"};
144  1 node.setProperty("first", multiValues);
145   
146    // WHEN
147  1 final RepositoryNode result = marshaller.marshallNode(node, 0, Collections.<String>emptyList(), true);
148   
149    // THEN
150  1 assertEquals(rootNodeName, result.getName());
151  1 assertEquals(1, result.getProperties().size());
152  1 for (String value : multiValues) {
153  3 assertTrue(result.getProperties().get(0).getValues().contains(value));
154    }
155    }
156   
 
157  1 toggle @Test
158    public void testMarshallNodeWithDepthSpecified() throws Exception {
159    // GIVEN
160  1 final String rootNodeName = "root";
161  1 final Node node = new MockNode(rootNodeName);
162  1 node.setProperty("first", "1");
163  1 node.setProperty("second", "2");
164  1 final String subNodeName = "subNode";
165  1 final Node subNode = node.addNode(subNodeName);
166  1 subNode.setProperty("first", "1");
167  1 subNode.setProperty("second", "2");
168  1 final String subNodeName2 = "subNode2";
169  1 final Node subNode2 = node.addNode(subNodeName2);
170  1 final String subSubNodeName = "subSubNode";
171  1 subNode2.addNode(subSubNodeName);
172   
173   
174    // WHEN
175  1 final RepositoryNode result = marshaller.marshallNode(node, 2, Collections.<String>emptyList(), true);
176   
177    // THEN
178  1 assertEquals(rootNodeName, result.getName());
179  1 assertEquals(2, result.getProperties().size());
180  1 assertHasProperty(result, "first");
181  1 assertHasProperty(result, "second");
182  1 assertEquals(2, result.getNodes().size());
183  1 assertEquals(subNodeName, result.getNodes().get(0).getName());
184  1 assertEquals(2, result.getNodes().get(0).getProperties().size());
185  1 assertEquals(subNodeName2, result.getNodes().get(1).getName());
186  1 assertNotEquals(null, result.getNodes().get(1).getNodes());
187    }
188   
 
189  1 toggle @Test
190    public void testMarshallNodeWithExcludeNodeTypesSpecified() throws Exception {
191    // GIVEN
192  1 final String rootNodeName = "root";
193  1 final Node node = new MockNode(rootNodeName);
194  1 node.setProperty("first", "1");
195  1 node.setProperty("second", "2");
196  1 final String subNodeName = "subNode";
197  1 node.addNode(subNodeName + "1", NodeTypes.Area.NAME);
198  1 final Node subNode2 = node.addNode(subNodeName + "2");
199  1 subNode2.setProperty("first", "1");
200  1 subNode2.setProperty("second", "2");
201   
202  1 final List<String> excludeNodeTypes = new ArrayList<String>();
203  1 excludeNodeTypes.add(NodeTypes.Area.NAME);
204   
205    // WHEN
206  1 final RepositoryNode result = marshaller.marshallNode(node, 1, excludeNodeTypes, true);
207   
208    // THEN
209  1 assertEquals(rootNodeName, result.getName());
210  1 assertEquals(2, result.getProperties().size());
211  1 assertHasProperty(result, "first");
212  1 assertHasProperty(result, "second");
213  1 assertEquals(1, result.getNodes().size());
214  1 assertEquals(subNodeName + "2", result.getNodes().get(0).getName());
215  1 assertEquals(2, result.getNodes().get(0).getProperties().size());
216  1 assertHasProperty(result.getNodes().get(0), "first");
217  1 assertHasProperty(result.getNodes().get(0), "second");
218    }
219   
 
220  1 toggle @Test
221    public void testUnmarshallProperties() throws RepositoryException {
222    // GIVEN
223  1 ArrayList<RepositoryProperty> properties = new ArrayList<RepositoryProperty>();
224  1 RepositoryProperty property = new RepositoryProperty();
225  1 property.setName("first");
226  1 property.setType(PropertyType.TYPENAME_STRING);
227  1 property.setValues(Collections.singletonList("firstValue"));
228  1 properties.add(property);
229   
230  1 MockSession session = new MockSession("website");
231  1 final Node node = session.getRootNode().addNode("node");
232   
233    // WHEN
234  1 marshaller.unmarshallProperties(node, properties);
235   
236    // THEN
237  1 assertTrue(node.hasProperty("first"));
238  1 assertEquals("firstValue", node.getProperty("first").getString());
239    }
240   
 
241  1 toggle @Test(expected = IllegalArgumentException.class)
242    public void testUnmarshallPropertiesFailsWhenValuesMissing() throws RepositoryException {
243    // GIVEN
244  1 ArrayList<RepositoryProperty> properties = new ArrayList<RepositoryProperty>();
245  1 RepositoryProperty property = new RepositoryProperty();
246  1 property.setName("first");
247  1 property.setType(PropertyType.TYPENAME_STRING);
248  1 properties.add(property);
249  1 MockSession session = new MockSession("website");
250   
251  1 final Node node = session.getRootNode().addNode("node");
252   
253    // WHEN
254  1 marshaller.unmarshallProperties(node, properties);
255    }
256   
 
257  1 toggle @Test(expected = IllegalArgumentException.class)
258    public void testUnmarshallPropertiesFailsWhenMultipleValuesOnNonMultiValueProperty() throws RepositoryException {
259    // GIVEN
260  1 ArrayList<RepositoryProperty> properties = new ArrayList<RepositoryProperty>();
261  1 RepositoryProperty property = new RepositoryProperty();
262  1 property.setName("first");
263  1 property.setType(PropertyType.TYPENAME_STRING);
264  1 property.setValues(Arrays.asList("firstValue", "secondValue"));
265  1 properties.add(property);
266  1 MockSession session = new MockSession("website");
267   
268  1 final Node node = session.getRootNode().addNode("node");
269   
270    // WHEN
271  1 marshaller.unmarshallProperties(node, properties);
272    }
273   
 
274  1 toggle @Test
275    public void testUnmarshallPropertiesCanStoreMultiValueProperty() throws RepositoryException {
276    // GIVEN
277  1 ArrayList<RepositoryProperty> properties = new ArrayList<RepositoryProperty>();
278  1 RepositoryProperty property = new RepositoryProperty();
279  1 property.setName("first");
280  1 property.setType(PropertyType.TYPENAME_STRING);
281  1 property.setMultiple(true);
282  1 property.setValues(Arrays.asList("firstValue", "secondValue"));
283  1 properties.add(property);
284  1 MockSession session = new MockSession("website");
285   
286  1 final Node node = session.getRootNode().addNode("node");
287   
288    // WHEN
289  1 marshaller.unmarshallProperties(node, properties);
290   
291    // THEN
292  1 assertTrue(node.hasProperty("first"));
293  1 assertTrue(node.getProperty("first").isMultiple());
294  1 assertEquals("firstValue", node.getProperty("first").getValues()[0].getString());
295  1 assertEquals("secondValue", node.getProperty("first").getValues()[1].getString());
296    }
297   
 
298  1 toggle @Test
299    public void testUnmarshallPropertiesCanStoreMultiValuePropertyWithJustOneValue() throws RepositoryException {
300    // GIVEN
301  1 ArrayList<RepositoryProperty> properties = new ArrayList<RepositoryProperty>();
302  1 RepositoryProperty property = new RepositoryProperty();
303  1 property.setName("first");
304  1 property.setType(PropertyType.TYPENAME_STRING);
305  1 property.setMultiple(true);
306  1 property.setValues(Arrays.asList("firstValue"));
307  1 properties.add(property);
308  1 MockSession session = new MockSession("website");
309   
310  1 final Node node = session.getRootNode().addNode("node");
311   
312    // WHEN
313  1 marshaller.unmarshallProperties(node, properties);
314   
315    // THEN
316  1 assertTrue(node.hasProperty("first"));
317  1 assertTrue(node.getProperty("first").isMultiple());
318  1 assertEquals("firstValue", node.getProperty("first").getValues()[0].getString());
319    }
320   
 
321  1 toggle @Test
322    public void testGetValueByTypeForBinary() throws RepositoryException, IOException {
323    // GIVEN
324  1 final MockSession session = new MockSession("website");
325  1 final ValueFactory valueFactory = session.getValueFactory();
326   
327  1 final String valueEncoded = new String(Base64.encodeBase64("test".getBytes()));
328   
329    // WHEN
330  1 Value value = marshaller.getValueByType(PropertyType.BINARY, valueEncoded, valueFactory);
331   
332    // THEN
333  1 assertEquals(PropertyType.BINARY, value.getType());
334  1 assertEquals("test", new String(IOUtils.toByteArray(value.getBinary().getStream())));
335    }
336   
 
337  1 toggle @Test
338    public void testGetValueByTypeForString() throws RepositoryException, IOException {
339    // GIVEN
340  1 final MockSession session = new MockSession("website");
341  1 final ValueFactory valueFactory = session.getValueFactory();
342   
343  1 final String stringValue = "test";
344   
345    // WHEN
346  1 Value value = marshaller.getValueByType(PropertyType.STRING, stringValue, valueFactory);
347   
348    // THEN
349  1 assertEquals(PropertyType.STRING, value.getType());
350  1 assertEquals("test", value.getString());
351    }
352   
 
353  1 toggle @Test
354    public void testGetValueByTypeForOtherTypes() throws RepositoryException, IOException, ParseException {
355    // GIVEN
356  1 final MockSession session = new MockSession("website");
357  1 final ValueFactory valueFactory = session.getValueFactory();
358  1 final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
359   
360  1 final Long aLong = 1L;
361  1 final Boolean aBoolean = true;
362  1 final String aDateString = "2014-01-13";
363  1 Calendar cal = Calendar.getInstance();
364  1 cal.setTime(dateFormat.parse(aDateString));
365   
366    // WHEN
367  1 Value valueLong = marshaller.getValueByType(PropertyType.LONG, aLong.toString(), valueFactory);
368  1 Value valueBoolean = marshaller.getValueByType(PropertyType.BOOLEAN, aBoolean.toString(), valueFactory);
369  1 Value valueDate = marshaller.getValueByType(PropertyType.DATE, aDateString, valueFactory);
370   
371    // THEN
372    // Long
373  1 assertEquals(PropertyType.LONG, valueLong.getType());
374  1 assertEquals((long)aLong, valueLong.getLong());
375    // Boolean
376  1 assertEquals(PropertyType.BOOLEAN, valueBoolean.getType());
377  1 assertEquals(aBoolean, valueBoolean.getBoolean());
378    // Date
379  1 assertEquals(PropertyType.DATE, valueDate.getType());
380  1 assertEquals(cal.get(Calendar.YEAR), valueDate.getDate().get(Calendar.YEAR));
381  1 assertEquals(cal.get(Calendar.MONTH), valueDate.getDate().get(Calendar.MONTH));
382  1 assertEquals(cal.get(Calendar.DAY_OF_MONTH), valueDate.getDate().get(Calendar.DAY_OF_MONTH));
383    }
384   
 
385  1 toggle @Test
386    public void testGetStringByValueDefault() throws RepositoryException {
387    // GIVEN
388  1 final MockSession session = new MockSession("website");
389  1 final ValueFactory valueFactory = session.getValueFactory();
390   
391  1 final String theString = "testString";
392  1 final Value value = valueFactory.createValue(theString);
393   
394    // WHEN
395  1 String stringValue = marshaller.getStringByValue(value);
396   
397    // THEN
398  1 assertEquals(theString, stringValue);
399    }
400   
 
401  1 toggle @Test
402    public void testGetStringByValueBinary() throws RepositoryException {
403    // GIVEN
404  1 final MockSession session = new MockSession("website");
405  1 final ValueFactory valueFactory = session.getValueFactory();
406   
407  1 final String theString = "0012DF4F6C";
408  1 final String theStringEncoded = new String(Base64.encodeBase64(theString.getBytes()));
409  1 final Value value = valueFactory.createValue(theString, PropertyType.BINARY);
410   
411    // WHEN
412  1 String stringValue = marshaller.getStringByValue(value);
413   
414    // THEN
415  1 assertEquals(theStringEncoded, stringValue);
416    }
417   
 
418  12 toggle private void assertHasProperty(RepositoryNode node, String propertyName) {
419  12 for (RepositoryProperty property : node.getProperties()) {
420  18 if (property.getName().equals(propertyName)) {
421  12 return;
422    }
423    }
424  0 fail("Node does not have expected property " + propertyName);
425    }
426   
427    }