1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.url.internal.standard.entity; |
21 |
|
|
22 |
|
import java.net.URL; |
23 |
|
import java.util.Arrays; |
24 |
|
import java.util.Collections; |
25 |
|
import java.util.LinkedHashMap; |
26 |
|
import java.util.List; |
27 |
|
import java.util.Map; |
28 |
|
|
29 |
|
import org.junit.Before; |
30 |
|
import org.junit.Test; |
31 |
|
import org.xwiki.component.util.ReflectionUtils; |
32 |
|
import org.xwiki.model.EntityType; |
33 |
|
import org.xwiki.model.reference.EntityReference; |
34 |
|
import org.xwiki.model.reference.EntityReferenceResolver; |
35 |
|
import org.xwiki.model.reference.WikiReference; |
36 |
|
import org.xwiki.resource.ResourceType; |
37 |
|
import org.xwiki.resource.entity.EntityResourceReference; |
38 |
|
import org.xwiki.resource.ResourceReference; |
39 |
|
import org.xwiki.resource.internal.entity.EntityResourceActionLister; |
40 |
|
import org.xwiki.url.ExtendedURL; |
41 |
|
import org.xwiki.url.internal.standard.StandardURLConfiguration; |
42 |
|
import org.xwiki.url.internal.standard.WikiReferenceExtractor; |
43 |
|
|
44 |
|
import static org.junit.Assert.assertEquals; |
45 |
|
import static org.mockito.Mockito.*; |
46 |
|
|
47 |
|
|
48 |
|
@link |
49 |
|
|
50 |
|
@version |
51 |
|
@since |
52 |
|
|
|
|
| 98.9% |
Uncovered Elements: 1 (92) |
Complexity: 12 |
Complexity Density: 0.16 |
|
53 |
|
public class BinEntityResourceReferenceResolverTest |
54 |
|
{ |
55 |
|
private BinEntityResourceReferenceResolver resolver; |
56 |
|
|
57 |
|
private WikiReference wikiReference = new WikiReference("wiki"); |
58 |
|
|
59 |
|
private WikiReferenceExtractor wikiReferenceExtractor; |
60 |
|
|
61 |
|
private EntityReferenceResolver<EntityReference> entityReferenceResolver; |
62 |
|
|
63 |
|
private EntityResourceActionLister entityResourceActionLister; |
64 |
|
|
65 |
|
private StandardURLConfiguration configuration; |
66 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
|
67 |
5 |
@Before... |
68 |
|
public void setUp() throws Exception |
69 |
|
{ |
70 |
5 |
this.resolver = new BinEntityResourceReferenceResolver(); |
71 |
|
|
72 |
5 |
this.wikiReferenceExtractor = mock(WikiReferenceExtractor.class); |
73 |
5 |
when(this.wikiReferenceExtractor.extract(any(ExtendedURL.class))).thenReturn(this.wikiReference); |
74 |
5 |
ReflectionUtils.setFieldValue(this.resolver, "wikiExtractor", this.wikiReferenceExtractor); |
75 |
|
|
76 |
5 |
this.entityReferenceResolver = mock(EntityReferenceResolver.class); |
77 |
5 |
ReflectionUtils.setFieldValue(this.resolver, "defaultReferenceEntityReferenceResolver", |
78 |
|
this.entityReferenceResolver); |
79 |
|
|
80 |
5 |
this.configuration = mock(StandardURLConfiguration.class); |
81 |
5 |
ReflectionUtils.setFieldValue(this.resolver, "configuration", this.configuration); |
82 |
|
|
83 |
5 |
this.entityResourceActionLister = mock(EntityResourceActionLister.class); |
84 |
5 |
when(this.entityResourceActionLister.listActions()).thenReturn(Arrays.asList("view", "download")); |
85 |
5 |
ReflectionUtils.setFieldValue(this.resolver, "entityResourceActionLister", this.entityResourceActionLister); |
86 |
|
} |
87 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
88 |
1 |
@Test... |
89 |
|
public void createResourceWhenViewActionHidden() throws Exception |
90 |
|
{ |
91 |
1 |
when(this.configuration.isViewActionHidden()).thenReturn(true); |
92 |
|
|
93 |
1 |
EntityReference fullSingleSpaceReference = buildEntityReference("wiki", Arrays.asList("space"), "page"); |
94 |
1 |
EntityReference fullTwoSpacesReference = |
95 |
|
buildEntityReference("wiki", Arrays.asList("space1", "space2"), "page"); |
96 |
|
|
97 |
|
|
98 |
1 |
testCreateResource("http://localhost/bin", "view", this.wikiReference, fullSingleSpaceReference, |
99 |
|
EntityType.DOCUMENT); |
100 |
|
|
101 |
|
|
102 |
1 |
testCreateResource("http://localhost/bin/", "view", this.wikiReference, fullSingleSpaceReference, |
103 |
|
EntityType.DOCUMENT); |
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
1 |
testCreateResource("http://localhost/bin/space", "view", |
113 |
|
buildEntityReference("wiki", Arrays.asList("space"), null), fullSingleSpaceReference, EntityType.DOCUMENT); |
114 |
|
|
115 |
|
|
116 |
1 |
testCreateResource("http://localhost/bin/space/", "view", |
117 |
|
buildEntityReference("wiki", Arrays.asList("space"), null), fullSingleSpaceReference, EntityType.DOCUMENT); |
118 |
|
|
119 |
|
|
120 |
1 |
testCreateResource("http://localhost/bin/space1/space2/", "view", |
121 |
|
buildEntityReference("wiki", Arrays.asList("space1", "space2"), null), fullTwoSpacesReference, |
122 |
|
EntityType.DOCUMENT); |
123 |
|
|
124 |
|
|
125 |
1 |
testCreateResource("http://localhost/bin/space/page", "view", fullSingleSpaceReference, |
126 |
|
fullSingleSpaceReference, EntityType.DOCUMENT); |
127 |
|
|
128 |
|
|
129 |
1 |
testCreateResource("http://localhost/bin/space1/space2/page", "view", fullTwoSpacesReference, |
130 |
|
fullTwoSpacesReference, EntityType.DOCUMENT); |
131 |
|
|
132 |
|
|
133 |
1 |
testCreateResource("http://localhost/bin/view/space/page", "view", fullSingleSpaceReference, |
134 |
|
fullSingleSpaceReference, EntityType.DOCUMENT); |
135 |
1 |
EntityReference viewTwoSpacesReference = |
136 |
|
buildEntityReference("wiki", Arrays.asList("view", "space2"), "page"); |
137 |
1 |
testCreateResource("http://localhost/bin/view/view/space2/page", "view", viewTwoSpacesReference, |
138 |
|
viewTwoSpacesReference, EntityType.DOCUMENT); |
139 |
|
|
140 |
|
|
141 |
1 |
EntityReference downloadTwoSpacesReference = |
142 |
|
buildEntityReference("wiki", Arrays.asList("download", "space2"), "page"); |
143 |
1 |
testCreateResource("http://localhost/bin/view/download/space2/page", "view", downloadTwoSpacesReference, |
144 |
|
downloadTwoSpacesReference, EntityType.DOCUMENT); |
145 |
|
|
146 |
|
|
147 |
1 |
testCreateResource("http://localhost/bin/download/space/page", "download", fullSingleSpaceReference, |
148 |
|
fullSingleSpaceReference, EntityType.DOCUMENT); |
149 |
|
} |
150 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
151 |
1 |
@Test... |
152 |
|
public void createResourceWhenViewActionShown() throws Exception |
153 |
|
{ |
154 |
1 |
when(this.configuration.isViewActionHidden()).thenReturn(false); |
155 |
|
|
156 |
1 |
EntityReference fullSingleSpaceReference = buildEntityReference("wiki", Arrays.asList("space"), "page"); |
157 |
1 |
EntityReference fullTwoSpacesReference = |
158 |
|
buildEntityReference("wiki", Arrays.asList("space1", "space2"), "page"); |
159 |
|
|
160 |
|
|
161 |
1 |
testCreateResource("http://localhost/bin/view/space", "view", |
162 |
|
buildEntityReference("wiki", Arrays.asList("space"), null), fullSingleSpaceReference, EntityType.DOCUMENT); |
163 |
|
|
164 |
|
|
165 |
1 |
testCreateResource("http://localhost/bin/view/space/", "view", |
166 |
|
buildEntityReference("wiki", Arrays.asList("space"), null), fullSingleSpaceReference, EntityType.DOCUMENT); |
167 |
|
|
168 |
|
|
169 |
1 |
testCreateResource("http://localhost/bin/view/space1/space2/", "view", |
170 |
|
buildEntityReference("wiki", Arrays.asList("space1", "space2"), null), fullTwoSpacesReference, |
171 |
|
EntityType.DOCUMENT); |
172 |
|
|
173 |
|
|
174 |
1 |
testCreateResource("http://localhost/bin/view/space/page", "view", fullSingleSpaceReference, |
175 |
|
fullSingleSpaceReference, EntityType.DOCUMENT); |
176 |
|
|
177 |
|
|
178 |
1 |
testCreateResource("http://localhost/bin/view/space1/space2/page", "view", fullTwoSpacesReference, |
179 |
|
fullTwoSpacesReference, EntityType.DOCUMENT); |
180 |
|
|
181 |
|
|
182 |
1 |
testCreateResource("http://localhost/bin/space/page", "view", fullSingleSpaceReference, |
183 |
|
fullSingleSpaceReference, EntityType.DOCUMENT); |
184 |
|
|
185 |
|
} |
186 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
187 |
1 |
@Test... |
188 |
|
public void createResourceWhenSpaceAndPageNamesContainDots() throws Exception |
189 |
|
{ |
190 |
1 |
EntityReference reference = buildEntityReference("wiki", Arrays.asList("space.with.dots"), "page.with.dots"); |
191 |
1 |
testCreateResource("http://localhost/bin/view/space.with.dots/page.with.dots", "view", |
192 |
|
reference, reference, EntityType.DOCUMENT); |
193 |
|
} |
194 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
195 |
1 |
@Test... |
196 |
|
public void createResourceWhenFileActionAction() throws Exception |
197 |
|
{ |
198 |
1 |
EntityReference singleSpaceReference = |
199 |
|
buildEntityReference("wiki", Arrays.asList("space"), "page", "attachment.ext"); |
200 |
1 |
EntityReference twoSpaceReference = |
201 |
|
buildEntityReference("wiki", Arrays.asList("space1", "space2"), "page", "attachment.ext"); |
202 |
|
|
203 |
1 |
testCreateResource("http://localhost/bin/download/space/page/attachment.ext", "download", singleSpaceReference, |
204 |
|
singleSpaceReference, EntityType.ATTACHMENT); |
205 |
1 |
testCreateResource("http://localhost/bin/download/space1/space2/page/attachment.ext", "download", |
206 |
|
twoSpaceReference, twoSpaceReference, EntityType.ATTACHMENT); |
207 |
|
|
208 |
1 |
testCreateResource("http://localhost/bin/delattachment/space/page/attachment.ext", "delattachment", |
209 |
|
singleSpaceReference, singleSpaceReference, EntityType.ATTACHMENT); |
210 |
1 |
testCreateResource("http://localhost/bin/delattachment/space1/space2/page/attachment.ext", "delattachment", |
211 |
|
twoSpaceReference, twoSpaceReference, EntityType.ATTACHMENT); |
212 |
|
|
213 |
1 |
testCreateResource("http://localhost/bin/viewattachrev/space/page/attachment.ext", "viewattachrev", |
214 |
|
singleSpaceReference, singleSpaceReference, EntityType.ATTACHMENT); |
215 |
1 |
testCreateResource("http://localhost/bin/viewattachrev/space1/space2/page/attachment.ext", "viewattachrev", |
216 |
|
twoSpaceReference, twoSpaceReference, EntityType.ATTACHMENT); |
217 |
|
|
218 |
1 |
testCreateResource("http://localhost/bin/downloadrev/space/page/attachment.ext", "downloadrev", |
219 |
|
singleSpaceReference, singleSpaceReference, EntityType.ATTACHMENT); |
220 |
1 |
testCreateResource("http://localhost/bin/downloadrev/space1/space2/page/attachment.ext", "downloadrev", |
221 |
|
twoSpaceReference, twoSpaceReference, EntityType.ATTACHMENT); |
222 |
|
} |
223 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
224 |
1 |
@Test... |
225 |
|
public void createResourceWhenURLHasParameters() throws Exception |
226 |
|
{ |
227 |
1 |
EntityReference fullReference = buildEntityReference("wiki", Arrays.asList("space"), "page"); |
228 |
1 |
ResourceReference resource = |
229 |
|
testCreateResource("http://localhost/bin/view/space/page?param1=value1¶m2=value2", "view", |
230 |
|
fullReference, fullReference, EntityType.DOCUMENT); |
231 |
|
|
232 |
|
|
233 |
|
|
234 |
1 |
Map<String, List<String>> expectedMap = new LinkedHashMap<String, List<String>>(); |
235 |
1 |
expectedMap.put("param1", Arrays.asList("value1")); |
236 |
1 |
expectedMap.put("param2", Arrays.asList("value2")); |
237 |
1 |
assertEquals(expectedMap, resource.getParameters()); |
238 |
|
|
239 |
|
|
240 |
1 |
resource = testCreateResource("http://localhost/bin/view/space/page?param", |
241 |
|
"view", fullReference, fullReference, EntityType.DOCUMENT); |
242 |
1 |
expectedMap = new LinkedHashMap<>(); |
243 |
1 |
expectedMap.put("param", Collections.<String>emptyList()); |
244 |
1 |
assertEquals(expectedMap, resource.getParameters()); |
245 |
|
} |
246 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
247 |
28 |
private ResourceReference testCreateResource(String testURL, String expectedActionName,... |
248 |
|
EntityReference expectedReference, EntityReference returnedReference, EntityType expectedEntityType) |
249 |
|
throws Exception |
250 |
|
{ |
251 |
28 |
when(this.entityReferenceResolver.resolve(expectedReference, expectedEntityType)).thenReturn(returnedReference); |
252 |
28 |
ExtendedURL extendedURL = new ExtendedURL(new URL(testURL), null); |
253 |
|
|
254 |
28 |
extendedURL.getSegments().remove(0); |
255 |
28 |
EntityResourceReference entityResource = this.resolver.resolve(extendedURL, |
256 |
|
new ResourceType("bin"), Collections.<String, Object>emptyMap()); |
257 |
|
|
258 |
28 |
assertEquals(expectedActionName, entityResource.getAction().getActionName()); |
259 |
28 |
assertEquals(returnedReference, entityResource.getEntityReference()); |
260 |
|
|
261 |
28 |
return entityResource; |
262 |
|
} |
263 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
264 |
14 |
private EntityReference buildEntityReference(String wiki, List<String> spaces, String page)... |
265 |
|
{ |
266 |
14 |
return buildEntityReference(wiki, spaces, page, null); |
267 |
|
} |
268 |
|
|
|
|
| 94.1% |
Uncovered Elements: 1 (17) |
Complexity: 4 |
Complexity Density: 0.36 |
|
269 |
16 |
private EntityReference buildEntityReference(String wiki, List<String> spaces, String page, String attachment)... |
270 |
|
{ |
271 |
16 |
EntityReference entityReference = new WikiReference(wiki); |
272 |
16 |
if (spaces != null) { |
273 |
16 |
EntityReference parent = entityReference; |
274 |
16 |
for (String space : spaces) { |
275 |
23 |
entityReference = new EntityReference(space, EntityType.SPACE, parent); |
276 |
23 |
parent = entityReference; |
277 |
|
} |
278 |
|
} |
279 |
16 |
if (page != null) { |
280 |
10 |
entityReference = new EntityReference(page, EntityType.DOCUMENT, entityReference); |
281 |
|
} |
282 |
16 |
if (attachment != null) { |
283 |
2 |
entityReference = new EntityReference(attachment, EntityType.ATTACHMENT, entityReference); |
284 |
|
} |
285 |
16 |
return entityReference; |
286 |
|
} |
287 |
|
} |