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 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.assignment.Assignment;
034import org.kuali.kpme.core.calendar.entry.CalendarEntry;
035import org.kuali.kpme.core.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("REG", "OVT", "SD1", "BW", "TEST-DEPT", workArea, task, new BigDecimal(8), new BigDecimal("9.00"), null);
071        String tdocId = KPME788_789(
072               new ArrayList<Map<String, Object>>() {{
073                    add(new HashMap<String, Object>() {{
074                        put("earnCode", "RGN");
075                        put("hours", "5.0");
076                    }});
077                }},
078                new HashMap<String, Object>() {{
079                    put("earnCode", "RGN");
080                    put("startNoTz", "2011-03-02T08:00:00");
081                    put("endNoTz", "2011-03-02T13:00:00");
082                    put("title", "SDR1 Work Area");
083                    put("assignment", "30_30_30");
084                }},
085                new ArrayList<Map<String, Object>>() {{
086                    add(new HashMap<String, Object>() {{
087                        put("earnCode", "RGN");
088                        put("hours", "5.0");
089                    }});
090                }},
091                new HashMap<String, Object>() {{
092                    put("earnCode", "RGN");
093                    put("startNoTz", "2011-03-02T13:10:00");
094                    put("endNoTz", "2011-03-02T18:10:00");
095                    put("title", "SDR1 Work Area");
096                    put("assignment", "30_30_30");
097                }}
098        );
099        deleteTimesheet(tdocId);
100
101        createDailyOvertimeRule("REG", "OVT", "SD1", "BW", "TEST-DEPT", workArea, task, new BigDecimal(8), new BigDecimal("10.00"), null);
102        tdocId = KPME788_789(
103                new ArrayList<Map<String, Object>>() {{
104                     add(new HashMap<String, Object>() {{
105                         put("earnCode", "RGN");
106                         put("hours", "5.0");
107                     }});
108                 }},
109                 new HashMap<String, Object>() {{
110                     put("earnCode", "RGN");
111                     put("startNoTz", "2011-03-02T08:00:00");
112                     put("endNoTz", "2011-03-02T13:00:00");
113                     put("title", "SDR1 Work Area");
114                     put("assignment", "30_30_30");
115                 }},
116                 new ArrayList<Map<String, Object>>() {{
117                     add(new HashMap<String, Object>() {{
118                         put("earnCode", "RGN");
119                         put("hours", "3.0");
120                         put("earnCode", "OVT");
121                         put("hours", "2.0");
122                     }});
123                 }},
124                 new HashMap<String, Object>() {{
125                     put("earnCode", "RGN");
126                     put("startNoTz", "2011-03-02T13:10:00");
127                     put("endNoTz", "2011-03-02T18:10:00");
128                     put("title", "SDR1 Work Area");
129                     put("assignment", "30_30_30");
130                 }}
131        );
132        deleteTimesheet(tdocId);
133    }
134
135
136    public void deleteTimesheet(String tdocId) throws Exception {
137        HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), HrTestConstants.Urls.PORTAL_URL);
138        Assert.assertNotNull(page);
139        page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), TkTestConstants.Urls.DELETE_TIMESHEET_URL + "?deleteDocumentId="+tdocId);
140        HtmlUnitUtil.createTempFile(page, "Deleted");
141    }
142
143    public String KPME788_789(ArrayList<Map<String, Object>> tb1ThdItems, HashMap<String, Object> tb1Items, ArrayList<Map<String, Object>> tb2ThdItems, HashMap<String, Object> tb2Items) throws Exception {
144        DateTime asOfDate = new DateTime(2011, 3, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone());
145        CalendarEntry pcd = HrServiceLocator.getCalendarEntryService().getCurrentCalendarDates(USER_PRINCIPAL_ID, asOfDate);
146        Assert.assertNotNull("No PayCalendarDates", pcd);
147
148        TimesheetDocument tdoc = TkServiceLocator.getTimesheetService().openTimesheetDocument(USER_PRINCIPAL_ID, pcd);
149        String tdocId = tdoc.getDocumentId();
150        HtmlPage page = loginAndGetTimeDetailsHtmlPage(getWebClient(), "admin", tdocId, true);
151        Assert.assertNotNull(page);
152        HtmlForm form = page.getFormByName("TimeDetailActionForm");
153        Assert.assertNotNull(form);
154
155        List<Assignment> assignments = HrServiceLocator.getAssignmentService().getAssignments(HrContext.getPrincipalId(), JAN_AS_OF_DATE.toLocalDate());
156        Assignment assignment = assignments.get(0);
157
158        List<EarnCode> earnCodes = TkServiceLocator.getTimesheetService().getEarnCodesForTime(assignment, JAN_AS_OF_DATE.toLocalDate());
159        EarnCode earnCode = earnCodes.get(0);
160        Assert.assertEquals("There should be no existing time blocks.", 0, tdoc.getTimeBlocks().size());
161
162        // 2. Set Timeblock Start and End time
163        // 3/02/2011 - 8:00a to 6:00pm
164        // OVT - 0 Hrs Expected
165        DateTime start = new DateTime(2011, 3, 2, 8, 0, 0, 0, TKUtils.getSystemDateTimeZone());
166        DateTime end = new DateTime(2011, 3, 2, 13, 0, 0, 0, TKUtils.getSystemDateTimeZone());
167        TimeDetailActionFormBase tdaf = TimeDetailTestUtils.buildDetailActionForm(tdoc, assignment, earnCode, start, end, null, false, null, true);
168        List<String> errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, tdaf);
169        Assert.assertEquals("There should be no errors in this time detail submission", 0, errors.size());
170        page = TimeDetailTestUtils.submitTimeDetails(getWebClient(), getTimesheetDocumentUrl(tdocId), tdaf);
171        Assert.assertNotNull(page);
172
173        start = new DateTime(2011, 3, 2, 13, 10, 0, 0, TKUtils.getSystemDateTimeZone());
174        end = new DateTime(2011, 3, 2, 18, 10, 0, 0, TKUtils.getSystemDateTimeZone());
175        tdaf = TimeDetailTestUtils.buildDetailActionForm(tdoc, assignment, earnCode, start, end, null, false, null, true);
176        errors = TimeDetailTestUtils.setTimeBlockFormDetails(form, tdaf);
177        Assert.assertEquals("There should be no errors in this time detail submission", 0, errors.size());
178        page = TimeDetailTestUtils.submitTimeDetails(getWebClient(), getTimesheetDocumentUrl(tdocId), tdaf);
179        HtmlUnitUtil.createTempFile(page, "Hours");
180        Assert.assertNotNull(page);
181
182        // Verify block present on rendered page.
183        String pageAsText = page.asText();
184        // Grab the timeblock data from the text area. We can check specifics there
185        // to be more fine grained in our validation.
186        String dataText = page.getElementById("timeBlockString").getFirstChild().getNodeValue();
187        JSONObject jsonData = (JSONObject) JSONValue.parse(dataText);
188        Assert.assertTrue("TimeBlock #1 Data Missing.", checkJSONValues(jsonData, tb1ThdItems, tb1Items));
189        Assert.assertTrue("TimeBlock #2 Data Missing.", checkJSONValues(jsonData, tb2ThdItems, tb2Items));
190
191        return tdocId;
192    }
193
194    private DailyOvertimeRule createDailyOvertimeRule(String fromEarnGroup, String earnCode, String location, String paytype, String dept, Long workArea, Long task, BigDecimal minHours, BigDecimal maxGap, String overtimePref) {
195        DailyOvertimeRule rule = new DailyOvertimeRule();
196        rule.setEffectiveLocalDate(JAN_AS_OF_DATE.toLocalDate());
197        rule.setFromEarnGroup(fromEarnGroup);
198        rule.setEarnCode(earnCode);
199        rule.setLocation(location);
200        rule.setPaytype(paytype);
201        rule.setDept(dept);
202        rule.setWorkArea(workArea);
203        rule.setMaxGap(maxGap);
204        rule.setMinHours(minHours);
205        rule.setActive(true);
206        TkServiceLocator.getDailyOvertimeRuleService().saveOrUpdate(rule);
207        return rule;
208    }
209}