View Javadoc

1   /**
2    * Copyright 2004-2013 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  
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 = ""; // moved to setUp() method -- initialization order for TkTestConstants problem.
50  
51      /**
52       * @throws Exception
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          //KPMELoginFilter.TEST_ID = "admin";
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       * Uses an ID hack to manipulate the current Test user Login.
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          // need to create new web client for new user
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      * Examines the JSON structure that is written to each output TimeDetails
105      * page.
106      * @param json The JSON Object to examine
107      * @param thdList The (optional) list of Time Hour Details values
108      * @param checkValues The list of values to check for in the JSON object
109      * @return true if the JSON object contains the required values, false otherwise.
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              // This is the outer TimeBlock ID -> Time Block Data mapping
116              Map.Entry e = (Map.Entry)eso;
117              JSONObject innerMap = (JSONObject)e.getValue(); // the values we actually care about.
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                  // Null Checks..
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              // Check Time Hour Details
133              if (localContains && thdList != null) {
134                  JSONArray thdjarray = (JSONArray) JSONValue.parse((String) innerMap.get("timeHourDetails"));
135                  // For each user provided THD check element
136                  for (Map<String,Object> u_thd_o : thdList) {
137                      // Look at each Inner TimeHourDetails Map
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 }