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