View Javadoc

1   /**
2    * Copyright 2004-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.core.earncode.group;
17  
18  import java.math.BigDecimal;
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.apache.log4j.Logger;
25  import org.joda.time.LocalDate;
26  import org.junit.Assert;
27  import org.junit.Test;
28  import org.kuali.hr.KPMEWebTestCase;
29  import org.kuali.hr.util.HtmlUnitUtil;
30  import org.kuali.kpme.core.FunctionalTest;
31  import org.kuali.kpme.core.earncode.EarnCode;
32  import org.kuali.kpme.core.earncode.group.EarnCodeGroup;
33  import org.kuali.kpme.core.earncode.group.EarnCodeGroupDefinition;
34  import org.kuali.kpme.core.service.HrServiceLocator;
35  import org.kuali.kpme.core.util.HrTestConstants;
36  import org.kuali.rice.krad.service.KRADServiceLocator;
37  
38  import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
39  import com.gargoylesoftware.htmlunit.html.HtmlElement;
40  import com.gargoylesoftware.htmlunit.html.HtmlInput;
41  import com.gargoylesoftware.htmlunit.html.HtmlPage;
42  import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
43  
44  @FunctionalTest
45  public class EarnCodeGroupMaintenanceTest extends KPMEWebTestCase {
46      private static final Logger LOG = Logger.getLogger(EarnCodeGroupMaintenanceTest.class);
47      private static final LocalDate TEST_DATE = LocalDate.now();
48      private static final String EARN_CODE = "RGN";
49      private static String hrEarnGroupId;
50      private static String hrEarnCodeId;
51      private static String hrEarnGroupIdRGG;
52  
53      @Override
54      public void setUp() throws Exception {
55          super.setUp();
56          List<EarnCodeGroupDefinition> earnGroups = new ArrayList<EarnCodeGroupDefinition>();
57          EarnCodeGroupDefinition definition = new EarnCodeGroupDefinition();
58          definition.setEarnCode(EARN_CODE);
59          earnGroups.add(definition);
60  
61          EarnCodeGroup earnGroup = new EarnCodeGroup();
62          earnGroup.setEarnCodeGroup("testGroup");
63          earnGroup.setDescr("test");
64          earnGroup.setEffectiveLocalDate(TEST_DATE);
65          earnGroup.setShowSummary(true);
66          earnGroup.setActive(true);
67          earnGroup.setEarnCodeGroups(earnGroups);
68          KRADServiceLocator.getBusinessObjectService().save(earnGroup);
69          hrEarnGroupId = earnGroup.getHrEarnCodeGroupId();
70  
71          // Set up earn code RGG in tk-earn_code_t
72          EarnCode earnCode = new EarnCode();
73          earnCode.setActive(true);
74          earnCode.setEarnCode("RGG");
75          earnCode.setEffectiveLocalDate(TEST_DATE);
76          earnCode.setRecordMethod("T");
77          earnCode.setFractionalTimeAllowed("99");
78          earnCode.setRoundingOption("T");
79          earnCode.setEligibleForAccrual("Y");
80          earnCode.setAffectPay("Y");
81          earnCode.setAllowScheduledLeave("Y");
82          earnCode.setFmla("Y");
83          earnCode.setWorkmansComp("Y");
84          earnCode.setAllowNegativeAccrualBalance("Y");
85          earnCode.setDescription("RGG");
86          earnCode.setOvtEarnCode(false);
87          earnCode.setInflateMinHours(BigDecimal.ZERO);
88          earnCode.setInflateFactor(BigDecimal.ZERO);
89          earnCode = KRADServiceLocator.getBusinessObjectService().save(earnCode);
90          hrEarnCodeId = earnCode.getHrEarnCodeId();
91  
92          // Set up earn group RGG in tk-earn_group_t
93          EarnCodeGroup earnGroupRGG = new EarnCodeGroup();
94          earnGroupRGG.setEarnCodeGroup("RGG");
95          earnGroupRGG.setDescr("test RGG");
96          earnGroupRGG.setEffectiveLocalDate(TEST_DATE);
97          earnGroupRGG.setShowSummary(true);
98          earnGroupRGG.setActive(true);
99          earnGroupRGG = KRADServiceLocator.getBusinessObjectService().save(earnGroupRGG);
100         hrEarnGroupIdRGG = earnGroupRGG.getHrEarnCodeGroupId();
101     }
102 
103     @Override
104     public void tearDown() throws Exception {
105         EarnCodeGroup earnGroupObj = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(EarnCodeGroup.class, hrEarnGroupId);
106         KRADServiceLocator.getBusinessObjectService().delete(earnGroupObj);
107 
108         EarnCodeGroup earnGroupObjRGG = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(EarnCodeGroup.class, hrEarnGroupIdRGG);
109         KRADServiceLocator.getBusinessObjectService().delete(earnGroupObjRGG);
110 
111         if (StringUtils.isNotBlank(hrEarnCodeId)) {
112             EarnCode earnCodeObj = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(EarnCode.class, Collections.singletonMap("hrEarnCodeId", hrEarnCodeId));
113             KRADServiceLocator.getBusinessObjectService().delete(earnCodeObj);
114         }
115         super.tearDown();
116     }
117 
118 	@Test
119 	public void testEarnGroupMaintenancePage() throws Exception{				
120 		HtmlPage earnCodeLookUp = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), HrTestConstants.Urls.EARN_CODE_GROUP_MAINT_URL);
121 		earnCodeLookUp = HtmlUnitUtil.clickInputContainingText(earnCodeLookUp, "search");
122 		HtmlUnitUtil.createTempFile(earnCodeLookUp);
123 		Assert.assertTrue("Page contains REG entry", earnCodeLookUp.asText().contains("REG"));	
124 		
125 		EarnCodeGroup earnGroup = HrServiceLocator.getEarnCodeGroupService().getEarnCodeGroup("REG", LocalDate.now());
126 		String earnGroupId = earnGroup.getHrEarnCodeGroupId().toString();
127 		HtmlPage maintPage = HtmlUnitUtil.clickAnchorContainingText(earnCodeLookUp, "edit", earnGroupId);		
128 		HtmlUnitUtil.createTempFile(maintPage);
129 		Assert.assertTrue("Maintenance Page contains REG entry",maintPage.asText().contains("REG"));		
130 	}	
131     
132     @Test
133     public void testEditExistingEarnGroup() throws Exception {
134         HtmlPage earnGroupLookUp = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), HrTestConstants.Urls.EARN_CODE_GROUP_MAINT_URL);
135         earnGroupLookUp = HtmlUnitUtil.clickInputContainingText(earnGroupLookUp, "search");
136         Assert.assertTrue("Page contains test Earn Group", earnGroupLookUp.asText().contains("test"));
137         HtmlPage maintPage = HtmlUnitUtil.clickAnchorContainingText(earnGroupLookUp, "edit", hrEarnGroupId.toString());
138         Assert.assertTrue("Maintenance Page contains test ClockLog", maintPage.asText().contains("test"));
139         HtmlTextInput text = (HtmlTextInput) maintPage.getHtmlElementById("document.documentHeader.documentDescription");
140         text.setValueAttribute("test1");
141 
142         // pull out earn group RGG to edit.
143         earnGroupLookUp = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), HrTestConstants.Urls.EARN_CODE_GROUP_MAINT_URL);
144         earnGroupLookUp = HtmlUnitUtil.clickInputContainingText(earnGroupLookUp, "search");
145         Assert.assertTrue("Page contains test Earn Group", earnGroupLookUp.asText().contains("test RGG"));
146         HtmlPage testEditRGGPage = HtmlUnitUtil.clickAnchorContainingText(earnGroupLookUp, "edit", hrEarnGroupIdRGG.toString());
147         Assert.assertTrue("Maintenance Page contains test ClockLog", testEditRGGPage.asText().contains("test RGG"));
148         text = (HtmlTextInput) testEditRGGPage.getHtmlElementById("document.documentHeader.documentDescription");
149         text.setValueAttribute("testEditRGG");
150 
151         // Add a new Earn Code Group Definition
152         text = (HtmlTextInput) testEditRGGPage.getHtmlElementById("document.newMaintainableObject.add.earnCodeGroups.earnCode");
153         text.setValueAttribute("RGG");
154         HtmlElement element = HtmlUnitUtil.getInputContainingText(testEditRGGPage, "methodToCall.addLine.earnCodeGroups");
155         HtmlPage newCodeAddedPage = element.click();
156 //        HtmlUnitUtil.createTempFile(newCodeAddedPage);
157         Assert.assertFalse("Page contains Error", newCodeAddedPage.asText().contains("error"));
158 
159         // Delete this Earn Code Group Definition
160 //        element = HtmlUnitUtil.getInputContainingText(newCodeAddedPage,"methodToCall.deleteLine.earnGroups");
161 //        HtmlPage deleteCodePage = element.click();
162 //		assertFalse("Page contains Error", deleteCodePage.asText().contains("error"));
163 
164         // submit the changes
165         element = newCodeAddedPage.getElementByName("methodToCall.route");
166         HtmlPage finalPage = element.click();
167 
168         LOG.debug("Final Space is >>> " + finalPage.asText());
169 //        HtmlUnitUtil.createTempFile(finalPage);
170         Assert.assertTrue("Maintenance page is submitted successfully", finalPage.asText().contains("Document was successfully submitted."));
171 
172     }
173 
174 
175     @Test
176     //tests EarnCodeGroupValidation
177     public void testSubmitEarnGroupMaint() throws Exception {
178         String baseUrl = getBaseURL() + "/kr/maintenance.do?businessObjectClassName=org.kuali.kpme.core.earncode.group.EarnCodeGroup&methodToCall=start";
179         HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), baseUrl);
180         Assert.assertNotNull(page);
181 
182         HtmlTextInput text = (HtmlTextInput) page.getHtmlElementById("document.documentHeader.documentDescription");
183         text.setValueAttribute("test");
184         text = (HtmlTextInput) page.getHtmlElementById(HrTestConstants.DOC_NEW_ELEMENT_ID_PREFIX + "descr");
185         text.setValueAttribute("Test Earn Group");
186         text = (HtmlTextInput) page.getHtmlElementById(HrTestConstants.DOC_NEW_ELEMENT_ID_PREFIX + "earnCodeGroup");
187         text.setValueAttribute("Test");
188         // set an old effective date so that the earn code that's added later is not effective by that date
189         text = (HtmlTextInput) page.getHtmlElementById(HrTestConstants.DOC_NEW_ELEMENT_ID_PREFIX + "effectiveDate");
190         text.setValueAttribute("12/01/2008");
191 
192         HtmlCheckBoxInput checkbox = (HtmlCheckBoxInput) page.getHtmlElementById(HrTestConstants.DOC_NEW_ELEMENT_ID_PREFIX + "showSummary");
193         checkbox.setChecked(true);
194         checkbox = (HtmlCheckBoxInput) page.getHtmlElementById(HrTestConstants.DOC_NEW_ELEMENT_ID_PREFIX + "active");
195         checkbox.setChecked(true);
196 
197         // add an Earn code that is being used by another active earn group, submit, should generate error message
198         text = (HtmlTextInput) page.getHtmlElementById(HrTestConstants.DOC_NEW_ELEMENT_ID_PREFIX + "add.earnCodeGroups.earnCode");
199         text.setValueAttribute(EARN_CODE);
200         HtmlElement element = HtmlUnitUtil.getInputContainingText(page, "methodToCall.addLine.earnCodeGroups");
201         HtmlPage page1 = element.click();
202         //error for earn code not being effective by the effectiveDate of the earn group
203         LOG.debug("Page1 click >>>>"+page1.asText());
204         
205         Assert.assertTrue("Maintenance Page contains error messages", page1.asText().contains("The specified Earncode '" + EARN_CODE + "' does not exist."));
206 
207         // set the effective date to one that works for the earn code
208         text = (HtmlTextInput) page1.getHtmlElementById(HrTestConstants.DOC_NEW_ELEMENT_ID_PREFIX + "effectiveDate");
209         text.setValueAttribute("5/01/2011");
210         element = HtmlUnitUtil.getInputContainingText(page1, "methodToCall.addLine.earnCodeGroups");
211         HtmlPage page2 = element.click();
212         Assert.assertFalse("Page contains Error", page2.asText().contains("error"));
213 
214         // add the same earn code again to get the duplicate error
215         text = (HtmlTextInput) page2.getHtmlElementById(HrTestConstants.DOC_NEW_ELEMENT_ID_PREFIX + "add.earnCodeGroups.earnCode");
216         text.setValueAttribute(EARN_CODE);
217         element = HtmlUnitUtil.getInputContainingText(page2, "methodToCall.addLine.earnCodeGroups");
218         page1 = element.click();
219         Assert.assertTrue("Maintenance Page contains error messages", page1.asText().contains(EARN_CODE + " is already a part of this earngroup."));
220 
221         element = page1.getElementByName("methodToCall.route");
222         HtmlPage finalPage = element.click();
223         // error for earn code that is being used by another earn group
224         Assert.assertTrue("Maintenance Page contains error messages", finalPage.asText().contains(EARN_CODE + " is used by another earn group - 'test'."));
225 
226         //delete this earn code
227         element = HtmlUnitUtil.getInputContainingText(finalPage, "methodToCall.deleteLine.earnCodeGroups");
228         HtmlPage page3 = element.click();
229         Assert.assertFalse("Page contains Error", page3.asText().contains("error"));
230 
231         //add an earn code that is not being used, submit, should get success message
232         text = (HtmlTextInput) page3.getHtmlElementById(HrTestConstants.DOC_NEW_ELEMENT_ID_PREFIX + "add.earnCodeGroups.earnCode");
233         text.setValueAttribute("XZZ");
234         element = HtmlUnitUtil.getInputContainingText(page3, "methodToCall.addLine.earnCodeGroups");
235         page1 = element.click();
236         LOG.error("page1 click >>> adding SDR to earn code group >>>" + page1.asXml());
237         Assert.assertFalse("Page contains Error", page1.asText().contains("error"));
238         element = page1.getElementByName("methodToCall.route");
239         finalPage = element.click();
240 //        HtmlUnitUtil.createTempFile(finalPage);
241         Assert.assertTrue("Maintenance page is submitted successfully", finalPage.asText().contains("Document was successfully submitted."));
242         Assert.assertTrue("Maintenance page is submitted successfully", finalPage.asText().contains("FINAL"));
243     }
244 
245     @Test
246     public void testSubmitEarnGroupWithNewerVersionMaint() throws Exception {
247         String baseUrl = getBaseURL() + "/kr/maintenance.do?businessObjectClassName=org.kuali.kpme.core.earncode.group.EarnCodeGroup&methodToCall=start";
248         HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), baseUrl);
249         Assert.assertNotNull(page);
250 
251         //save a Earn code
252         populateEarnGroup(page, "01/01/2011");
253         HtmlInput text = (HtmlTextInput) page.getHtmlElementById("document.newMaintainableObject.add.earnCodeGroups.earnCode");
254         text.setValueAttribute("RGH");
255         HtmlElement element = HtmlUnitUtil.getInputContainingText(page, "methodToCall.addLine.earnCodeGroups");
256         HtmlPage newCodeAddedPage = element.click();
257 //        HtmlUnitUtil.createTempFile(newCodeAddedPage);
258         Assert.assertFalse("Page contains Error", newCodeAddedPage.asText().contains("error"));
259         element = newCodeAddedPage.getElementByName("methodToCall.route");
260         HtmlPage finalPage = element.click();
261 
262         Assert.assertTrue("Maintenance page is submitted successfully", finalPage.asText().contains("Document was successfully submitted."));
263 
264         //try to save the same Earn code with older effective date
265         page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), baseUrl);
266         populateEarnGroup(page, "01/01/2010");
267         text = (HtmlTextInput) page.getHtmlElementById("document.newMaintainableObject.add.earnCodeGroups.earnCode");
268         text.setValueAttribute("RGH");
269         element = HtmlUnitUtil.getInputContainingText(page, "methodToCall.addLine.earnCodeGroups");
270         newCodeAddedPage = element.click();
271         Assert.assertFalse("Page contains Error", newCodeAddedPage.asText().contains("error"));
272         element = newCodeAddedPage.getElementByName("methodToCall.route");
273         finalPage = element.click();
274         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"));
275         HtmlElement yesButton = finalPage.getElementByName("methodToCall.processAnswer.button0");
276         finalPage = yesButton.click();
277         Assert.assertTrue("Maintenance Page contains error messages", finalPage.asText().contains("There is a newer version of this Earn Group."));
278 
279     }
280 
281     private void populateEarnGroup(HtmlPage page, String effDateString) {
282         HtmlUnitUtil.setFieldValue(page, "document.documentHeader.documentDescription", "test");
283         HtmlUnitUtil.setFieldValue(page, HrTestConstants.DOC_NEW_ELEMENT_ID_PREFIX + "descr", "Test Earn Group");
284         HtmlUnitUtil.setFieldValue(page, HrTestConstants.DOC_NEW_ELEMENT_ID_PREFIX + "earnCodeGroup", "MM");
285         HtmlUnitUtil.setFieldValue(page, HrTestConstants.DOC_NEW_ELEMENT_ID_PREFIX + "effectiveDate", effDateString);
286         HtmlUnitUtil.setFieldValue(page, HrTestConstants.DOC_NEW_ELEMENT_ID_PREFIX + "showSummary", "on");
287         HtmlUnitUtil.setFieldValue(page, HrTestConstants.DOC_NEW_ELEMENT_ID_PREFIX + "active", "on");
288     }
289 
290 
291 }