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 import org.kuali.rice.krad.UserSession;
44 import org.kuali.rice.krad.util.GlobalVariables;
45
46 @Ignore
47 public class TimesheetWebTestBase extends KPMEWebTestCase {
48 private static final Logger LOG = Logger.getLogger(TimesheetWebTestBase.class);
49 public static final DateTime JAN_AS_OF_DATE = new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
50 public static final String USER_PRINCIPAL_ID = "admin";
51 public static String BASE_DETAIL_URL = "";
52
53
54
55
56 @Override
57 public void setUp() throws Exception {
58 super.setUp();
59 BASE_DETAIL_URL = TkTestConstants.Urls.TIME_DETAIL_URL + "?documentId=";
60 }
61
62 @Override
63 public void tearDown() throws Exception {
64 super.tearDown();
65
66 TestAutoLoginFilter.OVERRIDE_ID = "";
67 }
68
69 public static String getTimesheetDocumentUrl(String tdocId) {
70 return BASE_DETAIL_URL + tdocId;
71 }
72
73 public static String getTargetedTimesheetDocumentUrl(String tdocId, String targetUser) {
74 return HrTestConstants.BASE_URL + "/changeTargetPerson.do?methodToCall=changeTargetPerson&principalName=" + targetUser + "&targetUrl=TimeDetail.do?documentId=" + tdocId;
75 }
76
77
78
79
80
81 public static synchronized HtmlPage loginAndGetTimeDetailsHtmlPage(WebClient webClient, String principalId, String tdocId, boolean assertValid) throws Exception {
82
83 Person person = KimApiServiceLocator.getPersonService().getPerson(principalId);
84 Assert.assertNotNull(person);
85 Assert.assertEquals(person.getPrincipalId(), principalId);
86 TestAutoLoginFilter.OVERRIDE_ID = principalId;
87
88 webClient.getPage(new URL(TkTestConstants.Urls.LOG_OUT_URL));
89 webClient.closeAllWindows();
90 HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(webClient, getTimesheetDocumentUrl(tdocId));
91 GlobalVariables.setUserSession(new UserSession(person.getPrincipalName()));
92 TestAutoLoginFilter.OVERRIDE_ID = "";
93 Assert.assertNotNull(page);
94 HtmlUnitUtil.createTempFile(page, "Login-"+principalId);
95 LOG.debug(page.asText());
96 String pageAsText = page.asText();
97 if (assertValid) {
98 Assert.assertTrue("Login info not present.", pageAsText.contains("Employee Id:"));
99 Assert.assertTrue("Login info not present for " + person.getName() , pageAsText.contains(person.getName()));
100 Assert.assertTrue("Wrong Document Loaded.", pageAsText.contains(tdocId));
101 }
102
103 return page;
104 }
105
106
107
108
109
110
111
112
113
114 public static boolean checkJSONValues(JSONObject json, List<Map<String,Object>> thdList, Map<String,Object> checkValues) {
115 boolean contains = false;
116
117 for(Object eso : json.entrySet()) {
118
119 Map.Entry e = (Map.Entry)eso;
120 JSONObject innerMap = (JSONObject)e.getValue();
121 boolean localContains = true;
122 for (Map.Entry<String,Object> cve : checkValues.entrySet()) {
123 Object joValue = innerMap.get(cve.getKey());
124 Object cvValue = cve.getValue();
125
126
127 if (cvValue == null && joValue == null)
128 localContains &= true;
129 else if (joValue == null || cvValue == null)
130 localContains = false;
131 else
132 localContains &= StringUtils.equals(joValue.toString(), cvValue.toString());
133 }
134
135
136 if (localContains && thdList != null) {
137 JSONArray thdjarray = (JSONArray) JSONValue.parse((String) innerMap.get("timeHourDetails"));
138
139 for (Map<String,Object> u_thd_o : thdList) {
140
141 boolean thd_l = false;
142 for (final Object o : thdjarray) {
143 thd_l |= checkJSONValues(new JSONObject() {{ put("outer", o); }}, null, u_thd_o);
144 }
145 localContains &= thd_l;
146 }
147 }
148
149 contains |= localContains;
150 }
151
152 return contains;
153 }
154
155 public static boolean checkJSONValues(String json, List<Map<String,Object>> thdList, Map<String,Object> checkValues) {
156 return checkJSONValues((JSONObject)JSONValue.parse(json), thdList, checkValues);
157 }
158 }