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