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