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