001/**
002 * Copyright 2004-2015 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;
043
044@Ignore
045public class TimesheetWebTestBase extends KPMEWebTestCase {
046    private static final Logger LOG = Logger.getLogger(TimesheetWebTestBase.class);
047    public static final DateTime JAN_AS_OF_DATE = new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
048    public static final String USER_PRINCIPAL_ID = "admin";
049    public static String BASE_DETAIL_URL = ""; // moved to setUp() method -- initialization order for TkTestConstants problem.
050
051    /**
052     * @throws Exception
053     */
054    @Override
055    public void setUp() throws Exception {
056        super.setUp();
057        BASE_DETAIL_URL = TkTestConstants.Urls.TIME_DETAIL_URL + "?documentId=";
058    }
059
060    @Override
061    public void tearDown() throws Exception {
062        super.tearDown();
063        //KPMELoginFilter.TEST_ID = "admin";
064        TestAutoLoginFilter.OVERRIDE_ID = "";
065    }
066
067    public static String getTimesheetDocumentUrl(String tdocId) {
068        return BASE_DETAIL_URL + tdocId;
069    }
070    
071    public static String getTargetedTimesheetDocumentUrl(String tdocId, String targetUser) {
072        return HrTestConstants.BASE_URL + "/changeTargetPerson.do?methodToCall=changeTargetPerson&principalName=" + targetUser + "&targetUrl=TimeDetail.do?documentId=" + tdocId;
073    }
074
075    /**
076     * Uses an ID hack to manipulate the current Test user Login.
077     *
078     */
079    public static synchronized HtmlPage loginAndGetTimeDetailsHtmlPage(WebClient webClient, String principalId, String tdocId, boolean assertValid) throws Exception {
080
081        Person person = KimApiServiceLocator.getPersonService().getPerson(principalId);
082        Assert.assertNotNull(person);
083        Assert.assertEquals(person.getPrincipalId(), principalId);
084        TestAutoLoginFilter.OVERRIDE_ID = principalId;
085        // need to create new web client for new user
086        webClient.getPage(new URL(TkTestConstants.Urls.LOG_OUT_URL));
087        webClient.closeAllWindows();
088        HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(webClient, getTimesheetDocumentUrl(tdocId));
089        TestAutoLoginFilter.OVERRIDE_ID = "";
090        Assert.assertNotNull(page);
091        HtmlUnitUtil.createTempFile(page, "Login-"+principalId);
092        LOG.debug(page.asText());
093        String pageAsText = page.asText();
094        if (assertValid) {
095                Assert.assertTrue("Login info not present.", pageAsText.contains("Employee Id:"));
096                Assert.assertTrue("Login info not present for " + person.getName() , pageAsText.contains(person.getName()));
097                Assert.assertTrue("Wrong Document Loaded.", pageAsText.contains(tdocId));
098        }
099
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}