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  
18  import com.gargoylesoftware.htmlunit.html.HtmlPage;
19  import junit.framework.Assert;
20  
21  import org.apache.log4j.Logger;
22  import org.joda.time.DateTime;
23  import org.junit.Test;
24  import org.kuali.hr.lm.leave.web.LeaveCalendarWSForm;
25  import org.kuali.hr.lm.leaveCalendar.LeaveCalendarWebTestBase;
26  import org.kuali.hr.lm.leaveSummary.LeaveSummary;
27  import org.kuali.hr.lm.leaveSummary.LeaveSummaryRow;
28  import org.kuali.hr.lm.leavecalendar.LeaveCalendarDocument;
29  import org.kuali.hr.lm.leavecalendar.web.LeaveCalendarSubmitForm;
30  import org.kuali.hr.lm.util.LeaveCalendarTestUtils;
31  import org.kuali.hr.time.calendar.CalendarEntries;
32  import org.kuali.hr.time.service.base.TkServiceLocator;
33  import org.kuali.hr.time.test.HtmlUnitUtil;
34  import org.kuali.hr.time.test.TkTestConstants;
35  import org.kuali.hr.time.util.TKUtils;
36  import org.kuali.hr.time.util.TkConstants;
37  
38  import java.math.BigDecimal;
39  import java.sql.Date;
40  import static org.junit.Assert.assertTrue;
41  
42  
43  public class BalanceTransferTest extends LeaveCalendarWebTestBase {
44      private static final Logger LOG = Logger.getLogger(BalanceTransferTest.class);
45      public static final String USER_PRINCIPAL_ID = "admin";
46  	private Date JAN_AS_OF_DATE = new Date((new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
47  	private BalanceTransfer balanceTransfer;
48  	
49  	@Override
50  	public void setUp() throws Exception {
51  		super.setUp();
52          setBaseDetailURL(TkTestConstants.Urls.LEAVE_CALENDAR_SUBMIT_URL + "?documentId=");
53          balanceTransfer = new BalanceTransfer();
54          balanceTransfer.setTransferAmount(new BigDecimal(20));
55          balanceTransfer.setForfeitedAmount(new BigDecimal(0));
56          balanceTransfer.setAmountTransferred(new BigDecimal(10));
57          balanceTransfer.setAccrualCategoryRule("5000");
58  	}
59  	
60  	@Test
61  	public void testTransferWithAccrualRule() throws Exception {
62  		BalanceTransfer btd = new BalanceTransfer();
63  		//btd.setCreditedAccrualCategory(TKTestUtils.creat)
64  		assertTrue("Dummy assertion",true);
65  	}
66  	
67  	@Test
68  	public void testAdjustLowerTransferAmount() {
69  		BigDecimal adjustedTransferAmount = new BigDecimal(10);
70  		balanceTransfer = balanceTransfer.adjust(adjustedTransferAmount);
71  		
72  		assertTrue("Transfer Amount not equals", balanceTransfer.getTransferAmount().compareTo(adjustedTransferAmount) == 0);
73  		assertTrue("Forfeited amount not updated", balanceTransfer.getForfeitedAmount().compareTo(new BigDecimal(10)) == 0);
74  		assertTrue(balanceTransfer.getAmountTransferred().compareTo(new BigDecimal(5)) == 0);
75  	}
76  	
77  	@Test
78  	public void testAdjustLowerTransferAmountWithForfeiture() {
79  		BigDecimal adjustedTransferAmount = new BigDecimal(10);
80  		balanceTransfer.setForfeitedAmount(new BigDecimal(10));
81  		balanceTransfer = balanceTransfer.adjust(adjustedTransferAmount);
82  		
83  		assertTrue("Transfer Amount not equals", balanceTransfer.getTransferAmount().compareTo(adjustedTransferAmount) == 0);
84  		assertTrue("Forfeited amount not updated", balanceTransfer.getForfeitedAmount().compareTo(new BigDecimal(20)) == 0);
85  		assertTrue(balanceTransfer.getAmountTransferred().compareTo(new BigDecimal(5)) == 0);
86  	}
87  	
88  	@Test
89  	public void testAdjustRaiseTransferAmount() {
90  		BigDecimal adjustedTransferAmount = new BigDecimal(30);
91  		balanceTransfer = balanceTransfer.adjust(adjustedTransferAmount);
92  		
93  		assertTrue("Transfer Amount not equals", balanceTransfer.getTransferAmount().compareTo(adjustedTransferAmount) == 0);
94  		assertTrue("Forfeited amount not updated", balanceTransfer.getForfeitedAmount().compareTo(BigDecimal.ZERO) == 0);
95  		assertTrue(balanceTransfer.getAmountTransferred().compareTo(new BigDecimal(15)) == 0);
96  	}
97  	
98  	@Test
99  	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 }