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.timesheet.web;
17  
18  import java.sql.Date;
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.joda.time.DateTime;
24  import org.json.simple.JSONArray;
25  import org.json.simple.JSONObject;
26  import org.json.simple.JSONValue;
27  import org.junit.Assert;
28  import org.junit.Ignore;
29  import org.kuali.hr.test.KPMETestCase;
30  import org.kuali.hr.time.test.HtmlUnitUtil;
31  import org.kuali.hr.time.test.TkTestConstants;
32  import org.kuali.hr.time.util.TKUtils;
33  import org.kuali.hr.time.util.TkConstants;
34  import org.kuali.hr.time.web.TkLoginFilter;
35  import org.kuali.hr.util.filter.TestAutoLoginFilter;
36  import org.kuali.rice.kim.api.identity.Person;
37  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
38  
39  import com.gargoylesoftware.htmlunit.html.HtmlPage;
40  import org.kuali.rice.krad.util.GlobalVariables;
41  
42  @Ignore
43  public class TimesheetWebTestBase extends KPMETestCase {
44  
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 = ""; // moved to setUp() method -- initialization order for TkTestConstants problem.
48  
49      /**
50       * @throws Exception
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          //TkLoginFilter.TEST_ID = "admin";
62          TestAutoLoginFilter.OVERRIDE_ID = "";
63      }
64  
65      public static String getTimesheetDocumentUrl(String tdocId) {
66          return BASE_DETAIL_URL + tdocId;
67      }
68  
69      /**
70       * Uses an ID hack to manipulate the current Test user Login.
71       *
72       */
73      public static synchronized HtmlPage loginAndGetTimeDetailsHtmlPage(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          HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getTimesheetDocumentUrl(tdocId));
80          TestAutoLoginFilter.OVERRIDE_ID = "";
81          Assert.assertNotNull(page);
82          HtmlUnitUtil.createTempFile(page, "Login-"+principalId);
83  
84          String pageAsText = page.asText();
85          if (assertValid) {
86          	Assert.assertTrue("Login info not present.", pageAsText.contains("Employee Id:"));
87          	Assert.assertTrue("Login info not present for " + person.getName() , pageAsText.contains(person.getName()));
88          	Assert.assertTrue("Wrong Document Loaded.", pageAsText.contains(tdocId));
89          }
90  
91          return page;
92      }
93  
94      /**
95       * Examines the JSON structure that is written to each output TimeDetails
96       * page.
97       * @param json The JSON Object to examine
98       * @param thdList The (optional) list of Time Hour Details values
99       * @param checkValues The list of values to check for in the JSON object
100      * @return true if the JSON object contains the required values, false otherwise.
101      */
102     public static boolean checkJSONValues(JSONObject json, List<Map<String,Object>> thdList, Map<String,Object> checkValues) {
103         boolean contains = false;
104 
105         for(Object eso : json.entrySet()) {
106              // This is the outer TimeBlock ID -> Time Block Data mapping
107              Map.Entry e = (Map.Entry)eso;
108              JSONObject innerMap = (JSONObject)e.getValue(); // the values we actually care about.
109              boolean localContains = true;
110              for (Map.Entry<String,Object> cve : checkValues.entrySet()) {
111                  Object joValue = innerMap.get(cve.getKey());
112                  Object cvValue = cve.getValue();
113 
114                  // Null Checks..
115                  if (cvValue == null && joValue == null)
116                      localContains &= true;
117                  else if (joValue == null || cvValue == null)
118                      localContains = false;
119                  else
120                      localContains &= StringUtils.equals(joValue.toString(), cvValue.toString());
121              }
122 
123              // Check Time Hour Details
124              if (localContains && thdList != null) {
125                  JSONArray thdjarray = (JSONArray) JSONValue.parse((String) innerMap.get("timeHourDetails"));
126                  // For each user provided THD check element
127                  for (Map<String,Object> u_thd_o : thdList) {
128                      // Look at each Inner TimeHourDetails Map
129                      boolean thd_l = false;
130                      for (final Object o : thdjarray) {
131                          thd_l |= checkJSONValues(new JSONObject() {{ put("outer", o); }}, null, u_thd_o);
132                      }
133                      localContains &= thd_l;
134                  }
135              }
136 
137              contains |= localContains;
138         }
139 
140         return contains;
141     }
142 
143     public static boolean checkJSONValues(String json, List<Map<String,Object>> thdList, Map<String,Object> checkValues) {
144         return checkJSONValues((JSONObject)JSONValue.parse(json), thdList, checkValues);
145     }
146 }