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.identity.principal.Principal;
23  import org.kuali.rice.kim.api.role.Role;
24  import org.kuali.rice.kim.api.role.RoleMember;
25  import org.kuali.rice.kim.api.role.RoleService;
26  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
27  import org.kuali.rice.kim.impl.role.RoleServiceImpl;
28  import org.kuali.rice.kim.test.KIMTestCase;
29  
30  import javax.xml.namespace.QName;
31  import java.util.ArrayList;
32  import java.util.Collections;
33  import java.util.HashMap;
34  import java.util.List;
35  import java.util.Map;
36  
37  import static org.junit.Assert.assertFalse;
38  import static org.junit.Assert.assertTrue;
39  
40  
41  public class RoleServiceImplTest extends KIMTestCase {
42  
43  	private RoleService roleService;
44  
45  	public void setUp() throws Exception {
46  		super.setUp();
47  		roleService = (RoleService) GlobalResourceLoader.getService(
48                  new QName(KimApiConstants.Namespaces.KIM_NAMESPACE_2_0, KimApiConstants.ServiceNames.ROLE_SERVICE_SOAP));
49  	}
50  
51  	@Test
52  	public void testPrincipaHasRoleOfDirectAssignment() {
53  		List <String>roleIds = new ArrayList<String>();
54  		roleIds.add("r1");
55  		assertTrue( "p1 has direct role r1", roleService.principalHasRole("p1", roleIds,  Collections
56                  .<String, String>emptyMap() ));
57  		//assertFalse( "p4 has no direct/higher level role r1", roleService.principalHasRole("p4", roleIds, null ));
58  		Map<String, String> qualification = new HashMap<String, String>();
59  		qualification.put("Attribute 2", "CHEM");
60  		assertTrue( "p1 has direct role r1 with rp2 attr data", roleService.principalHasRole("p1", roleIds, qualification));
61  		qualification.clear();
62  		//requested qualification rolls up to a higher element in some hierarchy 
63  		// method not implemented yet, not quite clear how this works
64  		qualification.put("Attribute 3", "PHYS");
65  		assertTrue( "p1 has direct role r1 with rp2 attr data", roleService.principalHasRole("p1", roleIds, Maps.newHashMap(
66                  qualification)));
67  	}
68  
69  	@Test
70  	public void testPrincipalHasRoleOfHigherLevel() {
71  		// "p3" is in "r2" and "r2 contains "r1"
72  		List <String>roleIds = new ArrayList<String>();
73  		roleIds.add("r2");
74  		assertTrue( "p1 has assigned in higher level role r1", roleService.principalHasRole("p1", roleIds,  Collections.<String, String>emptyMap() ));
75  	}
76  	
77  	@Test
78  	public void testPrincipalHasRoleContainsGroupAssigned() {
79  		// "p2" is in "g1" and "g1" assigned to "r2"
80  		List <String>roleIds = new ArrayList<String>();
81  		roleIds.add("r2");
82  		assertTrue( "p2 is assigned to g1 and g1 assigned to r2", roleService.principalHasRole("p2", roleIds,  Collections.<String, String>emptyMap() ));
83  	}
84  
85      @Test
86      public void testAddPrincipalToRoleAndRemove() {
87          /*Role r2 = roleService.getRole("r2");
88          roleService.assignPrincipalToRole("user4", r2.getNamespaceCode(), r2.getName(),
89                  new HashMap<String, String>());
90  
91          assertTrue("principal should be assigned to role", roleService.principalHasRole("user4", Collections.singletonList(
92                  r2.getId()), new HashMap<String, String>()));
93          
94          roleService.removePrincipalFromRole("user4", r2.getNamespaceCode(), r2.getName(), new HashMap<String, String>());
95  
96          assertFalse("principal should not be assigned to role", roleService.principalHasRole("user4", Collections.singletonList(
97                  r2.getId()), new HashMap<String, String>()));*/
98  
99          Role r2 = roleService.getRole("r2");
100         RoleMember rm1 = roleService.assignPrincipalToRole("user4", r2.getNamespaceCode(), r2.getName(),
101                 new HashMap<String, String>());
102 
103         assertTrue("principal should be assigned to role", roleService.principalHasRole("user4", Collections.singletonList(
104                 r2.getId()), new HashMap<String, String>()));
105 
106         roleService.removePrincipalFromRole("user4", r2.getNamespaceCode(), r2.getName(), new HashMap<String, String>());
107 
108         RoleMember rm2 = roleService.assignPrincipalToRole("user4", r2.getNamespaceCode(), r2.getName(),
109                 new HashMap<String, String>());
110 
111         assertFalse(rm1.getId().equals(rm2.getId()));
112     }
113 	
114 	/**
115 	 * Tests to ensure that a circular role membership cannot be created via the RoleService.
116 	 * 
117 	 * @throws Exception
118 	 */
119 	@Test (expected=IllegalArgumentException.class)
120 	public void testCircularRoleAssignment() {
121 		Map<String, String> map = new HashMap<String, String>();
122 		List <String>roleIds = new ArrayList<String>();
123 		roleIds.add("r1");
124 		roleService.assignRoleToRole("r5", "AUTH_SVC_TEST2", "RoleThree", map);
125 	}
126 }