1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (68) |
Complexity: 11 |
Complexity Density: 0.19 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
71 |
7 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
84 |
1 |
@Test... |
85 |
|
public void writingNodeWithoutFiltering() throws Exception { |
86 |
|
|
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 |
|
|
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 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
1PASS
|
|
104 |
|
@Test... |
105 |
|
public void isWritable() { |
106 |
|
|
107 |
|
boolean isWritable = writer.isWriteable(Node.class, null, new Annotation[] {}, MediaType.APPLICATION_JSON_TYPE); |
108 |
|
|
109 |
|
|
110 |
|
assertEquals(true, isWritable); |
111 |
|
} |
112 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
113 |
1 |
@Test... |
114 |
|
public void writingNodeReturnsJsonIncludingMetaData() throws Exception { |
115 |
|
|
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 |
|
|
120 |
1 |
assertTrue(map.containsKey("company")); |
121 |
1 |
assertTrue(map.containsKey(NodeTypes.LastModified.NAME)); |
122 |
|
} |
123 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
124 |
1 |
@Test... |
125 |
|
public void writingNodeHavingMultiplePropertiesReturnExpectedJson() throws Exception { |
126 |
|
|
127 |
1 |
addTagsPropertyToAboutNode(); |
128 |
|
|
129 |
|
|
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 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
139 |
3 |
private void addTagsPropertyToAboutNode() throws RepositoryException {... |
140 |
|
|
141 |
3 |
about.setProperty("tags", new String[] { "travel", "culture", "cuisine" }); |
142 |
|
} |
143 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
144 |
1 |
@Test... |
145 |
|
public void propertyHavingTheSameNameWithSubNodeShouldBePrefixedWithUnderscore() throws Exception { |
146 |
|
|
147 |
1 |
addTagsPropertyToAboutNode(); |
148 |
1 |
addTagsNodeToAboutNode(); |
149 |
|
|
150 |
|
|
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 |
|
|
155 |
1 |
assertTags(map); |
156 |
|
} |
157 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
158 |
2 |
private void addTagsNodeToAboutNode() throws RepositoryException {... |
159 |
2 |
Node tags = about.addNode("tags", NodeTypes.Page.NAME); |
160 |
2 |
tags.setProperty("title", "Tags"); |
161 |
|
} |
162 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
163 |
2 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
172 |
1 |
@Test... |
173 |
|
public void propertyHavingTheSameNameWithSubNodeShouldBePrefixedWithUnderscore_ChangeInsertionOrder() throws Exception { |
174 |
|
|
175 |
1 |
addTagsNodeToAboutNode(); |
176 |
1 |
addTagsPropertyToAboutNode(); |
177 |
|
|
178 |
|
|
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 |
|
|
183 |
1 |
assertTags(map); |
184 |
|
} |
185 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
186 |
1 |
@Test... |
187 |
|
public void resultContainsMetaProperties() throws Exception { |
188 |
|
|
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 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
199 |
7 |
@After... |
200 |
|
public void end() throws IOException { |
201 |
7 |
baos.close(); |
202 |
|
} |
203 |
|
} |