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.List;
21 import java.util.Map;
22 import java.util.Set;
23
24 import org.joda.time.DateTime;
25 import org.joda.time.LocalDate;
26 import org.junit.After;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.kuali.hr.KPMEWebTestCase;
31 import org.kuali.kpme.core.FunctionalTest;
32 import org.kuali.kpme.tklm.leave.block.LeaveBlock;
33 import org.kuali.kpme.tklm.leave.calendar.validation.LeaveCalendarValidationUtil;
34 import org.kuali.kpme.tklm.leave.summary.LeaveSummary;
35 import org.kuali.kpme.tklm.leave.summary.LeaveSummaryRow;
36
37 @FunctionalTest
38 public class LeaveCalendarValidationServiceTest extends KPMEWebTestCase {
39
40 @Before
41 public void setUp() throws Exception {
42 super.setUp();
43 }
44
45 @After
46 public void tearDown() throws Exception {
47 super.tearDown();
48 }
49
50 @Test
51 public void testValidateAvailableLeaveBalance() throws Exception {
52 LeaveSummary ls = new LeaveSummary();
53 LeaveSummaryRow lsr = new LeaveSummaryRow();
54 lsr.setAccrualCategory("testAC");
55 lsr.setAccrualCategoryId("5000");
56 lsr.setLeaveBalance(new BigDecimal(5));
57 List<LeaveSummaryRow> lsrList = new ArrayList<LeaveSummaryRow>();
58 lsrList.add(lsr);
59 ls.setLeaveSummaryRows(lsrList);
60
61
62
63 List<String> errors = LeaveCalendarValidationUtil.validateAvailableLeaveBalanceForUsage("EC", "02/15/2012", "02/15/2012", new BigDecimal(8), null);
64 Assert.assertEquals("Incorrect number of error messages", 1, errors.size());
65 String anError = errors.get(0);
66 Assert.assertTrue("error message not correct" , anError.equals("Requested leave amount 8 is greater than available leave balance 0.00"));
67
68
69 errors = LeaveCalendarValidationUtil.validateAvailableLeaveBalanceForUsage("EC1", "02/15/2012", "02/15/2012", new BigDecimal(8), null);
70 Assert.assertTrue("There should NOT be error message(s)" , errors.isEmpty());
71
72
73 LeaveBlock aLeaveBlock = new LeaveBlock();
74 aLeaveBlock.setEarnCode("EC");
75 aLeaveBlock.setLeaveAmount(new BigDecimal(-10));
76
77 errors = LeaveCalendarValidationUtil.validateAvailableLeaveBalanceForUsage("EC", "02/15/2012", "02/15/2012", new BigDecimal(3), aLeaveBlock);
78 Assert.assertTrue("There should NOT be error message(s)" , errors.isEmpty());
79
80 aLeaveBlock.setLeaveAmount(new BigDecimal(-2));
81 errors = LeaveCalendarValidationUtil.validateAvailableLeaveBalanceForUsage("EC", "02/15/2012", "02/15/2012", new BigDecimal(10), aLeaveBlock);
82 anError = errors.get(0);
83 Assert.assertTrue("error message not correct" , anError.equals("Requested leave amount 10 is greater than available leave balance 2.00"));
84 }
85
86 @Test
87 public void testValidateLeaveSpanOverMaxUsageRule() throws Exception {
88 LeaveSummary ls = new LeaveSummary();
89 LeaveSummaryRow lsr = new LeaveSummaryRow();
90 lsr.setAccrualCategory("testAC");
91 lsr.setUsageLimit(new BigDecimal(39));
92 List<LeaveSummaryRow> lsrList = new ArrayList<LeaveSummaryRow>();
93 lsrList.add(lsr);
94 ls.setLeaveSummaryRows(lsrList);
95
96 List<String> errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC", "02/15/2012", "02/19/2012", new BigDecimal(8), null);
97 Assert.assertEquals("There should be 1 error message" , 1, errors.size());
98 String anError = errors.get(0);
99 Assert.assertTrue("error message not correct" , anError.equals("This leave request would exceed the usage limit for " + lsr.getAccrualCategory()));
100 }
101
102 @Test
103 public void testValidateLeaveSpanUnderMaxUsageRule() throws Exception {
104 LeaveSummary ls = new LeaveSummary();
105 LeaveSummaryRow lsr = new LeaveSummaryRow();
106 lsr.setAccrualCategory("testAC");
107 lsr.setUsageLimit(new BigDecimal(41));
108 List<LeaveSummaryRow> lsrList = new ArrayList<LeaveSummaryRow>();
109 lsrList.add(lsr);
110 ls.setLeaveSummaryRows(lsrList);
111
112 List<String> errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC", "02/15/2012", "02/19/2012", new BigDecimal(8), null);
113 Assert.assertEquals("There should be no error message" , 0, errors.size());
114 }
115
116 @Test
117 public void testValidateLeaveSpanEqualMaxUsageRule() throws Exception {
118 LeaveSummary ls = new LeaveSummary();
119 LeaveSummaryRow lsr = new LeaveSummaryRow();
120 lsr.setAccrualCategory("testAC");
121 lsr.setUsageLimit(new BigDecimal(40));
122 List<LeaveSummaryRow> lsrList = new ArrayList<LeaveSummaryRow>();
123 lsrList.add(lsr);
124 ls.setLeaveSummaryRows(lsrList);
125
126 List<String> errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC", "02/15/2012", "02/19/2012", new BigDecimal(8), null);
127 Assert.assertEquals("There should be no error message" , 0, errors.size());
128 }
129
130 @Test
131 public void testValidateLeaveNonSpanOverMaxUsageRule() throws Exception {
132 LeaveSummary ls = new LeaveSummary();
133 LeaveSummaryRow lsr = new LeaveSummaryRow();
134 lsr.setAccrualCategory("testAC");
135 lsr.setUsageLimit(new BigDecimal(5));
136 List<LeaveSummaryRow> lsrList = new ArrayList<LeaveSummaryRow>();
137 lsrList.add(lsr);
138 ls.setLeaveSummaryRows(lsrList);
139
140 List<String> errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC", "02/15/2012", "02/15/2012", new BigDecimal(8), null);
141 Assert.assertEquals("There should be 1 error message" , 1, errors.size());
142 String anError = errors.get(0);
143 Assert.assertTrue("error message not correct" , anError.equals("This leave request would exceed the usage limit for " + lsr.getAccrualCategory()));
144 }
145
146 @Test
147 public void testValidateLeaveNonSpanEqualsMaxUsageRule() throws Exception {
148 LeaveSummary ls = new LeaveSummary();
149 LeaveSummaryRow lsr = new LeaveSummaryRow();
150 lsr.setAccrualCategory("testAC");
151 lsr.setUsageLimit(new BigDecimal(5));
152 List<LeaveSummaryRow> lsrList = new ArrayList<LeaveSummaryRow>();
153 lsrList.add(lsr);
154 ls.setLeaveSummaryRows(lsrList);
155
156 List<String> errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC", "02/15/2012", "02/15/2012", new BigDecimal(5), null);
157 Assert.assertEquals("There should be no error message" , 0, errors.size());
158
159 }
160
161 @Test
162 public void testValidateEditLeaveBlockMaxUsageRuleCaseOne() throws Exception {
163
164 LeaveSummary ls = new LeaveSummary();
165 LeaveSummaryRow lsr = new LeaveSummaryRow();
166 lsr.setAccrualCategory("testAC");
167 lsr.setUsageLimit(new BigDecimal(50));
168 lsr.setPendingLeaveRequests(new BigDecimal(25));
169 lsr.setYtdApprovedUsage(new BigDecimal(15));
170 List<LeaveSummaryRow> lsrList = new ArrayList<LeaveSummaryRow>();
171 lsrList.add(lsr);
172 ls.setLeaveSummaryRows(lsrList);
173
174
175 LeaveBlock aLeaveBlock = new LeaveBlock();
176 aLeaveBlock.setEarnCode("EC");
177 aLeaveBlock.setLeaveAmount(new BigDecimal(-10));
178 List<String> errors = new ArrayList<String>();
179
180
181
182 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC", "02/15/2012", "02/15/2012", new BigDecimal(19), aLeaveBlock);
183 Assert.assertTrue("There should be no error message test 1" , errors.size()== 0);
184
185
186 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC", "02/15/2012", "02/15/2012", new BigDecimal(20), aLeaveBlock);
187 Assert.assertTrue("There should be no error message test 2" , errors.size()== 0);
188
189
190 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC", "02/15/2012", "02/15/2012", new BigDecimal(21), aLeaveBlock);
191 Assert.assertTrue("There should be 1 error message test 3" , errors.size()== 1);
192
193
194 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC", "02/15/2012", "02/20/2012", new BigDecimal(12), aLeaveBlock);
195 Assert.assertTrue("There should be 1 error message test 5" , errors.size()== 1);
196
197
198 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC", "02/15/2012", "02/18/2012", new BigDecimal(12), aLeaveBlock);
199 Assert.assertTrue("There should be 1 error message test 6" , errors.size()== 1);
200 }
201
202 @Test
203 public void testValidateEditLeaveBlockMaxUsageRuleCaseTwo() throws Exception {
204
205 LeaveSummary ls = new LeaveSummary();
206 LeaveSummaryRow lsr = new LeaveSummaryRow();
207 lsr.setAccrualCategory("testAC");
208 lsr.setUsageLimit(new BigDecimal(50));
209 lsr.setPendingLeaveRequests(new BigDecimal(25));
210 lsr.setYtdApprovedUsage(new BigDecimal(30));
211 List<LeaveSummaryRow> lsrList = new ArrayList<LeaveSummaryRow>();
212 lsrList.add(lsr);
213 ls.setLeaveSummaryRows(lsrList);
214
215
216
217
218 LeaveBlock aLeaveBlock = new LeaveBlock();
219 aLeaveBlock.setEarnCode("EC");
220 aLeaveBlock.setLeaveAmount(new BigDecimal(-10));
221 List<String> errors = new ArrayList<String>();
222
223
224 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC", "02/15/2012", "02/15/2012", new BigDecimal(8), aLeaveBlock);
225 Assert.assertTrue("There should be 1 error message" , errors.size()== 1);
226
227
228 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC", "02/15/2012", "02/19/2012", new BigDecimal(9.5), aLeaveBlock);
229 Assert.assertTrue("There should be 1 error message" , errors.size()== 1);
230 }
231
232 @Test
233 public void testValidateEditLeaveBlockMaxUsageRuleCaseThree() throws Exception {
234
235 LeaveSummary ls = new LeaveSummary();
236 LeaveSummaryRow lsr = new LeaveSummaryRow();
237 lsr.setAccrualCategory("testAC");
238 lsr.setUsageLimit(new BigDecimal(50));
239 lsr.setPendingLeaveRequests(new BigDecimal(25));
240 lsr.setYtdApprovedUsage(new BigDecimal(15));
241
242 LeaveSummaryRow lsr2 = new LeaveSummaryRow();
243 lsr2.setAccrualCategory("testAC2");
244 lsr2.setUsageLimit(new BigDecimal(15));
245 lsr2.setPendingLeaveRequests(new BigDecimal(5));
246 lsr2.setYtdApprovedUsage(new BigDecimal(4));
247
248 List<LeaveSummaryRow> lsrList = new ArrayList<LeaveSummaryRow>();
249 lsrList.add(lsr);
250 lsrList.add(lsr2);
251 ls.setLeaveSummaryRows(lsrList);
252
253
254 LeaveBlock aLeaveBlock = new LeaveBlock();
255 aLeaveBlock.setEarnCode("EC");
256 aLeaveBlock.setAccrualCategory("testAC");
257 aLeaveBlock.setLeaveAmount(new BigDecimal(-10));
258 List<String> errors = new ArrayList<String>();
259
260
261 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC2", "02/15/2012", "02/15/2012", new BigDecimal(6), aLeaveBlock);
262 Assert.assertTrue("There should be no error message. reached usage limit." , errors.size()== 0);
263
264
265 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC2", "02/15/2012", "02/15/2012", new BigDecimal(7), aLeaveBlock);
266 Assert.assertTrue("There should be 1 error message, there were " + errors.size() + " errors" , errors.size()== 1);
267
268
269 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC2", "02/15/2012", "02/19/2012", new BigDecimal(1), aLeaveBlock);
270 Assert.assertTrue("There should be no error message, there were " + errors.size() + " errors" , errors.size()== 0);
271
272
273 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC2", "02/15/2012", "02/20/2012", new BigDecimal(1), aLeaveBlock);
274 Assert.assertTrue("There should be no error message, there were " + errors.size() + " errors" , errors.size()== 0);
275
276
277 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC2", "02/15/2012", "02/21/2012", new BigDecimal(1), aLeaveBlock);
278 Assert.assertTrue("There should be 1 error message, there were " + errors.size() + " errors" , errors.size()== 1);
279
280
281 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC1", "02/15/2012", "02/15/2012", new BigDecimal(10), aLeaveBlock);
282 Assert.assertTrue("There should be no error message, there were " + errors.size() + " errors" , errors.size()== 0);
283
284
285 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC1", "02/15/2012", "02/19/2012", new BigDecimal(2), aLeaveBlock);
286 Assert.assertTrue("There should be 0 error message, there were " + errors.size() + " errors" , errors.size()== 0);
287
288
289 errors = LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, "EC2", "02/15/2012", "02/25/2012", new BigDecimal(1), aLeaveBlock);
290 Assert.assertTrue("There should be 1 error message, there were " + errors.size() + " errors" , errors.size()== 1);
291
292 }
293 @Test
294 public void testGetWarningTextForLeaveBlocks() throws Exception {
295
296
297
298
299
300 List<LeaveBlock> leaveBlocs = new ArrayList<LeaveBlock>();
301 LeaveBlock lbA = new LeaveBlock();
302 lbA.setEarnCode("ECA");
303 lbA.setLeaveDate(LocalDate.now().toDate());
304 leaveBlocs.add(lbA);
305
306 LeaveBlock lbB = new LeaveBlock();
307 lbB.setEarnCode("ECB");
308 lbB.setLeaveDate(LocalDate.now().toDate());
309 leaveBlocs.add(lbB);
310
311 LeaveBlock lbC = new LeaveBlock();
312 lbC.setEarnCode("ECC");
313 lbC.setLeaveDate(LocalDate.now().toDate());
314 leaveBlocs.add(lbC);
315
316 Map<String, Set<String>> allMessages = LeaveCalendarValidationUtil.getWarningMessagesForLeaveBlocks(leaveBlocs, LocalDate.now().toDateTimeAtStartOfDay().toDate(), new DateTime().plusDays(1).toDate());
317 int numberOfMessages = 0;
318 for (Set<String> msgs : allMessages.values()){
319 numberOfMessages += msgs.size();
320 }
321 Assert.assertTrue("There should be 2 warning messages, not " + numberOfMessages, numberOfMessages== 2);
322
323 for (Set<String> msgs : allMessages.values()){
324 for (String message : msgs) {
325 Assert.assertTrue("Warning message should be 'Test Message' or 'Test Message1'", message.equals("Test Message") || message.equals("Test Message1"));
326 }
327 }
328 }
329
330
331
332
333
334
335
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 }