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 com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
41  import com.gargoylesoftware.htmlunit.WebClient;
42  import com.gargoylesoftware.htmlunit.html.HtmlPage;
43  import sun.util.LocaleServiceProviderPool;
44  
45  import static org.junit.Assert.assertTrue;
46  
47  
48  public class BalanceTransferTest extends LeaveCalendarWebTestBase {
49      private static final Logger LOG = Logger.getLogger(BalanceTransferTest.class);
50      public static final String USER_PRINCIPAL_ID = "admin";
51  	private Date JAN_AS_OF_DATE = new Date((new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
52  	private BalanceTransfer balanceTransfer;
53  	
54  	@Override
55  	public void setUp() throws Exception {
56  		super.setUp();
57          setBaseDetailURL(TkTestConstants.Urls.LEAVE_CALENDAR_SUBMIT_URL + "?documentId=");
58          balanceTransfer = new BalanceTransfer();
59          balanceTransfer.setTransferAmount(new BigDecimal(20));
60          balanceTransfer.setForfeitedAmount(new BigDecimal(0));
61          balanceTransfer.setAmountTransferred(new BigDecimal(10));
62          balanceTransfer.setAccrualCategoryRule("5000");
63  	}
64  	
65  	@Test
66  	public void testTransferWithAccrualRule() throws Exception {
67  		BalanceTransfer btd = new BalanceTransfer();
68  		//btd.setCreditedAccrualCategory(TKTestUtils.creat)
69  		assertTrue("Dummy assertion",true);
70  	}
71  	
72  	@Test
73  	public void testAdjustLowerTransferAmount() {
74  		BigDecimal adjustedTransferAmount = new BigDecimal(10);
75  		balanceTransfer = balanceTransfer.adjust(adjustedTransferAmount);
76  		
77  		assertTrue("Transfer Amount not equals", balanceTransfer.getTransferAmount().compareTo(adjustedTransferAmount) == 0);
78  		assertTrue("Forfeited amount not updated", balanceTransfer.getForfeitedAmount().compareTo(new BigDecimal(10)) == 0);
79  		assertTrue(balanceTransfer.getAmountTransferred().compareTo(new BigDecimal(5)) == 0);
80  	}
81  	
82  	@Test
83  	public void testAdjustLowerTransferAmountWithForfeiture() {
84  		BigDecimal adjustedTransferAmount = new BigDecimal(10);
85  		balanceTransfer.setForfeitedAmount(new BigDecimal(10));
86  		balanceTransfer = balanceTransfer.adjust(adjustedTransferAmount);
87  		
88  		assertTrue("Transfer Amount not equals", balanceTransfer.getTransferAmount().compareTo(adjustedTransferAmount) == 0);
89  		assertTrue("Forfeited amount not updated", balanceTransfer.getForfeitedAmount().compareTo(new BigDecimal(20)) == 0);
90  		assertTrue(balanceTransfer.getAmountTransferred().compareTo(new BigDecimal(5)) == 0);
91  	}
92  	
93  	@Test
94  	public void testAdjustRaiseTransferAmount() {
95  		BigDecimal adjustedTransferAmount = new BigDecimal(30);
96  		balanceTransfer = balanceTransfer.adjust(adjustedTransferAmount);
97  		
98  		assertTrue("Transfer Amount not equals", balanceTransfer.getTransferAmount().compareTo(adjustedTransferAmount) == 0);
99  		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 }