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.kew.role;
17  
18  import org.junit.Test;
19  import org.kuali.rice.core.api.delegation.DelegationType;
20  import org.kuali.rice.core.api.membership.MemberType;
21  import org.kuali.rice.kew.api.KewApiServiceLocator;
22  import org.kuali.rice.kew.api.WorkflowDocument;
23  import org.kuali.rice.kew.api.WorkflowDocumentFactory;
24  import org.kuali.rice.kew.api.action.ActionRequest;
25  import org.kuali.rice.kew.api.action.ActionRequestPolicy;
26  import org.kuali.rice.kew.api.action.ActionType;
27  import org.kuali.rice.kew.api.document.node.RouteNodeInstance;
28  import org.kuali.rice.kew.test.KEWTestCase;
29  import org.kuali.rice.kew.api.KewApiConstants;
30  import org.kuali.rice.kim.api.identity.principal.Principal;
31  import org.kuali.rice.kim.api.role.Role;
32  import org.kuali.rice.kim.api.role.RoleMembership;
33  import org.kuali.rice.kim.api.role.RoleResponsibilityAction;
34  import org.kuali.rice.kim.api.role.RoleService;
35  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
36  import org.kuali.rice.kim.impl.common.attribute.KimAttributeBo;
37  import org.kuali.rice.kim.impl.common.delegate.DelegateTypeBo;
38  import org.kuali.rice.kim.impl.common.delegate.DelegateMemberBo;
39  import org.kuali.rice.kim.impl.responsibility.ResponsibilityAttributeBo;
40  import org.kuali.rice.kim.impl.responsibility.ResponsibilityBo;
41  import org.kuali.rice.kim.impl.responsibility.ResponsibilityTemplateBo;
42  import org.kuali.rice.kim.impl.role.RoleBo;
43  import org.kuali.rice.kim.impl.role.RoleMemberAttributeDataBo;
44  import org.kuali.rice.kim.impl.role.RoleMemberBo;
45  import org.kuali.rice.kim.impl.role.RoleResponsibilityActionBo;
46  import org.kuali.rice.kim.impl.role.RoleResponsibilityBo;
47  import org.kuali.rice.kim.impl.type.KimTypeAttributeBo;
48  import org.kuali.rice.kim.impl.type.KimTypeBo;
49  import org.kuali.rice.krad.service.KRADServiceLocator;
50  import org.kuali.rice.test.BaselineTestCase;
51  import org.springframework.transaction.TransactionStatus;
52  import org.springframework.transaction.support.TransactionCallback;
53  
54  import java.util.ArrayList;
55  import java.util.Collections;
56  import java.util.HashMap;
57  import java.util.List;
58  import java.util.Map;
59  
60  import static org.junit.Assert.*;
61  
62  /**
63   * Tests Role-based routing integration between KEW and KIM.
64   *
65   * @author Kuali Rice Team (rice.collab@kuali.org)
66   *
67   */
68  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.CLEAR_DB)
69  public class RoleRouteModuleTest extends KEWTestCase {
70  
71      private static final String NAMESPACE = KewApiConstants.KEW_NAMESPACE;
72      private static final String ROLE_NAME = "RoleRouteModuleTestRole";
73  
74      private static boolean suiteDataInitialized = false;
75      private static boolean suiteCreateDelegateInitialized = false;
76  
77      protected void loadTestData() throws Exception {
78          loadXmlFile("RoleRouteModuleTestConfig.xml");
79  
80          // only create this data once per suite!
81          if (suiteDataInitialized) {
82              return;
83          }
84  
85          /**
86           * First we need to set up:
87           *
88           * 1) KimAttributes for both chart and org
89           * 2) The KimType for "chart/org"
90           * 3) The KimTypeAttributes for chart and org to define relationship between KimType and it's KimAttributes
91           */
92  
93          // create "chart" KimAttribute
94          Long chartAttributeId = KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_ATTR_DEFN_ID_S");
95          KimAttributeBo chartAttribute = new KimAttributeBo();
96          chartAttribute.setId("" + chartAttributeId);
97          chartAttribute.setAttributeName("chart");
98          chartAttribute.setComponentName("org.kuali.rice.kim.bo.impl.KimAttributes");
99          chartAttribute.setNamespaceCode(NAMESPACE);
100         chartAttribute.setAttributeLabel("chart");
101         chartAttribute.setActive(true);
102         chartAttribute = (KimAttributeBo) KRADServiceLocator.getBusinessObjectService().save(chartAttribute);
103 
104         // create "org" KimAttribute
105         Long orgAttributeId = KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_ATTR_DEFN_ID_S");
106         KimAttributeBo orgAttribute = new KimAttributeBo();
107         orgAttribute.setId("" + orgAttributeId);
108         orgAttribute.setComponentName("org.kuali.rice.kim.bo.impl.KimAttributes");
109         orgAttribute.setAttributeName("org");
110         orgAttribute.setNamespaceCode(NAMESPACE);
111         orgAttribute.setAttributeLabel("org");
112         orgAttribute.setActive(true);
113         orgAttribute = (KimAttributeBo) KRADServiceLocator.getBusinessObjectService().save(orgAttribute);
114 
115         // create KimType
116         Long kimTypeId = KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_TYP_ID_S");
117         KimTypeBo kimType = new KimTypeBo();
118         kimType.setId("" + kimTypeId);
119         kimType.setName("ChartOrg");
120         kimType.setNamespaceCode(NAMESPACE);
121         kimType.setServiceName("testBaseRoleTypeService"); // do we need to set the kim type service yet? we shall see...
122         kimType.setActive(true);
123         kimType = (KimTypeBo) KRADServiceLocator.getBusinessObjectService().save(kimType);
124 
125         // create chart KimTypeAttribute
126         Long chartTypeAttributeId = KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_TYP_ATTR_ID_S");
127         KimTypeAttributeBo chartTypeAttribute = new KimTypeAttributeBo();
128         chartTypeAttribute.setId("" + chartTypeAttributeId);
129         chartTypeAttribute.setActive(true);
130         chartTypeAttribute.setKimAttributeId(chartAttribute.getId());
131         chartTypeAttribute.setKimTypeId(kimType.getId());
132         chartTypeAttribute = (KimTypeAttributeBo) KRADServiceLocator.getBusinessObjectService().save(chartTypeAttribute);
133 
134         // create org KimTypeAttribute
135         Long orgTypeAttributeId = KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_TYP_ATTR_ID_S");
136         KimTypeAttributeBo orgTypeAttribute = new KimTypeAttributeBo();
137         orgTypeAttribute.setId("" + orgTypeAttributeId);
138         orgTypeAttribute.setActive(true);
139         orgTypeAttribute.setKimAttributeId(orgAttribute.getId());
140         orgTypeAttribute.setKimTypeId(kimType.getId());
141         orgTypeAttribute = (KimTypeAttributeBo) KRADServiceLocator.getBusinessObjectService().save(orgTypeAttribute);
142 
143         /**
144          * New let's create the Role
145          */
146 
147         String roleId = "" + KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_ROLE_ID_S");
148         RoleBo role = new RoleBo();
149         role.setId(roleId);
150         role.setNamespaceCode(NAMESPACE);
151         role.setDescription("");
152         role.setName(ROLE_NAME);
153         role.setActive(true);
154         role.setKimTypeId(kimType.getId());
155 
156         String roleMemberId1 = "" + KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_ROLE_ID_S");
157         RoleMemberBo adminRolePrincipal = new RoleMemberBo();
158         adminRolePrincipal.setId(roleMemberId1);
159         adminRolePrincipal.setRoleId(roleId);
160         Principal adminPrincipal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName("admin");
161         assertNotNull(adminPrincipal);
162         adminRolePrincipal.setMemberId(adminPrincipal.getPrincipalId());
163         adminRolePrincipal.setType( MemberType.PRINCIPAL );
164 
165         String roleMemberId2 = "" + KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_ROLE_ID_S");
166         RoleMemberBo user2RolePrincipal = new RoleMemberBo();
167         user2RolePrincipal.setId(roleMemberId2);
168         user2RolePrincipal.setRoleId(roleId);
169         Principal user2Principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName("user2");
170         assertNotNull(user2Principal);
171         user2RolePrincipal.setMemberId(user2Principal.getPrincipalId());
172         user2RolePrincipal.setType( MemberType.PRINCIPAL );
173 
174         String roleMemberId3 = "" + KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_ROLE_ID_S");
175         RoleMemberBo user1RolePrincipal = new RoleMemberBo();
176         user1RolePrincipal.setId(roleMemberId3);
177         user1RolePrincipal.setRoleId(roleId);
178         Principal user1Principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName("user1");
179         assertNotNull(user1Principal);
180         user1RolePrincipal.setMemberId(user1Principal.getPrincipalId());
181         user1RolePrincipal.setType( MemberType.PRINCIPAL );
182 
183         List<RoleMemberBo> memberPrincipals = new ArrayList<RoleMemberBo>();
184         memberPrincipals.add(adminRolePrincipal);
185         memberPrincipals.add(user2RolePrincipal);
186         memberPrincipals.add(user1RolePrincipal);
187 
188         role.setMembers(memberPrincipals);
189 
190         /**
191          * Let's create qualifiers for chart and org for our role members
192          */
193 
194         String dataId = "" + KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_GRP_ATTR_DATA_ID_S");
195         RoleMemberAttributeDataBo chartDataBL = new RoleMemberAttributeDataBo();
196         chartDataBL.setId(dataId);
197         chartDataBL.setAttributeValue("BL");
198         chartDataBL.setKimAttribute(chartAttribute);
199         chartDataBL.setKimAttributeId(chartAttribute.getId());
200         chartDataBL.setKimTypeId(kimType.getId());
201         chartDataBL.setAssignedToId(adminRolePrincipal.getId());
202 
203         dataId = "" + KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_GRP_ATTR_DATA_ID_S");
204         RoleMemberAttributeDataBo chartDataBL2 = new RoleMemberAttributeDataBo();
205         chartDataBL2.setId(dataId);
206         chartDataBL2.setAttributeValue("BL");
207         chartDataBL2.setKimAttribute(chartAttribute);
208         chartDataBL2.setKimAttributeId(chartAttribute.getId());
209         chartDataBL2.setKimTypeId(kimType.getId());
210         chartDataBL2.setAssignedToId(user2RolePrincipal.getId());
211 
212         dataId = "" + KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_GRP_ATTR_DATA_ID_S");
213         RoleMemberAttributeDataBo orgDataBUS = new RoleMemberAttributeDataBo();
214         orgDataBUS.setId(dataId);
215         orgDataBUS.setAttributeValue("BUS");
216         orgDataBUS.setKimAttribute(orgAttribute);
217         orgDataBUS.setKimAttributeId(orgAttribute.getId());
218         orgDataBUS.setKimTypeId(kimType.getId());
219         orgDataBUS.setAssignedToId(adminRolePrincipal.getId());
220 
221         dataId = "" + KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_GRP_ATTR_DATA_ID_S");
222         RoleMemberAttributeDataBo orgDataBUS2 = new RoleMemberAttributeDataBo();
223         orgDataBUS2.setId(dataId);
224         orgDataBUS2.setAttributeValue("BUS");
225         orgDataBUS2.setKimAttribute(orgAttribute);
226         orgDataBUS2.setKimAttributeId(orgAttribute.getId());
227         orgDataBUS2.setKimTypeId(kimType.getId());
228         orgDataBUS2.setAssignedToId(user2RolePrincipal.getId());
229 
230 
231         dataId = "" + KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_GRP_ATTR_DATA_ID_S");
232         RoleMemberAttributeDataBo chartDataIN = new RoleMemberAttributeDataBo();
233         chartDataIN.setId(dataId);
234         chartDataIN.setAttributeValue("IN");
235         chartDataIN.setKimAttribute(chartAttribute);
236         chartDataIN.setKimAttributeId(chartAttribute.getId());
237         chartDataIN.setKimTypeId(kimType.getId());
238         chartDataIN.setAssignedToId(user1RolePrincipal.getId());
239 
240         dataId = "" + KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_GRP_ATTR_DATA_ID_S");
241         RoleMemberAttributeDataBo orgDataMED = new RoleMemberAttributeDataBo();
242         orgDataMED.setId(dataId);
243         orgDataMED.setAttributeValue("MED");
244         orgDataMED.setKimAttribute(orgAttribute);
245         orgDataMED.setKimAttributeId(orgAttribute.getId());
246         orgDataMED.setKimTypeId(kimType.getId());
247         orgDataMED.setAssignedToId(user1RolePrincipal.getId());
248 
249         List<RoleMemberAttributeDataBo> user1Attributes = new ArrayList<RoleMemberAttributeDataBo>();
250         user1Attributes.add(chartDataIN);
251         user1Attributes.add(orgDataMED);
252         user1RolePrincipal.setAttributeDetails(user1Attributes);
253 
254         List<RoleMemberAttributeDataBo> user2Attributes = new ArrayList<RoleMemberAttributeDataBo>();
255         user2Attributes.add(chartDataBL2);
256         user2Attributes.add(orgDataBUS2);
257         user2RolePrincipal.setAttributeDetails(user2Attributes);
258 
259         List<RoleMemberAttributeDataBo> adminAttributes = new ArrayList<RoleMemberAttributeDataBo>();
260         adminAttributes.add(chartDataBL);
261         adminAttributes.add(orgDataBUS);
262         adminRolePrincipal.setAttributeDetails(adminAttributes);
263 
264 
265         /**
266          * Now we can save the role!
267          */
268 
269         role = KRADServiceLocator.getBusinessObjectService().save(role);
270 
271 
272         /**
273          * Let's set up attributes for responsibility details
274          */
275 
276         // create "documentType" KimAttribute
277         Long documentTypeAttributeId = KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_ATTR_DEFN_ID_S");
278         KimAttributeBo documentTypeAttribute = new KimAttributeBo();
279         documentTypeAttribute.setId("" + documentTypeAttributeId);
280         documentTypeAttribute.setAttributeName(KewApiConstants.DOCUMENT_TYPE_NAME_DETAIL);
281         documentTypeAttribute.setNamespaceCode(NAMESPACE);
282         documentTypeAttribute.setAttributeLabel("documentType");
283         documentTypeAttribute.setActive(true);
284         documentTypeAttribute = (KimAttributeBo) KRADServiceLocator.getBusinessObjectService().save(documentTypeAttribute);
285 
286         // create "node name" KimAttribute
287         Long nodeNameAttributeId = KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_ATTR_DEFN_ID_S");
288         KimAttributeBo nodeNameAttribute = new KimAttributeBo();
289         nodeNameAttribute.setId("" + nodeNameAttributeId);
290         nodeNameAttribute.setAttributeName(KewApiConstants.ROUTE_NODE_NAME_DETAIL);
291         nodeNameAttribute.setNamespaceCode(NAMESPACE);
292         nodeNameAttribute.setAttributeLabel("nodeName");
293         nodeNameAttribute.setActive(true);
294         nodeNameAttribute = (KimAttributeBo) KRADServiceLocator.getBusinessObjectService().save(nodeNameAttribute);
295 
296         // create KimType for responsibility details
297         Long kimRespTypeId = KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_TYP_ID_S");
298         KimTypeBo kimRespType = new KimTypeBo();
299         kimRespType.setId("" + kimRespTypeId);
300         kimRespType.setName("RespDetails");
301         kimRespType.setNamespaceCode(NAMESPACE);
302         kimRespType.setServiceName("testBaseResponsibilityTypeService");
303         kimRespType.setActive(true);
304         kimRespType = (KimTypeBo) KRADServiceLocator.getBusinessObjectService().save(kimRespType);
305 
306         // create document type KimTypeAttribute
307         Long documentTypeTypeAttributeId = KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_TYP_ATTR_ID_S");
308         KimTypeAttributeBo documentTypeTypeAttribute = new KimTypeAttributeBo();
309         documentTypeTypeAttribute.setId("" + documentTypeTypeAttributeId);
310         documentTypeTypeAttribute.setActive(true);
311         documentTypeTypeAttribute.setKimAttributeId(chartAttribute.getId());
312         documentTypeTypeAttribute.setKimTypeId(kimType.getId());
313         documentTypeTypeAttribute = KRADServiceLocator.getBusinessObjectService().save(documentTypeTypeAttribute);
314 
315         // create nodeNameType KimTypeAttribute
316         Long nodeNameTypeAttributeId = KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_TYP_ATTR_ID_S");
317         KimTypeAttributeBo nodeNameTypeAttribute = new KimTypeAttributeBo();
318         nodeNameTypeAttribute.setId("" + nodeNameTypeAttributeId);
319         nodeNameTypeAttribute.setActive(true);
320         nodeNameTypeAttribute.setKimAttributeId(orgAttribute.getId());
321         nodeNameTypeAttribute.setKimTypeId(kimType.getId());
322         nodeNameTypeAttribute = KRADServiceLocator.getBusinessObjectService().save(nodeNameTypeAttribute);
323 
324         createResponsibilityForRoleRouteModuleTest(role, documentTypeAttribute, nodeNameAttribute, kimRespType, user1RolePrincipal, user2RolePrincipal, adminRolePrincipal,
325                                                    "FirstApproveReview", "RoleRouteModuleTest1", "resp1", "VoluntaryReview1", ActionRequestPolicy.FIRST);
326         //createResponsibilityForRoleRouteModuleTest1(role, documentTypeAttribute, nodeNameAttribute, kimRespType, user1RolePrincipal, user2RolePrincipal, adminRolePrincipal);
327         createResponsibilityForRoleRouteModuleTest(role, documentTypeAttribute, nodeNameAttribute, kimRespType, user1RolePrincipal, user2RolePrincipal, adminRolePrincipal,
328                                                    "AllApproveReview", "RoleRouteModuleTest2", "resp2", "VoluntaryReview2", ActionRequestPolicy.ALL);
329         //createResponsibilityForRoleRouteModuleTest2(role, documentTypeAttribute, nodeNameAttribute, kimRespType, user1RolePrincipal, user2RolePrincipal, adminRolePrincipal);
330 
331         suiteDataInitialized = true;
332     }
333 
334     private void createResponsibilityForRoleRouteModuleTest(RoleBo role, KimAttributeBo documentTypeAttribute, KimAttributeBo nodeNameAttribute, KimTypeBo kimRespType, RoleMemberBo user1RolePrincipal, RoleMemberBo user2RolePrincipal, RoleMemberBo adminRolePrincipal,
335                                                             String templateName, String docTypeDetailValue, String responsibilityName, String responsibilityDesc, ActionRequestPolicy actionRequestPolicy) {
336 
337         /**
338          * Create the responsibility template
339          */
340 
341         String templateId = String.valueOf(KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_RSP_TMPL_ID_S"));
342         ResponsibilityTemplateBo template = new ResponsibilityTemplateBo();
343         template.setId(templateId);
344         template.setNamespaceCode(NAMESPACE);
345         template.setName(templateName);
346         template.setKimTypeId(kimRespType.getId());
347         template.setActive(true);
348         template.setDescription("description");
349 
350         template = KRADServiceLocator.getBusinessObjectService().save(template);
351 
352 
353         /**
354          * Create the responsibility details for RoleRouteModuleTest1
355          */
356 
357         String responsibilityId = "" + KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_ROLE_RSP_ID_S");
358 
359         String dataId = "" + KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_GRP_ATTR_DATA_ID_S");
360         ResponsibilityAttributeBo documentTypeDetail = new ResponsibilityAttributeBo();
361         documentTypeDetail.setId(dataId);
362         documentTypeDetail.setAttributeValue(docTypeDetailValue);
363         documentTypeDetail.setKimAttribute(documentTypeAttribute);
364         documentTypeDetail.setKimAttributeId(documentTypeAttribute.getId());
365         documentTypeDetail.setKimTypeId(kimRespType.getId());
366         documentTypeDetail.setAssignedToId(responsibilityId);
367 
368         dataId = "" + KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_GRP_ATTR_DATA_ID_S");
369         ResponsibilityAttributeBo nodeNameDetail = new ResponsibilityAttributeBo();
370         nodeNameDetail.setId(dataId);
371         nodeNameDetail.setAttributeValue("Role1");
372         nodeNameDetail.setKimAttribute(nodeNameAttribute);
373         nodeNameDetail.setKimAttributeId(nodeNameAttribute.getId());
374         nodeNameDetail.setKimTypeId(kimRespType.getId());
375         nodeNameDetail.setAssignedToId(responsibilityId);
376 
377 
378 
379         /**
380          * Create the responsibility
381          */
382 
383         List<ResponsibilityAttributeBo> detailObjects = new ArrayList<ResponsibilityAttributeBo>();
384         detailObjects.add(documentTypeDetail);
385         detailObjects.add(nodeNameDetail);
386 
387         ResponsibilityBo responsibility = new ResponsibilityBo();
388         responsibility.setActive(true);
389         responsibility.setDescription(responsibilityDesc);
390         responsibility.setAttributeDetails(detailObjects);
391         responsibility.setName(responsibilityName);
392         responsibility.setNamespaceCode(NAMESPACE);
393         responsibility.setId(responsibilityId);
394         responsibility.setTemplate(template);
395         responsibility.setTemplateId(template.getId());
396 
397         responsibility = (ResponsibilityBo) KRADServiceLocator.getBusinessObjectService().save(responsibility);
398 
399         /**
400          * Create the RoleResponsibility
401          */
402 
403         String roleResponsibilityId = "" + KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_ROLE_RSP_ID_S");
404         RoleResponsibilityBo roleResponsibility = new RoleResponsibilityBo();
405         roleResponsibility.setRoleResponsibilityId(roleResponsibilityId);
406         roleResponsibility.setActive(true);
407         roleResponsibility.setResponsibilityId(responsibilityId);
408         roleResponsibility.setRoleId(role.getId());
409 
410         roleResponsibility = KRADServiceLocator.getBusinessObjectService().save(roleResponsibility);
411 
412         /**
413          * Create the various responsibility actions
414          */
415         String roleResponsibilityActionId = "" + KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_ROLE_RSP_ACTN_ID_S");
416         RoleResponsibilityActionBo roleResponsibilityAction1 = new RoleResponsibilityActionBo();
417         roleResponsibilityAction1.setId(roleResponsibilityActionId);
418         roleResponsibilityAction1.setRoleResponsibilityId(roleResponsibilityId);
419         roleResponsibilityAction1.setRoleMemberId(user1RolePrincipal.getId());
420         roleResponsibilityAction1.setActionTypeCode(KewApiConstants.ACTION_REQUEST_APPROVE_REQ);
421         roleResponsibilityAction1.setActionPolicyCode(actionRequestPolicy.getCode());
422         roleResponsibilityAction1.setPriorityNumber(1);
423         roleResponsibilityAction1 = KRADServiceLocator.getBusinessObjectService().save(roleResponsibilityAction1);
424 
425         roleResponsibilityActionId = "" + KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_ROLE_RSP_ACTN_ID_S");
426         RoleResponsibilityActionBo roleResponsibilityAction2 = new RoleResponsibilityActionBo();
427         roleResponsibilityAction2.setId(roleResponsibilityActionId);
428         roleResponsibilityAction2.setRoleResponsibilityId(roleResponsibilityId);
429         roleResponsibilityAction2.setRoleMemberId(user2RolePrincipal.getId());
430         roleResponsibilityAction2.setActionTypeCode(KewApiConstants.ACTION_REQUEST_APPROVE_REQ);
431         roleResponsibilityAction2.setActionPolicyCode(actionRequestPolicy.getCode());
432         roleResponsibilityAction2.setPriorityNumber(1);
433         roleResponsibilityAction2 = KRADServiceLocator.getBusinessObjectService().save(roleResponsibilityAction2);
434 
435         roleResponsibilityActionId = "" + KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_ROLE_RSP_ACTN_ID_S");
436         RoleResponsibilityActionBo roleResponsibilityAction3 = new RoleResponsibilityActionBo();
437         roleResponsibilityAction3.setId(roleResponsibilityActionId);
438         roleResponsibilityAction3.setRoleResponsibilityId(roleResponsibilityId);
439         roleResponsibilityAction3.setRoleMemberId(adminRolePrincipal.getId());
440         roleResponsibilityAction3.setActionTypeCode(KewApiConstants.ACTION_REQUEST_APPROVE_REQ);
441         roleResponsibilityAction3.setActionPolicyCode(actionRequestPolicy.getCode());
442         roleResponsibilityAction3.setPriorityNumber(1);
443         roleResponsibilityAction3 = KRADServiceLocator.getBusinessObjectService().save(roleResponsibilityAction3);
444 
445     }
446 
447     private void createDelegate(){
448 
449         if (suiteCreateDelegateInitialized) {
450             return;
451         }
452 
453         // create delegation KimType
454         Long kimDlgnTypeId = KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_TYP_ID_S");
455         KimTypeBo kimDlgnType = new KimTypeBo();
456         kimDlgnType.setId("" + kimDlgnTypeId);
457         kimDlgnType.setName("TestBaseDelegationType");
458         kimDlgnType.setNamespaceCode(NAMESPACE);
459         kimDlgnType.setServiceName("testBaseDelegationTypeService");
460         kimDlgnType.setActive(true);
461         kimDlgnType = KRADServiceLocator.getBusinessObjectService().save(kimDlgnType);
462 
463         /*
464          * Manually create a delegate
465          */
466         String id = "" + KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_DLGN_MBR_ID_S");
467         DelegateTypeBo delegate = new DelegateTypeBo();
468 
469         delegate.setDelegationId(id);
470         delegate.setDelegationType(DelegationType.PRIMARY);
471         delegate.setActive(true);
472         delegate.setKimTypeId("" + kimDlgnTypeId);
473         /*
474          * Assign it a role that was created above.  This should mean that every
475          * principle in the role can have the delegate added below as a delegate
476          */
477         Role role = KimApiServiceLocator.getRoleService().getRoleByNamespaceCodeAndName(NAMESPACE, ROLE_NAME);
478         assertNotNull("Role should exist.", role);
479         delegate.setRoleId(role.getId());
480         delegate = KRADServiceLocator.getBusinessObjectService().save(delegate);
481 
482         // BC of the way the jpa is handled we have to create the delagate, then the members
483         String delgMemberId = "" + KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("KRIM_DLGN_MBR_ID_S");
484         DelegateMemberBo user1RoleDelegate = new DelegateMemberBo();
485         user1RoleDelegate.setDelegationMemberId(delgMemberId);
486         // This is the user the delegation requests should be sent to.
487         Principal kPrincipal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName("ewestfal");
488         assertNotNull(kPrincipal);
489         user1RoleDelegate.setMemberId(kPrincipal.getPrincipalId());
490         /*
491          * this is checked when adding delegates in both the ActionRequestFactory
492          * and RoleServiceImpl
493          */
494         user1RoleDelegate.setType( MemberType.PRINCIPAL );
495 
496         // attach it to the delegate we created above
497         user1RoleDelegate.setDelegationId(delegate.getDelegationId());
498 
499         user1RoleDelegate = KRADServiceLocator.getBusinessObjectService().save(user1RoleDelegate);
500 
501         suiteCreateDelegateInitialized = true;
502 
503     }
504 
505 
506     @Test
507     public void testRoleRouteModule_FirstApprove() throws Exception {
508 
509         WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "RoleRouteModuleTest1");
510         document.route("");
511 
512         // in this case we should have a first approve role that contains admin and user2, we
513         // should also have a first approve role that contains just user1
514 
515         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("admin"), document.getDocumentId());
516         assertTrue("Approval should be requested.", document.isApprovalRequested());
517         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user1"), document.getDocumentId());
518         assertTrue("Approval should be requested.", document.isApprovalRequested());
519         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user2"), document.getDocumentId());
520         assertTrue("Approval should be requested.", document.isApprovalRequested());
521 
522         // examine the action requests
523         List<ActionRequest> actionRequests = KewApiServiceLocator.getWorkflowDocumentService().getRootActionRequests(document.getDocumentId());
524         // there should be 2 root action requests returned here, 1 containing the 2 requests for "BL", and one containing the request for "IN"
525         assertEquals("Should have 3 action requests.", 3, actionRequests.size());
526         int numRoots = 0;
527         for (ActionRequest actionRequest : actionRequests) {
528             // each of these should be "first approve"
529             if (actionRequest.getRequestPolicy() != null) {
530                 assertEquals(ActionRequestPolicy.FIRST, actionRequest.getRequestPolicy());
531             }
532             if (actionRequest.getParentActionRequestId() == null) {
533                 numRoots++;
534             }
535         }
536         assertEquals("There should have been 3 root requests.", 3, numRoots);
537 
538         // let's approve as "user1" and verify the document is still ENROUTE
539         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user1"), document.getDocumentId());
540         document.approve("");
541         assertTrue("Document should be ENROUTE.", document.isEnroute());
542 
543         // verify that admin and user2 still have requests
544         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("admin"), document.getDocumentId());
545         assertTrue("Approval should be requested.", document.isApprovalRequested());
546         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user2"), document.getDocumentId());
547         assertTrue("Approval should be requested.", document.isApprovalRequested());
548 
549         // let's approve as "user2" and verify the document is still ENROUTE
550         document.approve("");
551         assertTrue("Document should be ENROUTE.", document.isEnroute());
552 
553         // let's approve as "admin" and verify the document has gone FINAL
554         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("admin"), document.getDocumentId());
555         document.approve("");
556         assertTrue("Document should be FINAL.", document.isFinal());
557     }
558 
559     @Test
560     public void testRoleRouteModule_AllApprove() throws Exception {
561 
562         WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "RoleRouteModuleTest2");
563         document.route("");
564 
565         // in this case we should have all approve roles for admin, user1 and user2
566 
567         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("admin"), document.getDocumentId());
568         assertTrue("Approval should be requested.", document.isApprovalRequested());
569         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user1"), document.getDocumentId());
570         assertTrue("Approval should be requested.", document.isApprovalRequested());
571         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user2"), document.getDocumentId());
572         assertTrue("Approval should be requested.", document.isApprovalRequested());
573 
574         // examine the action requests, because there was only 1 responsibility on each role, the KEW->KIM integration
575         // transitions the requests from all approve to first approve
576         List<ActionRequest> actionRequests = KewApiServiceLocator
577                 .getWorkflowDocumentService().getRootActionRequests(document.getDocumentId());
578         assertEquals("Should have 3 action requests.", 3, actionRequests.size());
579         int numRoots = 0;
580         for (ActionRequest actionRequest : actionRequests) {
581             assertNotNull(actionRequest.getRequestPolicy());
582             assertEquals(ActionRequestPolicy.FIRST, actionRequest.getRequestPolicy());
583             if (actionRequest.getParentActionRequestId() == null) {
584                 numRoots++;
585             }
586         }
587         assertEquals("There should have been 3 root requests.", 3, numRoots);
588 
589         // let's approve as "user1" and verify the document does NOT go FINAL
590         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user1"), document.getDocumentId());
591         document.approve("");
592         assertTrue("Document should still be enroute.", document.isEnroute());
593 
594         // verify that admin and user2 still have requests
595         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("admin"), document.getDocumentId());
596         assertTrue("Approval should be requested.", document.isApprovalRequested());
597         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user2"), document.getDocumentId());
598         assertTrue("Approval should be requested.", document.isApprovalRequested());
599 
600         // approve as "user2" and verify document is still ENROUTE
601         document.approve("");
602         assertTrue("Document should be ENROUTE.", document.isEnroute());
603 
604         // now approve as "admin", coument should be FINAL
605         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("admin"), document.getDocumentId());
606         document.approve("");
607         assertTrue("Document should be FINAL.", document.isFinal());
608     }
609 
610     /**
611      * Tests that ActionRequests are regenerated when RoleResponsibilityActions are programmatically changes via the RoleService
612      * (Must be run before tests which alter delegate configuration)
613      */
614     @Test
615     public void testRoleRouteModule_RoleResponsibilityActionUpdate() throws Exception {
616 
617         WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "RoleRouteModuleTest1");
618         document.route("");
619 
620         // in this case we should have a first approve role that contains admin and user2, we
621         // should also have a first approve role that contains just user1
622 
623         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("admin"), document.getDocumentId());
624         assertTrue("Approval should be requested.", document.isApprovalRequested());
625         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user1"), document.getDocumentId());
626         assertTrue("Approval should be requested.", document.isApprovalRequested());
627         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user2"), document.getDocumentId());
628         assertTrue("Approval should be requested.", document.isApprovalRequested());
629 
630         // examine the action requests
631         List<ActionRequest> actionRequests = KewApiServiceLocator.getWorkflowDocumentService().getRootActionRequests(document.getDocumentId());
632         // there should be 2 root action requests returned here, 1 containing the 2 requests for "BL", and one containing the request for "IN"
633         assertEquals("Should have 3 action requests.", 3, actionRequests.size());
634         int numRoots = 0;
635         for (ActionRequest actionRequest : actionRequests) {
636             // each of these should be "first approve"
637             if (actionRequest.getRequestPolicy() != null) {
638                 assertEquals(ActionRequestPolicy.FIRST, actionRequest.getRequestPolicy());
639             }
640             if (actionRequest.getParentActionRequestId() == null) {
641                 numRoots++;
642             }
643         }
644         assertEquals("There should have been 3 root requests.", 3, numRoots);
645 
646         RoleService roleService = KimApiServiceLocator.getRoleService();
647         Role role = roleService.getRoleByNamespaceCodeAndName(NAMESPACE, ROLE_NAME);
648         assertNotNull(role);
649         Principal user1Principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName("user1");
650         List<RoleMembership> members = roleService.getRoleMembers(Collections.singletonList(role.getId()), null);
651         // find user1principal
652         RoleMembership m = null;
653         for (RoleMembership rm: members) {
654             if (user1Principal.getPrincipalId().equals(rm.getMemberId())) {
655                 m = rm;
656                 break;
657             }
658         }
659         assertNotNull("Failed to find user1Principal role membership", m);
660         assertEquals(user1Principal.getPrincipalId(), m.getMemberId());
661         List<RoleResponsibilityAction> rras = roleService.getRoleMemberResponsibilityActions(m.getId());
662         assertEquals(2, rras.size());
663         RoleResponsibilityAction rra = rras.get(0);
664         RoleResponsibilityAction.Builder b = RoleResponsibilityAction.Builder.create(rra);
665         b.setActionTypeCode(ActionType.ACKNOWLEDGE.getCode());
666 
667         roleService.updateRoleResponsibilityAction(b.build());
668 
669         // action request should have changed!
670         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user1"), document.getDocumentId());
671         assertFalse("Approval should NOT be requested.", document.isApprovalRequested());
672         assertTrue("Acknowledge should be requested.", document.isAcknowledgeRequested());
673         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("admin"), document.getDocumentId());
674         assertTrue("Approval should be requested.", document.isApprovalRequested());
675         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user2"), document.getDocumentId());
676         assertTrue("Approval should be requested.", document.isApprovalRequested());
677 
678         roleService.deleteRoleResponsibilityAction(rras.get(0).getId());
679 
680         // no actions should be requested of user1 now
681         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user1"), document.getDocumentId());
682         assertTrue(document.getRequestedActions().getRequestedActions().isEmpty());
683 
684         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("admin"), document.getDocumentId());
685         assertTrue("Approval should be requested.", document.isApprovalRequested());
686         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("user2"), document.getDocumentId());
687         assertTrue("Approval should be requested.", document.isApprovalRequested());
688 
689         // examine the action requests
690         actionRequests = KewApiServiceLocator.getWorkflowDocumentService().getRootActionRequests(document.getDocumentId());
691         // now should be only 2 requests
692         assertEquals("Should have 2 action requests.", 2, actionRequests.size());
693     }
694 
695     @Test
696     public void testDelegate() throws Exception{
697         this.createDelegate();
698 
699         WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), "RoleRouteModuleTest2");
700         document.route("");
701 
702         String ewestfalPrincipalId = getPrincipalIdForName("ewestfal");
703 
704         // now our fancy new delegate should have an action request
705         document = WorkflowDocumentFactory.loadDocument(ewestfalPrincipalId, document.getDocumentId());
706         assertTrue("ewestfal should be able to approve", document.isApprovalRequested());
707 
708         // let's look at the action requests, there should be 3 root requests, each one should have a delegation to ewestfal
709         List<ActionRequest> actionRequests = document.getRootActionRequests();
710         assertEquals(3, actionRequests.size());
711 
712         for (ActionRequest actionRequest : actionRequests) {
713             // none of the root requests should be to ewestfal
714             assertFalse(ewestfalPrincipalId.equals(actionRequest.getPrincipalId()));
715             // but all of the delegate requests should!
716             assertEquals(1, actionRequest.getChildRequests().size());
717             ActionRequest delegateRequest = actionRequest.getChildRequests().get(0);
718             assertEquals(ewestfalPrincipalId, delegateRequest.getPrincipalId());
719             assertEquals("Delegation type should been PRIMARY", DelegationType.PRIMARY, delegateRequest.getDelegationType());
720         }
721     }
722 
723     @Test
724     public void testDelegateApproval() throws Exception{
725         this.createDelegate();
726 
727         WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), "RoleRouteModuleTest2");
728         document.route("");
729 
730         // See if the delegate can approve the document
731         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
732         assertTrue("ewestfal should have an approval request", document.isApprovalRequested());
733         document.approve("");
734 
735         assertTrue("Document should have been approved by the delegate.", document.isFinal());
736     }
737 
738     @Test
739     public void testRoleWithNoMembers() throws Exception {
740         getTransactionTemplate().execute(new TransactionCallback<Object>() {
741             public Object doInTransaction(TransactionStatus status) {
742 
743                 try {
744 
745                     // first let's clear all of the members out of our role
746 
747                     Role role = KimApiServiceLocator.getRoleService().getRoleByNamespaceCodeAndName(NAMESPACE,
748                             ROLE_NAME);
749                     Map<String, String> criteria = new HashMap<String, String>();
750                     criteria.put("roleId", role.getId());
751                     List<RoleMemberBo> roleMembers = (List<RoleMemberBo>) KRADServiceLocator
752                             .getBusinessObjectService().findMatching(RoleMemberBo.class, criteria);
753                     assertFalse("role member list should not currently be empty", roleMembers.isEmpty());
754                     for (RoleMemberBo roleMember : roleMembers) {
755                         //String roleMemberId = roleMember.getRoleMemberId();
756                         //RoleMemberImpl roleMemberImpl = KRADServiceLocatorInternal.getBusinessObjectService().findBySinglePrimaryKey(RoleMemberImpl.class, roleMemberId);
757                         assertNotNull("Role Member should exist.", roleMember);
758                         KRADServiceLocator.getBusinessObjectService().delete(roleMember);
759                     }
760 
761                     List<RoleMembership> roleMemberInfos = KimApiServiceLocator.getRoleService().getRoleMembers(Collections.singletonList(role.getId()), Collections.<String, String>emptyMap());
762                     assertEquals("role member list should be empty now", 0, roleMemberInfos.size());
763 
764                     // now that we've removed all members from the Role, let's trying routing the doc!
765                     WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "RoleRouteModuleTest1");
766                     document.route("");
767 
768                     // the document should be final now, because the role has no members so all nodes should have been skipped for routing purposes
769 
770                     assertTrue("document should now be in final status", document.isFinal());
771 
772                     // verify that the document went through the appropriate route path
773 
774                     List<RouteNodeInstance> routeNodeInstances = document.getRouteNodeInstances();
775                     assertEquals("Document should have 2 route node instances", 2, routeNodeInstances.size());
776 
777                     return null;
778                 } finally {
779                     status.setRollbackOnly();
780                 }
781             }
782         });
783     }
784 
785 }