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     */
016    package org.kuali.rice.kew.impl.peopleflow;
017    
018    import org.junit.Before;
019    import org.junit.Test;
020    import org.kuali.rice.core.api.membership.MemberType;
021    import org.kuali.rice.kew.api.KewApiServiceLocator;
022    import org.kuali.rice.kew.api.action.ActionRequestPolicy;
023    import org.kuali.rice.kew.api.peopleflow.PeopleFlowDefinition;
024    import org.kuali.rice.kew.api.peopleflow.PeopleFlowDelegate;
025    import org.kuali.rice.kew.api.peopleflow.PeopleFlowMember;
026    import org.kuali.rice.kew.api.peopleflow.PeopleFlowService;
027    import org.kuali.rice.kew.test.KEWTestCase;
028    import org.kuali.rice.kim.api.group.Group;
029    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
030    import org.kuali.rice.test.BaselineTestCase;
031    
032    import java.util.Arrays;
033    import java.util.Iterator;
034    
035    import static org.junit.Assert.*;
036    
037    /**
038     * An integration test which tests the PeopleFlowService reference implementation.
039     *
040     * @author Kuali Rice Team (rice.collab@kuali.org)
041     */
042    @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.CLEAR_DB)
043    public class PeopleFlowServiceTest extends KEWTestCase {
044    
045        private static final String NAMESPACE_CODE = "MyNamespace";
046        private static final String NAME = "MyFlow";
047    
048        private static final String NAME2 = "MyFlowUpdate";
049        private static final String DESCRIPTION = "Description after update";
050    
051        private PeopleFlowService peopleFlowService;
052    
053        @Before
054        public void setupServiceUnderTest() {
055            setPeopleFlowService(KewApiServiceLocator.getPeopleFlowService());
056        }
057    
058        protected void setPeopleFlowService(PeopleFlowService peopleFlowService) {
059            this.peopleFlowService = peopleFlowService;
060        }
061    
062        protected PeopleFlowService getPeopleFlowService() {
063            return peopleFlowService;
064        }
065    
066        @Test
067        public void testCRUD() throws Exception {
068    
069            // create a flow with a principal and a group member
070            PeopleFlowDefinition.Builder builder = PeopleFlowDefinition.Builder.create(NAMESPACE_CODE, NAME);
071            PeopleFlowMember.Builder memberBuilder = PeopleFlowMember.Builder.create("admin", MemberType.PRINCIPAL);
072            memberBuilder.setPriority(1);
073            builder.getMembers().add(memberBuilder);
074            Group group = KimApiServiceLocator.getGroupService().getGroupByNamespaceCodeAndName("KR-WKFLW", "TestWorkgroup");
075            memberBuilder = PeopleFlowMember.Builder.create(group.getId(), MemberType.GROUP);
076            memberBuilder.setPriority(2);
077            builder.getMembers().add(memberBuilder);
078            
079            // now create it
080            PeopleFlowDefinition peopleFlow = getPeopleFlowService().createPeopleFlow(builder.build());
081            assertPeopleFlowCreate(peopleFlow, group);
082    
083            // load by id and check it's state after a fresh load
084            peopleFlow = getPeopleFlowService().getPeopleFlow(peopleFlow.getId());
085            assertPeopleFlowCreate(peopleFlow, group);
086    
087            // load by name and check it's state after a fresh load
088            peopleFlow = getPeopleFlowService().getPeopleFlowByName(NAMESPACE_CODE, NAME);
089            assertPeopleFlowCreate(peopleFlow, group);
090    
091            // try an update
092            builder = PeopleFlowDefinition.Builder.create(peopleFlow);
093            builder.setName(NAME2);
094            // create a new member and update an existing member, remove the group member first
095            for (Iterator<PeopleFlowMember.Builder> iterator = builder.getMembers().iterator(); iterator.hasNext();) {
096                PeopleFlowMember.Builder member = iterator.next();
097                if (member.getMemberType() == MemberType.GROUP) {
098                    iterator.remove();
099                }
100            }
101            // save the admin member for some checks later
102            PeopleFlowMember.Builder adminMember = builder.getMembers().get(0);
103            assertEquals(1, builder.getMembers().size());
104            memberBuilder = PeopleFlowMember.Builder.create("ewestfal", MemberType.PRINCIPAL);
105            builder.getMembers().add(memberBuilder);
106            builder.setDescription(DESCRIPTION);
107    
108            // execute the update
109            PeopleFlowDefinition updatedPeopleFlow = getPeopleFlowService().updatePeopleFlow(builder.build());
110            updatedPeopleFlow = getPeopleFlowService().getPeopleFlow(updatedPeopleFlow.getId());
111            assertNotNull(updatedPeopleFlow);
112            assertEquals(NAME2, updatedPeopleFlow.getName());
113            assertEquals(DESCRIPTION, updatedPeopleFlow.getDescription());
114            assertEquals("Ids should be the same", peopleFlow.getId(), updatedPeopleFlow.getId());
115            assertEquals("Version number should be one higher", new Long(peopleFlow.getVersionNumber() + 1),
116                    updatedPeopleFlow.getVersionNumber());
117            assertEquals("Should have 2 members", 2, updatedPeopleFlow.getMembers().size());
118            
119            // now check the members
120            for (PeopleFlowMember member : updatedPeopleFlow.getMembers()) {
121                assertTrue("should not have any delegates", member.getDelegates().isEmpty());
122                assertEquals(MemberType.PRINCIPAL, member.getMemberType());
123                assertEquals(1, member.getPriority());
124                if (!(member.getMemberId().equals("admin") || member.getMemberId().equals("ewestfal"))) {
125                    fail("Encountered a member that shouldn't exist! " + member.getMemberId());
126                }
127            }
128    
129        }
130    
131        @Test
132        public void testMemberAndDelegateBuilder() throws Exception {
133    
134            // create a flow with a principal and a group member
135            PeopleFlowDefinition.Builder builder = PeopleFlowDefinition.Builder.create(NAMESPACE_CODE, NAME);
136            PeopleFlowMember.Builder memberBuilder = PeopleFlowMember.Builder.create("admin", MemberType.PRINCIPAL);
137            memberBuilder.setPriority(1);
138    
139            builder.getMembers().add(memberBuilder);
140    
141            Group group = KimApiServiceLocator.getGroupService().getGroupByNamespaceCodeAndName("KR-WKFLW", "TestWorkgroup");
142            memberBuilder = PeopleFlowMember.Builder.create(group.getId(), MemberType.GROUP);
143            memberBuilder.setPriority(2);
144    
145            builder.getMembers().add(memberBuilder);
146    
147            // now create it
148            PeopleFlowDefinition peopleFlow = getPeopleFlowService().createPeopleFlow(builder.build());
149            assertPeopleFlowCreate(peopleFlow, group);
150    
151            // load by id and check it's state after a fresh load
152            peopleFlow = getPeopleFlowService().getPeopleFlow(peopleFlow.getId());
153            assertPeopleFlowCreate(peopleFlow, group);
154    
155            // load by name and check it's state after a fresh load
156            peopleFlow = getPeopleFlowService().getPeopleFlowByName(NAMESPACE_CODE, NAME);
157            assertPeopleFlowCreate(peopleFlow, group);
158    
159            // try an update
160            builder = PeopleFlowDefinition.Builder.create(peopleFlow);
161            builder.setName(NAME2);
162            // create a new member and update an existing member, remove the group member first
163            for (Iterator<PeopleFlowMember.Builder> iterator = builder.getMembers().iterator(); iterator.hasNext();) {
164                PeopleFlowMember.Builder member = iterator.next();
165                if (member.getMemberType() == MemberType.GROUP) {
166                    iterator.remove();
167                }
168            }
169            // save the admin member for some checks later
170            PeopleFlowMember.Builder adminMember = builder.getMembers().get(0);
171            assertEquals(1, builder.getMembers().size());
172            memberBuilder = PeopleFlowMember.Builder.create("ewestfal", MemberType.PRINCIPAL);
173    
174            PeopleFlowDelegate.Builder testMember = PeopleFlowDelegate.Builder.create("test1",MemberType.PRINCIPAL);
175    
176            memberBuilder.setDelegates(Arrays.asList(testMember));
177            builder.getMembers().add(memberBuilder);
178            builder.setDescription(DESCRIPTION);
179    
180            // execute the update
181            PeopleFlowDefinition updatedPeopleFlow = getPeopleFlowService().updatePeopleFlow(builder.build());
182            updatedPeopleFlow = getPeopleFlowService().getPeopleFlow(updatedPeopleFlow.getId());
183            assertNotNull(updatedPeopleFlow);
184            assertEquals(NAME2, updatedPeopleFlow.getName());
185            assertEquals(DESCRIPTION, updatedPeopleFlow.getDescription());
186            assertEquals("Ids should be the same", peopleFlow.getId(), updatedPeopleFlow.getId());
187            assertEquals("Version number should be one higher", new Long(peopleFlow.getVersionNumber() + 1),
188                    updatedPeopleFlow.getVersionNumber());
189            assertEquals("Should have 2 members", 2, updatedPeopleFlow.getMembers().size());
190    
191            // now check the members
192            for (PeopleFlowMember member : updatedPeopleFlow.getMembers()) {
193                if (member.getMemberId().equals("ewestfal")) {
194                    assertEquals("should have one delegate",1, member.getDelegates().size());
195                } else {
196                    assertEquals("should not have delegates",0, member.getDelegates().size());
197                }
198    
199                assertEquals(MemberType.PRINCIPAL, member.getMemberType());
200                assertEquals(1, member.getPriority());
201                if (!(member.getMemberId().equals("admin") || member.getMemberId().equals("ewestfal"))) {
202                    fail("Encountered a member that shouldn't exist! " + member.getMemberId());
203                }
204            }
205    
206        }
207    
208        private void assertPeopleFlowCreate(PeopleFlowDefinition peopleFlow, Group groupMember) {
209            assertNotNull(peopleFlow);
210            assertNotNull(peopleFlow.getId());
211            assertEquals(2, peopleFlow.getMembers().size());
212            
213            for (PeopleFlowMember member : peopleFlow.getMembers()) {
214                assertTrue("should not have any delegates", member.getDelegates().isEmpty());
215                if (MemberType.PRINCIPAL == member.getMemberType()) {
216                    assertEquals(1, member.getPriority());
217                    assertEquals("admin", member.getMemberId());
218                } else if (MemberType.GROUP == member.getMemberType()) {
219                    assertEquals(2, member.getPriority());
220                    assertEquals(groupMember.getId(), member.getMemberId());
221                } else {
222                    fail("Invalid member found!");
223                }
224            }
225    
226            assertTrue("should have no attributes", peopleFlow.getAttributes().isEmpty());
227            assertNull("description should be null", peopleFlow.getDescription());
228            assertEquals(NAMESPACE_CODE, peopleFlow.getNamespaceCode());
229            assertEquals(NAME, peopleFlow.getName());
230            assertNull(peopleFlow.getTypeId());
231            assertTrue(peopleFlow.isActive());
232            assertNotNull("should have a non-null version number", peopleFlow.getVersionNumber());
233        }
234    
235    
236    }