View Javadoc

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