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     */
016    package org.kuali.hr.core.earncode;
017    
018    import java.math.BigDecimal;
019    import java.text.DateFormat;
020    import java.text.SimpleDateFormat;
021    import java.util.List;
022    
023    import org.joda.time.DateTime;
024    import org.junit.Assert;
025    import org.junit.Ignore;
026    import org.junit.Test;
027    import org.kuali.hr.KPMEWebTestCase;
028    import org.kuali.hr.util.HtmlUnitUtil;
029    import org.kuali.kpme.core.FunctionalTest;
030    import org.kuali.kpme.core.earncode.EarnCode;
031    import org.kuali.kpme.core.util.HrTestConstants;
032    import org.kuali.kpme.core.util.TKUtils;
033    import org.kuali.kpme.tklm.time.timeblock.TimeBlock;
034    import org.kuali.rice.krad.service.KRADServiceLocator;
035    
036    import com.gargoylesoftware.htmlunit.html.DomElement;
037    import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
038    import com.gargoylesoftware.htmlunit.html.HtmlElement;
039    import com.gargoylesoftware.htmlunit.html.HtmlInput;
040    import com.gargoylesoftware.htmlunit.html.HtmlPage;
041    import com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput;
042    import com.gargoylesoftware.htmlunit.html.HtmlSelect;
043    import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
044    
045    @FunctionalTest
046    public class EarnCodeMaintenanceTest extends KPMEWebTestCase {
047            private static final DateTime TEST_DATE = new DateTime(2009, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
048            private static final String EARN_CODE = "RGN";
049            private static final DateFormat DATE_FORMAT = new SimpleDateFormat("MM/dd/yy");
050            
051            private static String hrEarnCodeId;
052            private static String timeBlockId;
053            
054            @Override
055            public void setUp() throws Exception {
056                    super.setUp();
057                    EarnCode earnCode = new EarnCode();
058                    earnCode.setActive(true);
059                    earnCode.setEarnCode(EARN_CODE);
060                    earnCode.setEffectiveLocalDate(TEST_DATE.toLocalDate());
061                    earnCode.setRecordMethod("T");
062                    earnCode.setFractionalTimeAllowed("99");
063                    earnCode.setRoundingOption("T");
064                    earnCode.setAffectPay("Y");
065                    earnCode.setWorkmansComp("Y");
066                    earnCode.setEligibleForAccrual("Y");
067                    earnCode.setAllowScheduledLeave("Y");
068                    earnCode.setFmla("Y");
069                    earnCode.setAllowNegativeAccrualBalance("Y");
070                    earnCode.setDescription("RGN Test");
071                    earnCode.setOvtEarnCode(false);
072                    earnCode.setInflateMinHours(BigDecimal.ZERO);
073                    earnCode.setInflateFactor(BigDecimal.ZERO);             
074    
075                    KRADServiceLocator.getBusinessObjectService().save(earnCode);   
076                    hrEarnCodeId = earnCode.getHrEarnCodeId();
077                    
078                    TimeBlock timeBlock = new TimeBlock();
079                    timeBlock.setAmount(BigDecimal.ONE);
080                    timeBlock.setAssignmentKey("somedesc");
081                    timeBlock.setJobNumber(new Long(30));
082                    timeBlock.setBeginDate(DATE_FORMAT.parse("01/01/2010"));
083                    timeBlock.setEndDate(DATE_FORMAT.parse("01/03/2010"));
084                    timeBlock.setBeginDateTime(new DateTime(2010, 1, 1, 0, 0, 0));
085                    timeBlock.setEndDateTime(new DateTime(2010, 1, 3, 0, 0, 0));
086                    timeBlock.setEarnCode(EARN_CODE);
087                    timeBlock.setWorkArea(new Long(20));
088                    timeBlock.setTask(new Long(15));
089                    timeBlock.setLunchDeleted(false);
090                    timeBlock.setTimestamp(TKUtils.getCurrentTimestamp());
091                    timeBlock.setUserPrincipalId("princ");
092                    timeBlock.setHours(BigDecimal.TEN);
093                    timeBlock.setClockLogCreated(true);
094                    timeBlock.setDocumentId("10039");
095    
096                    KRADServiceLocator.getBusinessObjectService().save(timeBlock);
097                    timeBlockId = timeBlock.getTkTimeBlockId();
098            }
099    
100            @Override
101            public void tearDown() throws Exception {
102                    EarnCode earnCodeObj = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(EarnCode.class, hrEarnCodeId);
103                    KRADServiceLocator.getBusinessObjectService().delete(earnCodeObj);
104    /*              TimeBlock timeBlockObj = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(TimeBlock.class, timeBlockId);
105                    KRADServiceLocator.getBusinessObjectService().delete(timeBlockObj);*/
106                    super.tearDown();
107            }
108            
109            @Test
110            public void testEarnCodeMaintenancePage() throws Exception{
111    
112                    HtmlPage earnCodeLookUp = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), HrTestConstants.Urls.EARN_CODE_MAINT_URL);
113                    earnCodeLookUp = HtmlUnitUtil.clickInputContainingText(earnCodeLookUp, "search");
114                    Assert.assertTrue("Page must contain SDR entry", earnCodeLookUp.asText().contains("SDR"));
115                    HtmlPage maintPage = HtmlUnitUtil.clickAnchorContainingText(earnCodeLookUp, "edit","1");
116    
117                    //Sai - confirm that the error is throw by not selecting a record type
118                    HtmlSelect inputText= maintPage.getHtmlElementById("document.newMaintainableObject.recordMethod");
119                    inputText.setDefaultValue("H");
120    
121                    HtmlInput inputForDescription = HtmlUnitUtil.getInputContainingText(
122                                    maintPage, "* Document Description");
123                    inputForDescription.setValueAttribute("Description");
124    //              HtmlUnitUtil.setFieldValue(maintPage, "document.newMaintainableObject.fractionalTimeAllowed", "99");
125    //              HtmlUnitUtil.setFieldValue(maintPage, "document.newMaintainableObject.roundingOption", "T");
126                    
127                    HtmlRadioButtonInput hb = maintPage.getHtmlElementById("document.newMaintainableObject.fractionalTimeAllowed99");
128                    hb.setChecked(true);
129                    
130                    HtmlPage resultantPageAfterEdit = HtmlUnitUtil
131                                    .clickInputContainingText(maintPage, "submit");
132                    LOG.debug(resultantPageAfterEdit.asText());
133    
134    //              assertTrue("Error message for not selecting any record type",
135    //                              resultantPageAfterEdit.asText().contains("For this earn code you must specify Record Hours or Record Time or Record Amount"));
136    
137                    //Sai - confirm that the error is thrown if more than one record type is selected
138    //              checkBox  = maintPage.getHtmlElementById("document.newMaintainableObject.recordTime");
139    //              checkBox.setChecked(true);
140    //              checkBox  = maintPage.getHtmlElementById("document.newMaintainableObject.recordHours");
141    //              checkBox.setChecked(true);
142    
143                    inputForDescription = HtmlUnitUtil.getInputContainingText(
144                                    maintPage, "* Document Description");
145                    inputForDescription.setValueAttribute("Description");
146                    resultantPageAfterEdit = HtmlUnitUtil
147                                    .clickInputContainingText(maintPage, "submit");
148    //              assertTrue("Error message for selecting more than one record type",
149    //                              resultantPageAfterEdit.asText().contains("For this earn code you can only specify one of the Record types"));
150            }
151            
152            @Test
153            public void testEditExistingEarnCode() throws Exception {
154                    HtmlPage earnCodeLookUp = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), HrTestConstants.Urls.EARN_CODE_MAINT_URL);
155                    List<DomElement> lstElements = earnCodeLookUp.getElementsByIdAndOrName("history");
156                    for(DomElement e : lstElements) {
157                            HtmlRadioButtonInput radioButton = (HtmlRadioButtonInput) e;
158                            if(e.getAttribute("title").equals("Show History - Yes")) {
159                                    radioButton.setChecked(true);   // set show history to yes
160                                    break;
161                            }
162                    }
163                    earnCodeLookUp = HtmlUnitUtil.clickInputContainingText(earnCodeLookUp, "search");
164                    Assert.assertTrue("Page contains test Earn Code", earnCodeLookUp.asText().contains("RGN Test"));                
165                    HtmlPage maintPage = HtmlUnitUtil.clickAnchorContainingText(earnCodeLookUp, "edit", hrEarnCodeId.toString());
166                    Assert.assertTrue("Maintenance Page contains Test ",maintPage.asText().contains("RGN Test"));
167                    HtmlTextInput text  = (HtmlTextInput) maintPage.getHtmlElementById("document.documentHeader.documentDescription");
168                    text.setValueAttribute("test");
169                    HtmlElement element = maintPage.getElementByName("methodToCall.route");
170            HtmlPage finalPage = element.click();
171    
172            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"));
173            HtmlElement yesButton = finalPage.getElementByName("methodToCall.processAnswer.button0");
174            finalPage = yesButton.click();
175            Assert.assertTrue("Maintenance Page contains error messages", finalPage.asText().contains("There is a newer version of this Earn Code."));
176            }
177            
178            @Test
179            public void testDeactivateEarnCodeWithActiveTimeBlock() throws Exception {
180                    
181                    HtmlPage earnCodeLookUp = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), HrTestConstants.Urls.EARN_CODE_MAINT_URL);
182                    List<DomElement> lstElements = earnCodeLookUp.getElementsByIdAndOrName("history");
183                    for(DomElement e : lstElements) {
184                            HtmlRadioButtonInput radioButton = (HtmlRadioButtonInput) e;
185                            if(e.getAttribute("title").equals("Show History - Yes")) {
186                                    radioButton.setChecked(true);   // set show history to yes
187                                    break;
188                            }
189                    }
190                    earnCodeLookUp = HtmlUnitUtil.clickInputContainingText(earnCodeLookUp, "search");
191                    Assert.assertTrue("Page contains test Earn Code", earnCodeLookUp.asText().contains("RGN Test"));                
192                    HtmlPage maintPage = HtmlUnitUtil.clickAnchorContainingText(earnCodeLookUp, "edit", hrEarnCodeId.toString());
193                    Assert.assertTrue("Maintenance Page contains Test ",maintPage.asText().contains("RGN Test"));
194                    HtmlTextInput text  = (HtmlTextInput) maintPage.getHtmlElementById("document.documentHeader.documentDescription");
195                    text.setValueAttribute("test");
196                    HtmlCheckBoxInput active = (HtmlCheckBoxInput) maintPage.getHtmlElementById("document.newMaintainableObject.active");
197                    active.setChecked(false);
198                    HtmlTextInput effectiveDate = (HtmlTextInput) maintPage.getHtmlElementById("document.newMaintainableObject.effectiveDate");
199                    effectiveDate.setValueAttribute("01/02/2010");
200                    HtmlElement element = maintPage.getElementByName("methodToCall.route");
201            HtmlPage finalPage = element.click();
202            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"));
203            }
204            
205            @Test
206            public void testDeactivateEarnCodeWithInActiveTimeBlock() throws Exception {
207                    
208                    HtmlPage earnCodeLookUp = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), HrTestConstants.Urls.EARN_CODE_MAINT_URL);
209                    List<DomElement> lstElements = earnCodeLookUp.getElementsByIdAndOrName("history");
210                    for(DomElement e : lstElements) {
211                            HtmlRadioButtonInput radioButton = (HtmlRadioButtonInput) e;
212                            if(e.getAttribute("title").equals("Show History - Yes")) {
213                                    radioButton.setChecked(true);   // set show history to yes
214                                    break;
215                            }
216                    }
217                    earnCodeLookUp = HtmlUnitUtil.clickInputContainingText(earnCodeLookUp, "search");
218                    Assert.assertTrue("Page contains test Earn Code", earnCodeLookUp.asText().contains("RGN Test"));                
219                    HtmlPage maintPage = HtmlUnitUtil.clickAnchorContainingText(earnCodeLookUp, "edit", hrEarnCodeId.toString());
220                    Assert.assertTrue("Maintenance Page contains Test ",maintPage.asText().contains("RGN Test"));
221                    HtmlTextInput text  = (HtmlTextInput) maintPage.getHtmlElementById("document.documentHeader.documentDescription");
222                    text.setValueAttribute("test");
223                    HtmlCheckBoxInput active = (HtmlCheckBoxInput) maintPage.getHtmlElementById("document.newMaintainableObject.active");
224                    active.setChecked(false);
225                    HtmlTextInput effectiveDate = (HtmlTextInput) maintPage.getHtmlElementById("document.newMaintainableObject.effectiveDate");
226                    effectiveDate.setValueAttribute("01/04/2010");
227                    HtmlElement element = maintPage.getElementByName("methodToCall.route");
228            HtmlPage finalPage = element.click();
229            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"));
230            }
231            
232    }