forked from DSpace/DSpace
/
GroupTest.java
485 lines (424 loc) · 18.9 KB
/
GroupTest.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
package org.dspace.eperson;
import org.apache.commons.lang.ArrayUtils;
import org.apache.log4j.Logger;
import org.dspace.AbstractUnitTest;
import org.dspace.authorize.AuthorizeException;
import org.dspace.eperson.factory.EPersonServiceFactory;
import org.dspace.eperson.service.EPersonService;
import org.dspace.eperson.service.GroupService;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.sql.SQLException;
import java.util.*;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.*;
/**
* Unit tests for the Group class
*
* @author kevinvandevelde at atmire.com
*/
public class GroupTest extends AbstractUnitTest {
private static final Logger log = Logger.getLogger(GroupTest.class);
//TODO: test duplicate names ?
private Group topGroup;
private Group level1Group;
private Group level2Group;
protected EPersonService ePersonService = EPersonServiceFactory.getInstance().getEPersonService();
protected GroupService groupService = EPersonServiceFactory.getInstance().getGroupService();
/**
* This method will be run before every test as per @Before. It will
* initialize resources required for the tests.
*
* Other methods can be annotated with @Before here or in subclasses
* but no execution order is guaranteed
*/
@Before
@Override
public void init()
{
super.init();
try {
//Only admins can perform group operations, so add as default user
context.turnOffAuthorisationSystem();
topGroup = createGroup("topGroup");
level1Group = createGroup("level1Group");
groupService.addMember(context, topGroup, level1Group);
level2Group = createGroup("level2Group");
groupService.addMember(context, level1Group, level2Group);
groupService.update(context, topGroup);
groupService.update(context, level1Group);
groupService.update(context, level2Group);
context.restoreAuthSystemState();
}catch(SQLException ex)
{
log.error("SQL Error in init", ex);
fail("SQL Error in init: " + ex.getMessage());
} catch (AuthorizeException ex) {
log.error("Authorization Error in init", ex);
fail("Authorization Error in init: " + ex.getMessage());
}
}
@After
@Override
public void destroy()
{
try {
context.turnOffAuthorisationSystem();
if(level1Group != null)
{
groupService.delete(context, level1Group);
level1Group = null;
}
if(level2Group != null)
{
groupService.delete(context,level2Group);
level2Group = null;
}
if(topGroup != null)
{
groupService.delete(context,topGroup);
topGroup = null;
}
context.restoreAuthSystemState();
super.destroy();
} catch (SQLException ex) {
log.error("SQL Error in init", ex);
fail("SQL Error in init: " + ex.getMessage());
} catch (AuthorizeException ex) {
log.error("Authorization Error in init", ex);
fail("Authorization Error in init: " + ex.getMessage());
} catch (IOException ex) {
log.error("IO Error in init", ex);
fail("IO Error in init: " + ex.getMessage());
}
}
@Test
public void createGroup() throws SQLException, AuthorizeException, IOException {
Group group = null;
try {
context.turnOffAuthorisationSystem();
group = groupService.create(context);
assertThat("testCreateGroup", group, notNullValue());
} finally {
if(group != null)
{
groupService.delete(context, group);
}
context.restoreAuthSystemState();
}
}
@Test(expected = AuthorizeException.class)
public void createGroupUnAuthorized() throws SQLException, AuthorizeException {
context.setCurrentUser(null);
groupService.create(context);
}
@Test
public void setGroupName() throws SQLException, AuthorizeException {
topGroup.setName(context, "new name");
groupService.update(context, topGroup);
assertThat("setGroupName 1", topGroup.getName(), notNullValue());
assertEquals("setGroupName 2", topGroup.getName(), "new name");
}
@Test
public void findByName() throws SQLException {
Group group = groupService.findByName(context, "topGroup");
assertThat("findByName 1", group, notNullValue());
assertThat("findByName 2", group.getName(), notNullValue());
assertEquals("findByName 2", group.getName(), "topGroup");
}
@Test
public void findAll() throws SQLException {
List<Group> groups = groupService.findAll(context, GroupService.NAME);
assertThat("findAll 1", groups, notNullValue());
System.out.println("TEST GROUP OUTPUT " + groups);
assertTrue("findAll 2", 0 < groups.size());
}
//No longer possible, wouldn't make sense since we are using UUID'S
// @Test
// public void findAllIdSort() throws SQLException {
// List<Group> groups = groupService.findAll(context, GroupService.ID);
//
// assertThat("findAllIdSort 1", groups, notNullValue());
//
// //Check our sorting order by adding to a treeSet & check against arraylist values
// List<String> listNames = new ArrayList<String>();
// Set<String> setNames = new TreeSet<String>();
// for (Group group : groups) {
// listNames.add(group.getID().toString());
// setNames.add(group.getID().toString());
// }
// assertTrue("findAllIdSort 2 ", ArrayUtils.isEquals(setNames.toArray(new String[setNames.size()]), listNames.toArray(new String[listNames.size()])));
// }
@Test
public void findAllNameSort() throws SQLException {
List<Group> groups = groupService.findAll(context, GroupService.NAME);
assertThat("findAllNameSort 1", groups, notNullValue());
//Check our sorting order by adding to a treeSet & check against arraylist values
List<String> listNames = new ArrayList<String>();
Set<String> setNames = new TreeSet<String>();
for (Group group : groups) {
listNames.add(group.getName());
setNames.add(group.getName());
}
assertTrue("findAllNameSort 2 ", ArrayUtils.isEquals(setNames.toArray(new String[setNames.size()]), listNames.toArray(new String[listNames.size()])));
}
@Test
public void searchByName() throws SQLException {
//We can find 2 groups so attempt to retrieve with offset 0 and a max of one
List<Group> groups = groupService.search(context, "level", 0, 1);
assertThat("search 1", groups, notNullValue());
assertEquals("search 2", groups.size(), 1);
String firstGroupName = groups.iterator().next().getName();
assertTrue("search 3", firstGroupName.equals("level1Group") || firstGroupName.equals("level2Group"));
//Retrieve the second group
groups = groupService.search(context, "level", 1, 2);
assertThat("search 1", groups, notNullValue());
assertEquals("search 2", groups.size(), 1);
String secondGroupName = groups.iterator().next().getName();
assertTrue("search 3", secondGroupName.equals("level1Group") || secondGroupName.equals("level2Group"));
}
@Test
public void searchByID() throws SQLException
{
List<Group> searchResult = groupService.search(context, String.valueOf(topGroup.getID()), 0, 10);
assertEquals("searchID 1", searchResult.size(), 1);
assertEquals("searchID 2", searchResult.iterator().next(), topGroup);
}
@Test
public void searchResultCount() throws SQLException {
assertEquals("searchResultCount", groupService.searchResultCount(context, "level"), 2);
}
@Test
public void addMemberEPerson() throws SQLException, AuthorizeException, EPersonDeletionException, IOException {
EPerson ePerson = null;
try {
ePerson = createEPersonAndAddToGroup("addMemberEPerson@dspace.org", topGroup);
groupService.update(context, topGroup);
assertEquals("addMemberEPerson 1", topGroup.getMembers().size(), 1);
assertTrue("addMemberEPerson 2", topGroup.getMembers().contains(ePerson));
} finally {
if(ePerson != null)
{
context.turnOffAuthorisationSystem();
ePersonService.delete(context, ePerson);
context.restoreAuthSystemState();
}
}
}
@Test
public void addMemberGroup() throws SQLException, AuthorizeException, EPersonDeletionException, IOException {
context.turnOffAuthorisationSystem();
Group parentGroup = createGroup("parentGroup");
Group childGroup = createGroup("childGroup");
groupService.addMember(context, parentGroup, childGroup);
groupService.update(context, parentGroup);
groupService.update(context, childGroup);
groupService.delete(context, parentGroup);
groupService.delete(context, childGroup);
context.restoreAuthSystemState();
}
@Test
public void deleteGroupEPersonMembers() throws SQLException, AuthorizeException, EPersonDeletionException, IOException {
EPerson ePerson = null;
context.turnOffAuthorisationSystem();
try {
Group toDeleteGroup = createGroup("toDelete");
ePerson = createEPerson("deleteGroupEPersonMembers@dspace.org");
groupService.addMember(context, toDeleteGroup, ePerson);
groupService.update(context, toDeleteGroup);
groupService.delete(context, toDeleteGroup);
assertEquals("deleteGroupEPersonMembers", ePerson.getGroups().size(), 0);
} finally {
if(ePerson != null)
{
ePersonService.delete(context, ePerson);
}
context.restoreAuthSystemState();
}
}
@Test
public void deleteGroupGroupMembers() throws SQLException, AuthorizeException, EPersonDeletionException, IOException {
//Delete parent first
Group parentGroup = createGroup("toDeleteParent");
Group childGroup = createGroup("toDeleteChild");
groupService.addMember(context, parentGroup, childGroup);
groupService.update(context, parentGroup);
groupService.update(context, childGroup);
groupService.delete(context, parentGroup);
groupService.delete(context, childGroup);
//Delete child first
parentGroup = createGroup("toDeleteParent");
childGroup = createGroup("toDeleteChild");
groupService.addMember(context, parentGroup, childGroup);
groupService.update(context, parentGroup);
groupService.update(context, childGroup);
groupService.delete(context, childGroup);
groupService.delete(context, parentGroup);
}
@Test
public void isMemberGroup() throws SQLException
{
assertTrue("isMemberGroup 1", groupService.isMember(topGroup, level1Group));
assertTrue("isMemberGroup 2", groupService.isMember(level1Group, level2Group));
assertFalse("isMemberGroup 3", groupService.isMember(level1Group, topGroup));
assertFalse("isMemberGroup 4", groupService.isMember(level2Group, level1Group));
}
@Test
public void isMemberEPerson() throws SQLException, AuthorizeException, EPersonDeletionException, IOException {
EPerson ePerson = null;
try {
context.turnOffAuthorisationSystem();
ePerson = createEPersonAndAddToGroup("isMemberEPerson@dspace.org", level1Group);
assertTrue(groupService.isDirectMember(level1Group, ePerson));
assertFalse(groupService.isDirectMember(topGroup, ePerson));
} finally {
if(ePerson != null)
{
ePersonService.delete(context, ePerson);
}
context.restoreAuthSystemState();
}
}
@Test
public void isMemberContext() throws SQLException, AuthorizeException, EPersonDeletionException, IOException {
EPerson ePerson = null;
try {
ePerson = createEPersonAndAddToGroup("isMemberContext@dspace.org", level2Group);
context.setCurrentUser(ePerson);
assertTrue(groupService.isMember(context, topGroup));
assertTrue(groupService.isMember(context, level1Group));
assertTrue(groupService.isMember(context, level2Group));
} finally {
if(ePerson != null)
{
context.turnOffAuthorisationSystem();
ePersonService.delete(context, ePerson);
}
}
}
@Test
public void isMemberContextGroupId() throws SQLException, AuthorizeException, EPersonDeletionException, IOException {
EPerson ePerson = null;
try {
ePerson = createEPersonAndAddToGroup("isMemberContextGroupId@dspace.org", level2Group);
context.setCurrentUser(ePerson);
assertTrue(groupService.isMember(context, topGroup));
assertTrue(groupService.isMember(context, level1Group));
assertTrue(groupService.isMember(context, level2Group));
} finally {
if(ePerson != null)
{
context.turnOffAuthorisationSystem();
ePersonService.delete(context, ePerson);
}
}
}
@Test
public void removeMemberEPerson() throws SQLException, AuthorizeException, EPersonDeletionException, IOException {
EPerson ePerson = null;
try {
// Test normal behavior, add user to group & remove
ePerson = createEPersonAndAddToGroup("removeMemberEPerson@dspace.org", level2Group);
context.setCurrentUser(ePerson);
assertTrue(groupService.isMember(context, topGroup));
assertTrue(groupService.isMember(context, level1Group));
assertTrue(groupService.isMember(context, level2Group));
groupService.removeMember(context, level2Group, ePerson);
assertFalse(groupService.isMember(context, topGroup));
assertFalse(groupService.isMember(context, level1Group));
assertFalse(groupService.isMember(context, level2Group));
//Test non recursive removal, if not a member do not add
groupService.addMember(context, level2Group, ePerson);
assertTrue(groupService.isMember(context, topGroup));
assertTrue(groupService.isMember(context, level1Group));
assertTrue(groupService.isMember(context, level2Group));
groupService.removeMember(context, topGroup, ePerson);
assertTrue(groupService.isMember(context, topGroup));
assertTrue(groupService.isMember(context, level1Group));
assertTrue(groupService.isMember(context, level2Group));
} finally {
if(ePerson != null)
{
context.turnOffAuthorisationSystem();
ePersonService.delete(context, ePerson);
}
}
}
@Test
public void removeMemberGroup() throws SQLException {
assertTrue(groupService.isMember(topGroup, level1Group));
groupService.removeMember(context, topGroup, level1Group);
assertFalse(groupService.isMember(topGroup, level1Group));
}
@Test
public void allMemberGroups() throws SQLException, AuthorizeException, EPersonDeletionException, IOException {
EPerson ePerson = createEPersonAndAddToGroup("allMemberGroups@dspace.org", level1Group);
try {
assertTrue(groupService.allMemberGroups(context, ePerson).containsAll(Arrays.asList(topGroup, level1Group)));
} finally {
context.turnOffAuthorisationSystem();
ePersonService.delete(context, ePerson);
context.restoreAuthSystemState();
}
}
@Test
public void allMembers() throws SQLException, AuthorizeException, EPersonDeletionException, IOException {
List<EPerson> allEPeopleAdded = new ArrayList<EPerson>();
try {
context.turnOffAuthorisationSystem();
allEPeopleAdded.add(createEPersonAndAddToGroup("allMemberGroups1@dspace.org", topGroup));
allEPeopleAdded.add(createEPersonAndAddToGroup("allMemberGroups2@dspace.org", level1Group));
allEPeopleAdded.add(createEPersonAndAddToGroup("allMemberGroups3@dspace.org", level2Group));
assertTrue(groupService.allMembers(context, topGroup).containsAll(allEPeopleAdded));
assertTrue(groupService.allMembers(context, level1Group).containsAll(allEPeopleAdded.subList(1, 2)));
assertTrue(groupService.allMembers(context, level2Group).containsAll(allEPeopleAdded.subList(2, 2)));
} finally {
//Remove all the people added
for (EPerson ePerson : allEPeopleAdded) {
ePersonService.delete(context, ePerson);
}
context.restoreAuthSystemState();
}
}
@Test
public void isEmpty() throws SQLException, AuthorizeException, EPersonDeletionException, IOException {
assertTrue(groupService.isEmpty(topGroup));
assertTrue(groupService.isEmpty(level1Group));
assertTrue(groupService.isEmpty(level2Group));
EPerson person = createEPersonAndAddToGroup("isEmpty@dspace.org", level2Group);
assertFalse(groupService.isEmpty(topGroup));
assertFalse(groupService.isEmpty(level1Group));
assertFalse(groupService.isEmpty(level2Group));
context.turnOffAuthorisationSystem();
ePersonService.delete(context, person);
context.restoreAuthSystemState();
assertTrue(groupService.isEmpty(level2Group));
}
protected Group createGroup(String name) throws SQLException, AuthorizeException {
context.turnOffAuthorisationSystem();
Group group = groupService.create(context);
group.setName(context, name);
groupService.update(context, group);
context.restoreAuthSystemState();
return group;
}
protected EPerson createEPersonAndAddToGroup(String email, Group group) throws SQLException, AuthorizeException {
context.turnOffAuthorisationSystem();
EPerson ePerson = createEPerson(email);
groupService.addMember(context, group, ePerson);
groupService.update(context, group);
ePersonService.update(context, ePerson);
context.restoreAuthSystemState();
return ePerson;
}
protected EPerson createEPerson(String email) throws SQLException, AuthorizeException {
context.turnOffAuthorisationSystem();
EPerson ePerson = ePersonService.create(context);
ePerson.setEmail(email);
ePersonService.update(context, ePerson);
context.restoreAuthSystemState();
return ePerson;
}
}