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.impl.peopleflow;
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.action.ActionRequestPolicy;
22  import org.kuali.rice.kew.impl.type.KewAttributeDefinitionBo;
23  import org.kuali.rice.kew.impl.type.KewTypeAttributeBo;
24  import org.kuali.rice.kew.impl.type.KewTypeBo;
25  import org.kuali.rice.kew.responsibility.service.ResponsibilityIdService;
26  import org.kuali.rice.kew.service.KEWServiceLocator;
27  import org.kuali.rice.kew.test.KEWTestCase;
28  import org.kuali.rice.krad.service.BusinessObjectService;
29  import org.kuali.rice.krad.service.KRADServiceLocator;
30  
31  import java.util.HashMap;
32  import java.util.Map;
33  
34  import static org.junit.Assert.*;
35  
36  /**
37   * Test the basic persistence of business objects related to PeopleFlows
38   */
39  public class PeopleFlowBoTest extends KEWTestCase {
40  
41      private BusinessObjectService boService;
42      private ResponsibilityIdService responsibilityIdService;
43  
44      @org.junit.Before
45      public void setupBoService() {
46          boService = KRADServiceLocator.getBusinessObjectService();
47          responsibilityIdService = KEWServiceLocator.getResponsibilityIdService();
48      }
49  
50      @Test(expected = java.lang.Throwable.class)
51      public void testKewTypeBoBasicPersist() {
52          KewTypeBoBuilder builder = new KewTypeBoBuilder("testType", "testNamespace");
53  
54          boService.save(builder.build());
55  //        try {
56              // same info again should be a no go
57              boService.save(builder.build());
58  //            fail("this should violate unique constraints");
59  //        } catch (Exception e) {
60              // good
61  //        }
62      }
63      @Test
64      public void testKewTypeBoFullPersist() {
65          KewTypeBoBuilder builder = new KewTypeBoBuilder("testType", "testNamespace").setServiceName("testService");
66          KewTypeBo kewTypeBo = builder.build();
67  
68          for (int i=1; i<=3; i++) {
69              KewAttributeDefinitionBo attributeDefn = new KewAttributeDefinitionBo();
70              attributeDefn.setName("attrDef"+i);
71              attributeDefn.setDescription("this is a description of attrDef" + i);
72              attributeDefn.setComponentName("componentName" + i);
73              attributeDefn.setLabel("label" + i);
74              attributeDefn.setNamespace(kewTypeBo.getNamespace());
75  
76              boService.save(attributeDefn);
77  
78              KewTypeAttributeBo typeAttribute = new KewTypeAttributeBo();
79              typeAttribute.setSequenceNumber(i);
80              typeAttribute.setAttributeDefinition(attributeDefn);
81              kewTypeBo.getAttributes().add(typeAttribute);
82          }
83  
84          boService.save(kewTypeBo);
85      }
86  
87      @Test
88      public void testPeopleFlowPersonMembers() {
89          PeopleFlowMemberBo peopleFlowMember = new PeopleFlowMemberBo();
90          peopleFlowMember.setMemberType(MemberType.PRINCIPAL);
91          peopleFlowMember.setMemberId("admin");
92          peopleFlowMember.setPriority(1);
93          peopleFlowMember.setResponsibilityId(responsibilityIdService.getNewResponsibilityId());
94          assertNotNull(peopleFlowMember.getPerson());
95          assertEquals("admin", peopleFlowMember.getPerson().getPrincipalName());
96  
97          PeopleFlowDelegateBo peopleFlowDelegate = new PeopleFlowDelegateBo();
98          peopleFlowDelegate.setMemberType(MemberType.PRINCIPAL);
99          peopleFlowDelegate.setMemberId("admin");
100         peopleFlowDelegate.setDelegationTypeCode(DelegationType.PRIMARY.getCode());
101         peopleFlowDelegate.setResponsibilityId(responsibilityIdService.getNewResponsibilityId());
102         assertNotNull(peopleFlowDelegate.getPerson());
103         assertEquals("admin", peopleFlowDelegate.getPerson().getPrincipalName());
104     }
105 
106     @Test
107     public void testPeopleFlowBoPersist() {
108         testKewTypeBoFullPersist();
109 
110         Map<String,String> keysMap = new HashMap<String, String>();
111         keysMap.put("name", "testType");
112         keysMap.put("namespace", "testNamespace");
113 
114         KewTypeBo kewTypeBo = boService.findByPrimaryKey(KewTypeBo.class, keysMap);
115 
116         // minimal peopleflow
117         PeopleFlowBo peopleFlowBo = new PeopleFlowBo();
118         peopleFlowBo.setDescription("description of testPeopleFlow");
119         peopleFlowBo.setName("testPeopleFlow");
120         peopleFlowBo.setNamespaceCode("testNamespace");
121         peopleFlowBo.setTypeId(kewTypeBo.getId());
122         
123         boService.save(peopleFlowBo);
124 
125         // fill out peopleflow
126         KewTypeAttributeBo attribute = kewTypeBo.getAttributes().get(0);
127 
128         PeopleFlowAttributeBo peopleFlowAttr = new PeopleFlowAttributeBo();
129         peopleFlowAttr.setAttributeDefinition(attribute.getAttributeDefinition());
130         peopleFlowAttr.setPeopleFlowId(peopleFlowBo.getId());
131         peopleFlowAttr.setValue("testAttrValue");
132 
133         peopleFlowBo.getAttributeBos().add(peopleFlowAttr);
134 
135         PeopleFlowMemberBo peopleFlowMember = new PeopleFlowMemberBo();
136         peopleFlowMember.setMemberType(MemberType.PRINCIPAL);
137         peopleFlowMember.setMemberId("admin");
138         peopleFlowMember.setPriority(1);
139         peopleFlowMember.setResponsibilityId(responsibilityIdService.getNewResponsibilityId());
140 
141         peopleFlowBo.getMembers().add(peopleFlowMember);
142 
143         PeopleFlowDelegateBo peopleFlowDelegate1 = new PeopleFlowDelegateBo();
144         peopleFlowDelegate1.setMemberType(MemberType.GROUP);
145         peopleFlowDelegate1.setMemberId("1");
146         peopleFlowDelegate1.setDelegationTypeCode(DelegationType.PRIMARY.getCode());
147         peopleFlowDelegate1.setResponsibilityId(responsibilityIdService.getNewResponsibilityId());
148         peopleFlowMember.getDelegates().add(peopleFlowDelegate1);
149 
150         PeopleFlowDelegateBo peopleFlowDelegate2 = new PeopleFlowDelegateBo();
151         peopleFlowDelegate2.setMemberType(MemberType.ROLE);
152         peopleFlowDelegate2.setMemberId("2");
153         peopleFlowDelegate2.setActionRequestPolicyCode(ActionRequestPolicy.FIRST.getCode());
154         peopleFlowDelegate2.setDelegationTypeCode(DelegationType.SECONDARY.getCode());
155         peopleFlowDelegate2.setResponsibilityId(responsibilityIdService.getNewResponsibilityId());
156         peopleFlowMember.getDelegates().add(peopleFlowDelegate2);
157 
158         boService.save(peopleFlowBo);
159 
160         assertNotNull(peopleFlowBo.getId());
161         peopleFlowBo = boService.findBySinglePrimaryKey(PeopleFlowBo.class, peopleFlowBo.getId());
162 
163         assertNotNull(peopleFlowBo);
164         assertNotNull(peopleFlowBo.getId());
165         assertTrue(peopleFlowBo.getMembers().size() == 1);
166 
167         PeopleFlowMemberBo memberBo = peopleFlowBo.getMembers().get(0);
168         assertNotNull(memberBo.getId());
169         assertEquals(peopleFlowBo.getId(), memberBo.getPeopleFlowId());
170         assertEquals("admin", memberBo.getMemberId());
171         assertEquals(MemberType.PRINCIPAL, memberBo.getMemberType());
172         assertNotNull(memberBo.getPerson());
173         assertEquals("admin", memberBo.getPerson().getPrincipalName());
174         assertEquals(peopleFlowMember.getResponsibilityId(), memberBo.getResponsibilityId());
175         assertSame(1, memberBo.getPriority());
176         assertTrue(memberBo.getDelegates().size() == 2);
177 
178         PeopleFlowDelegateBo delegateBo1 = memberBo.getDelegates().get(0);
179         assertNotNull(delegateBo1.getId());
180         assertEquals(memberBo.getId(), delegateBo1.getPeopleFlowMemberId());
181         assertEquals("1", delegateBo1.getMemberId());
182         assertEquals(MemberType.GROUP, delegateBo1.getMemberType());
183         assertEquals(DelegationType.PRIMARY.getCode(), delegateBo1.getDelegationTypeCode());
184         assertEquals(peopleFlowDelegate1.getResponsibilityId(), delegateBo1.getResponsibilityId());
185         assertNull(delegateBo1.getActionRequestPolicyCode());
186 
187         PeopleFlowDelegateBo delegateBo2 = memberBo.getDelegates().get(1);
188         assertNotNull(delegateBo2.getId());
189         assertEquals(memberBo.getId(), delegateBo2.getPeopleFlowMemberId());
190         assertEquals("2", delegateBo2.getMemberId());
191         assertEquals(MemberType.ROLE, delegateBo2.getMemberType());
192         assertEquals(DelegationType.SECONDARY.getCode(), delegateBo2.getDelegationTypeCode());
193         assertEquals(peopleFlowDelegate2.getResponsibilityId(), delegateBo2.getResponsibilityId());
194         assertEquals(ActionRequestPolicy.FIRST.getCode(), delegateBo2.getActionRequestPolicyCode());
195     }
196 
197     public static KewTypeBo buildMinimalKewTypeBo() {
198         KewTypeBo kewTypeBo = new KewTypeBo();
199         kewTypeBo.setName("TestType");
200         kewTypeBo.setNamespace("TestNamespace");
201         return kewTypeBo;
202     }
203 
204     private static class KewTypeBoBuilder {
205 
206         private boolean active = true;
207         private String name;
208         private String namespace;
209         private String serviceName;
210 
211         public KewTypeBoBuilder(String name, String namespace) {
212             this.name = name;
213             this.namespace = namespace;
214         }
215 
216         public KewTypeBoBuilder setServiceName(String serviceName) {
217             this.serviceName = serviceName;
218             return this;
219         }
220 
221         public KewTypeBoBuilder setName(String name) {
222             this.name = name;
223             return this;
224         }
225 
226         public KewTypeBoBuilder setNamespace(String namespace) {
227             this.namespace = namespace;
228             return this;
229         }
230 
231         public KewTypeBoBuilder setActive(boolean active) {
232             this.active = active;
233             return this;
234         }
235 
236         public KewTypeBo build() {
237             KewTypeBo kewTypeBo = new KewTypeBo();
238             kewTypeBo.setActive(active);
239             kewTypeBo.setName(name);
240             kewTypeBo.setNamespace(namespace);
241             kewTypeBo.setServiceName(serviceName);
242             return kewTypeBo;
243         }
244     }
245 
246 }