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 com.google.common.collect.Maps;
19  import org.junit.Test;
20  import org.kuali.rice.kim.api.role.RoleService;
21  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
22  import org.kuali.rice.kim.test.KIMTestCase;
23  import org.kuali.rice.test.BaselineTestCase;
24  
25  import java.util.ArrayList;
26  import java.util.Collection;
27  import java.util.Collections;
28  import java.util.HashMap;
29  import java.util.List;
30  import java.util.Map;
31  
32  import static org.junit.Assert.*;
33  
34  /**
35   * Test the RoleService
36   * 
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   *
39   */
40  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
41  public class RoleServiceTest extends KIMTestCase {
42  
43  	private RoleService roleService;
44  
45  	public void setUp() throws Exception {
46  		super.setUp();
47  		setRoleService(KimApiServiceLocator.getRoleService());
48  	}
49  	
50  	@Test
51  	public void testPrincipaHasRoleOfDirectAssignment() {
52  		List <String>roleIds = new ArrayList<String>();
53  		roleIds.add("r1");
54  		assertTrue( "p1 has direct role r1", getRoleService().principalHasRole("p1", roleIds, Collections.<String, String>emptyMap() ));
55  		//assertFalse( "p4 has no direct/higher level role r1", getRoleService().principalHasRole("p4", roleIds, null ));	
56  		Map<String, String> qualification = new HashMap<String, String>();
57  		qualification.put("Attribute 2", "CHEM");
58  		assertTrue( "p1 has direct role r1 with rp2 attr data", getRoleService().principalHasRole("p1", roleIds,
59                  qualification));
60  		qualification.clear();
61  		//requested qualification rolls up to a higher element in some hierarchy 
62  		// method not implemented yet, not quite clear how this works
63  		qualification.put("Attribute 3", "PHYS");
64  		assertTrue( "p1 has direct role r1 with rp2 attr data", getRoleService().principalHasRole("p1", roleIds, Maps.newHashMap(
65                  qualification)));
66  	}
67  
68  	@Test
69  	public void testPrincipalHasRoleOfHigherLevel() {
70  		// "p3" is in "r2" and "r2 contains "r1"
71  		List <String>roleIds = new ArrayList<String>();
72  		roleIds.add("r2");
73  		assertTrue( "p1 has assigned in higher level role r1", getRoleService().principalHasRole("p1", roleIds, Collections.<String, String>emptyMap() ));
74  	}
75  	
76  	@Test
77  	public void testPrincipalHasRoleContainsGroupAssigned() {
78  		// "p2" is in "g1" and "g1" assigned to "r2"
79  		List <String>roleIds = new ArrayList<String>();
80  		roleIds.add("r2");
81  		assertTrue( "p2 is assigned to g1 and g1 assigned to r2", getRoleService().principalHasRole("p2", roleIds, Collections.<String, String>emptyMap() ));
82  	}
83  
84  	@Test
85  	public void testGetPrincipalsFromCircularRoles() {
86  		// "p2" is in "g1" and "g1" assigned to "r2"
87  		List <String>roleIds = new ArrayList<String>();
88  		Collection <String>rolePrincipalIds;
89  		roleIds.add("r101");
90  		rolePrincipalIds = getRoleService().getRoleMemberPrincipalIds("ADDL_ROLES_TESTS", "Role A",  Collections
91                  .<String, String>emptyMap());
92  		assertNotNull(rolePrincipalIds);
93  		assertEquals("RoleTwo should have 6 principal ids", 5, rolePrincipalIds.size());
94  	}
95  	
96  	public RoleService getRoleService() {
97  		return this.roleService;
98  	}
99  
100 	public void setRoleService(RoleService roleService) {
101 		this.roleService = roleService;
102 	}
103 
104 }