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.lm.workflow.LeaveCalendarDocumentHeader;
032    import org.kuali.hr.test.KPMETestCase;
033    import org.kuali.hr.time.approval.web.ApprovalLeaveSummaryRow;
034    import org.kuali.hr.time.calendar.CalendarEntries;
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> testPrincipalIds = new ArrayList<String>();
046                    testPrincipalIds.add("admin");
047                    List<ApprovalLeaveSummaryRow> rows = TkServiceLocator.getLeaveApprovalService().getLeaveApprovalSummaryRows(testPrincipalIds, ce, leaveSummaryDates);
048                    Assert.assertTrue("Rows should not be empty. ", CollectionUtils.isNotEmpty(rows));
049                    
050                    ApprovalLeaveSummaryRow aRow = rows.get(0);
051                    Map<Date, Map<String, BigDecimal>> aMap = aRow.getEarnCodeLeaveHours();
052                    Assert.assertTrue("Leave Approval Summary Rows should have 14 items, not " + aMap.size(), aMap.size() == 14);
053            }
054            
055            @Test
056            public void testGetPrincipalDocumentHeader() {
057                    CalendarEntries ce = TkServiceLocator.getCalendarEntriesService().getCalendarEntries("55");
058                    List<String> testPrincipalIds = new ArrayList<String>();
059                    testPrincipalIds.add("admin");
060                    Map<String, LeaveCalendarDocumentHeader> lvCalHdr = TkServiceLocator.getLeaveApprovalService().getPrincipalDocumentHeader(testPrincipalIds, ce.getBeginPeriodDateTime(), ce.getEndPeriodDateTime());
061                    Assert.assertTrue("Header should not be empty. ", CollectionUtils.isNotEmpty(lvCalHdr.values()));
062    
063            }
064            
065            @Test
066            public void testGetEarnCodeLeaveHours() throws Exception {
067                    CalendarEntries ce = TkServiceLocator.getCalendarEntriesService().getCalendarEntries("55");
068                    List<Date> leaveSummaryDates = TkServiceLocator.getLeaveSummaryService().getLeaveSummaryDates(ce);
069                    
070                    List<LeaveBlock> lbList = TkServiceLocator.getLeaveBlockService().getLeaveBlocks("admin", ce.getBeginPeriodDateTime(), ce.getEndPeriodDateTime());
071                    Assert.assertTrue("Leave Block list should not be empty. ", CollectionUtils.isNotEmpty(lbList));
072                    Map<Date, Map<String, BigDecimal>> aMap = TkServiceLocator.getLeaveApprovalService().getEarnCodeLeaveHours(lbList, leaveSummaryDates);
073                    
074                    Assert.assertTrue("Map should have 14 entries, not " + aMap.size(), aMap.size() == 14);
075                    Map<String, BigDecimal> dayMap = aMap.get(DATE_FORMAT.parse("03/05/2012"));
076                    Assert.assertTrue("Map on day 03/05 should have 1 entries, not " + dayMap.size(), dayMap.size() == 1);
077                    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)));
078            }
079            
080            @Test
081            public void testGetAccrualCategoryLeaveHours() throws Exception {
082                    CalendarEntries ce = TkServiceLocator.getCalendarEntriesService().getCalendarEntries("55");
083                    List<Date> leaveSummaryDates = TkServiceLocator.getLeaveSummaryService().getLeaveSummaryDates(ce);
084                    
085                    List<LeaveBlock> lbList = TkServiceLocator.getLeaveBlockService().getLeaveBlocks("admin", ce.getBeginPeriodDateTime(), ce.getEndPeriodDateTime());
086                    Assert.assertTrue("Leave Block list should not be empty. ", CollectionUtils.isNotEmpty(lbList));
087                    Map<Date, Map<String, BigDecimal>> aMap = TkServiceLocator.getLeaveApprovalService().getAccrualCategoryLeaveHours(lbList, leaveSummaryDates);
088                    
089                    Assert.assertTrue("Map should have 14 entries, not " + aMap.size(), aMap.size() == 14);
090                    Map<String, BigDecimal> dayMap = aMap.get(DATE_FORMAT.parse("03/05/2012"));
091                    Assert.assertTrue("Map on day 03/05 should have 1 entries, not " + dayMap.size(), dayMap.size() == 1);
092                    Assert.assertTrue("testAC on day 03/05 should have 8 hours, not " + dayMap.get("testAC"), dayMap.get("testAC").equals(new BigDecimal(8)));
093            }
094            
095            @Test
096            public void testGetLeavePrincipalIdsWithSearchCriteria() throws ParseException {
097                    List<String> workAreaList = new ArrayList<String>();
098                    String calendarGroup = "leaveCal";
099                    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    }