1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.user.internal.group

File DefaultGroupManagerTest.java

 

Code metrics

0
113
20
1
338
258
22
0.19
5.65
20
1.1

Classes

Class Line # Actions
DefaultGroupManagerTest 73 113 0% 22 0
1.0100%
 

Contributing tests

This file is covered by 10 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.user.internal.group;
21   
22    import java.util.Arrays;
23    import java.util.Collection;
24    import java.util.Collections;
25    import java.util.HashSet;
26    import java.util.LinkedHashSet;
27    import java.util.List;
28    import java.util.Set;
29    import java.util.stream.Collectors;
30   
31    import org.junit.jupiter.api.BeforeEach;
32    import org.junit.jupiter.api.Test;
33    import org.xwiki.cache.CacheException;
34    import org.xwiki.cache.CacheManager;
35    import org.xwiki.cache.internal.MapCache;
36    import org.xwiki.component.manager.ComponentLookupException;
37    import org.xwiki.model.internal.reference.EntityReferenceFactory;
38    import org.xwiki.model.reference.DocumentReference;
39    import org.xwiki.test.annotation.BeforeComponent;
40    import org.xwiki.test.annotation.ComponentList;
41    import org.xwiki.test.junit5.mockito.InjectMockComponents;
42    import org.xwiki.test.junit5.mockito.MockComponent;
43    import org.xwiki.user.group.GroupException;
44    import org.xwiki.user.group.WikiTarget;
45    import org.xwiki.user.internal.group.AbstractGroupCache.GroupCacheEntry;
46    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
47    import org.xwiki.wiki.manager.WikiManagerException;
48   
49    import com.xpn.xwiki.XWikiException;
50    import com.xpn.xwiki.test.MockitoOldcore;
51    import com.xpn.xwiki.test.junit5.mockito.InjectMockitoOldcore;
52    import com.xpn.xwiki.test.junit5.mockito.OldcoreTest;
53    import com.xpn.xwiki.test.reference.ReferenceComponentList;
54    import com.xpn.xwiki.user.api.XWikiGroupService;
55   
56    import static com.xpn.xwiki.test.mockito.OldcoreMatchers.isContextWiki;
57    import static org.junit.jupiter.api.Assertions.assertEquals;
58    import static org.junit.jupiter.api.Assertions.assertSame;
59    import static org.mockito.ArgumentMatchers.any;
60    import static org.mockito.ArgumentMatchers.anyInt;
61    import static org.mockito.ArgumentMatchers.eq;
62    import static org.mockito.ArgumentMatchers.same;
63    import static org.mockito.Mockito.when;
64   
65    /**
66    * Validate {@link DefaultGroupManager}.
67    *
68    * @version $Id: 4262bac03786c913492cebe26695927ca0152302 $
69    */
70    @OldcoreTest
71    @ComponentList(value = { GroupsCache.class, MembersCache.class, EntityReferenceFactory.class })
72    @ReferenceComponentList
 
73    public class DefaultGroupManagerTest
74    {
75    private static final DocumentReference GLOBAL_USER_1 = new DocumentReference("xwiki", "XWiki", "user1");
76   
77    private static final DocumentReference GLOBAL_GROUP_1 = new DocumentReference("xwiki", "XWiki", "group1");
78   
79    private static final DocumentReference GLOBAL_GROUP_2 = new DocumentReference("xwiki", "XWiki", "group2");
80   
81    private static final DocumentReference WIKI_GROUP_1 = new DocumentReference("wiki", "XWiki", "group1");
82   
83    private static final DocumentReference WIKI_GROUP_2 = new DocumentReference("wiki", "XWiki", "group2");
84   
85    @InjectMockitoOldcore
86    private MockitoOldcore oldcore;
87   
88    @MockComponent
89    private CacheManager cacheManager;
90   
91    @InjectMockComponents
92    private DefaultGroupManager manager;
93   
94    private XWikiGroupService groupService;
95   
96    private MapCache<GroupCacheEntry> cache = new MapCache<>();
97   
98    private Set<String> wikis = new LinkedHashSet<>();
99   
 
100  10 toggle @BeforeComponent
101    public void beforeComponent() throws CacheException
102    {
103  10 when(this.cacheManager.<GroupCacheEntry>createNewCache(any())).thenReturn(this.cache);
104    }
105   
 
106  10 toggle @BeforeEach
107    public void beforeEach() throws WikiManagerException, ComponentLookupException
108    {
109  10 this.wikis.add(this.oldcore.getXWikiContext().getWikiId());
110   
111  10 this.groupService = this.oldcore.getMockGroupService();
112   
113  10 WikiDescriptorManager wikiManager = this.oldcore.getWikiDescriptorManager();
114   
115  10 when(wikiManager.getAllIds()).thenAnswer((invocation) -> {
116  14 return wikis;
117    });
118  10 when(wikiManager.getCurrentWikiId()).thenAnswer((invocation) -> {
119  8 return oldcore.getXWikiContext().getWikiId();
120    });
121    }
122   
 
123  11 toggle private void mockGroups(String wiki, DocumentReference user, List<DocumentReference> groups)
124    {
125  11 this.wikis.add(wiki);
126  11 try {
127  11 when(this.groupService.getAllGroupsReferencesForMember(same(user), anyInt(), anyInt(), isContextWiki(wiki)))
128    .thenReturn(groups);
129    } catch (XWikiException e) {
130    // Cannot happen
131    }
132    }
133   
 
134  30 toggle private void assertGetGroups(DocumentReference expected, DocumentReference reference, Object wikiTarget,
135    boolean recurse) throws GroupException
136    {
137  30 assertGetGroups(Arrays.asList(expected), reference, wikiTarget, recurse);
138    }
139   
 
140  10 toggle private void assertGetGroupsEmpty(DocumentReference reference, Object wikiTarget, boolean recurse)
141    throws GroupException
142    {
143  10 assertGetGroups(Collections.emptyList(), reference, wikiTarget, recurse);
144    }
145   
 
146  56 toggle private void assertGetGroups(Collection<DocumentReference> expected, DocumentReference reference, Object wikiTarget,
147    boolean recurse) throws GroupException
148    {
149  56 Collection<DocumentReference> actual = this.manager.getGroups(reference, wikiTarget, recurse);
150   
151  56 assertEquals(new HashSet<>(expected), new HashSet<>(actual));
152   
153    // Make sure the result is cached
154  56 assertSame(actual, this.manager.getGroups(reference, wikiTarget, recurse), "The result hasn't been cached");
155    }
156   
 
157  5 toggle private void mockMembers(DocumentReference group, List<DocumentReference> groups)
158    {
159  5 try {
160  5 when(this.groupService.getAllMembersNamesForGroup(eq(group.toString()), anyInt(), anyInt(), any()))
161    .thenReturn(groups.stream().map(element -> element.toString()).collect(Collectors.toList()));
162    } catch (XWikiException e) {
163    // Cannot happen
164    }
165    }
166   
 
167  2 toggle private void assertGetMembersEmpty(DocumentReference reference, boolean recurse) throws GroupException
168    {
169  2 assertGetMembers(Collections.emptyList(), reference, recurse);
170    }
171   
 
172  9 toggle private void assertGetMembers(DocumentReference expected, DocumentReference reference, boolean recurse)
173    throws GroupException
174    {
175  9 assertGetMembers(Arrays.asList(expected), reference, recurse);
176    }
177   
 
178  12 toggle private void assertGetMembers(Collection<DocumentReference> expected, DocumentReference reference, boolean recurse)
179    throws GroupException
180    {
181  12 Collection<DocumentReference> actual = this.manager.getMembers(reference, recurse);
182   
183  12 assertEquals(new HashSet<>(expected), new HashSet<>(actual));
184   
185    // Make sure the result is cached
186  12 assertSame(actual, this.manager.getMembers(reference, recurse), "The result hasn't been cached");
187    }
188   
 
189  1 toggle @Test
190    public void getGroupsWhenNoGroup() throws GroupException
191    {
192  1 assertGetGroupsEmpty(GLOBAL_USER_1, WikiTarget.ENTITY, false);
193  1 assertGetGroupsEmpty(GLOBAL_USER_1, WikiTarget.ENTITY, true);
194  1 assertGetGroupsEmpty(GLOBAL_USER_1, null, false);
195  1 assertGetGroupsEmpty(GLOBAL_USER_1, null, true);
196  1 assertGetGroupsEmpty(GLOBAL_USER_1, "xwiki", false);
197  1 assertGetGroupsEmpty(GLOBAL_USER_1, "xwiki", true);
198    }
199   
 
200  1 toggle @Test
201    public void getGroupsWhenOneDirect() throws GroupException
202    {
203  1 mockGroups("xwiki", GLOBAL_USER_1, Arrays.asList(GLOBAL_GROUP_1));
204   
205  1 assertGetGroups(GLOBAL_GROUP_1, GLOBAL_USER_1, WikiTarget.ENTITY, false);
206  1 assertGetGroups(GLOBAL_GROUP_1, GLOBAL_USER_1, WikiTarget.ENTITY, true);
207  1 assertGetGroups(GLOBAL_GROUP_1, GLOBAL_USER_1, null, false);
208  1 assertGetGroups(GLOBAL_GROUP_1, GLOBAL_USER_1, null, true);
209  1 assertGetGroups(GLOBAL_GROUP_1, GLOBAL_USER_1, "xwiki", false);
210  1 assertGetGroups(GLOBAL_GROUP_1, GLOBAL_USER_1, "xwiki", true);
211  1 assertGetGroupsEmpty(GLOBAL_USER_1, "otherwiki", false);
212  1 assertGetGroupsEmpty(GLOBAL_USER_1, "otherwiki", true);
213  1 assertGetGroups(GLOBAL_GROUP_1, GLOBAL_USER_1, Arrays.asList("xwiki", "otherwiki"), false);
214  1 assertGetGroups(GLOBAL_GROUP_1, GLOBAL_USER_1, Arrays.asList("xwiki", "otherwiki"), true);
215    }
216   
 
217  1 toggle @Test
218    public void getGroupsWhenOneDirectAndOneRecursive() throws GroupException
219    {
220  1 mockGroups("xwiki", GLOBAL_USER_1, Arrays.asList(GLOBAL_GROUP_1));
221  1 mockGroups("xwiki", GLOBAL_GROUP_1, Arrays.asList(GLOBAL_GROUP_2));
222  1 List<DocumentReference> all = Arrays.asList(GLOBAL_GROUP_1, GLOBAL_GROUP_2);
223   
224  1 assertGetGroups(GLOBAL_GROUP_1, GLOBAL_USER_1, WikiTarget.ENTITY, false);
225  1 assertGetGroups(all, GLOBAL_USER_1, WikiTarget.ENTITY, true);
226  1 assertGetGroups(GLOBAL_GROUP_1, GLOBAL_USER_1, null, false);
227  1 assertGetGroups(all, GLOBAL_USER_1, null, true);
228  1 assertGetGroups(GLOBAL_GROUP_1, GLOBAL_USER_1, "xwiki", false);
229  1 assertGetGroups(all, GLOBAL_USER_1, "xwiki", true);
230  1 assertGetGroupsEmpty(GLOBAL_USER_1, "otherwiki", false);
231  1 assertGetGroupsEmpty(GLOBAL_USER_1, "otherwiki", true);
232  1 assertGetGroups(GLOBAL_GROUP_1, GLOBAL_USER_1, Arrays.asList("xwiki", "otherwiki"), false);
233  1 assertGetGroups(all, GLOBAL_USER_1, Arrays.asList("xwiki", "otherwiki"), true);
234   
235  1 assertGetGroups(GLOBAL_GROUP_2, GLOBAL_GROUP_1, WikiTarget.ENTITY, false);
236  1 assertGetGroups(GLOBAL_GROUP_2, GLOBAL_GROUP_1, WikiTarget.ENTITY, true);
237    }
238   
 
239  1 toggle @Test
240    public void getGroupsWhenOneDirectOnSeveralWikis() throws GroupException
241    {
242  1 mockGroups("xwiki", GLOBAL_USER_1, Arrays.asList(GLOBAL_GROUP_1));
243  1 mockGroups("wiki", GLOBAL_USER_1, Arrays.asList(WIKI_GROUP_1));
244  1 List<DocumentReference> all = Arrays.asList(GLOBAL_GROUP_1, WIKI_GROUP_1);
245   
246  1 assertGetGroups(GLOBAL_GROUP_1, GLOBAL_USER_1, WikiTarget.ENTITY, false);
247  1 assertGetGroups(GLOBAL_GROUP_1, GLOBAL_USER_1, WikiTarget.ENTITY, true);
248  1 assertGetGroups(GLOBAL_GROUP_1, GLOBAL_USER_1, "xwiki", false);
249  1 assertGetGroups(GLOBAL_GROUP_1, GLOBAL_USER_1, "xwiki", true);
250  1 assertGetGroups(WIKI_GROUP_1, GLOBAL_USER_1, "wiki", false);
251  1 assertGetGroups(WIKI_GROUP_1, GLOBAL_USER_1, "wiki", true);
252  1 assertGetGroups(all, GLOBAL_USER_1, null, false);
253  1 assertGetGroups(all, GLOBAL_USER_1, null, true);
254  1 assertGetGroups(all, GLOBAL_USER_1, Arrays.asList("xwiki", "wiki", "otherwiki"), false);
255  1 assertGetGroups(all, GLOBAL_USER_1, Arrays.asList("xwiki", "wiki", "otherwiki"), true);
256  1 this.oldcore.getXWikiContext().setWikiId("wiki");
257  1 assertGetGroups(all, GLOBAL_USER_1, WikiTarget.ENTITY_AND_CURRENT, false);
258  1 assertGetGroups(all, GLOBAL_USER_1, WikiTarget.ENTITY_AND_CURRENT, true);
259    }
260   
 
261  1 toggle @Test
262    public void getGroupsWhenOneDirectOnRecursiveOnSeveralWikis() throws GroupException
263    {
264  1 mockGroups("xwiki", GLOBAL_USER_1, Arrays.asList(GLOBAL_GROUP_1));
265  1 mockGroups("wiki", GLOBAL_GROUP_1, Arrays.asList(WIKI_GROUP_2));
266  1 mockGroups("wiki", GLOBAL_USER_1, Arrays.asList(WIKI_GROUP_1));
267  1 mockGroups("xwiki", WIKI_GROUP_1, Arrays.asList(GLOBAL_GROUP_2));
268  1 List<DocumentReference> allDirect = Arrays.asList(GLOBAL_GROUP_1, WIKI_GROUP_1);
269  1 List<DocumentReference> all = Arrays.asList(GLOBAL_GROUP_1, GLOBAL_GROUP_2, WIKI_GROUP_1, WIKI_GROUP_2);
270   
271  1 assertGetGroups(GLOBAL_GROUP_1, GLOBAL_USER_1, WikiTarget.ENTITY, false);
272  1 assertGetGroups(GLOBAL_GROUP_1, GLOBAL_USER_1, WikiTarget.ENTITY, true);
273  1 assertGetGroups(GLOBAL_GROUP_1, GLOBAL_USER_1, "xwiki", false);
274  1 assertGetGroups(GLOBAL_GROUP_1, GLOBAL_USER_1, "xwiki", true);
275  1 assertGetGroups(WIKI_GROUP_1, GLOBAL_USER_1, "wiki", false);
276  1 assertGetGroups(WIKI_GROUP_1, GLOBAL_USER_1, "wiki", true);
277  1 assertGetGroups(allDirect, GLOBAL_USER_1, null, false);
278  1 assertGetGroups(all, GLOBAL_USER_1, null, true);
279  1 assertGetGroups(allDirect, GLOBAL_USER_1, Arrays.asList("xwiki", "wiki", "otherwiki"), false);
280  1 assertGetGroups(all, GLOBAL_USER_1, Arrays.asList("xwiki", "wiki", "otherwiki"), true);
281  1 this.oldcore.getXWikiContext().setWikiId("wiki");
282  1 assertGetGroups(allDirect, GLOBAL_USER_1, WikiTarget.ENTITY_AND_CURRENT, false);
283  1 assertGetGroups(all, GLOBAL_USER_1, WikiTarget.ENTITY_AND_CURRENT, true);
284    }
285   
 
286  1 toggle @Test
287    public void getGroupsWhenCrossReference() throws GroupException
288    {
289  1 mockGroups("xwiki", GLOBAL_GROUP_1, Arrays.asList(GLOBAL_GROUP_2));
290  1 mockGroups("xwiki", GLOBAL_GROUP_2, Arrays.asList(GLOBAL_GROUP_1));
291   
292  1 assertGetGroups(GLOBAL_GROUP_1, GLOBAL_GROUP_2, WikiTarget.ENTITY, false);
293  1 assertGetGroups(GLOBAL_GROUP_2, GLOBAL_GROUP_1, WikiTarget.ENTITY, false);
294  1 assertGetGroups(GLOBAL_GROUP_1, GLOBAL_GROUP_2, WikiTarget.ENTITY, true);
295  1 assertGetGroups(GLOBAL_GROUP_2, GLOBAL_GROUP_1, WikiTarget.ENTITY, true);
296    }
297   
 
298  1 toggle @Test
299    public void getMembersWhenNoMembers() throws GroupException
300    {
301  1 assertGetMembersEmpty(GLOBAL_GROUP_1, false);
302  1 assertGetMembersEmpty(GLOBAL_GROUP_1, true);
303    }
304   
 
305  1 toggle @Test
306    public void getMembersWhenOneDirect() throws GroupException
307    {
308  1 mockMembers(GLOBAL_GROUP_1, Arrays.asList(GLOBAL_USER_1));
309   
310  1 assertGetMembers(GLOBAL_USER_1, GLOBAL_GROUP_1, false);
311  1 assertGetMembers(GLOBAL_USER_1, GLOBAL_GROUP_1, true);
312    }
313   
 
314  1 toggle @Test
315    public void getMembersWhenOneDirectAndOneRecursive() throws GroupException
316    {
317  1 mockMembers(GLOBAL_GROUP_1, Arrays.asList(GLOBAL_USER_1));
318  1 mockMembers(GLOBAL_GROUP_2, Arrays.asList(GLOBAL_GROUP_1));
319   
320  1 assertGetMembers(GLOBAL_GROUP_1, GLOBAL_GROUP_2, false);
321  1 assertGetMembers(Arrays.asList(GLOBAL_GROUP_1, GLOBAL_USER_1), GLOBAL_GROUP_2, true);
322   
323  1 assertGetMembers(GLOBAL_USER_1, GLOBAL_GROUP_1, false);
324  1 assertGetMembers(GLOBAL_USER_1, GLOBAL_GROUP_1, true);
325    }
326   
 
327  1 toggle @Test
328    public void getMembersWhenCrossReference() throws GroupException
329    {
330  1 mockMembers(GLOBAL_GROUP_1, Arrays.asList(GLOBAL_GROUP_2));
331  1 mockMembers(GLOBAL_GROUP_2, Arrays.asList(GLOBAL_GROUP_1));
332   
333  1 assertGetMembers(GLOBAL_GROUP_1, GLOBAL_GROUP_2, false);
334  1 assertGetMembers(GLOBAL_GROUP_2, GLOBAL_GROUP_1, false);
335  1 assertGetMembers(GLOBAL_GROUP_1, GLOBAL_GROUP_2, true);
336  1 assertGetMembers(GLOBAL_GROUP_2, GLOBAL_GROUP_1, true);
337    }
338    }