001    /*
002     * Copyright 2006-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    
017    package org.kuali.rice.kew.actionlist;
018    
019    import static org.junit.Assert.assertEquals;
020    import static org.junit.Assert.assertFalse;
021    import static org.junit.Assert.assertTrue;
022    
023    import java.util.ArrayList;
024    import java.util.Collection;
025    import java.util.List;
026    
027    import org.junit.Test;
028    import org.kuali.rice.kew.actionitem.ActionItem;
029    import org.kuali.rice.kew.actions.asyncservices.ActionInvocation;
030    import org.kuali.rice.kew.api.WorkflowDocument;
031    import org.kuali.rice.kew.api.WorkflowDocumentFactory;
032    import org.kuali.rice.kew.api.action.ActionRequestType;
033    import org.kuali.rice.kew.preferences.Preferences;
034    import org.kuali.rice.kew.preferences.service.impl.PreferencesServiceImpl;
035    import org.kuali.rice.kew.rule.TestRuleAttribute;
036    import org.kuali.rice.kew.service.KEWServiceLocator;
037    import org.kuali.rice.kew.test.KEWTestCase;
038    import org.kuali.rice.kew.util.KEWConstants;
039    import org.kuali.rice.test.BaselineTestCase;
040    import org.springframework.transaction.TransactionStatus;
041    import org.springframework.transaction.support.TransactionCallback;
042    import org.springframework.transaction.support.TransactionTemplate;
043    
044    /**
045     * Tests Outbox functionality
046     *
047     * @author Kuali Rice Team (rice.collab@kuali.org)
048     */
049    @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
050    public class OutboxTest extends KEWTestCase {
051    
052        protected void loadTestData() throws Exception {
053            loadXmlFile("OutboxTestConfig.xml");
054        }
055    
056        private void turnOnOutboxForUser(final String principalId) {
057            new TransactionTemplate(KEWServiceLocator.getPlatformTransactionManager()).execute(new TransactionCallback() {
058                public Object doInTransaction(TransactionStatus status) {
059                    KEWServiceLocator.getUserOptionsService().save(principalId, PreferencesServiceImpl.USE_OUT_BOX, KEWConstants.PREFERENCES_YES_VAL);
060                    return null;
061                }
062            });
063        }
064    
065        @Test
066        public void testOutboxItemNotSavedOnSavedDocumentStatus() throws Exception {
067            final String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
068            List<String> recipients = new ArrayList<String>();
069            recipients.add(rkirkendPrincipalId);
070            TestRuleAttribute.setRecipientPrincipalIds("TestRole", "qualRole", recipients);
071    
072            WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("quickstart"), "TestDocumentType");
073            document.route("");
074    
075            document = WorkflowDocumentFactory.loadDocument(rkirkendPrincipalId, document.getDocumentId());
076            assertTrue("approve should be requested", document.isApprovalRequested());
077    
078            turnOnOutboxForUser(rkirkendPrincipalId);
079    
080            document.saveDocument("");
081    
082            Collection<ActionItem> outbox = KEWServiceLocator.getActionListService().getOutbox(rkirkendPrincipalId, new ActionListFilter());
083            assertEquals("there should not be any outbox items", 0, outbox.size());
084        }
085    
086        @Test
087        public void testTakeActionCreatesOutboxItem() throws Exception {
088    
089            final String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
090            List<String> recipients = new ArrayList<String>();
091            recipients.add(rkirkendPrincipalId);
092            TestRuleAttribute.setRecipientPrincipalIds("TestRole", "qualRole", recipients);
093    
094            WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("quickstart"), "TestDocumentType");
095            document.route("");
096    
097            document = WorkflowDocumentFactory.loadDocument(rkirkendPrincipalId, document.getDocumentId());
098            assertTrue("approve should be requested", document.isApprovalRequested());
099    
100            turnOnOutboxForUser(rkirkendPrincipalId);
101    
102            document.approve("");
103    
104            Collection<ActionItem> outbox = KEWServiceLocator.getActionListService().getOutbox(rkirkendPrincipalId, new ActionListFilter());
105            assertEquals("there should be an outbox item", 1, outbox.size());
106        }
107    
108        @Test
109        public void testSingleOutboxItemPerDocument() throws Exception {
110            final String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
111            final String user1PrincipalId = getPrincipalIdForName("user1");
112            List<String> recipients = new ArrayList<String>();
113            recipients.add(rkirkendPrincipalId);
114            recipients.add(user1PrincipalId);
115            TestRuleAttribute.setRecipientPrincipalIds("TestRole", "qualRole", recipients);
116    
117            WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("quickstart"), "TestDocumentType");
118            document.route("");
119    
120            document = WorkflowDocumentFactory.loadDocument(rkirkendPrincipalId, document.getDocumentId());
121            assertTrue("approve should be requested", document.isApprovalRequested());
122    
123            turnOnOutboxForUser(rkirkendPrincipalId);
124    
125            document.adHocToPrincipal(ActionRequestType.APPROVE, "", user1PrincipalId, "", true);
126    
127            document.approve("");
128    
129            Collection<ActionItem> outbox = KEWServiceLocator.getActionListService().getOutbox(rkirkendPrincipalId, new ActionListFilter());
130            assertEquals("there should be an outbox item", 1, outbox.size());
131    
132            document = WorkflowDocumentFactory.loadDocument(user1PrincipalId, document.getDocumentId());
133            assertTrue("approve should be requested", document.isApprovalRequested());
134    
135            document.adHocToPrincipal(ActionRequestType.APPROVE, "", rkirkendPrincipalId, "", true);
136    
137            document = WorkflowDocumentFactory.loadDocument(rkirkendPrincipalId, document.getDocumentId());
138            assertTrue("approve should be requested", document.isApprovalRequested());
139            document.approve("");
140    
141            outbox = KEWServiceLocator.getActionListService().getOutbox(rkirkendPrincipalId, new ActionListFilter());
142            assertEquals("there should be an outbox item", 1, outbox.size());
143        }
144    
145        @Test
146        public void testOnlyPersonWhoTookActionReceivesOutboxItem_Route() throws Exception {
147            final String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
148            final String user1PrincipalId = getPrincipalIdForName("user1");
149            List<String> recipients = new ArrayList<String>();
150            recipients.add(getPrincipalIdForName("rkirkend"));
151            recipients.add(getPrincipalIdForName("user1"));
152            TestRuleAttribute.setRecipientPrincipalIds("TestRole", "qualRole", recipients);
153    
154            turnOnOutboxForUser(rkirkendPrincipalId);
155            turnOnOutboxForUser(user1PrincipalId);
156    
157            WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("quickstart"), "TestDocumentType");
158            document.route("");
159    
160            // verify test is sane and users have action items
161            assertFalse(KEWServiceLocator.getActionListService().getActionList(rkirkendPrincipalId, new ActionListFilter()).isEmpty());
162            assertFalse(KEWServiceLocator.getActionListService().getActionList(user1PrincipalId, new ActionListFilter()).isEmpty());
163    
164            document = WorkflowDocumentFactory.loadDocument(user1PrincipalId, document.getDocumentId());
165            document.approve("");
166            // verify only user who took action has the outbox item
167            assertTrue(KEWServiceLocator.getActionListService().getOutbox(rkirkendPrincipalId, new ActionListFilter()).isEmpty());
168            assertEquals(1, KEWServiceLocator.getActionListService().getOutbox(user1PrincipalId, new ActionListFilter()).size());
169        }
170    
171        @Test
172        public void testOnlyPersonWhoTookActionReceivesOutboxItem_BlanketApprove() throws Exception {
173            final String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
174            final String user1PrincipalId = getPrincipalIdForName("user1");
175            List<String> recipients = new ArrayList<String>();
176            recipients.add(rkirkendPrincipalId);
177            recipients.add(user1PrincipalId);
178            TestRuleAttribute.setRecipientPrincipalIds("TestRole", "qualRole", recipients);
179    
180            turnOnOutboxForUser(rkirkendPrincipalId);
181            turnOnOutboxForUser(user1PrincipalId);
182    
183            WorkflowDocument document = WorkflowDocumentFactory.createDocument(rkirkendPrincipalId, "TestDocumentType");
184            document.blanketApprove("");
185            // verify only user who took action has the outbox item
186            assertEquals("Wrong number of outbox items found for rkirkend", 0, KEWServiceLocator.getActionListService().getOutbox(rkirkendPrincipalId, new ActionListFilter()).size());
187            assertEquals("Wrong number of outbox items found for user1", 0, KEWServiceLocator.getActionListService().getOutbox(user1PrincipalId, new ActionListFilter()).size());
188    
189            document = WorkflowDocumentFactory.createDocument(rkirkendPrincipalId, "TestDocumentType");
190            document.saveDocument("");
191            // verify test is sane and users have action items
192            assertEquals("Wrong number of action items found for rkirkend", 1, KEWServiceLocator.getActionListService().getActionList(rkirkendPrincipalId, new ActionListFilter()).size());
193            // verify that outboxes of two users are clear
194            assertEquals("Wrong number of outbox items found for rkirkend", 0, KEWServiceLocator.getActionListService().getOutbox(rkirkendPrincipalId, new ActionListFilter()).size());
195            assertEquals("Wrong number of outbox items found for user1", 0, KEWServiceLocator.getActionListService().getOutbox(user1PrincipalId, new ActionListFilter()).size());
196    
197            document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
198            document.blanketApprove("");
199            // verify only user who took action has the outbox item
200            assertEquals("Wrong number of outbox items found for rkirkend", 1, KEWServiceLocator.getActionListService().getOutbox(rkirkendPrincipalId, new ActionListFilter()).size());
201            assertEquals("Wrong number of outbox items found for user1", 0, KEWServiceLocator.getActionListService().getOutbox(user1PrincipalId, new ActionListFilter()).size());
202        }
203    
204        @Test
205        public void testOnlyPersonWhoTookActionReceivesOutboxItem_Workgroup() throws Exception {
206            final String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
207            final String user1PrincipalId = getPrincipalIdForName("user1");
208            final String ewestfalPrincipalId = getPrincipalIdForName("ewestfal");
209    
210            turnOnOutboxForUser(rkirkendPrincipalId);
211            turnOnOutboxForUser(user1PrincipalId);
212    
213            WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("user2"), "OutboxTestDocumentType");
214            document.route("");
215            // verify action items exist
216            assertEquals("Wrong number of action items found for rkirkend", 1, KEWServiceLocator.getActionListService().getActionList(rkirkendPrincipalId, new ActionListFilter()).size());
217            assertEquals("Wrong number of action items found for ewestfal", 1, KEWServiceLocator.getActionListService().getActionList(ewestfalPrincipalId, new ActionListFilter()).size());
218            assertEquals("Wrong number of action items found for user1", 1, KEWServiceLocator.getActionListService().getActionList(user1PrincipalId, new ActionListFilter()).size());
219            // verify outboxes are clear
220            assertEquals("Wrong number of outbox items found for rkirkend", 0, KEWServiceLocator.getActionListService().getOutbox(rkirkendPrincipalId, new ActionListFilter()).size());
221            assertEquals("Wrong number of outbox items found for ewestfal", 0, KEWServiceLocator.getActionListService().getOutbox(ewestfalPrincipalId, new ActionListFilter()).size());
222            assertEquals("Wrong number of outbox items found for user1", 0, KEWServiceLocator.getActionListService().getOutbox(user1PrincipalId, new ActionListFilter()).size());
223    
224            document = WorkflowDocumentFactory.loadDocument(rkirkendPrincipalId, document.getDocumentId());
225            document.approve("");
226            // verify only user who took action has the outbox item
227            assertEquals("Wrong number of outbox items found for rkirkend", 1, KEWServiceLocator.getActionListService().getOutbox(rkirkendPrincipalId, new ActionListFilter()).size());
228            assertEquals("Wrong number of outbox items found for ewestfal", 0, KEWServiceLocator.getActionListService().getOutbox(ewestfalPrincipalId, new ActionListFilter()).size());
229            assertEquals("Wrong number of outbox items found for user1", 0, KEWServiceLocator.getActionListService().getOutbox(user1PrincipalId, new ActionListFilter()).size());
230        }
231    
232        @Test
233        public void testOutBoxDefaultPreferenceOnConfigParam() throws Exception {
234            final String user1PrincipalId = getPrincipalIdForName("user1");
235            Preferences prefs = KEWServiceLocator.getPreferencesService().getPreferences(user1PrincipalId);
236            assertTrue("By default the user's pref should be outbox on", prefs.isUsingOutbox());
237    
238            //TODO: this can no longer be set on the fly and grabbed through the preference service (default values are set at startup)
239            //TODO: this is a prime candidate for a mocking tool
240            /*ConfigContext.getCurrentContextConfig().putProperty(KEWConstants.USER_OPTIONS_DEFAULT_USE_OUTBOX_PARAM, "false");
241            final String natjohnsPrincipalId = getPrincipalIdForName("natjohns");
242            prefs = KEWServiceLocator.getPreferencesService().getPreferences(natjohnsPrincipalId);
243            assertFalse("The user's pref should be outbox off", prefs.isUsingOutbox());*/
244        }
245        
246        @Test
247        public void testTakeMassActions() throws Exception {
248            final String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
249            List<String> recipients = new ArrayList<String>();
250            recipients.add(rkirkendPrincipalId);
251            TestRuleAttribute.setRecipientPrincipalIds("TestRole", "qualRole", recipients);
252            turnOnOutboxForUser(rkirkendPrincipalId);
253    
254            WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("quickstart"), "TestDocumentType");
255            document.route("");
256            
257            document = WorkflowDocumentFactory.loadDocument(rkirkendPrincipalId, document.getDocumentId());
258            assertTrue("approve should be requested", document.isApprovalRequested());
259         
260            document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("user1"), "OutboxTestDocumentType");
261            document.route("");
262            
263            document = WorkflowDocumentFactory.loadDocument(rkirkendPrincipalId, document.getDocumentId());
264            assertTrue("approve should be requested", document.isApprovalRequested());
265            
266            List<ActionItem> actionList = new ArrayList<ActionItem>(KEWServiceLocator.getActionListService().getActionList(rkirkendPrincipalId, new ActionListFilter()));
267            List<ActionInvocation> invocations = new ArrayList<ActionInvocation>();
268            ActionToTake actionToTake = new ActionToTake();
269            ActionItem actionItem = new ActionItem();
270    
271            for (ActionItem actinItem : actionList)
272            {
273                actionToTake.setActionItemId(actinItem.getId());
274                actionToTake.setActionTakenCd(actinItem.getActionRequestCd());
275                invocations.add(new ActionInvocation(actinItem.getId(), actionToTake.getActionTakenCd()));
276            }
277            KEWServiceLocator.getWorkflowDocumentService().takeMassActions(rkirkendPrincipalId, invocations);
278            assertEquals("Wrong number of outbox items found for rkirkendPrincipalId", 2, KEWServiceLocator.getActionListService().getOutbox(rkirkendPrincipalId, new ActionListFilter()).size());
279              
280        }
281    }