1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.lm.leaveCalendar.validation;
17
18 import java.math.BigDecimal;
19 import java.util.ArrayList;
20 import java.util.Calendar;
21 import java.util.Date;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26
27 import org.apache.commons.lang.StringUtils;
28 import org.junit.After;
29 import org.junit.Assert;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.kuali.hr.lm.balancetransfer.BalanceTransfer;
33 import org.kuali.hr.lm.leaveSummary.LeaveSummary;
34 import org.kuali.hr.lm.leaveSummary.LeaveSummaryRow;
35 import org.kuali.hr.lm.leaveblock.LeaveBlock;
36 import org.kuali.hr.lm.leavecalendar.validation.LeaveCalendarValidationUtil;
37 import org.kuali.hr.test.KPMETestCase;
38 import org.kuali.hr.time.service.base.TkServiceLocator;
39 import org.kuali.hr.time.util.TKUtils;
40 import org.kuali.rice.kew.api.document.DocumentContentUpdate;
41 import org.kuali.rice.kew.api.exception.WorkflowException;
42 import org.kuali.rice.kim.api.identity.principal.EntityNamePrincipalName;
43 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
44 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
45 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
46 import org.kuali.rice.krad.util.KRADConstants;
47
48 public class LeaveCalendarValidationServiceTest extends KPMETestCase {
49
50 @Before
51 public void setUp() throws Exception {
52 super.setUp();
53 }
54
55 @After
56 public void tearDown() throws Exception {
57 super.tearDown();
58 }
59
60 @Test
61 public void testValidateAvailableLeaveBalance() throws Exception {
62 LeaveSummary ls = new LeaveSummary();
63 LeaveSummaryRow lsr = new LeaveSummaryRow();
64 lsr.setAccrualCategory("testAC");
65 lsr.setAccrualCategoryId("5000");
66 lsr.setLeaveBalance(new BigDecimal(5));
67 List<LeaveSummaryRow> lsrList = new ArrayList<LeaveSummaryRow>();
68 lsrList.add(lsr);
69 ls.setLeaveSummaryRows(lsrList);
70
71
72
73 List<String> errors = LeaveCalendarValidationUtil.validateAvailableLeaveBalanceForUsage("EC", "02/15/2012", "02/15/2012", new BigDecimal(8), null);
74 Assert.assertEquals("Incorrect number of error messages", 1, errors.size());
75 String anError = errors.get(0);
76 Assert.assertTrue("error message not correct" , anError.equals("Requested leave amount 8 is greater than available leave balance 0.00"));
77
78
79 errors = LeaveCalendarValidationUtil.validateAvailableLeaveBalanceForUsage("EC1", "02/15/2012", "02/15/2012", new BigDecimal(8), null);
80 Assert.assertTrue("There should NOT be error message(s)" , errors.isEmpty());
81
82
83 LeaveBlock aLeaveBlock = new LeaveBlock();
84 aLeaveBlock.setEarnCode("EC");
85 aLeaveBlock.setLeaveAmount(new BigDecimal(-10));
86
87 errors = LeaveCalendarValidationUtil.validateAvailableLeaveBalanceForUsage("EC", "02/15/2012", "02/15/2012", new BigDecimal(3), aLeaveBlock);
88 Assert.assertTrue("There should NOT be error message(s)" , errors.isEmpty());
89
90 aLeaveBlock.setLeaveAmount(new BigDecimal(-2));
91 errors = LeaveCalendarValidationUtil.validateAvailableLeaveBalanceForUsage("EC", "02/15/2012", "02/15/2012", new BigDecimal(10), aLeaveBlock);
92 anError = errors.get(0);
93 Assert.assertTrue("error message not correct" , anError.equals("Requested leave amount 10 is greater than available leave balance 2.00"));
94 }
95
96 @Test
97 public void testValidateLeaveSpanOverMaxUsageRule() throws Exception {
98 LeaveSummary ls = new LeaveSummary();
99 LeaveSummaryRow lsr = new LeaveSummaryRow();
100 lsr.setAccrualCategory("testAC");
101 lsr.setUsageLimit(new BigDecimal(39));
102 List<LeaveSummaryRow> lsrList = new ArrayList<LeaveSummaryRow>();
103 lsrList.add(lsr);
104 ls.setLeaveSummaryRows(lsrList);
105
106 List<String> errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC", "02/15/2012", "02/19/2012", new BigDecimal(8), null);
107 Assert.assertEquals("There should be 1 error message" , 1, errors.size());
108 String anError = errors.get(0);
109 Assert.assertTrue("error message not correct" , anError.equals("This leave request would exceed the usage limit for " + lsr.getAccrualCategory()));
110 }
111
112 @Test
113 public void testValidateLeaveSpanUnderMaxUsageRule() throws Exception {
114 LeaveSummary ls = new LeaveSummary();
115 LeaveSummaryRow lsr = new LeaveSummaryRow();
116 lsr.setAccrualCategory("testAC");
117 lsr.setUsageLimit(new BigDecimal(41));
118 List<LeaveSummaryRow> lsrList = new ArrayList<LeaveSummaryRow>();
119 lsrList.add(lsr);
120 ls.setLeaveSummaryRows(lsrList);
121
122 List<String> errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC", "02/15/2012", "02/19/2012", new BigDecimal(8), null);
123 Assert.assertEquals("There should be no error message" , 0, errors.size());
124 }
125
126 @Test
127 public void testValidateLeaveSpanEqualMaxUsageRule() throws Exception {
128 LeaveSummary ls = new LeaveSummary();
129 LeaveSummaryRow lsr = new LeaveSummaryRow();
130 lsr.setAccrualCategory("testAC");
131 lsr.setUsageLimit(new BigDecimal(40));
132 List<LeaveSummaryRow> lsrList = new ArrayList<LeaveSummaryRow>();
133 lsrList.add(lsr);
134 ls.setLeaveSummaryRows(lsrList);
135
136 List<String> errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC", "02/15/2012", "02/19/2012", new BigDecimal(8), null);
137 Assert.assertEquals("There should be no error message" , 0, errors.size());
138 }
139
140 @Test
141 public void testValidateLeaveNonSpanOverMaxUsageRule() throws Exception {
142 LeaveSummary ls = new LeaveSummary();
143 LeaveSummaryRow lsr = new LeaveSummaryRow();
144 lsr.setAccrualCategory("testAC");
145 lsr.setUsageLimit(new BigDecimal(5));
146 List<LeaveSummaryRow> lsrList = new ArrayList<LeaveSummaryRow>();
147 lsrList.add(lsr);
148 ls.setLeaveSummaryRows(lsrList);
149
150 List<String> errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC", "02/15/2012", "02/15/2012", new BigDecimal(8), null);
151 Assert.assertEquals("There should be 1 error message" , 1, errors.size());
152 String anError = errors.get(0);
153 Assert.assertTrue("error message not correct" , anError.equals("This leave request would exceed the usage limit for " + lsr.getAccrualCategory()));
154 }
155
156 @Test
157 public void testValidateLeaveNonSpanEqualsMaxUsageRule() throws Exception {
158 LeaveSummary ls = new LeaveSummary();
159 LeaveSummaryRow lsr = new LeaveSummaryRow();
160 lsr.setAccrualCategory("testAC");
161 lsr.setUsageLimit(new BigDecimal(5));
162 List<LeaveSummaryRow> lsrList = new ArrayList<LeaveSummaryRow>();
163 lsrList.add(lsr);
164 ls.setLeaveSummaryRows(lsrList);
165
166 List<String> errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC", "02/15/2012", "02/15/2012", new BigDecimal(5), null);
167 Assert.assertEquals("There should be no error message" , 0, errors.size());
168
169 }
170
171 @Test
172 public void testValidateEditLeaveBlockMaxUsageRuleCaseOne() throws Exception {
173
174 LeaveSummary ls = new LeaveSummary();
175 LeaveSummaryRow lsr = new LeaveSummaryRow();
176 lsr.setAccrualCategory("testAC");
177 lsr.setUsageLimit(new BigDecimal(50));
178 lsr.setPendingLeaveRequests(new BigDecimal(25));
179 lsr.setYtdApprovedUsage(new BigDecimal(15));
180 List<LeaveSummaryRow> lsrList = new ArrayList<LeaveSummaryRow>();
181 lsrList.add(lsr);
182 ls.setLeaveSummaryRows(lsrList);
183
184
185 LeaveBlock aLeaveBlock = new LeaveBlock();
186 aLeaveBlock.setEarnCode("EC");
187 aLeaveBlock.setLeaveAmount(new BigDecimal(-10));
188 List<String> errors = new ArrayList<String>();
189
190
191
192 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC", "02/15/2012", "02/15/2012", new BigDecimal(19), aLeaveBlock);
193 Assert.assertTrue("There should be no error message test 1" , errors.size()== 0);
194
195
196 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC", "02/15/2012", "02/15/2012", new BigDecimal(20), aLeaveBlock);
197 Assert.assertTrue("There should be no error message test 2" , errors.size()== 0);
198
199
200 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC", "02/15/2012", "02/15/2012", new BigDecimal(21), aLeaveBlock);
201 Assert.assertTrue("There should be 1 error message test 3" , errors.size()== 1);
202
203
204 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC", "02/15/2012", "02/20/2012", new BigDecimal(12), aLeaveBlock);
205 Assert.assertTrue("There should be 1 error message test 5" , errors.size()== 1);
206
207
208 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC", "02/15/2012", "02/18/2012", new BigDecimal(12), aLeaveBlock);
209 Assert.assertTrue("There should be 1 error message test 6" , errors.size()== 1);
210 }
211
212 @Test
213 public void testValidateEditLeaveBlockMaxUsageRuleCaseTwo() throws Exception {
214
215 LeaveSummary ls = new LeaveSummary();
216 LeaveSummaryRow lsr = new LeaveSummaryRow();
217 lsr.setAccrualCategory("testAC");
218 lsr.setUsageLimit(new BigDecimal(50));
219 lsr.setPendingLeaveRequests(new BigDecimal(25));
220 lsr.setYtdApprovedUsage(new BigDecimal(30));
221 List<LeaveSummaryRow> lsrList = new ArrayList<LeaveSummaryRow>();
222 lsrList.add(lsr);
223 ls.setLeaveSummaryRows(lsrList);
224
225
226
227
228 LeaveBlock aLeaveBlock = new LeaveBlock();
229 aLeaveBlock.setEarnCode("EC");
230 aLeaveBlock.setLeaveAmount(new BigDecimal(-10));
231 List<String> errors = new ArrayList<String>();
232
233
234 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC", "02/15/2012", "02/15/2012", new BigDecimal(8), aLeaveBlock);
235 Assert.assertTrue("There should be 1 error message" , errors.size()== 1);
236
237
238 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC", "02/15/2012", "02/19/2012", new BigDecimal(9.5), aLeaveBlock);
239 Assert.assertTrue("There should be 1 error message" , errors.size()== 1);
240 }
241
242 @Test
243 public void testValidateEditLeaveBlockMaxUsageRuleCaseThree() throws Exception {
244
245 LeaveSummary ls = new LeaveSummary();
246 LeaveSummaryRow lsr = new LeaveSummaryRow();
247 lsr.setAccrualCategory("testAC");
248 lsr.setUsageLimit(new BigDecimal(50));
249 lsr.setPendingLeaveRequests(new BigDecimal(25));
250 lsr.setYtdApprovedUsage(new BigDecimal(15));
251
252 LeaveSummaryRow lsr2 = new LeaveSummaryRow();
253 lsr2.setAccrualCategory("testAC2");
254 lsr2.setUsageLimit(new BigDecimal(15));
255 lsr2.setPendingLeaveRequests(new BigDecimal(5));
256 lsr2.setYtdApprovedUsage(new BigDecimal(4));
257
258 List<LeaveSummaryRow> lsrList = new ArrayList<LeaveSummaryRow>();
259 lsrList.add(lsr);
260 lsrList.add(lsr2);
261 ls.setLeaveSummaryRows(lsrList);
262
263
264 LeaveBlock aLeaveBlock = new LeaveBlock();
265 aLeaveBlock.setEarnCode("EC");
266 aLeaveBlock.setAccrualCategory("testAC");
267 aLeaveBlock.setLeaveAmount(new BigDecimal(-10));
268 List<String> errors = new ArrayList<String>();
269
270
271 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC2", "02/15/2012", "02/15/2012", new BigDecimal(6), aLeaveBlock);
272 Assert.assertTrue("There should be no error message. reached usage limit." , errors.size()== 0);
273
274
275 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC2", "02/15/2012", "02/15/2012", new BigDecimal(7), aLeaveBlock);
276 Assert.assertTrue("There should be 1 error message, there were " + errors.size() + " errors" , errors.size()== 1);
277
278
279 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC2", "02/15/2012", "02/19/2012", new BigDecimal(1), aLeaveBlock);
280 Assert.assertTrue("There should be no error message, there were " + errors.size() + " errors" , errors.size()== 0);
281
282
283 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC2", "02/15/2012", "02/20/2012", new BigDecimal(1), aLeaveBlock);
284 Assert.assertTrue("There should be no error message, there were " + errors.size() + " errors" , errors.size()== 0);
285
286
287 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC2", "02/15/2012", "02/21/2012", new BigDecimal(1), aLeaveBlock);
288 Assert.assertTrue("There should be 1 error message, there were " + errors.size() + " errors" , errors.size()== 1);
289
290
291 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC1", "02/15/2012", "02/15/2012", new BigDecimal(10), aLeaveBlock);
292 Assert.assertTrue("There should be no error message, there were " + errors.size() + " errors" , errors.size()== 0);
293
294
295 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC1", "02/15/2012", "02/19/2012", new BigDecimal(2), aLeaveBlock);
296 Assert.assertTrue("There should be 0 error message, there were " + errors.size() + " errors" , errors.size()== 0);
297
298
299 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC2", "02/15/2012", "02/25/2012", new BigDecimal(1), aLeaveBlock);
300 Assert.assertTrue("There should be 1 error message, there were " + errors.size() + " errors" , errors.size()== 1);
301
302 }
303 @Test
304 public void testGetWarningTextForLeaveBlocks() throws Exception {
305
306
307
308
309
310 List<LeaveBlock> leaveBlocs = new ArrayList<LeaveBlock>();
311 LeaveBlock lbA = new LeaveBlock();
312 lbA.setEarnCode("ECA");
313 lbA.setLeaveDate(TKUtils.getCurrentDate());
314 leaveBlocs.add(lbA);
315
316 LeaveBlock lbB = new LeaveBlock();
317 lbB.setEarnCode("ECB");
318 lbB.setLeaveDate(TKUtils.getCurrentDate());
319 leaveBlocs.add(lbB);
320
321 LeaveBlock lbC = new LeaveBlock();
322 lbC.setEarnCode("ECC");
323 lbC.setLeaveDate(TKUtils.getCurrentDate());
324 leaveBlocs.add(lbC);
325
326 Map<String, Set<String>> allMessages = LeaveCalendarValidationUtil.getWarningMessagesForLeaveBlocks(leaveBlocs);
327 int numberOfMessages = 0;
328 for (Set<String> msgs : allMessages.values()){
329 numberOfMessages += msgs.size();
330 }
331 Assert.assertTrue("There should be 2 warning messages, not " + numberOfMessages, numberOfMessages== 2);
332
333 for (Set<String> msgs : allMessages.values()){
334 for (String message : msgs) {
335 Assert.assertTrue("Warning message should be 'Test Message' or 'Test Message1'", message.equals("Test Message") || message.equals("Test Message1"));
336 }
337 }
338 }
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417 }