View Javadoc

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