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.balancetransfer;
017    import static org.junit.Assert.*;
018    
019    import java.math.BigDecimal;
020    import java.sql.Date;
021    import java.util.ArrayList;
022    import java.util.List;
023    
024    import junit.framework.Assert;
025    
026    import org.joda.time.DateTime;
027    import org.joda.time.Interval;
028    import org.junit.Test;
029    import org.kuali.hr.lm.leave.web.LeaveCalendarWSForm;
030    import org.kuali.hr.lm.leaveCalendar.LeaveCalendarWebTestBase;
031    import org.kuali.hr.lm.leaveSummary.LeaveSummary;
032    import org.kuali.hr.lm.leaveSummary.LeaveSummaryRow;
033    import org.kuali.hr.lm.leaveblock.LeaveBlock;
034    import org.kuali.hr.lm.leavecalendar.LeaveCalendarDocument;
035    import org.kuali.hr.lm.leavecalendar.web.LeaveCalendarSubmitForm;
036    import org.kuali.hr.lm.util.LeaveCalendarTestUtils;
037    import org.kuali.hr.test.KPMETestCase;
038    import org.kuali.hr.time.calendar.CalendarEntries;
039    import org.kuali.hr.time.service.base.TkServiceLocator;
040    import org.kuali.hr.time.test.HtmlUnitUtil;
041    import org.kuali.hr.time.test.TkTestConstants;
042    import org.kuali.hr.time.util.TKUtils;
043    import org.kuali.hr.time.util.TkConstants;
044    
045    import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
046    import com.gargoylesoftware.htmlunit.WebClient;
047    import com.gargoylesoftware.htmlunit.html.HtmlPage;
048    
049    
050    public class BalanceTransferTest extends LeaveCalendarWebTestBase {
051            
052        public static final String USER_PRINCIPAL_ID = "admin";
053            private Date JAN_AS_OF_DATE = new Date((new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
054            private BalanceTransfer balanceTransfer;
055            
056            @Override
057            public void setUp() throws Exception {
058                    super.setUp();
059            setBaseDetailURL(TkTestConstants.Urls.LEAVE_CALENDAR_SUBMIT_URL + "?documentId=");
060            balanceTransfer = new BalanceTransfer();
061            balanceTransfer.setTransferAmount(new BigDecimal(20));
062            balanceTransfer.setForfeitedAmount(new BigDecimal(0));
063            balanceTransfer.setAmountTransferred(new BigDecimal(10));
064            balanceTransfer.setAccrualCategoryRule("5000");
065            }
066            
067            @Test
068            public void testTransferWithAccrualRule() throws Exception {
069                    BalanceTransfer btd = new BalanceTransfer();
070                    //btd.setCreditedAccrualCategory(TKTestUtils.creat)
071                    assertTrue("Dummy assertion",true);
072            }
073            
074            @Test
075            public void testAdjustLowerTransferAmount() {
076                    BigDecimal adjustedTransferAmount = new BigDecimal(10);
077                    balanceTransfer = balanceTransfer.adjust(adjustedTransferAmount);
078                    
079                    assertTrue("Transfer Amount not equals", balanceTransfer.getTransferAmount().compareTo(adjustedTransferAmount) == 0);
080                    assertTrue("Forfeited amount not updated", balanceTransfer.getForfeitedAmount().compareTo(new BigDecimal(10)) == 0);
081                    assertTrue(balanceTransfer.getAmountTransferred().compareTo(new BigDecimal(5)) == 0);
082            }
083            
084            @Test
085            public void testAdjustLowerTransferAmountWithForfeiture() {
086                    BigDecimal adjustedTransferAmount = new BigDecimal(10);
087                    balanceTransfer.setForfeitedAmount(new BigDecimal(10));
088                    balanceTransfer = balanceTransfer.adjust(adjustedTransferAmount);
089                    
090                    assertTrue("Transfer Amount not equals", balanceTransfer.getTransferAmount().compareTo(adjustedTransferAmount) == 0);
091                    assertTrue("Forfeited amount not updated", balanceTransfer.getForfeitedAmount().compareTo(new BigDecimal(20)) == 0);
092                    assertTrue(balanceTransfer.getAmountTransferred().compareTo(new BigDecimal(5)) == 0);
093            }
094            
095            @Test
096            public void testAdjustRaiseTransferAmount() {
097                    BigDecimal adjustedTransferAmount = new BigDecimal(30);
098                    balanceTransfer = balanceTransfer.adjust(adjustedTransferAmount);
099                    
100                    assertTrue("Transfer Amount not equals", balanceTransfer.getTransferAmount().compareTo(adjustedTransferAmount) == 0);
101                    assertTrue("Forfeited amount not updated", balanceTransfer.getForfeitedAmount().compareTo(BigDecimal.ZERO) == 0);
102                    assertTrue(balanceTransfer.getAmountTransferred().compareTo(new BigDecimal(15)) == 0);
103            }
104            
105            @Test
106            public void testAdjustRaiseTransferAmountWithForfeitureLessThanDifference() {
107                    BigDecimal adjustedTransferAmount = new BigDecimal(40);
108                    balanceTransfer.setForfeitedAmount(new BigDecimal(10));
109                    
110                    balanceTransfer = balanceTransfer.adjust(adjustedTransferAmount);
111                    
112                    assertTrue("Transfer Amount not equals", balanceTransfer.getTransferAmount().compareTo(adjustedTransferAmount) == 0);
113                    assertTrue("Forfeited amount not updated", balanceTransfer.getForfeitedAmount().compareTo(BigDecimal.ZERO) == 0);
114                    assertTrue(balanceTransfer.getAmountTransferred().compareTo(new BigDecimal(20)) == 0);
115            }
116            
117            @Test
118            public void testAdjustRaiseTransferAmountWithForfeitureMoreThanDifference() {
119                    BigDecimal adjustedTransferAmount = new BigDecimal(30);
120                    balanceTransfer.setForfeitedAmount(new BigDecimal(15));
121                    balanceTransfer = balanceTransfer.adjust(adjustedTransferAmount);
122                    
123                    assertTrue("Transfer Amount not equals", balanceTransfer.getTransferAmount().compareTo(adjustedTransferAmount) == 0);
124                    assertTrue("Forfeited amount not updated", balanceTransfer.getForfeitedAmount().compareTo(new BigDecimal(5)) == 0);
125                    assertTrue(balanceTransfer.getAmountTransferred().compareTo(new BigDecimal(15)) == 0);
126            }
127            
128            /**
129             * Tests balance transfers triggered by max balance action frequency "leave approve"
130             * Specifically, when a single accrual category has exceeded the max balance defined by the
131             * accrual category's rules, max balance action frequency is leave approve, and action at max balance is transfer.
132             * Would be better placed in LeaveCalendarWorkflowIntegrationTest.
133             * @throws Exception 
134             */
135            @Test
136            public void testSingleLeaveApproveBalanceTransfer() throws Exception {
137                    //Create a leave summary with a single row containing an accrual category that has exceeded max balance limit.
138                    //Generate accrual entries for debitAC1 and creditAC1
139                    LeaveSummary ls = new LeaveSummary();
140    
141                    //Leave summary is created on the fly
142                    Date startDate = new Date((new DateTime(2012, 1, 1, 0, 0, 0, 1, TKUtils.getSystemDateTimeZone())).getMillis());
143            Date asOfDate = new Date((new DateTime(2012, 11, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
144            TkServiceLocator.getAccrualService().runAccrual(USER_PRINCIPAL_ID,startDate,asOfDate,false);
145            CalendarEntries pcd = TkServiceLocator.getCalendarService().getCurrentCalendarDatesForLeaveCalendar(USER_PRINCIPAL_ID, asOfDate);
146            Assert.assertNotNull("No CalendarEntries", pcd);
147    
148            //opens a leave calendar document and initiates a workflow document. Sets document status to Initiated.
149            LeaveCalendarDocument tdoc = TkServiceLocator.getLeaveCalendarService().openLeaveCalendarDocument(USER_PRINCIPAL_ID, pcd);
150            String tdocId = tdoc.getDocumentId();
151    
152            // Build an action form - we're using it as a POJO, it ties into the
153            // existing TK validation setup
154            //Can be removed if LeaveCalendarService.openLeaveCalendarDocument takes on the task.
155            ls = TkServiceLocator.getLeaveSummaryService().getLeaveSummary(USER_PRINCIPAL_ID, pcd);
156            LeaveCalendarWSForm tdaf = LeaveCalendarTestUtils.buildLeaveCalendarFormForSubmission(tdoc, ls);
157            LeaveCalendarSubmitForm lcsf = new LeaveCalendarSubmitForm();
158            lcsf.setAction(TkConstants.DOCUMENT_ACTIONS.ROUTE);
159            lcsf.setDocumentId(tdaf.getDocumentId());
160            for(LeaveSummaryRow lsRow : ls.getLeaveSummaryRows()) {
161                    System.out.println("Accrued balance : " + lsRow.getAccruedBalance());
162            }
163            HtmlPage page = LeaveCalendarTestUtils.submitLeaveCalendar2(getLeaveCalendarUrl(tdocId), tdaf);
164            //LeaveCalendarWSForm extends LeaveCalendarForm. In order to fully implement this test, a LeaveCalendarSubmitForm must be created
165            //with an action of "DOCUMENT_ACTIONS.ROUTE".
166            Assert.assertNotNull(page);
167            HtmlUnitUtil.createTempFile(page, "LeaveBlockPresent");
168    
169            // Verify block present on rendered page.
170            String pageAsText = page.asText();
171            System.out.print(pageAsText);
172                    
173                    
174                    //LeaveCalendarWSForm mockForm = LeaveCalendarTestUtils.buildLeaveCalendarForm(tdoc, assignment, earnCode, start, end, null, true);
175                    //Attach leave summary to relevant object (leavecalendardocument?)
176                    //load LeaveCalendarDocument into web container
177                    //leave calendar document must have status "initiated"
178                    //mock the submit action on behalf of the principal who owns the leave calendar.
179                    //redirect should occur, which loads the balance transfer document.
180                    //balance transfer document should have all fields locked except for transfer amount.
181                    //mock transfer action on behalf of the principal.
182                    //verify leave block creation
183                    assertTrue("Dummy assertion 2", true);
184            }
185            
186            /**
187             * Tests balance transfers triggered by max balance action frequency "leave approve"
188             * Specifically, when multiple accrual categories have exceeded the max balance defined by the
189             * accrual category's rules, max balance action frequency is leave approve, and action at max balance is transfer.
190             */
191            @Test
192            public void testMultipleLeaveApproveBalanceTransfer() throws Exception {
193                    //Create a leave summary with multiple rows containing an accrual category that has exceeded max balance limit.
194                    //Attach leave summary to relevant object (leavecalendardocument?)
195                    //load LeaveCalendarDocument into web container
196                    //leave calendar document must have status "initiated"
197                    //mock the submit action on behalf of the principal who owns the leave calendar.
198                    //redirect should occur, which loads the balance transfer document.
199                    //balance transfer document should have all fields locked except for transfer amount.
200                    //mock transfer action on behalf of the principal.
201                    //verify transfer action has been triggered the correct number of times.
202                    //verify the correct number of leave blocks have been generated?
203                    assertTrue("Dummy assertion 3",true);
204            }
205            
206            /**
207             * Tests leave calendar submit action does not trigger action redirects for balance transfer
208             * submission when no accrual category is eligable for transfer.
209             */
210            @Test
211            public void testNoEligibleAccrualCategorysForLeaveApproveBalanceTransfer() throws Exception {
212                    //Create leave summary with x number of rows, none of which contain an accrual category that has exceeded its limt.
213                    //Attach leave summary to relevant object (leavecalendardocument?)
214                    //load LeaveCalendarDocument into web container
215                    //leave calendar document must have status "initiated"
216                    //mock the submit action on behalf of the principal who owns the leave calendar.
217                    //redirect should occur, which loads the balance transfer document.
218                    //balance transfer document should have all fields locked except for transfer amount.
219                    //mock transfer action on behalf of the principal.
220                    //verify transfer action has been triggered the correct number of times.
221                    //verify the correct number of leave blocks have been generated?
222                    assertTrue("Dummy assertion 4", true);
223            }
224            
225            /**
226             * Test the lookup results page, there should not be "edit" actions, only "view" actions
227             * @throws Exception
228             */
229            @Test
230            public void testLookupPage() throws Exception {  
231                    HtmlPage btLookup = HtmlUnitUtil.gotoPageAndLogin(TkTestConstants.Urls.BALANCE_TRANSFER_MAINT_URL);
232                    btLookup = HtmlUnitUtil.clickInputContainingText(btLookup, "search");
233                    System.out.println(btLookup.asXml());
234                    Assert.assertTrue("Page contains test Balance Transfer", btLookup.asText().contains("fromAC"));
235                    Assert.assertFalse("Page should not contain edit action", btLookup.asText().contains("edit")); 
236                    Assert.assertTrue("Page should contain view action", btLookup.asText().contains("view"));
237            }
238            
239            
240    }