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