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.detail.web;
17  
18  import java.sql.Date;
19  import java.util.List;
20  
21  import org.joda.time.DateTime;
22  import org.junit.Assert;
23  import org.junit.Test;
24  import org.kuali.hr.test.KPMETestCase;
25  import org.kuali.hr.time.assignment.Assignment;
26  import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
27  import org.kuali.hr.time.calendar.CalendarEntries;
28  import org.kuali.hr.time.earncode.EarnCode;
29  import org.kuali.hr.time.service.base.TkServiceLocator;
30  import org.kuali.hr.time.test.HtmlUnitUtil;
31  import org.kuali.hr.time.test.TkTestConstants;
32  import org.kuali.hr.time.timesheet.TimesheetDocument;
33  import org.kuali.hr.time.util.TKUtils;
34  import org.kuali.hr.time.util.TimeDetailTestUtils;
35  
36  import com.gargoylesoftware.htmlunit.html.HtmlForm;
37  import com.gargoylesoftware.htmlunit.html.HtmlPage;
38  
39  public class SimpleTimeEntryValidationTest extends KPMETestCase {
40  
41      public static final String USER_PRINCIPAL_ID = "admin";
42  	private Date JAN_AS_OF_DATE = new Date((new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
43  	private Date CALENDAR_ENTRY_DATE = new Date((new DateTime(2011, 1, 15, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
44  	
45      @Test
46      /**
47       * Example test to demonstrate adding time blocks to a time sheet using
48       * HTMLUnit rather than JS calls.
49       */
50      public void testExample1ShouldBeValidationErrors() throws Exception {
51      	CalendarEntries calendarEntry = TkServiceLocator.getCalendarService().getCurrentCalendarDates(USER_PRINCIPAL_ID, CALENDAR_ENTRY_DATE);
52      	TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().openTimesheetDocument(USER_PRINCIPAL_ID, calendarEntry);
53      	
54          String baseUrl = TkTestConstants.Urls.TIME_DETAIL_URL + "?documentId=" + timesheetDocument.getDocumentId();
55          HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), baseUrl);
56          Assert.assertNotNull(page);
57          String pageAsText = page.asText();
58          
59          Assert.assertTrue("Login info not present.", pageAsText.contains("Employee Id:"));
60          Assert.assertTrue("Login info not present.", pageAsText.contains("admin, admin"));
61  
62          HtmlForm form = page.getFormByName("TimeDetailActionForm");
63          Assert.assertNotNull(form);
64  
65          // 1. Obtain User Data
66          Assignment assignment = TkServiceLocator.getAssignmentService().getAssignment(USER_PRINCIPAL_ID, new AssignmentDescriptionKey("30_30_30"), JAN_AS_OF_DATE);
67  		EarnCode earnCode = TkServiceLocator.getEarnCodeService().getEarnCode("RGN", JAN_AS_OF_DATE);
68  
69          // 2. Set Timeblock Start and End time
70          // Note - in this case, we're setting time that is outside of the valid
71          // pay period for document 2.
72          DateTime start = new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
73          DateTime end = new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
74  
75          // Build an action form - we're using it as a POJO, it ties into the
76          // existing TK validation setup
77          TimeDetailActionFormBase tdaf = TimeDetailTestUtils.buildDetailActionForm(timesheetDocument, assignment, earnCode, start, end, null, false, null, true);
78          List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, tdaf);
79  
80          // Check for errors
81          Assert.assertEquals("There should be 1 error in this time detail submission", 1, errors.size());
82          Assert.assertEquals("Error String Unexpected", "The start date/time is outside the pay period", errors.get(0));
83      }
84  
85  
86      @Test
87      /**
88       * Example test to demonstrate adding time blocks to a time sheet using
89       * HTMLUnit rather than JS calls.
90       */
91      public void testExample2AddValidTimeBlock() throws Exception {
92      	CalendarEntries calendarEntry = TkServiceLocator.getCalendarService().getCurrentCalendarDates(USER_PRINCIPAL_ID, CALENDAR_ENTRY_DATE);
93      	TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().openTimesheetDocument(USER_PRINCIPAL_ID, calendarEntry);
94      	
95          String baseUrl = TkTestConstants.Urls.TIME_DETAIL_URL + "?documentId=" + timesheetDocument.getDocumentId();
96          HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), baseUrl);
97          Assert.assertNotNull(page);
98          //HtmlUnitUtil.createTempFile(page, "SimpleTimeEntry");
99          String pageAsText = page.asText();
100         Assert.assertTrue("Login info not present.", pageAsText.contains("Employee Id:"));
101         Assert.assertTrue("Login info not present.", pageAsText.contains("admin, admin"));
102 
103         HtmlForm form = page.getFormByName("TimeDetailActionForm");
104         Assert.assertNotNull(form);
105 
106         // 1. Obtain User Data
107         Assignment assignment = TkServiceLocator.getAssignmentService().getAssignment(USER_PRINCIPAL_ID, new AssignmentDescriptionKey("30_30_30"), JAN_AS_OF_DATE);
108 		EarnCode earnCode = TkServiceLocator.getEarnCodeService().getEarnCode("RGN", JAN_AS_OF_DATE);
109 
110         // 2. Set Timeblock Start and End time
111         // 1/18/2011 - 8a to 10a
112         DateTime start = new DateTime(2011, 1, 18, 8, 0, 0, 0, TKUtils.getSystemDateTimeZone());
113         DateTime end = new DateTime(2011, 1, 18, 10, 0, 0, 0, TKUtils.getSystemDateTimeZone());
114 
115         // Build an action form - we're using it as a POJO, it ties into the
116         // existing TK validation setup
117         TimeDetailActionFormBase tdaf = TimeDetailTestUtils.buildDetailActionForm(timesheetDocument, assignment, earnCode, start, end, null, false, null, true);
118         List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, tdaf);
119 
120         // Check for errors
121         Assert.assertEquals("There should be no errors in this time detail submission", 0, errors.size());
122 
123         // Submit the Form to the Page.
124         // Note - This currently uses a less than desirable method to accomplish this...
125         page = TimeDetailTestUtils.submitTimeDetails(getWebClient(), baseUrl, tdaf);
126         Assert.assertNotNull(page);
127         //HtmlUnitUtil.createTempFile(page, "TimeBlockPresent");
128 
129         // Verify block present on rendered page.
130         pageAsText = page.asText();
131         Assert.assertTrue("TimeBlock not Present.", pageAsText.contains("08:00 AM - 10:00 AM"));
132         Assert.assertTrue("TimeBlock not Present.", pageAsText.contains("RGN - 2.00 hours"));
133     }
134 
135 
136 }