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.earncode.validation; 017 018 import java.math.BigDecimal; 019 import java.sql.Date; 020 import java.sql.Timestamp; 021 import java.text.DateFormat; 022 import java.text.SimpleDateFormat; 023 import java.util.List; 024 025 import com.gargoylesoftware.htmlunit.html.*; 026 import org.joda.time.DateTime; 027 import org.junit.Assert; 028 import org.junit.Test; 029 import org.kuali.hr.test.KPMETestCase; 030 import org.kuali.hr.time.earncode.EarnCode; 031 import org.kuali.hr.time.test.HtmlUnitUtil; 032 import org.kuali.hr.time.test.TkTestConstants; 033 import org.kuali.hr.time.timeblock.TimeBlock; 034 import org.kuali.hr.time.util.TKUtils; 035 import org.kuali.hr.time.util.TkConstants; 036 import org.kuali.rice.krad.service.KRADServiceLocator; 037 038 public class EarnCodeMaintenaceTest extends KPMETestCase { 039 private static final java.sql.Date TEST_DATE = new Date((new DateTime(2009, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()); 040 private static final String EARN_CODE = "RGN"; 041 private static final DateFormat DATE_FORMAT = new SimpleDateFormat("MM/dd/yy"); 042 043 private static String hrEarnCodeId; 044 private static String timeBlockId; 045 046 @Override 047 public void setUp() throws Exception { 048 super.setUp(); 049 EarnCode earnCode = new EarnCode(); 050 earnCode.setActive(true); 051 earnCode.setEarnCode(EARN_CODE); 052 earnCode.setEffectiveDate(TEST_DATE); 053 earnCode.setRecordMethod("T"); 054 earnCode.setFractionalTimeAllowed("99"); 055 earnCode.setRoundingOption("T"); 056 earnCode.setAffectPay("Y"); 057 earnCode.setWorkmansComp("Y"); 058 earnCode.setEligibleForAccrual("Y"); 059 earnCode.setAllowScheduledLeave("Y"); 060 earnCode.setFmla("Y"); 061 earnCode.setAllowNegativeAccrualBalance("Y"); 062 earnCode.setDescription("RGN Test"); 063 earnCode.setOvtEarnCode(false); 064 earnCode.setInflateMinHours(BigDecimal.ZERO); 065 earnCode.setInflateFactor(BigDecimal.ZERO); 066 067 KRADServiceLocator.getBusinessObjectService().save(earnCode); 068 hrEarnCodeId = earnCode.getHrEarnCodeId(); 069 070 TimeBlock timeBlock = new TimeBlock(); 071 timeBlock.setAmount(BigDecimal.ONE); 072 timeBlock.setAssignmentKey("somedesc"); 073 timeBlock.setJobNumber(new Long(30)); 074 timeBlock.setBeginDate(new java.sql.Date(DATE_FORMAT.parse("01/01/2010").getTime())); 075 timeBlock.setEndDate(new java.sql.Date(DATE_FORMAT.parse("01/03/2010").getTime())); 076 timeBlock.setBeginTimestamp(Timestamp.valueOf("2010-01-01 00:00:00.0")); 077 timeBlock.setEndTimestamp(Timestamp.valueOf("2010-01-03 00:00:00.0")); 078 timeBlock.setEarnCode(EARN_CODE); 079 timeBlock.setWorkArea(new Long(20)); 080 timeBlock.setTask(new Long(15)); 081 timeBlock.setLunchDeleted(false); 082 timeBlock.setBeginTimestampTimezone("TZ"); 083 timeBlock.setTimestamp(Timestamp.valueOf("2010-01-01 00:00:00.0")); 084 timeBlock.setUserPrincipalId("princ"); 085 timeBlock.setHours(BigDecimal.TEN); 086 timeBlock.setClockLogCreated(true); 087 timeBlock.setDocumentId("10039"); 088 089 KRADServiceLocator.getBusinessObjectService().save(timeBlock); 090 timeBlockId = timeBlock.getTkTimeBlockId(); 091 } 092 093 @Override 094 public void tearDown() throws Exception { 095 EarnCode earnCodeObj = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(EarnCode.class, hrEarnCodeId); 096 KRADServiceLocator.getBusinessObjectService().delete(earnCodeObj); 097 TimeBlock timeBlockObj = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(TimeBlock.class, timeBlockId); 098 KRADServiceLocator.getBusinessObjectService().delete(timeBlockObj); 099 super.tearDown(); 100 } 101 102 103 @Test 104 public void testEditExistingEarnCode() throws Exception { 105 HtmlPage earnCodeLookUp = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), TkTestConstants.Urls.EARN_CODE_MAINT_URL); 106 List<HtmlElement> lstElements = earnCodeLookUp.getElementsByIdAndOrName("history"); 107 for(HtmlElement e : lstElements) { 108 HtmlRadioButtonInput radioButton = (HtmlRadioButtonInput) e; 109 if(e.getAttribute("title").equals("Show History - Yes")) { 110 radioButton.setChecked(true); // set show history to yes 111 break; 112 } 113 } 114 earnCodeLookUp = HtmlUnitUtil.clickInputContainingText(earnCodeLookUp, "search"); 115 Assert.assertTrue("Page contains test Earn Code", earnCodeLookUp.asText().contains("RGN Test")); 116 HtmlPage maintPage = HtmlUnitUtil.clickAnchorContainingText(earnCodeLookUp, "edit", hrEarnCodeId.toString()); 117 Assert.assertTrue("Maintenance Page contains Test ",maintPage.asText().contains("RGN Test")); 118 HtmlTextInput text = (HtmlTextInput) maintPage.getHtmlElementById("document.documentHeader.documentDescription"); 119 text.setValueAttribute("test"); 120 HtmlElement element = maintPage.getElementByName("methodToCall.route"); 121 HtmlPage finalPage = element.click(); 122 123 Assert.assertTrue("Maintenance Page doesn't return warning about later effective date", finalPage.asText().contains("A record for this object exists with a later effective date")); 124 HtmlElement yesButton = finalPage.getElementByName("methodToCall.processAnswer.button0"); 125 finalPage = yesButton.click(); 126 Assert.assertTrue("Maintenance Page contains error messages", finalPage.asText().contains("There is a newer version of this Earn Code.")); 127 } 128 129 @Test 130 public void testDeactivateEarnCodeWithActiveTimeBlock() throws Exception { 131 132 EarnCode earnCodeObj = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(EarnCode.class, hrEarnCodeId); 133 134 HtmlPage earnCodeLookUp = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), TkTestConstants.Urls.EARN_CODE_MAINT_URL); 135 List<HtmlElement> lstElements = earnCodeLookUp.getElementsByIdAndOrName("history"); 136 for(HtmlElement e : lstElements) { 137 HtmlRadioButtonInput radioButton = (HtmlRadioButtonInput) e; 138 if(e.getAttribute("title").equals("Show History - Yes")) { 139 radioButton.setChecked(true); // set show history to yes 140 break; 141 } 142 } 143 earnCodeLookUp = HtmlUnitUtil.clickInputContainingText(earnCodeLookUp, "search"); 144 Assert.assertTrue("Page contains test Earn Code", earnCodeLookUp.asText().contains("RGN Test")); 145 HtmlPage maintPage = HtmlUnitUtil.clickAnchorContainingText(earnCodeLookUp, "edit", hrEarnCodeId.toString()); 146 Assert.assertTrue("Maintenance Page contains Test ",maintPage.asText().contains("RGN Test")); 147 HtmlTextInput text = (HtmlTextInput) maintPage.getHtmlElementById("document.documentHeader.documentDescription"); 148 text.setValueAttribute("test"); 149 HtmlCheckBoxInput active = (HtmlCheckBoxInput) maintPage.getHtmlElementById("document.newMaintainableObject.active"); 150 active.setChecked(false); 151 HtmlTextInput effectiveDate = (HtmlTextInput) maintPage.getHtmlElementById("document.newMaintainableObject.effectiveDate"); 152 effectiveDate.setValueAttribute("01/02/2010"); 153 HtmlElement element = maintPage.getElementByName("methodToCall.route"); 154 HtmlPage finalPage = element.click(); 155 Assert.assertTrue("Maintenance Page does not return warning about active timeblock existence.", finalPage.asText().contains("Can not inactivate earn code 'RGN'. It is used in active time blocks")); 156 } 157 158 @Test 159 public void testDeactivateEarnCodeWithInActiveTimeBlock() throws Exception { 160 161 EarnCode earnCodeObj = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(EarnCode.class, hrEarnCodeId); 162 163 HtmlPage earnCodeLookUp = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), TkTestConstants.Urls.EARN_CODE_MAINT_URL); 164 List<HtmlElement> lstElements = earnCodeLookUp.getElementsByIdAndOrName("history"); 165 for(HtmlElement e : lstElements) { 166 HtmlRadioButtonInput radioButton = (HtmlRadioButtonInput) e; 167 if(e.getAttribute("title").equals("Show History - Yes")) { 168 radioButton.setChecked(true); // set show history to yes 169 break; 170 } 171 } 172 earnCodeLookUp = HtmlUnitUtil.clickInputContainingText(earnCodeLookUp, "search"); 173 Assert.assertTrue("Page contains test Earn Code", earnCodeLookUp.asText().contains("RGN Test")); 174 HtmlPage maintPage = HtmlUnitUtil.clickAnchorContainingText(earnCodeLookUp, "edit", hrEarnCodeId.toString()); 175 Assert.assertTrue("Maintenance Page contains Test ",maintPage.asText().contains("RGN Test")); 176 HtmlTextInput text = (HtmlTextInput) maintPage.getHtmlElementById("document.documentHeader.documentDescription"); 177 text.setValueAttribute("test"); 178 HtmlCheckBoxInput active = (HtmlCheckBoxInput) maintPage.getHtmlElementById("document.newMaintainableObject.active"); 179 active.setChecked(false); 180 HtmlTextInput effectiveDate = (HtmlTextInput) maintPage.getHtmlElementById("document.newMaintainableObject.effectiveDate"); 181 effectiveDate.setValueAttribute("01/04/2010"); 182 HtmlElement element = maintPage.getElementByName("methodToCall.route"); 183 HtmlPage finalPage = element.click(); 184 Assert.assertTrue("Maintenance Page should not return warning about active timeblock existence.", !finalPage.asText().contains("Can not inactivate earn code 'RGN'. It is used in active time blocks")); 185 } 186 187 }