001    /**
002     * Copyright 2004-2012 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.overtime.web;
017    
018    import java.sql.Date;
019    import java.util.*;
020    
021    import org.joda.time.DateTime;
022    import org.json.simple.JSONArray;
023    import org.json.simple.JSONObject;
024    import org.json.simple.JSONValue;
025    import org.junit.Assert;
026    import org.junit.Test;
027    import org.kuali.hr.time.assignment.Assignment;
028    import org.kuali.hr.time.calendar.CalendarEntries;
029    import org.kuali.hr.time.detail.web.TimeDetailActionFormBase;
030    import org.kuali.hr.time.earncode.EarnCode;
031    import org.kuali.hr.time.service.base.TkServiceLocator;
032    import org.kuali.hr.time.timesheet.TimesheetDocument;
033    import org.kuali.hr.time.timesheet.web.TimesheetWebTestBase;
034    import org.kuali.hr.time.util.*;
035    
036    import com.gargoylesoftware.htmlunit.html.HtmlForm;
037    import com.gargoylesoftware.htmlunit.html.HtmlPage;
038    
039    public class DailyOvertimeWebIntegrationTest extends TimesheetWebTestBase {
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    
044        @Test
045        /**
046         * Tests daily overtime for:
047         *
048         * 3/2/2011 10hrs; 8 RGN, 2 OVT
049         * 3/3/2011 10hrs; 8 RGN, 2 OVT
050         */
051        public void testSimpleDOTCalculationIntegration() throws Exception {
052            Date asOfDate = new Date((new DateTime(2011, 3, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
053    
054            CalendarEntries pcd = TkServiceLocator.getCalendarService().getCurrentCalendarDates(USER_PRINCIPAL_ID, asOfDate);
055            Assert.assertNotNull("No PayCalendarDates", pcd);
056    
057            TimesheetDocument tdoc = TkServiceLocator.getTimesheetService().openTimesheetDocument(USER_PRINCIPAL_ID, pcd);
058            String tdocId = tdoc.getDocumentId();
059            HtmlPage page = loginAndGetTimeDetailsHtmlPage("admin", tdocId,true);
060            Assert.assertNotNull(page);
061            HtmlForm form = page.getFormByName("TimeDetailActionForm");
062            Assert.assertNotNull(form);
063    
064            // 1. Obtain User Data
065            List<Assignment> assignments = TkServiceLocator.getAssignmentService().getAssignments(TKContext.getPrincipalId(), JAN_AS_OF_DATE);
066            Assignment assignment = assignments.get(0);
067            List<EarnCode> earnCodes = TkServiceLocator.getEarnCodeService().getEarnCodes(assignment, JAN_AS_OF_DATE);
068            EarnCode earnCode = earnCodes.get(0);
069            Assert.assertEquals("There should be no existing time blocks.", 0, tdoc.getTimeBlocks().size());
070    
071            // 2. Set Timeblock Start and End time
072            // 3/02/2011 - 8:00a to 6:00pm
073            // OVT - 2 Hrs Expected
074            DateTime start = new DateTime(2011, 3, 2, 8, 0, 0, 0, TKUtils.getSystemDateTimeZone());
075            DateTime end = new DateTime(2011, 3, 3, 18, 0, 0, 0, TKUtils.getSystemDateTimeZone());
076    
077            // Build an action form - we're using it as a POJO, it ties into the
078            // existing TK validation setup
079            TimeDetailActionFormBase tdaf = TimeDetailTestUtils.buildDetailActionForm(tdoc, assignment, earnCode, start, end, null, true, null, true);
080            List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, tdaf);
081            // Check for errors
082            Assert.assertEquals("There should be no errors in this time detail submission", 0, errors.size());
083    
084            page = TimeDetailTestUtils.submitTimeDetails(getTimesheetDocumentUrl(tdocId), tdaf);
085            Assert.assertNotNull(page);
086            //HtmlUnitUtil.createTempFile(page, "TimeBlockPresent");
087    
088            // Verify block present on rendered page.
089            String pageAsText = page.asText();
090            //HtmlUnitUtil.createTempFile(page, "Hours");
091            // JSON
092            //
093            //
094            // Grab the timeblock data from the text area. We can check specifics there
095            // to be more fine grained in our validation.
096            String dataText = page.getElementById("timeBlockString").getFirstChild().getNodeValue();
097            JSONArray jsonData = (JSONArray) JSONValue.parse(dataText);
098            final JSONObject jsonDataObject = (JSONObject) jsonData.get(0);
099            Assert.assertTrue("TimeBlock #1 Data Missing.", checkJSONValues(new JSONObject() {{ put("outer", jsonDataObject); }},
100                    new ArrayList<Map<String, Object>>() {{
101                        add(new HashMap<String, Object>() {{
102                            put("earnCode", "RGN");
103                            put("hours", "8.0");
104                        }});
105                        add(new HashMap<String, Object>() {{
106                            put("earnCode", "OVT");
107                            put("hours", "2.0");
108                        }});
109                    }},
110                    new HashMap<String, Object>() {{
111                        put("earnCode", "RGN");
112                        put("startNoTz", "2011-03-02T08:00:00");
113                        put("endNoTz", "2011-03-02T18:00:00");
114                        put("title", "SDR1 Work Area");
115                        put("assignment", "30_30_30");
116                    }}
117            ));
118            final JSONObject jsonDataObject2 = (JSONObject) jsonData.get(1);
119            Assert.assertTrue("TimeBlock #2 Data Missing.", checkJSONValues(new JSONObject() {{ put("outer", jsonDataObject2); }},
120                    new ArrayList<Map<String, Object>>() {{
121                        add(new HashMap<String, Object>() {{
122                            put("earnCode", "RGN");
123                            put("hours", "8.0");
124                        }});
125                        add(new HashMap<String, Object>() {{
126                            put("earnCode", "OVT");
127                            put("hours", "2.0");
128                        }});
129                    }},
130                    new HashMap<String, Object>() {{
131                        put("earnCode", "RGN");
132                        put("startNoTz", "2011-03-03T08:00:00");
133                        put("endNoTz", "2011-03-03T18:00:00");
134                        put("title", "SDR1 Work Area");
135                        put("assignment", "30_30_30");
136                    }}
137            ));
138    
139    
140            // Check the Display Rendered Text for Time Block, Quick Check
141            // Not as accurate as teh checkJSONValues tests above.
142            Assert.assertTrue("TimeBlock not Present.", pageAsText.contains("08:00 AM - 06:00 PM"));
143            Assert.assertTrue("TimeBlock not Present.", pageAsText.contains("RGN - 8.00 hours"));
144            Assert.assertTrue("TimeBlock not Present.", pageAsText.contains("OVT - 2.00 hours"));
145        }
146    
147    }