1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.shiftdiff.rule;
17
18 import java.math.BigDecimal;
19 import java.sql.Time;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
23
24 import org.joda.time.DateTime;
25 import org.joda.time.DateTimeZone;
26 import org.joda.time.LocalTime;
27 import org.junit.Assert;
28 import org.junit.Ignore;
29 import org.junit.Test;
30 import org.kuali.hr.KPMEWebTestCase;
31 import org.kuali.kpme.core.FunctionalTest;
32 import org.kuali.kpme.core.assignment.Assignment;
33 import org.kuali.kpme.core.assignment.AssignmentDescriptionKey;
34 import org.kuali.kpme.core.calendar.entry.CalendarEntry;
35 import org.kuali.kpme.core.service.HrServiceLocator;
36 import org.kuali.kpme.core.util.TKUtils;
37 import org.kuali.kpme.tklm.time.rules.shiftdifferential.ShiftDifferentialRule;
38 import org.kuali.kpme.tklm.time.rules.shiftdifferential.service.ShiftDifferentialRuleService;
39 import org.kuali.kpme.tklm.time.service.TkServiceLocator;
40 import org.kuali.kpme.tklm.time.timeblock.TimeBlock;
41 import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
42 import org.kuali.kpme.tklm.time.util.TkTimeBlockAggregate;
43 import org.kuali.kpme.tklm.utils.TkTestUtils;
44
45
46
47
48
49
50 @FunctionalTest
51 public class ShiftDifferentialRuleServiceProcessTest extends KPMEWebTestCase {
52
53
54 public static final String USER_PRINCIPAL_ID = "admin";
55 private DateTime JAN_AS_OF_DATE = new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110 @SuppressWarnings("serial")
111 @Test
112 public void testProcessTimesheetBoundaryCarryoverOverlapCase() throws Exception {
113 DateTimeZone tz = HrServiceLocator.getTimezoneService().getUserTimezoneWithFallback();
114
115 boolean[] dayArray = {false, false, true, true, true, true, true};
116
117 Long jobNumber = 30L;
118 Long workArea = 0L;
119 this.createShiftDifferentialRule(
120 "BWS-CAL", "REG", "PRM", "SD1", "SD1", "SD1",
121 (new DateTime(2010, 8, 31, 22, 0, 0, 0, tz)),
122 (new DateTime(2010, 8, 31, 4, 0, 0, 0, tz)),
123 new BigDecimal(3),
124 new BigDecimal("15.00"),
125 dayArray);
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156 DateTime beginPeriodDate = new DateTime(2010, 8, 15, 0, 0, 0, 0, tz);
157 DateTime endPeriodDate = new DateTime(2010, 9, 1, 0, 0, 0, 0);
158 CalendarEntry endOfAugust = HrServiceLocator.getCalendarEntryService().getCalendarEntryByIdAndPeriodEndDate("2", endPeriodDate);
159 DateTime start = new DateTime(2010, 8, 31, 21, 45, 0, 0, tz);
160 List<TimeBlock> blocks = new ArrayList<TimeBlock>();
161 TimesheetDocument tdoc = TkServiceLocator.getTimesheetService().openTimesheetDocument("admin", endOfAugust);
162 Assignment assignment = HrServiceLocator.getAssignmentService().getAssignment("admin", AssignmentDescriptionKey.get("30_30_30"), beginPeriodDate.toLocalDate());
163 blocks.addAll(TkTestUtils.createUniformActualTimeBlocks(tdoc, assignment, "RGN", start, 1, new BigDecimal(2), BigDecimal.ZERO, "admin"));
164 TkTimeBlockAggregate aggregate = new TkTimeBlockAggregate(blocks, endOfAugust, HrServiceLocator.getCalendarService().getCalendar(endOfAugust.getHrCalendarId()), true);
165 tdoc.setTimeBlocks(blocks);
166 TkServiceLocator.getShiftDifferentialRuleService().processShiftDifferentialRules(tdoc, aggregate);
167 TkTestUtils.verifyAggregateHourSumsFlatList("August Pre-Check", new HashMap<String,BigDecimal>() {{put("PRM", BigDecimal.ZERO);put("RGN", new BigDecimal(2));}},aggregate);
168 TkServiceLocator.getTimeBlockService().saveTimeBlocks(new ArrayList<TimeBlock>(), aggregate.getFlattenedTimeBlockList(), "admin");
169
170
171
172
173 start = new DateTime(2010, 9, 1, 0, 0, 0, 0, tz);
174 CalendarEntry payCalendarEntry = HrServiceLocator.getCalendarEntryService().getCurrentCalendarDates("admin", start.toLocalDate().toDateTimeAtStartOfDay());
175 tdoc = TkServiceLocator.getTimesheetService().openTimesheetDocument("admin", payCalendarEntry);
176 blocks = new ArrayList<TimeBlock>();
177 blocks.addAll(TkTestUtils.createUniformTimeBlocks(start, 1, new BigDecimal("5"), "RGN", jobNumber, workArea));
178 blocks.addAll(TkTestUtils.createUniformTimeBlocks(start.plusHours(6), 1, new BigDecimal("6"), "RGN", jobNumber, workArea));
179 blocks.addAll(TkTestUtils.createUniformTimeBlocks(start.plusHours(22), 1, new BigDecimal("2"), "RGN", jobNumber, workArea));
180 blocks.addAll(TkTestUtils.createUniformTimeBlocks(start.plusDays(1), 1, new BigDecimal("1"), "RGN", jobNumber, workArea));
181 blocks.addAll(TkTestUtils.createUniformTimeBlocks(start.plusDays(1).plusHours(17), 1, new BigDecimal("6"), "RGN", jobNumber, workArea));
182 setDocumentIdOnBlocks(blocks, tdoc.getDocumentId());
183
184 aggregate = new TkTimeBlockAggregate(blocks, payCalendarEntry, HrServiceLocator.getCalendarService().getCalendar(payCalendarEntry.getHrCalendarId()), true);
185
186 TkTestUtils.verifyAggregateHourSumsFlatList("September Pre-Check", new HashMap<String,BigDecimal>() {{put("PRM", BigDecimal.ZERO);put("RGN", new BigDecimal(20));}},aggregate);
187
188
189 TkServiceLocator.getShiftDifferentialRuleService().processShiftDifferentialRules(tdoc, aggregate);
190 TkTestUtils.verifyAggregateHourSumsFlatList("September Post-Check", new HashMap<String,BigDecimal>() {{put("PRM", new BigDecimal("8.75"));put("RGN", new BigDecimal(20));}},aggregate);
191 }
192
193 private void setDocumentIdOnBlocks(List<TimeBlock> blocks, String id) {
194 for (TimeBlock b : blocks) {
195 b.setDocumentId(id);
196 }
197 }
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216 @SuppressWarnings("serial")
217 @Test
218 public void testProcessShiftTimesheeetBoundaryCarryoverCase() throws Exception {
219
220 boolean[] dayArray = {false, false, true, false, true, true, true};
221
222 Long jobNumber = 30L;
223 Long workArea = 0L;
224
225 DateTimeZone tz = HrServiceLocator.getTimezoneService().getUserTimezoneWithFallback();
226 this.createShiftDifferentialRule(
227 "BWS-CAL",
228 "REG",
229 "PRM",
230 "SD1",
231 "SD1",
232 "SD1",
233 (new DateTime(2010, 8, 31, 22, 0, 0, 0, tz)),
234 (new DateTime(2010, 8, 31, 5, 0, 0, 0, tz)),
235 new BigDecimal(3),
236 new BigDecimal("0.25"),
237 dayArray);
238
239
240 DateTime endPeriodDate = new DateTime(2010, 9, 1, 0, 0, 0, 0);
241 CalendarEntry endOfAugust = HrServiceLocator.getCalendarEntryService().getCalendarEntryByIdAndPeriodEndDate("2", endPeriodDate);
242 DateTime start = new DateTime(2010, 8, 31, 22, 0, 0, 0, tz);
243 List<TimeBlock> blocks = new ArrayList<TimeBlock>();
244 TimesheetDocument tdoc = TkServiceLocator.getTimesheetService().openTimesheetDocument("admin", endOfAugust);
245 Assignment assignment = HrServiceLocator.getAssignmentService().getAssignment("admin", AssignmentDescriptionKey.get("30_30_30"), endOfAugust.getBeginPeriodFullDateTime().toLocalDate());
246 blocks.addAll(TkTestUtils.createUniformActualTimeBlocks(tdoc, assignment, "RGN", start, 1, new BigDecimal(2), BigDecimal.ZERO, "admin"));
247 TkTimeBlockAggregate aggregate = new TkTimeBlockAggregate(blocks, endOfAugust, HrServiceLocator.getCalendarService().getCalendar(endOfAugust.getHrCalendarId()), true);
248
249
250
251 tdoc.setTimeBlocks(blocks);
252 TkServiceLocator.getShiftDifferentialRuleService().processShiftDifferentialRules(tdoc, aggregate);
253 TkTestUtils.verifyAggregateHourSumsFlatList("August Pre-Check", new HashMap<String,BigDecimal>() {{put("PRM", BigDecimal.ZERO);put("RGN", new BigDecimal(2));}},aggregate);
254 TkServiceLocator.getTimeBlockService().saveTimeBlocks(new ArrayList<TimeBlock>(), aggregate.getFlattenedTimeBlockList(), "admin");
255
256
257
258 start = new DateTime(2010, 9, 1, 0, 0, 0, 0, tz);
259 CalendarEntry payCalendarEntry = HrServiceLocator.getCalendarEntryService().getCurrentCalendarDates("admin", start.toLocalDate().toDateTimeAtStartOfDay());
260 tdoc = TkServiceLocator.getTimesheetService().openTimesheetDocument("admin", payCalendarEntry);
261 blocks = new ArrayList<TimeBlock>();
262 blocks.addAll(TkTestUtils.createUniformTimeBlocks(start, 1, new BigDecimal("5"), "RGN", jobNumber, workArea));
263 aggregate = new TkTimeBlockAggregate(blocks, payCalendarEntry, HrServiceLocator.getCalendarService().getCalendar(payCalendarEntry.getHrCalendarId()), true);
264 TkTestUtils.verifyAggregateHourSumsFlatList("September Pre-Check", new HashMap<String,BigDecimal>() {{put("PRM", BigDecimal.ZERO);put("RGN", new BigDecimal(5));}},aggregate);
265
266
267 TkServiceLocator.getShiftDifferentialRuleService().processShiftDifferentialRules(tdoc, aggregate);
268 TkTestUtils.verifyAggregateHourSumsFlatList("September Post-Check", new HashMap<String,BigDecimal>() {{put("PRM", new BigDecimal(7));put("RGN", new BigDecimal(5));}},aggregate);
269 }
270
271 @SuppressWarnings("serial")
272 @Test
273
274
275
276
277
278
279
280
281
282
283 public void testProcessShiftSimpleNoisyCase() throws Exception {
284
285 boolean[] dayArray = {true, true, true, true, true, true, true};
286
287 Long jobNumber = 30L;
288 Long workArea = 0L;
289 this.createShiftDifferentialRule(
290 "BWS-CAL",
291 "REG",
292 "PRM",
293 "SD1",
294 "SD1",
295 "SD1",
296 (new DateTime(2010, 3, 29, 16, 0, 0, 0, TKUtils.getSystemDateTimeZone())),
297 (new DateTime(2010, 3, 30, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())),
298 new BigDecimal(4),
299 new BigDecimal("15"),
300 dayArray);
301
302
303 DateTime start = new DateTime(2010, 3, 29, 14, 0, 0, 0, TKUtils.getSystemDateTimeZone());
304 List<TimeBlock> blocks = new ArrayList<TimeBlock>();
305 CalendarEntry payCalendarEntry = HrServiceLocator.getCalendarEntryService().getCurrentCalendarDates("admin", start);
306 blocks.addAll(TkTestUtils.createUniformTimeBlocks(start, 2, new BigDecimal("4"), "RGN", jobNumber, workArea));
307 blocks.addAll(TkTestUtils.createUniformTimeBlocks(start.plusHours(4).plusMinutes(15), 2, new BigDecimal("2"), "RGN", jobNumber, workArea));
308 blocks.addAll(TkTestUtils.createUniformTimeBlocks(new DateTime(2010, 3, 29, 12, 58, 0, 0, TKUtils.getSystemDateTimeZone()), 2, new BigDecimal(1), "RGN", jobNumber, workArea));
309 TkTimeBlockAggregate aggregate = new TkTimeBlockAggregate(blocks, payCalendarEntry);
310
311
312 TkTestUtils.verifyAggregateHourSums("Pre-Check", new HashMap<String,BigDecimal>() {{put("PRM", BigDecimal.ZERO);put("RGN", new BigDecimal(14));}},aggregate,2);
313
314
315 TimesheetDocument tdoc = TkTestUtils.populateBlankTimesheetDocument(start, "admin");
316 tdoc.setTimeBlocks(blocks);
317 TkServiceLocator.getShiftDifferentialRuleService().processShiftDifferentialRules(tdoc, aggregate);
318
319
320 TkTestUtils.verifyAggregateHourSums("Post Rules Check", new HashMap<String,BigDecimal>() {{put("PRM", new BigDecimal(8));put("RGN", new BigDecimal(14));}},aggregate,2);
321 }
322
323 @SuppressWarnings("serial")
324 @Test
325
326
327
328
329
330
331
332 public void testProcessShiftSimpleCase() throws Exception {
333
334 boolean[] dayArray = {true, true, true, true, true, true, true};
335
336 Long jobNumber = 30L;
337 Long workArea = 0L;
338 this.createShiftDifferentialRule(
339 "BWS-CAL",
340 "REG",
341 "PRM",
342 "SD1",
343 "SD1",
344 "SD1",
345 (new DateTime(2010, 3, 29, 16, 0, 0, 0, TKUtils.getSystemDateTimeZone())),
346 (new DateTime(2010, 3, 30, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())),
347 new BigDecimal(4),
348 new BigDecimal("15.00"),
349 dayArray);
350
351
352 DateTime start = new DateTime(2010, 3, 29, 14, 0, 0, 0, TKUtils.getSystemDateTimeZone());
353 List<TimeBlock> blocks = new ArrayList<TimeBlock>();
354 CalendarEntry payCalendarEntry = HrServiceLocator.getCalendarEntryService().getCurrentCalendarDates("admin", start);
355 blocks.addAll(TkTestUtils.createUniformTimeBlocks(start, 2, new BigDecimal("4"), "REG", jobNumber, workArea));
356 blocks.addAll(TkTestUtils.createUniformTimeBlocks(start.plusHours(4).plusMinutes(15), 2, new BigDecimal("2"), "REG", jobNumber, workArea));
357 TkTimeBlockAggregate aggregate = new TkTimeBlockAggregate(blocks, payCalendarEntry);
358
359
360 TkTestUtils.verifyAggregateHourSums("Pre-Check", new HashMap<String,BigDecimal>() {{put("PRM", BigDecimal.ZERO);put("REG", new BigDecimal(12));}},aggregate,2);
361
362
363 TimesheetDocument tdoc = TkTestUtils.populateBlankTimesheetDocument(start, "admin");
364 tdoc.setTimeBlocks(blocks);
365 TkServiceLocator.getShiftDifferentialRuleService().processShiftDifferentialRules(tdoc, aggregate);
366
367
368 TkTestUtils.verifyAggregateHourSums("Post Rules Check", new HashMap<String,BigDecimal>() {{put("PRM", new BigDecimal(8));put("REG", new BigDecimal(12));}},aggregate,2);
369 }
370
371
372
373
374
375
376 private void createShiftDifferentialRule(String pyCalendarGroup, String fromEarnGroup, String premiumEarnCode, String location, String payGrade, String hrSalGroup, DateTime startTime, DateTime endTime, BigDecimal minHours, BigDecimal maxGap, boolean dayBooleans[]) {
377 Assert.assertTrue("Wrong number of day booleans", dayBooleans.length == 7);
378
379 ShiftDifferentialRuleService service = TkServiceLocator.getShiftDifferentialRuleService();
380 ShiftDifferentialRule sdr = new ShiftDifferentialRule();
381
382 sdr.setBeginTime(new Time(startTime.getMillis()));
383 sdr.setEndTime(new Time(endTime.getMillis()));
384 sdr.setMinHours(minHours);
385 sdr.setMaxGap(maxGap);
386 sdr.setActive(true);
387 sdr.setUserPrincipalId(USER_PRINCIPAL_ID);
388 sdr.setEffectiveLocalDate(JAN_AS_OF_DATE.toLocalDate());
389 sdr.setLocation(location);
390 sdr.setPayGrade(payGrade);
391 sdr.setHrSalGroup(hrSalGroup);
392 sdr.setFromEarnGroup(fromEarnGroup);
393 sdr.setPyCalendarGroup(pyCalendarGroup);
394 sdr.setEarnCode(premiumEarnCode);
395
396 for (int i=0; i<dayBooleans.length; i++) {
397 switch(i) {
398 case 0:
399 sdr.setSunday(dayBooleans[i]);
400 break;
401 case 1:
402 sdr.setMonday(dayBooleans[i]);
403 break;
404 case 2:
405 sdr.setTuesday(dayBooleans[i]);
406 break;
407 case 3:
408 sdr.setWednesday(dayBooleans[i]);
409 break;
410 case 4:
411 sdr.setThursday(dayBooleans[i]);
412 break;
413 case 5:
414 sdr.setFriday(dayBooleans[i]);
415 break;
416 case 6:
417 sdr.setSaturday(dayBooleans[i]);
418 break;
419 }
420 }
421
422 service.saveOrUpdate(sdr);
423
424 ShiftDifferentialRule sdrBack = service.getShiftDifferentialRule(sdr.getTkShiftDiffRuleId());
425
426 DateTimeZone tz = HrServiceLocator.getTimezoneService().getUserTimezoneWithFallback();
427 LocalTime orig_start = new LocalTime(sdr.getBeginTime(), tz);
428 LocalTime orig_end = new LocalTime(sdr.getEndTime(), tz);
429
430 LocalTime stored_start = new LocalTime(sdrBack.getBeginTime(), tz);
431 LocalTime stored_end = new LocalTime(sdrBack.getEndTime(), tz);
432
433 Assert.assertTrue("Start times not equal.", orig_start.equals(stored_start));
434 Assert.assertTrue("End times not equal.", orig_end.equals(stored_end));
435 }
436
437
438 @Ignore
439 @Test
440
441
442
443
444
445
446
447
448
449
450 public void simpleCaseWithWorkSchedule() throws Exception {
451
452 boolean[] dayArray = {true, true, true, true, true, true, true};
453
454 Long jobNumber = 30L;
455 Long workArea = 0L;
456 this.createShiftDifferentialRule(
457 "BWS-CAL",
458 "REG",
459 "PRM",
460 "SD1",
461 "SD1",
462 "SD1",
463 (new DateTime(2010, 3, 29, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())),
464 (new DateTime(2010, 3, 29, 17, 0, 0, 0, TKUtils.getSystemDateTimeZone())),
465 new BigDecimal(4),
466 new BigDecimal("15.00"),
467 dayArray);
468
469
470 DateTime start = new DateTime(2010, 3, 29, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone());
471 DateTime holtime = new DateTime(2010, 3, 30, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
472 List<TimeBlock> blocks = new ArrayList<TimeBlock>();
473 CalendarEntry payCalendarEntry = HrServiceLocator.getCalendarEntryService().getCurrentCalendarDates("admin", start);
474 blocks.addAll(TkTestUtils.createUniformTimeBlocks(start, 1, new BigDecimal("4"), "REG", jobNumber, workArea));
475 blocks.addAll(TkTestUtils.createUniformTimeBlocks(holtime, 1, new BigDecimal("4"), "HOL", jobNumber, workArea));
476
477 TkTimeBlockAggregate aggregate = new TkTimeBlockAggregate(blocks, payCalendarEntry);
478
479
480 TkTestUtils.verifyAggregateHourSums("Pre-Check", new HashMap<String,BigDecimal>() {{put("PRM", BigDecimal.ZERO);put("REG", new BigDecimal(4));put("HOL", new BigDecimal(4));}},aggregate,2);
481
482
483 TimesheetDocument tdoc = TkTestUtils.populateBlankTimesheetDocument(start, "admin");
484 tdoc.setTimeBlocks(blocks);
485 TkServiceLocator.getShiftDifferentialRuleService().processShiftDifferentialRules(tdoc, aggregate);
486
487
488 TkTestUtils.verifyAggregateHourSums("Post Rules Check", new HashMap<String,BigDecimal>() {{put("PRM", new BigDecimal(8));put("REG", new BigDecimal(4));}},aggregate,2);
489
490 }
491
492 }