1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.internal.plugin.rightsmanager; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.Arrays; |
24 |
|
import java.util.Collections; |
25 |
|
import java.util.Iterator; |
26 |
|
import java.util.List; |
27 |
|
import java.util.NoSuchElementException; |
28 |
|
|
29 |
|
import org.apache.commons.collections.IteratorUtils; |
30 |
|
import org.junit.Rule; |
31 |
|
import org.junit.Test; |
32 |
|
import org.xwiki.context.Execution; |
33 |
|
import org.xwiki.context.ExecutionContext; |
34 |
|
import org.xwiki.model.EntityType; |
35 |
|
import org.xwiki.model.reference.DocumentReference; |
36 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
37 |
|
import org.xwiki.model.reference.EntityReference; |
38 |
|
import org.xwiki.test.mockito.MockitoComponentManagerRule; |
39 |
|
|
40 |
|
import com.xpn.xwiki.XWiki; |
41 |
|
import com.xpn.xwiki.XWikiContext; |
42 |
|
import com.xpn.xwiki.XWikiException; |
43 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
44 |
|
import com.xpn.xwiki.objects.BaseObject; |
45 |
|
|
46 |
|
import static org.hamcrest.Matchers.containsInAnyOrder; |
47 |
|
import static org.junit.Assert.*; |
48 |
|
import static org.mockito.Mockito.*; |
49 |
|
|
50 |
|
|
51 |
|
@link |
52 |
|
|
53 |
|
@version |
54 |
|
@since |
55 |
|
@since |
56 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (280) |
Complexity: 31 |
Complexity Density: 0.12 |
|
57 |
|
public class ReferenceUserIteratorTest |
58 |
|
{ |
59 |
|
@Rule |
60 |
|
public MockitoComponentManagerRule componentManager = new MockitoComponentManagerRule(); |
61 |
|
|
62 |
|
private XWiki xwiki; |
63 |
|
|
64 |
|
private XWikiContext xwikiContext; |
65 |
|
|
66 |
|
private Execution execution; |
67 |
|
|
68 |
|
private DocumentReferenceResolver<String> resolver; |
69 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
1PASS
|
|
70 |
1 |
@Test... |
71 |
|
public void getMembersWhenNoExecutionContext() throws Exception |
72 |
|
{ |
73 |
1 |
Execution execution = this.componentManager.registerMockComponent(Execution.class); |
74 |
1 |
DocumentReference userReference = new DocumentReference("userwiki", "XWiki", "userpage"); |
75 |
1 |
try { |
76 |
1 |
new ReferenceUserIterator(userReference, null, execution).next(); |
77 |
|
} catch (RuntimeException expected) { |
78 |
1 |
assertEquals("Aborting member extraction from passed references [[userwiki:XWiki.userpage]] since no " |
79 |
|
+ "XWiki Context was found", expected.getMessage()); |
80 |
|
} |
81 |
|
} |
82 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
1PASS
|
|
83 |
1 |
@Test... |
84 |
|
public void getMembersWhenNoXWikiContext() throws Exception |
85 |
|
{ |
86 |
1 |
Execution execution = this.componentManager.registerMockComponent(Execution.class); |
87 |
1 |
when(execution.getContext()).thenReturn(new ExecutionContext()); |
88 |
1 |
DocumentReference userReference = new DocumentReference("userwiki", "XWiki", "userpage"); |
89 |
1 |
try { |
90 |
1 |
new ReferenceUserIterator(userReference, null, execution).next(); |
91 |
|
} catch (RuntimeException expected) { |
92 |
1 |
assertEquals("Aborting member extraction from passed references [[userwiki:XWiki.userpage]] since no " |
93 |
|
+ "XWiki Context was found", expected.getMessage()); |
94 |
|
} |
95 |
|
} |
96 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
97 |
1 |
@Test... |
98 |
|
public void getMembersWhenReferenceIsNull() throws Exception |
99 |
|
{ |
100 |
1 |
setUpBaseMocks(); |
101 |
|
|
102 |
1 |
Iterator<DocumentReference> iterator = |
103 |
|
new ReferenceUserIterator((DocumentReference) null, null, this.execution); |
104 |
|
|
105 |
1 |
assertFalse(iterator.hasNext()); |
106 |
|
} |
107 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
108 |
1 |
@Test... |
109 |
|
public void getMembersWhenSuperadmin() throws Exception |
110 |
|
{ |
111 |
1 |
setUpBaseMocks(); |
112 |
1 |
DocumentReference userReference = new DocumentReference("userwiki", "XWiki", "superadmin"); |
113 |
|
|
114 |
1 |
Iterator<DocumentReference> iterator = new ReferenceUserIterator(userReference, null, this.execution); |
115 |
|
|
116 |
1 |
assertTrue(iterator.hasNext()); |
117 |
1 |
assertEquals(userReference, iterator.next()); |
118 |
1 |
assertFalse(iterator.hasNext()); |
119 |
|
} |
120 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
121 |
1 |
@Test... |
122 |
|
public void getMembersWhenGuest() throws Exception |
123 |
|
{ |
124 |
1 |
setUpBaseMocks(); |
125 |
1 |
DocumentReference userReference = new DocumentReference("userwiki", "XWiki", "XWikiGuest"); |
126 |
|
|
127 |
1 |
Iterator<DocumentReference> iterator = new ReferenceUserIterator(userReference, null, this.execution); |
128 |
|
|
129 |
1 |
assertTrue(iterator.hasNext()); |
130 |
1 |
assertEquals(userReference, iterator.next()); |
131 |
1 |
assertFalse(iterator.hasNext()); |
132 |
|
} |
133 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
134 |
1 |
@Test... |
135 |
|
public void getMembersWhenSingleUser() throws Exception |
136 |
|
{ |
137 |
1 |
Iterator<DocumentReference> iterator = setUpSingleUser(); |
138 |
|
|
139 |
1 |
assertTrue(iterator.hasNext()); |
140 |
1 |
assertEquals(new DocumentReference("userwiki", "XWiki", "userpage"), iterator.next()); |
141 |
1 |
assertFalse(iterator.hasNext()); |
142 |
|
} |
143 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
144 |
1 |
@Test... |
145 |
|
public void getMembersWhenSingleUserWithoutCallingHasNextFirst() throws Exception |
146 |
|
{ |
147 |
1 |
Iterator<DocumentReference> iterator = setUpSingleUser(); |
148 |
|
|
149 |
1 |
assertEquals(new DocumentReference("userwiki", "XWiki", "userpage"), iterator.next()); |
150 |
1 |
assertFalse(iterator.hasNext()); |
151 |
|
} |
152 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
1PASS
|
|
153 |
1 |
@Test... |
154 |
|
public void getMembersWhenCallingNextWithNoMoreReference() throws Exception |
155 |
|
{ |
156 |
1 |
Iterator<DocumentReference> iterator = setUpSingleUser(); |
157 |
|
|
158 |
1 |
assertEquals(new DocumentReference("userwiki", "XWiki", "userpage"), iterator.next()); |
159 |
1 |
try { |
160 |
1 |
iterator.next(); |
161 |
|
} catch (NoSuchElementException expected) { |
162 |
1 |
assertEquals("No more users to extract from the passed references [[userwiki:XWiki.userpage]]", |
163 |
|
expected.getMessage()); |
164 |
|
} |
165 |
|
} |
166 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
167 |
3 |
public Iterator<DocumentReference> setUpSingleUser() throws Exception... |
168 |
|
{ |
169 |
3 |
setUpBaseMocks(); |
170 |
3 |
DocumentReference userReference = new DocumentReference("userwiki", "XWiki", "userpage"); |
171 |
3 |
setUpUserPageMocks(userReference); |
172 |
|
|
173 |
3 |
return new ReferenceUserIterator(userReference, null, this.execution); |
174 |
|
} |
175 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
176 |
1 |
@Test... |
177 |
|
public void getMembersWhenSingleReferenceButDocumentDoesntExist() throws Exception |
178 |
|
{ |
179 |
1 |
setUpBaseMocks(); |
180 |
1 |
DocumentReference userReference = new DocumentReference("wiki", "XWiki", "invalid"); |
181 |
1 |
XWikiDocument document = mock(XWikiDocument.class); |
182 |
1 |
when(document.isNew()).thenReturn(true); |
183 |
1 |
when(this.xwiki.getDocument(userReference, this.xwikiContext)).thenReturn(document); |
184 |
|
|
185 |
1 |
Iterator<DocumentReference> iterator = new ReferenceUserIterator(userReference, null, this.execution); |
186 |
|
|
187 |
1 |
assertFalse(iterator.hasNext()); |
188 |
|
} |
189 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (22) |
Complexity: 1 |
Complexity Density: 0.05 |
1PASS
|
|
190 |
1 |
@Test... |
191 |
|
public void getMembersWhenSingleUserButBothUserAndGroupReference() throws Exception |
192 |
|
{ |
193 |
1 |
setUpBaseMocks(); |
194 |
1 |
DocumentReference reference = new DocumentReference("wiki", "XWiki", "page"); |
195 |
|
|
196 |
1 |
XWikiDocument document = mock(XWikiDocument.class); |
197 |
1 |
when(document.isNew()).thenReturn(false); |
198 |
1 |
when(document.getDocumentReference()).thenReturn(reference); |
199 |
1 |
when(this.xwiki.getDocument(reference, this.xwikiContext)).thenReturn(document); |
200 |
|
|
201 |
|
|
202 |
1 |
BaseObject bo1 = mock(BaseObject.class); |
203 |
1 |
when(document.getXObject( |
204 |
|
new EntityReference("XWikiUsers", EntityType.DOCUMENT, new EntityReference("XWiki", EntityType.SPACE)))) |
205 |
|
.thenReturn(bo1); |
206 |
|
|
207 |
|
|
208 |
1 |
List<BaseObject> memberObjects = new ArrayList<>(); |
209 |
1 |
BaseObject bo2 = mock(BaseObject.class); |
210 |
1 |
when(bo2.getStringValue("member")).thenReturn("XWiki.user"); |
211 |
1 |
memberObjects.add(bo2); |
212 |
1 |
when(document.getXObjects( |
213 |
|
new EntityReference("XWikiGroups", EntityType.DOCUMENT, new EntityReference("XWiki", EntityType.SPACE)))) |
214 |
|
.thenReturn(memberObjects); |
215 |
1 |
DocumentReference userReference = new DocumentReference("wiki", "XWiki", "user"); |
216 |
1 |
when(this.resolver.resolve("XWiki.user", reference)).thenReturn(userReference); |
217 |
1 |
setUpUserPageMocks(userReference); |
218 |
|
|
219 |
1 |
Iterator<DocumentReference> iterator = new ReferenceUserIterator(reference, this.resolver, this.execution); |
220 |
|
|
221 |
1 |
assertTrue(iterator.hasNext()); |
222 |
1 |
assertEquals(new DocumentReference("wiki", "XWiki", "page"), iterator.next()); |
223 |
1 |
assertTrue(iterator.hasNext()); |
224 |
1 |
assertEquals(new DocumentReference("wiki", "XWiki", "user"), iterator.next()); |
225 |
1 |
assertFalse(iterator.hasNext()); |
226 |
|
} |
227 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
1PASS
|
|
228 |
1 |
@Test... |
229 |
|
public void getMembersWhenSingleUserButDocumentFailsToLoad() throws Exception |
230 |
|
{ |
231 |
1 |
setUpBaseMocks(); |
232 |
1 |
DocumentReference userReference = new DocumentReference("userwiki", "XWiki", "userpage"); |
233 |
1 |
when(xwiki.getDocument(userReference, this.xwikiContext)).thenThrow(new XWikiException()); |
234 |
|
|
235 |
1 |
Iterator<DocumentReference> iterator = new ReferenceUserIterator(userReference, null, this.execution); |
236 |
|
|
237 |
1 |
try { |
238 |
1 |
iterator.next(); |
239 |
|
} catch (RuntimeException expected) { |
240 |
1 |
assertEquals("Failed to get document for User or Group [userwiki:XWiki.userpage] when extracting all " |
241 |
|
+ "users for the references [[userwiki:XWiki.userpage]]", expected.getMessage()); |
242 |
|
} |
243 |
|
} |
244 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
245 |
1 |
@Test... |
246 |
|
public void getMembersWhenGroupWithTwoUsers() throws Exception |
247 |
|
{ |
248 |
1 |
setUpBaseMocks(); |
249 |
1 |
DocumentReference groupReference = new DocumentReference("groupwiki", "XWiki", "grouppage"); |
250 |
1 |
DocumentReference userReference1 = new DocumentReference("userwiki", "XWiki", "user1"); |
251 |
1 |
DocumentReference userReference2 = new DocumentReference("userwiki", "XWiki", "user2"); |
252 |
1 |
setUpGroupPageMocks(groupReference, userReference1, userReference2); |
253 |
|
|
254 |
1 |
Iterator<DocumentReference> iterator = new ReferenceUserIterator(groupReference, this.resolver, this.execution); |
255 |
|
|
256 |
1 |
assertTrue(iterator.hasNext()); |
257 |
1 |
assertEquals(userReference1, iterator.next()); |
258 |
1 |
assertTrue(iterator.hasNext()); |
259 |
1 |
assertEquals(userReference2, iterator.next()); |
260 |
1 |
assertFalse(iterator.hasNext()); |
261 |
|
} |
262 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
263 |
1 |
@Test... |
264 |
|
public void getMembersWhenTwoUsers() throws Exception |
265 |
|
{ |
266 |
1 |
setUpBaseMocks(); |
267 |
1 |
DocumentReference userReference1 = new DocumentReference("userwiki", "XWiki", "user1"); |
268 |
1 |
setUpUserPageMocks(userReference1); |
269 |
1 |
DocumentReference userReference2 = new DocumentReference("userwiki", "XWiki", "user2"); |
270 |
1 |
setUpUserPageMocks(userReference2); |
271 |
|
|
272 |
1 |
Iterator<DocumentReference> iterator = new ReferenceUserIterator(Arrays.asList(userReference1, userReference2), |
273 |
|
null, this.resolver, this.execution); |
274 |
|
|
275 |
1 |
assertTrue(iterator.hasNext()); |
276 |
1 |
assertEquals(userReference1, iterator.next()); |
277 |
1 |
assertTrue(iterator.hasNext()); |
278 |
1 |
assertEquals(userReference2, iterator.next()); |
279 |
1 |
assertFalse(iterator.hasNext()); |
280 |
|
} |
281 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
282 |
1 |
@Test... |
283 |
|
public void getMembersWhenGroupWithOneUser() throws Exception |
284 |
|
{ |
285 |
1 |
setUpBaseMocks(); |
286 |
1 |
DocumentReference groupReference = new DocumentReference("groupwiki", "XWiki", "grouppage"); |
287 |
1 |
DocumentReference userReference1 = new DocumentReference("userwiki", "XWiki", "user1"); |
288 |
1 |
setUpGroupPageMocks(groupReference, userReference1); |
289 |
|
|
290 |
1 |
Iterator<DocumentReference> iterator = new ReferenceUserIterator(groupReference, this.resolver, this.execution); |
291 |
|
|
292 |
1 |
assertTrue(iterator.hasNext()); |
293 |
1 |
assertEquals(userReference1, iterator.next()); |
294 |
1 |
assertFalse(iterator.hasNext()); |
295 |
|
} |
296 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
297 |
1 |
@Test... |
298 |
|
public void getMembersWhenGroupWithOneBlankUser() throws Exception |
299 |
|
{ |
300 |
1 |
setUpBaseMocks(); |
301 |
1 |
DocumentReference groupReference = new DocumentReference("groupwiki", "XWiki", "grouppage"); |
302 |
1 |
XWikiDocument document = mock(XWikiDocument.class); |
303 |
1 |
when(document.isNew()).thenReturn(false); |
304 |
1 |
when(document.getDocumentReference()).thenReturn(groupReference); |
305 |
1 |
when(xwiki.getDocument(groupReference, this.xwikiContext)).thenReturn(document); |
306 |
1 |
List<BaseObject> memberObjects = new ArrayList<>(); |
307 |
1 |
BaseObject bo = mock(BaseObject.class); |
308 |
1 |
when(bo.getStringValue("member")).thenReturn(""); |
309 |
1 |
memberObjects.add(bo); |
310 |
1 |
when(document.getXObjects(new DocumentReference("groupwiki", "XWiki", "XWikiGroups"))).thenReturn( |
311 |
|
memberObjects); |
312 |
|
|
313 |
1 |
Iterator<DocumentReference> iterator = new ReferenceUserIterator(groupReference, this.resolver, this.execution); |
314 |
|
|
315 |
1 |
assertFalse(iterator.hasNext()); |
316 |
|
} |
317 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
318 |
1 |
@Test... |
319 |
|
public void getMembersWhenGroupWithNullMember() throws Exception |
320 |
|
{ |
321 |
1 |
setUpBaseMocks(); |
322 |
1 |
DocumentReference groupReference = new DocumentReference("groupwiki", "XWiki", "grouppage"); |
323 |
1 |
XWikiDocument document = mock(XWikiDocument.class); |
324 |
1 |
when(document.isNew()).thenReturn(false); |
325 |
1 |
when(document.getDocumentReference()).thenReturn(groupReference); |
326 |
1 |
when(xwiki.getDocument(groupReference, this.xwikiContext)).thenReturn(document); |
327 |
1 |
List<BaseObject> memberObjects = new ArrayList<>(); |
328 |
1 |
memberObjects.add(null); |
329 |
1 |
when(document.getXObjects(new DocumentReference("groupwiki", "XWiki", "XWikiGroups"))).thenReturn( |
330 |
|
memberObjects); |
331 |
|
|
332 |
1 |
Iterator<DocumentReference> iterator = new ReferenceUserIterator(groupReference, this.resolver, this.execution); |
333 |
|
|
334 |
1 |
assertFalse(iterator.hasNext()); |
335 |
|
} |
336 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (37) |
Complexity: 1 |
Complexity Density: 0.03 |
1PASS
|
|
337 |
1 |
@Test... |
338 |
|
public void getMembersWhenGroupIsLooping() throws Exception |
339 |
|
{ |
340 |
1 |
setUpBaseMocks(); |
341 |
|
|
342 |
1 |
DocumentReference groupReference = new DocumentReference("groupwiki", "XWiki", "grouppage"); |
343 |
1 |
XWikiDocument document = mock(XWikiDocument.class); |
344 |
1 |
when(document.isNew()).thenReturn(false); |
345 |
1 |
when(document.getDocumentReference()).thenReturn(groupReference); |
346 |
1 |
when(xwiki.getDocument(groupReference, this.xwikiContext)).thenReturn(document); |
347 |
|
|
348 |
1 |
List<BaseObject> memberObjects = new ArrayList<>(); |
349 |
1 |
BaseObject bo = mock(BaseObject.class); |
350 |
1 |
when(bo.getStringValue("member")).thenReturn("XWiki.othergroup"); |
351 |
1 |
memberObjects.add(bo); |
352 |
1 |
bo = mock(BaseObject.class); |
353 |
1 |
when(bo.getStringValue("member")).thenReturn("XWiki.userpage"); |
354 |
1 |
memberObjects.add(bo); |
355 |
1 |
when(document.getXObjects( |
356 |
|
new EntityReference("XWikiGroups", EntityType.DOCUMENT, new EntityReference("XWiki", EntityType.SPACE)))) |
357 |
|
.thenReturn(memberObjects); |
358 |
|
|
359 |
1 |
DocumentReference userpageReference = new DocumentReference("groupwiki", "XWiki", "userpage"); |
360 |
1 |
setUpUserPageMocks(userpageReference); |
361 |
1 |
when(this.resolver.resolve("XWiki.userpage", groupReference)).thenReturn(userpageReference); |
362 |
|
|
363 |
1 |
DocumentReference otherGroupReference = new DocumentReference("groupwiki", "XWiki", "othergroup"); |
364 |
1 |
document = mock(XWikiDocument.class); |
365 |
1 |
when(document.isNew()).thenReturn(false); |
366 |
1 |
when(document.getDocumentReference()).thenReturn(otherGroupReference); |
367 |
1 |
when(xwiki.getDocument(otherGroupReference, this.xwikiContext)).thenReturn(document); |
368 |
|
|
369 |
1 |
memberObjects = new ArrayList<>(); |
370 |
1 |
bo = mock(BaseObject.class); |
371 |
1 |
when(bo.getStringValue("member")).thenReturn("XWiki.grouppage"); |
372 |
1 |
memberObjects.add(bo); |
373 |
1 |
bo = mock(BaseObject.class); |
374 |
1 |
when(bo.getStringValue("member")).thenReturn("XWiki.anotheruser"); |
375 |
1 |
memberObjects.add(bo); |
376 |
1 |
when(document.getXObjects( |
377 |
|
new EntityReference("XWikiGroups", EntityType.DOCUMENT, new EntityReference("XWiki", EntityType.SPACE)))) |
378 |
|
.thenReturn(memberObjects); |
379 |
|
|
380 |
1 |
DocumentReference anotheruserReference = new DocumentReference("groupwiki", "XWiki", "anotheruser"); |
381 |
1 |
setUpUserPageMocks(anotheruserReference); |
382 |
1 |
when(this.resolver.resolve("XWiki.anotheruser", otherGroupReference)).thenReturn(anotheruserReference); |
383 |
|
|
384 |
1 |
when(this.resolver.resolve("XWiki.grouppage", otherGroupReference)).thenReturn(groupReference); |
385 |
1 |
when(this.resolver.resolve("XWiki.othergroup", groupReference)).thenReturn(otherGroupReference); |
386 |
|
|
387 |
1 |
Iterator<DocumentReference> iterator = new ReferenceUserIterator(groupReference, this.resolver, this.execution); |
388 |
|
|
389 |
1 |
assertThat((List<DocumentReference>) IteratorUtils.toList(iterator), |
390 |
|
containsInAnyOrder(userpageReference, anotheruserReference)); |
391 |
|
} |
392 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 1 |
Complexity Density: 0.07 |
1PASS
|
|
393 |
1 |
@Test... |
394 |
|
public void getMembersWhenGroupHasReferenceToItself() throws Exception |
395 |
|
{ |
396 |
1 |
setUpBaseMocks(); |
397 |
1 |
DocumentReference groupReference = new DocumentReference("groupwiki", "XWiki", "grouppage"); |
398 |
1 |
XWikiDocument document = mock(XWikiDocument.class); |
399 |
1 |
when(document.isNew()).thenReturn(false); |
400 |
1 |
when(document.getDocumentReference()).thenReturn(groupReference); |
401 |
1 |
when(xwiki.getDocument(groupReference, this.xwikiContext)).thenReturn(document); |
402 |
1 |
List<BaseObject> memberObjects = new ArrayList<>(); |
403 |
1 |
BaseObject bo = mock(BaseObject.class); |
404 |
1 |
when(bo.getStringValue("member")).thenReturn("XWiki.grouppage"); |
405 |
1 |
memberObjects.add(bo); |
406 |
1 |
when(document.getXObjects(new DocumentReference("groupwiki", "XWiki", "XWikiGroups"))).thenReturn( |
407 |
|
memberObjects); |
408 |
1 |
when(this.resolver.resolve("XWiki.grouppage", groupReference)).thenReturn(groupReference); |
409 |
|
|
410 |
1 |
Iterator<DocumentReference> iterator = new ReferenceUserIterator(groupReference, this.resolver, this.execution); |
411 |
|
|
412 |
1 |
assertFalse(iterator.hasNext()); |
413 |
|
} |
414 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
415 |
1 |
@Test... |
416 |
|
public void getMembersWhenSingleReferenceNotAUser() throws Exception |
417 |
|
{ |
418 |
1 |
setUpBaseMocks(); |
419 |
1 |
XWikiDocument document = mock(XWikiDocument.class); |
420 |
1 |
when(document.isNew()).thenReturn(false); |
421 |
1 |
DocumentReference reference = new DocumentReference("somewiki", "XWiki", "somepage"); |
422 |
1 |
when(document.getDocumentReference()).thenReturn(reference); |
423 |
1 |
when(document.getXObject(new DocumentReference("somewiki", "XWiki", "XWikiUsers"))).thenReturn(null); |
424 |
1 |
when(this.xwiki.getDocument(reference, this.xwikiContext)).thenReturn(document); |
425 |
|
|
426 |
1 |
Iterator<DocumentReference> iterator = new ReferenceUserIterator(reference, null, this.execution); |
427 |
|
|
428 |
1 |
assertFalse(iterator.hasNext()); |
429 |
|
} |
430 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (19) |
Complexity: 1 |
Complexity Density: 0.05 |
1PASS
|
|
431 |
1 |
@Test... |
432 |
|
public void getMembersWhenNestedGroup() throws Exception |
433 |
|
{ |
434 |
1 |
setUpBaseMocks(); |
435 |
1 |
DocumentReference groupReference1 = new DocumentReference("groupwiki", "XWiki", "grouppage1"); |
436 |
1 |
DocumentReference userReference1 = new DocumentReference("userwiki", "XWiki", "user1"); |
437 |
1 |
DocumentReference userReference2 = new DocumentReference("userwiki", "XWiki", "user2"); |
438 |
1 |
DocumentReference groupReference2 = new DocumentReference("groupwiki", "XWiki", "grouppage2"); |
439 |
1 |
DocumentReference userReference3 = new DocumentReference("userwiki", "XWiki", "user3"); |
440 |
1 |
DocumentReference userReference4 = new DocumentReference("userwiki", "XWiki", "user4"); |
441 |
1 |
setUpGroupPageMocks(groupReference1, userReference1, userReference2, groupReference2); |
442 |
1 |
setUpGroupPageMocks(groupReference2, userReference3, userReference4); |
443 |
|
|
444 |
1 |
Iterator<DocumentReference> iterator = |
445 |
|
new ReferenceUserIterator(groupReference1, this.resolver, this.execution); |
446 |
|
|
447 |
1 |
assertTrue(iterator.hasNext()); |
448 |
1 |
assertEquals(userReference1, iterator.next()); |
449 |
1 |
assertTrue(iterator.hasNext()); |
450 |
1 |
assertEquals(userReference2, iterator.next()); |
451 |
1 |
assertTrue(iterator.hasNext()); |
452 |
1 |
assertEquals(userReference3, iterator.next()); |
453 |
1 |
assertTrue(iterator.hasNext()); |
454 |
1 |
assertEquals(userReference4, iterator.next()); |
455 |
1 |
assertFalse(iterator.hasNext()); |
456 |
|
} |
457 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
458 |
1 |
@Test... |
459 |
|
public void getMembersWhenNestedGroupAndUserAndGroupExcludes() throws Exception |
460 |
|
{ |
461 |
1 |
setUpBaseMocks(); |
462 |
1 |
DocumentReference groupReference1 = new DocumentReference("groupwiki", "XWiki", "grouppage1"); |
463 |
1 |
DocumentReference userReference1 = new DocumentReference("userwiki", "XWiki", "user1"); |
464 |
1 |
DocumentReference userReference2 = new DocumentReference("userwiki", "XWiki", "user2"); |
465 |
1 |
DocumentReference groupReference2 = new DocumentReference("groupwiki", "XWiki", "grouppage2"); |
466 |
1 |
DocumentReference userReference3 = new DocumentReference("userwiki", "XWiki", "user3"); |
467 |
1 |
DocumentReference userReference4 = new DocumentReference("userwiki", "XWiki", "user4"); |
468 |
1 |
setUpGroupPageMocks(groupReference1, userReference1, userReference2, groupReference2); |
469 |
1 |
setUpGroupPageMocks(groupReference2, userReference3, userReference4); |
470 |
|
|
471 |
1 |
Iterator<DocumentReference> iterator = new ReferenceUserIterator(Collections.singletonList(groupReference1), |
472 |
|
Arrays.asList(userReference2, groupReference2), this.resolver, this.execution); |
473 |
|
|
474 |
1 |
assertTrue(iterator.hasNext()); |
475 |
1 |
assertEquals(userReference1, iterator.next()); |
476 |
1 |
assertFalse(iterator.hasNext()); |
477 |
|
} |
478 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 3 |
Complexity Density: 0.23 |
|
479 |
6 |
private void setUpGroupPageMocks(DocumentReference... references)... |
480 |
|
throws Exception |
481 |
|
{ |
482 |
6 |
XWikiDocument document = mock(XWikiDocument.class); |
483 |
6 |
when(document.isNew()).thenReturn(false); |
484 |
6 |
when(document.getDocumentReference()).thenReturn(references[0]); |
485 |
6 |
when(xwiki.getDocument(references[0], this.xwikiContext)).thenReturn(document); |
486 |
|
|
487 |
6 |
List<BaseObject> memberObjects = new ArrayList<>(); |
488 |
19 |
for (int i = 1; i < references.length; i++) { |
489 |
13 |
BaseObject bo = mock(BaseObject.class); |
490 |
13 |
when(bo.getStringValue("member")).thenReturn( |
491 |
|
references[i].getLastSpaceReference().getName() + references[i].getName()); |
492 |
13 |
memberObjects.add(bo); |
493 |
|
} |
494 |
6 |
when(document.getXObjects( |
495 |
|
new EntityReference("XWikiGroups", EntityType.DOCUMENT, new EntityReference("XWiki", EntityType.SPACE)))) |
496 |
|
.thenReturn(memberObjects); |
497 |
|
|
498 |
19 |
for (int i = 1; i < references.length; i++) { |
499 |
13 |
setUpUserPageMocks(references[i]); |
500 |
13 |
when(this.resolver.resolve(references[i].getLastSpaceReference().getName() + references[i].getName(), |
501 |
|
references[0])).thenReturn(references[i]); |
502 |
|
} |
503 |
|
} |
504 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
505 |
21 |
private void setUpUserPageMocks(DocumentReference userReference)... |
506 |
|
throws Exception |
507 |
|
{ |
508 |
21 |
XWikiDocument document = mock(XWikiDocument.class); |
509 |
21 |
when(document.isNew()).thenReturn(false); |
510 |
21 |
when(document.getDocumentReference()).thenReturn(userReference); |
511 |
21 |
BaseObject baseObject = mock(BaseObject.class); |
512 |
21 |
when(document.getXObject( |
513 |
|
new EntityReference("XWikiUsers", EntityType.DOCUMENT, new EntityReference("XWiki", EntityType.SPACE)))) |
514 |
|
.thenReturn(baseObject); |
515 |
21 |
when(this.xwiki.getDocument(userReference, this.xwikiContext)).thenReturn(document); |
516 |
|
} |
517 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
518 |
19 |
private void setUpBaseMocks() throws Exception... |
519 |
|
{ |
520 |
19 |
this.resolver = this.componentManager.registerMockComponent(DocumentReferenceResolver.TYPE_STRING, "explicit"); |
521 |
19 |
this.execution = this.componentManager.registerMockComponent(Execution.class); |
522 |
19 |
this.xwikiContext = mock(XWikiContext.class); |
523 |
19 |
ExecutionContext executionContext = new ExecutionContext(); |
524 |
19 |
executionContext.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, this.xwikiContext); |
525 |
19 |
when(this.execution.getContext()).thenReturn(executionContext); |
526 |
19 |
this.xwiki = mock(XWiki.class); |
527 |
19 |
when(this.xwikiContext.getWiki()).thenReturn(this.xwiki); |
528 |
|
} |
529 |
|
} |