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.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
22
23 import org.joda.time.DateTime;
24 import org.joda.time.DateTimeZone;
25 import org.junit.Assert;
26 import org.junit.Test;
27 import org.kuali.hr.KPMEWebTestCase;
28 import org.kuali.kpme.core.FunctionalTest;
29 import org.kuali.kpme.core.api.calendar.entry.CalendarEntry;
30 import org.kuali.kpme.core.service.HrServiceLocator;
31 import org.kuali.kpme.core.util.TKUtils;
32 import org.kuali.kpme.tklm.api.time.timeblock.TimeBlock;
33 import org.kuali.kpme.tklm.time.rules.overtime.daily.DailyOvertimeRule;
34 import org.kuali.kpme.tklm.time.rules.overtime.daily.service.DailyOvertimeRuleService;
35 import org.kuali.kpme.tklm.time.service.TkServiceLocator;
36 import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
37 import org.kuali.kpme.tklm.time.util.TkTimeBlockAggregate;
38 import org.kuali.kpme.tklm.utils.TkTestUtils;
39
40 @FunctionalTest
41 public class DailyOvertimeRuleServiceTest extends KPMEWebTestCase {
42
43 public static final String USER_PRINCIPAL_ID = "admin";
44 private DateTime JAN_AS_OF_DATE = new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
45
46 private void createDailyOvertimeRule(String groupKeyCode, String fromEarnGroup, String earnCode,
47
48 String paytype, String dept, Long workArea, Long task, BigDecimal minHours, BigDecimal maxGap, String overtimePref) {
49 DailyOvertimeRuleService service = TkServiceLocator.getDailyOvertimeRuleService();
50 DailyOvertimeRule rule = new DailyOvertimeRule();
51
52 rule.setGroupKeyCode("IU-IN");
53 rule.setEffectiveLocalDate(JAN_AS_OF_DATE.toLocalDate());
54 rule.setFromEarnGroup(fromEarnGroup);
55 rule.setEarnCode(earnCode);
56
57 rule.setPaytype(paytype);
58 rule.setDept(dept);
59 rule.setWorkArea(workArea);
60 rule.setMaxGap(maxGap);
61 rule.setMinHours(minHours);
62 rule.setActive(true);
63 rule.setUserPrincipalId("admin");
64
65 service.saveOrUpdate(rule);
66 }
67
68
69 @SuppressWarnings("serial")
70 @Test
71 public void testDailyOvertimeGapExceeded() throws Exception {
72 Long jobNumber = 30L;
73 Long workArea = 30L;
74 Long task = 30L;
75 String groupKey = "IU-IN";
76 createDailyOvertimeRule("IU-IN", "REG", "OVT",
77
78 "BW", "TEST-DEPT", workArea,
79 task, new BigDecimal(8), new BigDecimal("0.10"), null);
80
81
82 DateTimeZone zone = HrServiceLocator.getTimezoneService().getTargetUserTimezoneWithFallback();
83 DateTime start = new DateTime(2010, 3, 29, 14, 0, 0, 0, TKUtils.getSystemDateTimeZone());
84 TimesheetDocument tdoc = TkTestUtils.populateBlankTimesheetDocument(start, "admin");
85 DateTime tbStart = new DateTime(2010, 3, 29, 14, 0, 0, 0, zone);
86 List<TimeBlock> blocks = new ArrayList<TimeBlock>();
87 CalendarEntry payCalendarEntry = HrServiceLocator.getCalendarEntryService().getCurrentCalendarDates("admin", start);
88 blocks.addAll(TkTestUtils.createUniformTimeBlocks(tbStart, 2, new BigDecimal("4"), "REG", jobNumber, workArea, task, groupKey, tdoc.getDocumentId()));
89 blocks.addAll(TkTestUtils.createUniformTimeBlocks(tbStart.plusHours(4).plusMinutes(15), 2, new BigDecimal("5"), "REG", jobNumber, workArea, task, groupKey, tdoc.getDocumentId()));
90 TkTimeBlockAggregate aggregate = new TkTimeBlockAggregate(blocks, payCalendarEntry);
91
92
93 TkTestUtils.verifyAggregateHourSums("admin","Pre-Check", new HashMap<String,BigDecimal>() {{put("OVT", BigDecimal.ZERO);put("REG", new BigDecimal(18));}},aggregate,2);
94
95
96
97 tdoc.setTimeBlocks(blocks);
98 TkServiceLocator.getDailyOvertimeRuleService().processDailyOvertimeRules(tdoc, aggregate);
99
100
101 TkTestUtils.verifyAggregateHourSums("admin","Post Rules Check", new HashMap<String,BigDecimal>() {{put("OVT", new BigDecimal(0));put("REG", new BigDecimal(18));}},aggregate,2);
102 }
103
104
105 @SuppressWarnings("serial")
106 @Test
107 public void testDailyOvertimeSimpleCase() throws Exception {
108 Long jobNumber = 30L;
109 Long workArea = 30L;
110 Long task = 30L;
111 String groupKey = "IU-IN";
112 createDailyOvertimeRule("IU-IN", "REG", "OVT",
113
114 "BW", "TEST-DEPT", workArea,
115 task, new BigDecimal(8), new BigDecimal("15.0"), null);
116
117
118
119 DateTimeZone zone = HrServiceLocator.getTimezoneService().getTargetUserTimezoneWithFallback();
120 DateTime start = new DateTime(2010, 3, 29, 14, 0, 0, 0, TKUtils.getSystemDateTimeZone());
121 TimesheetDocument tdoc = TkTestUtils.populateBlankTimesheetDocument(start, "admin");
122 DateTime tbStart = new DateTime(2010, 3, 29, 14, 0, 0, 0, zone);
123 List<TimeBlock> blocks = new ArrayList<TimeBlock>();
124 CalendarEntry payCalendarEntry = HrServiceLocator.getCalendarEntryService().getCurrentCalendarDates("admin", start);
125 blocks.addAll(TkTestUtils.createUniformTimeBlocks(tbStart, 2, new BigDecimal("4"), "REG", jobNumber, workArea, task, groupKey, tdoc.getDocumentId()));
126 blocks.addAll(TkTestUtils.createUniformTimeBlocks(tbStart.plusHours(4).plusMinutes(15), 2, new BigDecimal("5"), "REG", jobNumber, workArea, task, groupKey, tdoc.getDocumentId()));
127 TkTimeBlockAggregate aggregate = new TkTimeBlockAggregate(blocks, payCalendarEntry);
128
129
130 TkTestUtils.verifyAggregateHourSums("admin","Pre-Check", new HashMap<String,BigDecimal>() {{put("OVT", BigDecimal.ZERO);put("REG", new BigDecimal(18));}},aggregate,2);
131
132
133
134 tdoc.setTimeBlocks(blocks);
135 TkServiceLocator.getDailyOvertimeRuleService().processDailyOvertimeRules(tdoc, aggregate);
136
137
138 TkTestUtils.verifyAggregateHourSums("admin","Post Rules Check", new HashMap<String,BigDecimal>() {{put("OVT", new BigDecimal(2));put("REG", new BigDecimal(16));}},aggregate,2);
139 }
140
141 @Test
142 public void testRuleCreationAndRetrieval() throws Exception {
143 Long workArea = 0L;
144 Long task = 30L;
145 createDailyOvertimeRule("IU-IN", "REG", "OVT",
146
147 "BW", "TEST-DEPT", workArea,
148 task, new BigDecimal(8), new BigDecimal("0.25"), null);
149 DailyOvertimeRule rule = TkServiceLocator.getDailyOvertimeRuleService().getDailyOvertimeRule("IU-IN",
150 "BW", "TEST-DEPT", workArea, JAN_AS_OF_DATE.toLocalDate());
151 Assert.assertNotNull("Rule not created.", rule);
152 }
153
154 @Test
155 public void testSearchDailyOvertimeRules() throws Exception {
156 createDailyOvertimeRule("IU-IN", "REG", "OVT",
157
158 "BW", "TEST-DEPT", 30L, 30L, new BigDecimal(8), new BigDecimal("0.10"), null);
159 createDailyOvertimeRule("IU-IN", "REG", "OVT",
160
161 "BW", "TEST-DEPT5", 5555L, 30L, new BigDecimal(8), new BigDecimal("0.10"), null);
162
163
164
165
166
167
168 }
169
170 }