View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.lm.balancetransfer;
17  import static org.junit.Assert.*;
18  
19  import java.math.BigDecimal;
20  import java.sql.Date;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import junit.framework.Assert;
25  
26  import org.joda.time.DateTime;
27  import org.joda.time.Interval;
28  import org.junit.Test;
29  import org.kuali.hr.lm.leave.web.LeaveCalendarWSForm;
30  import org.kuali.hr.lm.leaveCalendar.LeaveCalendarWebTestBase;
31  import org.kuali.hr.lm.leaveSummary.LeaveSummary;
32  import org.kuali.hr.lm.leaveSummary.LeaveSummaryRow;
33  import org.kuali.hr.lm.leaveblock.LeaveBlock;
34  import org.kuali.hr.lm.leavecalendar.LeaveCalendarDocument;
35  import org.kuali.hr.lm.leavecalendar.web.LeaveCalendarSubmitForm;
36  import org.kuali.hr.lm.util.LeaveCalendarTestUtils;
37  import org.kuali.hr.test.KPMETestCase;
38  import org.kuali.hr.time.calendar.CalendarEntries;
39  import org.kuali.hr.time.service.base.TkServiceLocator;
40  import org.kuali.hr.time.test.HtmlUnitUtil;
41  import org.kuali.hr.time.test.TkTestConstants;
42  import org.kuali.hr.time.util.TKUtils;
43  import org.kuali.hr.time.util.TkConstants;
44  
45  import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
46  import com.gargoylesoftware.htmlunit.WebClient;
47  import com.gargoylesoftware.htmlunit.html.HtmlPage;
48  
49  
50  public class BalanceTransferTest extends LeaveCalendarWebTestBase {
51  	
52      public static final String USER_PRINCIPAL_ID = "admin";
53  	private Date JAN_AS_OF_DATE = new Date((new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
54  	private BalanceTransfer balanceTransfer;
55  	
56  	@Override
57  	public void setUp() throws Exception {
58  		super.setUp();
59          setBaseDetailURL(TkTestConstants.Urls.LEAVE_CALENDAR_SUBMIT_URL + "?documentId=");
60          balanceTransfer = new BalanceTransfer();
61          balanceTransfer.setTransferAmount(new BigDecimal(20));
62          balanceTransfer.setForfeitedAmount(new BigDecimal(0));
63          balanceTransfer.setAmountTransferred(new BigDecimal(10));
64          balanceTransfer.setAccrualCategoryRule("5000");
65  	}
66  	
67  	@Test
68  	public void testTransferWithAccrualRule() throws Exception {
69  		BalanceTransfer btd = new BalanceTransfer();
70  		//btd.setCreditedAccrualCategory(TKTestUtils.creat)
71  		assertTrue("Dummy assertion",true);
72  	}
73  	
74  	@Test
75  	public void testAdjustLowerTransferAmount() {
76  		BigDecimal adjustedTransferAmount = new BigDecimal(10);
77  		balanceTransfer = balanceTransfer.adjust(adjustedTransferAmount);
78  		
79  		assertTrue("Transfer Amount not equals", balanceTransfer.getTransferAmount().compareTo(adjustedTransferAmount) == 0);
80  		assertTrue("Forfeited amount not updated", balanceTransfer.getForfeitedAmount().compareTo(new BigDecimal(10)) == 0);
81  		assertTrue(balanceTransfer.getAmountTransferred().compareTo(new BigDecimal(5)) == 0);
82  	}
83  	
84  	@Test
85  	public void testAdjustLowerTransferAmountWithForfeiture() {
86  		BigDecimal adjustedTransferAmount = new BigDecimal(10);
87  		balanceTransfer.setForfeitedAmount(new BigDecimal(10));
88  		balanceTransfer = balanceTransfer.adjust(adjustedTransferAmount);
89  		
90  		assertTrue("Transfer Amount not equals", balanceTransfer.getTransferAmount().compareTo(adjustedTransferAmount) == 0);
91  		assertTrue("Forfeited amount not updated", balanceTransfer.getForfeitedAmount().compareTo(new BigDecimal(20)) == 0);
92  		assertTrue(balanceTransfer.getAmountTransferred().compareTo(new BigDecimal(5)) == 0);
93  	}
94  	
95  	@Test
96  	public void testAdjustRaiseTransferAmount() {
97  		BigDecimal adjustedTransferAmount = new BigDecimal(30);
98  		balanceTransfer = balanceTransfer.adjust(adjustedTransferAmount);
99  		
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 }