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