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.department.lunch.rule;
17  
18  import java.math.BigDecimal;
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.apache.commons.lang.StringUtils;
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.util.HrConstants;
29  import org.kuali.kpme.core.util.TKUtils;
30  import org.kuali.kpme.tklm.api.time.timeblock.TimeBlock;
31  import org.kuali.kpme.tklm.api.time.timehourdetail.TimeHourDetail;
32  import org.kuali.kpme.tklm.time.rules.lunch.department.DeptLunchRule;
33  import org.kuali.kpme.tklm.time.service.TkServiceLocator;
34  import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
35  import org.kuali.kpme.tklm.utils.TkTestUtils;
36  import org.kuali.rice.krad.service.KRADServiceLocator;
37  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
38  
39  @FunctionalTest
40  public class DepartmentLunchRuleTest extends KPMEWebTestCase {
41      private DateTime JAN_AS_OF_DATE = new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
42  
43  	@Test
44  	public void testDepartmentLunchRuleFetch() throws Exception{
45  		DeptLunchRule deptLunchRule = new DeptLunchRule();
46  		deptLunchRule.setActive(true);
47  		deptLunchRule.setDept("TEST");
48  		deptLunchRule.setWorkArea(1234L);
49  		deptLunchRule.setEffectiveLocalDate(JAN_AS_OF_DATE.toLocalDate());
50  		deptLunchRule.setJobNumber(0L);
51  		deptLunchRule.setPrincipalId("admin");
52  		deptLunchRule.setDeductionMins(new BigDecimal(30));
53  		deptLunchRule.setShiftHours(new BigDecimal(6));
54  		deptLunchRule.setTkDeptLunchRuleId("1001");
55          deptLunchRule.setUserPrincipalId("admin");
56          deptLunchRule.setGroupKeyCode("IU-BL");
57          
58  		KRADServiceLocatorWeb.getLegacyDataAdapter().save(deptLunchRule);
59  
60  		deptLunchRule = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRule("TEST",
61  											1234L, "admin", 0L, "IU-BL", JAN_AS_OF_DATE.toLocalDate());
62  		Assert.assertTrue("dept lunch rule fetched ", deptLunchRule!=null);
63  
64  	}
65  
66  	/**
67  	 * Test if the minute deduction rule is applied correctly if there is a valid department lunch rule
68  	 */
69  
70  	@Test
71  	public void testDepartmentLunchRule() throws Exception {
72  		// create a dept lunch rule
73  		DeptLunchRule deptLunchRule = new DeptLunchRule();
74  		deptLunchRule.setActive(true);
75  		deptLunchRule.setDept("TEST-DEPT");
76  		deptLunchRule.setWorkArea(1234L);
77  		deptLunchRule.setEffectiveLocalDate(JAN_AS_OF_DATE.toLocalDate());
78  		deptLunchRule.setJobNumber(1L);
79  		deptLunchRule.setPrincipalId("edna");
80  		deptLunchRule.setDeductionMins(new BigDecimal(30));
81  		deptLunchRule.setShiftHours(new BigDecimal(6));
82  		deptLunchRule.setTkDeptLunchRuleId("1001");
83          deptLunchRule.setUserPrincipalId("admin");
84          deptLunchRule.setGroupKeyCode("IU-BL");
85          
86  		KRADServiceLocatorWeb.getLegacyDataAdapter().save(deptLunchRule);
87  
88  		deptLunchRule = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRule("TEST-DEPT",
89  											1234L, "edna", 1L, "IU-BL", JAN_AS_OF_DATE.toLocalDate());
90  		Assert.assertTrue("dept lunch rule fetched ", deptLunchRule!=null);
91  
92  		TimesheetDocument doc = TkTestUtils.populateTimesheetDocument(JAN_AS_OF_DATE, "edna");
93  
94          List<TimeBlock> timeBlocks = new ArrayList<TimeBlock>(doc.getTimeBlocks());
95  		for(TimeBlock tb : doc.getTimeBlocks()){
96              TimeBlock.Builder b = TimeBlock.Builder.create(tb);
97  			b.setClockLogCreated(true);
98              timeBlocks.add(b.build());
99  		}
100 		for(TimeBlock tb : timeBlocks) {
101 			if(tb.getHours().compareTo(deptLunchRule.getShiftHours()) == 1) {
102 				for(TimeHourDetail thd : tb.getTimeHourDetails()){
103 					// 	this assumes the hours for the dummy timeblocks are always 10
104 					if(!StringUtils.equals(thd.getEarnCode(), HrConstants.LUNCH_EARN_CODE)){
105 						Assert.assertEquals(new BigDecimal(9.50).setScale(2), tb.getHours());
106 					}
107 				}
108 			}
109 		}
110 
111 	}
112 
113 	@Test
114 	public void testSearchDepartmentLunchRules() throws Exception {
115 		//List<DeptLunchRule> allResults = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRules("admin", null, null, null, null, "IU-BL", null, null, "Y", "N");
116 		//Assert.assertEquals("Search returned the correct number of results.", 2, allResults.size());
117 		
118 		//List<DeptLunchRule> restrictedResults = TkServiceLocator.getDepartmentLunchRuleService().getDepartmentLunchRules("fran", null, null, null, null, "IU-BL", null, null, "Y", "N");
119 		//Assert.assertEquals("Search returned the correct number of results.", 0, restrictedResults.size());
120 	}
121 	
122 }