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.earngroup.validation;
017    
018    import java.math.BigDecimal;
019    import java.util.ArrayList;
020    import java.util.Calendar;
021    import java.util.Collections;
022    import java.util.List;
023    
024    import org.apache.commons.lang.StringUtils;
025    import org.apache.log4j.Logger;
026    import org.junit.Assert;
027    import org.junit.Test;
028    import org.kuali.hr.test.KPMETestCase;
029    import org.kuali.hr.time.earncode.EarnCode;
030    import org.kuali.hr.time.earncodegroup.EarnCodeGroup;
031    import org.kuali.hr.time.earncodegroup.EarnCodeGroupDefinition;
032    import org.kuali.hr.time.test.HtmlUnitUtil;
033    import org.kuali.hr.time.test.TkTestConstants;
034    import org.kuali.rice.krad.service.KRADServiceLocator;
035    
036    import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
037    import com.gargoylesoftware.htmlunit.html.HtmlElement;
038    import com.gargoylesoftware.htmlunit.html.HtmlInput;
039    import com.gargoylesoftware.htmlunit.html.HtmlPage;
040    import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
041    
042    public class EarnCodeGroupMaintenanceTest extends KPMETestCase {
043        private static final Logger LOG = Logger.getLogger(EarnCodeGroupMaintenanceTest.class);
044        private static final java.sql.Date TEST_DATE = new java.sql.Date(Calendar.getInstance().getTimeInMillis());
045        private static final String EARN_CODE = "RGN";
046        private static String hrEarnGroupId;
047        private static String hrEarnCodeId;
048        private static String hrEarnGroupIdRGG;
049    
050        @Override
051        public void setUp() throws Exception {
052            super.setUp();
053            List<EarnCodeGroupDefinition> earnGroups = new ArrayList<EarnCodeGroupDefinition>();
054            EarnCodeGroupDefinition definition = new EarnCodeGroupDefinition();
055            definition.setEarnCode(EARN_CODE);
056            earnGroups.add(definition);
057    
058            EarnCodeGroup earnGroup = new EarnCodeGroup();
059            earnGroup.setEarnCodeGroup("testGroup");
060            earnGroup.setDescr("test");
061            earnGroup.setEffectiveDate(TEST_DATE);
062            earnGroup.setShowSummary(true);
063            earnGroup.setActive(true);
064            earnGroup.setEarnCodeGroups(earnGroups);
065            KRADServiceLocator.getBusinessObjectService().save(earnGroup);
066            hrEarnGroupId = earnGroup.getHrEarnCodeGroupId();
067    
068            // Set up earn code RGG in tk-earn_code_t
069            EarnCode earnCode = new EarnCode();
070            earnCode.setActive(true);
071            earnCode.setEarnCode("RGG");
072            earnCode.setEffectiveDate(TEST_DATE);
073            earnCode.setRecordMethod("T");
074            earnCode.setFractionalTimeAllowed("99");
075            earnCode.setRoundingOption("T");
076            earnCode.setEligibleForAccrual("Y");
077            earnCode.setAffectPay("Y");
078            earnCode.setAllowScheduledLeave("Y");
079            earnCode.setFmla("Y");
080            earnCode.setWorkmansComp("Y");
081            earnCode.setAllowNegativeAccrualBalance("Y");
082            earnCode.setDescription("RGG");
083            earnCode.setOvtEarnCode(false);
084            earnCode.setInflateMinHours(BigDecimal.ZERO);
085            earnCode.setInflateFactor(BigDecimal.ZERO);
086            earnCode = KRADServiceLocator.getBusinessObjectService().save(earnCode);
087            hrEarnCodeId = earnCode.getHrEarnCodeId();
088    
089            // Set up earn group RGG in tk-earn_group_t
090            EarnCodeGroup earnGroupRGG = new EarnCodeGroup();
091            earnGroupRGG.setEarnCodeGroup("RGG");
092            earnGroupRGG.setDescr("test RGG");
093            earnGroupRGG.setEffectiveDate(TEST_DATE);
094            earnGroupRGG.setShowSummary(true);
095            earnGroupRGG.setActive(true);
096            earnGroupRGG = KRADServiceLocator.getBusinessObjectService().save(earnGroupRGG);
097            hrEarnGroupIdRGG = earnGroupRGG.getHrEarnCodeGroupId();
098        }
099    
100        @Override
101        public void tearDown() throws Exception {
102            EarnCodeGroup earnGroupObj = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(EarnCodeGroup.class, hrEarnGroupId);
103            KRADServiceLocator.getBusinessObjectService().delete(earnGroupObj);
104    
105            EarnCodeGroup earnGroupObjRGG = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(EarnCodeGroup.class, hrEarnGroupIdRGG);
106            KRADServiceLocator.getBusinessObjectService().delete(earnGroupObjRGG);
107    
108            if (StringUtils.isNotBlank(hrEarnCodeId)) {
109                EarnCode earnCodeObj = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(EarnCode.class, Collections.singletonMap("hrEarnCodeId", hrEarnCodeId));
110                KRADServiceLocator.getBusinessObjectService().delete(earnCodeObj);
111            }
112            super.tearDown();
113        }
114    
115        @Test
116        public void testEditExistingEarnGroup() throws Exception {
117            HtmlPage earnGroupLookUp = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), TkTestConstants.Urls.EARN_CODE_GROUP_MAINT_URL);
118            earnGroupLookUp = HtmlUnitUtil.clickInputContainingText(earnGroupLookUp, "search");
119            Assert.assertTrue("Page contains test Earn Group", earnGroupLookUp.asText().contains("test"));
120            HtmlPage maintPage = HtmlUnitUtil.clickAnchorContainingText(earnGroupLookUp, "edit", hrEarnGroupId.toString());
121            Assert.assertTrue("Maintenance Page contains test ClockLog", maintPage.asText().contains("test"));
122            HtmlTextInput text = (HtmlTextInput) maintPage.getHtmlElementById("document.documentHeader.documentDescription");
123            text.setValueAttribute("test1");
124    
125            // pull out earn group RGG to edit.
126            earnGroupLookUp = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), TkTestConstants.Urls.EARN_CODE_GROUP_MAINT_URL);
127            earnGroupLookUp = HtmlUnitUtil.clickInputContainingText(earnGroupLookUp, "search");
128            Assert.assertTrue("Page contains test Earn Group", earnGroupLookUp.asText().contains("test RGG"));
129            HtmlPage testEditRGGPage = HtmlUnitUtil.clickAnchorContainingText(earnGroupLookUp, "edit", hrEarnGroupIdRGG.toString());
130            Assert.assertTrue("Maintenance Page contains test ClockLog", testEditRGGPage.asText().contains("test RGG"));
131            text = (HtmlTextInput) testEditRGGPage.getHtmlElementById("document.documentHeader.documentDescription");
132            text.setValueAttribute("testEditRGG");
133    
134            // Add a new Earn Code Group Definition
135            text = (HtmlTextInput) testEditRGGPage.getHtmlElementById("document.newMaintainableObject.add.earnCodeGroups.earnCode");
136            text.setValueAttribute("RGG");
137            HtmlElement element = HtmlUnitUtil.getInputContainingText(testEditRGGPage, "methodToCall.addLine.earnCodeGroups");
138            HtmlPage newCodeAddedPage = element.click();
139    //        HtmlUnitUtil.createTempFile(newCodeAddedPage);
140            Assert.assertFalse("Page contains Error", newCodeAddedPage.asText().contains("error"));
141    
142            // Delete this Earn Code Group Definition
143    //        element = HtmlUnitUtil.getInputContainingText(newCodeAddedPage,"methodToCall.deleteLine.earnGroups");
144    //        HtmlPage deleteCodePage = element.click();
145    //              assertFalse("Page contains Error", deleteCodePage.asText().contains("error"));
146    
147            // submit the changes
148            element = newCodeAddedPage.getElementByName("methodToCall.route");
149            HtmlPage finalPage = element.click();
150    
151            LOG.debug("Final Space is >>> " + finalPage.asText());
152    //        HtmlUnitUtil.createTempFile(finalPage);
153            Assert.assertTrue("Maintenance page is submitted successfully", finalPage.asText().contains("Document was successfully submitted."));
154    
155        }
156    
157    
158        @Test
159        //tests EarnCodeGroupValidation
160        public void testSubmitEarnGroupMaint() throws Exception {
161            String baseUrl = HtmlUnitUtil.getBaseURL() + "/kr/maintenance.do?businessObjectClassName=org.kuali.hr.time.earncodegroup.EarnCodeGroup&methodToCall=start";
162            HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), baseUrl);
163            Assert.assertNotNull(page);
164    
165            HtmlTextInput text = (HtmlTextInput) page.getHtmlElementById("document.documentHeader.documentDescription");
166            text.setValueAttribute("test");
167            text = (HtmlTextInput) page.getHtmlElementById(TkTestConstants.DOC_NEW_ELEMENT_ID_PREFIX + "descr");
168            text.setValueAttribute("Test Earn Group");
169            text = (HtmlTextInput) page.getHtmlElementById(TkTestConstants.DOC_NEW_ELEMENT_ID_PREFIX + "earnCodeGroup");
170            text.setValueAttribute("Test");
171            // set an old effective date so that the earn code that's added later is not effective by that date
172            text = (HtmlTextInput) page.getHtmlElementById(TkTestConstants.DOC_NEW_ELEMENT_ID_PREFIX + "effectiveDate");
173            text.setValueAttribute("12/01/2008");
174    
175            HtmlCheckBoxInput checkbox = (HtmlCheckBoxInput) page.getHtmlElementById(TkTestConstants.DOC_NEW_ELEMENT_ID_PREFIX + "showSummary");
176            checkbox.setChecked(true);
177            checkbox = (HtmlCheckBoxInput) page.getHtmlElementById(TkTestConstants.DOC_NEW_ELEMENT_ID_PREFIX + "active");
178            checkbox.setChecked(true);
179    
180            // add an Earn code that is being used by another active earn group, submit, should generate error message
181            text = (HtmlTextInput) page.getHtmlElementById(TkTestConstants.DOC_NEW_ELEMENT_ID_PREFIX + "add.earnCodeGroups.earnCode");
182            text.setValueAttribute(EARN_CODE);
183            HtmlElement element = HtmlUnitUtil.getInputContainingText(page, "methodToCall.addLine.earnCodeGroups");
184            HtmlPage page1 = element.click();
185            //error for earn code not being effective by the effectiveDate of the earn group
186            LOG.debug("Page1 click >>>>"+page1.asText());
187            
188            Assert.assertTrue("Maintenance Page contains error messages", page1.asText().contains("The specified Earncode '" + EARN_CODE + "' does not exist."));
189    
190            // set the effective date to one that works for the earn code
191            text = (HtmlTextInput) page1.getHtmlElementById(TkTestConstants.DOC_NEW_ELEMENT_ID_PREFIX + "effectiveDate");
192            text.setValueAttribute("5/01/2011");
193            element = HtmlUnitUtil.getInputContainingText(page1, "methodToCall.addLine.earnCodeGroups");
194            HtmlPage page2 = element.click();
195            Assert.assertFalse("Page contains Error", page2.asText().contains("error"));
196    
197            // add the same earn code again to get the duplicate error
198            text = (HtmlTextInput) page2.getHtmlElementById(TkTestConstants.DOC_NEW_ELEMENT_ID_PREFIX + "add.earnCodeGroups.earnCode");
199            text.setValueAttribute(EARN_CODE);
200            element = HtmlUnitUtil.getInputContainingText(page2, "methodToCall.addLine.earnCodeGroups");
201            page1 = element.click();
202            Assert.assertTrue("Maintenance Page contains error messages", page1.asText().contains(EARN_CODE + " is already a part of this earngroup."));
203    
204            element = page1.getElementByName("methodToCall.route");
205            HtmlPage finalPage = element.click();
206            // error for earn code that is being used by another earn group
207            Assert.assertTrue("Maintenance Page contains error messages", finalPage.asText().contains(EARN_CODE + " is used by another earn group - 'test'."));
208    
209            //delete this earn code
210            element = HtmlUnitUtil.getInputContainingText(finalPage, "methodToCall.deleteLine.earnCodeGroups");
211            HtmlPage page3 = element.click();
212            Assert.assertFalse("Page contains Error", page3.asText().contains("error"));
213    
214            //add an earn code that is not being used, submit, should get success message
215            text = (HtmlTextInput) page3.getHtmlElementById(TkTestConstants.DOC_NEW_ELEMENT_ID_PREFIX + "add.earnCodeGroups.earnCode");
216            text.setValueAttribute("XZZ");
217            element = HtmlUnitUtil.getInputContainingText(page3, "methodToCall.addLine.earnCodeGroups");
218            page1 = element.click();
219            LOG.error("page1 click >>> adding SDR to earn code group >>>" + page1.asXml());
220            Assert.assertFalse("Page contains Error", page1.asText().contains("error"));
221            element = page1.getElementByName("methodToCall.route");
222            finalPage = element.click();
223    //        HtmlUnitUtil.createTempFile(finalPage);
224            Assert.assertTrue("Maintenance page is submitted successfully", finalPage.asText().contains("Document was successfully submitted."));
225            Assert.assertTrue("Maintenance page is submitted successfully", finalPage.asText().contains("Status:     FINAL"));
226        }
227    
228        @Test
229        public void testSubmitEarnGroupWithNewerVersionMaint() throws Exception {
230            String baseUrl = HtmlUnitUtil.getBaseURL() + "/kr/maintenance.do?businessObjectClassName=org.kuali.hr.time.earncodegroup.EarnCodeGroup&methodToCall=start";
231            HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), baseUrl);
232            Assert.assertNotNull(page);
233    
234            //save a Earn code
235            populateEarnGroup(page, "01/01/2011");
236            HtmlInput text = (HtmlTextInput) page.getHtmlElementById("document.newMaintainableObject.add.earnCodeGroups.earnCode");
237            text.setValueAttribute("RGH");
238            HtmlElement element = HtmlUnitUtil.getInputContainingText(page, "methodToCall.addLine.earnCodeGroups");
239            HtmlPage newCodeAddedPage = element.click();
240    //        HtmlUnitUtil.createTempFile(newCodeAddedPage);
241            Assert.assertFalse("Page contains Error", newCodeAddedPage.asText().contains("error"));
242            element = newCodeAddedPage.getElementByName("methodToCall.route");
243            HtmlPage finalPage = element.click();
244    
245            Assert.assertTrue("Maintenance page is submitted successfully", finalPage.asText().contains("Document was successfully submitted."));
246    
247            //try to save the same Earn code with older effective date
248            page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), baseUrl);
249            populateEarnGroup(page, "01/01/2010");
250            text = (HtmlTextInput) page.getHtmlElementById("document.newMaintainableObject.add.earnCodeGroups.earnCode");
251            text.setValueAttribute("RGH");
252            element = HtmlUnitUtil.getInputContainingText(page, "methodToCall.addLine.earnCodeGroups");
253            newCodeAddedPage = element.click();
254            Assert.assertFalse("Page contains Error", newCodeAddedPage.asText().contains("error"));
255            element = newCodeAddedPage.getElementByName("methodToCall.route");
256            finalPage = element.click();
257            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"));
258            HtmlElement yesButton = finalPage.getElementByName("methodToCall.processAnswer.button0");
259            finalPage = yesButton.click();
260            Assert.assertTrue("Maintenance Page contains error messages", finalPage.asText().contains("There is a newer version of this Earn Group."));
261    
262        }
263    
264        private void populateEarnGroup(HtmlPage page, String effDateString) {
265            setFieldValue(page, "document.documentHeader.documentDescription", "test");
266            setFieldValue(page, TkTestConstants.DOC_NEW_ELEMENT_ID_PREFIX + "descr", "Test Earn Group");
267            setFieldValue(page, TkTestConstants.DOC_NEW_ELEMENT_ID_PREFIX + "earnCodeGroup", "MM");
268            setFieldValue(page, TkTestConstants.DOC_NEW_ELEMENT_ID_PREFIX + "effectiveDate", effDateString);
269            setFieldValue(page, TkTestConstants.DOC_NEW_ELEMENT_ID_PREFIX + "showSummary", "on");
270            setFieldValue(page, TkTestConstants.DOC_NEW_ELEMENT_ID_PREFIX + "active", "on");
271        }
272    
273    
274    }