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.leaveblock.service;
017    
018    
019    import java.util.*;
020    
021    import junit.framework.Assert;
022    
023    import org.joda.time.DateTime;
024    import org.junit.After;
025    import org.junit.Before;
026    import org.junit.Test;
027    import org.kuali.hr.lm.LMConstants;
028    import org.kuali.hr.lm.leaveblock.LeaveBlock;
029    import org.kuali.hr.test.KPMETestCase;
030    import org.kuali.hr.time.service.base.TkServiceLocator;
031    import org.kuali.hr.time.util.TKUtils;
032    
033    public class LeaveBlockServiceImplTest extends KPMETestCase {
034            
035            private String TEST_USER = "admin";
036            private LeaveBlockService leaveBlockService;
037    
038            @Before
039            public void setUp() throws Exception{
040                    super.setUp();
041                    leaveBlockService = TkServiceLocator.getLeaveBlockService();
042            }
043    
044            @After
045            public void tearDown() throws Exception {
046                    super.tearDown();
047            }
048    
049            @Test
050            public void testGetLeaveBlock() {
051                    LeaveBlock leaveBlock = leaveBlockService.getLeaveBlock("1000");
052                    Assert.assertNotNull("Leave Block not found ", leaveBlock);
053            }
054    
055        @Test
056        public void testGetLeaveCarryOverDates() {
057            Map<String, LeaveBlock> dates = leaveBlockService.getLastCarryOverBlocks("admin", null);
058            Assert.assertNotNull("Leave Block not found ", dates);
059        }
060    
061    
062            
063            @Test
064            public void testGetLeaveBlocks(){
065                    // 03/01/2012 to 03/02/2012
066                    Calendar cal = Calendar.getInstance();
067                    cal.setTime(TKUtils.getTimelessDate(null));
068                    cal.set(Calendar.YEAR, 2012);
069                    cal.set(Calendar.MONTH, 2);
070                    cal.set(Calendar.DATE, 1);
071                    
072                    Date beginDate = cal.getTime();
073                    cal.add(Calendar.DATE,1);
074                    Date endDate = cal.getTime();
075                    List<LeaveBlock> leaveBlocks = leaveBlockService.getLeaveBlocks(TEST_USER, beginDate, endDate);
076                    Assert.assertNotNull("Leave blocks not found for user ", leaveBlocks);
077                    Assert.assertTrue("There should be 2 leave blocks, not " + leaveBlocks.size(), leaveBlocks.size()== 2);
078            }
079            
080            @Test
081            public void testGetLeaveBlocksForDocumentId() {
082                    List<LeaveBlock> leaveBlocks = leaveBlockService.getLeaveBlocksForDocumentId("12546");
083                    Assert.assertNotNull("Leave Blocks not foudn for document", leaveBlocks);
084            }
085            
086            @Test
087            public void testGetLeaveBlocksByLeaveRequestStatus(){
088                    String requestStatus = LMConstants.REQUEST_STATUS.PLANNED;
089                    Date currentDate = TKUtils.getTimelessDate(new Date());
090                    List<LeaveBlock> leaveBlocks = leaveBlockService.getLeaveBlocks(TEST_USER, LMConstants.LEAVE_BLOCK_TYPE.LEAVE_CALENDAR, requestStatus, currentDate);
091                    Assert.assertNotNull("Leave Blocks not found of Request status", leaveBlocks);
092            }
093            @Test
094            public void testGetLeaveBlocksForLeaveDate(){
095                    Date leaveDate = TKUtils.getTimelessDate(new Date());
096                    List<LeaveBlock> leaveBlocks = leaveBlockService.getLeaveBlocksForDate(TEST_USER, leaveDate);
097                    Assert.assertNotNull("Leave Blocks not found of Request status", leaveBlocks);
098            }
099            
100            @Test
101            public void testGetLeaveBlocksForTimeCalendar() {
102                    // 03/01/2012 to 03/06/2012
103                    Calendar cal = Calendar.getInstance();
104                    cal.setTime(TKUtils.getTimelessDate(null));
105                    cal.set(Calendar.YEAR, 2012);
106                    cal.set(Calendar.MONTH, 2);
107                    cal.set(Calendar.DATE, 1);
108                    
109                    Date beginDate = cal.getTime();
110                    cal.add(Calendar.DATE,5);
111                    Date endDate = cal.getTime();
112                    List<String> assignmentKeys = new ArrayList<String>();
113                    List<LeaveBlock> leaveBlocks = leaveBlockService.getLeaveBlocksForTimeCalendar(TEST_USER, beginDate, endDate, assignmentKeys);
114                    Assert.assertNotNull("Leave blocks not found for user ", leaveBlocks);
115                    Assert.assertTrue("There should be 6 leave blocks, not " + leaveBlocks.size(), leaveBlocks.size()== 6);
116                    
117                    assignmentKeys.add("0_12345_0");
118                    leaveBlocks = leaveBlockService.getLeaveBlocksForTimeCalendar(TEST_USER, beginDate, endDate, assignmentKeys);
119                    Assert.assertNotNull("Leave blocks not found for user ", leaveBlocks);
120                    Assert.assertTrue("There should be 2 leave blocks, not " + leaveBlocks.size(), leaveBlocks.size()== 2);
121                    
122                    LeaveBlock lb = TkServiceLocator.getLeaveBlockService().getLeaveBlock("1001");
123                    lb.setRequestStatus(LMConstants.REQUEST_STATUS.APPROVED);
124                    TkServiceLocator.getLeaveBlockService().saveLeaveBlock(lb, TEST_USER);
125                    leaveBlocks = leaveBlockService.getLeaveBlocksForTimeCalendar(TEST_USER, beginDate, endDate, assignmentKeys);
126                    Assert.assertTrue("There should be 3 leave blocks, not " + leaveBlocks.size(), leaveBlocks.size()== 3);
127                    Assert.assertTrue("Approved leave block should be in the list", leaveBlocks.contains(lb));
128            }
129            
130            @Test
131            public void testGetLeaveBlocksForLeaveCalendar() {
132                    // 03/01/2012 to 03/06/2012
133                    Calendar cal = Calendar.getInstance();
134                    cal.setTime(TKUtils.getTimelessDate(null));
135                    cal.set(Calendar.YEAR, 2012);
136                    cal.set(Calendar.MONTH, 2);
137                    cal.set(Calendar.DATE, 1);
138                    
139                    Date beginDate = cal.getTime();
140                    cal.add(Calendar.DATE,5);
141                    Date endDate = cal.getTime();
142                    List<String> assignmentKeys = new ArrayList<String>();
143                    List<LeaveBlock> leaveBlocks = leaveBlockService.getLeaveBlocksForLeaveCalendar(TEST_USER, beginDate, endDate, assignmentKeys);
144                    Assert.assertNotNull("Leave blocks not found for user ", leaveBlocks);
145                    Assert.assertTrue("There should be 6 leave blocks, not " + leaveBlocks.size(), leaveBlocks.size()== 6);
146                    
147                    assignmentKeys.add("0_12345_0");
148                    leaveBlocks = leaveBlockService.getLeaveBlocksForLeaveCalendar(TEST_USER, beginDate, endDate, assignmentKeys);
149                    Assert.assertNotNull("Leave blocks not found for user ", leaveBlocks);
150                    Assert.assertTrue("There should be 5 leave blocks, not " + leaveBlocks.size(), leaveBlocks.size()== 5);
151                    
152                    assignmentKeys.add("1_12345_0");
153                    leaveBlocks = leaveBlockService.getLeaveBlocksForLeaveCalendar(TEST_USER, beginDate, endDate, assignmentKeys);
154                    Assert.assertNotNull("Leave blocks not found for user ", leaveBlocks);
155                    Assert.assertTrue("There should be 6 leave blocks, not " + leaveBlocks.size(), leaveBlocks.size()== 6);
156            }
157    
158    
159    }