1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.util;
17
18 import java.math.BigDecimal;
19 import java.net.URLEncoder;
20 import java.util.List;
21
22 import com.gargoylesoftware.htmlunit.WebClient;
23 import org.apache.log4j.Logger;
24 import org.joda.time.DateTime;
25 import org.joda.time.Interval;
26 import org.kuali.hr.time.assignment.Assignment;
27 import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
28 import org.kuali.hr.time.detail.validation.TimeDetailValidationUtil;
29 import org.kuali.hr.time.detail.web.TimeDetailActionFormBase;
30 import org.kuali.hr.time.earncode.EarnCode;
31 import org.kuali.hr.time.test.HtmlUnitUtil;
32 import org.kuali.hr.time.timesheet.TimesheetDocument;
33
34 import com.gargoylesoftware.htmlunit.html.HtmlButton;
35 import com.gargoylesoftware.htmlunit.html.HtmlForm;
36 import com.gargoylesoftware.htmlunit.html.HtmlPage;
37 import org.kuali.hr.util.filter.TestAutoLoginFilter;
38
39 public class TimeDetailTestUtils {
40
41 private static final Logger LOG = Logger.getLogger(TimeDetailTestUtils.class);
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public static TimeDetailActionFormBase buildDetailActionForm(TimesheetDocument timeshetDocument, Assignment assignment, EarnCode earnCode, DateTime start, DateTime end, BigDecimal amount, boolean acrossDays, String timeblockId, boolean spanningWeeks) {
60 TimeDetailActionFormBase tdaf = new TimeDetailActionFormBase();
61
62 BigDecimal hours = null;
63 String startTimeS = null;
64 String endTimeS = null;
65 String startDateS;
66 String endDateS;
67 String selectedEarnCode;
68 String selectedAssignment;
69
70 if (amount == null) {
71 if (start != null && end != null) {
72 Interval se_i = new Interval(start, end);
73 hours = TKUtils.convertMillisToHours(se_i.toDurationMillis());
74 }
75
76
77 startTimeS = start.toString("H:mm");
78 endTimeS = end.toString("H:mm");
79 }
80
81 startDateS = start.toString("MM/dd/YYYY");
82 endDateS = end.toString("MM/dd/YYYY");
83
84 AssignmentDescriptionKey adk = new AssignmentDescriptionKey(assignment);
85 selectedAssignment = adk.toAssignmentKeyString();
86
87 selectedEarnCode = earnCode.getEarnCode();
88
89 tdaf.setAcrossDays(acrossDays ? "y" : "n");
90 tdaf.setSpanningWeeks(spanningWeeks ? "y" : "n");
91 tdaf.setAmount(amount);
92 tdaf.setHours(hours);
93 tdaf.setStartTime(startTimeS);
94 tdaf.setEndTime(endTimeS);
95 tdaf.setStartDate(startDateS);
96 tdaf.setEndDate(endDateS);
97 tdaf.setTkTimeBlockId(timeblockId);
98 tdaf.setTimesheetDocument(timeshetDocument);
99 tdaf.setSelectedAssignment(selectedAssignment);
100 tdaf.setSelectedEarnCode(selectedEarnCode);
101 tdaf.setMethodToCall("addTimeBlock");
102
103 return tdaf;
104 }
105
106
107
108
109
110
111
112
113
114
115 public static List<String> setTimeBlockFormDetails(HtmlForm form, TimeDetailActionFormBase tdaf) {
116
117 List<String> errors = TimeDetailValidationUtil.validateTimeEntryDetails(tdaf);
118
119
120 if (errors.size() == 0) {
121 if (tdaf.getTkTimeBlockId() != null) {
122 form.setAttribute("tkTimeBlockId", tdaf.getTkTimeBlockId().toString());
123 }
124 form.setAttribute("startDate", tdaf.getStartDate());
125 form.setAttribute("endDate", tdaf.getEndDate());
126
127 if (tdaf.getAmount() != null) {
128 form.setAttribute("amount", tdaf.getAmount().toString());
129 } else {
130 form.setAttribute("startTime", tdaf.getStartTime());
131 form.setAttribute("endTime", tdaf.getEndTime());
132 form.setAttribute("hours", tdaf.getHours().toString());
133 }
134
135 form.setAttribute("selectedEarnCode", tdaf.getSelectedEarnCode());
136 form.setAttribute("selectedAssignment", tdaf.getSelectedAssignment());
137 form.setAttribute("acrossDays", tdaf.getAcrossDays());
138 form.setAttribute("methodToCall", tdaf.getMethodToCall());
139 }
140
141 return errors;
142 }
143
144
145
146
147
148
149
150
151
152
153 private static HtmlPage submitTimeDetailsDep(HtmlPage page, HtmlForm form) {
154 HtmlButton submitButton = null;
155
156
157
158 if (submitButton == null) {
159 submitButton = (HtmlButton)page.createElement("button");
160 submitButton.setAttribute("type", "submit");
161 form.appendChild(submitButton);
162 }
163
164 HtmlPage newPage = null;
165 try {
166 submitButton.click();
167 } catch (Exception e) {
168 LOG.error("While submitting time detail form", e);
169 }
170
171 return newPage;
172 }
173
174
175
176
177
178
179
180
181 public static HtmlPage submitTimeDetails(WebClient webClient, String baseUrl, TimeDetailActionFormBase tdaf) {
182
183
184
185
186 String url = baseUrl + buildPostFromFormParams(tdaf);
187 HtmlPage page = null;
188
189 try {
190 page = HtmlUnitUtil.gotoPageAndLogin(webClient, url);
191 } catch (Exception e) {
192 LOG.error("Error while submitting form", e);
193 }
194
195 return page;
196 }
197
198
199
200
201
202
203
204 public static HtmlPage submitTimeDetails(WebClient webClient, String principalId, String baseUrl, TimeDetailActionFormBase tdaf) {
205
206
207
208
209 String url = baseUrl + buildPostFromFormParams(tdaf);
210 HtmlPage page = null;
211
212 try {
213 TestAutoLoginFilter.OVERRIDE_ID = principalId;
214 page = HtmlUnitUtil.gotoPageAndLogin(webClient, url);
215 TestAutoLoginFilter.OVERRIDE_ID = "";
216 } catch (Exception e) {
217 LOG.error("Error while submitting form", e);
218 }
219
220 return page;
221 }
222
223 private static String buildPostFromFormParams(TimeDetailActionFormBase tdaf) {
224 StringBuilder builder = new StringBuilder();
225
226 try {
227 builder.append("&methodToCall=").append(URLEncoder.encode(tdaf.getMethodToCall(), "UTF-8"));
228 builder.append("&acrossDays=").append(URLEncoder.encode(tdaf.getAcrossDays(), "UTF-8"));
229 if (tdaf.getAmount() != null) {
230 builder.append("&amount=").append(URLEncoder.encode(tdaf.getAmount().toString(), "UTF-8"));
231 } else {
232 builder.append("&hours=").append(URLEncoder.encode(tdaf.getHours().toString(), "UTF-8"));
233 builder.append("&startTime=").append(URLEncoder.encode(tdaf.getStartTime(), "UTF-8"));
234 builder.append("&endTime=").append(URLEncoder.encode(tdaf.getEndTime(), "UTF-8"));
235 }
236 builder.append("&startDate=").append(URLEncoder.encode(tdaf.getStartDate(), "UTF-8"));
237 builder.append("&endDate=").append(URLEncoder.encode(tdaf.getEndDate(), "UTF-8"));
238 builder.append("&selectedAssignment=").append(URLEncoder.encode(tdaf.getSelectedAssignment(), "UTF-8"));
239 builder.append("&selectedEarnCode=").append(URLEncoder.encode(tdaf.getSelectedEarnCode(), "UTF-8"));
240 if (tdaf.getTkTimeBlockId() != null) {
241 builder.append("&tkTimeBlockId=").append(URLEncoder.encode(tdaf.getTkTimeBlockId().toString(), "UTF-8"));
242 }
243 } catch (Exception e) {
244 LOG.error("Exception building Post String", e);
245 }
246
247 return builder.toString();
248 }
249 }