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.time.permissions;
017    
018    import org.junit.After;
019    import org.junit.Assert;
020    import org.junit.Before;
021    import org.junit.Test;
022    import org.kuali.hr.lm.leaveblock.LeaveBlock;
023    import org.kuali.hr.test.KPMETestCase;
024    import org.kuali.hr.time.principal.PrincipalHRAttributes;
025    import org.kuali.hr.time.service.base.TkServiceLocator;
026    import org.kuali.hr.time.util.TKUser;
027    import org.kuali.hr.time.util.TKUtils;
028    import org.kuali.rice.kim.api.identity.Person;
029    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
030    import org.kuali.rice.krad.service.KRADServiceLocator;
031    
032    public class TkPermissionServiceTest extends KPMETestCase {
033            
034            @Before
035            public void setUp() throws Exception{
036                    super.setUp();
037                    // change taget person to a non-admin
038            String testUserId = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName("eric").getPrincipalId();
039                TKUser.setTargetPerson(testUserId);
040                PrincipalHRAttributes phra = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalHRAttributes("2");
041                phra.setLeaveCalendar("BWS-LM");
042                phra.setLeaveCalObj(TkServiceLocator.getCalendarService().getCalendarByGroup("BWS-LM"));
043                KRADServiceLocator.getBusinessObjectService().save(phra);
044            }
045    
046            @After
047            public void tearDown() throws Exception {
048                    super.tearDown();
049            String testUserId = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName("admin").getPrincipalId();
050            TKUser.setTargetPerson(testUserId);
051                PrincipalHRAttributes phra = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalHRAttributes("2");
052                phra.setLeaveCalendar(null);
053                phra.setLeaveCalObj(null);
054                KRADServiceLocator.getBusinessObjectService().save(phra);
055            }
056            
057            @Test
058            // added this brand new test for kpme-2109 so this test does not cover all existing scenarios,
059            // only system scheduled time off usage leave blocks that can be banked or transferred
060            public void testCanDeleteLeaveBlockForSSTOUsageLB() {
061                    // leave block 6000 is a bankable ssto usage block that is on current leave calendar, 
062                    // ssto 2000's unused time is "B"
063                    LeaveBlock lb = TkServiceLocator.getLeaveBlockService().getLeaveBlock("6000");
064                    lb.setLeaveDate(TKUtils.getCurrentDate());
065                    boolean deleteFlag = TkServiceLocator.getPermissionsService().canDeleteLeaveBlock(lb);
066                    Assert.assertTrue("Leave Block 6000 should be deletable", deleteFlag);
067                    
068                    // leave block 6001 is a ssto usage block that can be transferred, ssto 2001's unused time is "T"
069                    lb = TkServiceLocator.getLeaveBlockService().getLeaveBlock("6001");
070                    lb.setLeaveDate(TKUtils.getCurrentDate());
071                    deleteFlag = TkServiceLocator.getPermissionsService().canDeleteLeaveBlock(lb);
072                    Assert.assertTrue("Leave Block 6001 should be deletable", deleteFlag);
073                    
074                    // leave block 6002 is a ssto usage block that is not on current leave calendar
075                    lb = TkServiceLocator.getLeaveBlockService().getLeaveBlock("6002");
076                    deleteFlag = TkServiceLocator.getPermissionsService().canDeleteLeaveBlock(lb);
077                    Assert.assertFalse("Leave Block 6002 should NOT be deletable", deleteFlag);
078                    
079                    // leave block 6003 is a ssto accrual block, not a usage, it's leave amount is 8
080                    lb = TkServiceLocator.getLeaveBlockService().getLeaveBlock("6003");
081                    lb.setLeaveDate(TKUtils.getCurrentDate());
082                    deleteFlag = TkServiceLocator.getPermissionsService().canDeleteLeaveBlock(lb);
083                    Assert.assertFalse("Leave Block 6003 should NOT be deletable", deleteFlag);
084                    
085                    
086            }
087    }