View Javadoc

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