View Javadoc

1   /**
2    * Copyright 2004-2013 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.lm.leave.approval.service;
17  
18  import java.math.BigDecimal;
19  import java.text.DateFormat;
20  import java.text.ParseException;
21  import java.text.SimpleDateFormat;
22  import java.util.ArrayList;
23  import java.util.Date;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.commons.collections.CollectionUtils;
28  import org.junit.Assert;
29  import org.junit.Test;
30  import org.kuali.hr.lm.leaveblock.LeaveBlock;
31  import org.kuali.hr.lm.workflow.LeaveCalendarDocumentHeader;
32  import org.kuali.hr.test.KPMETestCase;
33  import org.kuali.hr.time.approval.web.ApprovalLeaveSummaryRow;
34  import org.kuali.hr.time.calendar.CalendarEntries;
35  import org.kuali.hr.time.service.base.TkServiceLocator;
36  
37  public class LeaveApprovalServiceTest extends KPMETestCase {
38  	
39  	private static final DateFormat DATE_FORMAT = new SimpleDateFormat("MM/dd/yy");
40  	
41  	@Test
42  	public void testGetLeaveApprovalSummaryRows() {
43  		CalendarEntries ce = TkServiceLocator.getCalendarEntriesService().getCalendarEntries("55");
44  		List<Date> leaveSummaryDates = TkServiceLocator.getLeaveSummaryService().getLeaveSummaryDates(ce);
45  		List<String> testPrincipalIds = new ArrayList<String>();
46  		testPrincipalIds.add("admin");
47  		List<ApprovalLeaveSummaryRow> rows = TkServiceLocator.getLeaveApprovalService().getLeaveApprovalSummaryRows(testPrincipalIds, ce, leaveSummaryDates);
48  		Assert.assertTrue("Rows should not be empty. ", CollectionUtils.isNotEmpty(rows));
49  		
50  		ApprovalLeaveSummaryRow aRow = rows.get(0);
51  		Map<Date, Map<String, BigDecimal>> aMap = aRow.getEarnCodeLeaveHours();
52  		Assert.assertTrue("Leave Approval Summary Rows should have 14 items, not " + aMap.size(), aMap.size() == 14);
53  	}
54  	
55  	@Test
56  	public void testGetPrincipalDocumentHeader() {
57  		CalendarEntries ce = TkServiceLocator.getCalendarEntriesService().getCalendarEntries("55");
58  		List<String> testPrincipalIds = new ArrayList<String>();
59  		testPrincipalIds.add("admin");
60  		Map<String, LeaveCalendarDocumentHeader> lvCalHdr = TkServiceLocator.getLeaveApprovalService().getPrincipalDocumentHeader(testPrincipalIds, ce.getBeginPeriodDateTime(), ce.getEndPeriodDateTime());
61  		Assert.assertTrue("Header should not be empty. ", CollectionUtils.isNotEmpty(lvCalHdr.values()));
62  
63  	}
64  	
65  	@Test
66  	public void testGetEarnCodeLeaveHours() throws Exception {
67  		CalendarEntries ce = TkServiceLocator.getCalendarEntriesService().getCalendarEntries("55");
68  		List<Date> leaveSummaryDates = TkServiceLocator.getLeaveSummaryService().getLeaveSummaryDates(ce);
69  		
70  		List<LeaveBlock> lbList = TkServiceLocator.getLeaveBlockService().getLeaveBlocks("admin", ce.getBeginPeriodDateTime(), ce.getEndPeriodDateTime());
71  		Assert.assertTrue("Leave Block list should not be empty. ", CollectionUtils.isNotEmpty(lbList));
72  		Map<Date, Map<String, BigDecimal>> aMap = TkServiceLocator.getLeaveApprovalService().getEarnCodeLeaveHours(lbList, leaveSummaryDates);
73  		
74  		Assert.assertTrue("Map should have 14 entries, not " + aMap.size(), aMap.size() == 14);
75  		Map<String, BigDecimal> dayMap = aMap.get(DATE_FORMAT.parse("03/05/2012"));
76  		Assert.assertTrue("Map on day 03/05 should have 1 entries, not " + dayMap.size(), dayMap.size() == 1);
77  		Assert.assertTrue("EC on day 03/05 should have 8 hours, not " + dayMap.get("EC6|P|AS"), dayMap.get("EC6|P|AS").equals(new BigDecimal(8)));
78  	}
79  	
80  	@Test
81  	public void testGetAccrualCategoryLeaveHours() throws Exception {
82  		CalendarEntries ce = TkServiceLocator.getCalendarEntriesService().getCalendarEntries("55");
83  		List<Date> leaveSummaryDates = TkServiceLocator.getLeaveSummaryService().getLeaveSummaryDates(ce);
84  		
85  		List<LeaveBlock> lbList = TkServiceLocator.getLeaveBlockService().getLeaveBlocks("admin", ce.getBeginPeriodDateTime(), ce.getEndPeriodDateTime());
86  		Assert.assertTrue("Leave Block list should not be empty. ", CollectionUtils.isNotEmpty(lbList));
87  		Map<Date, Map<String, BigDecimal>> aMap = TkServiceLocator.getLeaveApprovalService().getAccrualCategoryLeaveHours(lbList, leaveSummaryDates);
88  		
89  		Assert.assertTrue("Map should have 14 entries, not " + aMap.size(), aMap.size() == 14);
90  		Map<String, BigDecimal> dayMap = aMap.get(DATE_FORMAT.parse("03/05/2012"));
91  		Assert.assertTrue("Map on day 03/05 should have 1 entries, not " + dayMap.size(), dayMap.size() == 1);
92  		Assert.assertTrue("testAC on day 03/05 should have 8 hours, not " + dayMap.get("testAC"), dayMap.get("testAC").equals(new BigDecimal(8)));
93  	}
94  	
95  	@Test
96  	public void testGetLeavePrincipalIdsWithSearchCriteria() throws ParseException {
97  		List<String> workAreaList = new ArrayList<String>();
98  		String calendarGroup = "leaveCal";
99  		java.sql.Date beginDate = new java.sql.Date(DATE_FORMAT.parse("03/01/2012").getTime());
100 		java.sql.Date endDate = new java.sql.Date(DATE_FORMAT.parse("03/30/2012").getTime());
101 		
102 		List<String> idList = TkServiceLocator.getLeaveApprovalService()
103 			.getLeavePrincipalIdsWithSearchCriteria(workAreaList, calendarGroup, endDate, beginDate, endDate);		
104 		Assert.assertTrue("There should be 0 principal ids when searching with empty workarea list, not " + idList.size(), idList.isEmpty());
105 		
106 		workAreaList.add("1111");
107 		workAreaList.add("2222");
108 		idList = TkServiceLocator.getLeaveApprovalService()
109 			.getLeavePrincipalIdsWithSearchCriteria(workAreaList, calendarGroup, endDate, beginDate, endDate);		
110 		Assert.assertTrue("There should be 2 principal ids when searching with both workareas, not " + idList.size(), idList.size() == 2);
111 		// there's an principal id '1033' in setup that is not eligible for leave, so it should not be in the search results
112 		for(String anId : idList) {
113 			if(!(anId.equals("1011") || anId.equals("1022"))) {
114 				Assert.fail("PrincipalIds searched with both workareas should be either '1011' or '1022', not " + anId);
115 			}
116 		}
117 		
118 		workAreaList = new ArrayList<String>();
119 		workAreaList.add("1111");
120 		idList = TkServiceLocator.getLeaveApprovalService()
121 			.getLeavePrincipalIdsWithSearchCriteria(workAreaList, calendarGroup, endDate, beginDate, endDate);		
122 		Assert.assertTrue("There should be 1 principal ids for workArea '1111', not " + idList.size(), idList.size() == 1);
123 		Assert.assertTrue("Principal id for workArea '1111' should be principalA, not " + idList.get(0), idList.get(0).equals("1011"));
124 		
125 		workAreaList = new ArrayList<String>();
126 		workAreaList.add("2222");
127 		idList = TkServiceLocator.getLeaveApprovalService()
128 			.getLeavePrincipalIdsWithSearchCriteria(workAreaList, calendarGroup, endDate, beginDate, endDate);		
129 		Assert.assertTrue("There should be 1 principal ids for workArea '2222', not " + idList.size(), idList.size() == 1);
130 		Assert.assertTrue("Principal id for workArea '2222' should be principalB, not " + idList.get(0), idList.get(0).equals("1022"));
131 	}
132 
133 
134 }