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.joda.time.DateTime;
20  import org.joda.time.DateTimeComparator;
21  import org.junit.Test;
22  import org.kuali.rice.core.api.delegation.DelegationType;
23  import org.kuali.rice.core.api.membership.MemberType;
24  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
25  import org.kuali.rice.kim.api.KimApiConstants;
26  import org.kuali.rice.kim.api.common.delegate.DelegateMember;
27  import org.kuali.rice.kim.api.common.delegate.DelegateMemberContract;
28  import org.kuali.rice.kim.api.identity.principal.Principal;
29  import org.kuali.rice.kim.api.role.Role;
30  import org.kuali.rice.kim.api.role.RoleMember;
31  import org.kuali.rice.kim.api.role.RoleService;
32  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
33  import org.kuali.rice.kim.impl.common.delegate.DelegateMemberBo;
34  import org.kuali.rice.kim.impl.common.delegate.DelegateTypeBo;
35  import org.kuali.rice.kim.impl.role.RoleServiceImpl;
36  import org.kuali.rice.kim.test.KIMTestCase;
37  import org.kuali.rice.krad.service.KRADServiceLocator;
38  
39  import javax.xml.namespace.QName;
40  import java.util.ArrayList;
41  import java.util.Calendar;
42  import java.util.Collections;
43  import java.util.Date;
44  import java.util.HashMap;
45  import java.util.List;
46  import java.util.Map;
47  
48  import static org.junit.Assert.*;
49  
50  public class RoleServiceImplTest extends KIMTestCase {
51  
52  	private RoleService roleService;
53  
54  	public void setUp() throws Exception {
55  		super.setUp();
56  		roleService = (RoleService) GlobalResourceLoader.getService(
57                  new QName(KimApiConstants.Namespaces.KIM_NAMESPACE_2_0, KimApiConstants.ServiceNames.ROLE_SERVICE_SOAP));
58  	}
59  
60  	@Test
61  	public void testPrincipaHasRoleOfDirectAssignment() {
62  		List <String>roleIds = new ArrayList<String>();
63  		roleIds.add("r1");
64  		assertTrue( "p1 has direct role r1", roleService.principalHasRole("p1", roleIds,  Collections
65                  .<String, String>emptyMap() ));
66  		//assertFalse( "p4 has no direct/higher level role r1", roleService.principalHasRole("p4", roleIds, null ));
67  		Map<String, String> qualification = new HashMap<String, String>();
68  		qualification.put("Attribute 2", "CHEM");
69  		assertTrue( "p1 has direct role r1 with rp2 attr data", roleService.principalHasRole("p1", roleIds, qualification));
70  		qualification.clear();
71  		//requested qualification rolls up to a higher element in some hierarchy 
72  		// method not implemented yet, not quite clear how this works
73  		qualification.put("Attribute 3", "PHYS");
74  		assertTrue( "p1 has direct role r1 with rp2 attr data", roleService.principalHasRole("p1", roleIds, Maps.newHashMap(
75                  qualification)));
76  	}
77  
78  	@Test
79  	public void testPrincipalHasRoleOfHigherLevel() {
80  		// "p3" is in "r2" and "r2 contains "r1"
81  		List <String>roleIds = new ArrayList<String>();
82  		roleIds.add("r2");
83  		assertTrue( "p1 has assigned in higher level role r1", roleService.principalHasRole("p1", roleIds,  Collections.<String, String>emptyMap() ));
84          assertTrue( "p1 has assigned in higher level role r1", roleService.principalHasRole("p1", roleIds,  Collections.<String, String>emptyMap() ));
85  	}
86  
87      @Test
88      public void testDelegateMemberCreateUpdateRemove() {
89  
90          Role r2 = roleService.getRole("r2");
91          RoleMember rm1 = roleService.assignPrincipalToRole("user2", r2.getNamespaceCode(), r2.getName(),
92                  new HashMap<String, String>());
93          String kimTypeId = "1";
94  
95          //Create delegation
96          String id = "" + KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_DLGN_MBR_ID_S");
97          DelegateTypeBo delegate = new DelegateTypeBo();
98          delegate.setDelegationId(id);
99          delegate.setDelegationType(DelegationType.PRIMARY);
100         delegate.setRoleId(r2.getId());
101         delegate.setActive(true);
102         delegate.setKimTypeId("" + kimTypeId);
103         delegate = KRADServiceLocator.getBusinessObjectService().save(delegate);
104 
105         //Create delegate member
106         DelegateMember.Builder delegateMemberInfo = DelegateMember.Builder.create();
107         delegateMemberInfo.setAttributes(Collections.<String, String>emptyMap());
108         delegateMemberInfo.setDelegationId(delegate.getDelegationId());
109         delegateMemberInfo.setMemberId("user4");
110         delegateMemberInfo.setRoleMemberId(rm1.getId());
111         delegateMemberInfo.setType( MemberType.PRINCIPAL );
112         DelegateMember inDelegateMember =  delegateMemberInfo.build();
113         DelegateMember newDelegateMember = roleService.createDelegateMember(inDelegateMember);
114         assertNotNull("delegateMember not created",newDelegateMember);
115 
116         //Update delegate member
117         Long versionNumber = newDelegateMember.getVersionNumber();
118         DateTime dateTimeFrom   = DateTime.now().minusDays(3);
119         delegateMemberInfo.setActiveFromDate(dateTimeFrom);
120         DateTime dateTimeTo = DateTime.now().plusDays(3);
121         delegateMemberInfo.setActiveToDate(dateTimeTo);
122         inDelegateMember = delegateMemberInfo.build();
123         DelegateMember updateDelegateMember = roleService.createDelegateMember(inDelegateMember);
124         assertNotNull("updateDelegateMember not created",newDelegateMember);
125         assertEquals("activeFromDate not updated",dateTimeFrom,updateDelegateMember.getActiveFromDate());
126         assertEquals("activeToDate not updated",dateTimeTo,updateDelegateMember.getActiveToDate());
127 
128         //remove (inactivate) delegate member
129         List<DelegateMember>  removeDelegateMembers = new ArrayList<DelegateMember>();
130         removeDelegateMembers.add(updateDelegateMember);
131         roleService.removeDelegateMembers(removeDelegateMembers);
132         DelegateMember removeDelegate = roleService.getDelegationMemberById(updateDelegateMember.getDelegationMemberId()) ;
133         assertTrue("removeDelegates did not update activeToDate",removeDelegate.getActiveToDate().equals(updateDelegateMember.getActiveToDate()));
134     }
135 
136 
137 	
138 	@Test
139 	public void testPrincipalHasRoleContainsGroupAssigned() {
140 		// "p2" is in "g1" and "g1" assigned to "r2"
141 		List <String>roleIds = new ArrayList<String>();
142 		roleIds.add("r2");
143 		assertTrue( "p2 is assigned to g1 and g1 assigned to r2", roleService.principalHasRole("p2", roleIds,  Collections.<String, String>emptyMap() ));
144 	}
145 
146     @Test
147     public void testAddPrincipalToRoleAndRemove() {
148         /*Role r2 = roleService.getRole("r2");
149         roleService.assignPrincipalToRole("user4", r2.getNamespaceCode(), r2.getName(),
150                 new HashMap<String, String>());
151 
152         assertTrue("principal should be assigned to role", roleService.principalHasRole("user4", Collections.singletonList(
153                 r2.getId()), new HashMap<String, String>()));
154         
155         roleService.removePrincipalFromRole("user4", r2.getNamespaceCode(), r2.getName(), new HashMap<String, String>());
156 
157         assertFalse("principal should not be assigned to role", roleService.principalHasRole("user4", Collections.singletonList(
158                 r2.getId()), new HashMap<String, String>()));*/
159 
160         Role r2 = roleService.getRole("r2");
161         RoleMember rm1 = roleService.assignPrincipalToRole("user4", r2.getNamespaceCode(), r2.getName(),
162                 new HashMap<String, String>());
163 
164         assertTrue("principal should be assigned to role", roleService.principalHasRole("user4", Collections.singletonList(
165                 r2.getId()), new HashMap<String, String>()));
166 
167         roleService.removePrincipalFromRole("user4", r2.getNamespaceCode(), r2.getName(), new HashMap<String, String>());
168 
169         RoleMember rm2 = roleService.assignPrincipalToRole("user4", r2.getNamespaceCode(), r2.getName(),
170                 new HashMap<String, String>());
171 
172         assertFalse(rm1.getId().equals(rm2.getId()));
173     }
174 	
175 	/**
176 	 * Tests to ensure that a circular role membership cannot be created via the RoleService.
177 	 * 
178 	 * @throws Exception
179 	 */
180 	@Test (expected=IllegalArgumentException.class)
181 	public void testCircularRoleAssignment() {
182 		Map<String, String> map = new HashMap<String, String>();
183 		List <String>roleIds = new ArrayList<String>();
184 		roleIds.add("r1");
185 		roleService.assignRoleToRole("r5", "AUTH_SVC_TEST2", "RoleThree", map);
186 	}
187 }