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 */ 016 package org.kuali.rice.kim.test.service; 017 018 import com.google.common.collect.Maps; 019 import org.junit.Test; 020 import org.kuali.rice.kim.api.role.RoleService; 021 import org.kuali.rice.kim.api.services.KimApiServiceLocator; 022 import org.kuali.rice.kim.test.KIMTestCase; 023 import org.kuali.rice.test.BaselineTestCase; 024 025 import java.util.ArrayList; 026 import java.util.Collection; 027 import java.util.Collections; 028 import java.util.HashMap; 029 import java.util.List; 030 import java.util.Map; 031 032 import static org.junit.Assert.*; 033 034 /** 035 * Test the RoleService 036 * 037 * @author Kuali Rice Team (rice.collab@kuali.org) 038 * 039 */ 040 @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE) 041 public class RoleServiceTest extends KIMTestCase { 042 043 private RoleService roleService; 044 045 public void setUp() throws Exception { 046 super.setUp(); 047 setRoleService(KimApiServiceLocator.getRoleService()); 048 } 049 050 @Test 051 public void testPrincipaHasRoleOfDirectAssignment() { 052 List <String>roleIds = new ArrayList<String>(); 053 roleIds.add("r1"); 054 assertTrue( "p1 has direct role r1", getRoleService().principalHasRole("p1", roleIds, Collections.<String, String>emptyMap() )); 055 //assertFalse( "p4 has no direct/higher level role r1", getRoleService().principalHasRole("p4", roleIds, null )); 056 Map<String, String> qualification = new HashMap<String, String>(); 057 qualification.put("Attribute 2", "CHEM"); 058 assertTrue( "p1 has direct role r1 with rp2 attr data", getRoleService().principalHasRole("p1", roleIds, 059 qualification)); 060 qualification.clear(); 061 //requested qualification rolls up to a higher element in some hierarchy 062 // method not implemented yet, not quite clear how this works 063 qualification.put("Attribute 3", "PHYS"); 064 assertTrue( "p1 has direct role r1 with rp2 attr data", getRoleService().principalHasRole("p1", roleIds, Maps.newHashMap( 065 qualification))); 066 } 067 068 @Test 069 public void testPrincipalHasRoleOfHigherLevel() { 070 // "p3" is in "r2" and "r2 contains "r1" 071 List <String>roleIds = new ArrayList<String>(); 072 roleIds.add("r2"); 073 assertTrue( "p1 has assigned in higher level role r1", getRoleService().principalHasRole("p1", roleIds, Collections.<String, String>emptyMap() )); 074 } 075 076 @Test 077 public void testPrincipalHasRoleContainsGroupAssigned() { 078 // "p2" is in "g1" and "g1" assigned to "r2" 079 List <String>roleIds = new ArrayList<String>(); 080 roleIds.add("r2"); 081 assertTrue( "p2 is assigned to g1 and g1 assigned to r2", getRoleService().principalHasRole("p2", roleIds, Collections.<String, String>emptyMap() )); 082 } 083 084 @Test 085 public void testGetPrincipalsFromCircularRoles() { 086 // "p2" is in "g1" and "g1" assigned to "r2" 087 List <String>roleIds = new ArrayList<String>(); 088 Collection <String>rolePrincipalIds; 089 roleIds.add("r101"); 090 rolePrincipalIds = getRoleService().getRoleMemberPrincipalIds("ADDL_ROLES_TESTS", "Role A", Collections 091 .<String, String>emptyMap()); 092 assertNotNull(rolePrincipalIds); 093 assertEquals("RoleTwo should have 6 principal ids", 5, rolePrincipalIds.size()); 094 } 095 096 public RoleService getRoleService() { 097 return this.roleService; 098 } 099 100 public void setRoleService(RoleService roleService) { 101 this.roleService = roleService; 102 } 103 104 }