001/**
002 * Copyright 2004-2014 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 */
016package org.kuali.hr.time.workflow;
017
018import java.net.URL;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.commons.lang.StringUtils;
023import org.apache.log4j.Logger;
024import org.joda.time.DateTime;
025import org.json.simple.JSONArray;
026import org.json.simple.JSONObject;
027import org.json.simple.JSONValue;
028import org.junit.Assert;
029import org.junit.Ignore;
030import org.kuali.hr.KPMEWebTestCase;
031import org.kuali.hr.TestAutoLoginFilter;
032import org.kuali.hr.util.HtmlUnitUtil;
033import org.kuali.kpme.core.service.HrServiceLocator;
034import org.kuali.kpme.core.util.HrTestConstants;
035import org.kuali.kpme.core.util.TKUtils;
036import org.kuali.kpme.tklm.utils.TkTestConstants;
037import org.kuali.rice.core.api.config.property.ConfigContext;
038import org.kuali.rice.kim.api.identity.Person;
039import org.kuali.rice.kim.api.services.KimApiServiceLocator;
040
041import com.gargoylesoftware.htmlunit.WebClient;
042import com.gargoylesoftware.htmlunit.html.HtmlPage;
043import org.kuali.rice.krad.UserSession;
044import org.kuali.rice.krad.util.GlobalVariables;
045
046@Ignore
047public class TimesheetWebTestBase extends KPMEWebTestCase {
048    private static final Logger LOG = Logger.getLogger(TimesheetWebTestBase.class);
049    public static final DateTime JAN_AS_OF_DATE = new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
050    public static final String USER_PRINCIPAL_ID = "admin";
051    public static String BASE_DETAIL_URL = ""; // moved to setUp() method -- initialization order for TkTestConstants problem.
052
053    /**
054     * @throws Exception
055     */
056    @Override
057    public void setUp() throws Exception {
058        super.setUp();
059        BASE_DETAIL_URL = TkTestConstants.Urls.TIME_DETAIL_URL + "?documentId=";
060    }
061
062    @Override
063    public void tearDown() throws Exception {
064        super.tearDown();
065        //KPMELoginFilter.TEST_ID = "admin";
066        TestAutoLoginFilter.OVERRIDE_ID = "";
067    }
068
069    public static String getTimesheetDocumentUrl(String tdocId) {
070        return BASE_DETAIL_URL + tdocId;
071    }
072    
073    public static String getTargetedTimesheetDocumentUrl(String tdocId, String targetUser) {
074        return HrTestConstants.BASE_URL + "/changeTargetPerson.do?methodToCall=changeTargetPerson&principalName=" + targetUser + "&targetUrl=TimeDetail.do?documentId=" + tdocId;
075    }
076
077    /**
078     * Uses an ID hack to manipulate the current Test user Login.
079     *
080     */
081    public static synchronized HtmlPage loginAndGetTimeDetailsHtmlPage(WebClient webClient, String principalId, String tdocId, boolean assertValid) throws Exception {
082
083        Person person = KimApiServiceLocator.getPersonService().getPerson(principalId);
084        Assert.assertNotNull(person);
085        Assert.assertEquals(person.getPrincipalId(), principalId);
086        TestAutoLoginFilter.OVERRIDE_ID = principalId;
087        // need to create new web client for new user
088        webClient.getPage(new URL(TkTestConstants.Urls.LOG_OUT_URL));
089        webClient.closeAllWindows();
090        HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(webClient, getTimesheetDocumentUrl(tdocId));
091        GlobalVariables.setUserSession(new UserSession(person.getPrincipalName()));
092        TestAutoLoginFilter.OVERRIDE_ID = "";
093        Assert.assertNotNull(page);
094        HtmlUnitUtil.createTempFile(page, "Login-"+principalId);
095        LOG.debug(page.asText());
096        String pageAsText = page.asText();
097        if (assertValid) {
098                Assert.assertTrue("Login info not present.", pageAsText.contains("Employee Id:"));
099                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}