Clover icon

Magnolia REST Content Delivery 2.0-rc1

  1. Project Clover database Mon Oct 30 2017 16:36:57 CET
  2. Package info.magnolia.rest.delivery.jcr

File NodeIteratorWriterTest.java

 

Code metrics

0
30
3
1
134
79
3
0.1
10
3
1

Classes

Class Line # Actions
NodeIteratorWriterTest 65 30 0% 3 0
1.0100%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    /**
2    * This file Copyright (c) 2017 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;
35   
36    import static org.hamcrest.CoreMatchers.is;
37    import static org.junit.Assert.*;
38    import static org.mockito.Mockito.*;
39   
40    import info.magnolia.context.MgnlContext;
41    import info.magnolia.jcr.util.NodeTypes;
42    import info.magnolia.jcr.util.NodeUtil;
43    import info.magnolia.jcr.util.PropertiesImportExport;
44    import info.magnolia.repository.RepositoryConstants;
45    import info.magnolia.test.RepositoryTestCase;
46   
47    import java.io.ByteArrayOutputStream;
48    import java.util.Arrays;
49    import java.util.List;
50    import java.util.Map;
51   
52    import javax.jcr.Node;
53    import javax.jcr.NodeIterator;
54    import javax.jcr.Session;
55    import javax.jcr.Workspace;
56    import javax.ws.rs.ext.Providers;
57   
58    import org.apache.jackrabbit.commons.iterator.NodeIteratorAdapter;
59    import org.junit.Before;
60    import org.junit.Test;
61   
62    import com.fasterxml.jackson.core.type.TypeReference;
63    import com.fasterxml.jackson.databind.ObjectMapper;
64   
 
65    public class NodeIteratorWriterTest extends RepositoryTestCase {
66   
67    private Session session;
68    private ByteArrayOutputStream baos;
69    private ObjectMapper mapper;
70    private NodeIteratorWriter nodeIteratorWriter;
71    private Workspace workspace;
72   
 
73  2 toggle @Before
74    public void setUp() throws Exception {
75  2 super.setUp();
76  2 session = MgnlContext.getJCRSession(RepositoryConstants.WEBSITE);
77  2 new PropertiesImportExport().createNodes(session.getRootNode(), getClass().getClassLoader().getResourceAsStream("travel-test-data.properties"));
78  2 session.save();
79   
80  2 baos = new ByteArrayOutputStream();
81  2 mapper = new ObjectMapper();
82  2 workspace = session.getWorkspace();
83  2 NodeWriter nodeWriter = new NodeWriter();
84  2 Providers providers = mock(Providers.class);
85  2 when(providers.getMessageBodyWriter(Node.class, null, null, null)).thenReturn(nodeWriter);
86  2 nodeIteratorWriter = new NodeIteratorWriter();
87  2 nodeIteratorWriter.setProviders(providers);
88    }
89   
 
90  1 toggle @Test
91    public void listNodes() throws Exception {
92    // GIVEN
93  1 NodeIterator nodeIterator = QueryBuilder.inWorkspace(workspace)
94    .nodeTypes(Arrays.asList(NodeTypes.Page.NAME))
95    .build()
96    .execute()
97    .getNodes();
98  1 List<Node> nodes = NodeUtil.asList(NodeUtil.asIterable(nodeIterator));
99   
100    // WHEN
101  1 nodeIteratorWriter.writeTo(new NodeIteratorAdapter(nodes.iterator()), null, null, null, null, null, baos);
102  1 List<Object> jsonNodeList = mapper.readValue(baos.toString(), new TypeReference<List<Object>>() {});
103   
104    // THEN
105  1 assertNotNull(nodeIterator);
106  1 assertThat(nodes.size(), is(jsonNodeList.size()));
107  1 assertThat(nodes.get(0).getProperty("title").getString(), is(((Map<String, String>)jsonNodeList.get(0)).get("title")));
108  1 assertThat(nodes.get(1).getProperty("title").getString(), is(((Map<String, String>)jsonNodeList.get(1)).get("title")));
109  1 assertThat(nodes.get(2).getProperty("title").getString(), is(((Map<String, String>)jsonNodeList.get(2)).get("title")));
110    }
111   
 
112  1 toggle @Test
113    public void listNodesAndSortByLastModifiedDate() throws Exception {
114    // GIVEN
115  1 NodeIterator nodeIterator = QueryBuilder.inWorkspace(workspace)
116    .nodeTypes(Arrays.asList(NodeTypes.Page.NAME))
117    .orderBy(Arrays.asList(NodeTypes.LastModified.NAME + " desc"))
118    .build()
119    .execute()
120    .getNodes();
121  1 List<Node> nodes = NodeUtil.asList(NodeUtil.asIterable(nodeIterator));
122   
123    // WHEN
124  1 nodeIteratorWriter.writeTo(new NodeIteratorAdapter(nodes.iterator()), null, null, null, null, null, baos);
125  1 List<Object> jsonNodeList = mapper.readValue(baos.toString(), new TypeReference<List<Object>>() {});
126   
127    // THEN
128  1 assertNotNull(jsonNodeList);
129  1 assertThat(nodes.size(), is(jsonNodeList.size()));
130  1 assertThat(nodes.get(0).getProperty("title").getString(), is(((Map<String, String>)jsonNodeList.get(0)).get("title")));
131  1 assertThat(nodes.get(1).getProperty("title").getString(), is(((Map<String, String>)jsonNodeList.get(1)).get("title")));
132  1 assertThat(nodes.get(2).getProperty("title").getString(), is(((Map<String, String>)jsonNodeList.get(2)).get("title")));
133    }
134    }