View Javadoc

1   /**
2    * Copyright 2005-2011 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.service.impl;
17  
18  import com.google.common.collect.Maps;
19  import org.junit.Test;
20  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
21  import org.kuali.rice.kim.api.KimApiConstants;
22  import org.kuali.rice.kim.api.role.RoleService;
23  import org.kuali.rice.kim.impl.role.RoleServiceImpl;
24  import org.kuali.rice.kim.test.KIMTestCase;
25  
26  import javax.xml.namespace.QName;
27  import java.util.ArrayList;
28  import java.util.Collections;
29  import java.util.HashMap;
30  import java.util.List;
31  import java.util.Map;
32  
33  import static org.junit.Assert.assertTrue;
34  
35  
36  public class RoleServiceImplTest extends KIMTestCase {
37  
38  	private RoleService roleService;
39  
40  	public void setUp() throws Exception {
41  		super.setUp();
42  		roleService = (RoleService) GlobalResourceLoader.getService(
43                  new QName(KimApiConstants.Namespaces.KIM_NAMESPACE_2_0, KimApiConstants.ServiceNames.ROLE_SERVICE_SOAP));
44  	}
45  
46  	@Test
47  	public void testPrincipaHasRoleOfDirectAssignment() {
48  		List <String>roleIds = new ArrayList<String>();
49  		roleIds.add("r1");
50  		assertTrue( "p1 has direct role r1", roleService.principalHasRole("p1", roleIds,  Collections
51                  .<String, String>emptyMap() ));
52  		//assertFalse( "p4 has no direct/higher level role r1", roleService.principalHasRole("p4", roleIds, null ));
53  		Map<String, String> qualification = new HashMap<String, String>();
54  		qualification.put("Attribute 2", "CHEM");
55  		assertTrue( "p1 has direct role r1 with rp2 attr data", roleService.principalHasRole("p1", roleIds, qualification));
56  		qualification.clear();
57  		//requested qualification rolls up to a higher element in some hierarchy 
58  		// method not implemented yet, not quite clear how this works
59  		qualification.put("Attribute 3", "PHYS");
60  		assertTrue( "p1 has direct role r1 with rp2 attr data", roleService.principalHasRole("p1", roleIds, Maps.newHashMap(
61                  qualification)));
62  	}
63  
64  	@Test
65  	public void testPrincipalHasRoleOfHigherLevel() {
66  		// "p3" is in "r2" and "r2 contains "r1"
67  		List <String>roleIds = new ArrayList<String>();
68  		roleIds.add("r2");
69  		assertTrue( "p1 has assigned in higher level role r1", roleService.principalHasRole("p1", roleIds,  Collections.<String, String>emptyMap() ));
70  	}
71  	
72  	@Test
73  	public void testPrincipalHasRoleContainsGroupAssigned() {
74  		// "p2" is in "g1" and "g1" assigned to "r2"
75  		List <String>roleIds = new ArrayList<String>();
76  		roleIds.add("r2");
77  		assertTrue( "p2 is assigned to g1 and g1 assigned to r2", roleService.principalHasRole("p2", roleIds,  Collections.<String, String>emptyMap() ));
78  	}
79  	
80  	/**
81  	 * Tests to ensure that a circular role membership cannot be created via the RoleService.
82  	 * 
83  	 * @throws Exception
84  	 */
85  	@Test (expected=IllegalArgumentException.class)
86  	public void testCircularRoleAssignment() {
87  		Map<String, String> map = new HashMap<String, String>();
88  		List <String>roleIds = new ArrayList<String>();
89  		roleIds.add("r1");
90  		roleService.assignRoleToRole("r5", "AUTH_SVC_TEST2", "RoleThree", map);
91  	}
92  }