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 */
016package org.kuali.rice.kew.actions;
017
018import static org.junit.Assert.assertEquals;
019import static org.junit.Assert.assertFalse;
020import static org.junit.Assert.assertTrue;
021import mocks.MockEmailNotificationService;
022
023import org.junit.Ignore;
024import org.junit.Test;
025import org.kuali.rice.kew.actions.BlanketApproveTest.NotifySetup;
026import org.kuali.rice.kew.api.WorkflowDocument;
027import org.kuali.rice.kew.api.WorkflowDocumentFactory;
028import org.kuali.rice.kew.api.exception.WorkflowException;
029import org.kuali.rice.kew.service.KEWServiceLocator;
030import org.kuali.rice.kew.test.KEWTestCase;
031import org.kuali.rice.kew.test.TestUtilities;
032import org.kuali.rice.kew.api.KewApiConstants;
033
034public class DisapproveActionTest extends KEWTestCase {
035
036    protected void loadTestData() throws Exception {
037        loadXmlFile("ActionsConfig.xml");
038    }
039
040    @Test public void testDisapprove() throws Exception {
041        WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), NotifySetup.DOCUMENT_TYPE_NAME);
042        document.route("");
043
044        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("jhopf"), document.getDocumentId());
045        assertTrue("This user should have an approve request", document.isApprovalRequested());
046        document.approve("");
047
048        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
049        assertTrue("This user should have an approve request", document.isApprovalRequested());
050        document.approve("");//ewestfal had force action rule
051
052        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
053        assertTrue("This user should have an approve request", document.isApprovalRequested());
054        document.approve("");
055
056        //this be the role delegate of jitrue
057        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("natjohns"), document.getDocumentId());
058        assertTrue("This user should have an approve request", document.isApprovalRequested());
059        document.approve("");
060
061        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("bmcgough"), document.getDocumentId());
062        assertTrue("This user should have an approve request", document.isApprovalRequested());
063        // assert that the document is at the same node before and after disapprove
064        TestUtilities.assertAtNode(document, NotifySetup.NOTIFY_FINAL_NODE);
065        document.disapprove("");
066        TestUtilities.assertAtNode(document, NotifySetup.NOTIFY_FINAL_NODE);
067        // reload just to double check
068        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("bmcgough"), document.getDocumentId());
069        TestUtilities.assertAtNode(document, NotifySetup.NOTIFY_FINAL_NODE);
070
071        assertTrue("Document should be disapproved", document.isDisapproved());
072        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
073        assertTrue("ack should be requested as part of disapprove notification", document.isAcknowledgeRequested());
074
075        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("jhopf"), document.getDocumentId());
076        assertTrue("ack should be requested as part of disapprove notification", document.isAcknowledgeRequested());
077
078        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
079        assertTrue("ack should be requested as part of disapprove notification", document.isAcknowledgeRequested());
080
081        //jitrue while part of original approval chain did not take approve action and therefore should
082        //not get action
083        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("jitrue"), document.getDocumentId());
084        assertFalse("ack should be requested as part of disapprove notification", document.isAcknowledgeRequested());
085
086        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("natjohns"), document.getDocumentId());
087        assertTrue("ack should be requested as part of disapprove notification", document.isAcknowledgeRequested());
088
089        //shenl part of approval chain but didn't take action
090        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("shenl"), document.getDocumentId());
091        assertFalse("ack should be requested as part of disapprove notification", document.isAcknowledgeRequested());
092
093        //check that all the emailing went right.
094        assertEquals("jhopf should have been sent an approve email", 1, getMockEmailService().immediateReminderEmailsSent("jhopf", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
095        assertEquals("jhopf should have been sent an ack email", 1, getMockEmailService().immediateReminderEmailsSent("jhopf", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ));
096
097        assertEquals("ewestfal should have been sent an approve email", 1, getMockEmailService().immediateReminderEmailsSent("ewestfal", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
098        assertEquals("ewestfal should have been sent an ack email", 1, getMockEmailService().immediateReminderEmailsSent("ewestfal", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ));
099
100        //rkirkend is a primary delegate and therefore should not receive email notification
101        assertEquals("rkirkend should not have been sent an approve email", 0, getMockEmailService().immediateReminderEmailsSent("rkirkend", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
102        assertEquals("rkirkend should have been sent an ack email", 1, getMockEmailService().immediateReminderEmailsSent("rkirkend", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ));
103
104        //temay is rkirkend primary delegate she should have received notification
105        assertEquals("temay should have been sent an approve email", 1, getMockEmailService().immediateReminderEmailsSent("temay", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
106
107        //there should be no ack emails for temay
108        assertEquals("temay should have been sent an ack email", 0, getMockEmailService().immediateReminderEmailsSent("temay", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ));
109
110        // pmckown is a secondary delegate here so he should NOT have received a notification
111        assertEquals("pmckown should not have been sent an approve email", 0, getMockEmailService().immediateReminderEmailsSent("pmckown", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
112        assertEquals("pmckown should not have been sent an ack email", 0, getMockEmailService().immediateReminderEmailsSent("pmckown", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ));
113
114        //this is a secondary delegator and should receive notifications
115        assertEquals("jitrue should have been sent an approve email", 1, getMockEmailService().immediateReminderEmailsSent("jitrue", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
116        //no ack emails to jitrue
117        assertEquals("jitrue should have been sent an ack email", 0, getMockEmailService().immediateReminderEmailsSent("jitrue", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ));
118
119        //the 2nd delegates should NOT receive notifications by default
120        assertEquals("natjohns should not have been sent an approve email", 0, getMockEmailService().immediateReminderEmailsSent("natjohns", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
121        //2ndary delegate
122        assertEquals("natjohns should not have been sent an ack email", 1, getMockEmailService().immediateReminderEmailsSent("natjohns", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ));
123        assertEquals("shenl should not have been sent an approve email", 0, getMockEmailService().immediateReminderEmailsSent("shenl", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
124        assertEquals("shenl should not have been sent an ack email", 0, getMockEmailService().immediateReminderEmailsSent("shenl", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ));
125
126        assertEquals("bmcgough should have been sent an approve email", 1, getMockEmailService().immediateReminderEmailsSent("bmcgough", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_APPROVE_REQ));
127        assertEquals("bmcgough should not have been sent an ack email", 0, getMockEmailService().immediateReminderEmailsSent("bmcgough", document.getDocumentId(), KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ));
128    }
129
130    /**
131     * Tests whether the initator who disapproved a doc gets an acknowledgement
132     *
133     */
134    @Ignore("This test will fail until KULRICE-752 is resolved")
135    @Test public void testInitiatorRoleDisapprove() throws WorkflowException {
136        // test initiator disapproval of their own doc via InitiatorRoleAttribute
137        WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("arh14"), "InitiatorRoleApprovalTest");
138        document.route("routing document");
139
140        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("arh14"), document.getDocumentId());
141        document.disapprove("disapproving the document");
142
143        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("arh14"), document.getDocumentId());
144        assertFalse("Initiator should not have an Ack request from disapproval because they were the disapprover user", document.isAcknowledgeRequested());
145    }
146
147    /**
148     * Tests whether the initator who disapproved a doc gets an acknowledgement
149     * 
150     */
151    @Ignore("This test will fail until KULRICE-752 is resolved")
152    @Test public void testInitiatorDisapprove() throws WorkflowException {
153        // test initiator disapproval, via normal request with forceAction=true
154        WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), NotifySetup.DOCUMENT_TYPE_NAME);
155        document.route("");
156
157        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
158        document.disapprove("");
159
160        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
161        assertFalse("Initiator should not have an Ack request from disapproval because they were the disapprover user", document.isAcknowledgeRequested());
162    }
163
164    @Test public void testDisapproveByArbitraryRecipient() throws WorkflowException {
165        // test approval by some other person
166        WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "BlanketApproveSequentialTest");
167        document.route("");
168
169        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("bmcgough"), document.getDocumentId());
170        document.disapprove("disapproving as bmcgough");
171
172        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("bmcgough"), document.getDocumentId());
173        assertFalse("Acknowledge was incorrectly sent to non-initiator disapprover", document.isAcknowledgeRequested());
174
175        document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
176        assertTrue("Acknowledge was not sent to initiator", document.isAcknowledgeRequested());
177    }
178
179    private MockEmailNotificationService getMockEmailService() {
180        return (MockEmailNotificationService)KEWServiceLocator.getActionListEmailService();
181    }
182}