1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.clock.web;
17
18 import java.math.BigDecimal;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.Iterator;
23 import java.util.LinkedHashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.joda.time.DateTime;
28 import org.joda.time.DateTimeZone;
29 import org.joda.time.LocalDate;
30 import org.junit.Assert;
31 import org.junit.Ignore;
32 import org.junit.Test;
33 import org.kuali.hr.KPMEWebTestCase;
34 import org.kuali.hr.util.HtmlUnitUtil;
35 import org.kuali.kpme.core.FunctionalTest;
36 import org.kuali.kpme.core.service.HrServiceLocator;
37 import org.kuali.kpme.core.util.HrTestConstants;
38 import org.kuali.kpme.core.util.TKUtils;
39 import org.kuali.kpme.tklm.api.common.TkConstants;
40 import org.kuali.kpme.tklm.api.time.clocklog.ClockLog;
41 import org.kuali.kpme.tklm.api.time.timeblock.TimeBlock;
42 import org.kuali.kpme.tklm.time.clocklog.ClockLogBo;
43 import org.kuali.kpme.tklm.time.rules.graceperiod.GracePeriodRule;
44 import org.kuali.kpme.tklm.time.service.TkServiceLocator;
45 import org.kuali.kpme.tklm.time.timeblock.TimeBlockBo;
46 import org.kuali.kpme.tklm.time.timehourdetail.TimeHourDetailBo;
47 import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
48 import org.kuali.kpme.tklm.time.workflow.TimesheetDocumentHeader;
49 import org.kuali.kpme.tklm.utils.TkTestConstants;
50 import org.kuali.rice.core.api.config.property.ConfigContext;
51 import org.kuali.rice.kns.service.KNSServiceLocator;
52 import org.kuali.rice.krad.service.KRADServiceLocator;
53
54 import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
55 import com.gargoylesoftware.htmlunit.WebClient;
56 import com.gargoylesoftware.htmlunit.html.HtmlElement;
57 import com.gargoylesoftware.htmlunit.html.HtmlPage;
58 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
59
60
61 @FunctionalTest
62 public class ClockWebTest extends KPMEWebTestCase {
63
64 private String tbId;
65
66 public Long maxDocumentId() {
67 Collection aCol = KRADServiceLocatorWeb.getLegacyDataAdapter().findAll(TimesheetDocumentHeader.class);
68 Long maxId = new Long(-1);
69 Iterator<TimesheetDocumentHeader> itr = aCol.iterator();
70 while (itr.hasNext()) {
71 TimesheetDocumentHeader tdh = itr.next();
72 Long temp = new Long(tdh.getDocumentId());
73 if (temp > maxId) {
74 maxId = temp;
75 }
76 }
77 return maxId;
78 }
79
80 public Long maxTimeBlockId() {
81 Collection aCol = KRADServiceLocatorWeb.getLegacyDataAdapter().findAll(TimeBlockBo.class);
82 Long maxId = new Long(-1);
83 Iterator<TimeBlockBo> itr = aCol.iterator();
84 while (itr.hasNext()) {
85 TimeBlockBo tb = itr.next();
86 Long temp = new Long(tb.getTkTimeBlockId());
87 if (temp > maxId) {
88 maxId = temp;
89 }
90 }
91 return maxId;
92 }
93
94 public void createTB() {
95 TimeBlockBo timeBlock = new TimeBlockBo();
96 timeBlock.setUserPrincipalId("admin");
97 timeBlock.setJobNumber(2L);
98 timeBlock.setWorkArea(1234L);
99 timeBlock.setTask(1L);
100 timeBlock.setEarnCode("RGN");
101 timeBlock.setBeginTimestamp(TKUtils.getCurrentTimestamp());
102 timeBlock.setEndTimestamp(TKUtils.getCurrentTimestamp());
103 TimeHourDetailBo timeHourDetail = new TimeHourDetailBo();
104 timeHourDetail.setEarnCode("RGN");
105 timeHourDetail.setHours(new BigDecimal(2.0));
106 timeBlock.getTimeHourDetails().add(timeHourDetail);
107 timeBlock.setHours(new BigDecimal(2.0));
108 List<TimeBlock> tbList = new ArrayList<TimeBlock>();
109 String documentId = this.maxDocumentId().toString();
110 timeBlock.setDocumentId(documentId);
111 tbList.add(TimeBlockBo.to(timeBlock));
112 TkServiceLocator.getTimeBlockService().saveTimeBlocks(tbList);
113
114 tbId = timeBlock.getTkTimeBlockId();
115 TimesheetDocument td = TkServiceLocator.getTimesheetService().getTimesheetDocument(documentId);
116 td.setTimeBlocks(tbList);
117
118 }
119
120 @Ignore
121 public void testDistributeTB() throws Exception {
122 String baseUrl = TkTestConstants.Urls.CLOCK_URL;
123 HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), baseUrl);
124 Assert.assertNotNull(page);
125 Assert.assertTrue("Clock Page contains Distribute Button", page.asText().contains("Distribute Time Blocks"));
126 this.createTB();
127 updateWebClient();
128 HtmlElement element = page.getElementByName("distributeTime");
129 Assert.assertNotNull(element);
130
131
132
133
134
135 String distributeUrl = baseUrl + "?methodToCall=distributeTimeBlocks";
136 HtmlPage page1 = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), distributeUrl);
137 Assert.assertTrue("Distribute Page contains Close button", page1.asText().contains("Close"));
138 Assert.assertTrue("Distribute Page contains Close button", page1.asText().contains("Edit"));
139
140 element = page1.getElementByName("editTimeBlock");
141 Assert.assertNotNull(element);
142 Assert.assertTrue("Onclick attribute of Edit button contains", element.getAttribute("onclick").contains("Clock.do?methodToCall=editTimeBlock&editTimeBlockId="));
143
144 if (tbId == null) {
145 tbId = this.maxTimeBlockId().toString();
146 }
147
148
149 String editUrl = baseUrl + "?methodToCall=editTimeBlock&editTimeBlockId=" + tbId;
150 HtmlPage page3 = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), editUrl);
151
152
153 Assert.assertTrue("Edit Time Blocks Page contains Cancel button", page3.asText().contains("Add"));
154 Assert.assertTrue("Edit Time Blocks Page contains Save button", page3.asText().contains("Save"));
155 Assert.assertTrue("Edit Time Blocks Page contains Cancel button", page3.asText().contains("Cancel"));
156
157 element = page3.getElementByName("addTimeBlock");
158 Assert.assertNotNull(element);
159 Assert.assertTrue("Onclick attribute of Add button contains", element.getAttribute("onclick").contains("javascript: addTimeBlockRow(this.form);"));
160
161 updateWebClient();
162
163
164
165
166 }
167
168 public void updateWebClient() {
169 WebClient webClient = getWebClient();
170 webClient.getOptions().setJavaScriptEnabled(true);
171 webClient.getOptions().setThrowExceptionOnScriptError(false);
172 webClient.setAjaxController(new NicelyResynchronizingAjaxController());
173 webClient.waitForBackgroundJavaScript(10000);
174 }
175
176
177
178
179
180
181
182
183 @Test
184 public void testClockActionWithoutGracePeriodRule() throws Exception {
185
186 GracePeriodRule gpr = TkServiceLocator.getGracePeriodService().getGracePeriodRule(LocalDate.now());
187 if (gpr != null && gpr.isActive()) {
188 gpr.setActive(false);
189 KRADServiceLocatorWeb.getLegacyDataAdapter().save(gpr);
190 }
191
192
193 clockIn();
194
195 ClockLog lastClockLog = TkServiceLocator.getClockLogService().getLastClockLog("admin");
196
197 Assert.assertTrue("The seconds on clock timestamp should be preserved", lastClockLog.getClockDateTime().getSecondOfMinute() != 0);
198 Assert.assertTrue("The seconds on timestamp should be preserved", lastClockLog.getCreateTime().getSecondOfMinute() != 0);
199
200
201 clockOut();
202
203 lastClockLog = TkServiceLocator.getClockLogService().getLastClockLog("admin");
204 Assert.assertTrue("The seconds on clock timestamp should be preserved", lastClockLog.getClockDateTime().getSecondOfMinute() != 0);
205 Assert.assertTrue("The seconds on timestamp should be preserved", lastClockLog.getCreateTime().getSecondOfMinute() != 0);
206 }
207
208 @Test
209 public void testClockActionWithGracePeriodRule() throws Exception {
210
211 KNSServiceLocator.getBusinessObjectService().deleteMatching(
212 ClockLogBo.class,
213 Collections.singletonMap("principalId", "admin"));
214 GracePeriodRule gpr = new GracePeriodRule();
215
216 gpr.setEffectiveLocalDate(new LocalDate(2010, 1, 1));
217 gpr.setHourFactor(new BigDecimal(3));
218 gpr.setTimestamp(TKUtils.getCurrentTimestamp());
219 gpr.setUserPrincipalId("admin");
220
221 gpr.setActive(true);
222 KNSServiceLocator.getBusinessObjectService().save(gpr);
223
224
225 clockIn();
226
227 ClockLog lastClockLog = TkServiceLocator.getClockLogService()
228 .getLastClockLog("admin");
229
230 Assert.assertTrue(
231 "The seconds on clock timestamp should NOT be preserved",
232 lastClockLog.getClockDateTime().getSecondOfMinute() == 0);
233 Assert.assertTrue("The seconds on timestamp should be preserved",
234 lastClockLog.getCreateTime().getSecondOfMinute() != 0);
235
236
237 clockOut();
238
239 lastClockLog = TkServiceLocator.getClockLogService().getLastClockLog(
240 "admin");
241 Assert.assertTrue(
242 "The seconds on clock timestamp should NOT be preserved",
243 lastClockLog.getClockDateTime().getSecondOfMinute() == 0);
244 Assert.assertTrue("The seconds on timestamp should be preserved",
245 lastClockLog.getCreateTime().getSecondOfMinute() != 0);
246 }
247
248
249 @Test
250 public void testClockInOutWithTimezone() throws Exception {
251 HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), TkTestConstants.Urls.CLOCK_URL,true);
252 Assert.assertNotNull(page);
253
254 Map<String, Object> criteria = new LinkedHashMap<String, Object>();
255 criteria.put("selectedAssignment", new String[]{HrTestConstants.FormElementTypes.DROPDOWN, "IU-IN_30_30_30"});
256
257 page = HtmlUnitUtil.fillOutForm(page, criteria);
258 Assert.assertNotNull(page);
259
260 page = HtmlUnitUtil.clickButton(page, "clockAction");
261
262 page = HtmlUnitUtil.clickButton(page, "clockAction");
263 HtmlUnitUtil.createTempFile(page);
264
265 DateTimeZone dateTimeZone = DateTimeZone.forID(HrServiceLocator.getTimezoneService().getUserTimezone());
266 DateTime dateTime = new DateTime().withZone(dateTimeZone);
267 String timeZone = dateTime.toString("zzzz");
268 Assert.assertTrue("Time zone information is incorrect", page.asText().contains(timeZone));
269 }
270
271 private HtmlPage clockIn() throws Exception {
272
273
274 HtmlPage page = clockAction(TkConstants.CLOCK_IN);
275
276
277 HtmlUnitUtil.createTempFile(page);
278 Assert.assertTrue("The clock out button should have displayed", page.asText().contains("Clock Out"));
279
280
281
282 return page;
283 }
284
285 private HtmlPage clockOut() throws Exception {
286 DateTime dateTime = new DateTime();
287 if (dateTime.getSecondOfMinute() >= 58
288 || dateTime.getSecondOfMinute() == 0) {
289 Thread.sleep(4000);
290 }
291
292 HtmlPage page = clockAction(TkConstants.CLOCK_OUT);
293
294
295 HtmlUnitUtil.createTempFile(page);
296 Assert.assertTrue("The clock out button should have displayed", page.asText().contains("Clock In"));
297
298 return page;
299 }
300
301
302
303
304
305
306
307
308
309 private HtmlPage clockAction(String clockAction) throws Exception {
310 DateTime dateTime = new DateTime();
311 if (dateTime.getSecondOfMinute() >= 58
312 || dateTime.getSecondOfMinute() == 0) {
313 Thread.sleep(4000);
314 }
315 String baseUrl = TkTestConstants.Urls.CLOCK_URL;
316 String actionUrl = baseUrl + "?methodToCall=clockAction&selectedAssignment=IU-IN_30_30_30¤tClockAction=" + clockAction;
317 HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), actionUrl);
318 Assert.assertNotNull("The login page shouldn't be null", page);
319 Thread.sleep(3000);
320 return page;
321 }
322
323
324 }