View Javadoc

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