1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
63
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
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
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
88
89
90
91
92
93
94
95
96
97
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
116
117
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 }