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