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