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  	}
85  
86      @Test
87      public void testDelegateMemberCreateUpdateRemove() {
88  
89          Role r2 = roleService.getRole("r2");
90          RoleMember rm1 = roleService.assignPrincipalToRole("user2", r2.getNamespaceCode(), r2.getName(),
91                  new HashMap<String, String>());
92          String kimTypeId = "1";
93  
94          //Create delegation
95          String id = "" + KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_DLGN_MBR_ID_S");
96          DelegateTypeBo delegate = new DelegateTypeBo();
97          delegate.setDelegationId(id);
98          delegate.setDelegationType(DelegationType.PRIMARY);
99          delegate.setRoleId(r2.getId());
100         delegate.setActive(true);
101         delegate.setKimTypeId("" + kimTypeId);
102         delegate = KRADServiceLocator.getBusinessObjectService().save(delegate);
103 
104         //Create delegate member
105         DelegateMember.Builder delegateMemberInfo = DelegateMember.Builder.create();
106         delegateMemberInfo.setAttributes(Collections.<String, String>emptyMap());
107         delegateMemberInfo.setDelegationId(delegate.getDelegationId());
108         delegateMemberInfo.setMemberId("user4");
109         delegateMemberInfo.setRoleMemberId(rm1.getId());
110         delegateMemberInfo.setType( MemberType.PRINCIPAL );
111         DelegateMember inDelegateMember =  delegateMemberInfo.build();
112         DelegateMember newDelegateMember = roleService.createDelegateMember(inDelegateMember);
113         assertNotNull("delegateMember not created",newDelegateMember);
114 
115         //Update delegate member
116         Long versionNumber = newDelegateMember.getVersionNumber();
117         DateTime dateTimeFrom   = DateTime.now().minusDays(3);
118         delegateMemberInfo.setActiveFromDate(dateTimeFrom);
119         DateTime dateTimeTo = DateTime.now().plusDays(3);
120         delegateMemberInfo.setActiveToDate(dateTimeTo);
121         inDelegateMember = delegateMemberInfo.build();
122         DelegateMember updateDelegateMember = roleService.createDelegateMember(inDelegateMember);
123         assertNotNull("updateDelegateMember not created",newDelegateMember);
124         assertEquals("activeFromDate not updated",dateTimeFrom,updateDelegateMember.getActiveFromDate());
125         assertEquals("activeToDate not updated",dateTimeTo,updateDelegateMember.getActiveToDate());
126 
127         //remove (inactivate) delegate member
128         List<DelegateMember>  removeDelegateMembers = new ArrayList<DelegateMember>();
129         removeDelegateMembers.add(updateDelegateMember);
130         roleService.removeDelegateMembers(removeDelegateMembers);
131         DelegateMember removeDelegate = roleService.getDelegationMemberById(updateDelegateMember.getDelegationMemberId()) ;
132         assertTrue("removeDelegates did not update activeToDate",removeDelegate.getActiveToDate().equals(updateDelegateMember.getActiveToDate()));
133     }
134 
135 
136 	
137 	@Test
138 	public void testPrincipalHasRoleContainsGroupAssigned() {
139 		// "p2" is in "g1" and "g1" assigned to "r2"
140 		List <String>roleIds = new ArrayList<String>();
141 		roleIds.add("r2");
142 		assertTrue( "p2 is assigned to g1 and g1 assigned to r2", roleService.principalHasRole("p2", roleIds,  Collections.<String, String>emptyMap() ));
143 	}
144 
145     @Test
146     public void testAddPrincipalToRoleAndRemove() {
147         /*Role r2 = roleService.getRole("r2");
148         roleService.assignPrincipalToRole("user4", r2.getNamespaceCode(), r2.getName(),
149                 new HashMap<String, String>());
150 
151         assertTrue("principal should be assigned to role", roleService.principalHasRole("user4", Collections.singletonList(
152                 r2.getId()), new HashMap<String, String>()));
153         
154         roleService.removePrincipalFromRole("user4", r2.getNamespaceCode(), r2.getName(), new HashMap<String, String>());
155 
156         assertFalse("principal should not be assigned to role", roleService.principalHasRole("user4", Collections.singletonList(
157                 r2.getId()), new HashMap<String, String>()));*/
158 
159         Role r2 = roleService.getRole("r2");
160         RoleMember rm1 = roleService.assignPrincipalToRole("user4", r2.getNamespaceCode(), r2.getName(),
161                 new HashMap<String, String>());
162 
163         assertTrue("principal should be assigned to role", roleService.principalHasRole("user4", Collections.singletonList(
164                 r2.getId()), new HashMap<String, String>()));
165 
166         roleService.removePrincipalFromRole("user4", r2.getNamespaceCode(), r2.getName(), new HashMap<String, String>());
167 
168         RoleMember rm2 = roleService.assignPrincipalToRole("user4", r2.getNamespaceCode(), r2.getName(),
169                 new HashMap<String, String>());
170 
171         assertFalse(rm1.getId().equals(rm2.getId()));
172     }
173 	
174 	/**
175 	 * Tests to ensure that a circular role membership cannot be created via the RoleService.
176 	 * 
177 	 * @throws Exception
178 	 */
179 	@Test (expected=IllegalArgumentException.class)
180 	public void testCircularRoleAssignment() {
181 		Map<String, String> map = new HashMap<String, String>();
182 		List <String>roleIds = new ArrayList<String>();
183 		roleIds.add("r1");
184 		roleService.assignRoleToRole("r5", "AUTH_SVC_TEST2", "RoleThree", map);
185 	}
186 }