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.actions;
017    
018    import static org.junit.Assert.assertEquals;
019    import static org.junit.Assert.assertFalse;
020    import static org.junit.Assert.assertTrue;
021    
022    import java.util.List;
023    
024    import org.junit.Test;
025    import org.kuali.rice.kew.actionrequest.ActionRequestValue;
026    import org.kuali.rice.kew.actions.BlanketApproveTest.NotifySetup;
027    import org.kuali.rice.kew.api.WorkflowDocument;
028    import org.kuali.rice.kew.api.WorkflowDocumentFactory;
029    import org.kuali.rice.kew.api.action.ActionRequestType;
030    import org.kuali.rice.kew.api.action.ActionType;
031    import org.kuali.rice.kew.api.document.DocumentStatus;
032    import org.kuali.rice.kew.service.KEWServiceLocator;
033    import org.kuali.rice.kew.test.KEWTestCase;
034    import org.kuali.rice.kew.api.KewApiConstants;
035    
036    /**
037     * Tests the super user actions available on the API.
038     */
039    public class SuperUserActionRequestApproveEventTest extends KEWTestCase {
040    
041        protected void loadTestData() throws Exception {
042            loadXmlFile("ActionsConfig.xml");
043        }
044    
045        @Test public void testSuperUserActionsOnEnroute() throws Exception {
046            WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), NotifySetup.DOCUMENT_TYPE_NAME);
047            document.adHocToPrincipal(ActionRequestType.FYI, "", getPrincipalIdForName("rkirkend"), "", true);
048            document.adHocToPrincipal(ActionRequestType.APPROVE, "", getPrincipalIdForName("jhopf"), "", true);
049            document.route("");
050    
051            document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
052            assertTrue("rkirkend should have an FYI request.", document.isFYIRequested());
053    
054            String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
055            List<ActionRequestValue> actionRequests = KEWServiceLocator.getActionRequestService().findAllValidRequests(rkirkendPrincipalId, document.getDocumentId(), KewApiConstants.ACTION_REQUEST_FYI_REQ);
056            assertEquals("There should only be 1 fyi request to rkirkend.", 1, actionRequests.size());
057            document = WorkflowDocumentFactory.loadDocument(rkirkendPrincipalId, document.getDocumentId());
058            document.superUserTakeRequestedAction(actionRequests.get(0).getActionRequestId().toString(), "");
059    
060            // FYI should no longer be requested
061            document = WorkflowDocumentFactory.loadDocument(rkirkendPrincipalId, document.getDocumentId());
062            assertFalse("rkirkend should no longer have an FYI request.", document.isFYIRequested());
063    
064            // doc should still be enroute
065            assertTrue("Document should still be ENROUTE", document.isEnroute());
066    
067        }
068    
069        @Test public void testSuperUserActionsOnFinal() throws Exception {
070            WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "SuperUserApproveActionRequestFyiTest");
071            document.adHocToPrincipal(ActionRequestType.FYI, "", getPrincipalIdForName("rkirkend"), "", true);
072            document.route("");
073    
074            // doc should still be final
075            assertEquals("Document should be FINAL", DocumentStatus.FINAL, document.getStatus());
076    
077            document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
078            assertTrue("rkirkend should have an FYI request.", document.isFYIRequested());
079    
080            String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
081            List<ActionRequestValue> actionRequests = KEWServiceLocator.getActionRequestService().findAllValidRequests(rkirkendPrincipalId, document.getDocumentId(), KewApiConstants.ACTION_REQUEST_FYI_REQ);
082            assertEquals("There should only be 1 fyi request to rkirkend.", 1, actionRequests.size());
083            document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
084            document.superUserTakeRequestedAction(actionRequests.get(0).getActionRequestId().toString(), "");
085    
086            // FYI should no longer be requested
087            document = WorkflowDocumentFactory.loadDocument(rkirkendPrincipalId, document.getDocumentId());
088            assertFalse("rkirkend should no longer have an FYI request.", document.isFYIRequested());
089        }
090        
091        @Test public void testSuperUserActionsOnProcessed() throws Exception {
092            WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "SuperUserApproveActionRequestFyiTest");
093            document.adHocToPrincipal(ActionRequestType.ACKNOWLEDGE, "", getPrincipalIdForName("jhopf"), "", true);
094            document.adHocToPrincipal(ActionRequestType.FYI, "", getPrincipalIdForName("rkirkend"), "", true);
095            document.route("");
096    
097            // doc should still be processed
098            assertEquals("Document should be PROCESSED", DocumentStatus.PROCESSED, document.getStatus());
099    
100            document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
101            assertTrue("rkirkend should have an FYI request.", document.isFYIRequested());
102    
103            String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
104            List<ActionRequestValue> fyiActionRequests = KEWServiceLocator.getActionRequestService().findAllValidRequests(rkirkendPrincipalId, document.getDocumentId(), KewApiConstants.ACTION_REQUEST_FYI_REQ);
105            assertEquals("There should only be 1 fyi request to rkirkend.", 1, fyiActionRequests.size());
106            document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
107            document.superUserTakeRequestedAction(fyiActionRequests.get(0).getActionRequestId().toString(), "");
108    
109            // FYI should no longer be requested
110            document = WorkflowDocumentFactory.loadDocument(rkirkendPrincipalId, document.getDocumentId());
111            assertFalse("rkirkend should no longer have an FYI request.", document.isFYIRequested());
112    
113            // doc should still be processed
114            assertEquals("Document should be PROCESSED", DocumentStatus.PROCESSED, document.getStatus());
115    
116            String jhopfPrincipalId = getPrincipalIdForName("jhopf");
117            List<ActionRequestValue> ackActionRequests = KEWServiceLocator.getActionRequestService().findAllValidRequests(jhopfPrincipalId, document.getDocumentId(), KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ);
118            assertEquals("There should only be 1 ACK request to jhopf.", 1, ackActionRequests.size());
119            document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
120            document.superUserTakeRequestedAction(ackActionRequests.get(0).getActionRequestId().toString(), "");
121    
122            // ACK should no longer be requested
123            document = WorkflowDocumentFactory.loadDocument(jhopfPrincipalId, document.getDocumentId());
124            assertFalse("jhopf should no longer have an ACK request.", document.isAcknowledgeRequested());
125    
126            // doc should be final
127            assertEquals("Document should be FINAL", DocumentStatus.FINAL, document.getStatus());
128        }
129        
130    
131        @Test public void testSuperUserActionRoutesDocumentToEnroute() throws Exception {
132            String documentId = testSuperUserActionRoutesDocument("SuperUserApproveActionRequestApproveTest");
133            WorkflowDocument document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), documentId);
134            // doc should now be enroute
135            assertEquals("Document should be ENROUTE", DocumentStatus.ENROUTE, document.getStatus());
136        }
137    
138        @Test public void testSuperUserActionRoutesDocumentToFinal() throws Exception {
139            String documentId = testSuperUserActionRoutesDocument("SuperUserApproveActionRequestFyiTest");
140            WorkflowDocument document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), documentId);
141            // doc should now be enroute
142            assertEquals("Document should be FINAL", DocumentStatus.FINAL, document.getStatus());
143        }
144    
145        private String testSuperUserActionRoutesDocument(String documentType) throws Exception {
146            String ewestfalPrincipalId = getPrincipalIdForName("ewestfal");
147            WorkflowDocument document = WorkflowDocumentFactory.createDocument(ewestfalPrincipalId, documentType);
148            document.saveDocument("");
149            // doc should saved
150            assertEquals("Document should be SAVED", DocumentStatus.SAVED, document.getStatus());
151    
152            document = WorkflowDocumentFactory.loadDocument(ewestfalPrincipalId, document.getDocumentId());
153            assertTrue("ewestfal should have Complete request", document.isCompletionRequested());
154    
155            document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
156            assertFalse("rkirkend should not have Complete request", document.isCompletionRequested());
157            assertFalse("rkirkend should not have Approve request", document.isApprovalRequested());
158            assertTrue("rkirkend should be a super user of the document", document.isValidAction(ActionType.SU_APPROVE));
159            
160            List<ActionRequestValue> actionRequests = KEWServiceLocator.getActionRequestService().findAllValidRequests(ewestfalPrincipalId, document.getDocumentId(), KewApiConstants.ACTION_REQUEST_COMPLETE_REQ);
161            assertEquals("There should only be 1 complete request to ewestfal as result of the save.", 1, actionRequests.size());
162            document.superUserTakeRequestedAction(actionRequests.get(0).getActionRequestId().toString(), "");
163    
164            // Complete should no longer be requested
165            document = WorkflowDocumentFactory.loadDocument(ewestfalPrincipalId, document.getDocumentId());
166            assertFalse("ewestfal should not have Complete request", document.isCompletionRequested());
167    
168            return document.getDocumentId();
169        }
170    
171        @Test public void testSavedDocumentSuperUserAdhocActionsApprove() throws Exception {
172            String initiatorNetworkId = "ewestfal";
173            WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName(initiatorNetworkId), "SuperUserApproveActionRequestFyiTest");
174            String adhocActionUserNetworkId = "jhopf";
175            document.adHocToPrincipal(ActionRequestType.APPROVE, "", getPrincipalIdForName(adhocActionUserNetworkId), "", true);
176            document.saveDocument("");
177            // doc should be saved
178            assertEquals("Document should be SAVED", DocumentStatus.SAVED, document.getStatus());
179    
180            document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
181            assertTrue("ewestfal should have Complete request", document.isCompletionRequested());
182    
183            document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
184            assertFalse("rkirkend should not have Complete request", document.isCompletionRequested());
185            assertFalse("rkirkend should not have Approve request", document.isApprovalRequested());
186            assertTrue("rkirkend should be a super user of the document", document.isValidAction(ActionType.SU_APPROVE));
187            String adhocPrincipalId = getPrincipalIdForName(adhocActionUserNetworkId);
188            List<ActionRequestValue> actionRequests = KEWServiceLocator.getActionRequestService().findAllValidRequests(adhocPrincipalId, document.getDocumentId(), ActionRequestType.APPROVE.getCode());
189            assertEquals("There should only be 1 approve request to " + adhocActionUserNetworkId + ".", 1, actionRequests.size());
190            document.superUserTakeRequestedAction(actionRequests.get(0).getActionRequestId().toString(), "");
191    
192            // approve should no longer be requested
193            document = WorkflowDocumentFactory.loadDocument(adhocPrincipalId, document.getDocumentId());
194            assertFalse(adhocPrincipalId + " should not have approve request", document.isApprovalRequested());
195    
196            // complete should no longer be requested
197            document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName(initiatorNetworkId), document.getDocumentId());
198            assertTrue(initiatorNetworkId + " should not have complete request", document.isCompletionRequested());
199    
200            // doc should still be saved
201            assertEquals("Document should be SAVED", DocumentStatus.SAVED, document.getStatus());
202        }
203    
204    }