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