001 /** 002 * Copyright 2004-2013 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.hr.time.timesheet.web; 017 018 import java.sql.Date; 019 import java.util.ArrayList; 020 import java.util.HashMap; 021 import java.util.List; 022 import java.util.Map; 023 024 import org.joda.time.DateTime; 025 import org.joda.time.DateTimeZone; 026 import org.json.simple.JSONArray; 027 import org.json.simple.JSONObject; 028 import org.json.simple.JSONValue; 029 import org.junit.Assert; 030 import org.junit.Test; 031 import org.kuali.hr.time.assignment.Assignment; 032 import org.kuali.hr.time.assignment.AssignmentDescriptionKey; 033 import org.kuali.hr.time.calendar.CalendarEntries; 034 import org.kuali.hr.time.detail.web.TimeDetailActionFormBase; 035 import org.kuali.hr.time.earncode.EarnCode; 036 import org.kuali.hr.time.service.base.TkServiceLocator; 037 import org.kuali.hr.time.test.HtmlUnitUtil; 038 import org.kuali.hr.time.timesheet.TimesheetDocument; 039 import org.kuali.hr.time.util.TKUtils; 040 import org.kuali.hr.time.util.TimeDetailTestUtils; 041 042 import com.gargoylesoftware.htmlunit.html.HtmlForm; 043 import com.gargoylesoftware.htmlunit.html.HtmlPage; 044 045 //@Ignore 046 public class TimesheetIntegrationTest extends TimesheetWebTestBase { 047 048 public static final String USER_PRINCIPAL_ID = "admin"; 049 public static final Date TIME_SHEET_DATE = new Date( 050 (new DateTime(2011, 2, 15, 0, 0, 0, 0, DateTimeZone 051 .forID(TKUtils.getSystemTimeZone()))).getMillis()); 052 public TimesheetDocument timeDoc; 053 public List<Assignment> assignmentsOfUser; 054 public CalendarEntries payCal; 055 public String tdocId; 056 057 /** 058 * @throws Exception 059 */ 060 public void setUp() throws Exception { 061 062 super.setUp(); 063 064 payCal = TkServiceLocator.getCalendarService().getCurrentCalendarDates( 065 USER_PRINCIPAL_ID, TIME_SHEET_DATE); 066 Assert.assertNotNull("Pay calendar entries not found for admin", payCal); 067 068 // retrieving time sheet for according to the pay calendar 069 timeDoc = TkServiceLocator.getTimesheetService().openTimesheetDocument( 070 USER_PRINCIPAL_ID, payCal); 071 tdocId = timeDoc.getDocumentId(); 072 073 // login 074 HtmlPage page = loginAndGetTimeDetailsHtmlPage(USER_PRINCIPAL_ID, 075 tdocId, true); 076 Assert.assertNotNull(page); 077 078 assignmentsOfUser = TkServiceLocator.getAssignmentService() 079 .getAssignments(USER_PRINCIPAL_ID, TIME_SHEET_DATE); 080 Assert.assertNotNull("No Assignments found for the user ", assignmentsOfUser); 081 082 // check if page contains calendar for February 2011 083 Assert.assertTrue("Page could not find calendar for Feb 2011", page.asText() 084 .contains("February 2011")); 085 086 HtmlForm form = page.getFormByName("TimeDetailActionForm"); 087 Assert.assertNotNull(form); 088 } 089 090 public void tearDown() throws Exception { 091 super.tearDown(); 092 } 093 094 @Test 095 public void testAddTimeBlock() throws Exception { 096 HtmlPage page = loginAndGetTimeDetailsHtmlPage(USER_PRINCIPAL_ID, tdocId, true); 097 098 HtmlForm form = page.getFormByName("TimeDetailActionForm"); 099 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 }