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.junit.Assert;
24  import org.junit.Test;
25  import org.kuali.hr.test.KPMETestCase;
26  import org.kuali.hr.time.earncode.EarnCode;
27  import org.kuali.hr.time.earngroup.EarnGroup;
28  import org.kuali.hr.time.earngroup.EarnGroupDefinition;
29  import org.kuali.hr.time.test.HtmlUnitUtil;
30  import org.kuali.hr.time.test.TkTestConstants;
31  import org.kuali.rice.krad.service.KRADServiceLocator;
32  
33  import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
34  import com.gargoylesoftware.htmlunit.html.HtmlElement;
35  import com.gargoylesoftware.htmlunit.html.HtmlInput;
36  import com.gargoylesoftware.htmlunit.html.HtmlPage;
37  import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
38  
39  public class EarnGroupMaintenanceTest extends KPMETestCase {
40      private static final java.sql.Date TEST_DATE = new java.sql.Date(Calendar.getInstance().getTimeInMillis());
41      private static final String EARN_CODE = "RGN";
42      private static String hrEarnGroupId;
43      private static String hrEarnCodeId;
44      private static String hrEarnGroupIdRGG;
45  
46      @Override
47      public void setUp() throws Exception {
48          super.setUp();
49          List<EarnGroupDefinition> earnGroups = new ArrayList<EarnGroupDefinition>();
50          EarnGroupDefinition definition = new EarnGroupDefinition();
51          definition.setEarnCode(EARN_CODE);
52          earnGroups.add(definition);
53  
54          EarnGroup earnGroup = new EarnGroup();
55          earnGroup.setEarnGroup("testGroup");
56          earnGroup.setDescr("test");
57          earnGroup.setEffectiveDate(TEST_DATE);
58          earnGroup.setShowSummary(true);
59          earnGroup.setActive(true);
60          earnGroup.setEarnGroups(earnGroups);
61          KRADServiceLocator.getBusinessObjectService().save(earnGroup);
62          hrEarnGroupId = earnGroup.getHrEarnGroupId();
63  
64          // Set up earn code RGG in tk-earn_code_t
65          EarnCode earnCode = new EarnCode();
66          earnCode.setActive(true);
67          earnCode.setEarnCode("RGG");
68          earnCode.setEffectiveDate(TEST_DATE);
69          earnCode.setRecordMethod("T");
70          earnCode.setDescription("RGG");
71          earnCode.setOvtEarnCode(false);
72          earnCode.setInflateMinHours(BigDecimal.ZERO);
73          earnCode.setInflateFactor(BigDecimal.ZERO);
74          KRADServiceLocator.getBusinessObjectService().save(earnCode);
75          hrEarnCodeId = earnCode.getHrEarnCodeId();
76  
77          // Set up earn group RGG in tk-earn_group_t
78          EarnGroup earnGroupRGG = new EarnGroup();
79          earnGroupRGG.setEarnGroup("RGG");
80          earnGroupRGG.setDescr("test RGG");
81          earnGroupRGG.setEffectiveDate(TEST_DATE);
82          earnGroupRGG.setShowSummary(true);
83          earnGroupRGG.setActive(true);
84          KRADServiceLocator.getBusinessObjectService().save(earnGroupRGG);
85          hrEarnGroupIdRGG = earnGroupRGG.getHrEarnGroupId();
86      }
87  
88      @Override
89      public void tearDown() throws Exception {
90          EarnGroup earnGroupObj = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(EarnGroup.class, hrEarnGroupId);
91          KRADServiceLocator.getBusinessObjectService().delete(earnGroupObj);
92  
93          EarnGroup earnGroupObjRGG = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(EarnGroup.class, hrEarnGroupIdRGG);
94          KRADServiceLocator.getBusinessObjectService().delete(earnGroupObjRGG);
95  
96          EarnCode earnCodeObj = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(EarnCode.class, hrEarnCodeId);
97          KRADServiceLocator.getBusinessObjectService().delete(earnCodeObj);
98  
99          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 }