001/**
002 * Copyright 2005-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kew.impl.peopleflow;
017
018import org.junit.Test;
019import org.kuali.rice.core.api.criteria.QueryByCriteria;
020import org.kuali.rice.core.api.criteria.QueryResults;
021import org.kuali.rice.core.api.delegation.DelegationType;
022import org.kuali.rice.core.api.membership.MemberType;
023import org.kuali.rice.kew.api.action.ActionRequestPolicy;
024import org.kuali.rice.kew.impl.type.KewAttributeDefinitionBo;
025import org.kuali.rice.kew.impl.type.KewTypeAttributeBo;
026import org.kuali.rice.kew.impl.type.KewTypeBo;
027import org.kuali.rice.kew.responsibility.service.ResponsibilityIdService;
028import org.kuali.rice.kew.service.KEWServiceLocator;
029import org.kuali.rice.kew.test.KEWTestCase;
030import org.kuali.rice.krad.data.DataObjectService;
031import org.kuali.rice.krad.data.KradDataServiceLocator;
032import org.kuali.rice.krad.data.PersistenceOption;
033import org.springframework.orm.jpa.JpaSystemException;
034
035import static org.kuali.rice.core.api.criteria.PredicateFactory.*;
036
037import static org.junit.Assert.*;
038
039/**
040 * Test the basic persistence of business objects related to PeopleFlows.
041 *
042 * @author Kuali Rice Team (rice.collab@kuali.org)
043 */
044public class PeopleFlowBoTest extends KEWTestCase {
045
046    private DataObjectService dataObjectService;
047    private ResponsibilityIdService responsibilityIdService;
048
049    @org.junit.Before
050    public void setupBoService() {
051        dataObjectService = KradDataServiceLocator.getDataObjectService();
052        responsibilityIdService = KEWServiceLocator.getResponsibilityIdService();
053    }
054
055    @Test(expected = JpaSystemException.class)
056    public void testKewTypeBoBasicPersist() {
057        KewTypeBoBuilder builder = new KewTypeBoBuilder("testType", "testNamespace");
058
059        dataObjectService.save(builder.build(), PersistenceOption.FLUSH);
060        // try to persist the same information again, it should fail because of the unique constraint on name + namespace
061        dataObjectService.save(builder.build(), PersistenceOption.FLUSH);
062    }
063    @Test
064    public void testKewTypeBoFullPersist() {
065        KewTypeBoBuilder builder = new KewTypeBoBuilder("testType", "testNamespace").setServiceName("testService");
066        KewTypeBo kewTypeBo = builder.build();
067
068        for (int i=1; i<=3; i++) {
069            KewAttributeDefinitionBo attributeDefn = new KewAttributeDefinitionBo();
070            attributeDefn.setName("attrDef"+i);
071            attributeDefn.setDescription("this is a description of attrDef" + i);
072            attributeDefn.setComponentName("componentName" + i);
073            attributeDefn.setLabel("label" + i);
074            attributeDefn.setNamespace(kewTypeBo.getNamespace());
075
076            attributeDefn = dataObjectService.save(attributeDefn);
077
078            KewTypeAttributeBo typeAttribute = new KewTypeAttributeBo();
079            typeAttribute.setSequenceNumber(i);
080            typeAttribute.setAttributeDefinition(attributeDefn);
081            kewTypeBo.getAttributes().add(typeAttribute);
082            typeAttribute.setType(kewTypeBo);
083        }
084
085        dataObjectService.save(kewTypeBo);
086    }
087
088    @Test
089    public void testPeopleFlowPersonMembers() {
090        PeopleFlowMemberBo peopleFlowMember = new PeopleFlowMemberBo();
091        peopleFlowMember.setMemberType(MemberType.PRINCIPAL);
092        peopleFlowMember.setMemberId("admin");
093        peopleFlowMember.setPriority(1);
094        peopleFlowMember.setResponsibilityId(responsibilityIdService.getNewResponsibilityId());
095        assertNotNull(peopleFlowMember.getPerson());
096        assertEquals("admin", peopleFlowMember.getPerson().getPrincipalName());
097
098        PeopleFlowDelegateBo peopleFlowDelegate = new PeopleFlowDelegateBo();
099        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}