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

 

Code metrics

0
57
11
1
203
120
11
0.19
5.18
11
1
4.2% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
NodeWriterTest 63 57 4.2% 11 0
1.0100%
 

Contributing tests

This file is covered by 7 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.Matchers.contains;
37    import static org.junit.Assert.*;
38   
39    import info.magnolia.context.MgnlContext;
40    import info.magnolia.jcr.util.NodeTypes;
41    import info.magnolia.jcr.util.PropertiesImportExport;
42    import info.magnolia.repository.RepositoryConstants;
43    import info.magnolia.test.RepositoryTestCase;
44   
45    import java.io.ByteArrayOutputStream;
46    import java.io.IOException;
47    import java.lang.annotation.Annotation;
48    import java.util.List;
49    import java.util.Map;
50   
51    import javax.jcr.Node;
52    import javax.jcr.RepositoryException;
53    import javax.jcr.Session;
54    import javax.ws.rs.core.MediaType;
55   
56    import org.junit.After;
57    import org.junit.Before;
58    import org.junit.Test;
59   
60    import com.fasterxml.jackson.core.type.TypeReference;
61    import com.fasterxml.jackson.databind.ObjectMapper;
62   
 
63    public class NodeWriterTest extends RepositoryTestCase {
64   
65    private Session session;
66    private ByteArrayOutputStream baos;
67    private ObjectMapper mapper;
68    private NodeWriter writer;
69    private Node about;
70   
 
71  7 toggle @Before
72    public void setUp() throws Exception {
73  7 super.setUp();
74  7 session = MgnlContext.getJCRSession(RepositoryConstants.WEBSITE);
75  7 new PropertiesImportExport().createNodes(session.getRootNode(), getClass().getClassLoader().getResourceAsStream("travel-test-data.properties"));
76  7 session.save();
77   
78  7 baos = new ByteArrayOutputStream();
79  7 mapper = new ObjectMapper();
80  7 writer = new NodeWriter();
81  7 about = session.getRootNode().getNode("travel/about");
82    }
83   
 
84  1 toggle @Test
85    public void writingNodeWithoutFiltering() throws Exception {
86    // WHEN
87  1 writer.writeTo(about, null, null, null, null, null, baos);
88  1 Map<String, Object> map = mapper.readValue(baos.toString(), new TypeReference<Map<String, Object>>() {});
89   
90    // THEN
91  1 assertTrue(map.containsKey("company") && map.get("company") != null);
92  1 assertTrue(map.containsKey(NodeTypes.LastModified.NAME));
93  1 Map<String, Object> company = (Map<String, Object>) map.get("company");
94  1 assertTrue(company.containsKey("main") && company.get("main") != null);
95  1 assertTrue(company.containsKey(NodeTypes.LastModified.NAME));
96  1 Map<String, Object> main = (Map<String, Object>) company.get("main");
97  1 assertTrue(main.containsKey("00") && main.get("00") != null);
98  1 assertTrue(main.containsKey(NodeTypes.LastModified.NAME));
99  1 Map<String, String> doubleZero = (Map<String, String>) main.get("00");
100  1 assertTrue(doubleZero.containsKey("headline"));
101  1 assertTrue(map.containsKey("footer"));
102    }
103   
 
104    toggle @Test
105    public void isWritable() {
106    // WHEN
107    boolean isWritable = writer.isWriteable(Node.class, null, new Annotation[] {}, MediaType.APPLICATION_JSON_TYPE);
108   
109    // THEN
110    assertEquals(true, isWritable);
111    }
112   
 
113  1 toggle @Test
114    public void writingNodeReturnsJsonIncludingMetaData() throws Exception {
115    // WHEN
116  1 writer.writeTo(about, null, null, null, null, null, baos);
117  1 Map<String, Object> map = mapper.readValue(baos.toString(), new TypeReference<Map<String, Object>>() {});
118   
119    // THEN
120  1 assertTrue(map.containsKey("company"));
121  1 assertTrue(map.containsKey(NodeTypes.LastModified.NAME));
122    }
123   
 
124  1 toggle @Test
125    public void writingNodeHavingMultiplePropertiesReturnExpectedJson() throws Exception {
126    // GIVEN
127  1 addTagsPropertyToAboutNode();
128   
129    // WHEN
130  1 writer.writeTo(about, null, null, null, null, null, baos);
131  1 Map<String, Object> map = mapper.readValue(baos.toString(), new TypeReference<Map<String, Object>>() {});
132   
133    // THEN
134  1 assertTrue(map.containsKey("tags") && map.get("tags") != null);
135  1 List<String> tags = (List<String>) map.get("tags");
136  1 assertEquals(3, tags.size());
137    }
138   
 
139  3 toggle private void addTagsPropertyToAboutNode() throws RepositoryException {
140    // Add multiple-value property "tags" to "about" node.
141  3 about.setProperty("tags", new String[] { "travel", "culture", "cuisine" });
142    }
143   
 
144  1 toggle @Test
145    public void propertyHavingTheSameNameWithSubNodeShouldBePrefixedWithUnderscore() throws Exception {
146    // GIVEN
147  1 addTagsPropertyToAboutNode();
148  1 addTagsNodeToAboutNode();
149   
150    // WHEN
151  1 writer.writeTo(about, null, null, null, null, null, baos);
152  1 Map<String, Object> map = mapper.readValue(baos.toString(), new TypeReference<Map<String, Object>>() {});
153   
154    // THEN
155  1 assertTags(map);
156    }
157   
 
158  2 toggle private void addTagsNodeToAboutNode() throws RepositoryException {
159  2 Node tags = about.addNode("tags", NodeTypes.Page.NAME);
160  2 tags.setProperty("title", "Tags");
161    }
162   
 
163  2 toggle private void assertTags(Map<String, Object> map) {
164  2 assertTrue(map.containsKey("_tags") && map.get("_tags") != null);
165  2 List tags = (List) map.get("_tags");
166  2 assertEquals(3, tags.size());
167  2 assertTrue(map.containsKey("tags") && map.get("tags") != null);
168  2 Map<String, String> tagNodeMap = (Map<String, String>) map.get("tags");
169  2 assertTrue(tagNodeMap.containsKey("title"));
170    }
171   
 
172  1 toggle @Test
173    public void propertyHavingTheSameNameWithSubNodeShouldBePrefixedWithUnderscore_ChangeInsertionOrder() throws Exception {
174    // GIVEN
175  1 addTagsNodeToAboutNode();
176  1 addTagsPropertyToAboutNode();
177   
178    // WHEN
179  1 writer.writeTo(about, null, null, null, null, null, baos);
180  1 Map<String, Object> map = mapper.readValue(baos.toString(), new TypeReference<Map<String, Object>>() {});
181   
182    // THEN
183  1 assertTags(map);
184    }
185   
 
186  1 toggle @Test
187    public void resultContainsMetaProperties() throws Exception {
188    // WHEN
189  1 writer.writeTo(about, null, null, null, null, null, baos);
190  1 Map<String, Object> map = mapper.readValue(baos.toString(), new TypeReference<Map<String, Object>>() {});
191   
192    // THEN
193  1 assertTrue(map.containsKey("@name"));
194  1 assertTrue(map.containsKey("@path"));
195  1 assertTrue(map.containsKey("@nodes"));
196  1 assertThat((List<String>) map.get("@nodes"), contains("company", "footer"));
197    }
198   
 
199  7 toggle @After
200    public void end() throws IOException {
201  7 baos.close();
202    }
203    }