1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.timesheet.web;
17
18 import com.gargoylesoftware.htmlunit.WebClient;
19 import com.gargoylesoftware.htmlunit.html.HtmlPage;
20 import org.apache.commons.lang.StringUtils;
21 import org.apache.log4j.Logger;
22 import org.joda.time.DateTime;
23 import org.json.simple.JSONArray;
24 import org.json.simple.JSONObject;
25 import org.json.simple.JSONValue;
26 import org.junit.Assert;
27 import org.junit.Ignore;
28 import org.kuali.hr.test.KPMETestCase;
29 import org.kuali.hr.time.test.HtmlUnitUtil;
30 import org.kuali.hr.time.test.TkTestConstants;
31 import org.kuali.hr.time.util.TKUtils;
32 import org.kuali.hr.util.filter.TestAutoLoginFilter;
33 import org.kuali.rice.kim.api.identity.Person;
34 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
35 import org.openqa.selenium.logging.NeedsLocalLogs;
36
37 import java.net.URL;
38 import java.sql.Date;
39 import java.util.List;
40 import java.util.Map;
41
42 @Ignore
43 public class TimesheetWebTestBase extends KPMETestCase {
44 private static final Logger LOG = Logger.getLogger(TimesheetWebTestBase.class);
45 public static final Date JAN_AS_OF_DATE = new Date((new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
46 public static final String USER_PRINCIPAL_ID = "admin";
47 public static String BASE_DETAIL_URL = "";
48
49
50
51
52 @Override
53 public void setUp() throws Exception {
54 super.setUp();
55 BASE_DETAIL_URL = TkTestConstants.Urls.TIME_DETAIL_URL + "?documentId=";
56 }
57
58 @Override
59 public void tearDown() throws Exception {
60 super.tearDown();
61
62 TestAutoLoginFilter.OVERRIDE_ID = "";
63 }
64
65 public static String getTimesheetDocumentUrl(String tdocId) {
66 return BASE_DETAIL_URL + tdocId;
67 }
68
69
70
71
72
73 public static synchronized HtmlPage loginAndGetTimeDetailsHtmlPage(WebClient webClient, String principalId, String tdocId, boolean assertValid) throws Exception {
74
75 Person person = KimApiServiceLocator.getPersonService().getPerson(principalId);
76 Assert.assertNotNull(person);
77 Assert.assertEquals(person.getPrincipalId(), principalId);
78 TestAutoLoginFilter.OVERRIDE_ID = principalId;
79
80 webClient.getPage(new URL(TkTestConstants.Urls.LOG_OUT_URL));
81 webClient.closeAllWindows();
82 HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(webClient, getTimesheetDocumentUrl(tdocId));
83 TestAutoLoginFilter.OVERRIDE_ID = "";
84 Assert.assertNotNull(page);
85 HtmlUnitUtil.createTempFile(page, "Login-"+principalId);
86 LOG.debug(page.asText());
87 String pageAsText = page.asText();
88 if (assertValid) {
89 Assert.assertTrue("Login info not present.", pageAsText.contains("Employee Id:"));
90 Assert.assertTrue("Login info not present for " + person.getName() , pageAsText.contains(person.getName()));
91 Assert.assertTrue("Wrong Document Loaded.", pageAsText.contains(tdocId));
92 }
93
94 return page;
95 }
96
97
98
99
100
101
102
103
104
105 public static boolean checkJSONValues(JSONObject json, List<Map<String,Object>> thdList, Map<String,Object> checkValues) {
106 boolean contains = false;
107
108 for(Object eso : json.entrySet()) {
109
110 Map.Entry e = (Map.Entry)eso;
111 JSONObject innerMap = (JSONObject)e.getValue();
112 boolean localContains = true;
113 for (Map.Entry<String,Object> cve : checkValues.entrySet()) {
114 Object joValue = innerMap.get(cve.getKey());
115 Object cvValue = cve.getValue();
116
117
118 if (cvValue == null && joValue == null)
119 localContains &= true;
120 else if (joValue == null || cvValue == null)
121 localContains = false;
122 else
123 localContains &= StringUtils.equals(joValue.toString(), cvValue.toString());
124 }
125
126
127 if (localContains && thdList != null) {
128 JSONArray thdjarray = (JSONArray) JSONValue.parse((String) innerMap.get("timeHourDetails"));
129
130 for (Map<String,Object> u_thd_o : thdList) {
131
132 boolean thd_l = false;
133 for (final Object o : thdjarray) {
134 thd_l |= checkJSONValues(new JSONObject() {{ put("outer", o); }}, null, u_thd_o);
135 }
136 localContains &= thd_l;
137 }
138 }
139
140 contains |= localContains;
141 }
142
143 return contains;
144 }
145
146 public static boolean checkJSONValues(String json, List<Map<String,Object>> thdList, Map<String,Object> checkValues) {
147 return checkJSONValues((JSONObject)JSONValue.parse(json), thdList, checkValues);
148 }
149 }