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

 

Code metrics

0
30
3
1
151
89
3
0.1
10
3
1

Classes

Class Line # Actions
NodeIteratorWriterTest 70 30 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.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.core.MediaType;
58    import javax.ws.rs.ext.Providers;
59   
60    import org.apache.jackrabbit.commons.iterator.NodeIteratorAdapter;
61    import org.junit.Before;
62    import org.junit.Test;
63    import org.mockito.InjectMocks;
64    import org.mockito.Mock;
65    import org.mockito.MockitoAnnotations;
66   
67    import com.fasterxml.jackson.core.type.TypeReference;
68    import com.fasterxml.jackson.databind.ObjectMapper;
69   
 
70    public class NodeIteratorWriterTest extends RepositoryTestCase {
71   
72    private Session session;
73    private ByteArrayOutputStream baos;
74    private ObjectMapper mapper;
75    private Workspace workspace;
76   
77    @InjectMocks
78    private NodeIteratorWriter nodeIteratorWriter;
79   
80    @InjectMocks
81    private NodeWriter nodeWriter;
82   
83    @Mock
84    private Providers providers;
85   
 
86  2 toggle @Before
87    public void setUp() throws Exception {
88  2 super.setUp();
89  2 session = MgnlContext.getJCRSession(RepositoryConstants.WEBSITE);
90  2 new PropertiesImportExport().createNodes(session.getRootNode(), getClass().getClassLoader().getResourceAsStream("travel-test-data.properties"));
91  2 session.save();
92   
93  2 baos = new ByteArrayOutputStream();
94  2 mapper = new ObjectMapper();
95  2 workspace = session.getWorkspace();
96   
97  2 nodeWriter = new NodeWriter();
98  2 nodeIteratorWriter = new NodeIteratorWriter();
99   
100  2 MockitoAnnotations.initMocks(NodeIteratorWriterTest.this);
101   
102  2 when(providers.getMessageBodyWriter(Node.class, null, null, null)).thenReturn(nodeWriter);
103  2 when(providers.getContextResolver(ReferenceContext.class, MediaType.WILDCARD_TYPE)).thenReturn(type -> mock(ReferenceContext.class));
104   
105    }
106   
 
107  1 toggle @Test
108    public void listNodes() throws Exception {
109    // GIVEN
110  1 NodeIterator nodeIterator = QueryBuilder.inWorkspace(workspace)
111    .nodeTypes(Arrays.asList(NodeTypes.Page.NAME))
112    .build()
113    .execute()
114    .getNodes();
115  1 List<Node> nodes = NodeUtil.asList(NodeUtil.asIterable(nodeIterator));
116   
117    // WHEN
118  1 nodeIteratorWriter.writeTo(new NodeIteratorAdapter(nodes.iterator()), null, null, null, null, null, baos);
119  1 List<Object> jsonNodeList = mapper.readValue(baos.toString(), new TypeReference<List<Object>>() {});
120   
121    // THEN
122  1 assertNotNull(nodeIterator);
123  1 assertThat(nodes.size(), is(jsonNodeList.size()));
124  1 assertThat(nodes.get(0).getProperty("title").getString(), is(((Map<String, String>)jsonNodeList.get(0)).get("title")));
125  1 assertThat(nodes.get(1).getProperty("title").getString(), is(((Map<String, String>)jsonNodeList.get(1)).get("title")));
126  1 assertThat(nodes.get(2).getProperty("title").getString(), is(((Map<String, String>)jsonNodeList.get(2)).get("title")));
127    }
128   
 
129  1 toggle @Test
130    public void listNodesAndSortByLastModifiedDate() throws Exception {
131    // GIVEN
132  1 NodeIterator nodeIterator = QueryBuilder.inWorkspace(workspace)
133    .nodeTypes(Arrays.asList(NodeTypes.Page.NAME))
134    .orderBy(Arrays.asList(NodeTypes.LastModified.NAME + " desc"))
135    .build()
136    .execute()
137    .getNodes();
138  1 List<Node> nodes = NodeUtil.asList(NodeUtil.asIterable(nodeIterator));
139   
140    // WHEN
141  1 nodeIteratorWriter.writeTo(new NodeIteratorAdapter(nodes.iterator()), null, null, null, null, null, baos);
142  1 List<Object> jsonNodeList = mapper.readValue(baos.toString(), new TypeReference<List<Object>>() {});
143   
144    // THEN
145  1 assertNotNull(jsonNodeList);
146  1 assertThat(nodes.size(), is(jsonNodeList.size()));
147  1 assertThat(nodes.get(0).getProperty("title").getString(), is(((Map<String, String>)jsonNodeList.get(0)).get("title")));
148  1 assertThat(nodes.get(1).getProperty("title").getString(), is(((Map<String, String>)jsonNodeList.get(1)).get("title")));
149  1 assertThat(nodes.get(2).getProperty("title").getString(), is(((Map<String, String>)jsonNodeList.get(2)).get("title")));
150    }
151    }