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