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.detail.web;
017    
018    import java.sql.Date;
019    import java.util.List;
020    
021    import org.joda.time.DateTime;
022    import org.junit.Assert;
023    import org.junit.Test;
024    import org.kuali.hr.test.KPMETestCase;
025    import org.kuali.hr.time.assignment.Assignment;
026    import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
027    import org.kuali.hr.time.calendar.CalendarEntries;
028    import org.kuali.hr.time.earncode.EarnCode;
029    import org.kuali.hr.time.service.base.TkServiceLocator;
030    import org.kuali.hr.time.test.HtmlUnitUtil;
031    import org.kuali.hr.time.test.TkTestConstants;
032    import org.kuali.hr.time.timesheet.TimesheetDocument;
033    import org.kuali.hr.time.util.TKUtils;
034    import org.kuali.hr.time.util.TimeDetailTestUtils;
035    
036    import com.gargoylesoftware.htmlunit.html.HtmlForm;
037    import com.gargoylesoftware.htmlunit.html.HtmlPage;
038    
039    public class SimpleTimeEntryValidationTest extends KPMETestCase {
040    
041        public static final String USER_PRINCIPAL_ID = "admin";
042            private Date JAN_AS_OF_DATE = new Date((new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
043            private Date CALENDAR_ENTRY_DATE = new Date((new DateTime(2011, 1, 15, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
044            
045        @Test
046        /**
047         * Example test to demonstrate adding time blocks to a time sheet using
048         * HTMLUnit rather than JS calls.
049         */
050        public void testExample1ShouldBeValidationErrors() throws Exception {
051            CalendarEntries calendarEntry = TkServiceLocator.getCalendarService().getCurrentCalendarDates(USER_PRINCIPAL_ID, CALENDAR_ENTRY_DATE);
052            TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().openTimesheetDocument(USER_PRINCIPAL_ID, calendarEntry);
053            
054            String baseUrl = TkTestConstants.Urls.TIME_DETAIL_URL + "?documentId=" + timesheetDocument.getDocumentId();
055            HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), baseUrl);
056            Assert.assertNotNull(page);
057            String pageAsText = page.asText();
058            
059            Assert.assertTrue("Login info not present.", pageAsText.contains("Employee Id:"));
060            Assert.assertTrue("Login info not present.", pageAsText.contains("admin, admin"));
061    
062            HtmlForm form = page.getFormByName("TimeDetailActionForm");
063            Assert.assertNotNull(form);
064    
065            // 1. Obtain User Data
066            Assignment assignment = TkServiceLocator.getAssignmentService().getAssignment(USER_PRINCIPAL_ID, new AssignmentDescriptionKey("30_30_30"), JAN_AS_OF_DATE);
067                    EarnCode earnCode = TkServiceLocator.getEarnCodeService().getEarnCode("RGN", JAN_AS_OF_DATE);
068    
069            // 2. Set Timeblock Start and End time
070            // Note - in this case, we're setting time that is outside of the valid
071            // pay period for document 2.
072            DateTime start = new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
073            DateTime end = new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
074    
075            // Build an action form - we're using it as a POJO, it ties into the
076            // existing TK validation setup
077            TimeDetailActionFormBase tdaf = TimeDetailTestUtils.buildDetailActionForm(timesheetDocument, assignment, earnCode, start, end, null, false, null, true);
078            List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, tdaf);
079    
080            // Check for errors
081            Assert.assertEquals("There should be 1 error in this time detail submission", 1, errors.size());
082            Assert.assertEquals("Error String Unexpected", "The start date/time is outside the pay period", errors.get(0));
083        }
084    
085    
086        @Test
087        /**
088         * Example test to demonstrate adding time blocks to a time sheet using
089         * HTMLUnit rather than JS calls.
090         */
091        public void testExample2AddValidTimeBlock() throws Exception {
092            CalendarEntries calendarEntry = TkServiceLocator.getCalendarService().getCurrentCalendarDates(USER_PRINCIPAL_ID, CALENDAR_ENTRY_DATE);
093            TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().openTimesheetDocument(USER_PRINCIPAL_ID, calendarEntry);
094            
095            String baseUrl = TkTestConstants.Urls.TIME_DETAIL_URL + "?documentId=" + timesheetDocument.getDocumentId();
096            HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), baseUrl);
097            Assert.assertNotNull(page);
098            //HtmlUnitUtil.createTempFile(page, "SimpleTimeEntry");
099            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    }