1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.user.impl.xwiki

File XWikiGroupServiceImplTest.java

 

Code metrics

0
36
4
1
142
98
4
0.11
9
4
1

Classes

Class Line # Actions
XWikiGroupServiceImplTest 52 36 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 3 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 com.xpn.xwiki.user.impl.xwiki;
21   
22    import java.util.Arrays;
23    import java.util.Collections;
24    import java.util.HashSet;
25    import java.util.List;
26   
27    import org.junit.jupiter.api.BeforeEach;
28    import org.junit.jupiter.api.Test;
29    import org.xwiki.model.reference.DocumentReference;
30    import org.xwiki.query.Query;
31   
32    import com.xpn.xwiki.XWikiContext;
33    import com.xpn.xwiki.XWikiException;
34    import com.xpn.xwiki.doc.XWikiDocument;
35    import com.xpn.xwiki.objects.BaseObject;
36    import com.xpn.xwiki.test.MockitoOldcore;
37    import com.xpn.xwiki.test.junit5.mockito.InjectMockitoOldcore;
38    import com.xpn.xwiki.test.junit5.mockito.OldcoreTest;
39    import com.xpn.xwiki.test.reference.ReferenceComponentList;
40   
41    import static org.junit.jupiter.api.Assertions.assertEquals;
42    import static org.mockito.ArgumentMatchers.any;
43    import static org.mockito.ArgumentMatchers.anyString;
44    import static org.mockito.ArgumentMatchers.eq;
45    import static org.mockito.Mockito.doReturn;
46    import static org.mockito.Mockito.mock;
47    import static org.mockito.Mockito.verify;
48    import static org.mockito.Mockito.when;
49   
50    @OldcoreTest
51    @ReferenceComponentList
 
52    public class XWikiGroupServiceImplTest
53    {
54    @InjectMockitoOldcore
55    private MockitoOldcore oldcore;
56   
57    XWikiGroupServiceImpl groupService;
58   
59    private XWikiDocument user;
60   
61    private XWikiDocument userWithSpaces;
62   
63    private XWikiDocument group;
64   
65    private BaseObject groupObject;
66   
 
67  3 toggle @BeforeEach
68    public void before() throws Exception
69    {
70  3 this.groupService = new XWikiGroupServiceImpl();
71   
72  3 doReturn(0).when(this.oldcore.getSpyXWiki()).getMaxRecursiveSpaceChecks(any(XWikiContext.class));
73   
74  3 this.user = new XWikiDocument(new DocumentReference("wiki", "XWiki", "user"));
75  3 this.user.newXObject(new DocumentReference("wiki", "XWiki", "XWikiUser"), this.oldcore.getXWikiContext());
76  3 this.oldcore.getSpyXWiki().saveDocument(this.user, this.oldcore.getXWikiContext());
77   
78  3 this.userWithSpaces = new XWikiDocument(new DocumentReference("wiki", "XWiki", "user with spaces"));
79  3 this.userWithSpaces.newXObject(new DocumentReference("wiki", "XWiki", "XWikiUser"),
80    this.oldcore.getXWikiContext());
81  3 this.oldcore.getSpyXWiki().saveDocument(this.userWithSpaces, this.oldcore.getXWikiContext());
82   
83  3 this.group = new XWikiDocument(new DocumentReference("wiki", "XWiki", "group"));
84  3 this.groupObject =
85    this.group
86    .newXObject(new DocumentReference("wiki", "XWiki", "XWikiGroups"), this.oldcore.getXWikiContext());
87  3 this.groupObject.setStringValue("member", this.user.getFullName());
88  3 this.oldcore.getSpyXWiki().saveDocument(this.group, this.oldcore.getXWikiContext());
89   
90  3 this.oldcore.getXWikiContext().setWikiId("wiki");
91    }
92   
 
93  1 toggle @Test
94    public void testListMemberForGroup() throws XWikiException
95    {
96  1 assertEquals(
97    new HashSet<String>(Arrays.asList(this.user.getFullName())),
98    new HashSet<String>(this.groupService.listMemberForGroup(this.group.getFullName(),
99    this.oldcore.getXWikiContext())));
100   
101  1 this.groupObject.setStringValue("member", this.userWithSpaces.getFullName());
102  1 this.oldcore.getSpyXWiki().saveDocument(this.group, this.oldcore.getXWikiContext());
103   
104  1 assertEquals(new HashSet<String>(Arrays.asList(this.userWithSpaces.getFullName())), new HashSet<String>(
105    this.groupService.listMemberForGroup(this.group.getFullName(), this.oldcore.getXWikiContext())));
106    }
107   
 
108  1 toggle @Test
109    public void getAllMatchedMembersNamesForGroup() throws Exception
110    {
111  1 Query query = mock(Query.class);
112  1 when(this.oldcore.getQueryManager().createQuery(anyString(), eq(Query.HQL))).thenReturn(query);
113  1 List<Object> members = Arrays.asList("one", "two", "three");
114  1 when(query.execute()).thenReturn(members);
115   
116  1 assertEquals(members, this.groupService.getAllMatchedMembersNamesForGroup(this.group.getFullName(), "foo", 3,
117    10, false, this.oldcore.getXWikiContext()));
118   
119  1 verify(query).setWiki("wiki");
120  1 verify(query).setOffset(10);
121  1 verify(query).setLimit(3);
122  1 verify(query).bindValue("groupdocname", this.group.getFullName());
123  1 verify(query).bindValue("groupclassname", "XWiki.XWikiGroups");
124  1 verify(query).bindValue("matchfield", "%foo%");
125    }
126   
 
127  1 toggle @Test
128    public void countAllMatchedMembersNamesForGroup() throws Exception
129    {
130  1 Query query = mock(Query.class);
131  1 when(this.oldcore.getQueryManager().createQuery(anyString(), eq(Query.HQL))).thenReturn(query);
132  1 when(query.execute()).thenReturn(Collections.singletonList(5L));
133   
134  1 assertEquals(5, this.groupService.countAllMatchedMembersNamesForGroup(this.group.getFullName(), "foo",
135    this.oldcore.getXWikiContext()));
136   
137  1 verify(query).setWiki("wiki");
138  1 verify(query).bindValue("groupdocname", this.group.getFullName());
139  1 verify(query).bindValue("groupclassname", "XWiki.XWikiGroups");
140  1 verify(query).bindValue("matchfield", "%foo%");
141    }
142    }