001/**
002 * Copyright 2005-2014 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 */
016package org.kuali.rice.kim.test.service;
017
018import org.junit.Test;
019import org.kuali.rice.kim.api.group.Group;
020import org.kuali.rice.kim.api.group.GroupMember;
021import org.kuali.rice.kim.api.group.GroupService;
022import org.kuali.rice.kim.api.services.KimApiServiceLocator;
023import org.kuali.rice.kim.test.KIMTestCase;
024
025import java.util.ArrayList;
026import java.util.Collection;
027import java.util.List;
028
029import static org.junit.Assert.assertFalse;
030import static org.junit.Assert.assertTrue;
031
032/**
033 * This is a description of what this class does - kellerj don't forget to fill this in. 
034 * 
035 * @author Kuali Rice Team (rice.collab@kuali.org)
036 *
037 */
038public class GroupServiceImplTest extends KIMTestCase {
039
040        private GroupService groupService;
041
042        public void setUp() throws Exception {
043                super.setUp();
044                groupService = KimApiServiceLocator.getGroupService();
045        }
046
047        @Test
048        public void testGetDirectMemberGroupIds() {
049                List<String> groupIds = groupService.getDirectMemberGroupIds("g1");
050
051                assertTrue( "g1 must contain group g2", groupIds.contains( "g2" ) );
052                assertFalse( "g1 must not contain group g3", groupIds.contains( "g3" ) );
053
054                groupIds = groupService.getDirectMemberGroupIds("g2");
055                
056                assertTrue( "g2 must contain group g3", groupIds.contains( "g3" ) );
057                assertFalse( "g2 must not contain group g4 (inactive)", groupIds.contains( "g4" ) );
058                
059        }
060        
061        @Test
062        public void testGetMemberGroupIds() {
063                List<String> groupIds = groupService.getMemberGroupIds("g1");
064
065                assertTrue( "g1 must contain group g2", groupIds.contains( "g2" ) );
066                assertTrue( "g1 must contain group g3", groupIds.contains( "g3" ) );
067                assertFalse( "g1 must not contain group g4 (inactive)", groupIds.contains( "g4" ) );
068
069                groupIds = groupService.getMemberGroupIds("g2");
070
071                assertTrue( "g2 must contain group g3", groupIds.contains( "g3" ) );
072                assertFalse( "g2 must not contain group g1", groupIds.contains( "g1" ) );
073        }
074        
075        // test principal membership
076        @Test
077        public void testPrincipalMembership() {
078                assertTrue( "p1 must be in g2", groupService.isMemberOfGroup("p1", "g2") );
079                assertTrue( "p1 must be direct member of g2", groupService.isDirectMemberOfGroup("p1", "g2") );
080                assertTrue( "p3 must be in g2", groupService.isMemberOfGroup("p3", "g2") );
081                assertFalse( "p3 should not be a direct member of g2", groupService.isDirectMemberOfGroup("p3", "g2") );
082                assertFalse( "p4 should not be reported as a member of g2 (g4 is inactive)", groupService.isMemberOfGroup("p4", "g2") );
083                
084                // re-activate group 4
085                Group g4Info = groupService.getGroup("g4");
086        Group.Builder builder = Group.Builder.create(g4Info);
087        builder.setActive(true);
088                groupService.updateGroup("g4", builder.build());
089
090                assertTrue( "p4 should be reported as a member of g2 (now that g4 is active)", groupService.isMemberOfGroup("p4", "g2") );
091                
092        }
093
094        // test the various get methods, to verify that they work correctly against
095        // circular group memberships.
096        @Test
097        public void testCircularGetMembers() {
098                // get all principals from a circular group reference
099                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}