View Javadoc
1   /**
2    * Copyright 2005-2014 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 static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertFalse;
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.util.List;
24  
25  import org.junit.Test;
26  import org.kuali.rice.kim.api.group.Group;
27  import org.kuali.rice.kim.api.group.GroupMember;
28  import org.kuali.rice.kim.api.group.GroupService;
29  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
30  import org.kuali.rice.kim.test.KIMTestCase;
31  import org.kuali.rice.test.BaselineTestCase;
32  
33  /**
34   * Test the GroupService
35   *
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   *
38   */
39  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.ROLLBACK_CLEAR_DB)
40  public class GroupServiceTest extends KIMTestCase {
41  
42  	private GroupService groupService;
43  
44  	@Override
45      public void setUp() throws Exception {
46  		super.setUp();
47  		setGroupService(KimApiServiceLocator.getGroupService());
48  	}
49  
50      @Test
51      public void testGetGroup() {
52          Group g7 = groupService.getGroup("g7");
53          assertNotNull(g7);
54          assertEquals("GroupSeven", g7.getName());
55          assertEquals("KUALI", g7.getNamespaceCode());
56          assertEquals("Group Seven", g7.getDescription());
57  
58          // now fetch another group, this will help ensure that the cache is not always returning the same group,
59          // as per the issue reported here: https://jira.springsource.org/browse/SPR-8763
60          Group g8 = groupService.getGroup("g8");
61          assertNotNull(g8);
62          assertEquals("GroupEight", g8.getName());
63          assertEquals("KUALI", g8.getNamespaceCode());
64          assertEquals("Group Eight", g8.getDescription());
65      }
66  
67  	@Test
68  	public void testGetDirectMemberGroupIds() {
69  		List<String> groupIds = groupService.getDirectMemberGroupIds("g1");
70  
71  		assertTrue( "g1 must contain group g2", groupIds.contains( "g2" ) );
72  		assertFalse( "g1 must not contain group g3", groupIds.contains( "g3" ) );
73  
74  		groupIds = groupService.getDirectMemberGroupIds("g2");
75  
76  		assertTrue( "g2 must contain group g3", groupIds.contains( "g3" ) );
77  		assertFalse( "g2 must not contain group g4 (inactive)", groupIds.contains( "g4" ) );
78  
79  	}
80  
81  	@Test
82  	public void testGetMemberGroupIds() {
83  		List<String> groupIds = groupService.getMemberGroupIds("g1");
84  
85  		assertTrue( "g1 must contain group g2", groupIds.contains( "g2" ) );
86  		assertTrue( "g1 must contain group g3", groupIds.contains( "g3" ) );
87  		assertFalse( "g1 must not contain group g4 (inactive)", groupIds.contains( "g4" ) );
88  
89  		groupIds = groupService.getMemberGroupIds("g2");
90  
91  		assertTrue( "g2 must contain group g3", groupIds.contains( "g3" ) );
92  		assertFalse( "g2 must not contain group g1", groupIds.contains( "g1" ) );
93  	}
94  
95  	// test principal membership
96  	@Test
97  	public void testPrincipalMembership() {
98          clearNamedCache(Group.Cache.NAME);
99          clearNamedCache(GroupMember.Cache.NAME);
100 		assertTrue( "p1 must be in g2", groupService.isMemberOfGroup("p1", "g2") );
101 		assertTrue( "p1 must be direct member of g2", groupService.isDirectMemberOfGroup("p1", "g2") );
102 		assertTrue( "p3 must be in g2", groupService.isMemberOfGroup("p3", "g2") );
103 		assertFalse( "p3 should not be a direct member of g2", groupService.isDirectMemberOfGroup("p3", "g2") );
104 		assertFalse( "p4 should not be reported as a member of g2 (g4 is inactive)", groupService.isMemberOfGroup("p4", "g2") );
105 
106 		// re-activate group 4
107 		Group g4Info = groupService.getGroup("g4");
108         Group.Builder builder = Group.Builder.create(g4Info);
109 		builder.setActive(true);
110 
111 		Group ug = groupService.updateGroup("g4", builder.build());
112         assertTrue(ug.isActive());
113 
114         clearNamedCache(Group.Cache.NAME);
115         clearNamedCache(GroupMember.Cache.NAME);
116 
117 		Group gg = groupService.getGroup("g4");
118         assertTrue(gg.isActive());
119 		assertTrue( "p4 should be reported as a member of g2 (now that g4 is active)", groupService.isMemberOfGroup("p4", "g2") );
120 
121         builder = Group.Builder.create(gg);
122         builder.setActive(false);
123         groupService.updateGroup(builder.build());
124 
125         clearNamedCache(Group.Cache.NAME);
126         clearNamedCache(GroupMember.Cache.NAME);
127 
128         assertFalse( "p4 should be reported as a member of g2 (now that g4 is active)", groupService.isMemberOfGroup("p4", "g2") );
129 	}
130 
131 	public GroupService getGroupService() {
132 		return this.groupService;
133 	}
134 
135 	public void setGroupService(GroupService groupService) {
136 		this.groupService = groupService;
137 	}
138 
139 
140 }