View Javadoc
1   /**
2    * Copyright 2005-2014 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.criteria.QueryByCriteria;
20  import org.kuali.rice.core.api.criteria.QueryResults;
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.action.ActionRequestPolicy;
24  import org.kuali.rice.kew.impl.type.KewAttributeDefinitionBo;
25  import org.kuali.rice.kew.impl.type.KewTypeAttributeBo;
26  import org.kuali.rice.kew.impl.type.KewTypeBo;
27  import org.kuali.rice.kew.responsibility.service.ResponsibilityIdService;
28  import org.kuali.rice.kew.service.KEWServiceLocator;
29  import org.kuali.rice.kew.test.KEWTestCase;
30  import org.kuali.rice.krad.data.DataObjectService;
31  import org.kuali.rice.krad.data.KradDataServiceLocator;
32  import org.kuali.rice.krad.data.PersistenceOption;
33  import org.springframework.orm.jpa.JpaSystemException;
34  
35  import static org.kuali.rice.core.api.criteria.PredicateFactory.*;
36  
37  import static org.junit.Assert.*;
38  
39  /**
40   * Test the basic persistence of business objects related to PeopleFlows.
41   *
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   */
44  public class PeopleFlowBoTest extends KEWTestCase {
45  
46      private DataObjectService dataObjectService;
47      private ResponsibilityIdService responsibilityIdService;
48  
49      @org.junit.Before
50      public void setupBoService() {
51          dataObjectService = KradDataServiceLocator.getDataObjectService();
52          responsibilityIdService = KEWServiceLocator.getResponsibilityIdService();
53      }
54  
55      @Test(expected = JpaSystemException.class)
56      public void testKewTypeBoBasicPersist() {
57          KewTypeBoBuilder builder = new KewTypeBoBuilder("testType", "testNamespace");
58  
59          dataObjectService.save(builder.build(), PersistenceOption.FLUSH);
60          // try to persist the same information again, it should fail because of the unique constraint on name + namespace
61          dataObjectService.save(builder.build(), PersistenceOption.FLUSH);
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              attributeDefn = dataObjectService.save(attributeDefn);
77  
78              KewTypeAttributeBo typeAttribute = new KewTypeAttributeBo();
79              typeAttribute.setSequenceNumber(i);
80              typeAttribute.setAttributeDefinition(attributeDefn);
81              kewTypeBo.getAttributes().add(typeAttribute);
82              typeAttribute.setType(kewTypeBo);
83          }
84  
85          dataObjectService.save(kewTypeBo);
86      }
87  
88      @Test
89      public void testPeopleFlowPersonMembers() {
90          PeopleFlowMemberBo peopleFlowMember = new PeopleFlowMemberBo();
91          peopleFlowMember.setMemberType(MemberType.PRINCIPAL);
92          peopleFlowMember.setMemberId("admin");
93          peopleFlowMember.setPriority(1);
94          peopleFlowMember.setResponsibilityId(responsibilityIdService.getNewResponsibilityId());
95          assertNotNull(peopleFlowMember.getPerson());
96          assertEquals("admin", peopleFlowMember.getPerson().getPrincipalName());
97  
98          PeopleFlowDelegateBo peopleFlowDelegate = new PeopleFlowDelegateBo();
99          peopleFlowDelegate.setMemberType(MemberType.PRINCIPAL);
100         peopleFlowDelegate.setMemberId("admin");
101         peopleFlowDelegate.setDelegationTypeCode(DelegationType.PRIMARY.getCode());
102         peopleFlowDelegate.setResponsibilityId(responsibilityIdService.getNewResponsibilityId());
103         assertNotNull(peopleFlowDelegate.getPerson());
104         assertEquals("admin", peopleFlowDelegate.getPerson().getPrincipalName());
105     }
106 
107     @Test
108     public void testPeopleFlowBoPersist() {
109         testKewTypeBoFullPersist();
110 
111         QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
112         criteria.setPredicates(equal("name", "testType"), equal("namespace", "testNamespace"));
113 
114         QueryResults<KewTypeBo> results = dataObjectService.findMatching(KewTypeBo.class, criteria.build());
115         assertEquals(1, results.getResults().size());
116         KewTypeBo kewTypeBo = results.getResults().get(0);
117 
118         // minimal peopleflow
119         PeopleFlowBo peopleFlowBo = new PeopleFlowBo();
120         peopleFlowBo.setDescription("description of testPeopleFlow");
121         peopleFlowBo.setName("testPeopleFlow");
122         peopleFlowBo.setNamespaceCode("testNamespace");
123         peopleFlowBo.setTypeId(kewTypeBo.getId());
124         
125         peopleFlowBo = dataObjectService.save(peopleFlowBo);
126 
127         // fill out peopleflow
128         KewTypeAttributeBo attribute = kewTypeBo.getAttributes().get(0);
129 
130         PeopleFlowAttributeBo peopleFlowAttr = new PeopleFlowAttributeBo();
131         peopleFlowAttr.setPeopleFlow(peopleFlowBo);
132         peopleFlowAttr.setAttributeDefinition(attribute.getAttributeDefinition());
133         peopleFlowAttr.setPeopleFlow(peopleFlowBo);
134         peopleFlowAttr.setValue("testAttrValue");
135 
136         peopleFlowBo.getAttributeBos().add(peopleFlowAttr);
137 
138         PeopleFlowMemberBo peopleFlowMember = new PeopleFlowMemberBo();
139         peopleFlowMember.setPeopleFlow(peopleFlowBo);
140         peopleFlowMember.setMemberType(MemberType.PRINCIPAL);
141         peopleFlowMember.setMemberId("admin");
142         peopleFlowMember.setPriority(1);
143         peopleFlowMember.setResponsibilityId(responsibilityIdService.getNewResponsibilityId());
144 
145         peopleFlowBo.getMembers().add(peopleFlowMember);
146 
147         PeopleFlowDelegateBo peopleFlowDelegate1 = new PeopleFlowDelegateBo();
148         peopleFlowDelegate1.setPeopleFlowMember(peopleFlowMember);
149         peopleFlowDelegate1.setMemberType(MemberType.GROUP);
150         peopleFlowDelegate1.setMemberId("1");
151         peopleFlowDelegate1.setDelegationTypeCode(DelegationType.PRIMARY.getCode());
152         peopleFlowDelegate1.setResponsibilityId(responsibilityIdService.getNewResponsibilityId());
153         peopleFlowMember.getDelegates().add(peopleFlowDelegate1);
154 
155         PeopleFlowDelegateBo peopleFlowDelegate2 = new PeopleFlowDelegateBo();
156         peopleFlowDelegate2.setPeopleFlowMember(peopleFlowMember);
157         peopleFlowDelegate2.setMemberType(MemberType.ROLE);
158         peopleFlowDelegate2.setMemberId("2");
159         peopleFlowDelegate2.setActionRequestPolicyCode(ActionRequestPolicy.FIRST.getCode());
160         peopleFlowDelegate2.setDelegationTypeCode(DelegationType.SECONDARY.getCode());
161         peopleFlowDelegate2.setResponsibilityId(responsibilityIdService.getNewResponsibilityId());
162         peopleFlowMember.getDelegates().add(peopleFlowDelegate2);
163 
164         peopleFlowBo = dataObjectService.save(peopleFlowBo);
165 
166         assertNotNull(peopleFlowBo.getId());
167         peopleFlowBo = dataObjectService.find(PeopleFlowBo.class, peopleFlowBo.getId());
168 
169         assertNotNull(peopleFlowBo);
170         assertNotNull(peopleFlowBo.getId());
171         assertTrue(peopleFlowBo.getMembers().size() == 1);
172 
173         PeopleFlowMemberBo memberBo = peopleFlowBo.getMembers().get(0);
174         assertNotNull(memberBo.getId());
175         assertEquals(peopleFlowBo.getId(), memberBo.getPeopleFlow().getId());
176         assertEquals("admin", memberBo.getMemberId());
177         assertEquals(MemberType.PRINCIPAL, memberBo.getMemberType());
178         assertNotNull(memberBo.getPerson());
179         assertEquals("admin", memberBo.getPerson().getPrincipalName());
180         assertEquals(peopleFlowMember.getResponsibilityId(), memberBo.getResponsibilityId());
181         assertSame(1, memberBo.getPriority());
182         assertTrue(memberBo.getDelegates().size() == 2);
183 
184         PeopleFlowDelegateBo delegateBo1 = memberBo.getDelegates().get(0);
185         assertNotNull(delegateBo1.getId());
186         assertEquals(memberBo.getId(), delegateBo1.getPeopleFlowMember().getId());
187         assertEquals("1", delegateBo1.getMemberId());
188         assertEquals(MemberType.GROUP, delegateBo1.getMemberType());
189         assertEquals(DelegationType.PRIMARY.getCode(), delegateBo1.getDelegationTypeCode());
190         assertEquals(peopleFlowDelegate1.getResponsibilityId(), delegateBo1.getResponsibilityId());
191         assertNull(delegateBo1.getActionRequestPolicyCode());
192 
193         PeopleFlowDelegateBo delegateBo2 = memberBo.getDelegates().get(1);
194         assertNotNull(delegateBo2.getId());
195         assertEquals(memberBo.getId(), delegateBo2.getPeopleFlowMember().getId());
196         assertEquals("2", delegateBo2.getMemberId());
197         assertEquals(MemberType.ROLE, delegateBo2.getMemberType());
198         assertEquals(DelegationType.SECONDARY.getCode(), delegateBo2.getDelegationTypeCode());
199         assertEquals(peopleFlowDelegate2.getResponsibilityId(), delegateBo2.getResponsibilityId());
200         assertEquals(ActionRequestPolicy.FIRST.getCode(), delegateBo2.getActionRequestPolicyCode());
201     }
202 
203     public static KewTypeBo buildMinimalKewTypeBo() {
204         KewTypeBo kewTypeBo = new KewTypeBo();
205         kewTypeBo.setName("TestType");
206         kewTypeBo.setNamespace("TestNamespace");
207         return kewTypeBo;
208     }
209 
210     private static class KewTypeBoBuilder {
211 
212         private boolean active = true;
213         private String name;
214         private String namespace;
215         private String serviceName;
216 
217         public KewTypeBoBuilder(String name, String namespace) {
218             this.name = name;
219             this.namespace = namespace;
220         }
221 
222         public KewTypeBoBuilder setServiceName(String serviceName) {
223             this.serviceName = serviceName;
224             return this;
225         }
226 
227         public KewTypeBoBuilder setName(String name) {
228             this.name = name;
229             return this;
230         }
231 
232         public KewTypeBoBuilder setNamespace(String namespace) {
233             this.namespace = namespace;
234             return this;
235         }
236 
237         public KewTypeBoBuilder setActive(boolean active) {
238             this.active = active;
239             return this;
240         }
241 
242         public KewTypeBo build() {
243             KewTypeBo kewTypeBo = new KewTypeBo();
244             kewTypeBo.setActive(active);
245             kewTypeBo.setName(name);
246             kewTypeBo.setNamespace(namespace);
247             kewTypeBo.setServiceName(serviceName);
248             return kewTypeBo;
249         }
250     }
251 
252 }