View Javadoc
1   /**
2    * Copyright 2004-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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 = ""; // moved to setUp() method -- initialization order for TkTestConstants problem.
52  
53      /**
54       * @throws Exception
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          //KPMELoginFilter.TEST_ID = "admin";
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       * Uses an ID hack to manipulate the current Test user Login.
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          // need to create new web client for new user
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      * Examines the JSON structure that is written to each output TimeDetails
108      * page.
109      * @param json The JSON Object to examine
110      * @param thdList The (optional) list of Time Hour Details values
111      * @param checkValues The list of values to check for in the JSON object
112      * @return true if the JSON object contains the required values, false otherwise.
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              // This is the outer TimeBlock ID -> Time Block Data mapping
119              Map.Entry e = (Map.Entry)eso;
120              JSONObject innerMap = (JSONObject)e.getValue(); // the values we actually care about.
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                  // Null Checks..
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              // Check Time Hour Details
136              if (localContains && thdList != null) {
137                  JSONArray thdjarray = (JSONArray) JSONValue.parse((String) innerMap.get("timeHourDetails"));
138                  // For each user provided THD check element
139                  for (Map<String,Object> u_thd_o : thdList) {
140                      // Look at each Inner TimeHourDetails Map
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 }