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