001    /**
002     * Copyright 2005-2013 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     */
016    package org.kuali.rice.kew.impl.peopleflow;
017    
018    import org.junit.Test;
019    import org.kuali.rice.core.api.criteria.QueryByCriteria;
020    import org.kuali.rice.core.api.criteria.QueryResults;
021    import org.kuali.rice.core.api.delegation.DelegationType;
022    import org.kuali.rice.core.api.membership.MemberType;
023    import org.kuali.rice.kew.api.action.ActionRequestPolicy;
024    import org.kuali.rice.kew.impl.type.KewAttributeDefinitionBo;
025    import org.kuali.rice.kew.impl.type.KewTypeAttributeBo;
026    import org.kuali.rice.kew.impl.type.KewTypeBo;
027    import org.kuali.rice.kew.responsibility.service.ResponsibilityIdService;
028    import org.kuali.rice.kew.service.KEWServiceLocator;
029    import org.kuali.rice.kew.test.KEWTestCase;
030    import org.kuali.rice.krad.data.DataObjectService;
031    import org.kuali.rice.krad.data.KradDataServiceLocator;
032    import org.kuali.rice.krad.data.PersistenceOption;
033    
034    import javax.persistence.PersistenceException;
035    
036    import static org.kuali.rice.core.api.criteria.PredicateFactory.*;
037    
038    import static org.junit.Assert.*;
039    
040    /**
041     * Test the basic persistence of business objects related to PeopleFlows
042     *
043     * @author Kuali Rice Team (rice.collab@kuali.org)
044     */
045    public class PeopleFlowBoTest extends KEWTestCase {
046    
047        private DataObjectService dataObjectService;
048        private ResponsibilityIdService responsibilityIdService;
049    
050        @org.junit.Before
051        public void setupBoService() {
052            dataObjectService = KradDataServiceLocator.getDataObjectService();
053            responsibilityIdService = KEWServiceLocator.getResponsibilityIdService();
054        }
055    
056        // TODO - hopefully this test will eventually start failing because we actually want the save method to throw a
057        // {@link DataAccessException}, but that's not going to happen until the following jira is resolved:
058        //
059        // https://jira.kuali.org/browse/KULRICE-9798
060        @Test(expected = PersistenceException.class)
061        public void testKewTypeBoBasicPersist() {
062            KewTypeBoBuilder builder = new KewTypeBoBuilder("testType", "testNamespace");
063    
064            dataObjectService.save(builder.build(), PersistenceOption.FLUSH);
065            // try to persist the same information again, it should fail because of the unique constraint on name + namespace
066            dataObjectService.save(builder.build(), PersistenceOption.FLUSH);
067        }
068        @Test
069        public void testKewTypeBoFullPersist() {
070            KewTypeBoBuilder builder = new KewTypeBoBuilder("testType", "testNamespace").setServiceName("testService");
071            KewTypeBo kewTypeBo = builder.build();
072    
073            for (int i=1; i<=3; i++) {
074                KewAttributeDefinitionBo attributeDefn = new KewAttributeDefinitionBo();
075                attributeDefn.setName("attrDef"+i);
076                attributeDefn.setDescription("this is a description of attrDef" + i);
077                attributeDefn.setComponentName("componentName" + i);
078                attributeDefn.setLabel("label" + i);
079                attributeDefn.setNamespace(kewTypeBo.getNamespace());
080    
081                attributeDefn = dataObjectService.save(attributeDefn);
082    
083                KewTypeAttributeBo typeAttribute = new KewTypeAttributeBo();
084                typeAttribute.setSequenceNumber(i);
085                typeAttribute.setAttributeDefinition(attributeDefn);
086                kewTypeBo.getAttributes().add(typeAttribute);
087                typeAttribute.setType(kewTypeBo);
088            }
089    
090            dataObjectService.save(kewTypeBo);
091        }
092    
093        @Test
094        public void testPeopleFlowPersonMembers() {
095            PeopleFlowMemberBo peopleFlowMember = new PeopleFlowMemberBo();
096            peopleFlowMember.setMemberType(MemberType.PRINCIPAL);
097            peopleFlowMember.setMemberId("admin");
098            peopleFlowMember.setPriority(1);
099            peopleFlowMember.setResponsibilityId(responsibilityIdService.getNewResponsibilityId());
100            assertNotNull(peopleFlowMember.getPerson());
101            assertEquals("admin", peopleFlowMember.getPerson().getPrincipalName());
102    
103            PeopleFlowDelegateBo peopleFlowDelegate = new PeopleFlowDelegateBo();
104            peopleFlowDelegate.setMemberType(MemberType.PRINCIPAL);
105            peopleFlowDelegate.setMemberId("admin");
106            peopleFlowDelegate.setDelegationTypeCode(DelegationType.PRIMARY.getCode());
107            peopleFlowDelegate.setResponsibilityId(responsibilityIdService.getNewResponsibilityId());
108            assertNotNull(peopleFlowDelegate.getPerson());
109            assertEquals("admin", peopleFlowDelegate.getPerson().getPrincipalName());
110        }
111    
112        @Test
113        public void testPeopleFlowBoPersist() {
114            testKewTypeBoFullPersist();
115    
116            QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
117            criteria.setPredicates(equal("name", "testType"), equal("namespace", "testNamespace"));
118    
119            QueryResults<KewTypeBo> results = dataObjectService.findMatching(KewTypeBo.class, criteria.build());
120            assertEquals(1, results.getResults().size());
121            KewTypeBo kewTypeBo = results.getResults().get(0);
122    
123            // minimal peopleflow
124            PeopleFlowBo peopleFlowBo = new PeopleFlowBo();
125            peopleFlowBo.setDescription("description of testPeopleFlow");
126            peopleFlowBo.setName("testPeopleFlow");
127            peopleFlowBo.setNamespaceCode("testNamespace");
128            peopleFlowBo.setTypeId(kewTypeBo.getId());
129            
130            peopleFlowBo = dataObjectService.save(peopleFlowBo);
131    
132            // fill out peopleflow
133            KewTypeAttributeBo attribute = kewTypeBo.getAttributes().get(0);
134    
135            PeopleFlowAttributeBo peopleFlowAttr = new PeopleFlowAttributeBo();
136            peopleFlowAttr.setPeopleFlow(peopleFlowBo);
137            peopleFlowAttr.setAttributeDefinition(attribute.getAttributeDefinition());
138            peopleFlowAttr.setPeopleFlow(peopleFlowBo);
139            peopleFlowAttr.setValue("testAttrValue");
140    
141            peopleFlowBo.getAttributeBos().add(peopleFlowAttr);
142    
143            PeopleFlowMemberBo peopleFlowMember = new PeopleFlowMemberBo();
144            peopleFlowMember.setPeopleFlow(peopleFlowBo);
145            peopleFlowMember.setMemberType(MemberType.PRINCIPAL);
146            peopleFlowMember.setMemberId("admin");
147            peopleFlowMember.setPriority(1);
148            peopleFlowMember.setResponsibilityId(responsibilityIdService.getNewResponsibilityId());
149    
150            peopleFlowBo.getMembers().add(peopleFlowMember);
151    
152            PeopleFlowDelegateBo peopleFlowDelegate1 = new PeopleFlowDelegateBo();
153            peopleFlowDelegate1.setPeopleFlowMember(peopleFlowMember);
154            peopleFlowDelegate1.setMemberType(MemberType.GROUP);
155            peopleFlowDelegate1.setMemberId("1");
156            peopleFlowDelegate1.setDelegationTypeCode(DelegationType.PRIMARY.getCode());
157            peopleFlowDelegate1.setResponsibilityId(responsibilityIdService.getNewResponsibilityId());
158            peopleFlowMember.getDelegates().add(peopleFlowDelegate1);
159    
160            PeopleFlowDelegateBo peopleFlowDelegate2 = new PeopleFlowDelegateBo();
161            peopleFlowDelegate2.setPeopleFlowMember(peopleFlowMember);
162            peopleFlowDelegate2.setMemberType(MemberType.ROLE);
163            peopleFlowDelegate2.setMemberId("2");
164            peopleFlowDelegate2.setActionRequestPolicyCode(ActionRequestPolicy.FIRST.getCode());
165            peopleFlowDelegate2.setDelegationTypeCode(DelegationType.SECONDARY.getCode());
166            peopleFlowDelegate2.setResponsibilityId(responsibilityIdService.getNewResponsibilityId());
167            peopleFlowMember.getDelegates().add(peopleFlowDelegate2);
168    
169            peopleFlowBo = dataObjectService.save(peopleFlowBo);
170    
171            assertNotNull(peopleFlowBo.getId());
172            peopleFlowBo = dataObjectService.find(PeopleFlowBo.class, peopleFlowBo.getId());
173    
174            assertNotNull(peopleFlowBo);
175            assertNotNull(peopleFlowBo.getId());
176            assertTrue(peopleFlowBo.getMembers().size() == 1);
177    
178            PeopleFlowMemberBo memberBo = peopleFlowBo.getMembers().get(0);
179            assertNotNull(memberBo.getId());
180            assertEquals(peopleFlowBo.getId(), memberBo.getPeopleFlow().getId());
181            assertEquals("admin", memberBo.getMemberId());
182            assertEquals(MemberType.PRINCIPAL, memberBo.getMemberType());
183            assertNotNull(memberBo.getPerson());
184            assertEquals("admin", memberBo.getPerson().getPrincipalName());
185            assertEquals(peopleFlowMember.getResponsibilityId(), memberBo.getResponsibilityId());
186            assertSame(1, memberBo.getPriority());
187            assertTrue(memberBo.getDelegates().size() == 2);
188    
189            PeopleFlowDelegateBo delegateBo1 = memberBo.getDelegates().get(0);
190            assertNotNull(delegateBo1.getId());
191            assertEquals(memberBo.getId(), delegateBo1.getPeopleFlowMember().getId());
192            assertEquals("1", delegateBo1.getMemberId());
193            assertEquals(MemberType.GROUP, delegateBo1.getMemberType());
194            assertEquals(DelegationType.PRIMARY.getCode(), delegateBo1.getDelegationTypeCode());
195            assertEquals(peopleFlowDelegate1.getResponsibilityId(), delegateBo1.getResponsibilityId());
196            assertNull(delegateBo1.getActionRequestPolicyCode());
197    
198            PeopleFlowDelegateBo delegateBo2 = memberBo.getDelegates().get(1);
199            assertNotNull(delegateBo2.getId());
200            assertEquals(memberBo.getId(), delegateBo2.getPeopleFlowMember().getId());
201            assertEquals("2", delegateBo2.getMemberId());
202            assertEquals(MemberType.ROLE, delegateBo2.getMemberType());
203            assertEquals(DelegationType.SECONDARY.getCode(), delegateBo2.getDelegationTypeCode());
204            assertEquals(peopleFlowDelegate2.getResponsibilityId(), delegateBo2.getResponsibilityId());
205            assertEquals(ActionRequestPolicy.FIRST.getCode(), delegateBo2.getActionRequestPolicyCode());
206        }
207    
208        public static KewTypeBo buildMinimalKewTypeBo() {
209            KewTypeBo kewTypeBo = new KewTypeBo();
210            kewTypeBo.setName("TestType");
211            kewTypeBo.setNamespace("TestNamespace");
212            return kewTypeBo;
213        }
214    
215        private static class KewTypeBoBuilder {
216    
217            private boolean active = true;
218            private String name;
219            private String namespace;
220            private String serviceName;
221    
222            public KewTypeBoBuilder(String name, String namespace) {
223                this.name = name;
224                this.namespace = namespace;
225            }
226    
227            public KewTypeBoBuilder setServiceName(String serviceName) {
228                this.serviceName = serviceName;
229                return this;
230            }
231    
232            public KewTypeBoBuilder setName(String name) {
233                this.name = name;
234                return this;
235            }
236    
237            public KewTypeBoBuilder setNamespace(String namespace) {
238                this.namespace = namespace;
239                return this;
240            }
241    
242            public KewTypeBoBuilder setActive(boolean active) {
243                this.active = active;
244                return this;
245            }
246    
247            public KewTypeBo build() {
248                KewTypeBo kewTypeBo = new KewTypeBo();
249                kewTypeBo.setActive(active);
250                kewTypeBo.setName(name);
251                kewTypeBo.setNamespace(namespace);
252                kewTypeBo.setServiceName(serviceName);
253                return kewTypeBo;
254            }
255        }
256    
257    }