1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.ActionRequest;
23 import org.kuali.rice.kew.api.action.ActionType;
24 import org.kuali.rice.kew.api.KewApiServiceLocator;
25 import org.kuali.rice.kew.api.WorkflowDocument;
26 import org.kuali.rice.kew.api.WorkflowDocumentFactory;
27 import org.kuali.rice.kew.api.action.ActionRequestType;
28 import org.kuali.rice.kew.api.preferences.Preferences;
29 import org.kuali.rice.kew.preferences.service.impl.PreferencesServiceImpl;
30 import org.kuali.rice.kew.rule.TestRuleAttribute;
31 import org.kuali.rice.kew.service.KEWServiceLocator;
32 import org.kuali.rice.kew.test.KEWTestCase;
33 import org.kuali.rice.kew.api.KewApiConstants;
34 import org.kuali.rice.test.BaselineTestCase;
35 import org.springframework.transaction.TransactionStatus;
36 import org.springframework.transaction.support.TransactionCallback;
37 import org.springframework.transaction.support.TransactionTemplate;
38
39 import java.util.ArrayList;
40 import java.util.Collection;
41 import java.util.Iterator;
42 import java.util.List;
43
44 import static org.junit.Assert.*;
45
46
47
48
49
50
51 @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
52 public class OutboxTest extends KEWTestCase {
53
54 protected void loadTestData() throws Exception {
55 loadXmlFile("OutboxTestConfig.xml");
56 }
57
58 private void turnOnOutboxForUser(final String principalId) {
59 new TransactionTemplate(KEWServiceLocator.getPlatformTransactionManager()).execute(new TransactionCallback() {
60 public Object doInTransaction(TransactionStatus status) {
61 KEWServiceLocator.getUserOptionsService().save(principalId, Preferences.KEYS.USE_OUT_BOX, KewApiConstants.PREFERENCES_YES_VAL);
62 return null;
63 }
64 });
65 }
66
67 @Test
68 public void testOutboxItemNotSavedOnSavedDocumentStatus() throws Exception {
69 final String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
70 List<String> recipients = new ArrayList<String>();
71 recipients.add(rkirkendPrincipalId);
72 TestRuleAttribute.setRecipientPrincipalIds("TestRole", "qualRole", recipients);
73
74 WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("quickstart"), "TestDocumentType");
75 document.route("");
76
77 document = WorkflowDocumentFactory.loadDocument(rkirkendPrincipalId, document.getDocumentId());
78 assertTrue("approve should be requested", document.isApprovalRequested());
79
80 turnOnOutboxForUser(rkirkendPrincipalId);
81
82 document.saveDocument("");
83
84 Collection<ActionItem> outbox = KEWServiceLocator.getActionListService().getOutbox(rkirkendPrincipalId, new ActionListFilter());
85 assertEquals("there should not be any outbox items", 0, outbox.size());
86 }
87
88 @Test
89 public void testTakeActionsOnOutboxItem() throws Exception {
90
91 final String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
92 List<String> recipients = new ArrayList<String>();
93 recipients.add(rkirkendPrincipalId);
94 TestRuleAttribute.setRecipientPrincipalIds("TestRole", "qualRole", recipients);
95
96 WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("quickstart"),
97 "TestDocumentType");
98 document.route("");
99
100 document = WorkflowDocumentFactory.loadDocument(rkirkendPrincipalId, document.getDocumentId());
101 assertTrue("approve should be requested", document.isApprovalRequested());
102
103 turnOnOutboxForUser(rkirkendPrincipalId);
104
105 document.approve("");
106
107 Collection<ActionItem> outbox = KEWServiceLocator.getActionListService().getOutbox(rkirkendPrincipalId,
108 new ActionListFilter());
109 assertEquals("there should be an outbox item", 1, outbox.size());
110
111 outbox = KEWServiceLocator.getActionListService().getOutboxItemsByDocumentType(document.getDocumentTypeName());
112 assertEquals("there should be an outbox item", 1, outbox.size());
113
114 List<String> outBoxItemIds = new ArrayList();
115 ActionItem actionItem = null;
116 for (Iterator<ActionItem> iterator = outbox.iterator(); iterator.hasNext(); ) {
117 actionItem = iterator.next();
118 outBoxItemIds.add(actionItem.getId());
119 }
120
121 KEWServiceLocator.getActionListService().removeOutboxItems(rkirkendPrincipalId, outBoxItemIds);
122 outbox = KEWServiceLocator.getActionListService().getOutboxItemsByDocumentType(document.getDocumentTypeName());
123 assertEquals("there should be zero outbox item", 0, outbox.size());
124
125 KEWServiceLocator.getActionListService().saveOutboxItem(actionItem);
126 outbox = KEWServiceLocator.getActionListService().getOutboxItemsByDocumentType(document.getDocumentTypeName());
127 assertEquals("there should be 1 outbox item", 1, outbox.size());
128 }
129
130 @Test
131 public void testSingleOutboxItemPerDocument() throws Exception {
132 final String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
133 final String user1PrincipalId = getPrincipalIdForName("user1");
134 List<String> recipients = new ArrayList<String>();
135 recipients.add(rkirkendPrincipalId);
136 recipients.add(user1PrincipalId);
137 TestRuleAttribute.setRecipientPrincipalIds("TestRole", "qualRole", recipients);
138
139 WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("quickstart"), "TestDocumentType");
140 document.route("");
141
142 document = WorkflowDocumentFactory.loadDocument(rkirkendPrincipalId, document.getDocumentId());
143 assertTrue("approve should be requested", document.isApprovalRequested());
144
145 turnOnOutboxForUser(rkirkendPrincipalId);
146
147 document.adHocToPrincipal(ActionRequestType.APPROVE, "", user1PrincipalId, "", true);
148
149 document.approve("");
150
151 Collection<ActionItem> outbox = KEWServiceLocator.getActionListService().getOutbox(rkirkendPrincipalId, new ActionListFilter());
152 assertEquals("there should be an outbox item", 1, outbox.size());
153
154 document = WorkflowDocumentFactory.loadDocument(user1PrincipalId, document.getDocumentId());
155 assertTrue("approve should be requested", document.isApprovalRequested());
156
157 document.adHocToPrincipal(ActionRequestType.APPROVE, "", rkirkendPrincipalId, "", true);
158
159 document = WorkflowDocumentFactory.loadDocument(rkirkendPrincipalId, document.getDocumentId());
160 assertTrue("approve should be requested", document.isApprovalRequested());
161 document.approve("");
162
163 outbox = KEWServiceLocator.getActionListService().getOutbox(rkirkendPrincipalId, new ActionListFilter());
164 assertEquals("there should be an outbox item", 1, outbox.size());
165
166
167 }
168
169
170 @Test
171 public void testOnlyPersonWhoTookActionReceivesOutboxItem_Route() throws Exception {
172 final String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
173 final String user1PrincipalId = getPrincipalIdForName("user1");
174 List<String> recipients = new ArrayList<String>();
175 recipients.add(getPrincipalIdForName("rkirkend"));
176 recipients.add(getPrincipalIdForName("user1"));
177 TestRuleAttribute.setRecipientPrincipalIds("TestRole", "qualRole", recipients);
178
179 turnOnOutboxForUser(rkirkendPrincipalId);
180 turnOnOutboxForUser(user1PrincipalId);
181
182 WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("quickstart"), "TestDocumentType");
183 document.route("");
184
185
186 assertFalse(KEWServiceLocator.getActionListService().getActionList(rkirkendPrincipalId, new ActionListFilter()).isEmpty());
187 assertFalse(KEWServiceLocator.getActionListService().getActionList(user1PrincipalId, new ActionListFilter()).isEmpty());
188
189 document = WorkflowDocumentFactory.loadDocument(user1PrincipalId, document.getDocumentId());
190 document.approve("");
191
192 assertTrue(KEWServiceLocator.getActionListService().getOutbox(rkirkendPrincipalId, new ActionListFilter()).isEmpty());
193 assertEquals(1, KEWServiceLocator.getActionListService().getOutbox(user1PrincipalId, new ActionListFilter()).size());
194 }
195
196 @Test
197 public void testOnlyPersonWhoTookActionReceivesOutboxItem_BlanketApprove() throws Exception {
198 final String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
199 final String user1PrincipalId = getPrincipalIdForName("user1");
200 List<String> recipients = new ArrayList<String>();
201 recipients.add(rkirkendPrincipalId);
202 recipients.add(user1PrincipalId);
203 TestRuleAttribute.setRecipientPrincipalIds("TestRole", "qualRole", recipients);
204
205 turnOnOutboxForUser(rkirkendPrincipalId);
206 turnOnOutboxForUser(user1PrincipalId);
207
208 WorkflowDocument document = WorkflowDocumentFactory.createDocument(rkirkendPrincipalId, "TestDocumentType");
209 document.blanketApprove("");
210
211 assertEquals("Wrong number of outbox items found for rkirkend", 0, KEWServiceLocator.getActionListService().getOutbox(rkirkendPrincipalId, new ActionListFilter()).size());
212 assertEquals("Wrong number of outbox items found for user1", 0, KEWServiceLocator.getActionListService().getOutbox(user1PrincipalId, new ActionListFilter()).size());
213
214 document = WorkflowDocumentFactory.createDocument(rkirkendPrincipalId, "TestDocumentType");
215 document.saveDocument("");
216
217 assertEquals("Wrong number of action items found for rkirkend", 1, KEWServiceLocator.getActionListService().getActionList(rkirkendPrincipalId, new ActionListFilter()).size());
218
219 assertEquals("Wrong number of outbox items found for rkirkend", 0, KEWServiceLocator.getActionListService().getOutbox(rkirkendPrincipalId, new ActionListFilter()).size());
220 assertEquals("Wrong number of outbox items found for user1", 0, KEWServiceLocator.getActionListService().getOutbox(user1PrincipalId, new ActionListFilter()).size());
221
222 document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("rkirkend"), document.getDocumentId());
223 document.blanketApprove("");
224
225 assertEquals("Wrong number of outbox items found for rkirkend", 1, KEWServiceLocator.getActionListService().getOutbox(rkirkendPrincipalId, new ActionListFilter()).size());
226 assertEquals("Wrong number of outbox items found for user1", 0, KEWServiceLocator.getActionListService().getOutbox(user1PrincipalId, new ActionListFilter()).size());
227 }
228
229 @Test
230 public void testOnlyPersonWhoTookActionReceivesOutboxItem_Workgroup() throws Exception {
231 final String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
232 final String user1PrincipalId = getPrincipalIdForName("user1");
233 final String ewestfalPrincipalId = getPrincipalIdForName("ewestfal");
234
235 turnOnOutboxForUser(rkirkendPrincipalId);
236 turnOnOutboxForUser(user1PrincipalId);
237
238 WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("user2"), "OutboxTestDocumentType");
239 document.route("");
240
241 assertEquals("Wrong number of action items found for rkirkend", 1, KEWServiceLocator.getActionListService().getActionList(rkirkendPrincipalId, new ActionListFilter()).size());
242 assertEquals("Wrong number of action items found for ewestfal", 1, KEWServiceLocator.getActionListService().getActionList(ewestfalPrincipalId, new ActionListFilter()).size());
243 assertEquals("Wrong number of action items found for user1", 1, KEWServiceLocator.getActionListService().getActionList(user1PrincipalId, new ActionListFilter()).size());
244
245 assertEquals("Wrong number of outbox items found for rkirkend", 0, KEWServiceLocator.getActionListService().getOutbox(rkirkendPrincipalId, new ActionListFilter()).size());
246 assertEquals("Wrong number of outbox items found for ewestfal", 0, KEWServiceLocator.getActionListService().getOutbox(ewestfalPrincipalId, new ActionListFilter()).size());
247 assertEquals("Wrong number of outbox items found for user1", 0, KEWServiceLocator.getActionListService().getOutbox(user1PrincipalId, new ActionListFilter()).size());
248
249 document = WorkflowDocumentFactory.loadDocument(rkirkendPrincipalId, document.getDocumentId());
250 document.approve("");
251
252 assertEquals("Wrong number of outbox items found for rkirkend", 1, KEWServiceLocator.getActionListService().getOutbox(rkirkendPrincipalId, new ActionListFilter()).size());
253 assertEquals("Wrong number of outbox items found for ewestfal", 0, KEWServiceLocator.getActionListService().getOutbox(ewestfalPrincipalId, new ActionListFilter()).size());
254 assertEquals("Wrong number of outbox items found for user1", 0, KEWServiceLocator.getActionListService().getOutbox(user1PrincipalId, new ActionListFilter()).size());
255 }
256
257 @Test
258 public void testOutBoxDefaultPreferenceOnConfigParam() throws Exception {
259 final String user1PrincipalId = getPrincipalIdForName("user1");
260 Preferences prefs = KewApiServiceLocator.getPreferencesService().getPreferences(user1PrincipalId);
261 assertTrue("By default the user's pref should be outbox on", Truth.strToBooleanIgnoreCase(prefs.getUseOutbox()));
262
263
264
265
266
267
268
269 }
270
271 @Test
272 public void testTakeMassActions() throws Exception {
273 final String rkirkendPrincipalId = getPrincipalIdForName("rkirkend");
274 List<String> recipients = new ArrayList<String>();
275 recipients.add(rkirkendPrincipalId);
276 TestRuleAttribute.setRecipientPrincipalIds("TestRole", "qualRole", recipients);
277 turnOnOutboxForUser(rkirkendPrincipalId);
278
279 WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("quickstart"), "TestDocumentType");
280 document.route("");
281
282 document = WorkflowDocumentFactory.loadDocument(rkirkendPrincipalId, document.getDocumentId());
283 assertTrue("approve should be requested", document.isApprovalRequested());
284
285 document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("user1"), "OutboxTestDocumentType");
286 document.route("");
287
288 document = WorkflowDocumentFactory.loadDocument(rkirkendPrincipalId, document.getDocumentId());
289 assertTrue("approve should be requested", document.isApprovalRequested());
290
291 List<ActionItem> actionList = new ArrayList<ActionItem>(KEWServiceLocator.getActionListService().getActionList(rkirkendPrincipalId, new ActionListFilter()));
292 List<ActionInvocation> invocations = new ArrayList<ActionInvocation>();
293 ActionToTake actionToTake = new ActionToTake();
294 ActionItem actionItem = new ActionItem();
295
296 for (ActionItem actinItem : actionList)
297 {
298 actionToTake.setActionItemId(actinItem.getId());
299 actionToTake.setActionTakenCd(actinItem.getActionRequestCd());
300 invocations.add(ActionInvocation.create(ActionType.fromCode(actionToTake.getActionTakenCd()),
301 actinItem.getId()));
302 }
303 KEWServiceLocator.getWorkflowDocumentService().takeMassActions(rkirkendPrincipalId, invocations);
304 assertEquals("Wrong number of outbox items found for rkirkendPrincipalId", 2, KEWServiceLocator.getActionListService().getOutbox(rkirkendPrincipalId, new ActionListFilter()).size());
305
306 }
307 }