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