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 NodesResultWriterTest.java

 

Code metrics

0
37
3
1
143
86
3
0.08
12.33
3
1

Classes

Class Line # Actions
NodesResultWriterTest 66 37 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.Collections;
50    import java.util.List;
51    import java.util.Map;
52   
53    import javax.jcr.Node;
54    import javax.jcr.NodeIterator;
55    import javax.jcr.Session;
56    import javax.jcr.Workspace;
57    import javax.ws.rs.ext.Providers;
58   
59    import org.apache.jackrabbit.commons.iterator.NodeIteratorAdapter;
60    import org.junit.Before;
61    import org.junit.Test;
62   
63    import com.fasterxml.jackson.core.type.TypeReference;
64    import com.fasterxml.jackson.databind.ObjectMapper;
65   
 
66    public class NodesResultWriterTest extends RepositoryTestCase {
67   
68    private ByteArrayOutputStream baos;
69    private ObjectMapper mapper;
70    private NodesResultWriter nodesResultWriter;
71    private Workspace workspace;
72   
 
73  2 toggle @Before
74    public void setUp() throws Exception {
75  2 super.setUp();
76  2 Session 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 NodeIteratorWriter nodeIteratorWriter = new NodeIteratorWriter();
84  2 NodeWriter nodeWriter = new NodeWriter();
85  2 Providers providers = mock(Providers.class);
86  2 when(providers.getMessageBodyWriter(Node.class, null, null, null)).thenReturn(nodeWriter);
87  2 when(providers.getMessageBodyWriter(NodeIterator.class, null, null, null)).thenReturn(nodeIteratorWriter);
88  2 nodesResultWriter = new NodesResultWriter();
89  2 nodesResultWriter.setProviders(providers);
90  2 nodeIteratorWriter.setProviders(providers);
91    }
92   
 
93  1 toggle @Test
94    public void listNodesWithAdditionalMetaData() throws Exception {
95    // GIVEN
96  1 NodeIterator nodeIterator = QueryBuilder.inWorkspace(workspace)
97    .nodeTypes(Collections.singletonList(NodeTypes.Page.NAME))
98    .build()
99    .execute()
100    .getNodes();
101  1 List<Node> nodes = NodeUtil.asList(NodeUtil.asIterable(nodeIterator));
102   
103    // WHEN
104  1 NodesResult nodesResult = new NodesResult(new NodeIteratorAdapter(nodes.iterator()), 10);
105  1 nodesResultWriter.writeTo(nodesResult, null, null, null, null, null, baos);
106  1 Map<String, Object> result = mapper.readValue(baos.toString(), new TypeReference<Object>(){});
107    // assertEquals(result.get("total"), 10);
108  1 List<Object> jsonNodeList = (List<Object>) result.get("results");
109   
110    // THEN
111  1 assertNotNull(nodeIterator);
112  1 assertThat(nodes.size(), is(jsonNodeList.size()));
113  1 assertThat(nodes.get(0).getProperty("title").getString(), is(((Map<String, String>)jsonNodeList.get(0)).get("title")));
114  1 assertThat(nodes.get(1).getProperty("title").getString(), is(((Map<String, String>)jsonNodeList.get(1)).get("title")));
115  1 assertThat(nodes.get(2).getProperty("title").getString(), is(((Map<String, String>)jsonNodeList.get(2)).get("title")));
116    }
117   
 
118  1 toggle @Test
119    public void listNodesAndSortByLastModifiedDate() throws Exception {
120    // GIVEN
121  1 NodeIterator nodeIterator = QueryBuilder.inWorkspace(workspace)
122    .nodeTypes(Arrays.asList(NodeTypes.Page.NAME))
123    .orderBy(Collections.singletonList(NodeTypes.LastModified.NAME + " desc"))
124    .build()
125    .execute()
126    .getNodes();
127  1 List<Node> nodes = NodeUtil.asList(NodeUtil.asIterable(nodeIterator));
128   
129    // WHEN
130  1 NodesResult nodesResult = new NodesResult(new NodeIteratorAdapter(nodes.iterator()), 10);
131  1 nodesResultWriter.writeTo(nodesResult, null, null, null, null, null, baos);
132  1 Map<String, Object> result = mapper.readValue(baos.toString(), new TypeReference<Object>(){});
133    // assertEquals(result.get("total"), 10);
134  1 List<Object> jsonNodeList = (List<Object>) result.get("results");
135   
136    // THEN
137  1 assertNotNull(jsonNodeList);
138  1 assertThat(nodes.size(), is(jsonNodeList.size()));
139  1 assertThat(nodes.get(0).getProperty("title").getString(), is(((Map<String, String>)jsonNodeList.get(0)).get("title")));
140  1 assertThat(nodes.get(1).getProperty("title").getString(), is(((Map<String, String>)jsonNodeList.get(1)).get("title")));
141  1 assertThat(nodes.get(2).getProperty("title").getString(), is(((Map<String, String>)jsonNodeList.get(2)).get("title")));
142    }
143    }