001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.hr.time.timesheet.web;
017    
018    import com.gargoylesoftware.htmlunit.WebClient;
019    import com.gargoylesoftware.htmlunit.html.HtmlPage;
020    import org.apache.commons.lang.StringUtils;
021    import org.apache.log4j.Logger;
022    import org.joda.time.DateTime;
023    import org.json.simple.JSONArray;
024    import org.json.simple.JSONObject;
025    import org.json.simple.JSONValue;
026    import org.junit.Assert;
027    import org.junit.Ignore;
028    import org.kuali.hr.test.KPMETestCase;
029    import org.kuali.hr.time.test.HtmlUnitUtil;
030    import org.kuali.hr.time.test.TkTestConstants;
031    import org.kuali.hr.time.util.TKUtils;
032    import org.kuali.hr.util.filter.TestAutoLoginFilter;
033    import org.kuali.rice.kim.api.identity.Person;
034    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
035    import org.openqa.selenium.logging.NeedsLocalLogs;
036    
037    import java.net.URL;
038    import java.sql.Date;
039    import java.util.List;
040    import java.util.Map;
041    
042    @Ignore
043    public class TimesheetWebTestBase extends KPMETestCase {
044        private static final Logger LOG = Logger.getLogger(TimesheetWebTestBase.class);
045        public static final Date JAN_AS_OF_DATE = new Date((new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
046        public static final String USER_PRINCIPAL_ID = "admin";
047        public static String BASE_DETAIL_URL = ""; // moved to setUp() method -- initialization order for TkTestConstants problem.
048    
049        /**
050         * @throws Exception
051         */
052        @Override
053        public void setUp() throws Exception {
054            super.setUp();
055            BASE_DETAIL_URL = TkTestConstants.Urls.TIME_DETAIL_URL + "?documentId=";
056        }
057    
058        @Override
059        public void tearDown() throws Exception {
060            super.tearDown();
061            //TkLoginFilter.TEST_ID = "admin";
062            TestAutoLoginFilter.OVERRIDE_ID = "";
063        }
064    
065        public static String getTimesheetDocumentUrl(String tdocId) {
066            return BASE_DETAIL_URL + tdocId;
067        }
068    
069        /**
070         * Uses an ID hack to manipulate the current Test user Login.
071         *
072         */
073        public static synchronized HtmlPage loginAndGetTimeDetailsHtmlPage(WebClient webClient, String principalId, String tdocId, boolean assertValid) throws Exception {
074    
075            Person person = KimApiServiceLocator.getPersonService().getPerson(principalId);
076            Assert.assertNotNull(person);
077            Assert.assertEquals(person.getPrincipalId(), principalId);
078            TestAutoLoginFilter.OVERRIDE_ID = principalId;
079            // need to create new web client for new user
080            webClient.getPage(new URL(TkTestConstants.Urls.LOG_OUT_URL));
081            webClient.closeAllWindows();
082            HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(webClient, getTimesheetDocumentUrl(tdocId));
083            TestAutoLoginFilter.OVERRIDE_ID = "";
084            Assert.assertNotNull(page);
085            HtmlUnitUtil.createTempFile(page, "Login-"+principalId);
086            LOG.debug(page.asText());
087            String pageAsText = page.asText();
088            if (assertValid) {
089                    Assert.assertTrue("Login info not present.", pageAsText.contains("Employee Id:"));
090                    Assert.assertTrue("Login info not present for " + person.getName() , pageAsText.contains(person.getName()));
091                    Assert.assertTrue("Wrong Document Loaded.", pageAsText.contains(tdocId));
092            }
093    
094            return page;
095        }
096    
097        /**
098         * Examines the JSON structure that is written to each output TimeDetails
099         * page.
100         * @param json The JSON Object to examine
101         * @param thdList The (optional) list of Time Hour Details values
102         * @param checkValues The list of values to check for in the JSON object
103         * @return true if the JSON object contains the required values, false otherwise.
104         */
105        public static boolean checkJSONValues(JSONObject json, List<Map<String,Object>> thdList, Map<String,Object> checkValues) {
106            boolean contains = false;
107    
108            for(Object eso : json.entrySet()) {
109                 // This is the outer TimeBlock ID -> Time Block Data mapping
110                 Map.Entry e = (Map.Entry)eso;
111                 JSONObject innerMap = (JSONObject)e.getValue(); // the values we actually care about.
112                 boolean localContains = true;
113                 for (Map.Entry<String,Object> cve : checkValues.entrySet()) {
114                     Object joValue = innerMap.get(cve.getKey());
115                     Object cvValue = cve.getValue();
116    
117                     // Null Checks..
118                     if (cvValue == null && joValue == null)
119                         localContains &= true;
120                     else if (joValue == null || cvValue == null)
121                         localContains = false;
122                     else
123                         localContains &= StringUtils.equals(joValue.toString(), cvValue.toString());
124                 }
125    
126                 // Check Time Hour Details
127                 if (localContains && thdList != null) {
128                     JSONArray thdjarray = (JSONArray) JSONValue.parse((String) innerMap.get("timeHourDetails"));
129                     // For each user provided THD check element
130                     for (Map<String,Object> u_thd_o : thdList) {
131                         // Look at each Inner TimeHourDetails Map
132                         boolean thd_l = false;
133                         for (final Object o : thdjarray) {
134                             thd_l |= checkJSONValues(new JSONObject() {{ put("outer", o); }}, null, u_thd_o);
135                         }
136                         localContains &= thd_l;
137                     }
138                 }
139    
140                 contains |= localContains;
141            }
142    
143            return contains;
144        }
145    
146        public static boolean checkJSONValues(String json, List<Map<String,Object>> thdList, Map<String,Object> checkValues) {
147            return checkJSONValues((JSONObject)JSONValue.parse(json), thdList, checkValues);
148        }
149    }