View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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.GroupMember;
21  import org.kuali.rice.kim.api.group.GroupService;
22  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
23  import org.kuali.rice.kim.test.KIMTestCase;
24  
25  import java.util.ArrayList;
26  import java.util.Collection;
27  import java.util.List;
28  
29  import static org.junit.Assert.assertFalse;
30  import static org.junit.Assert.assertTrue;
31  
32  /**
33   * This is a description of what this class does - kellerj don't forget to fill this in. 
34   * 
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   *
37   */
38  public class GroupServiceImplTest extends KIMTestCase {
39  
40  	private GroupService groupService;
41  
42  	public void setUp() throws Exception {
43  		super.setUp();
44  		groupService = KimApiServiceLocator.getGroupService();
45  	}
46  
47  	@Test
48  	public void testGetDirectMemberGroupIds() {
49  		List<String> groupIds = groupService.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 = groupService.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 = groupService.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 = groupService.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  	// test principal membership
76  	@Test
77  	public void testPrincipalMembership() {
78  		assertTrue( "p1 must be in g2", groupService.isMemberOfGroup("p1", "g2") );
79  		assertTrue( "p1 must be direct member of g2", groupService.isDirectMemberOfGroup("p1", "g2") );
80  		assertTrue( "p3 must be in g2", groupService.isMemberOfGroup("p3", "g2") );
81  		assertFalse( "p3 should not be a direct member of g2", groupService.isDirectMemberOfGroup("p3", "g2") );
82  		assertFalse( "p4 should not be reported as a member of g2 (g4 is inactive)", groupService.isMemberOfGroup("p4", "g2") );
83  		
84  		// re-activate group 4
85  		Group g4Info = groupService.getGroup("g4");
86          Group.Builder builder = Group.Builder.create(g4Info);
87          builder.setActive(true);
88  		groupService.updateGroup("g4", builder.build());
89  
90  		assertTrue( "p4 should be reported as a member of g2 (now that g4 is active)", groupService.isMemberOfGroup("p4", "g2") );
91  		
92  	}
93  
94  	// test the various get methods, to verify that they work correctly against
95  	// circular group memberships.
96  	@Test
97  	public void testCircularGetMembers() {
98  		// get all principals from a circular group reference
99  		List<String> pIds = groupService.getMemberPrincipalIds("g101");
100 		assertTrue( "group A should have 3 members", pIds.size() == 3 );		
101 		assertTrue( "group A should have member p1", pIds.contains( "p1" ) );
102 		assertTrue( "group A should have member p3", pIds.contains( "p3" ) );
103 		assertTrue( "group A should have member p5", pIds.contains( "p5" ) );
104 
105 		// traverse completely through a circular group reference looking
106 		// for a principal that is not a member of the group.
107 		boolean isIt = groupService.isMemberOfGroup("p2", "g101");
108 		assertFalse( "p2 should not be a member of Group A", isIt );
109 		
110 		List<String> gIds = groupService.getGroupIdsByPrincipalId("p1");
111 		assertTrue( "p1 should be a member of Group A", gIds.contains("g101"));
112 		assertTrue( "p1 should be a member of Group B", gIds.contains("g102"));
113 		assertTrue( "p1 should be a member of Group C", gIds.contains("g103"));
114 		
115 		gIds = groupService.getGroupIdsByPrincipalIdAndNamespaceCode("p1", "ADDL_GROUPS_TESTS");
116 		assertTrue( "p1 should be a member of Group A", gIds.contains("g101"));
117 		assertTrue( "p1 should be a member of Group B", gIds.contains("g102"));
118 		assertTrue( "p1 should be a member of Group C", gIds.contains("g103"));
119 		
120 		List<String> inList = new ArrayList<String>();
121 		inList.add("g101");
122 		inList.add("g102");
123 		Collection<GroupMember> gMembership = groupService.getMembers(inList);
124 		assertTrue( "Should return 4 members total.", gMembership.size() == 4);
125 		
126 		gMembership = groupService.getMembersOfGroup("g102");
127 		assertTrue( "Group B should have 2 members.", gMembership.size() == 2);
128 		
129 		List<Group> gInfo = groupService.getGroupsByPrincipalId("p1");
130 		assertTrue( "p1 should be a member of at least 3 groups.", gInfo.size() >= 3);
131 		
132 		gInfo = groupService.getGroupsByPrincipalIdAndNamespaceCode("p1", "ADDL_GROUPS_TESTS");
133 		assertTrue( "p1 should be a member of exactly 3 groups with namespace = ADDL_GROUPS_TESTS.", gInfo.size() == 3);
134 		
135 		gIds = groupService.getMemberGroupIds("g101");
136 		assertTrue( "Group A should have 3 member groups", gIds.size() == 3);
137 		assertTrue( "Group B should be a member Group of Group A", gIds.contains("g102"));
138 		assertTrue( "Group C should be a member Group of Group A", gIds.contains("g103"));
139 		assertTrue( "Since these groups have a circular membership, Group A should have itself as a group member", gIds.contains("g101"));
140 		
141 		gIds = groupService.getParentGroupIds("g101");
142 		assertTrue( "Group A should have 3 parent groups", gIds.size() == 3);
143 		assertTrue( "Group B should be a parent of Group A", gIds.contains("g102"));
144 		assertTrue( "Group C should be a parent of Group A", gIds.contains("g103"));
145 		assertTrue( "Since these groups have a circular membership, Group A should be a parent of itself", gIds.contains("g101"));
146 	}
147 }