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.Before;
19  import org.junit.Test;
20  import org.kuali.rice.core.api.membership.MemberType;
21  import org.kuali.rice.kew.api.KewApiServiceLocator;
22  import org.kuali.rice.kew.api.action.ActionRequestPolicy;
23  import org.kuali.rice.kew.api.peopleflow.PeopleFlowDefinition;
24  import org.kuali.rice.kew.api.peopleflow.PeopleFlowDelegate;
25  import org.kuali.rice.kew.api.peopleflow.PeopleFlowMember;
26  import org.kuali.rice.kew.api.peopleflow.PeopleFlowService;
27  import org.kuali.rice.kew.test.KEWTestCase;
28  import org.kuali.rice.kim.api.group.Group;
29  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
30  import org.kuali.rice.test.BaselineTestCase;
31  
32  import java.util.Arrays;
33  import java.util.Iterator;
34  
35  import static org.junit.Assert.*;
36  
37  /**
38   * An integration test which tests the PeopleFlowService reference implementation.
39   *
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   */
42  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.CLEAR_DB)
43  public class PeopleFlowServiceTest extends KEWTestCase {
44  
45      private static final String NAMESPACE_CODE = "MyNamespace";
46      private static final String NAME = "MyFlow";
47  
48      private static final String NAME2 = "MyFlowUpdate";
49      private static final String DESCRIPTION = "Description after update";
50  
51      private PeopleFlowService peopleFlowService;
52  
53      @Before
54      public void setupServiceUnderTest() {
55          setPeopleFlowService(KewApiServiceLocator.getPeopleFlowService());
56      }
57  
58      protected void setPeopleFlowService(PeopleFlowService peopleFlowService) {
59          this.peopleFlowService = peopleFlowService;
60      }
61  
62      protected PeopleFlowService getPeopleFlowService() {
63          return peopleFlowService;
64      }
65  
66      @Test
67      public void testCRUD() throws Exception {
68  
69          // create a flow with a principal and a group member
70          PeopleFlowDefinition.Builder builder = PeopleFlowDefinition.Builder.create(NAMESPACE_CODE, NAME);
71          PeopleFlowMember.Builder memberBuilder = PeopleFlowMember.Builder.create("admin", MemberType.PRINCIPAL);
72          memberBuilder.setPriority(1);
73          builder.getMembers().add(memberBuilder);
74          Group group = KimApiServiceLocator.getGroupService().getGroupByNamespaceCodeAndName("KR-WKFLW", "TestWorkgroup");
75          memberBuilder = PeopleFlowMember.Builder.create(group.getId(), MemberType.GROUP);
76          memberBuilder.setPriority(2);
77          builder.getMembers().add(memberBuilder);
78          
79          // now create it
80          PeopleFlowDefinition peopleFlow = getPeopleFlowService().createPeopleFlow(builder.build());
81          assertPeopleFlowCreate(peopleFlow, group);
82  
83          // load by id and check it's state after a fresh load
84          peopleFlow = getPeopleFlowService().getPeopleFlow(peopleFlow.getId());
85          assertPeopleFlowCreate(peopleFlow, group);
86  
87          // load by name and check it's state after a fresh load
88          peopleFlow = getPeopleFlowService().getPeopleFlowByName(NAMESPACE_CODE, NAME);
89          assertPeopleFlowCreate(peopleFlow, group);
90  
91          // try an update
92          builder = PeopleFlowDefinition.Builder.create(peopleFlow);
93          builder.setName(NAME2);
94          // create a new member and update an existing member, remove the group member first
95          for (Iterator<PeopleFlowMember.Builder> iterator = builder.getMembers().iterator(); iterator.hasNext();) {
96              PeopleFlowMember.Builder member = iterator.next();
97              if (member.getMemberType() == MemberType.GROUP) {
98                  iterator.remove();
99              }
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 }