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.apache.commons.lang.StringUtils;
20  import org.joda.time.DateTime;
21  import org.joda.time.format.DateTimeFormat;
22  import org.joda.time.format.DateTimeFormatter;
23  import org.junit.Test;
24  import org.kuali.rice.core.api.delegation.DelegationType;
25  import org.kuali.rice.core.api.exception.RiceIllegalStateException;
26  import org.kuali.rice.core.api.membership.MemberType;
27  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
28  import org.kuali.rice.kew.api.action.ActionType;
29  import org.kuali.rice.kim.api.KimApiConstants;
30  import org.kuali.rice.kim.api.KimConstants;
31  import org.kuali.rice.kim.api.common.delegate.DelegateMember;
32  import org.kuali.rice.kim.api.role.Role;
33  import org.kuali.rice.kim.api.role.RoleMember;
34  import org.kuali.rice.kim.api.role.RoleMembership;
35  import org.kuali.rice.kim.api.role.RoleResponsibilityAction;
36  import org.kuali.rice.kim.api.role.RoleService;
37  import org.kuali.rice.kim.impl.common.delegate.DelegateMemberAttributeDataBo;
38  import org.kuali.rice.kim.impl.common.delegate.DelegateMemberBo;
39  import org.kuali.rice.kim.impl.common.delegate.DelegateTypeBo;
40  import org.kuali.rice.kim.impl.role.RoleMemberAttributeDataBo;
41  import org.kuali.rice.kim.impl.role.RoleMemberBo;
42  import org.kuali.rice.kim.test.KIMTestCase;
43  import org.kuali.rice.krad.service.BusinessObjectService;
44  import org.kuali.rice.krad.service.KRADServiceLocator;
45  
46  import javax.xml.namespace.QName;
47  import java.util.ArrayList;
48  import java.util.Collections;
49  import java.util.HashMap;
50  import java.util.List;
51  import java.util.Map;
52  import java.util.concurrent.atomic.AtomicReference;
53  
54  import static org.junit.Assert.*;
55  
56  public class RoleServiceImplTest extends KIMTestCase {
57      private RoleService roleService;
58      static final DateTimeFormatter FORMATTER = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
59      static final String ROLE_MEMBER_ID1 = "45123";
60      static final String ROLE_ID = "100";
61      static final String MEMBER_ID = "1";
62      static final MemberType MEMBER_TYPE_R = MemberType.ROLE;
63      static final String ACTIVE_FROM_STRING = "2011-01-01 12:00:00";
64      static final DateTime ACTIVE_FROM = new DateTime(FORMATTER.parseDateTime(ACTIVE_FROM_STRING));
65      static final String ACTIVE_TO_STRING1 = "2013-01-01 12:00:00";
66      static final String ACTIVE_TO_STRING2 = "2014-01-01 12:00:00";
67      static final DateTime ACTIVE_TO1 = new DateTime(FORMATTER.parseDateTime(ACTIVE_TO_STRING1));
68      static final DateTime ACTIVE_TO2 = new DateTime(FORMATTER.parseDateTime(ACTIVE_TO_STRING2));
69      private BusinessObjectService businessObjectService;
70  
71      public void setUp() throws Exception {
72  		super.setUp();
73  		roleService = (RoleService) GlobalResourceLoader.getService(
74                  new QName(KimApiConstants.Namespaces.KIM_NAMESPACE_2_0, KimApiConstants.ServiceNames.ROLE_SERVICE_SOAP));
75  	}
76  
77  	@Test
78  	public void testPrincipaHasRoleOfDirectAssignment() {
79  		List <String>roleIds = new ArrayList<String>();
80  		roleIds.add("r1");
81  		assertTrue( "p1 has direct role r1", roleService.principalHasRole("p1", roleIds,  Collections
82                  .<String, String>emptyMap() ));
83  		//assertFalse( "p4 has no direct/higher level role r1", roleService.principalHasRole("p4", roleIds, null ));
84  		Map<String, String> qualification = new HashMap<String, String>();
85  		qualification.put("Attribute 2", "CHEM");
86  		assertTrue( "p1 has direct role r1 with rp2 attr data", roleService.principalHasRole("p1", roleIds, qualification));
87  		qualification.clear();
88  		//requested qualification rolls up to a higher element in some hierarchy 
89  		// method not implemented yet, not quite clear how this works
90  		qualification.put("Attribute 3", "PHYS");
91  		assertTrue( "p1 has direct role r1 with rp2 attr data", roleService.principalHasRole("p1", roleIds, Maps.newHashMap(
92                  qualification)));
93  	}
94  
95  	@Test
96  	public void testPrincipalHasRoleOfHigherLevel() {
97  		// "p3" is in "r2" and "r2 contains "r1"
98  		List <String>roleIds = new ArrayList<String>();
99  		roleIds.add("r2");
100 		assertTrue( "p1 has assigned in higher level role r1", roleService.principalHasRole("p1", roleIds,  Collections.<String, String>emptyMap() ));
101         assertTrue( "p1 has assigned in higher level role r1", roleService.principalHasRole("p1", roleIds,  Collections.<String, String>emptyMap() ));
102 	}
103 
104     @Test
105     public void testDelegateMemberCreateUpdateRemove() {
106 
107         Role r2 = roleService.getRole("r2");
108         RoleMember rm1 = roleService.assignPrincipalToRole("user2", r2.getNamespaceCode(), r2.getName(),
109                 new HashMap<String, String>());
110         String kimTypeId = "1";
111 
112         //Create delegation
113         String id = "" + KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_DLGN_MBR_ID_S");
114         DelegateTypeBo delegate = new DelegateTypeBo();
115         delegate.setDelegationId(id);
116         delegate.setDelegationType(DelegationType.PRIMARY);
117         delegate.setRoleId(r2.getId());
118         delegate.setActive(true);
119         delegate.setKimTypeId("" + kimTypeId);
120         delegate = KRADServiceLocator.getBusinessObjectService().save(delegate);
121 
122         //Create delegate member
123         DelegateMember.Builder delegateMemberInfo = DelegateMember.Builder.create();
124         delegateMemberInfo.setAttributes(Collections.<String, String>emptyMap());
125         delegateMemberInfo.setDelegationId(delegate.getDelegationId());
126         delegateMemberInfo.setMemberId("user4");
127         delegateMemberInfo.setRoleMemberId(rm1.getId());
128         delegateMemberInfo.setType( MemberType.PRINCIPAL );
129         DelegateMember inDelegateMember =  delegateMemberInfo.build();
130         DelegateMember newDelegateMember = roleService.createDelegateMember(inDelegateMember);
131         assertNotNull("delegateMember not created",newDelegateMember);
132 
133         //Update delegate member
134         delegateMemberInfo.setDelegationMemberId(newDelegateMember.getDelegationMemberId());
135         DateTime dateTimeFrom   = DateTime.now().minusDays(3);
136         delegateMemberInfo.setActiveFromDate(dateTimeFrom);
137         DateTime dateTimeTo = DateTime.now().plusDays(3);
138         delegateMemberInfo.setActiveToDate(dateTimeTo);
139         inDelegateMember = delegateMemberInfo.build();
140         DelegateMember updatedDelegateMember = roleService.updateDelegateMember(inDelegateMember);
141         assertEquals("Delegate member was updated",newDelegateMember.getDelegationMemberId(),updatedDelegateMember.getDelegationMemberId());
142         assertNotNull("updateDelegateMember not created",updatedDelegateMember);
143         assertEquals("activeFromDate not updated",dateTimeFrom,updatedDelegateMember.getActiveFromDate());
144         assertEquals("activeToDate not updated",dateTimeTo,updatedDelegateMember.getActiveToDate());
145 
146         //remove (inactivate) delegate member
147         List<DelegateMember>  removeDelegateMembers = new ArrayList<DelegateMember>();
148         removeDelegateMembers.add(updatedDelegateMember);
149         roleService.removeDelegateMembers(removeDelegateMembers);
150         DelegateMember removedDelegateMember = roleService.getDelegationMemberById(updatedDelegateMember.getDelegationMemberId()) ;
151         assertTrue("removeDelegateMembers did not remove the existing member",removedDelegateMember.getDelegationMemberId().equals(updatedDelegateMember.getDelegationMemberId()));
152         assertTrue("removeDelegateMembers did not remove the existing member",removedDelegateMember.getVersionNumber().equals(updatedDelegateMember.getVersionNumber() + 1));
153         assertTrue("removeDelegateMembers did not update activeToDate",removedDelegateMember.getActiveToDate().isBeforeNow());
154     }
155 
156     @Test
157     public void testRoleMemberCreateUpdate() {
158 
159         Role roleId = roleService.getRole(ROLE_ID);
160         List<String> roleIds = new ArrayList<String>();
161         roleIds.add(roleId.getId());
162 
163         Map<String,String> attributes = new HashMap<String,String>();
164         attributes.put("parameterName", "parameterNameBefore");
165         attributes.put("namespaceCode", "namespaceCodeBefore");
166         attributes.put("componentName", "componentNameBefore");
167 
168         RoleMember roleMember =  roleService.createRoleMember(RoleMember.Builder.create(ROLE_ID, ROLE_MEMBER_ID1, MEMBER_ID, MEMBER_TYPE_R, ACTIVE_FROM, ACTIVE_TO1, attributes, "", "").build());
169         RoleMemberBo rmBo = getRoleMemberBo(roleMember.getId());
170 
171         RoleMember.Builder updatedRoleMember = RoleMember.Builder.create(roleMember);
172         updatedRoleMember.setActiveToDate(ACTIVE_TO2);
173         Map<String,String> newAttributes = new HashMap<String,String>();
174         newAttributes.put("parameterName", "parameterNameAfter");
175         newAttributes.put("namespaceCode", "namespaceCodeAfter");
176         newAttributes.put("componentName", "componentNameAfter");
177         updatedRoleMember.setAttributes(newAttributes);
178 
179         roleService.updateRoleMember(updatedRoleMember.build());
180         RoleMemberBo updatedRmBo = getRoleMemberBo(roleMember.getId());
181 
182         assertEquals(3,rmBo.getAttributeDetails().size());
183         assertEquals(3,updatedRmBo.getAttributeDetails().size());
184 
185         for (RoleMemberAttributeDataBo newRoleMemberAttrDataBo :  updatedRmBo.getAttributeDetails()) {
186             for (RoleMemberAttributeDataBo oldRoleMemberAttrDataBo :  rmBo.getAttributeDetails()) {
187                 if (newRoleMemberAttrDataBo.getKimTypeId().equals(oldRoleMemberAttrDataBo.getKimTypeId()) &&
188                     newRoleMemberAttrDataBo.getKimAttributeId().equals(oldRoleMemberAttrDataBo.getKimAttributeId())) {
189                         assertEquals(new Long(2), newRoleMemberAttrDataBo.getVersionNumber());
190                 }
191             }
192         }
193     }
194 
195     @Test
196     public void testRoleMemberCreateUpdateNoAttrChange() {
197 
198         Role roleId = roleService.getRole(ROLE_ID);
199         List<String> roleIds = new ArrayList<String>();
200         roleIds.add(roleId.getId());
201 
202         Map<String,String> attributes = new HashMap<String,String>();
203         attributes.put("parameterName", "parameterNameBefore");
204         attributes.put("namespaceCode", "namespaceCodeBefore");
205         attributes.put("componentName", "componentNameBefore");
206 
207         RoleMember roleMember =  roleService.createRoleMember(RoleMember.Builder.create(ROLE_ID, ROLE_MEMBER_ID1, MEMBER_ID, MEMBER_TYPE_R, ACTIVE_FROM, ACTIVE_TO1, attributes, "", "").build());
208         RoleMemberBo rmBo = getRoleMemberBo(roleMember.getId());
209 
210         RoleMember.Builder updatedRoleMember = RoleMember.Builder.create(roleMember);
211         updatedRoleMember.setActiveToDate(ACTIVE_TO2);
212         updatedRoleMember.setAttributes(rmBo.getAttributes());
213 
214         roleService.updateRoleMember(updatedRoleMember.build());
215         RoleMemberBo updatedRmBo = getRoleMemberBo(roleMember.getId());
216 
217         assertEquals(3,rmBo.getAttributeDetails().size());
218         assertEquals(3,updatedRmBo.getAttributeDetails().size());
219 
220         for (RoleMemberAttributeDataBo newRoleMemberAttrDataBo :  updatedRmBo.getAttributeDetails()) {
221             for (RoleMemberAttributeDataBo oldRoleMemberAttrDataBo :  rmBo.getAttributeDetails()) {
222                 if (newRoleMemberAttrDataBo.getKimTypeId().equals(oldRoleMemberAttrDataBo.getKimTypeId()) &&
223                         newRoleMemberAttrDataBo.getKimAttributeId().equals(oldRoleMemberAttrDataBo.getKimAttributeId())) {
224                     assertEquals(oldRoleMemberAttrDataBo.getAttributeValue(), newRoleMemberAttrDataBo.getAttributeValue());
225                     assertEquals(new Long(2), newRoleMemberAttrDataBo.getVersionNumber());
226                 }
227             }
228         }
229     }
230 
231     @Test
232     public void testRoleMemberCreateUpdateRemoveOneAttr() {
233 
234         Role roleId = roleService.getRole(ROLE_ID);
235         List<String> roleIds = new ArrayList<String>();
236         roleIds.add(roleId.getId());
237 
238         Map<String,String> attributes = new HashMap<String,String>();
239         attributes.put("parameterName", "parameterNameBefore");
240         attributes.put("namespaceCode", "namespaceCodeBefore");
241         attributes.put("componentName", "componentNameBefore");
242 
243         RoleMember roleMember =  roleService.createRoleMember(RoleMember.Builder.create(ROLE_ID, ROLE_MEMBER_ID1, MEMBER_ID, MEMBER_TYPE_R, ACTIVE_FROM, ACTIVE_TO1, attributes, "", "").build());
244         RoleMemberBo rmBo = getRoleMemberBo(roleMember.getId());
245 
246         RoleMember.Builder updatedRoleMember = RoleMember.Builder.create(roleMember);
247         updatedRoleMember.setActiveToDate(ACTIVE_TO2);
248         Map<String,String> newAttributes = new HashMap<String,String>();
249         newAttributes.put("parameterName", "parameterNameAfter");
250         newAttributes.put("namespaceCode", "namespaceCodeAfter");
251         updatedRoleMember.setAttributes(newAttributes);
252 
253         roleService.updateRoleMember(updatedRoleMember.build());
254         RoleMemberBo updatedRmBo = getRoleMemberBo(roleMember.getId());
255 
256         assertEquals(3,rmBo.getAttributeDetails().size());
257         assertEquals(2, updatedRmBo.getAttributeDetails().size());
258 
259         for (RoleMemberAttributeDataBo newRoleMemberAttrDataBo :  updatedRmBo.getAttributeDetails()) {
260             for (RoleMemberAttributeDataBo oldRoleMemberAttrDataBo :  rmBo.getAttributeDetails()) {
261                 if (newRoleMemberAttrDataBo.getKimTypeId().equals(oldRoleMemberAttrDataBo.getKimTypeId()) &&
262                         newRoleMemberAttrDataBo.getKimAttributeId().equals(oldRoleMemberAttrDataBo.getKimAttributeId())) {
263                     assertEquals(new Long(2), newRoleMemberAttrDataBo.getVersionNumber());
264                 }
265             }
266         }
267     }
268 
269     @Test
270     public void testRoleMemberCreateUpdateAddOneAttr() {
271 
272         Role roleId = roleService.getRole(ROLE_ID);
273         List<String> roleIds = new ArrayList<String>();
274         roleIds.add(roleId.getId());
275 
276         Map<String,String> attributes = new HashMap<String,String>();
277         attributes.put("parameterName", "parameterNameBefore");
278         attributes.put("namespaceCode", "namespaceCodeBefore");
279 
280         RoleMember roleMember =  roleService.createRoleMember(RoleMember.Builder.create(ROLE_ID, ROLE_MEMBER_ID1, MEMBER_ID, MEMBER_TYPE_R, ACTIVE_FROM, ACTIVE_TO1, attributes, "", "").build());
281         RoleMemberBo rmBo = getRoleMemberBo(roleMember.getId());
282 
283         RoleMember.Builder updatedRoleMember = RoleMember.Builder.create(roleMember);
284         updatedRoleMember.setActiveToDate(ACTIVE_TO2);
285         Map<String,String> newAttributes = new HashMap<String,String>();
286         newAttributes.put("parameterName", "parameterNameAfter");
287         newAttributes.put("namespaceCode", "namespaceCodeAfter");
288         newAttributes.put("componentName", "componentNameAfter");
289 
290         updatedRoleMember.setAttributes(newAttributes);
291 
292         roleService.updateRoleMember(updatedRoleMember.build());
293         RoleMemberBo updatedRmBo = getRoleMemberBo(roleMember.getId());
294 
295         assertEquals(2,rmBo.getAttributeDetails().size());
296         assertEquals(3,updatedRmBo.getAttributeDetails().size());
297 
298         for (RoleMemberAttributeDataBo newRoleMemberAttrDataBo :  updatedRmBo.getAttributeDetails()) {
299             for (RoleMemberAttributeDataBo oldRoleMemberAttrDataBo :  rmBo.getAttributeDetails()) {
300                 if (newRoleMemberAttrDataBo.getAttributeValue().equals("componentName")) {
301                     assertEquals(new Long(1), newRoleMemberAttrDataBo.getVersionNumber());
302                 } else if (newRoleMemberAttrDataBo.getKimTypeId().equals(oldRoleMemberAttrDataBo.getKimTypeId()) &&
303                         newRoleMemberAttrDataBo.getKimAttributeId().equals(oldRoleMemberAttrDataBo.getKimAttributeId())) {
304                     assertEquals(new Long(2), newRoleMemberAttrDataBo.getVersionNumber());
305                 }
306             }
307         }
308     }
309 
310 
311     @Test
312     public void testDelegateMemberCreateUpdateRemoveWithAttr() {
313 
314         Role r2 = roleService.getRole(ROLE_ID);
315         RoleMember rm1 = roleService.assignPrincipalToRole("user2", r2.getNamespaceCode(), r2.getName(),
316                 new HashMap<String, String>());
317         String kimTypeId = "1";
318 
319         //Create delegation
320         String id = "" + KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_DLGN_MBR_ID_S");
321         DelegateTypeBo delegate = new DelegateTypeBo();
322         delegate.setDelegationId(id);
323         delegate.setDelegationType(DelegationType.PRIMARY);
324         delegate.setRoleId(r2.getId());
325         delegate.setActive(true);
326         delegate.setKimTypeId("" + kimTypeId);
327         delegate = KRADServiceLocator.getBusinessObjectService().save(delegate);
328 
329         //Create delegate member
330         DelegateMember.Builder delegateMemberInfo = DelegateMember.Builder.create();
331         delegateMemberInfo.setDelegationId(delegate.getDelegationId());
332         delegateMemberInfo.setMemberId("user4");
333         delegateMemberInfo.setRoleMemberId(rm1.getId());
334         delegateMemberInfo.setType( MemberType.PRINCIPAL );
335         Map<String,String> attributes = new HashMap<String,String>();
336         attributes.put("parameterName", "parameterNameBefore");
337         attributes.put("namespaceCode", "namespaceCodeBefore");
338         attributes.put("componentName", "componentNameBefore");
339         delegateMemberInfo.setAttributes(attributes);
340         DelegateMember inDelegateMember =  delegateMemberInfo.build();
341         DelegateMember newDelegateMember = roleService.createDelegateMember(inDelegateMember);
342         assertNotNull("delegateMember not created",newDelegateMember);
343 
344         DelegateMemberBo originalDelegateMemberBo = getDelegateMemberBo(newDelegateMember.getDelegationMemberId());
345 
346         //Update delegate member
347         DateTime dateTimeFrom   = DateTime.now().minusDays(3);
348         delegateMemberInfo.setActiveFromDate(dateTimeFrom);
349         DateTime dateTimeTo = DateTime.now().plusDays(3);
350         delegateMemberInfo.setActiveToDate(dateTimeTo);
351         delegateMemberInfo.setDelegationMemberId(newDelegateMember.getDelegationMemberId());
352         Map<String,String> newAttributes = new HashMap<String,String>();
353         newAttributes.put("parameterName", "parameterNameAfter");
354         newAttributes.put("namespaceCode", "namespaceCodeAfter");
355         newAttributes.put("componentName", "componentNameAfter");
356         delegateMemberInfo.setAttributes(newAttributes);
357         newDelegateMember = delegateMemberInfo.build();
358         DelegateMember updateDelegateMember = roleService.updateDelegateMember(newDelegateMember);
359         assertNotNull("updateDelegateMember not updated", updateDelegateMember);
360         assertEquals("activeFromDate not updated",dateTimeFrom,updateDelegateMember.getActiveFromDate());
361         assertEquals("activeToDate not updated",dateTimeTo,updateDelegateMember.getActiveToDate());
362 
363         DelegateMemberBo updatedDelegateMemberBo = getDelegateMemberBo(updateDelegateMember.getDelegationMemberId());
364 
365         for (DelegateMemberAttributeDataBo newRoleMemberAttrDataBo :  updatedDelegateMemberBo.getAttributeDetails()) {
366             for (DelegateMemberAttributeDataBo oldRoleMemberAttrDataBo :  updatedDelegateMemberBo.getAttributeDetails()) {
367                 if (newRoleMemberAttrDataBo.getKimTypeId().equals(oldRoleMemberAttrDataBo.getKimTypeId()) &&
368                         newRoleMemberAttrDataBo.getKimAttributeId().equals(oldRoleMemberAttrDataBo.getKimAttributeId())) {
369                     assertEquals(new Long(2), newRoleMemberAttrDataBo.getVersionNumber());
370                 }
371             }
372         }
373 
374         //remove (inactivate) delegate member
375         List<DelegateMember>  removeDelegateMembers = new ArrayList<DelegateMember>();
376         removeDelegateMembers.add(updateDelegateMember);
377         roleService.removeDelegateMembers(removeDelegateMembers);
378         DelegateMember removedDelegateMember = roleService.getDelegationMemberById(updateDelegateMember.getDelegationMemberId()) ;
379         assertTrue("removeDelegateMembers did not remove the existing member",removedDelegateMember.getDelegationMemberId().equals(updateDelegateMember.getDelegationMemberId()));
380         assertTrue("removeDelegateMembers did not remove the existing member",removedDelegateMember.getVersionNumber().equals(updateDelegateMember.getVersionNumber() + 1));
381         assertTrue("removeDelegateMembers did not update activeToDate",removedDelegateMember.getActiveToDate().isBeforeNow());
382     }
383 
384     protected RoleMemberBo getRoleMemberBo(String roleMemberId) {
385         if (StringUtils.isBlank(roleMemberId)) {
386             return null;
387         }
388 
389         return getBusinessObjectService().findByPrimaryKey(RoleMemberBo.class, Collections.singletonMap(
390                 KimConstants.PrimaryKeyConstants.ID, roleMemberId));
391     }
392 
393     protected DelegateMemberBo getDelegateMemberBo(String delegationMemberId) {
394         if (StringUtils.isBlank(delegationMemberId)) {
395             return null;
396         }
397 
398         return getBusinessObjectService().findByPrimaryKey(DelegateMemberBo.class,
399                 Collections.singletonMap(KimConstants.PrimaryKeyConstants.DELEGATION_MEMBER_ID, delegationMemberId));
400     }
401 
402     protected BusinessObjectService getBusinessObjectService() {
403         if (businessObjectService == null) {
404             businessObjectService = KRADServiceLocator.getBusinessObjectService();
405         }
406         return businessObjectService;
407     }
408 
409 	@Test
410 	public void testPrincipalHasRoleContainsGroupAssigned() {
411 		// "p2" is in "g1" and "g1" assigned to "r2"
412 		List <String>roleIds = new ArrayList<String>();
413 		roleIds.add("r2");
414 		assertTrue( "p2 is assigned to g1 and g1 assigned to r2", roleService.principalHasRole("p2", roleIds,  Collections.<String, String>emptyMap() ));
415 	}
416 
417     @Test
418     public void testAddPrincipalToRoleAndRemove() {
419         /*Role r2 = roleService.getRole("r2");
420         roleService.assignPrincipalToRole("user4", r2.getNamespaceCode(), r2.getName(),
421                 new HashMap<String, String>());
422 
423         assertTrue("principal should be assigned to role", roleService.principalHasRole("user4", Collections.singletonList(
424                 r2.getId()), new HashMap<String, String>()));
425         
426         roleService.removePrincipalFromRole("user4", r2.getNamespaceCode(), r2.getName(), new HashMap<String, String>());
427 
428         assertFalse("principal should not be assigned to role", roleService.principalHasRole("user4", Collections.singletonList(
429                 r2.getId()), new HashMap<String, String>()));*/
430 
431         Role r2 = roleService.getRole("r2");
432         RoleMember rm1 = roleService.assignPrincipalToRole("user4", r2.getNamespaceCode(), r2.getName(),
433                 new HashMap<String, String>());
434 
435         assertTrue("principal should be assigned to role", roleService.principalHasRole("user4", Collections.singletonList(
436                 r2.getId()), new HashMap<String, String>()));
437 
438         roleService.removePrincipalFromRole("user4", r2.getNamespaceCode(), r2.getName(), new HashMap<String, String>());
439 
440         RoleMember rm2 = roleService.assignPrincipalToRole("user4", r2.getNamespaceCode(), r2.getName(),
441                 new HashMap<String, String>());
442 
443         assertFalse(rm1.getId().equals(rm2.getId()));
444     }
445 	
446 	/**
447 	 * Tests to ensure that a circular role membership cannot be created via the RoleService.
448 	 * 
449 	 * @throws Exception
450 	 */
451 	@Test (expected=IllegalArgumentException.class)
452 	public void testCircularRoleAssignment() {
453 		Map<String, String> map = new HashMap<String, String>();
454 		List <String>roleIds = new ArrayList<String>();
455 		roleIds.add("r1");
456 		roleService.assignRoleToRole("r5", "AUTH_SVC_TEST2", "RoleThree", map);
457 	}
458 
459     protected RoleResponsibilityAction createRoleResponsibilityAction() {
460         Role r = roleService.getRole("r1");
461         List<RoleMembership> members = roleService.getRoleMembers(Collections.singletonList("r1"), null);
462         RoleMembership rm = members.get(0);
463 
464         RoleResponsibilityAction.Builder builder = RoleResponsibilityAction.Builder.create();
465         builder.setRoleMemberId(rm.getMemberId());
466         builder.setActionTypeCode(ActionType.APPROVE.getCode());
467 
468         RoleResponsibilityAction saved = roleService.createRoleResponsibilityAction(builder.build());
469         List<RoleResponsibilityAction> rra = roleService.getRoleMemberResponsibilityActions(rm.getMemberId());
470         assertEquals(1, rra.size());
471         assertEquals(saved, rra.get(0));
472 
473         return rra.get(0);
474     }
475 
476     @Test
477     public void testCreateRoleResponsibilityAction() {
478         createRoleResponsibilityAction();
479     }
480 
481     @Test
482     public void testUpdateRoleResponsibilityAction() {
483         RoleResponsibilityAction rra = createRoleResponsibilityAction();
484         RoleResponsibilityAction.Builder builder = RoleResponsibilityAction.Builder.create(rra);
485         assertFalse(builder.isForceAction());
486         builder.setForceAction(true);
487         builder.setActionTypeCode(ActionType.ACKNOWLEDGE.getCode());
488 
489         RoleResponsibilityAction updated = roleService.updateRoleResponsibilityAction(builder.build());
490         builder.setVersionNumber(updated.getVersionNumber());
491         assertEquals(builder.build(), updated);
492 
493         // test that the value for rolemember is updated and not cached
494         List<RoleResponsibilityAction> rras = roleService.getRoleMemberResponsibilityActions(rra.getRoleMemberId());
495         assertEquals(1, rras.size());
496         assertEquals(updated, rras.get(0));
497     }
498 
499     @Test
500     public void testDeleteRoleResponsibilityAction() {
501         RoleResponsibilityAction rra = createRoleResponsibilityAction();
502 
503         roleService.deleteRoleResponsibilityAction(rra.getId());
504 
505         List<RoleResponsibilityAction> rras = roleService.getRoleMemberResponsibilityActions(rra.getRoleMemberId());
506         assertEquals(0, rras.size());
507 
508         try {
509             roleService.deleteRoleResponsibilityAction(rra.getId());
510             fail("Expected to throw RiceIllegalStateException due to missing RuleResponsibilityAction");
511         } catch (RiceIllegalStateException rise) {
512             // expected
513         }
514     }
515 }