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.lm.leaveCalendar;
017    
018    import com.gargoylesoftware.htmlunit.html.HtmlPage;
019    import org.apache.log4j.Logger;
020    import org.joda.time.DateTime;
021    import org.junit.Assert;
022    import org.kuali.hr.test.KPMETestCase;
023    import org.kuali.hr.time.test.HtmlUnitUtil;
024    import org.kuali.hr.time.test.TkTestConstants;
025    import org.kuali.hr.time.util.TKUtils;
026    import org.kuali.hr.util.filter.TestAutoLoginFilter;
027    import org.kuali.rice.kim.api.identity.Person;
028    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
029    
030    import java.sql.Date;
031    
032    
033    public class LeaveCalendarWebTestBase extends KPMETestCase {
034        private static final Logger LOG = Logger.getLogger(LeaveCalendarWebTestBase.class);
035        public static final Date JAN_AS_OF_DATE = new Date((new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
036        public static final String USER_PRINCIPAL_ID = "admin";
037    
038        private String baseDetailURL = ""; // moved to setUp() method -- initialization order for TkTestConstants problem.
039    
040        @Override
041        public void setUp() throws Exception {
042            super.setUp();
043            setBaseDetailURL(TkTestConstants.Urls.LEAVE_CALENDAR_URL + "?documentId=");
044        }
045    
046        @Override
047        public void tearDown() throws Exception {
048            super.tearDown();
049            //TkLoginFilter.TEST_ID = "admin";
050            TestAutoLoginFilter.OVERRIDE_ID = "";
051        }
052    
053        public String getLeaveCalendarUrl(String tdocId) {
054            return getBaseDetailURL() + tdocId;
055        }
056    
057        /**
058         * Uses an ID hack to manipulate the current Test user Login.
059         *
060         */
061        public synchronized HtmlPage loginAndGetLeaveCalendarHtmlPage(String principalId, String tdocId, boolean assertValid) throws Exception {
062    
063            Person person = KimApiServiceLocator.getPersonService().getPerson(principalId);
064            Assert.assertNotNull(person);
065            Assert.assertEquals(person.getPrincipalId(), principalId);
066            TestAutoLoginFilter.OVERRIDE_ID = principalId;
067            HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), getLeaveCalendarUrl(tdocId));
068            TestAutoLoginFilter.OVERRIDE_ID = "";
069            Assert.assertNotNull(page);
070            HtmlUnitUtil.createTempFile(page, "Login-"+principalId);
071            LOG.debug(page.asText());
072            String pageAsText = page.asText();
073            if (assertValid) {
074                Assert.assertTrue("Login info not present.", pageAsText.contains("Employee Id:"));
075                Assert.assertTrue("Login info not present for " + person.getName() , pageAsText.contains(person.getName()));
076                Assert.assertTrue("Wrong Document Loaded.", pageAsText.contains(tdocId));
077            }
078    
079            return page;
080        }
081    
082        /**
083         * Uses an ID hack to manipulate the current Test user Login.
084         *
085         */
086        public synchronized HtmlPage login(String principalId, String tdocId, boolean assertValid) throws Exception {
087    
088            Person person = KimApiServiceLocator.getPersonService().getPerson(principalId);
089            Assert.assertNotNull(person);
090            Assert.assertEquals(person.getPrincipalId(), principalId);
091            TestAutoLoginFilter.OVERRIDE_ID = principalId;
092            HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), getLeaveCalendarUrl(tdocId));
093            TestAutoLoginFilter.OVERRIDE_ID = "";
094            Assert.assertNotNull(page);
095    
096            return page;
097        }
098        
099        public String getBaseDetailURL() {
100            return baseDetailURL;
101        }
102    
103        public void setBaseDetailURL(String baseDetailURL) {
104            this.baseDetailURL = baseDetailURL;
105        }
106    }