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.actionlist;
17  
18  import org.junit.Test;
19  import org.kuali.rice.core.api.util.Truth;
20  import org.kuali.rice.kew.actionitem.ActionItem;
21  import org.kuali.rice.kew.api.action.ActionInvocation;
22  import org.kuali.rice.kew.api.action.ActionType;
23  import org.kuali.rice.kew.api.KewApiServiceLocator;
24  import org.kuali.rice.kew.api.WorkflowDocument;
25  import org.kuali.rice.kew.api.WorkflowDocumentFactory;
26  import org.kuali.rice.kew.api.action.ActionRequestType;
27  import org.kuali.rice.kew.api.preferences.Preferences;
28  import org.kuali.rice.kew.preferences.service.impl.PreferencesServiceImpl;
29  import org.kuali.rice.kew.rule.TestRuleAttribute;
30  import org.kuali.rice.kew.service.KEWServiceLocator;
31  import org.kuali.rice.kew.test.KEWTestCase;
32  import org.kuali.rice.kew.api.KewApiConstants;
33  import org.kuali.rice.test.BaselineTestCase;
34  import org.springframework.transaction.TransactionStatus;
35  import org.springframework.transaction.support.TransactionCallback;
36  import org.springframework.transaction.support.TransactionTemplate;
37  
38  import java.util.ArrayList;
39  import java.util.Collection;
40  import java.util.List;
41  
42  import static org.junit.Assert.*;
43  
44  /**
45   * Tests Outbox functionality
46   *
47   * @author Kuali Rice Team (rice.collab@kuali.org)
48   */
49  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
50  public class OutboxTest extends KEWTestCase {
51  
52      protected void loadTestData() throws Exception {
53          loadXmlFile("OutboxTestConfig.xml");
54      }
55  
56      private void turnOnOutboxForUser(final String principalId) {
57          new TransactionTemplate(KEWServiceLocator.getPlatformTransactionManager()).execute(new TransactionCallback() {
58              public Object doInTransaction(TransactionStatus status) {
59                  KEWServiceLocator.getUserOptionsService().save(principalId, Preferences.KEYS.USE_OUT_BOX, KewApiConstants.PREFERENCES_YES_VAL);
60                  return null;
61              }
62          });
63      }
64  
65      @Test
66      public void testOutboxItemNotSavedOnSavedDocumentStatus() throws Exception {
67          final String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
68          List<String> recipients = new ArrayList<String>();
69          recipients.add(rkirkendPrincipalId);
70          TestRuleAttribute.setRecipientPrincipalIds("TestRole", "qualRole", recipients);
71  
72          WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("quickstart"), "TestDocumentType");
73          document.route("");
74  
75          document = WorkflowDocumentFactory.loadDocument(rkirkendPrincipalId, document.getDocumentId());
76          assertTrue("approve should be requested", document.isApprovalRequested());
77  
78          turnOnOutboxForUser(rkirkendPrincipalId);
79  
80          document.saveDocument("");
81  
82          Collection<ActionItem> outbox = KEWServiceLocator.getActionListService().getOutbox(rkirkendPrincipalId, new ActionListFilter());
83          assertEquals("there should not be any outbox items", 0, outbox.size());
84      }
85  
86      @Test
87      public void testTakeActionCreatesOutboxItem() throws Exception {
88  
89      	final String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
90          List<String> recipients = new ArrayList<String>();
91          recipients.add(rkirkendPrincipalId);
92          TestRuleAttribute.setRecipientPrincipalIds("TestRole", "qualRole", recipients);
93  
94          WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("quickstart"), "TestDocumentType");
95          document.route("");
96  
97          document = WorkflowDocumentFactory.loadDocument(rkirkendPrincipalId, document.getDocumentId());
98          assertTrue("approve should be requested", document.isApprovalRequested());
99  
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 = KewApiServiceLocator.getPreferencesService().getPreferences(user1PrincipalId);
236         assertTrue("By default the user's pref should be outbox on", Truth.strToBooleanIgnoreCase(prefs.getUseOutbox()));
237 
238         //TODO: this can no longer be set on the fly and grabbed through the preferences service (default values are set at startup)
239         //TODO: this is a prime candidate for a mocking tool
240         /*ConfigContext.getCurrentContextConfig().putProperty(KewApiConstants.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(ActionInvocation.create(ActionType.fromCode(actionToTake.getActionTakenCd()),
276                     actinItem.getId()));
277         }
278         KEWServiceLocator.getWorkflowDocumentService().takeMassActions(rkirkendPrincipalId, invocations);
279         assertEquals("Wrong number of outbox items found for rkirkendPrincipalId", 2, KEWServiceLocator.getActionListService().getOutbox(rkirkendPrincipalId, new ActionListFilter()).size());
280           
281     }
282 }