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.v2; |
35 |
|
|
36 |
|
import static info.magnolia.rest.RestTestAccessManager.ANONYMOUS; |
37 |
|
import static info.magnolia.test.hamcrest.ExceptionMatcher.instanceOf; |
38 |
|
import static info.magnolia.test.hamcrest.ExecutionMatcher.throwsAnException; |
39 |
|
import static info.magnolia.test.hamcrest.NodeMatchers.*; |
40 |
|
import static info.magnolia.test.hamcrest.NodeMatchers.hasProperty; |
41 |
|
import static org.hamcrest.Matchers.*; |
42 |
|
import static org.junit.Assert.*; |
43 |
|
import static org.mockito.Mockito.*; |
44 |
|
|
45 |
|
import info.magnolia.cms.security.User; |
46 |
|
import info.magnolia.cms.util.ClasspathResourcesUtil; |
47 |
|
import info.magnolia.context.DefaultRepositoryStrategy; |
48 |
|
import info.magnolia.context.MgnlContext; |
49 |
|
import info.magnolia.context.UserContext; |
50 |
|
import info.magnolia.jcr.util.NodeTypes; |
51 |
|
import info.magnolia.jcr.util.NodeUtil; |
52 |
|
import info.magnolia.jcr.util.PropertiesImportExport; |
53 |
|
import info.magnolia.objectfactory.Components; |
54 |
|
import info.magnolia.repository.RepositoryConstants; |
55 |
|
import info.magnolia.rest.reference.ReferenceContext; |
56 |
|
import info.magnolia.test.ComponentsTestUtil; |
57 |
|
import info.magnolia.test.RepositoryTestCase; |
58 |
|
import info.magnolia.test.mock.MockWebContext; |
59 |
|
|
60 |
|
import java.io.InputStream; |
61 |
|
import java.util.ArrayList; |
62 |
|
import java.util.Arrays; |
63 |
|
import java.util.List; |
64 |
|
|
65 |
|
import javax.jcr.Node; |
66 |
|
import javax.jcr.NodeIterator; |
67 |
|
import javax.jcr.PathNotFoundException; |
68 |
|
import javax.jcr.RepositoryException; |
69 |
|
import javax.jcr.Session; |
70 |
|
import javax.ws.rs.core.MediaType; |
71 |
|
import javax.ws.rs.core.MultivaluedHashMap; |
72 |
|
import javax.ws.rs.core.MultivaluedMap; |
73 |
|
import javax.ws.rs.core.UriInfo; |
74 |
|
import javax.ws.rs.ext.Providers; |
75 |
|
|
76 |
|
import org.junit.Before; |
77 |
|
import org.junit.Test; |
78 |
|
import org.mockito.InjectMocks; |
79 |
|
import org.mockito.Mock; |
80 |
|
import org.mockito.MockitoAnnotations; |
81 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (43) |
Complexity: 6 |
Complexity Density: 0.16 |
|
82 |
|
public class JcrDeliveryEndpointTest extends RepositoryTestCase { |
83 |
|
|
84 |
|
private Session session; |
85 |
|
private ConfiguredJcrDeliveryEndpointDefinition websiteParameters; |
86 |
|
private MultivaluedMap<String, String> queryParameters; |
87 |
|
|
88 |
|
@InjectMocks |
89 |
|
private JcrDeliveryEndpoint endpoint; |
90 |
|
|
91 |
|
@Mock |
92 |
|
private Providers providers; |
93 |
|
|
94 |
|
@Mock |
95 |
|
private UriInfo uriInfo; |
96 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
|
97 |
3 |
@Before... |
98 |
|
public void setUp() throws Exception { |
99 |
3 |
super.setUp(); |
100 |
3 |
session = MgnlContext.getJCRSession(RepositoryConstants.WEBSITE); |
101 |
3 |
new PropertiesImportExport().createNodes(session.getRootNode(), getClass().getClassLoader().getResourceAsStream("travel-test-data.properties")); |
102 |
3 |
session.save(); |
103 |
|
|
104 |
3 |
websiteParameters = new ConfiguredJcrDeliveryEndpointDefinition(); |
105 |
3 |
websiteParameters.setWorkspace(RepositoryConstants.WEBSITE); |
106 |
3 |
endpoint = new JcrDeliveryEndpoint(websiteParameters, Components.getComponentProvider()); |
107 |
|
|
108 |
3 |
MockitoAnnotations.initMocks(this); |
109 |
3 |
queryParameters = new MultivaluedHashMap<>(); |
110 |
3 |
when(uriInfo.getQueryParameters()).thenReturn(queryParameters); |
111 |
3 |
when(providers.getContextResolver(ReferenceContext.class, MediaType.WILDCARD_TYPE)).thenReturn(type -> mock(ReferenceContext.class)); |
112 |
|
} |
113 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
114 |
3 |
@Override... |
115 |
|
protected InputStream getJackrabbitRepositoryConfigFileStream() throws Exception { |
116 |
3 |
return ClasspathResourcesUtil.getResource("jackrabbit-memory-search.xml").openStream(); |
117 |
|
} |
118 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
119 |
1 |
@Test... |
120 |
|
public void readNode() throws Exception { |
121 |
|
|
122 |
1 |
Node node = endpoint.readNode("/travel/about"); |
123 |
|
|
124 |
|
|
125 |
1 |
assertNotNull(node); |
126 |
1 |
assertEquals("about", node.getName()); |
127 |
1 |
assertThat(node, hasProperty("title")); |
128 |
1 |
assertThat(node, hasProperty(NodeTypes.LastModified.NAME)); |
129 |
1 |
assertThat(node, not(hasNode("footer"))); |
130 |
|
} |
131 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
132 |
1 |
@Test... |
133 |
|
public void queryNodes() throws Exception { |
134 |
|
|
135 |
1 |
websiteParameters.setNodeTypes(Arrays.asList(NodeTypes.Page.NAME)); |
136 |
|
|
137 |
|
|
138 |
1 |
NodeIterator nodeIterator = endpoint.queryNodes("", "", 0L, 10L).getResults(); |
139 |
|
|
140 |
|
|
141 |
1 |
assertNotNull(nodeIterator); |
142 |
1 |
assertEquals(3, nodeIterator.getSize()); |
143 |
1 |
List<Node> nodes = NodeUtil.asList(NodeUtil.asIterable(nodeIterator)); |
144 |
1 |
List<String> nodeNames = new ArrayList<>((int) nodeIterator.getSize()); |
145 |
1 |
for (Node node : nodes) { |
146 |
3 |
nodeNames.add(node.getName()); |
147 |
|
} |
148 |
1 |
assertThat(nodeNames, containsInAnyOrder("about", "company", "travel")); |
149 |
|
} |
150 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
151 |
1 |
@Test... |
152 |
|
public void readingNodeWithoutBypassingAclsThrowsException() throws Exception { |
153 |
|
|
154 |
1 |
setUpContextForAnonymous(); |
155 |
|
|
156 |
|
|
157 |
1 |
assertThat(() -> endpoint.readNode("/travel/about"), |
158 |
|
throwsAnException(instanceOf(PathNotFoundException.class))); |
159 |
|
} |
160 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
161 |
1 |
private void setUpContextForAnonymous() throws RepositoryException {... |
162 |
1 |
MockWebContext context = new MockWebContext(); |
163 |
1 |
ComponentsTestUtil.setInstance(UserContext.class, mock(UserContext.class)); |
164 |
1 |
DefaultRepositoryStrategy defaultRepositoryStrategy = Components.newInstance(DefaultRepositoryStrategy.class); |
165 |
1 |
context.setRepositoryStrategy(defaultRepositoryStrategy); |
166 |
|
|
167 |
|
|
168 |
1 |
User user = mock(User.class); |
169 |
1 |
when(user.getName()).thenReturn(ANONYMOUS); |
170 |
1 |
context.setUser(user); |
171 |
|
|
172 |
1 |
MgnlContext.setInstance(context); |
173 |
|
} |
174 |
|
} |