View Javadoc

1   /**
2    * Copyright 2004-2012 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.time.timesheet.web;
17  
18  import java.sql.Date;
19  import java.util.*;
20  
21  import org.joda.time.DateTime;
22  import org.joda.time.DateTimeZone;
23  import org.json.simple.JSONArray;
24  import org.json.simple.JSONObject;
25  import org.json.simple.JSONValue;
26  import org.junit.Assert;
27  import org.junit.Test;
28  import org.kuali.hr.time.assignment.Assignment;
29  import org.kuali.hr.time.calendar.CalendarEntries;
30  import org.kuali.hr.time.detail.web.TimeDetailActionFormBase;
31  import org.kuali.hr.time.earncode.EarnCode;
32  import org.kuali.hr.time.service.base.TkServiceLocator;
33  import org.kuali.hr.time.test.HtmlUnitUtil;
34  import org.kuali.hr.time.timesheet.TimesheetDocument;
35  import org.kuali.hr.time.util.TKUtils;
36  import org.kuali.hr.time.util.TimeDetailTestUtils;
37  
38  import com.gargoylesoftware.htmlunit.html.HtmlForm;
39  import com.gargoylesoftware.htmlunit.html.HtmlPage;
40  
41  //@Ignore
42  public class TimesheetIntegrationTest extends TimesheetWebTestBase {
43  
44  	public static final String USER_PRINCIPAL_ID = "admin";
45  	public static final Date TIME_SHEET_DATE = new Date(
46  			(new DateTime(2011, 2, 15, 0, 0, 0, 0, DateTimeZone
47  					.forID(TKUtils.getSystemTimeZone()))).getMillis());
48  	public TimesheetDocument timeDoc;
49  	public List<Assignment> assignmentsOfUser;
50  	public CalendarEntries payCal;
51  	public String tdocId;
52  
53  	/**
54  	 * @throws Exception
55  	 */
56  	public void setUp() throws Exception {
57  
58  		super.setUp();
59  
60  		payCal = TkServiceLocator.getCalendarService().getCurrentCalendarDates(
61  				USER_PRINCIPAL_ID, TIME_SHEET_DATE);
62  		Assert.assertNotNull("Pay calendar entries not found for admin", payCal);
63  
64  		// retrieving time sheet for according to the pay calendar
65  		timeDoc = TkServiceLocator.getTimesheetService().openTimesheetDocument(
66  				USER_PRINCIPAL_ID, payCal);
67  		tdocId = timeDoc.getDocumentId();
68  
69  		// login
70  		HtmlPage page = loginAndGetTimeDetailsHtmlPage(USER_PRINCIPAL_ID,
71  				tdocId, true);
72  		Assert.assertNotNull(page);
73  
74  		assignmentsOfUser = TkServiceLocator.getAssignmentService()
75  				.getAssignments(USER_PRINCIPAL_ID, TIME_SHEET_DATE);
76  		Assert.assertNotNull("No Assignments found for the user ", assignmentsOfUser);
77  
78  		// check if page contains calendar for February 2011
79  		Assert.assertTrue("Page could not find calendar for Feb 2011", page.asText()
80  				.contains("February 2011"));
81  
82  		HtmlForm form = page.getFormByName("TimeDetailActionForm");
83  		Assert.assertNotNull(form);
84  	}
85  
86  	public void tearDown() throws Exception {
87  		super.tearDown();
88  	}
89  
90  	@Test
91  	public void testAddTimeBlock() throws Exception {
92  
93  		EarnCode earnCode = null;
94  		HtmlPage page = loginAndGetTimeDetailsHtmlPage(USER_PRINCIPAL_ID,
95  				tdocId, true);
96  
97  		HtmlForm form = page.getFormByName("TimeDetailActionForm");
98  		Assert.assertNotNull(form);
99  
100 		// Assignment of user
101 		Assignment assToBeSelected = assignmentsOfUser.get(4);
102 
103         // retrieving earncode for the assignment
104 		List<EarnCode> earnCodes = TkServiceLocator.getEarnCodeService().getEarnCodes(assToBeSelected, TIME_SHEET_DATE);
105 		if (earnCodes != null && !earnCodes.isEmpty()) {
106 			earnCode = earnCodes.get(0);
107 		}
108 
109 		DateTime startTime = new DateTime(2011, 2, 15, 9, 0, 0, 0,
110 				TKUtils.getSystemDateTimeZone());
111 		DateTime endTime = new DateTime(2011, 2, 15, 11, 0, 0, 0,
112 				TKUtils.getSystemDateTimeZone());
113 
114 		// Setup TimeDetailActionForm
115 		TimeDetailActionFormBase addTB = TimeDetailTestUtils
116 				.buildDetailActionForm(timeDoc, assToBeSelected, earnCode,
117 						startTime, endTime, null, true, null, true);
118 		List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form,
119 				addTB);
120 
121 		// Check for errors
122 		Assert.assertEquals(
123 				"There should be no errors in this time detail submission", 0,
124 				errors.size());
125 
126 		// submit the details of Timeblock to be added.
127 		page = TimeDetailTestUtils.submitTimeDetails(
128 				TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId), addTB);
129 		Assert.assertNotNull(page);
130 
131 		// get Timeblocks objects from Timeblock string
132 		String dataText = page.getElementById("timeBlockString")
133 				.getFirstChild().getNodeValue();
134 
135 		// check the values in timeblockString
136 		JSONArray jsonData = (JSONArray) JSONValue.parse(dataText);
137 		final JSONObject jsonDataObject = (JSONObject) jsonData.get(0);
138 		final String assignmentKey = assToBeSelected.getAssignmentKey();
139 		Assert.assertTrue("TimeBlock Data Missing.", checkJSONValues(new JSONObject() {
140 			{
141 				put("outer", jsonDataObject);
142 			}
143 		}, new ArrayList<Map<String, Object>>() {
144 			{
145 				add(new HashMap<String, Object>() {
146 					{
147 						put("earnCode", "RGN");
148 						put("hours", "2.0");
149 						put("amount", null);
150 					}
151 				});
152 			}
153 		}, new HashMap<String, Object>() {
154 			{
155 				put("earnCode", "RGN");
156 				put("startNoTz", "2011-02-15T09:00:00");
157 				put("endNoTz", "2011-02-15T11:00:00");
158 				put("assignment", assignmentKey);
159 			}
160 		}));
161 
162 
163 		// check value ....
164 		Assert.assertTrue("TimeBlock did not created successfully.", page.asText()
165 				.contains("work area description-description 1"));
166 		Assert.assertTrue("TimeBlock did not created successfully.", page.asText()
167 				.contains("RGN - 2.00 hours"));
168 
169 	}
170 
171 	@Test
172 	public void testEditTimeBlock() throws Exception {
173 		EarnCode earnCode = null;
174 
175 		// login
176 		HtmlPage page = loginAndGetTimeDetailsHtmlPage(USER_PRINCIPAL_ID,
177 				tdocId, true);
178 
179 		// Assignment
180 		Assignment assToBeSelected = assignmentsOfUser.get(4);
181 
182         // retrieving earncode for the assignment
183 		List<EarnCode> earnCodes = TkServiceLocator.getEarnCodeService().getEarnCodes(assToBeSelected, TIME_SHEET_DATE);
184 		if (earnCodes != null && !earnCodes.isEmpty()) {
185 			earnCode = earnCodes.get(0);
186 		}
187 
188 		DateTime startTime = new DateTime(2011, 2, 15, 9, 0, 0, 0,
189 				TKUtils.getSystemDateTimeZone());
190 		DateTime endTime = new DateTime(2011, 2, 15, 11, 0, 0, 0,
191 				TKUtils.getSystemDateTimeZone());
192 
193 		HtmlForm form = page.getFormByName("TimeDetailActionForm");
194 		Assert.assertNotNull(form);
195 
196 		// Setup TimeDetailActionForm for adding time block
197 		TimeDetailActionFormBase addTB = TimeDetailTestUtils
198 				.buildDetailActionForm(timeDoc, assToBeSelected, earnCode,
199 						startTime, endTime, null, true, null, true);
200 		List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form,
201 				addTB);
202 
203 		// Check for errors
204 		Assert.assertEquals(
205 				"There should be no errors in this time detail submission", 0,
206 				errors.size());
207 
208 		page = TimeDetailTestUtils.submitTimeDetails(
209 				TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId), addTB);
210 		Assert.assertNotNull(page);
211 
212 		// chk if the page contains the created time block.
213 		Assert.assertTrue("TimeBlock not Present.",
214 				page.asText().contains("work area description-description 1"));
215 		Assert.assertTrue("TimeBlock not Present.",
216 				page.asText().contains("RGN - 2.00 hours"));
217 
218 		// now updating the time block
219 		timeDoc = TkServiceLocator.getTimesheetService().openTimesheetDocument(
220 				USER_PRINCIPAL_ID, payCal);
221 
222 		String createdTBId = timeDoc.getTimeBlocks().get(0).getTkTimeBlockId();
223 
224 		HtmlUnitUtil.createTempFile(page);
225 
226 		// change assignment and time
227 		assToBeSelected = assignmentsOfUser.get(3);
228 
229 		// earn codes related to the assignment
230 		earnCodes = TkServiceLocator.getEarnCodeService().getEarnCodes(assToBeSelected, TIME_SHEET_DATE);
231 		if (earnCodes != null && !earnCodes.isEmpty()) {
232 			earnCode = earnCodes.get(0);
233 		}
234 
235 		DateTime startTime1 = new DateTime(2011, 2, 15, 14, 0, 0, 0,
236 				TKUtils.getSystemDateTimeZone());
237 		DateTime endTime1 = new DateTime(2011, 2, 15, 17, 0, 0, 0,
238 				TKUtils.getSystemDateTimeZone());
239 
240 		form = page.getFormByName("TimeDetailActionForm");
241 
242 		TimeDetailActionFormBase updateTB = TimeDetailTestUtils
243 				.buildDetailActionForm(timeDoc, assToBeSelected, earnCode,
244 						startTime1, endTime1, null, true, createdTBId, true);
245 
246 		// validation of time block
247 		errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, updateTB);
248 		Assert.assertEquals(
249 				"There should be no errors in this time detail submission", 0,
250 				errors.size());
251 
252 		// update it
253 		page = TimeDetailTestUtils.submitTimeDetails(
254 				TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId), updateTB);
255 		Assert.assertNotNull(page);
256 
257 		// chk the timesheet contains the changes done with the time block
258 		Assert.assertTrue("TimeBlock did not updated properly.", page.asText()
259 				.contains("work area description-description 1"));
260 		Assert.assertTrue("TimeBlock did not updated properly.", page.asText()
261 				.contains("RGN - 3.00 hours"));
262 
263 	}
264 
265 	@Test
266 	public void testDeleteTimeBlock() throws Exception {
267 
268 		EarnCode earnCode = null;
269 
270 		// login
271 		HtmlPage page = loginAndGetTimeDetailsHtmlPage(USER_PRINCIPAL_ID,
272 				tdocId, true);
273 
274 		// Assignment of user
275 		Assignment assToBeSelected = assignmentsOfUser.get(4);
276 
277         // retrieving earncode for the assignment
278 		List<EarnCode> earnCodes = TkServiceLocator.getEarnCodeService().getEarnCodes(assToBeSelected, TIME_SHEET_DATE);
279 		if (earnCodes != null && !earnCodes.isEmpty()) {
280 			earnCode = earnCodes.get(0);
281 		}
282 
283 		DateTime startTime = new DateTime(2011, 2, 15, 9, 0, 0, 0,
284 				TKUtils.getSystemDateTimeZone());
285 		DateTime endTime = new DateTime(2011, 2, 15, 11, 0, 0, 0,
286 				TKUtils.getSystemDateTimeZone());
287 
288 		HtmlForm form = page.getFormByName("TimeDetailActionForm");
289 		Assert.assertNotNull(form);
290 
291 		// Setup TimeDetailActionForm
292 		TimeDetailActionFormBase addTB = TimeDetailTestUtils
293 				.buildDetailActionForm(timeDoc, assToBeSelected, earnCode,
294 						startTime, endTime, null, true, null, true);
295 		List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form,
296 				addTB);
297 
298 		// Check for errors
299 		Assert.assertEquals(
300 				"There should be no errors in this time detail submission", 0,
301 				errors.size());
302 
303 		page = TimeDetailTestUtils.submitTimeDetails(
304 				TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId), addTB);
305 		Assert.assertNotNull(page);
306 
307 		// chk the page must contain the created time block
308 		Assert.assertTrue("TimeBlock did not created successfully.", page.asText()
309 				.contains("work area description-description 1"));
310 		Assert.assertTrue("TimeBlock did not created successfully.", page.asText()
311 				.contains("RGN - 2.00 hours"));
312 
313 		timeDoc = TkServiceLocator.getTimesheetService().openTimesheetDocument(
314 				USER_PRINCIPAL_ID, payCal);
315 
316 		// Delete the timeblock
317 		String createTBId = timeDoc.getTimeBlocks().get(0).getTkTimeBlockId();
318 		HtmlUnitUtil.createTempFile(page);
319 		form = page.getFormByName("TimeDetailActionForm");
320 		Assert.assertNotNull(form);
321 
322 		// set detail for deleting time block
323 		TimeDetailActionFormBase deleteTB = TimeDetailTestUtils
324 				.buildDetailActionForm(timeDoc, assToBeSelected, earnCode,
325 						startTime, endTime, null, true, createTBId, true);
326 		deleteTB.setMethodToCall("deleteTimeBlock");
327 
328 		// submitting the page
329 		page = TimeDetailTestUtils.submitTimeDetails(
330 				TimesheetWebTestBase.getTimesheetDocumentUrl(tdocId), deleteTB);
331 		Assert.assertNotNull(page);
332 
333 		// chk the timesheet does not contain the time block
334 		Assert.assertTrue("TimeBlock did not deleted successfully.", !page.asText()
335 				.contains("work area description-description 1"));
336 		Assert.assertTrue("TimeBlock did not deleted successfully.", !page.asText()
337 				.contains("RGN - 2.00 hours"));
338 
339 	}
340 
341 
342 }