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 static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
23
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import javax.xml.namespace.QName;
31
32 import org.apache.commons.lang.StringUtils;
33 import org.joda.time.DateTime;
34 import org.joda.time.format.DateTimeFormat;
35 import org.joda.time.format.DateTimeFormatter;
36 import org.junit.Test;
37 import org.kuali.rice.core.api.delegation.DelegationType;
38 import org.kuali.rice.core.api.exception.RiceIllegalStateException;
39 import org.kuali.rice.core.api.membership.MemberType;
40 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
41 import org.kuali.rice.kew.api.action.ActionType;
42 import org.kuali.rice.kim.api.KimApiConstants;
43 import org.kuali.rice.kim.api.KimConstants;
44 import org.kuali.rice.kim.api.common.delegate.DelegateMember;
45 import org.kuali.rice.kim.api.role.Role;
46 import org.kuali.rice.kim.api.role.RoleMember;
47 import org.kuali.rice.kim.api.role.RoleMembership;
48 import org.kuali.rice.kim.api.role.RoleResponsibilityAction;
49 import org.kuali.rice.kim.api.role.RoleService;
50 import org.kuali.rice.kim.impl.common.delegate.DelegateMemberAttributeDataBo;
51 import org.kuali.rice.kim.impl.common.delegate.DelegateMemberBo;
52 import org.kuali.rice.kim.impl.common.delegate.DelegateTypeBo;
53 import org.kuali.rice.kim.impl.role.RoleMemberAttributeDataBo;
54 import org.kuali.rice.kim.impl.role.RoleMemberBo;
55 import org.kuali.rice.kim.test.KIMTestCase;
56 import org.kuali.rice.krad.data.KradDataServiceLocator;
57 import org.kuali.rice.krad.data.PersistenceOption;
58
59 import com.google.common.collect.Maps;
60
61 public class RoleServiceImplTest extends KIMTestCase {
62 private RoleService roleService;
63 static final DateTimeFormatter FORMATTER = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
64 static final String ROLE_MEMBER_ID1 = "45123";
65 static final String ROLE_ID = "100";
66 static final String MEMBER_ID = "1";
67 static final MemberType MEMBER_TYPE_R = MemberType.ROLE;
68 static final String ACTIVE_FROM_STRING = "2011-01-01 12:00:00";
69 static final DateTime ACTIVE_FROM = new DateTime(FORMATTER.parseDateTime(ACTIVE_FROM_STRING));
70 static final String ACTIVE_TO_STRING1 = "2013-01-01 12:00:00";
71 static final String ACTIVE_TO_STRING2 = "2014-01-01 12:00:00";
72 static final DateTime ACTIVE_TO1 = new DateTime(FORMATTER.parseDateTime(ACTIVE_TO_STRING1));
73 static final DateTime ACTIVE_TO2 = new DateTime(FORMATTER.parseDateTime(ACTIVE_TO_STRING2));
74
75 @Override
76 public void setUp() throws Exception {
77 super.setUp();
78 roleService = (RoleService) GlobalResourceLoader.getService(
79 new QName(KimApiConstants.Namespaces.KIM_NAMESPACE_2_0, KimApiConstants.ServiceNames.ROLE_SERVICE_SOAP));
80 }
81
82 @Test
83 public void testPrincipaHasRoleOfDirectAssignment() {
84 List <String>roleIds = new ArrayList<String>();
85 roleIds.add("r1");
86 assertTrue( "p1 has direct role r1", roleService.principalHasRole("p1", roleIds, Collections
87 .<String, String>emptyMap() ));
88
89 Map<String, String> qualification = new HashMap<String, String>();
90 qualification.put("Attribute 2", "CHEM");
91 assertTrue( "p1 has direct role r1 with rp2 attr data", roleService.principalHasRole("p1", roleIds, qualification));
92 qualification.clear();
93
94
95 qualification.put("Attribute 3", "PHYS");
96 assertTrue( "p1 has direct role r1 with rp2 attr data", roleService.principalHasRole("p1", roleIds, Maps.newHashMap(
97 qualification)));
98 }
99
100 @Test
101 public void testPrincipalHasRoleOfHigherLevel() {
102
103 List <String>roleIds = new ArrayList<String>();
104 roleIds.add("r2");
105 assertTrue( "p1 has assigned in higher level role r1", roleService.principalHasRole("p1", roleIds, Collections.<String, String>emptyMap() ));
106 assertTrue( "p1 has assigned in higher level role r1", roleService.principalHasRole("p1", roleIds, Collections.<String, String>emptyMap() ));
107 }
108
109 @Test
110 public void testDelegateMemberCreateUpdateRemove() {
111
112 Role r2 = roleService.getRole("r2");
113 RoleMember rm1 = roleService.assignPrincipalToRole("user2", r2.getNamespaceCode(), r2.getName(),
114 new HashMap<String, String>());
115 String kimTypeId = "1";
116
117
118 String id = getNextSequenceStringValue("KRIM_DLGN_MBR_ID_S");
119 DelegateTypeBo delegate = new DelegateTypeBo();
120 delegate.setDelegationId(id);
121 delegate.setDelegationType(DelegationType.PRIMARY);
122 delegate.setRoleId(r2.getId());
123 delegate.setActive(true);
124 delegate.setKimTypeId("" + kimTypeId);
125 delegate = KradDataServiceLocator.getDataObjectService().save(delegate, PersistenceOption.FLUSH);
126
127
128 DelegateMember.Builder delegateMemberInfo = DelegateMember.Builder.create();
129 delegateMemberInfo.setAttributes(Collections.<String, String>emptyMap());
130 delegateMemberInfo.setDelegationId(delegate.getDelegationId());
131 delegateMemberInfo.setMemberId("user4");
132 delegateMemberInfo.setRoleMemberId(rm1.getId());
133 delegateMemberInfo.setType( MemberType.PRINCIPAL );
134 DelegateMember inDelegateMember = delegateMemberInfo.build();
135 DelegateMember newDelegateMember = roleService.createDelegateMember(inDelegateMember);
136 assertNotNull("delegateMember not created",newDelegateMember);
137
138
139 delegateMemberInfo.setDelegationMemberId(newDelegateMember.getDelegationMemberId());
140 DateTime dateTimeFrom = DateTime.now().minusDays(3);
141 delegateMemberInfo.setActiveFromDate(dateTimeFrom);
142 DateTime dateTimeTo = DateTime.now().plusDays(3);
143 delegateMemberInfo.setActiveToDate(dateTimeTo);
144 inDelegateMember = delegateMemberInfo.build();
145 roleService.updateDelegateMember(inDelegateMember);
146 DelegateMember updatedDelegateMember = DelegateMember.Builder.create( KradDataServiceLocator.getDataObjectService().find(DelegateMemberBo.class, inDelegateMember.getDelegationMemberId()) ).build();
147
148 assertEquals("Delegate member was updated",newDelegateMember.getDelegationMemberId(),updatedDelegateMember.getDelegationMemberId());
149 assertNotNull("updateDelegateMember not created",updatedDelegateMember);
150 assertEquals("activeFromDate not updated",dateTimeFrom,updatedDelegateMember.getActiveFromDate());
151 assertEquals("activeToDate not updated",dateTimeTo,updatedDelegateMember.getActiveToDate());
152
153
154 roleService.removeDelegateMembers(Collections.singletonList(updatedDelegateMember));
155 DelegateMemberBo removedDelegateMember = KradDataServiceLocator.getDataObjectService().find(DelegateMemberBo.class, updatedDelegateMember.getDelegationMemberId());
156
157 assertNotNull("after removal, versionNumber should not be null", removedDelegateMember.getVersionNumber());
158 assertEquals("removeDelegateMembers did not update the existing member", new Long(updatedDelegateMember.getVersionNumber() + 1), removedDelegateMember.getVersionNumber() );
159 assertNotNull("after removal, active to date should not be null", removedDelegateMember.getActiveToDate());
160 assertTrue("removeDelegateMembers did not update activeToDate",removedDelegateMember.getActiveToDate().isBeforeNow());
161 }
162
163 @Test
164 public void testRoleMemberCreateUpdate() {
165
166 Role roleId = roleService.getRole(ROLE_ID);
167 List<String> roleIds = new ArrayList<String>();
168 roleIds.add(roleId.getId());
169
170 Map<String,String> attributes = new HashMap<String,String>();
171 attributes.put("parameterName", "parameterNameBefore");
172 attributes.put("namespaceCode", "namespaceCodeBefore");
173 attributes.put("componentName", "componentNameBefore");
174
175 RoleMember roleMember = roleService.createRoleMember(RoleMember.Builder.create(ROLE_ID, ROLE_MEMBER_ID1, MEMBER_ID, MEMBER_TYPE_R, ACTIVE_FROM, ACTIVE_TO1, attributes, "", "").build());
176 RoleMemberBo rmBo = getRoleMemberBo(roleMember.getId());
177
178 RoleMember.Builder updatedRoleMember = RoleMember.Builder.create(roleMember);
179 updatedRoleMember.setActiveToDate(ACTIVE_TO2);
180 Map<String,String> newAttributes = new HashMap<String,String>();
181 newAttributes.put("parameterName", "parameterNameAfter");
182 newAttributes.put("namespaceCode", "namespaceCodeAfter");
183 newAttributes.put("componentName", "componentNameAfter");
184 updatedRoleMember.setAttributes(newAttributes);
185
186 roleService.updateRoleMember(updatedRoleMember.build());
187 RoleMemberBo updatedRmBo = getRoleMemberBo(roleMember.getId());
188
189 assertEquals(3,rmBo.getAttributeDetails().size());
190 assertEquals(3,updatedRmBo.getAttributeDetails().size());
191
192 for (RoleMemberAttributeDataBo newRoleMemberAttrDataBo : updatedRmBo.getAttributeDetails()) {
193 for (RoleMemberAttributeDataBo oldRoleMemberAttrDataBo : rmBo.getAttributeDetails()) {
194 if (newRoleMemberAttrDataBo.getKimTypeId().equals(oldRoleMemberAttrDataBo.getKimTypeId()) &&
195 newRoleMemberAttrDataBo.getKimAttributeId().equals(oldRoleMemberAttrDataBo.getKimAttributeId())) {
196 assertEquals("updated role member version number incorrect", new Long(2), newRoleMemberAttrDataBo.getVersionNumber());
197 }
198 }
199 }
200 }
201
202 @Test
203 public void testRoleMemberCreateUpdateNoAttrChange() {
204
205 Role roleId = roleService.getRole(ROLE_ID);
206 List<String> roleIds = new ArrayList<String>();
207 roleIds.add(roleId.getId());
208
209 Map<String,String> attributes = new HashMap<String,String>();
210 attributes.put("parameterName", "parameterNameBefore");
211 attributes.put("namespaceCode", "namespaceCodeBefore");
212 attributes.put("componentName", "componentNameBefore");
213
214 RoleMember roleMember = roleService.createRoleMember(RoleMember.Builder.create(ROLE_ID, ROLE_MEMBER_ID1, MEMBER_ID, MEMBER_TYPE_R, ACTIVE_FROM, ACTIVE_TO1, attributes, "", "").build());
215 RoleMemberBo rmBo = getRoleMemberBo(roleMember.getId());
216
217 RoleMember.Builder updatedRoleMember = RoleMember.Builder.create(roleMember);
218 updatedRoleMember.setActiveToDate(ACTIVE_TO2);
219 updatedRoleMember.setAttributes(rmBo.getAttributes());
220
221 roleService.updateRoleMember(updatedRoleMember.build());
222 RoleMemberBo updatedRmBo = getRoleMemberBo(roleMember.getId());
223
224 assertEquals(3,rmBo.getAttributeDetails().size());
225 assertEquals(3,updatedRmBo.getAttributeDetails().size());
226
227 for (RoleMemberAttributeDataBo newRoleMemberAttrDataBo : updatedRmBo.getAttributeDetails()) {
228 for (RoleMemberAttributeDataBo oldRoleMemberAttrDataBo : rmBo.getAttributeDetails()) {
229 if (newRoleMemberAttrDataBo.getKimTypeId().equals(oldRoleMemberAttrDataBo.getKimTypeId()) &&
230 newRoleMemberAttrDataBo.getKimAttributeId().equals(oldRoleMemberAttrDataBo.getKimAttributeId())) {
231 assertEquals(oldRoleMemberAttrDataBo.getAttributeValue(), newRoleMemberAttrDataBo.getAttributeValue());
232 assertEquals("updated role member version number incorrect (since no update - should not have been changed)", new Long(1), newRoleMemberAttrDataBo.getVersionNumber());
233 }
234 }
235 }
236 }
237
238 @Test
239 public void testRoleMemberCreateUpdateRemoveOneAttr() {
240
241 Role roleId = roleService.getRole(ROLE_ID);
242 List<String> roleIds = new ArrayList<String>();
243 roleIds.add(roleId.getId());
244
245 Map<String,String> attributes = new HashMap<String,String>();
246 attributes.put("parameterName", "parameterNameBefore");
247 attributes.put("namespaceCode", "namespaceCodeBefore");
248 attributes.put("componentName", "componentNameBefore");
249
250 RoleMember roleMember = roleService.createRoleMember(RoleMember.Builder.create(ROLE_ID, ROLE_MEMBER_ID1, MEMBER_ID, MEMBER_TYPE_R, ACTIVE_FROM, ACTIVE_TO1, attributes, "", "").build());
251 RoleMemberBo rmBo = getRoleMemberBo(roleMember.getId());
252 assertEquals(3,rmBo.getAttributeDetails().size());
253
254 RoleMember.Builder updatedRoleMember = RoleMember.Builder.create(roleMember);
255 updatedRoleMember.setActiveToDate(ACTIVE_TO2);
256 Map<String,String> newAttributes = new HashMap<String,String>();
257 newAttributes.put("parameterName", "parameterNameAfter");
258 newAttributes.put("namespaceCode", "namespaceCodeAfter");
259 updatedRoleMember.setAttributes(newAttributes);
260
261 roleService.updateRoleMember(updatedRoleMember.build());
262 RoleMemberBo updatedRmBo = getRoleMemberBo(roleMember.getId());
263
264 assertEquals(2, updatedRmBo.getAttributeDetails().size());
265
266 for (RoleMemberAttributeDataBo newRoleMemberAttrDataBo : updatedRmBo.getAttributeDetails()) {
267 for (RoleMemberAttributeDataBo oldRoleMemberAttrDataBo : rmBo.getAttributeDetails()) {
268 if (newRoleMemberAttrDataBo.getKimTypeId().equals(oldRoleMemberAttrDataBo.getKimTypeId()) &&
269 newRoleMemberAttrDataBo.getKimAttributeId().equals(oldRoleMemberAttrDataBo.getKimAttributeId())) {
270 assertEquals(new Long(2), newRoleMemberAttrDataBo.getVersionNumber());
271 }
272 }
273 }
274 }
275
276 @Test
277 public void testRoleMemberCreateUpdateAddOneAttr() {
278
279 Role roleId = roleService.getRole(ROLE_ID);
280 List<String> roleIds = new ArrayList<String>();
281 roleIds.add(roleId.getId());
282
283 Map<String,String> attributes = new HashMap<String,String>();
284 attributes.put("parameterName", "parameterNameBefore");
285 attributes.put("namespaceCode", "namespaceCodeBefore");
286
287 RoleMember roleMember = roleService.createRoleMember(RoleMember.Builder.create(ROLE_ID, ROLE_MEMBER_ID1, MEMBER_ID, MEMBER_TYPE_R, ACTIVE_FROM, ACTIVE_TO1, attributes, "", "").build());
288 RoleMemberBo rmBo = getRoleMemberBo(roleMember.getId());
289 assertEquals("Original role member number of attributes is incorrect", 2,rmBo.getAttributeDetails().size());
290
291 RoleMember.Builder updatedRoleMember = RoleMember.Builder.create(roleMember);
292 updatedRoleMember.setActiveToDate(ACTIVE_TO2);
293 Map<String,String> newAttributes = new HashMap<String,String>();
294 newAttributes.put("parameterName", "parameterNameAfter");
295 newAttributes.put("namespaceCode", "namespaceCodeAfter");
296 newAttributes.put("componentName", "componentNameAfter");
297
298 updatedRoleMember.setAttributes(newAttributes);
299
300 roleService.updateRoleMember(updatedRoleMember.build());
301 RoleMemberBo updatedRmBo = getRoleMemberBo(roleMember.getId());
302
303
304 assertEquals("updated role member number of attributes is incorrect", 3,updatedRmBo.getAttributeDetails().size());
305
306 System.err.println( updatedRmBo );
307
308 for (RoleMemberAttributeDataBo newRoleMemberAttrDataBo : updatedRmBo.getAttributeDetails() ) {
309 assertEquals( newRoleMemberAttrDataBo.getKimAttribute().getAttributeName() + " value is incorrect", newRoleMemberAttrDataBo.getKimAttribute().getAttributeName() + "After", newRoleMemberAttrDataBo.getAttributeValue() );
310 if (newRoleMemberAttrDataBo.getKimAttribute().getAttributeName().equals("componentName")) {
311 assertEquals("componentName (new attribute) versionNumber incorrect", new Long(1), newRoleMemberAttrDataBo.getVersionNumber());
312 } else {
313 assertEquals(newRoleMemberAttrDataBo.getKimAttribute().getAttributeName() + " (updated attribute) versionNumber incorrect", new Long(2), newRoleMemberAttrDataBo.getVersionNumber());
314 }
315 }
316 }
317
318
319 @Test
320 public void testDelegateMemberCreateUpdateRemoveWithAttr() {
321
322 Role r2 = roleService.getRole(ROLE_ID);
323 RoleMember rm1 = roleService.assignPrincipalToRole("user2", r2.getNamespaceCode(), r2.getName(),
324 new HashMap<String, String>());
325 String kimTypeId = "1";
326
327
328 String id = getNextSequenceStringValue("KRIM_DLGN_MBR_ID_S");
329 DelegateTypeBo delegate = new DelegateTypeBo();
330 delegate.setDelegationId(id);
331 delegate.setDelegationType(DelegationType.PRIMARY);
332 delegate.setRoleId(r2.getId());
333 delegate.setActive(true);
334 delegate.setKimTypeId(kimTypeId);
335 delegate = KradDataServiceLocator.getDataObjectService().save(delegate, PersistenceOption.FLUSH);
336
337
338 DelegateMember.Builder delegateMemberInfo = DelegateMember.Builder.create();
339 delegateMemberInfo.setDelegationId(delegate.getDelegationId());
340 delegateMemberInfo.setMemberId("user4");
341 delegateMemberInfo.setRoleMemberId(rm1.getId());
342 delegateMemberInfo.setType( MemberType.PRINCIPAL );
343 Map<String,String> attributes = new HashMap<String,String>();
344 attributes.put("parameterName", "parameterNameBefore");
345 attributes.put("namespaceCode", "namespaceCodeBefore");
346 attributes.put("componentName", "componentNameBefore");
347 delegateMemberInfo.setAttributes(attributes);
348 DelegateMember inDelegateMember = delegateMemberInfo.build();
349 DelegateMember newDelegateMember = roleService.createDelegateMember(inDelegateMember);
350 assertNotNull("delegateMember not created",newDelegateMember);
351
352
353
354
355 DateTime threeDaysAgo = DateTime.now().minusDays(3);
356 DateTime threeDaysFromNow = DateTime.now().plusDays(3);
357 delegateMemberInfo.setActiveFromDate(threeDaysAgo);
358 delegateMemberInfo.setActiveToDate(threeDaysFromNow);
359 delegateMemberInfo.setDelegationMemberId(newDelegateMember.getDelegationMemberId());
360 Map<String,String> newAttributes = new HashMap<String,String>();
361 newAttributes.put("parameterName", "parameterNameAfter");
362 newAttributes.put("namespaceCode", "namespaceCodeAfter");
363 newAttributes.put("componentName", "componentNameAfter");
364 delegateMemberInfo.setAttributes(newAttributes);
365 newDelegateMember = delegateMemberInfo.build();
366 DelegateMember updateDelegateMember = roleService.updateDelegateMember(newDelegateMember);
367
368 assertNotNull("updateDelegateMember not updated", updateDelegateMember);
369 assertEquals("activeFromDate not updated",threeDaysAgo,updateDelegateMember.getActiveFromDate());
370 assertEquals("activeToDate not updated",threeDaysFromNow,updateDelegateMember.getActiveToDate());
371
372 DelegateMemberBo updatedDelegateMemberBo = getDelegateMemberBo(updateDelegateMember.getDelegationMemberId());
373
374 for (DelegateMemberAttributeDataBo newRoleMemberAttrDataBo : updatedDelegateMemberBo.getAttributeDetails()) {
375 for (DelegateMemberAttributeDataBo oldRoleMemberAttrDataBo : updatedDelegateMemberBo.getAttributeDetails()) {
376 if (newRoleMemberAttrDataBo.getKimTypeId().equals(oldRoleMemberAttrDataBo.getKimTypeId()) &&
377 newRoleMemberAttrDataBo.getKimAttributeId().equals(oldRoleMemberAttrDataBo.getKimAttributeId())) {
378 assertEquals("version number on new role member incorrect", new Long(2), newRoleMemberAttrDataBo.getVersionNumber());
379 }
380 }
381 }
382
383
384 List<DelegateMember> removeDelegateMembers = new ArrayList<DelegateMember>();
385 removeDelegateMembers.add(updateDelegateMember);
386 roleService.removeDelegateMembers(removeDelegateMembers);
387 DelegateMember removedDelegateMember = roleService.getDelegationMemberById(updateDelegateMember.getDelegationMemberId()) ;
388 assertTrue("removeDelegateMembers did not remove the existing member",removedDelegateMember.getDelegationMemberId().equals(updateDelegateMember.getDelegationMemberId()));
389 assertNotNull("after removal, versionNumber should not be null", removedDelegateMember.getVersionNumber());
390 assertTrue("removeDelegateMembers did not remove the existing member",removedDelegateMember.getVersionNumber().equals(updateDelegateMember.getVersionNumber() + 1));
391 assertNotNull("after removal, active to date should not be null", removedDelegateMember.getActiveToDate());
392 assertTrue("removeDelegateMembers did not update activeToDate",removedDelegateMember.getActiveToDate().isBeforeNow());
393 }
394
395 protected RoleMemberBo getRoleMemberBo(String roleMemberId) {
396 if (StringUtils.isBlank(roleMemberId)) {
397 return null;
398 }
399
400 return KradDataServiceLocator.getDataObjectService().find(RoleMemberBo.class, roleMemberId);
401 }
402
403 protected DelegateMemberBo getDelegateMemberBo(String delegationMemberId) {
404 if (StringUtils.isBlank(delegationMemberId)) {
405 return null;
406 }
407
408 return KradDataServiceLocator.getDataObjectService().find(DelegateMemberBo.class, delegationMemberId);
409 }
410
411 @Test
412 public void testPrincipalHasRoleContainsGroupAssigned() {
413
414 List <String>roleIds = new ArrayList<String>();
415 roleIds.add("r2");
416 assertTrue( "p2 is assigned to g1 and g1 assigned to r2", roleService.principalHasRole("p2", roleIds, Collections.<String, String>emptyMap() ));
417 }
418
419 @Test
420 public void testAddPrincipalToRoleAndRemove() {
421
422
423
424
425
426
427
428
429
430
431
432
433 Role r2 = roleService.getRole("r2");
434 RoleMember rm1 = roleService.assignPrincipalToRole("user4", r2.getNamespaceCode(), r2.getName(),
435 new HashMap<String, String>());
436
437 assertTrue("principal should be assigned to role", roleService.principalHasRole("user4", Collections.singletonList(
438 r2.getId()), new HashMap<String, String>()));
439
440 roleService.removePrincipalFromRole("user4", r2.getNamespaceCode(), r2.getName(), new HashMap<String, String>());
441
442 RoleMember rm2 = roleService.assignPrincipalToRole("user4", r2.getNamespaceCode(), r2.getName(),
443 new HashMap<String, String>());
444
445 assertFalse(rm1.getId().equals(rm2.getId()));
446 }
447
448 @Test
449 public void testAddMultiplePrincipalsToRole() {
450 Role r2 = roleService.getRole("r2");
451
452 Map<String, String> conditions = new HashMap<String, String>();
453
454 int originalNumberOfPrincipals = roleService.getRoleMemberPrincipalIds(r2.getNamespaceCode(), r2.getName(),
455 conditions).size();
456
457 RoleMember rm1 = roleService.assignPrincipalToRole("user3", r2.getNamespaceCode(), r2.getName(), conditions);
458 RoleMember rm2 = roleService.assignPrincipalToRole("user4", r2.getNamespaceCode(), r2.getName(), conditions);
459
460 assertTrue("principal should be assigned to role", roleService.principalHasRole("user3",
461 Collections.singletonList(r2.getId()), conditions));
462 assertTrue("principal should be assigned to role", roleService.principalHasRole("user4",
463 Collections.singletonList(r2.getId()), conditions));
464
465 int numberOfPrincipals = roleService.getRoleMemberPrincipalIds(r2.getNamespaceCode(), r2.getName(), conditions)
466 .size();
467
468 assertEquals("Should have been two Principals added to role", numberOfPrincipals - 2,
469 originalNumberOfPrincipals);
470
471 r2 = roleService.getRole("r2");
472 roleService.removePrincipalFromRole("user3", r2.getNamespaceCode(), r2.getName(), conditions);
473 roleService.removePrincipalFromRole("user4", r2.getNamespaceCode(), r2.getName(), conditions);
474
475 r2 = roleService.getRole("r2");
476 assertFalse("principal should have been removed from role", roleService.principalHasRole("user3",
477 Collections.singletonList(r2.getId()), conditions));
478 assertFalse("principal should have been removed from role", roleService.principalHasRole("user4",
479 Collections.singletonList(r2.getId()), conditions));
480 numberOfPrincipals = roleService.getRoleMemberPrincipalIds(r2.getNamespaceCode(), r2.getName(),
481 new HashMap<String, String>()).size();
482 assertEquals("Should have had the two added Principals removed", numberOfPrincipals,
483 originalNumberOfPrincipals);
484 }
485
486 @Test
487 public void testAddMultipleQualifiedPrincipalsToRole() {
488 Role rCampus = roleService.getRole("r-campus");
489
490 Map<String, String> conditions = Collections.singletonMap(KimConstants.AttributeConstants.CAMPUS_CODE, "BL");
491 int originalNumberOfPrincipals = roleService.getRoleMemberPrincipalIds(rCampus.getNamespaceCode(),
492 rCampus.getName(), Collections.singletonMap(KimConstants.AttributeConstants.CAMPUS_CODE, "BL")).size();
493
494 RoleMember rm1 = roleService.assignPrincipalToRole("user3", rCampus.getNamespaceCode(), rCampus.getName(),
495 conditions);
496 RoleMember rm2 = roleService.assignPrincipalToRole("user4", rCampus.getNamespaceCode(), rCampus.getName(),
497 conditions);
498
499 assertTrue("principal should be assigned to role", roleService.principalHasRole("user3",
500 Collections.singletonList(rCampus.getId()), conditions));
501 assertTrue("principal should be assigned to role", roleService.principalHasRole("user4",
502 Collections.singletonList(rCampus.getId()), conditions));
503
504 int numberOfPrincipals = roleService.getRoleMemberPrincipalIds(rCampus.getNamespaceCode(), rCampus.getName(),
505 Collections.singletonMap(KimConstants.AttributeConstants.CAMPUS_CODE, "BL")).size();
506
507 assertEquals("Should have been two Principals added to role", numberOfPrincipals,
508 originalNumberOfPrincipals + 2);
509
510 rCampus = roleService.getRole("r-campus");
511 roleService.removePrincipalFromRole("user3", rCampus.getNamespaceCode(), rCampus.getName(), conditions);
512 roleService.removePrincipalFromRole("user4", rCampus.getNamespaceCode(), rCampus.getName(), conditions);
513
514 rCampus = roleService.getRole("r-campus");
515 assertFalse("principal should have been removed from role", roleService.principalHasRole("user3",
516 Collections.singletonList(rCampus.getId()), conditions));
517 assertFalse("principal should have been removed from role", roleService.principalHasRole("user3",
518 Collections.singletonList(rCampus.getId()), conditions));
519
520 numberOfPrincipals = roleService.getRoleMemberPrincipalIds(rCampus.getNamespaceCode(), rCampus.getName(),
521 Collections.singletonMap(KimConstants.AttributeConstants.CAMPUS_CODE, "BL")).size();
522 assertEquals("Should have had the two added Principals removed", numberOfPrincipals,
523 originalNumberOfPrincipals);
524 }
525
526
527
528
529
530
531 @Test (expected=IllegalArgumentException.class)
532 public void testCircularRoleAssignment() {
533 Map<String, String> map = new HashMap<String, String>();
534 List <String>roleIds = new ArrayList<String>();
535 roleIds.add("r1");
536 roleService.assignRoleToRole("r5", "AUTH_SVC_TEST2", "RoleThree", map);
537 }
538
539 protected RoleResponsibilityAction createRoleResponsibilityAction() {
540 List<RoleMembership> members = roleService.getRoleMembers(Collections.singletonList("r1"), null);
541 RoleMembership rm = members.get(0);
542
543 RoleResponsibilityAction.Builder builder = RoleResponsibilityAction.Builder.create();
544 builder.setRoleMemberId(rm.getMemberId());
545 builder.setActionTypeCode(ActionType.APPROVE.getCode());
546
547 RoleResponsibilityAction saved = roleService.createRoleResponsibilityAction(builder.build());
548 List<RoleResponsibilityAction> rra = roleService.getRoleMemberResponsibilityActions(rm.getMemberId());
549 assertEquals("incorrect number of RoleResponsibilityAction returned", 1, rra.size());
550 assertEquals("saved RoleResponsibilityAction does not match expected", saved, rra.get(0));
551
552 return rra.get(0);
553 }
554
555 @Test
556 public void testGetRoleMembers() {
557 List<RoleMembership> members = roleService.getRoleMembers(Collections.singletonList("r1"), null);
558 assertNotNull( "returned member list should not be null", members);
559 assertEquals("Wrong numbers of members in the role", 2, members.size());
560 }
561
562 @Test
563 public void testGetRoleMembersWithExactMatchRoleTypeEmptyQualifier() {
564 Role rCampus = roleService.getRole("r-campus");
565 assertNotNull( "Campus-based role missing from test data", rCampus );
566 assertEquals( "Campus role type incorrect", "kt-campus", rCampus.getKimTypeId());
567 List<RoleMembership> members = roleService.getRoleMembers(Collections.singletonList("r-campus"), Collections.<String,String>emptyMap());
568 assertNotNull( "returned member list should not be null", members);
569 assertEquals("Wrong numbers of members in the role: " + members, 2, members.size());
570 }
571
572 @Test
573 public void testGetRoleMembersWithExactMatchRoleType() {
574 Role rCampus = roleService.getRole("r-campus");
575 assertNotNull( "Campus-based role missing from test data", rCampus );
576 assertEquals( "Campus role type incorrect", "kt-campus", rCampus.getKimTypeId());
577 List<RoleMembership> members = roleService.getRoleMembers(Collections.singletonList("r-campus"), Collections.singletonMap(KimConstants.AttributeConstants.CAMPUS_CODE, "BL"));
578 assertNotNull( "returned member list should not be null", members);
579 assertEquals("Wrong numbers of members returned from role: " + members, 1, members.size());
580 }
581
582 @Test
583 public void testRemoveRoleFromRoleWithExactQualification() {
584 Role rCampus = roleService.getRole("r-campus-2");
585 assertNotNull( "Campus-based role missing from test data", rCampus );
586 assertEquals( "Campus role type incorrect", "kt-campus", rCampus.getKimTypeId());
587
588 List<RoleMembership> firstLevelRoleMembers = roleService.getFirstLevelRoleMembers(Collections.singletonList("r-campus-2"));
589 assertEquals("wrong number of role members: " + firstLevelRoleMembers, 2, firstLevelRoleMembers.size());
590
591 roleService.removeRoleFromRole("r4", "AUTH_SVC_TEST2", "Campus Reviewer 2", Collections.singletonMap(KimConstants.AttributeConstants.CAMPUS_CODE, "BL"));
592
593 firstLevelRoleMembers = roleService.getFirstLevelRoleMembers(Collections.singletonList("r-campus-2"));
594 assertEquals("wrong number of role members after removal: " + firstLevelRoleMembers, 1, firstLevelRoleMembers.size());
595
596 assertEquals("Wrong role member remains", "r1", firstLevelRoleMembers.get(0).getMemberId() );
597 }
598
599 @Test
600 public void testGetRoleQualifersForPrincipalByNamespaceAndRolenameWithoutQualifier() {
601 Role rCampus = roleService.getRoleByNamespaceCodeAndName("AUTH_SVC_TEST2", "Campus Reviewer");
602 assertNotNull( "Campus-based role missing from test data", rCampus );
603 assertEquals( "Campus role type incorrect", "kt-campus", rCampus.getKimTypeId());
604
605 List<Map<String, String>> qualifiers = roleService.getRoleQualifersForPrincipalByNamespaceAndRolename("p9", "AUTH_SVC_TEST2", "Campus Reviewer", Collections.<String,String>emptyMap() );
606 assertNotNull( "Returned qualifier list should not be null", qualifiers );
607 assertEquals( "Qualifier list should have one entry", 1, qualifiers.size() );
608 assertTrue( "campus code qualifier missing", qualifiers.get(0).containsKey(KimConstants.AttributeConstants.CAMPUS_CODE) );
609 assertEquals( "campus code qualifier incorrect", "BL", qualifiers.get(0).get(KimConstants.AttributeConstants.CAMPUS_CODE) );
610 }
611
612 @Test
613 public void testGetRoleQualifersForPrincipalByNamespaceAndRolenameWithQualifier() {
614 Role rCampus = roleService.getRoleByNamespaceCodeAndName("AUTH_SVC_TEST2", "Campus Reviewer");
615 assertNotNull( "Campus-based role missing from test data", rCampus );
616 assertEquals( "Campus role type incorrect", "kt-campus", rCampus.getKimTypeId());
617
618 List<Map<String, String>> qualifiers = roleService.getRoleQualifersForPrincipalByNamespaceAndRolename("p9", "AUTH_SVC_TEST2", "Campus Reviewer", Collections.singletonMap(KimConstants.AttributeConstants.CAMPUS_CODE, "BL") );
619 assertNotNull( "Returned qualifier list should not be null", qualifiers );
620 assertEquals( "Qualifier list should have one entry", 1, qualifiers.size() );
621 assertTrue( "campus code qualifier missing", qualifiers.get(0).containsKey(KimConstants.AttributeConstants.CAMPUS_CODE) );
622 assertEquals( "campus code qualifier incorrect", "BL", qualifiers.get(0).get(KimConstants.AttributeConstants.CAMPUS_CODE) );
623 }
624
625 @Test
626 public void testCreateRoleResponsibilityAction() {
627 createRoleResponsibilityAction();
628 }
629
630 @Test
631 public void testUpdateRoleResponsibilityAction() {
632 RoleResponsibilityAction rra = createRoleResponsibilityAction();
633 RoleResponsibilityAction.Builder builder = RoleResponsibilityAction.Builder.create(rra);
634 assertFalse(builder.isForceAction());
635 builder.setForceAction(true);
636 builder.setActionTypeCode(ActionType.ACKNOWLEDGE.getCode());
637
638 RoleResponsibilityAction updated = roleService.updateRoleResponsibilityAction(builder.build());
639 builder.setVersionNumber(updated.getVersionNumber());
640 assertEquals(builder.build(), updated);
641
642
643 List<RoleResponsibilityAction> rras = roleService.getRoleMemberResponsibilityActions(rra.getRoleMemberId());
644 assertEquals("incorrect number of RoleResponsibilityAction returned", 1, rras.size());
645 assertEquals("updated RoleResponsibilityAction does not match expected", updated, rras.get(0));
646 }
647
648 @Test
649 public void testDeleteRoleResponsibilityAction() {
650 RoleResponsibilityAction rra = createRoleResponsibilityAction();
651
652 roleService.deleteRoleResponsibilityAction(rra.getId());
653
654 List<RoleResponsibilityAction> rras = roleService.getRoleMemberResponsibilityActions(rra.getRoleMemberId());
655 assertEquals(0, rras.size());
656
657 try {
658 roleService.deleteRoleResponsibilityAction(rra.getId());
659 fail("Expected to throw RiceIllegalStateException due to missing RuleResponsibilityAction");
660 } catch (RiceIllegalStateException rise) {
661
662 }
663 }
664 }