Clover icon

Magnolia REST Content Delivery 2.1

  1. Project Clover database Fri Mar 16 2018 18:21:08 CET
  2. Package info.magnolia.rest.delivery.jcr

File NodesResultWriterTest.java

 

Code metrics

0
36
3
1
160
97
3
0.08
12
3
1

Classes

Class Line # Actions
NodesResultWriterTest 71 36 0% 3 0
1.0100%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    /**
2    * This file Copyright (c) 2017-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.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.rest.reference.ReferenceContext;
46    import info.magnolia.test.RepositoryTestCase;
47   
48    import java.io.ByteArrayOutputStream;
49    import java.util.Arrays;
50    import java.util.Collections;
51    import java.util.List;
52    import java.util.Map;
53   
54    import javax.jcr.Node;
55    import javax.jcr.NodeIterator;
56    import javax.jcr.Session;
57    import javax.jcr.Workspace;
58    import javax.ws.rs.core.MediaType;
59    import javax.ws.rs.ext.Providers;
60   
61    import org.apache.jackrabbit.commons.iterator.NodeIteratorAdapter;
62    import org.junit.Before;
63    import org.junit.Test;
64    import org.mockito.InjectMocks;
65    import org.mockito.Mock;
66    import org.mockito.MockitoAnnotations;
67   
68    import com.fasterxml.jackson.core.type.TypeReference;
69    import com.fasterxml.jackson.databind.ObjectMapper;
70   
 
71    public class NodesResultWriterTest extends RepositoryTestCase {
72   
73    private ByteArrayOutputStream baos;
74    private ObjectMapper mapper;
75    private Workspace workspace;
76   
77    @InjectMocks
78    private NodesResultWriter nodesResultWriter;
79   
80    @InjectMocks
81    private NodeIteratorWriter nodeIteratorWriter;
82   
83    @InjectMocks
84    private NodeWriter nodeWriter;
85   
86    @Mock
87    private Providers providers;
88   
 
89  2 toggle @Before
90    public void setUp() throws Exception {
91  2 super.setUp();
92  2 Session session = MgnlContext.getJCRSession(RepositoryConstants.WEBSITE);
93  2 new PropertiesImportExport().createNodes(session.getRootNode(), getClass().getClassLoader().getResourceAsStream("travel-test-data.properties"));
94  2 session.save();
95   
96  2 baos = new ByteArrayOutputStream();
97  2 mapper = new ObjectMapper();
98  2 workspace = session.getWorkspace();
99   
100  2 nodesResultWriter = new NodesResultWriter();
101  2 nodeWriter = new NodeWriter();
102  2 nodeIteratorWriter = new NodeIteratorWriter();
103   
104  2 MockitoAnnotations.initMocks(this);
105  2 when(providers.getContextResolver(ReferenceContext.class, MediaType.WILDCARD_TYPE)).thenReturn(type -> mock(ReferenceContext.class));
106  2 when(providers.getMessageBodyWriter(Node.class, null, null, null)).thenReturn(nodeWriter);
107  2 when(providers.getMessageBodyWriter(NodeIterator.class, null, null, null)).thenReturn(nodeIteratorWriter);
108    }
109   
 
110  1 toggle @Test
111    public void listNodesWithAdditionalMetaData() throws Exception {
112    // GIVEN
113  1 NodeIterator nodeIterator = QueryBuilder.inWorkspace(workspace)
114    .nodeTypes(Collections.singletonList(NodeTypes.Page.NAME))
115    .build()
116    .execute()
117    .getNodes();
118  1 List<Node> nodes = NodeUtil.asList(NodeUtil.asIterable(nodeIterator));
119   
120    // WHEN
121  1 NodesResult nodesResult = new NodesResult(new NodeIteratorAdapter(nodes.iterator()), 10);
122  1 nodesResultWriter.writeTo(nodesResult, null, null, null, null, null, baos);
123  1 Map<String, Object> result = mapper.readValue(baos.toString(), new TypeReference<Object>(){});
124    // assertEquals(result.get("total"), 10);
125  1 List<Object> jsonNodeList = (List<Object>) result.get("results");
126   
127    // THEN
128  1 assertNotNull(nodeIterator);
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   
 
135  1 toggle @Test
136    public void listNodesAndSortByLastModifiedDate() throws Exception {
137    // GIVEN
138  1 NodeIterator nodeIterator = QueryBuilder.inWorkspace(workspace)
139    .nodeTypes(Arrays.asList(NodeTypes.Page.NAME))
140    .orderBy(Collections.singletonList(NodeTypes.LastModified.NAME + " desc"))
141    .build()
142    .execute()
143    .getNodes();
144  1 List<Node> nodes = NodeUtil.asList(NodeUtil.asIterable(nodeIterator));
145   
146    // WHEN
147  1 NodesResult nodesResult = new NodesResult(new NodeIteratorAdapter(nodes.iterator()), 10);
148  1 nodesResultWriter.writeTo(nodesResult, null, null, null, null, null, baos);
149  1 Map<String, Object> result = mapper.readValue(baos.toString(), new TypeReference<Object>(){});
150    // assertEquals(result.get("total"), 10);
151  1 List<Object> jsonNodeList = (List<Object>) result.get("results");
152   
153    // THEN
154  1 assertNotNull(jsonNodeList);
155  1 assertThat(nodes.size(), is(jsonNodeList.size()));
156  1 assertThat(nodes.get(0).getProperty("title").getString(), is(((Map<String, String>)jsonNodeList.get(0)).get("title")));
157  1 assertThat(nodes.get(1).getProperty("title").getString(), is(((Map<String, String>)jsonNodeList.get(1)).get("title")));
158  1 assertThat(nodes.get(2).getProperty("title").getString(), is(((Map<String, String>)jsonNodeList.get(2)).get("title")));
159    }
160    }