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.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  			//String location, 
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  		//rule.setLocation(location);
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  		createDailyOvertimeRule("IU-IN", "REG", "OVT", 
76  				//"IN", // 05/08 remove location because adding groupkeycode to DailyOvertimeRule // changed from "SD1" to "IN" for changes of adding groupKeyCode to Job
77  				"BW", "TEST-DEPT", workArea,
78  				task, new BigDecimal(8), new BigDecimal("0.10"), null);
79  		// Create Time Blocks (2 days, 2 blocks on each day, 15 minute gap between blocks, 4 hours first block, 5 the next.
80  		// Should end up with 2 hours total OVT.
81          DateTimeZone zone = HrServiceLocator.getTimezoneService().getTargetUserTimezoneWithFallback();
82  		DateTime start = new DateTime(2010, 3, 29, 14, 0, 0, 0, TKUtils.getSystemDateTimeZone());
83          DateTime tbStart = new DateTime(2010, 3, 29, 14, 0, 0, 0, zone);
84  		List<TimeBlock> blocks = new ArrayList<TimeBlock>();
85  		CalendarEntry payCalendarEntry =  HrServiceLocator.getCalendarEntryService().getCurrentCalendarDates("admin", start);
86  		blocks.addAll(TkTestUtils.createUniformTimeBlocks(tbStart, 2, new BigDecimal("4"), "REG", jobNumber, workArea, task));
87  		blocks.addAll(TkTestUtils.createUniformTimeBlocks(tbStart.plusHours(4).plusMinutes(15), 2, new BigDecimal("5"), "REG", jobNumber, workArea, task));
88  		TkTimeBlockAggregate aggregate = new TkTimeBlockAggregate(blocks, payCalendarEntry);
89  
90  		// Verify pre-Rule Run
91  		TkTestUtils.verifyAggregateHourSums("Pre-Check", new HashMap<String,BigDecimal>() {{put("OVT", BigDecimal.ZERO);put("REG", new BigDecimal(18));}},aggregate,2);
92  
93  		// Run Rule
94  		TimesheetDocument tdoc = TkTestUtils.populateBlankTimesheetDocument(start, "admin");
95  		tdoc.setTimeBlocks(blocks);
96  		TkServiceLocator.getDailyOvertimeRuleService().processDailyOvertimeRules(tdoc, aggregate);
97  
98  		// Verify post-Rule Run
99  		TkTestUtils.verifyAggregateHourSums("Post Rules Check", new HashMap<String,BigDecimal>() {{put("OVT", new BigDecimal(0));put("REG", new BigDecimal(18));}},aggregate,2);
100 	}
101 
102 
103 	@SuppressWarnings("serial")
104 	@Test
105 	public void testDailyOvertimeSimpleCase() throws Exception {
106 		Long jobNumber = 30L;
107 		Long workArea = 30L;
108 		Long task = 30L;
109 		createDailyOvertimeRule("IU-IN", "REG", "OVT", 
110 				//"IN", // changed from "SD1" to "IN" for changes of adding groupKeyCode to Job
111 				"BW", "TEST-DEPT", workArea,
112 				task, new BigDecimal(8), new BigDecimal("15.0"), null);
113 
114 		// Create Time Blocks (2 days, 2 blocks on each day, 15 minute gap between blocks, 4 hours first block, 5 the next.
115 		// Should end up with 2 hours total OVT.
116         DateTimeZone zone = HrServiceLocator.getTimezoneService().getTargetUserTimezoneWithFallback();
117 		DateTime start = new DateTime(2010, 3, 29, 14, 0, 0, 0, TKUtils.getSystemDateTimeZone());
118         DateTime tbStart = new DateTime(2010, 3, 29, 14, 0, 0, 0, zone);
119 		List<TimeBlock> blocks = new ArrayList<TimeBlock>();
120 		CalendarEntry payCalendarEntry =  HrServiceLocator.getCalendarEntryService().getCurrentCalendarDates("admin", start);
121 		blocks.addAll(TkTestUtils.createUniformTimeBlocks(tbStart, 2, new BigDecimal("4"), "REG", jobNumber, workArea, task));
122 		blocks.addAll(TkTestUtils.createUniformTimeBlocks(tbStart.plusHours(4).plusMinutes(15), 2, new BigDecimal("5"), "REG", jobNumber, workArea, task));
123 		TkTimeBlockAggregate aggregate = new TkTimeBlockAggregate(blocks, payCalendarEntry);
124 
125 		// Verify pre-Rule Run
126 		TkTestUtils.verifyAggregateHourSums("Pre-Check", new HashMap<String,BigDecimal>() {{put("OVT", BigDecimal.ZERO);put("REG", new BigDecimal(18));}},aggregate,2);
127 
128 		// Run Rule
129 		TimesheetDocument tdoc = TkTestUtils.populateBlankTimesheetDocument(start, "admin");
130 		tdoc.setTimeBlocks(blocks);
131 		TkServiceLocator.getDailyOvertimeRuleService().processDailyOvertimeRules(tdoc, aggregate);
132 
133 		// Verify post-Rule Run
134 		TkTestUtils.verifyAggregateHourSums("Post Rules Check", new HashMap<String,BigDecimal>() {{put("OVT", new BigDecimal(2));put("REG", new BigDecimal(16));}},aggregate,2);
135 	}
136 
137 	@Test
138 	public void testRuleCreationAndRetrieval() throws Exception {
139 		Long workArea = 0L;
140 		Long task = 30L;
141 		createDailyOvertimeRule("IU-IN", "REG", "OVT", 
142 				//"IN", // changed from "SD1" to "IN" for changes of adding groupKeyCode to Job
143 				"BW", "TEST-DEPT", workArea,
144 				task, new BigDecimal(8), new BigDecimal("0.25"), null);
145 		DailyOvertimeRule rule = TkServiceLocator.getDailyOvertimeRuleService().getDailyOvertimeRule("IU-IN", // changed from "SD1" to "IN" for changes of adding groupKeyCode to Job
146 				"BW", "TEST-DEPT", workArea, JAN_AS_OF_DATE.toLocalDate());
147 		Assert.assertNotNull("Rule not created.", rule);
148 	}
149 	
150 	@Test
151 	public void testSearchDailyOvertimeRules() throws Exception {
152 		createDailyOvertimeRule("IU-IN", "REG", "OVT", 
153 				//"IN", // changed from "SD1" to "IN" for changes of adding groupKeyCode to Job
154 				"BW", "TEST-DEPT", 30L, 30L, new BigDecimal(8), new BigDecimal("0.10"), null);
155 		createDailyOvertimeRule("IU-IN", "REG", "OVT", 
156 				//"IN", // changed from "SD1" to "IN" for changes of adding groupKeyCode to Job
157 				"BW", "TEST-DEPT5", 5555L, 30L, new BigDecimal(8), new BigDecimal("0.10"), null);
158 		
159 		//List<DailyOvertimeRule> allResults = TkServiceLocator.getDailyOvertimeRuleService().getDailyOvertimeRules("IU-IN", "admin", null, null, null, null, "Y", "N");
160 		//Assert.assertEquals("Search returned the wrong number of results.", 2, allResults.size());
161 		
162 		//List<DailyOvertimeRule> restrictedResults = TkServiceLocator.getDailyOvertimeRuleService().getDailyOvertimeRules("IU-IN", "fran", null, null, null, null, "Y", "N");
163 		//Assert.assertEquals("Search returned the wrong number of results.", 0, restrictedResults.size());
164 	}
165 
166 }