Clover icon

Magnolia REST Services 1.1

  1. Project Clover database Thu Aug 13 2015 19:10:59 CEST
  2. Package info.magnolia.rest.service.node.v1

File RepositoryMarshallerTest.java

 

Code metrics

2
181
18
1
420
263
19
0.1
10.06
18
1.06

Classes

Class Line # Actions
RepositoryMarshallerTest 66 181 0% 19 1
0.9950248699.5%
 

Contributing tests

This file is covered by 17 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.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   
169    // WHEN
170  1 final RepositoryNode result = marshaller.marshallNode(node, 1, Collections.<String>emptyList(), true);
171   
172    // THEN
173  1 assertEquals(rootNodeName, result.getName());
174  1 assertEquals(2, result.getProperties().size());
175  1 assertHasProperty(result, "first");
176  1 assertHasProperty(result, "second");
177  1 assertEquals(1, result.getNodes().size());
178  1 assertEquals(subNodeName, result.getNodes().get(0).getName());
179  1 assertEquals(2, result.getNodes().get(0).getProperties().size());
180    }
181   
 
182  1 toggle @Test
183    public void testMarshallNodeWithExcludeNodeTypesSpecified() throws Exception {
184    // GIVEN
185  1 final String rootNodeName = "root";
186  1 final Node node = new MockNode(rootNodeName);
187  1 node.setProperty("first", "1");
188  1 node.setProperty("second", "2");
189  1 final String subNodeName = "subNode";
190  1 node.addNode(subNodeName + "1", NodeTypes.Area.NAME);
191  1 final Node subNode2 = node.addNode(subNodeName + "2");
192  1 subNode2.setProperty("first", "1");
193  1 subNode2.setProperty("second", "2");
194   
195  1 final List<String> excludeNodeTypes = new ArrayList<String>();
196  1 excludeNodeTypes.add(NodeTypes.Area.NAME);
197   
198    // WHEN
199  1 final RepositoryNode result = marshaller.marshallNode(node, 1, excludeNodeTypes, true);
200   
201    // THEN
202  1 assertEquals(rootNodeName, result.getName());
203  1 assertEquals(2, result.getProperties().size());
204  1 assertHasProperty(result, "first");
205  1 assertHasProperty(result, "second");
206  1 assertEquals(1, result.getNodes().size());
207  1 assertEquals(subNodeName + "2", result.getNodes().get(0).getName());
208  1 assertEquals(2, result.getNodes().get(0).getProperties().size());
209  1 assertHasProperty(result.getNodes().get(0), "first");
210  1 assertHasProperty(result.getNodes().get(0), "second");
211    }
212   
 
213  1 toggle @Test
214    public void testUnmarshallProperties() throws RepositoryException {
215    // GIVEN
216  1 ArrayList<RepositoryProperty> properties = new ArrayList<RepositoryProperty>();
217  1 RepositoryProperty property = new RepositoryProperty();
218  1 property.setName("first");
219  1 property.setType(PropertyType.TYPENAME_STRING);
220  1 property.setValues(Collections.singletonList("firstValue"));
221  1 properties.add(property);
222   
223  1 MockSession session = new MockSession("website");
224  1 final Node node = session.getRootNode().addNode("node");
225   
226    // WHEN
227  1 marshaller.unmarshallProperties(node, properties);
228   
229    // THEN
230  1 assertTrue(node.hasProperty("first"));
231  1 assertEquals("firstValue", node.getProperty("first").getString());
232    }
233   
 
234  1 toggle @Test(expected = IllegalArgumentException.class)
235    public void testUnmarshallPropertiesFailsWhenValuesMissing() throws RepositoryException {
236    // GIVEN
237  1 ArrayList<RepositoryProperty> properties = new ArrayList<RepositoryProperty>();
238  1 RepositoryProperty property = new RepositoryProperty();
239  1 property.setName("first");
240  1 property.setType(PropertyType.TYPENAME_STRING);
241  1 properties.add(property);
242  1 MockSession session = new MockSession("website");
243   
244  1 final Node node = session.getRootNode().addNode("node");
245   
246    // WHEN
247  1 marshaller.unmarshallProperties(node, properties);
248    }
249   
 
250  1 toggle @Test(expected = IllegalArgumentException.class)
251    public void testUnmarshallPropertiesFailsWhenMultipleValuesOnNonMultiValueProperty() throws RepositoryException {
252    // GIVEN
253  1 ArrayList<RepositoryProperty> properties = new ArrayList<RepositoryProperty>();
254  1 RepositoryProperty property = new RepositoryProperty();
255  1 property.setName("first");
256  1 property.setType(PropertyType.TYPENAME_STRING);
257  1 property.setValues(Arrays.asList("firstValue", "secondValue"));
258  1 properties.add(property);
259  1 MockSession session = new MockSession("website");
260   
261  1 final Node node = session.getRootNode().addNode("node");
262   
263    // WHEN
264  1 marshaller.unmarshallProperties(node, properties);
265    }
266   
 
267  1 toggle @Test
268    public void testUnmarshallPropertiesCanStoreMultiValueProperty() throws RepositoryException {
269    // GIVEN
270  1 ArrayList<RepositoryProperty> properties = new ArrayList<RepositoryProperty>();
271  1 RepositoryProperty property = new RepositoryProperty();
272  1 property.setName("first");
273  1 property.setType(PropertyType.TYPENAME_STRING);
274  1 property.setMultiple(true);
275  1 property.setValues(Arrays.asList("firstValue", "secondValue"));
276  1 properties.add(property);
277  1 MockSession session = new MockSession("website");
278   
279  1 final Node node = session.getRootNode().addNode("node");
280   
281    // WHEN
282  1 marshaller.unmarshallProperties(node, properties);
283   
284    // THEN
285  1 assertTrue(node.hasProperty("first"));
286  1 assertTrue(node.getProperty("first").isMultiple());
287  1 assertEquals("firstValue", node.getProperty("first").getValues()[0].getString());
288  1 assertEquals("secondValue", node.getProperty("first").getValues()[1].getString());
289    }
290   
 
291  1 toggle @Test
292    public void testUnmarshallPropertiesCanStoreMultiValuePropertyWithJustOneValue() throws RepositoryException {
293    // GIVEN
294  1 ArrayList<RepositoryProperty> properties = new ArrayList<RepositoryProperty>();
295  1 RepositoryProperty property = new RepositoryProperty();
296  1 property.setName("first");
297  1 property.setType(PropertyType.TYPENAME_STRING);
298  1 property.setMultiple(true);
299  1 property.setValues(Arrays.asList("firstValue"));
300  1 properties.add(property);
301  1 MockSession session = new MockSession("website");
302   
303  1 final Node node = session.getRootNode().addNode("node");
304   
305    // WHEN
306  1 marshaller.unmarshallProperties(node, properties);
307   
308    // THEN
309  1 assertTrue(node.hasProperty("first"));
310  1 assertTrue(node.getProperty("first").isMultiple());
311  1 assertEquals("firstValue", node.getProperty("first").getValues()[0].getString());
312    }
313   
 
314  1 toggle @Test
315    public void testGetValueByTypeForBinary() throws RepositoryException, IOException {
316    // GIVEN
317  1 final MockSession session = new MockSession("website");
318  1 final ValueFactory valueFactory = session.getValueFactory();
319   
320  1 final String valueEncoded = new String(Base64.encodeBase64("test".getBytes()));
321   
322    // WHEN
323  1 Value value = marshaller.getValueByType(PropertyType.BINARY, valueEncoded, valueFactory);
324   
325    // THEN
326  1 assertEquals(PropertyType.BINARY, value.getType());
327  1 assertEquals("test", new String(IOUtils.toByteArray(value.getBinary().getStream())));
328    }
329   
 
330  1 toggle @Test
331    public void testGetValueByTypeForString() throws RepositoryException, IOException {
332    // GIVEN
333  1 final MockSession session = new MockSession("website");
334  1 final ValueFactory valueFactory = session.getValueFactory();
335   
336  1 final String stringValue = "test";
337   
338    // WHEN
339  1 Value value = marshaller.getValueByType(PropertyType.STRING, stringValue, valueFactory);
340   
341    // THEN
342  1 assertEquals(PropertyType.STRING, value.getType());
343  1 assertEquals("test", value.getString());
344    }
345   
 
346  1 toggle @Test
347    public void testGetValueByTypeForOtherTypes() throws RepositoryException, IOException, ParseException {
348    // GIVEN
349  1 final MockSession session = new MockSession("website");
350  1 final ValueFactory valueFactory = session.getValueFactory();
351  1 final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
352   
353  1 final Long aLong = 1L;
354  1 final Boolean aBoolean = true;
355  1 final String aDateString = "2014-01-13";
356  1 Calendar cal = Calendar.getInstance();
357  1 cal.setTime(dateFormat.parse(aDateString));
358   
359    // WHEN
360  1 Value valueLong = marshaller.getValueByType(PropertyType.LONG, aLong.toString(), valueFactory);
361  1 Value valueBoolean = marshaller.getValueByType(PropertyType.BOOLEAN, aBoolean.toString(), valueFactory);
362  1 Value valueDate = marshaller.getValueByType(PropertyType.DATE, aDateString, valueFactory);
363   
364    // THEN
365    // Long
366  1 assertEquals(PropertyType.LONG, valueLong.getType());
367  1 assertEquals((long)aLong, valueLong.getLong());
368    // Boolean
369  1 assertEquals(PropertyType.BOOLEAN, valueBoolean.getType());
370  1 assertEquals(aBoolean, valueBoolean.getBoolean());
371    // Date
372  1 assertEquals(PropertyType.DATE, valueDate.getType());
373  1 assertEquals(cal.get(Calendar.YEAR), valueDate.getDate().get(Calendar.YEAR));
374  1 assertEquals(cal.get(Calendar.MONTH), valueDate.getDate().get(Calendar.MONTH));
375  1 assertEquals(cal.get(Calendar.DAY_OF_MONTH), valueDate.getDate().get(Calendar.DAY_OF_MONTH));
376    }
377   
 
378  1 toggle @Test
379    public void testGetStringByValueDefault() throws RepositoryException {
380    // GIVEN
381  1 final MockSession session = new MockSession("website");
382  1 final ValueFactory valueFactory = session.getValueFactory();
383   
384  1 final String theString = "testString";
385  1 final Value value = valueFactory.createValue(theString);
386   
387    // WHEN
388  1 String stringValue = marshaller.getStringByValue(value);
389   
390    // THEN
391  1 assertEquals(theString, stringValue);
392    }
393   
 
394  1 toggle @Test
395    public void testGetStringByValueBinary() throws RepositoryException {
396    // GIVEN
397  1 final MockSession session = new MockSession("website");
398  1 final ValueFactory valueFactory = session.getValueFactory();
399   
400  1 final String theString = "0012DF4F6C";
401  1 final String theStringEncoded = new String(Base64.encodeBase64(theString.getBytes()));
402  1 final Value value = valueFactory.createValue(theString, PropertyType.BINARY);
403   
404    // WHEN
405  1 String stringValue = marshaller.getStringByValue(value);
406   
407    // THEN
408  1 assertEquals(theStringEncoded, stringValue);
409    }
410   
 
411  12 toggle private void assertHasProperty(RepositoryNode node, String propertyName) {
412  12 for (RepositoryProperty property : node.getProperties()) {
413  18 if (property.getName().equals(propertyName)) {
414  12 return;
415    }
416    }
417  0 fail("Node does not have expected property " + propertyName);
418    }
419   
420    }