1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.rice.kim.test.service;
17  
18  import org.junit.Test;
19  import org.kuali.rice.kim.api.group.Group;
20  import org.kuali.rice.kim.api.group.GroupService;
21  import org.kuali.rice.kim.api.group.GroupUpdateService;
22  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
23  import org.kuali.rice.kim.test.KIMTestCase;
24  
25  import java.util.List;
26  
27  import static org.junit.Assert.assertFalse;
28  import static org.junit.Assert.assertTrue;
29  
30  
31  
32  
33  
34  
35  
36  public class GroupServiceTest extends KIMTestCase {
37  
38  	private GroupService groupService;
39  	private GroupUpdateService groupUpdateService;
40  
41  	public void setUp() throws Exception {
42  		super.setUp();
43  		setGroupService(KimApiServiceLocator.getGroupService());
44  		setGroupUpdateService(KimApiServiceLocator.getGroupUpdateService());
45  	}
46  	
47  	@Test
48  	public void testGetDirectMemberGroupIds() {
49  		List<String> groupIds = getGroupService().getDirectMemberGroupIds("g1");
50  
51  		assertTrue( "g1 must contain group g2", groupIds.contains( "g2" ) );
52  		assertFalse( "g1 must not contain group g3", groupIds.contains( "g3" ) );
53  
54  		groupIds = getGroupService().getDirectMemberGroupIds("g2");
55  		
56  		assertTrue( "g2 must contain group g3", groupIds.contains( "g3" ) );
57  		assertFalse( "g2 must not contain group g4 (inactive)", groupIds.contains( "g4" ) );
58  		
59  	}
60  	
61  	@Test
62  	public void testGetMemberGroupIds() {
63  		List<String> groupIds = getGroupService().getMemberGroupIds("g1");
64  
65  		assertTrue( "g1 must contain group g2", groupIds.contains( "g2" ) );
66  		assertTrue( "g1 must contain group g3", groupIds.contains( "g3" ) );
67  		assertFalse( "g1 must not contain group g4 (inactive)", groupIds.contains( "g4" ) );
68  
69  		groupIds = getGroupService().getMemberGroupIds("g2");
70  
71  		assertTrue( "g2 must contain group g3", groupIds.contains( "g3" ) );
72  		assertFalse( "g2 must not contain group g1", groupIds.contains( "g1" ) );
73  	}
74  	
75  	
76  	@Test
77  	public void testPrincipalMembership() {
78  		assertTrue( "p1 must be in g2", getGroupService().isMemberOfGroup("p1", "g2") );
79  		assertTrue( "p1 must be direct member of g2", getGroupService().isDirectMemberOfGroup("p1", "g2") );
80  		assertTrue( "p3 must be in g2", getGroupService().isMemberOfGroup("p3", "g2") );
81  		assertFalse( "p3 should not be a direct member of g2", getGroupService().isDirectMemberOfGroup("p3", "g2") );
82  		assertFalse( "p4 should not be reported as a member of g2 (g4 is inactive)", getGroupService().isMemberOfGroup("p4", "g2") );
83  		
84  		
85  		Group g4Info = getGroupService().getGroup("g4");
86          Group.Builder builder = Group.Builder.create(g4Info);
87  		builder.setActive(true);
88  
89          
90  		getGroupUpdateService().updateGroup("g4", builder.build());
91  		g4Info = getGroupService().getGroup("g4");
92  		
93  		assertTrue( "p4 should be reported as a member of g2 (now that g4 is active)", getGroupService().isMemberOfGroup("p4", "g2") );
94  	}
95  
96  	public GroupService getGroupService() {
97  		return this.groupService;
98  	}
99  
100 	public void setGroupService(GroupService groupService) {
101 		this.groupService = groupService;
102 	}
103 
104 	public GroupUpdateService getGroupUpdateService() {
105 		return this.groupUpdateService;
106 	}
107 
108 	public void setGroupUpdateService(GroupUpdateService groupUpdateService) {
109 		this.groupUpdateService = groupUpdateService;
110 	}
111 
112 }