001 /**
002 * Copyright 2005-2011 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.kim.test.service;
017
018 import org.junit.Ignore;
019 import org.junit.Test;
020 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
021 import org.kuali.rice.kim.api.KimApiConstants;
022 import org.kuali.rice.kim.api.group.Group;
023 import org.kuali.rice.kim.api.group.GroupMember;
024 import org.kuali.rice.kim.impl.group.GroupServiceImpl;
025 import org.kuali.rice.kim.test.KIMTestCase;
026
027 import java.util.ArrayList;
028 import java.util.Collection;
029 import java.util.List;
030
031 import static org.junit.Assert.assertFalse;
032 import static org.junit.Assert.assertTrue;
033
034 /**
035 * This is a description of what this class does - kellerj don't forget to fill this in.
036 *
037 * @author Kuali Rice Team (rice.collab@kuali.org)
038 *
039 */
040 @Ignore
041 public class GroupServiceImplTest extends KIMTestCase {
042
043 private GroupServiceImpl groupService;
044
045 public void setUp() throws Exception {
046 super.setUp();
047 groupService = (GroupServiceImpl)GlobalResourceLoader.getService(KimApiConstants.ServiceNames.GROUP_SERVICE_SOAP);
048 }
049
050 @Test
051 public void testGetDirectMemberGroupIds() {
052 List<String> groupIds = groupService.getDirectMemberGroupIds("g1");
053
054 assertTrue( "g1 must contain group g2", groupIds.contains( "g2" ) );
055 assertFalse( "g1 must not contain group g3", groupIds.contains( "g3" ) );
056
057 groupIds = groupService.getDirectMemberGroupIds("g2");
058
059 assertTrue( "g2 must contain group g3", groupIds.contains( "g3" ) );
060 assertFalse( "g2 must not contain group g4 (inactive)", groupIds.contains( "g4" ) );
061
062 }
063
064 @Test
065 public void testGetMemberGroupIds() {
066 List<String> groupIds = groupService.getMemberGroupIds("g1");
067
068 assertTrue( "g1 must contain group g2", groupIds.contains( "g2" ) );
069 assertTrue( "g1 must contain group g3", groupIds.contains( "g3" ) );
070 assertFalse( "g1 must not contain group g4 (inactive)", groupIds.contains( "g4" ) );
071
072 groupIds = groupService.getMemberGroupIds("g2");
073
074 assertTrue( "g2 must contain group g3", groupIds.contains( "g3" ) );
075 assertFalse( "g2 must not contain group g1", groupIds.contains( "g1" ) );
076 }
077
078 // test principal membership
079 @Test
080 public void testPrincipalMembership() {
081 assertTrue( "p1 must be in g2", groupService.isMemberOfGroup("p1", "g2") );
082 assertTrue( "p1 must be direct member of g2", groupService.isDirectMemberOfGroup("p1", "g2") );
083 assertTrue( "p3 must be in g2", groupService.isMemberOfGroup("p3", "g2") );
084 assertFalse( "p3 should not be a direct member of g2", groupService.isDirectMemberOfGroup("p3", "g2") );
085 assertFalse( "p4 should not be reported as a member of g2 (g4 is inactive)", groupService.isMemberOfGroup("p4", "g2") );
086
087 // re-activate group 4
088 Group g4Info = groupService.getGroup("g4");
089 Group.Builder builder = Group.Builder.create(g4Info);
090 builder.setActive(true);
091 groupService.updateGroup("g4", builder.build());
092
093 assertTrue( "p4 should be reported as a member of g2 (now that g4 is active)", groupService.isMemberOfGroup("p4", "g2") );
094
095 }
096
097 // test the various get methods, to verify that they work correctly against
098 // circular group memberships.
099 @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 }