1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
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 |
|
@link |
65 |
|
|
|
|
| 99.5% |
Uncovered Elements: 1 (207) |
Complexity: 19 |
Complexity Density: 0.1 |
|
66 |
|
public class RepositoryMarshallerTest { |
67 |
|
|
68 |
|
private final RepositoryMarshaller marshaller = new RepositoryMarshaller(); |
69 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
70 |
1 |
@Test... |
71 |
|
public void testMarshallNode() throws Exception { |
72 |
|
|
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 |
|
|
79 |
1 |
final RepositoryNode result = marshaller.marshallNode(node); |
80 |
|
|
81 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
88 |
1 |
@Test... |
89 |
|
public void testMarshallNodeExcludingMetadata() throws Exception { |
90 |
|
|
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 |
|
|
96 |
1 |
final RepositoryNode result = marshaller.marshallNode(node, 0, null, false); |
97 |
|
|
98 |
|
|
99 |
1 |
assertEquals(rootNodeName, result.getName()); |
100 |
1 |
assertTrue(result.getProperties().isEmpty()); |
101 |
|
} |
102 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
103 |
1 |
@Test... |
104 |
|
public void testMarshallNodeIncludingMetadata() throws Exception { |
105 |
|
|
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 |
|
|
111 |
1 |
final RepositoryNode result = marshaller.marshallNode(node, 0, null, true); |
112 |
|
|
113 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
120 |
1 |
@Test... |
121 |
|
public void testMarshallNodeWithNullExcludeString() throws Exception { |
122 |
|
|
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 |
|
|
129 |
1 |
final RepositoryNode result = marshaller.marshallNode(node, 0, null, false); |
130 |
|
|
131 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
138 |
1 |
@Test... |
139 |
|
public void testMarshallNodeWithMultiValuedProperty() throws Exception { |
140 |
|
|
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 |
|
|
147 |
1 |
final RepositoryNode result = marshaller.marshallNode(node, 0, Collections.<String>emptyList(), true); |
148 |
|
|
149 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (22) |
Complexity: 1 |
Complexity Density: 0.05 |
1PASS
|
|
157 |
1 |
@Test... |
158 |
|
public void testMarshallNodeWithDepthSpecified() throws Exception { |
159 |
|
|
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 |
|
|
175 |
1 |
final RepositoryNode result = marshaller.marshallNode(node, 2, Collections.<String>emptyList(), true); |
176 |
|
|
177 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (21) |
Complexity: 1 |
Complexity Density: 0.05 |
1PASS
|
|
189 |
1 |
@Test... |
190 |
|
public void testMarshallNodeWithExcludeNodeTypesSpecified() throws Exception { |
191 |
|
|
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 |
|
|
206 |
1 |
final RepositoryNode result = marshaller.marshallNode(node, 1, excludeNodeTypes, true); |
207 |
|
|
208 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
220 |
1 |
@Test... |
221 |
|
public void testUnmarshallProperties() throws RepositoryException { |
222 |
|
|
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 |
|
|
234 |
1 |
marshaller.unmarshallProperties(node, properties); |
235 |
|
|
236 |
|
|
237 |
1 |
assertTrue(node.hasProperty("first")); |
238 |
1 |
assertEquals("firstValue", node.getProperty("first").getString()); |
239 |
|
} |
240 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
241 |
1 |
@Test(expected = IllegalArgumentException.class)... |
242 |
|
public void testUnmarshallPropertiesFailsWhenValuesMissing() throws RepositoryException { |
243 |
|
|
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 |
|
|
254 |
1 |
marshaller.unmarshallProperties(node, properties); |
255 |
|
} |
256 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
257 |
1 |
@Test(expected = IllegalArgumentException.class)... |
258 |
|
public void testUnmarshallPropertiesFailsWhenMultipleValuesOnNonMultiValueProperty() throws RepositoryException { |
259 |
|
|
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 |
|
|
271 |
1 |
marshaller.unmarshallProperties(node, properties); |
272 |
|
} |
273 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 1 |
Complexity Density: 0.07 |
1PASS
|
|
274 |
1 |
@Test... |
275 |
|
public void testUnmarshallPropertiesCanStoreMultiValueProperty() throws RepositoryException { |
276 |
|
|
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 |
|
|
289 |
1 |
marshaller.unmarshallProperties(node, properties); |
290 |
|
|
291 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
298 |
1 |
@Test... |
299 |
|
public void testUnmarshallPropertiesCanStoreMultiValuePropertyWithJustOneValue() throws RepositoryException { |
300 |
|
|
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 |
|
|
313 |
1 |
marshaller.unmarshallProperties(node, properties); |
314 |
|
|
315 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
321 |
1 |
@Test... |
322 |
|
public void testGetValueByTypeForBinary() throws RepositoryException, IOException { |
323 |
|
|
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 |
|
|
330 |
1 |
Value value = marshaller.getValueByType(PropertyType.BINARY, valueEncoded, valueFactory); |
331 |
|
|
332 |
|
|
333 |
1 |
assertEquals(PropertyType.BINARY, value.getType()); |
334 |
1 |
assertEquals("test", new String(IOUtils.toByteArray(value.getBinary().getStream()))); |
335 |
|
} |
336 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
337 |
1 |
@Test... |
338 |
|
public void testGetValueByTypeForString() throws RepositoryException, IOException { |
339 |
|
|
340 |
1 |
final MockSession session = new MockSession("website"); |
341 |
1 |
final ValueFactory valueFactory = session.getValueFactory(); |
342 |
|
|
343 |
1 |
final String stringValue = "test"; |
344 |
|
|
345 |
|
|
346 |
1 |
Value value = marshaller.getValueByType(PropertyType.STRING, stringValue, valueFactory); |
347 |
|
|
348 |
|
|
349 |
1 |
assertEquals(PropertyType.STRING, value.getType()); |
350 |
1 |
assertEquals("test", value.getString()); |
351 |
|
} |
352 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (19) |
Complexity: 1 |
Complexity Density: 0.05 |
1PASS
|
|
353 |
1 |
@Test... |
354 |
|
public void testGetValueByTypeForOtherTypes() throws RepositoryException, IOException, ParseException { |
355 |
|
|
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 |
|
|
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 |
|
|
372 |
|
|
373 |
1 |
assertEquals(PropertyType.LONG, valueLong.getType()); |
374 |
1 |
assertEquals((long)aLong, valueLong.getLong()); |
375 |
|
|
376 |
1 |
assertEquals(PropertyType.BOOLEAN, valueBoolean.getType()); |
377 |
1 |
assertEquals(aBoolean, valueBoolean.getBoolean()); |
378 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
385 |
1 |
@Test... |
386 |
|
public void testGetStringByValueDefault() throws RepositoryException { |
387 |
|
|
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 |
|
|
395 |
1 |
String stringValue = marshaller.getStringByValue(value); |
396 |
|
|
397 |
|
|
398 |
1 |
assertEquals(theString, stringValue); |
399 |
|
} |
400 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
401 |
1 |
@Test... |
402 |
|
public void testGetStringByValueBinary() throws RepositoryException { |
403 |
|
|
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 |
|
|
412 |
1 |
String stringValue = marshaller.getStringByValue(value); |
413 |
|
|
414 |
|
|
415 |
1 |
assertEquals(theStringEncoded, stringValue); |
416 |
|
} |
417 |
|
|
|
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
418 |
12 |
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 |
|
} |