View Javadoc

1   /**
2    * Copyright 2005-2011 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.peopleflow.PeopleFlowDefinition;
23  import org.kuali.rice.kew.api.peopleflow.PeopleFlowMember;
24  import org.kuali.rice.kew.api.peopleflow.PeopleFlowService;
25  import org.kuali.rice.kew.test.KEWTestCase;
26  import org.kuali.rice.kim.api.group.Group;
27  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
28  import org.kuali.rice.test.BaselineTestCase;
29  
30  import java.util.Iterator;
31  
32  import static org.junit.Assert.*;
33  
34  /**
35   * An integration test which tests the PeopleFlowService reference implementation.
36   *
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   */
39  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.CLEAR_DB)
40  public class PeopleFlowServiceTest extends KEWTestCase {
41  
42      private static final String NAMESPACE_CODE = "MyNamespace";
43      private static final String NAME = "MyFlow";
44  
45      private static final String NAME2 = "MyFlowUpdate";
46      private static final String DESCRIPTION = "Description after update";
47  
48      private PeopleFlowService peopleFlowService;
49  
50      @Before
51      public void setupServiceUnderTest() {
52          setPeopleFlowService(KewApiServiceLocator.getPeopleFlowService());
53      }
54  
55      protected void setPeopleFlowService(PeopleFlowService peopleFlowService) {
56          this.peopleFlowService = peopleFlowService;
57      }
58  
59      protected PeopleFlowService getPeopleFlowService() {
60          return peopleFlowService;
61      }
62  
63      @Test
64      public void testCRUD() throws Exception {
65  
66          // create a flow with a principal and a group member
67          PeopleFlowDefinition.Builder builder = PeopleFlowDefinition.Builder.create(NAMESPACE_CODE, NAME);
68          PeopleFlowMember.Builder memberBuilder = PeopleFlowMember.Builder.create("admin", MemberType.PRINCIPAL);
69          memberBuilder.setPriority(1);
70          builder.getMembers().add(memberBuilder);
71          Group group = KimApiServiceLocator.getGroupService().getGroupByNamespaceCodeAndName("KR-WKFLW", "TestWorkgroup");
72          memberBuilder = PeopleFlowMember.Builder.create(group.getId(), MemberType.GROUP);
73          memberBuilder.setPriority(2);
74          builder.getMembers().add(memberBuilder);
75          
76          // now create it
77          PeopleFlowDefinition peopleFlow = getPeopleFlowService().createPeopleFlow(builder.build());
78          assertPeopleFlowCreate(peopleFlow, group);
79  
80          // load by id and check it's state after a fresh load
81          peopleFlow = getPeopleFlowService().getPeopleFlow(peopleFlow.getId());
82          assertPeopleFlowCreate(peopleFlow, group);
83  
84          // load by name and check it's state after a fresh load
85          peopleFlow = getPeopleFlowService().getPeopleFlowByName(NAMESPACE_CODE, NAME);
86          assertPeopleFlowCreate(peopleFlow, group);
87  
88          // try an update
89          builder = PeopleFlowDefinition.Builder.create(peopleFlow);
90          builder.setName(NAME2);
91          // create a new member and update an existing member, remove the group member first
92          for (Iterator<PeopleFlowMember.Builder> iterator = builder.getMembers().iterator(); iterator.hasNext();) {
93              PeopleFlowMember.Builder member = iterator.next();
94              if (member.getMemberType() == MemberType.GROUP) {
95                  iterator.remove();
96              }
97          }
98          // save the admin member for some checks later
99          PeopleFlowMember.Builder adminMember = builder.getMembers().get(0);
100         assertEquals(1, builder.getMembers().size());
101         memberBuilder = PeopleFlowMember.Builder.create("ewestfal", MemberType.PRINCIPAL);
102         builder.getMembers().add(memberBuilder);
103         builder.setDescription(DESCRIPTION);
104 
105         // execute the update
106         PeopleFlowDefinition updatedPeopleFlow = getPeopleFlowService().updatePeopleFlow(builder.build());
107         updatedPeopleFlow = getPeopleFlowService().getPeopleFlow(updatedPeopleFlow.getId());
108         assertNotNull(updatedPeopleFlow);
109         assertEquals(NAME2, updatedPeopleFlow.getName());
110         assertEquals(DESCRIPTION, updatedPeopleFlow.getDescription());
111         assertEquals("Ids should be the same", peopleFlow.getId(), updatedPeopleFlow.getId());
112         assertEquals("Version number should be one higher", new Long(peopleFlow.getVersionNumber() + 1),
113                 updatedPeopleFlow.getVersionNumber());
114         assertEquals("Should have 2 members", 2, updatedPeopleFlow.getMembers().size());
115         
116         // now check the members
117         for (PeopleFlowMember member : updatedPeopleFlow.getMembers()) {
118             assertTrue("should not have any delegates", member.getDelegates().isEmpty());
119             assertEquals(MemberType.PRINCIPAL, member.getMemberType());
120             assertEquals(1, member.getPriority());
121             if (!(member.getMemberId().equals("admin") || member.getMemberId().equals("ewestfal"))) {
122                 fail("Encountered a member that shouldn't exist! " + member.getMemberId());
123             }
124         }
125 
126     }
127 
128     private void assertPeopleFlowCreate(PeopleFlowDefinition peopleFlow, Group groupMember) {
129         assertNotNull(peopleFlow);
130         assertNotNull(peopleFlow.getId());
131         assertEquals(2, peopleFlow.getMembers().size());
132         
133         for (PeopleFlowMember member : peopleFlow.getMembers()) {
134             assertTrue("should not have any delegates", member.getDelegates().isEmpty());
135             if (MemberType.PRINCIPAL == member.getMemberType()) {
136                 assertEquals(1, member.getPriority());
137                 assertEquals("admin", member.getMemberId());
138             } else if (MemberType.GROUP == member.getMemberType()) {
139                 assertEquals(2, member.getPriority());
140                 assertEquals(groupMember.getId(), member.getMemberId());
141             } else {
142                 fail("Invalid member found!");
143             }
144         }
145 
146         assertTrue("should have no attributes", peopleFlow.getAttributes().isEmpty());
147         assertNull("description should be null", peopleFlow.getDescription());
148         assertEquals(NAMESPACE_CODE, peopleFlow.getNamespaceCode());
149         assertEquals(NAME, peopleFlow.getName());
150         assertNull(peopleFlow.getTypeId());
151         assertTrue(peopleFlow.isActive());
152         assertNotNull("should have a non-null version number", peopleFlow.getVersionNumber());
153     }
154 
155 
156 }