View Javadoc

1   /*
2    * Copyright 2007-2008 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 java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.List;
21  
22  import javax.xml.namespace.QName;
23  
24  import org.junit.Test;
25  import org.kuali.rice.core.resourceloader.GlobalResourceLoader;
26  import org.kuali.rice.kim.bo.group.dto.GroupInfo;
27  import org.kuali.rice.kim.bo.group.dto.GroupMembershipInfo;
28  import org.kuali.rice.kim.service.impl.GroupServiceImpl;
29  import org.kuali.rice.kim.service.impl.GroupUpdateServiceImpl;
30  import org.kuali.rice.kim.test.KIMTestCase;
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 GroupServiceImpl groupService;
41  	private GroupUpdateServiceImpl groupUpdateService;
42  
43  	public void setUp() throws Exception {
44  		super.setUp();
45  		groupService = (GroupServiceImpl)GlobalResourceLoader.getService(new QName("KIM", "kimGroupService"));
46  		groupUpdateService = (GroupUpdateServiceImpl)GlobalResourceLoader.getService(new QName("KIM", "kimGroupUpdateService"));
47  	}
48  
49  	@Test
50  	public void testGetDirectMemberGroupIds() {
51  		List<String> groupIds = groupService.getDirectMemberGroupIds("g1");
52  
53  		assertTrue( "g1 must contain group g2", groupIds.contains( "g2" ) );
54  		assertFalse( "g1 must not contain group g3", groupIds.contains( "g3" ) );
55  
56  		groupIds = groupService.getDirectMemberGroupIds("g2");
57  		
58  		assertTrue( "g2 must contain group g3", groupIds.contains( "g3" ) );
59  		assertFalse( "g2 must not contain group g4 (inactive)", groupIds.contains( "g4" ) );
60  		
61  	}
62  	
63  	@Test
64  	public void testGetMemberGroupIds() {
65  		List<String> groupIds = groupService.getMemberGroupIds("g1");
66  
67  		assertTrue( "g1 must contain group g2", groupIds.contains( "g2" ) );
68  		assertTrue( "g1 must contain group g3", groupIds.contains( "g3" ) );
69  		assertFalse( "g1 must not contain group g4 (inactive)", groupIds.contains( "g4" ) );
70  
71  		groupIds = groupService.getMemberGroupIds("g2");
72  
73  		assertTrue( "g2 must contain group g3", groupIds.contains( "g3" ) );
74  		assertFalse( "g2 must not contain group g1", groupIds.contains( "g1" ) );
75  	}
76  	
77  	// test principal membership
78  	@Test
79  	public void testPrincipalMembership() {
80  		assertTrue( "p1 must be in g2", groupService.isMemberOfGroup("p1", "g2") );
81  		assertTrue( "p1 must be direct member of g2", groupService.isDirectMemberOfGroup("p1", "g2") );
82  		assertTrue( "p3 must be in g2", groupService.isMemberOfGroup("p3", "g2") );
83  		assertFalse( "p3 should not be a direct member of g2", groupService.isDirectMemberOfGroup("p3", "g2") );
84  		assertFalse( "p4 should not be reported as a member of g2 (g4 is inactive)", groupService.isMemberOfGroup("p4", "g2") );
85  		
86  		// re-activate group 4
87  		GroupInfo g4Info = groupService.getGroupInfo("g4");
88  		g4Info.setActive(true);
89  		groupUpdateService.updateGroup("g4", g4Info);
90  
91  		assertTrue( "p4 should be reported as a member of g2 (now that g4 is active)", groupService.isMemberOfGroup("p4", "g2") );
92  		
93  	}
94  
95  	// test the various get methods, to verify that they work correctly against
96  	// circular group memberships.
97  	@Test
98  	public void testCircularGetMembers() {
99  		// get all principals from a circular group reference
100 		List<String> pIds = groupService.getMemberPrincipalIds("g101");
101 		assertTrue( "group A should have 3 members", pIds.size() == 3 );		
102 		assertTrue( "group A should have member p1", pIds.contains( "p1" ) );
103 		assertTrue( "group A should have member p3", pIds.contains( "p3" ) );
104 		assertTrue( "group A should have member p5", pIds.contains( "p5" ) );
105 
106 		// traverse completely through a circular group reference looking
107 		// for a principal that is not a member of the group.
108 		boolean isIt = groupService.isMemberOfGroup("p2", "g101");
109 		assertFalse( "p2 should not be a member of Group A", isIt );
110 		
111 		List<String> gIds = groupService.getGroupIdsForPrincipal("p1");
112 		assertTrue( "p1 should be a member of Group A", gIds.contains("g101"));
113 		assertTrue( "p1 should be a member of Group B", gIds.contains("g102"));
114 		assertTrue( "p1 should be a member of Group C", gIds.contains("g103"));
115 		
116 		gIds = groupService.getGroupIdsForPrincipalByNamespace("p1", "ADDL_GROUPS_TESTS");
117 		assertTrue( "p1 should be a member of Group A", gIds.contains("g101"));
118 		assertTrue( "p1 should be a member of Group B", gIds.contains("g102"));
119 		assertTrue( "p1 should be a member of Group C", gIds.contains("g103"));
120 		
121 		List<String> inList = new ArrayList<String>();
122 		inList.add("g101");
123 		inList.add("g102");
124 		Collection<GroupMembershipInfo> gMembership = groupService.getGroupMembers(inList);
125 		assertTrue( "Should return 4 members total.", gMembership.size() == 4);
126 		
127 		gMembership = groupService.getGroupMembersOfGroup("g102");
128 		assertTrue( "Group B should have 2 members.", gMembership.size() == 2);
129 		
130 		List<GroupInfo> gInfo = groupService.getGroupsForPrincipal("p1");
131 		assertTrue( "p1 should be a member of at least 3 groups.", gInfo.size() >= 3);
132 		
133 		gInfo = groupService.getGroupsForPrincipalByNamespace("p1", "ADDL_GROUPS_TESTS");
134 		assertTrue( "p1 should be a member of exactly 3 groups with namespace = ADDL_GROUPS_TESTS.", gInfo.size() == 3);
135 		
136 		gIds = groupService.getMemberGroupIds("g101");
137 		assertTrue( "Group A should have 3 member groups", gIds.size() == 3);
138 		assertTrue( "Group B should be a member Group of Group A", gIds.contains("g102"));
139 		assertTrue( "Group C should be a member Group of Group A", gIds.contains("g103"));
140 		assertTrue( "Since these groups have a circular membership, Group A should have itself as a group member", gIds.contains("g101"));
141 		
142 		gIds = groupService.getParentGroupIds("g101");
143 		assertTrue( "Group A should have 3 parent groups", gIds.size() == 3);
144 		assertTrue( "Group B should be a parent of Group A", gIds.contains("g102"));
145 		assertTrue( "Group C should be a parent of Group A", gIds.contains("g103"));
146 		assertTrue( "Since these groups have a circular membership, Group A should be a parent of itself", gIds.contains("g101"));
147 	}
148 
149 	
150 }