View Javadoc

1   /**
2    * Copyright 2004-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  		// Create Time Blocks (2 days, 2 blocks on each day, 15 minute gap between blocks, 4 hours first block, 5 the next.
72  		// Should end up with 2 hours total OVT.
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  		// Verify pre-Rule Run
81  		TkTestUtils.verifyAggregateHourSums("Pre-Check", new HashMap<String,BigDecimal>() {{put("OVT", BigDecimal.ZERO);put("REG", new BigDecimal(18));}},aggregate,2);
82  
83  		// Run Rule
84  		TimesheetDocument tdoc = TkTestUtils.populateBlankTimesheetDocument(new Date(start.getMillis()));
85  		tdoc.setTimeBlocks(blocks);
86  		TkServiceLocator.getDailyOvertimeRuleService().processDailyOvertimeRules(tdoc, aggregate);
87  
88  		// Verify post-Rule Run
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 		// Create Time Blocks (2 days, 2 blocks on each day, 15 minute gap between blocks, 4 hours first block, 5 the next.
103 		// Should end up with 2 hours total OVT.
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 		// Verify pre-Rule Run
112 		TkTestUtils.verifyAggregateHourSums("Pre-Check", new HashMap<String,BigDecimal>() {{put("OVT", BigDecimal.ZERO);put("REG", new BigDecimal(18));}},aggregate,2);
113 
114 		// Run Rule
115 		TimesheetDocument tdoc = TkTestUtils.populateBlankTimesheetDocument(new Date(start.getMillis()));
116 		tdoc.setTimeBlocks(blocks);
117 		TkServiceLocator.getDailyOvertimeRuleService().processDailyOvertimeRules(tdoc, aggregate);
118 
119 		// Verify post-Rule Run
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 }