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