001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.hr.lm.leave.approval.service;
017    
018    import java.math.BigDecimal;
019    import java.text.DateFormat;
020    import java.text.ParseException;
021    import java.text.SimpleDateFormat;
022    import java.util.ArrayList;
023    import java.util.Date;
024    import java.util.List;
025    import java.util.Map;
026    
027    import org.apache.commons.collections.CollectionUtils;
028    import org.junit.Assert;
029    import org.junit.Test;
030    import org.kuali.hr.lm.leaveblock.LeaveBlock;
031    import org.kuali.hr.test.KPMETestCase;
032    import org.kuali.hr.time.approval.web.ApprovalLeaveSummaryRow;
033    import org.kuali.hr.time.calendar.CalendarEntries;
034    import org.kuali.hr.time.person.TKPerson;
035    import org.kuali.hr.time.service.base.TkServiceLocator;
036    
037    public class LeaveApprovalServiceTest extends KPMETestCase {
038            
039            private static final DateFormat DATE_FORMAT = new SimpleDateFormat("MM/dd/yy");
040            
041            @Test
042            public void testGetLeaveApprovalSummaryRows() {
043                    CalendarEntries ce = TkServiceLocator.getCalendarEntriesService().getCalendarEntries("55");
044                    List<Date> leaveSummaryDates = TkServiceLocator.getLeaveSummaryService().getLeaveSummaryDates(ce);
045                    List<String> ids = new ArrayList<String>();
046                    ids.add("admin");
047                    List<TKPerson> persons = TkServiceLocator.getPersonService().getPersonCollection(ids);
048                    
049                    List<ApprovalLeaveSummaryRow> rows = TkServiceLocator.getLeaveApprovalService().getLeaveApprovalSummaryRows(persons, ce, leaveSummaryDates);
050                    Assert.assertTrue("Rows should not be empty. ", CollectionUtils.isNotEmpty(rows));
051                    
052                    ApprovalLeaveSummaryRow aRow = rows.get(0);
053                    Map<Date, Map<String, BigDecimal>> aMap = aRow.getEarnCodeLeaveHours();
054                    Assert.assertTrue("Leave Approval Summary Rows should have 14 items, not " + aMap.size(), aMap.size() == 14);
055            }
056            
057            @Test
058            public void testGetEarnCodeLeaveHours() throws Exception {
059                    CalendarEntries ce = TkServiceLocator.getCalendarEntriesService().getCalendarEntries("55");
060                    List<Date> leaveSummaryDates = TkServiceLocator.getLeaveSummaryService().getLeaveSummaryDates(ce);
061                    
062                    List<LeaveBlock> lbList = TkServiceLocator.getLeaveBlockService().getLeaveBlocks("admin", ce.getBeginPeriodDateTime(), ce.getEndPeriodDateTime());
063                    Assert.assertTrue("Leave Block list should not be empty. ", CollectionUtils.isNotEmpty(lbList));
064                    Map<Date, Map<String, BigDecimal>> aMap = TkServiceLocator.getLeaveApprovalService().getEarnCodeLeaveHours(lbList, leaveSummaryDates);
065                    
066                    Assert.assertTrue("Map should have 14 entries, not " + aMap.size(), aMap.size() == 14);
067                    Map<String, BigDecimal> dayMap = aMap.get(DATE_FORMAT.parse("03/05/2012"));
068                    Assert.assertTrue("Map on day 03/05 should have 1 entries, not " + dayMap.size(), dayMap.size() == 1);
069                    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)));
070            }
071            
072            @Test
073            public void testGetAccrualCategoryLeaveHours() throws Exception {
074                    CalendarEntries ce = TkServiceLocator.getCalendarEntriesService().getCalendarEntries("55");
075                    List<Date> leaveSummaryDates = TkServiceLocator.getLeaveSummaryService().getLeaveSummaryDates(ce);
076                    
077                    List<LeaveBlock> lbList = TkServiceLocator.getLeaveBlockService().getLeaveBlocks("admin", ce.getBeginPeriodDateTime(), ce.getEndPeriodDateTime());
078                    Assert.assertTrue("Leave Block list should not be empty. ", CollectionUtils.isNotEmpty(lbList));
079                    Map<Date, Map<String, BigDecimal>> aMap = TkServiceLocator.getLeaveApprovalService().getAccrualCategoryLeaveHours(lbList, leaveSummaryDates);
080                    
081                    Assert.assertTrue("Map should have 14 entries, not " + aMap.size(), aMap.size() == 14);
082                    Map<String, BigDecimal> dayMap = aMap.get(DATE_FORMAT.parse("03/05/2012"));
083                    Assert.assertTrue("Map on day 03/05 should have 1 entries, not " + dayMap.size(), dayMap.size() == 1);
084                    Assert.assertTrue("testAC on day 03/05 should have 8 hours, not " + dayMap.get("testAC"), dayMap.get("testAC").equals(new BigDecimal(8)));
085            }
086            
087            @Test
088            public void testGetLeavePrincipalIdsWithSearchCriteria() throws ParseException {
089                    List<String> workAreaList = new ArrayList<String>();
090                    String calendarGroup = "leaveCal";
091                    java.sql.Date beginDate = new java.sql.Date(DATE_FORMAT.parse("03/01/2012").getTime());
092                    java.sql.Date endDate = new java.sql.Date(DATE_FORMAT.parse("03/30/2012").getTime());
093                    
094                    List<String> idList = TkServiceLocator.getLeaveApprovalService()
095                            .getLeavePrincipalIdsWithSearchCriteria(workAreaList, calendarGroup, endDate, beginDate, endDate);              
096                    Assert.assertTrue("There should be 0 principal ids when searching with empty workarea list, not " + idList.size(), idList.isEmpty());
097                    
098                    workAreaList.add("1111");
099                    workAreaList.add("2222");
100                    idList = TkServiceLocator.getLeaveApprovalService()
101                            .getLeavePrincipalIdsWithSearchCriteria(workAreaList, calendarGroup, endDate, beginDate, endDate);              
102                    Assert.assertTrue("There should be 2 principal ids when searching with both workareas, not " + idList.size(), idList.size() == 2);
103                    // there's an principal id '1033' in setup that is not eligible for leave, so it should not be in the search results
104                    for(String anId : idList) {
105                            if(!(anId.equals("1011") || anId.equals("1022"))) {
106                                    Assert.fail("PrincipalIds searched with both workareas should be either '1011' or '1022', not " + anId);
107                            }
108                    }
109                    
110                    workAreaList = new ArrayList<String>();
111                    workAreaList.add("1111");
112                    idList = TkServiceLocator.getLeaveApprovalService()
113                            .getLeavePrincipalIdsWithSearchCriteria(workAreaList, calendarGroup, endDate, beginDate, endDate);              
114                    Assert.assertTrue("There should be 1 principal ids for workArea '1111', not " + idList.size(), idList.size() == 1);
115                    Assert.assertTrue("Principal id for workArea '1111' should be principalA, not " + idList.get(0), idList.get(0).equals("1011"));
116                    
117                    workAreaList = new ArrayList<String>();
118                    workAreaList.add("2222");
119                    idList = TkServiceLocator.getLeaveApprovalService()
120                            .getLeavePrincipalIdsWithSearchCriteria(workAreaList, calendarGroup, endDate, beginDate, endDate);              
121                    Assert.assertTrue("There should be 1 principal ids for workArea '2222', not " + idList.size(), idList.size() == 1);
122                    Assert.assertTrue("Principal id for workArea '2222' should be principalB, not " + idList.get(0), idList.get(0).equals("1022"));
123            }
124    
125    }