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.earncode.EarnCode;
028    import org.kuali.hr.time.service.base.TkServiceLocator;
029    import org.kuali.hr.time.test.HtmlUnitUtil;
030    import org.kuali.hr.time.test.TkTestConstants;
031    import org.kuali.hr.time.timesheet.TimesheetDocument;
032    import org.kuali.hr.time.util.*;
033    
034    import com.gargoylesoftware.htmlunit.html.HtmlForm;
035    import com.gargoylesoftware.htmlunit.html.HtmlPage;
036    
037    /**
038     * Test data should contain:
039     *
040     * insert into tk_document_header_t values ('2', 'admin', '2011-02-01 00:00:00', 'I', '2011-01-15 00:00:00', NULL, '1');
041     */
042    public class SimpleTimeEntryValidationTest extends KPMETestCase {
043    
044        public static final String USER_PRINCIPAL_ID = "admin";
045            private Date JAN_AS_OF_DATE = new Date((new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
046    
047        @Test
048        /**
049         * Example test to demonstrate adding time blocks to a time sheet using
050         * HTMLUnit rather than JS calls.
051         */
052        public void testExample1ShouldBeValidationErrors() throws Exception {
053            // Load document 2 -- an initiated document 1/15 - 2/01 // 2011
054            String tdocId = "2"; // The timesheet to open.
055            String baseUrl = TkTestConstants.Urls.TIME_DETAIL_URL + "?documentId=" + tdocId;
056            HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(baseUrl);
057            Assert.assertNotNull(page);
058            String pageAsText = page.asText();
059            
060            Assert.assertTrue("Login info not present.", pageAsText.contains("Employee Id:"));
061            Assert.assertTrue("Login info not present.", pageAsText.contains("admin, admin"));
062    
063            HtmlForm form = page.getFormByName("TimeDetailActionForm");
064            Assert.assertNotNull(form);
065    
066            // 1. Obtain User Data
067            Assignment assignment = TkServiceLocator.getAssignmentService().getAssignment(USER_PRINCIPAL_ID, new AssignmentDescriptionKey("30_30_30"), JAN_AS_OF_DATE);
068                    EarnCode earnCode = TkServiceLocator.getEarnCodeService().getEarnCode("RGN", JAN_AS_OF_DATE);
069    
070            TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(tdocId);
071    
072            // 2. Set Timeblock Start and End time
073            // Note - in this case, we're setting time that is outside of the valid
074            // pay period for document 2.
075            DateTime start = new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
076            DateTime end = new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
077    
078            // Build an action form - we're using it as a POJO, it ties into the
079            // existing TK validation setup
080            TimeDetailActionFormBase tdaf = TimeDetailTestUtils.buildDetailActionForm(timesheetDocument, assignment, earnCode, start, end, null, false, null, true);
081            List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, tdaf);
082    
083            // Check for errors
084            Assert.assertEquals("There should be 1 error in this time detail submission", 1, errors.size());
085            Assert.assertEquals("Error String Unexpected", "The start date/time is outside the pay period", errors.get(0));
086        }
087    
088    
089        @Test
090        /**
091         * Example test to demonstrate adding time blocks to a time sheet using
092         * HTMLUnit rather than JS calls.
093         */
094        public void testExample2AddValidTimeBlock() throws Exception {
095            // Load document 2 -- an initiated document 1/15 - 2/01 // 2011
096            String tdocId = "2"; // The timesheet to open.
097            String baseUrl = TkTestConstants.Urls.TIME_DETAIL_URL + "?documentId=" + tdocId;
098            HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(baseUrl);
099            Assert.assertNotNull(page);
100            //HtmlUnitUtil.createTempFile(page, "SimpleTimeEntry");
101            String pageAsText = page.asText();
102            Assert.assertTrue("Login info not present.", pageAsText.contains("Employee Id:"));
103            Assert.assertTrue("Login info not present.", pageAsText.contains("admin, admin"));
104    
105            HtmlForm form = page.getFormByName("TimeDetailActionForm");
106            Assert.assertNotNull(form);
107    
108            // 1. Obtain User Data
109            Assignment assignment = TkServiceLocator.getAssignmentService().getAssignment(USER_PRINCIPAL_ID, new AssignmentDescriptionKey("30_30_30"), JAN_AS_OF_DATE);
110                    EarnCode earnCode = TkServiceLocator.getEarnCodeService().getEarnCode("RGN", JAN_AS_OF_DATE);
111    
112            TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(tdocId);
113    
114            // 2. Set Timeblock Start and End time
115            // 1/18/2011 - 8a to 10a
116            DateTime start = new DateTime(2011, 1, 18, 8, 0, 0, 0, TKUtils.getSystemDateTimeZone());
117            DateTime end = new DateTime(2011, 1, 18, 10, 0, 0, 0, TKUtils.getSystemDateTimeZone());
118    
119            // Build an action form - we're using it as a POJO, it ties into the
120            // existing TK validation setup
121            TimeDetailActionFormBase tdaf = TimeDetailTestUtils.buildDetailActionForm(timesheetDocument, assignment, earnCode, start, end, null, false, null, true);
122            List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, tdaf);
123    
124            // Check for errors
125            Assert.assertEquals("There should be no errors in this time detail submission", 0, errors.size());
126    
127            // Submit the Form to the Page.
128            // Note - This currently uses a less than desirable method to accomplish this...
129            page = TimeDetailTestUtils.submitTimeDetails(baseUrl, tdaf);
130            Assert.assertNotNull(page);
131            //HtmlUnitUtil.createTempFile(page, "TimeBlockPresent");
132    
133            // Verify block present on rendered page.
134            pageAsText = page.asText();
135            Assert.assertTrue("TimeBlock not Present.", pageAsText.contains("08:00 AM - 10:00 AM"));
136            Assert.assertTrue("TimeBlock not Present.", pageAsText.contains("RGN - 2.00 hours"));
137        }
138    
139    
140    }