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