001    /**
002     * Copyright 2005-2011 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.peopleflow.PeopleFlowDefinition;
023    import org.kuali.rice.kew.api.peopleflow.PeopleFlowMember;
024    import org.kuali.rice.kew.api.peopleflow.PeopleFlowService;
025    import org.kuali.rice.kew.test.KEWTestCase;
026    import org.kuali.rice.kim.api.group.Group;
027    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
028    import org.kuali.rice.test.BaselineTestCase;
029    
030    import java.util.Iterator;
031    
032    import static org.junit.Assert.*;
033    
034    /**
035     * An integration test which tests the PeopleFlowService reference implementation.
036     *
037     * @author Kuali Rice Team (rice.collab@kuali.org)
038     */
039    @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.CLEAR_DB)
040    public class PeopleFlowServiceTest extends KEWTestCase {
041    
042        private static final String NAMESPACE_CODE = "MyNamespace";
043        private static final String NAME = "MyFlow";
044    
045        private static final String NAME2 = "MyFlowUpdate";
046        private static final String DESCRIPTION = "Description after update";
047    
048        private PeopleFlowService peopleFlowService;
049    
050        @Before
051        public void setupServiceUnderTest() {
052            setPeopleFlowService(KewApiServiceLocator.getPeopleFlowService());
053        }
054    
055        protected void setPeopleFlowService(PeopleFlowService peopleFlowService) {
056            this.peopleFlowService = peopleFlowService;
057        }
058    
059        protected PeopleFlowService getPeopleFlowService() {
060            return peopleFlowService;
061        }
062    
063        @Test
064        public void testCRUD() throws Exception {
065    
066            // create a flow with a principal and a group member
067            PeopleFlowDefinition.Builder builder = PeopleFlowDefinition.Builder.create(NAMESPACE_CODE, NAME);
068            PeopleFlowMember.Builder memberBuilder = PeopleFlowMember.Builder.create("admin", MemberType.PRINCIPAL);
069            memberBuilder.setPriority(1);
070            builder.getMembers().add(memberBuilder);
071            Group group = KimApiServiceLocator.getGroupService().getGroupByNameAndNamespaceCode("KR-WKFLW", "TestWorkgroup");
072            memberBuilder = PeopleFlowMember.Builder.create(group.getId(), MemberType.GROUP);
073            memberBuilder.setPriority(2);
074            builder.getMembers().add(memberBuilder);
075            
076            // now create it
077            PeopleFlowDefinition peopleFlow = getPeopleFlowService().createPeopleFlow(builder.build());
078            assertPeopleFlowCreate(peopleFlow, group);
079    
080            // load by id and check it's state after a fresh load
081            peopleFlow = getPeopleFlowService().getPeopleFlow(peopleFlow.getId());
082            assertPeopleFlowCreate(peopleFlow, group);
083    
084            // load by name and check it's state after a fresh load
085            peopleFlow = getPeopleFlowService().getPeopleFlowByName(NAMESPACE_CODE, NAME);
086            assertPeopleFlowCreate(peopleFlow, group);
087    
088            // try an update
089            builder = PeopleFlowDefinition.Builder.create(peopleFlow);
090            builder.setName(NAME2);
091            // create a new member and update an existing member, remove the group member first
092            for (Iterator<PeopleFlowMember.Builder> iterator = builder.getMembers().iterator(); iterator.hasNext();) {
093                PeopleFlowMember.Builder member = iterator.next();
094                if (member.getMemberType() == MemberType.GROUP) {
095                    iterator.remove();
096                }
097            }
098            // save the admin member for some checks later
099            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    }