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