001/**
002 * Copyright 2004-2014 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 java.math.BigDecimal;
019import java.util.ArrayList;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.joda.time.DateTime;
025import org.json.simple.JSONObject;
026import org.json.simple.JSONValue;
027import org.junit.Assert;
028import org.junit.Test;
029import org.kuali.hr.time.util.TimeDetailTestUtils;
030import org.kuali.hr.time.workflow.TimesheetWebTestBase;
031import org.kuali.hr.util.HtmlUnitUtil;
032import org.kuali.kpme.core.FunctionalTest;
033import org.kuali.kpme.core.api.assignment.Assignment;
034import org.kuali.kpme.core.api.calendar.entry.CalendarEntry;
035import org.kuali.kpme.core.api.earncode.EarnCode;
036import org.kuali.kpme.core.service.HrServiceLocator;
037import org.kuali.kpme.core.util.HrContext;
038import org.kuali.kpme.core.util.HrTestConstants;
039import org.kuali.kpme.core.util.TKUtils;
040import org.kuali.kpme.tklm.time.detail.web.TimeDetailActionFormBase;
041import org.kuali.kpme.tklm.time.rules.overtime.daily.DailyOvertimeRule;
042import org.kuali.kpme.tklm.time.service.TkServiceLocator;
043import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
044import org.kuali.kpme.tklm.utils.TkTestConstants;
045
046import com.gargoylesoftware.htmlunit.html.HtmlForm;
047import com.gargoylesoftware.htmlunit.html.HtmlPage;
048
049/**
050 * https://wiki.kuali.org/display/KPME/Test+Cases+-+Business+Logic+Daily+Overtime
051 *
052 * Implementations of tests from this page.
053 */
054@FunctionalTest
055public class DOTIntegrationConfluenceTests extends TimesheetWebTestBase {
056
057    @Test
058    /**
059     * Daily OT - Shift hours met (in a single day) but Max Gap minutes exceeded (in a single gap)
060     * Daily OT - Shift hours met (in a single day) but Max Gap matches exactly
061     *
062     * https://jira.kuali.org/browse/KPME-788
063     * https://jira.kuali.org/browse/KPME-789
064     *
065     */
066    public void testKPME788_789() throws Exception {
067        Long jobNumber = 30L;
068        Long workArea = 30L;
069        Long task = 30L;
070        DailyOvertimeRule rule = createDailyOvertimeRule("IU-IN", "REG", "OVT", 
071                        //"IN", // 05/08 remove location because adding groupkeycode to DailyOvertimeRule // changed from "SD1" to "IN" for changes of adding groupKeyCode to Job
072                        "BW", "TEST-DEPT", workArea, task, new BigDecimal(8), new BigDecimal("9.00"), null);
073        String tdocId = KPME788_789(
074               new ArrayList<Map<String, Object>>() {{
075                    add(new HashMap<String, Object>() {{
076                        put("earnCode", "RGN");
077                        put("hours", "5.0");
078                    }});
079                }},
080                new HashMap<String, Object>() {{
081                    put("earnCode", "RGN");
082                    put("startNoTz", "2011-03-02T08:00:00");
083                    put("endNoTz", "2011-03-02T13:00:00");
084                    put("title", "SDR1 Work Area");
085                    put("assignment", "IU-BL_30_30_30");
086                }},
087                new ArrayList<Map<String, Object>>() {{
088                    add(new HashMap<String, Object>() {{
089                        put("earnCode", "RGN");
090                        put("hours", "5.0");
091                    }});
092                }},
093                new HashMap<String, Object>() {{
094                    put("earnCode", "RGN");
095                    put("startNoTz", "2011-03-02T13:10:00");
096                    put("endNoTz", "2011-03-02T18:10:00");
097                    put("title", "SDR1 Work Area");
098                    put("assignment", "IU-BL_30_30_30");
099                }}
100        );
101        deleteTimesheet(tdocId);
102
103        createDailyOvertimeRule("IN-IU", "REG", "OVT", 
104                        //"IN", // 05/08 remove location because adding groupkeycode to DailyOvertimeRule  // changed from "SD1" to "IN" for changes of adding groupKeyCode to Job
105                        "BW", "TEST-DEPT", workArea, task, new BigDecimal(8), new BigDecimal("10.00"), null);
106        tdocId = KPME788_789(
107                new ArrayList<Map<String, Object>>() {{
108                     add(new HashMap<String, Object>() {{
109                         put("earnCode", "RGN");
110                         put("hours", "5.0");
111                     }});
112                 }},
113                 new HashMap<String, Object>() {{
114                     put("earnCode", "RGN");
115                     put("startNoTz", "2011-03-02T08:00:00");
116                     put("endNoTz", "2011-03-02T13:00:00");
117                     put("title", "SDR1 Work Area");
118                     put("assignment", "IU-BL_30_30_30");
119                 }},
120                 new ArrayList<Map<String, Object>>() {{
121                     add(new HashMap<String, Object>() {{
122                         put("earnCode", "RGN");
123                         put("hours", "3.0");
124                         put("earnCode", "OVT");
125                         put("hours", "2.0");
126                     }});
127                 }},
128                 new HashMap<String, Object>() {{
129                     put("earnCode", "RGN");
130                     put("startNoTz", "2011-03-02T13:10:00");
131                     put("endNoTz", "2011-03-02T18:10:00");
132                     put("title", "SDR1 Work Area");
133                     put("assignment", "IU-BL_30_30_30");
134                 }}
135        );
136        deleteTimesheet(tdocId);
137    }
138
139
140    public void deleteTimesheet(String tdocId) throws Exception {
141        HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), HrTestConstants.Urls.PORTAL_URL);
142        Assert.assertNotNull(page);
143        page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), TkTestConstants.Urls.DELETE_TIMESHEET_URL + "?deleteDocumentId="+tdocId);
144        HtmlUnitUtil.createTempFile(page, "Deleted");
145    }
146
147    public String KPME788_789(ArrayList<Map<String, Object>> tb1ThdItems, HashMap<String, Object> tb1Items, ArrayList<Map<String, Object>> tb2ThdItems, HashMap<String, Object> tb2Items) throws Exception {
148        DateTime asOfDate = new DateTime(2011, 3, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone());
149        CalendarEntry pcd =  HrServiceLocator.getCalendarEntryService().getCurrentCalendarDates(USER_PRINCIPAL_ID, asOfDate);
150        Assert.assertNotNull("No PayCalendarDates", pcd);
151
152        TimesheetDocument tdoc = TkServiceLocator.getTimesheetService().openTimesheetDocument(USER_PRINCIPAL_ID, pcd);
153        String tdocId = tdoc.getDocumentId();
154        HtmlPage page = loginAndGetTimeDetailsHtmlPage(getWebClient(), "admin", tdocId, true);
155        Assert.assertNotNull(page);
156        HtmlForm form = page.getFormByName("TimeDetailActionForm");
157        Assert.assertNotNull(form);
158
159        List<Assignment> assignments = HrServiceLocator.getAssignmentService().getAssignments(HrContext.getPrincipalId(), JAN_AS_OF_DATE.toLocalDate());
160        Assignment assignment = assignments.get(0);
161
162        List<EarnCode> earnCodes = TkServiceLocator.getTimesheetService().getEarnCodesForTime(assignment, JAN_AS_OF_DATE.toLocalDate());
163        EarnCode earnCode = earnCodes.get(0);
164        Assert.assertEquals("There should be no existing time blocks.", 0, tdoc.getTimeBlocks().size());
165
166        // 2. Set Timeblock Start and End time
167        // 3/02/2011 - 8:00a to 6:00pm
168        // OVT - 0 Hrs Expected
169        DateTime start = new DateTime(2011, 3, 2, 8, 0, 0, 0, TKUtils.getSystemDateTimeZone());
170        DateTime end = new DateTime(2011, 3, 2, 13, 0, 0, 0, TKUtils.getSystemDateTimeZone());
171        TimeDetailActionFormBase tdaf = TimeDetailTestUtils.buildDetailActionForm(tdoc, assignment, earnCode, start, end, null, false, null, true, null, null, null, null, null, null);
172        List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, tdaf);
173        Assert.assertEquals("There should be no errors in this time detail submission", 0, errors.size());
174        page = TimeDetailTestUtils.submitTimeDetails(getWebClient(), getTimesheetDocumentUrl(tdocId), tdaf);
175        Assert.assertNotNull(page);
176
177        start = new DateTime(2011, 3, 2, 13, 10, 0, 0, TKUtils.getSystemDateTimeZone());
178        end = new DateTime(2011, 3, 2, 18, 10, 0, 0, TKUtils.getSystemDateTimeZone());
179        tdaf = TimeDetailTestUtils.buildDetailActionForm(tdoc, assignment, earnCode, start, end, null, false, null, true, null, null, null, null, null, null);
180        errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, tdaf);
181        Assert.assertEquals("There should be no errors in this time detail submission", 0, errors.size());
182        page = TimeDetailTestUtils.submitTimeDetails(getWebClient(), getTimesheetDocumentUrl(tdocId), tdaf);
183        HtmlUnitUtil.createTempFile(page, "Hours");
184        Assert.assertNotNull(page);
185
186        // Verify block present on rendered page.
187        String pageAsText = page.asText();
188        // Grab the timeblock data from the text area. We can check specifics there
189        // to be more fine grained in our validation.
190        String dataText = page.getElementById("timeBlockString").getFirstChild().getNodeValue();
191        JSONObject jsonData = (JSONObject) JSONValue.parse(dataText);
192        Assert.assertTrue("TimeBlock #1 Data Missing.", checkJSONValues(jsonData, tb1ThdItems, tb1Items));
193        Assert.assertTrue("TimeBlock #2 Data Missing.", checkJSONValues(jsonData, tb2ThdItems, tb2Items));
194
195        return tdocId;
196    }
197
198    private DailyOvertimeRule createDailyOvertimeRule(String groupKeyCode, String fromEarnGroup, String earnCode, 
199                //String location, 
200                String paytype, String dept, Long workArea, Long task, BigDecimal minHours, BigDecimal maxGap, String overtimePref) {
201        DailyOvertimeRule rule = new DailyOvertimeRule();
202        
203        rule.setGroupKeyCode("IU-IN");
204        rule.setEffectiveLocalDate(JAN_AS_OF_DATE.toLocalDate());
205        rule.setFromEarnGroup(fromEarnGroup);
206        rule.setEarnCode(earnCode);
207        //rule.setLocation(location);
208        rule.setPaytype(paytype);
209        rule.setDept(dept);
210        rule.setWorkArea(workArea);
211        rule.setMaxGap(maxGap);
212        rule.setMinHours(minHours);
213        rule.setActive(true);
214        TkServiceLocator.getDailyOvertimeRuleService().saveOrUpdate(rule);
215        return rule;
216    }
217}