1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.overtime.daily.rule.service;
17
18 import java.math.BigDecimal;
19 import java.sql.Date;
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.junit.Assert;
26 import org.junit.Test;
27 import org.kuali.hr.test.KPMETestCase;
28 import org.kuali.hr.time.calendar.CalendarEntries;
29 import org.kuali.hr.time.overtime.daily.rule.DailyOvertimeRule;
30 import org.kuali.hr.time.service.base.TkServiceLocator;
31 import org.kuali.hr.time.test.TkTestUtils;
32 import org.kuali.hr.time.timeblock.TimeBlock;
33 import org.kuali.hr.time.timesheet.TimesheetDocument;
34 import org.kuali.hr.time.util.TKUtils;
35 import org.kuali.hr.time.util.TkConstants;
36 import org.kuali.hr.time.util.TkTimeBlockAggregate;
37
38 public class DailyOvertimeRuleServiceTest extends KPMETestCase {
39
40 public static final String USER_PRINCIPAL_ID = "admin";
41 private Date JAN_AS_OF_DATE = new Date((new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
42
43
44 private void createDailyOvertimeRule(String fromEarnGroup, String earnCode, String location, String paytype, String dept, Long workArea, Long task, BigDecimal minHours, BigDecimal maxGap, String overtimePref) {
45 DailyOvertimeRuleService service = TkServiceLocator.getDailyOvertimeRuleService();
46 DailyOvertimeRule rule = new DailyOvertimeRule();
47
48 rule.setEffectiveDate(JAN_AS_OF_DATE);
49 rule.setFromEarnGroup(fromEarnGroup);
50 rule.setEarnCode(earnCode);
51 rule.setLocation(location);
52 rule.setPaytype(paytype);
53 rule.setDept(dept);
54 rule.setWorkArea(workArea);
55 rule.setMaxGap(maxGap);
56 rule.setMinHours(minHours);
57 rule.setActive(true);
58
59 service.saveOrUpdate(rule);
60 }
61
62
63 @SuppressWarnings("serial")
64 @Test
65 public void testDailyOvertimeGapExceeded() throws Exception {
66 Long jobNumber = 30L;
67 Long workArea = 30L;
68 Long task = 30L;
69 createDailyOvertimeRule("REG", "OVT", "SD1", "BW", "TEST-DEPT", workArea,
70 task, new BigDecimal(8), new BigDecimal("0.10"), null);
71
72
73 DateTime start = new DateTime(2010, 3, 29, 14, 0, 0, 0, TKUtils.getSystemDateTimeZone());
74 List<TimeBlock> blocks = new ArrayList<TimeBlock>();
75 CalendarEntries payCalendarEntry = TkServiceLocator.getCalendarService().getCurrentCalendarDates("admin", new Date(start.getMillis()));
76 blocks.addAll(TkTestUtils.createUniformTimeBlocks(start, 2, new BigDecimal("4"), "REG", jobNumber, workArea, task));
77 blocks.addAll(TkTestUtils.createUniformTimeBlocks(start.plusHours(4).plusMinutes(15), 2, new BigDecimal("5"), "REG", jobNumber, workArea, task));
78 TkTimeBlockAggregate aggregate = new TkTimeBlockAggregate(blocks, payCalendarEntry);
79
80
81 TkTestUtils.verifyAggregateHourSums("Pre-Check", new HashMap<String,BigDecimal>() {{put("OVT", BigDecimal.ZERO);put("REG", new BigDecimal(18));}},aggregate,2);
82
83
84 TimesheetDocument tdoc = TkTestUtils.populateBlankTimesheetDocument(new Date(start.getMillis()));
85 tdoc.setTimeBlocks(blocks);
86 TkServiceLocator.getDailyOvertimeRuleService().processDailyOvertimeRules(tdoc, aggregate);
87
88
89 TkTestUtils.verifyAggregateHourSums("Post Rules Check", new HashMap<String,BigDecimal>() {{put("OVT", new BigDecimal(0));put("REG", new BigDecimal(18));}},aggregate,2);
90 }
91
92
93 @SuppressWarnings("serial")
94 @Test
95 public void testDailyOvertimeSimpleCase() throws Exception {
96 Long jobNumber = 30L;
97 Long workArea = 30L;
98 Long task = 30L;
99 createDailyOvertimeRule("REG", "OVT", "SD1", "BW", "TEST-DEPT", workArea,
100 task, new BigDecimal(8), new BigDecimal("15.0"), null);
101
102
103
104 DateTime start = new DateTime(2010, 3, 29, 14, 0, 0, 0, TKUtils.getSystemDateTimeZone());
105 List<TimeBlock> blocks = new ArrayList<TimeBlock>();
106 CalendarEntries payCalendarEntry = TkServiceLocator.getCalendarService().getCurrentCalendarDates("admin", new Date(start.getMillis()));
107 blocks.addAll(TkTestUtils.createUniformTimeBlocks(start, 2, new BigDecimal("4"), "REG", jobNumber, workArea, task));
108 blocks.addAll(TkTestUtils.createUniformTimeBlocks(start.plusHours(4).plusMinutes(15), 2, new BigDecimal("5"), "REG", jobNumber, workArea, task));
109 TkTimeBlockAggregate aggregate = new TkTimeBlockAggregate(blocks, payCalendarEntry);
110
111
112 TkTestUtils.verifyAggregateHourSums("Pre-Check", new HashMap<String,BigDecimal>() {{put("OVT", BigDecimal.ZERO);put("REG", new BigDecimal(18));}},aggregate,2);
113
114
115 TimesheetDocument tdoc = TkTestUtils.populateBlankTimesheetDocument(new Date(start.getMillis()));
116 tdoc.setTimeBlocks(blocks);
117 TkServiceLocator.getDailyOvertimeRuleService().processDailyOvertimeRules(tdoc, aggregate);
118
119
120 TkTestUtils.verifyAggregateHourSums("Post Rules Check", new HashMap<String,BigDecimal>() {{put("OVT", new BigDecimal(2));put("REG", new BigDecimal(16));}},aggregate,2);
121 }
122
123 @Test
124 public void testRuleCreationAndRetrieval() throws Exception {
125 Long workArea = 0L;
126 Long task = 30L;
127 createDailyOvertimeRule("REG", "OVT", "SD1", "BW", "TEST-DEPT", workArea,
128 task, new BigDecimal(8), new BigDecimal("0.25"), null);
129 DailyOvertimeRule rule = TkServiceLocator.getDailyOvertimeRuleService().getDailyOvertimeRule("SD1", "BW", "TEST-DEPT", workArea, JAN_AS_OF_DATE);
130 Assert.assertNotNull("Rule not created.", rule);
131 }
132
133 }