View Javadoc
1   /**
2    * Copyright 2004-2014 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.util.ArrayList;
20  import java.util.HashMap;
21  import java.util.List;
22  
23  import org.joda.time.DateTime;
24  import org.junit.Assert;
25  import org.junit.Test;
26  import org.kuali.hr.KPMEWebTestCase;
27  import org.kuali.kpme.core.FunctionalTest;
28  import org.kuali.kpme.core.calendar.entry.CalendarEntry;
29  import org.kuali.kpme.core.service.HrServiceLocator;
30  import org.kuali.kpme.core.util.TKUtils;
31  import org.kuali.kpme.tklm.time.rules.overtime.daily.DailyOvertimeRule;
32  import org.kuali.kpme.tklm.time.rules.overtime.daily.service.DailyOvertimeRuleService;
33  import org.kuali.kpme.tklm.time.service.TkServiceLocator;
34  import org.kuali.kpme.tklm.time.timeblock.TimeBlock;
35  import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
36  import org.kuali.kpme.tklm.time.util.TkTimeBlockAggregate;
37  import org.kuali.kpme.tklm.utils.TkTestUtils;
38  
39  @FunctionalTest
40  public class DailyOvertimeRuleServiceTest extends KPMEWebTestCase {
41  
42  	public static final String USER_PRINCIPAL_ID = "admin";
43  	private DateTime JAN_AS_OF_DATE = new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
44  
45  
46  	private void createDailyOvertimeRule(String fromEarnGroup, String earnCode, String location, String paytype, String dept, Long workArea, Long task, BigDecimal minHours, BigDecimal maxGap, String overtimePref) {
47  		DailyOvertimeRuleService service = TkServiceLocator.getDailyOvertimeRuleService();
48  		DailyOvertimeRule rule = new DailyOvertimeRule();
49  
50  		rule.setEffectiveLocalDate(JAN_AS_OF_DATE.toLocalDate());
51  		rule.setFromEarnGroup(fromEarnGroup);
52  		rule.setEarnCode(earnCode);
53  		rule.setLocation(location);
54  		rule.setPaytype(paytype);
55  		rule.setDept(dept);
56  		rule.setWorkArea(workArea);
57  		rule.setMaxGap(maxGap);
58  		rule.setMinHours(minHours);
59  		rule.setActive(true);
60  
61  		service.saveOrUpdate(rule);
62  	}
63  
64  
65  	@SuppressWarnings("serial")
66  	@Test
67  	public void testDailyOvertimeGapExceeded() throws Exception {
68  		Long jobNumber = 30L;
69  		Long workArea = 30L;
70  		Long task = 30L;
71  		createDailyOvertimeRule("REG", "OVT", "SD1", "BW", "TEST-DEPT", workArea,
72  				task, new BigDecimal(8), new BigDecimal("0.10"), null);
73  		// Create Time Blocks (2 days, 2 blocks on each day, 15 minute gap between blocks, 4 hours first block, 5 the next.
74  		// Should end up with 2 hours total OVT.
75  		DateTime start = new DateTime(2010, 3, 29, 14, 0, 0, 0, TKUtils.getSystemDateTimeZone());
76  		List<TimeBlock> blocks = new ArrayList<TimeBlock>();
77  		CalendarEntry payCalendarEntry = HrServiceLocator.getCalendarEntryService().getCurrentCalendarDates("admin", start);
78  		blocks.addAll(TkTestUtils.createUniformTimeBlocks(start, 2, new BigDecimal("4"), "REG", jobNumber, workArea, task));
79  		blocks.addAll(TkTestUtils.createUniformTimeBlocks(start.plusHours(4).plusMinutes(15), 2, new BigDecimal("5"), "REG", jobNumber, workArea, task));
80  		TkTimeBlockAggregate aggregate = new TkTimeBlockAggregate(blocks, payCalendarEntry);
81  
82  		// Verify pre-Rule Run
83  		TkTestUtils.verifyAggregateHourSums("Pre-Check", new HashMap<String,BigDecimal>() {{put("OVT", BigDecimal.ZERO);put("REG", new BigDecimal(18));}},aggregate,2);
84  
85  		// Run Rule
86  		TimesheetDocument tdoc = TkTestUtils.populateBlankTimesheetDocument(start, "admin");
87  		tdoc.setTimeBlocks(blocks);
88  		TkServiceLocator.getDailyOvertimeRuleService().processDailyOvertimeRules(tdoc, aggregate);
89  
90  		// Verify post-Rule Run
91  		TkTestUtils.verifyAggregateHourSums("Post Rules Check", new HashMap<String,BigDecimal>() {{put("OVT", new BigDecimal(0));put("REG", new BigDecimal(18));}},aggregate,2);
92  	}
93  
94  
95  	@SuppressWarnings("serial")
96  	@Test
97  	public void testDailyOvertimeSimpleCase() throws Exception {
98  		Long jobNumber = 30L;
99  		Long workArea = 30L;
100 		Long task = 30L;
101 		createDailyOvertimeRule("REG", "OVT", "SD1", "BW", "TEST-DEPT", workArea,
102 				task, new BigDecimal(8), new BigDecimal("15.0"), null);
103 
104 		// Create Time Blocks (2 days, 2 blocks on each day, 15 minute gap between blocks, 4 hours first block, 5 the next.
105 		// Should end up with 2 hours total OVT.
106 		DateTime start = new DateTime(2010, 3, 29, 14, 0, 0, 0, TKUtils.getSystemDateTimeZone());
107 		List<TimeBlock> blocks = new ArrayList<TimeBlock>();
108 		CalendarEntry payCalendarEntry = HrServiceLocator.getCalendarEntryService().getCurrentCalendarDates("admin", start);
109 		blocks.addAll(TkTestUtils.createUniformTimeBlocks(start, 2, new BigDecimal("4"), "REG", jobNumber, workArea, task));
110 		blocks.addAll(TkTestUtils.createUniformTimeBlocks(start.plusHours(4).plusMinutes(15), 2, new BigDecimal("5"), "REG", jobNumber, workArea, task));
111 		TkTimeBlockAggregate aggregate = new TkTimeBlockAggregate(blocks, payCalendarEntry);
112 
113 		// Verify pre-Rule Run
114 		TkTestUtils.verifyAggregateHourSums("Pre-Check", new HashMap<String,BigDecimal>() {{put("OVT", BigDecimal.ZERO);put("REG", new BigDecimal(18));}},aggregate,2);
115 
116 		// Run Rule
117 		TimesheetDocument tdoc = TkTestUtils.populateBlankTimesheetDocument(start, "admin");
118 		tdoc.setTimeBlocks(blocks);
119 		TkServiceLocator.getDailyOvertimeRuleService().processDailyOvertimeRules(tdoc, aggregate);
120 
121 		// Verify post-Rule Run
122 		TkTestUtils.verifyAggregateHourSums("Post Rules Check", new HashMap<String,BigDecimal>() {{put("OVT", new BigDecimal(2));put("REG", new BigDecimal(16));}},aggregate,2);
123 	}
124 
125 	@Test
126 	public void testRuleCreationAndRetrieval() throws Exception {
127 		Long workArea = 0L;
128 		Long task = 30L;
129 		createDailyOvertimeRule("REG", "OVT", "SD1", "BW", "TEST-DEPT", workArea,
130 				task, new BigDecimal(8), new BigDecimal("0.25"), null);
131 		DailyOvertimeRule rule = TkServiceLocator.getDailyOvertimeRuleService().getDailyOvertimeRule("SD1", "BW", "TEST-DEPT", workArea, JAN_AS_OF_DATE.toLocalDate());
132 		Assert.assertNotNull("Rule not created.", rule);
133 	}
134 	
135 	@Test
136 	public void testSearchDailyOvertimeRules() throws Exception {
137 		createDailyOvertimeRule("REG", "OVT", "SD1", "BL", "TEST-DEPT", 30L, 30L, new BigDecimal(8), new BigDecimal("0.10"), null);
138 		createDailyOvertimeRule("REG", "OVT", "SD1", "BL", "TEST-DEPT5", 5555L, 30L, new BigDecimal(8), new BigDecimal("0.10"), null);
139 		
140 		List<DailyOvertimeRule> allResults = TkServiceLocator.getDailyOvertimeRuleService().getDailyOvertimeRules("admin", null, null, null, null, null, "Y", "N");
141 		Assert.assertEquals("Search returned the wrong number of results.", 2, allResults.size());
142 		
143 		List<DailyOvertimeRule> restrictedResults = TkServiceLocator.getDailyOvertimeRuleService().getDailyOvertimeRules("fran", null, null, null, null, null, "Y", "N");
144 		Assert.assertEquals("Search returned the wrong number of results.", 0, restrictedResults.size());
145 	}
146 
147 }