View Javadoc
1   /**
2    * Copyright 2004-2014 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.kpme.tklm.time.util;
17  
18  import java.math.BigDecimal;
19  import java.util.*;
20  import java.util.Map.Entry;
21  
22  import org.joda.time.DateTime;
23  import org.joda.time.LocalDate;
24  import org.junit.After;
25  import org.junit.Assert;
26  import org.junit.Before;
27  import org.junit.Test;
28  import org.kuali.kpme.core.IntegrationTest;
29  import org.kuali.kpme.core.api.calendar.entry.CalendarEntry;
30  import org.kuali.kpme.core.api.earncode.EarnCode;
31  import org.kuali.kpme.core.service.HrServiceLocator;
32  import org.kuali.kpme.core.util.TKUtils;
33  import org.kuali.kpme.tklm.TKLMIntegrationTestCase;
34  import org.kuali.kpme.tklm.api.leave.block.LeaveBlock;
35  import org.kuali.kpme.tklm.api.time.timeblock.TimeBlock;
36  import org.kuali.kpme.tklm.leave.block.LeaveBlockBo;
37  import org.kuali.kpme.tklm.time.detail.web.ActionFormUtils;
38  import org.kuali.kpme.tklm.time.detail.web.TimeDetailActionFormBase;
39  import org.kuali.kpme.tklm.time.timeblock.TimeBlockBo;
40  import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
41  import org.kuali.kpme.tklm.utils.TkTestUtils;
42  
43  @IntegrationTest
44  public class ActionFormUtilsTest extends TKLMIntegrationTestCase {
45  	
46  	private CalendarEntry createdCalendarEntry = null;
47  	private CalendarEntry createdCalendarEntry2 = null;
48  	private CalendarEntry createdCalendarEntry3 = null;
49  	private List<String> warningMessages = new ArrayList<String>();
50  	
51  	//TimeDetailActionFormBase tdaf = new TimeDetailActionFormBase(); 
52  	DateTime aDate = new DateTime(2011, 7, 7, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
53  	
54  	@Before
55  	public void setUp() throws Exception {
56  	super.setUp();
57  		CalendarEntry.Builder pce = CalendarEntry.Builder.create();
58  		pce.setHrCalendarEntryId("105");
59  		pce.setHrCalendarId("2");
60  		pce.setCalendarName("BWS-CAL");
61  		pce.setBeginPeriodFullDateTime(new DateTime(2014, 04, 01, 0, 0, 0, 0));
62  		pce.setEndPeriodFullDateTime(new DateTime(2014, 04, 15, 0, 0, 0, 0));
63  		pce.setBatchEmployeeApprovalFullDateTime(null);
64  		pce.setBatchInitiateFullDateTime(null);
65  		pce.setBatchEndPayPeriodFullDateTime(null);
66  		pce.setBatchEmployeeApprovalFullDateTime(null);
67  		pce.setBatchSupervisorApprovalFullDateTime(null);
68  		pce.setBatchPayrollApprovalFullDateTime(null);
69  		pce.setVersionNumber(new Long(1));
70  		createdCalendarEntry = pce.build();	
71  		
72  		CalendarEntry.Builder pce2 = CalendarEntry.Builder.create();
73  		pce2.setHrCalendarEntryId("104");
74  		pce2.setHrCalendarId("2");
75  		pce2.setCalendarName("BWS-CAL");
76  		pce2.setBeginPeriodFullDateTime(new DateTime(2014, 03, 15, 0, 0, 0, 0));
77  		pce2.setEndPeriodFullDateTime(new DateTime(2014, 04, 01, 0, 0, 0, 0));
78  		pce2.setBatchEmployeeApprovalFullDateTime(null);
79  		pce2.setBatchInitiateFullDateTime(null);
80  		pce2.setBatchEndPayPeriodFullDateTime(null);
81  		pce2.setBatchEmployeeApprovalFullDateTime(null);
82  		pce2.setBatchSupervisorApprovalFullDateTime(null);
83  		pce2.setBatchPayrollApprovalFullDateTime(null);
84  		pce2.setVersionNumber(new Long(1));
85  		createdCalendarEntry2 = pce2.build();
86  		
87  		CalendarEntry.Builder pce3 = CalendarEntry.Builder.create();
88  		pce3.setHrCalendarEntryId("123");
89  		pce3.setHrCalendarId("2");
90  		pce3.setCalendarName("BWS-CAL");
91  		pce3.setBeginPeriodFullDateTime(new DateTime(2015, 01, 01, 0, 0, 0, 0));
92  		pce3.setEndPeriodFullDateTime(new DateTime(2015, 01, 15, 0, 0, 0, 0));
93  		pce3.setBatchEmployeeApprovalFullDateTime(null);
94  		pce3.setBatchInitiateFullDateTime(null);
95  		pce3.setBatchEndPayPeriodFullDateTime(null);
96  		pce3.setBatchEmployeeApprovalFullDateTime(null);
97  		pce3.setBatchSupervisorApprovalFullDateTime(null);
98  		pce3.setBatchPayrollApprovalFullDateTime(null);
99  		pce3.setVersionNumber(new Long(1));
100 		createdCalendarEntry3 = pce3.build();
101 		
102 		String warningMessage1 = "warningMessage1";
103 		String warningMessage2 = "warningMessage2";
104 		warningMessages.add(warningMessage1);
105 		warningMessages.add(warningMessage2);
106 	}
107 	
108 	@After
109 	public void tearDown() throws Exception {
110 		super.tearDown();
111 	}
112 	
113 	@Test
114 	public void testBuildAssignmentStyleClassMap() {
115 		DateTime aDate = new DateTime(2011, 7, 7, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
116 		TimesheetDocument doc = TkTestUtils.populateTimesheetDocument(aDate, "admin");
117 		
118 		Map<String, String> aMap = ActionFormUtils.buildAssignmentStyleClassMap(doc.getTimeBlocks());
119 		Assert.assertEquals("Wrong number of classes in style class map", 1, aMap.size());
120 		Assert.assertEquals("Wrong key for class assignment0", "assignment0", aMap.get("IU-BL_1_1234_1"));
121 	}
122 	
123 	@Test
124 	public void testGetUnitOfTimeForEarnCode() throws Exception {
125 		// earn code with an existing Accrual category
126 		EarnCode earnCode = HrServiceLocator.getEarnCodeService().getEarnCodeById("5000");
127 		String unitOfTime = ActionFormUtils.getUnitOfTimeForEarnCode(earnCode);
128 		Assert.assertTrue("Unit of Time should be 'H', not " + unitOfTime, unitOfTime.equals("H"));
129 		// earn code without an existing accrual category
130 		earnCode = HrServiceLocator.getEarnCodeService().getEarnCodeById("5002");
131 		unitOfTime = ActionFormUtils.getUnitOfTimeForEarnCode(earnCode);
132 		Assert.assertTrue("Unit of Time should be 'H', not " + unitOfTime, unitOfTime.equals("H"));	
133 	}
134 	
135 	@Test
136 	public void testGetLeaveBlocksJson() {
137 		List<LeaveBlock> lbList = new ArrayList<LeaveBlock>();
138 		LeaveBlockBo lb = new LeaveBlockBo();
139 		//lb.setAssignmentTitle("testAssignment");
140 		lb.setAssignmentKey("0-123-0");
141 		lb.setEarnCode("EarnCode");
142 		lb.setLmLeaveBlockId("1111");
143 		lb.setLeaveAmount(new BigDecimal(3));
144 		lb.setLeaveLocalDate(new DateTime(2012, 2, 20, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone()).toLocalDate());
145 		lb.setAccrualGenerated(false);
146 		lbList.add(LeaveBlockBo.to(lb));
147 		
148 		String jsonString = ActionFormUtils.getLeaveBlocksJson(lbList);
149 		String expectedString = "[{\"title\":\"\",\"assignment\":\"0-123-0\",\"earnCode\":\"EarnCode\",\"lmLeaveBlockId\":\"1111\",\"leaveAmount\":\"3\",\"leaveDate\":\"02\\/20\\/2012\",\"id\":\"1111\",\"canTransfer\":false,\"startDate\":\"02\\/20\\/2012\",\"endDate\":\"02\\/20\\/2012\"}]";
150 		Assert.assertTrue("Leave Block Json should include assignment", jsonString.equals(expectedString));
151 	}
152 	
153 	//following tests are changes of KPME-3367
154 	@Test
155 	public void testIsOnCurrentPeriodFlag(){	
156 		CalendarEntry calendarEntry = HrServiceLocator.getCalendarEntryService().getCurrentCalendarDates("admin", new LocalDate().toDateTimeAtStartOfDay());
157 		
158 		boolean gotOne = ActionFormUtils.isOnCurrentPeriodFlag(calendarEntry);
159 		Assert.assertTrue("Is on Current Period", gotOne);
160 		
161 		boolean notGotOne = ActionFormUtils.isOnCurrentPeriodFlag(createdCalendarEntry2);
162 		Assert.assertTrue("Not on Current Period", !notGotOne);
163 	}
164 	
165 	@Test
166 	public void testGetAllCalendarEntriesForYear() {
167 		List<CalendarEntry> calendarEntries = new ArrayList<CalendarEntry>();
168 		calendarEntries.add(createdCalendarEntry);
169 		calendarEntries.add(createdCalendarEntry2);
170 		calendarEntries.add(createdCalendarEntry3); // this one does not belong to year 2014
171 		
172 		List<CalendarEntry> allCalendarEntriesForYear = ActionFormUtils.getAllCalendarEntriesForYear(calendarEntries, "2014");
173 		
174 		Assert.assertTrue(allCalendarEntriesForYear.size() == 2);
175 	}
176 	
177 	@Test
178 	public void testGetPlanningMonthsForEmployee(){
179 		String principleId = "testPrincial";
180 		int plannningMonths = 0;
181 		plannningMonths = ActionFormUtils.getPlanningMonthsForEmployee(principleId);
182 		
183 		Assert.assertTrue(plannningMonths == 12);
184 	}	
185 	
186 	@Test
187 	public void testAddUniqueWarningsToForm(){
188 		TimeDetailActionFormBase tdaf = new TimeDetailActionFormBase(); 
189 		DateTime aDate = new DateTime(2011, 7, 7, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
190 		TimesheetDocument timesheetDocument = TkTestUtils.populateTimesheetDocument(aDate, "admin");
191 		
192 		tdaf.setTkTimeBlockId("5000");
193 		tdaf.setWarningMessages(warningMessages);
194 		tdaf.setTimesheetDocument(timesheetDocument);
195 		
196 		Assert.assertEquals("Number of messages should be 2 BEFORE the call.", 2, tdaf.getWarningMessages().size());
197 		ActionFormUtils.addUniqueWarningsToForm(tdaf, warningMessages);
198 		
199 		Assert.assertEquals("Number of messages should be 2 AFTER the call.", 2, tdaf.getWarningMessages().size());
200 		Assert.assertTrue("warningMessage2 should show up.", tdaf.getWarningMessages().get(0).equalsIgnoreCase("warningMessage2"));
201 		Assert.assertTrue("warningMessage1 should show up.", tdaf.getWarningMessages().get(1).equalsIgnoreCase("warningMessage1"));
202 	}
203 	
204 	@Test
205 	public void testBuildAssignmentStyleClassMap2(){
206 		DateTime aDate = new DateTime(2011, 7, 7, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
207 		TimesheetDocument doc = TkTestUtils.populateTimesheetDocument(aDate, "admin");
208 		
209 		List<LeaveBlock> lbList = new ArrayList<LeaveBlock>();
210 		LeaveBlockBo lb = new LeaveBlockBo();
211 		//lb.setAssignmentTitle("testAssignment");
212 		lb.setAssignmentKey("IU-BL_0_123_0");
213 		lb.setEarnCode("EarnCode");
214 		lb.setLmLeaveBlockId("1111");
215 		lb.setLeaveAmount(new BigDecimal(3));
216 		lb.setLeaveLocalDate(new DateTime(2012, 2, 20, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone()).toLocalDate());
217 		lb.setAccrualGenerated(false);
218 		lbList.add(LeaveBlockBo.to(lb));
219 		
220 		Map<String, String> aMap = ActionFormUtils.buildAssignmentStyleClassMap(doc.getTimeBlocks(), lbList);
221 		Assert.assertEquals("Wrong number of classes in style class map", 2, aMap.size());
222 		Assert.assertEquals("Wrong key for class assignment0", "assignment1", aMap.get("IU-BL_1_1234_1"));
223 		Assert.assertEquals("Wrong key for class assignment0", "assignment0", aMap.get("IU-BL_0_123_0"));
224 	}
225 	
226 	@Test
227 	public void testGetTimeBlocksJson() {
228 		List<TimeBlock> timeBlockList = new ArrayList<TimeBlock>();
229 		TimeBlockBo timeBlockBo = new TimeBlockBo();
230 		
231 		timeBlockBo.setAssignmentValue("testAssignment");
232 		timeBlockBo.setEarnCode("SDR");
233 		timeBlockBo.setTkTimeBlockId("1111");
234         timeBlockBo.setGroupKeyCode("IU-BL");
235 		timeBlockBo.setWorkArea(new Long(30));
236 		timeBlockBo.setBeginDateTime(new DateTime(2014, 4, 10, 0, 0, 0, 0));
237 		timeBlockBo.setEndDateTime(new DateTime(2014, 4, 10, 0, 0, 0, 0));
238 		timeBlockBo.setJobNumber(new Long(30));
239 		timeBlockBo.setAmount(new BigDecimal(10));
240 		
241 		timeBlockList.add(TimeBlockBo.to(timeBlockBo));
242 		
243 		String jsonString = ActionFormUtils.getTimeBlocksJson(timeBlockList);
244 		// expected jsonString:
245 		// [{"isApprover":true,"isSynchronousUser":null,"canEditTb":false,"canEditTBOvt":true,"canEditTBAll":false,"canEditTBAssgOnly":true,"documentId":null,"title":"SDR1 Work Area","earnCode":"SDR","earnCodeDesc":"SHIFT DIFF","earnCodeType":null,"start":"2014-04-10T00:00:00-04:00","end":"2014-04-10T00:00:00-04:00","startDate":"04\/10\/2014","endDate":"04\/10\/2014","startNoTz":"2014-04-10T00:00:00","endNoTz":"2014-04-10T00:00:00","startTimeHourMinute":"12:00 AM","endTimeHourMinute":"12:00 AM","startTime":"0:00","endTime":"0:00","id":"1111","hours":0.00,"amount":10.00,"timezone":"America\/New_York","assignment":"30_30_0","tkTimeBlockId":"1111","lunchDeleted":false,"timeHourDetails":"[]"}]
246 		Assert.assertTrue("Leave Block Json should include earnCode", jsonString.contains("\"earnCode\":\"SDR\""));
247 		Assert.assertTrue("Leave Block Json should include title", jsonString.contains("\"title\":\"SDR1 Work Area\""));
248 		Assert.assertTrue("Leave Block Json should include tkTimeBlockId", jsonString.contains("\"tkTimeBlockId\":\"1111\""));
249 		Assert.assertTrue("Leave Block Json should include amount", jsonString.contains("\"amount\":10.00"));
250 		Assert.assertTrue("Leave Block Json should include assignment", jsonString.contains("\"assignment\":\"IU-BL_30_30_0\""));
251 	}
252 	
253 	@Test
254 	public void testGetPayPeriodsMap(){
255 		List<CalendarEntry> calendarEntries = new ArrayList<CalendarEntry>();
256 		calendarEntries.add(createdCalendarEntry);
257 		calendarEntries.add(createdCalendarEntry2);
258 		
259 		Map<String, String> pMap = ActionFormUtils.getPayPeriodsMap(calendarEntries, "admin");
260 		
261 		Set<Entry<String, String>> mapValues = pMap.entrySet();
262 	    int maplength = mapValues.size();
263 	    Entry<String,String>[] test = new Entry[maplength];
264 	    mapValues.toArray(test);
265 	    
266 	    // pMap should have:
267 	 	// {105=04/01/2014 - 04/14/2014, 104=03/15/2014 - 03/31/2014}
268 	    Assert.assertTrue("Should return two pay periods", pMap.size() == 2);
269 	    
270         Assert.assertTrue("Key of the first pay period", test[0].getKey().equals("105"));
271         Assert.assertTrue("Value of the first pay period", test[0].getValue().equals("04/01/2014 - 04/14/2014"));
272         
273         Assert.assertTrue("Key of the second pay period", test[1].getKey().equals("104"));
274         Assert.assertTrue("Value of the second pay period", test[1].getValue().equals("03/15/2014 - 03/31/2014"));
275 	}
276 	
277 }