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.department.earncode;
017    
018    import java.sql.Date;
019    import java.util.Collection;
020    import java.util.Collections;
021    import java.util.List;
022    
023    import org.apache.commons.lang.StringUtils;
024    import org.joda.time.DateTime;
025    import org.junit.Assert;
026    import org.junit.Test;
027    import org.kuali.hr.lm.earncodesec.EarnCodeSecurity;
028    import org.kuali.hr.test.KPMETestCase;
029    import org.kuali.hr.time.test.HtmlUnitUtil;
030    import org.kuali.hr.time.test.TkTestConstants;
031    import org.kuali.hr.time.util.TKUtils;
032    import org.kuali.hr.time.util.TkConstants;
033    import org.kuali.rice.krad.service.KRADServiceLocator;
034    
035    import com.gargoylesoftware.htmlunit.html.HtmlInput;
036    import com.gargoylesoftware.htmlunit.html.HtmlPage;
037    
038    public class EarnCodeSecurityMaintenanceTest extends KPMETestCase{
039            private static final java.sql.Date TEST_DATE_OLD = new Date((new DateTime(2009, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
040            private static final java.sql.Date TEST_DATE_NEW = new Date((new DateTime(2010, 1, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
041            private static final String EARN_CODE = "RGN";
042            private static final String DEPT = "TEST-DEPT";
043            private static final String SAL_GROUP = "SD1";
044            private static String hrDeptEarnCodeId;
045            private static String dupTkDeptEarnCodeId;
046    
047            @Test
048            public void testEarnCodeSecurityMaint() throws Exception {
049                    HtmlPage deptEarnCodeLookup = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), TkTestConstants.Urls.EARN_CODE_SECURITY_MAINT_URL);
050                    deptEarnCodeLookup = HtmlUnitUtil.clickInputContainingText(deptEarnCodeLookup, "search");
051                    Assert.assertTrue("Page contains test EarnCodeSecurity", deptEarnCodeLookup.asText().contains(DEPT));
052                    HtmlPage maintPage = HtmlUnitUtil.clickAnchorContainingText(deptEarnCodeLookup, "edit", "hrEarnCodeSecurityId=" + hrDeptEarnCodeId);
053                    Assert.assertTrue("Maintenance Page contains test EarnCodeSecurity",maintPage.asText().contains(DEPT));
054            }
055            
056            @Test
057            public void testEarnCodeSecurityMaintForErrorMessages() throws Exception {
058    
059            String testDept = "testDept";
060            String testSalGroup = "testSalGroup";
061            String testEarnCode = "testEarnCode";
062    
063    
064                    HtmlPage deptEarnCodeLookup = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), TkTestConstants.Urls.EARN_CODE_SECURITY_MAINT_URL);
065                    deptEarnCodeLookup = HtmlUnitUtil.clickInputContainingText(deptEarnCodeLookup, "search");
066                    Assert.assertTrue("Page contains test EarnCodeSecurity", deptEarnCodeLookup.asText().contains(DEPT));
067                    HtmlPage maintPage = HtmlUnitUtil.clickAnchorContainingText(deptEarnCodeLookup, "edit", "hrEarnCodeSecurityId");
068                    
069                    HtmlInput inputForDescription = HtmlUnitUtil.getInputContainingText(
070                                    maintPage, "* Document Description");
071                    inputForDescription.setValueAttribute("Test_Description");
072    
073            HtmlInput inputForDept = HtmlUnitUtil.getInputContainingText(maintPage, "document.newMaintainableObject.dept");
074            inputForDept.setValueAttribute(testDept);
075    
076            HtmlInput inputForSalGroup = HtmlUnitUtil.getInputContainingText(maintPage, "document.newMaintainableObject.hrSalGroup");
077            inputForSalGroup.setValueAttribute(testSalGroup);
078    
079            HtmlInput inputForEarnCode = HtmlUnitUtil.getInputContainingText(maintPage, "document.newMaintainableObject.earnCode");
080            inputForEarnCode.setValueAttribute(testEarnCode);
081    
082            HtmlPage resultantPageAfterEdit = HtmlUnitUtil
083                                    .clickInputContainingText(maintPage, "submit");
084    
085    
086            HtmlUnitUtil.createTempFile(resultantPageAfterEdit);
087            Assert.assertTrue("Maintenance Page contains test deptErrormessage",
088                                    resultantPageAfterEdit.asText().contains(
089                                                    "The specified department '"
090                                                                    + testDept
091                                                                    + "' does not exist."));
092                    
093                    Assert.assertTrue("Maintenance Page contains test SalGroupErrormessage",
094                                    resultantPageAfterEdit.asText().contains(
095                                                    "The specified Salgroup '"
096                                                                    + testSalGroup
097                                                                    + "' does not exist."));        
098                    
099                    Assert.assertTrue("Maintenance Page contains test Earncode",
100                                    resultantPageAfterEdit.asText().contains(
101                                                    "The specified Earncode '"
102                                                                    + testEarnCode
103                                                                    + "' does not exist."));
104                                    
105                    
106            }
107            
108            @Test
109            public void testEditingEarnCodeSecurityMaint() throws Exception {
110                    HtmlPage deptEarnCodeLookup = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), TkTestConstants.Urls.EARN_CODE_SECURITY_MAINT_URL);
111                    deptEarnCodeLookup = HtmlUnitUtil.clickInputContainingText(deptEarnCodeLookup, "search");
112                    Assert.assertTrue("Page contains TEST-DEPT", deptEarnCodeLookup.asText().contains(DEPT));
113                    HtmlPage maintPage = HtmlUnitUtil.clickAnchorContainingText(deptEarnCodeLookup, "edit", "hrEarnCodeSecurityId=" + hrDeptEarnCodeId);
114                    Assert.assertFalse("Maintenance Page contains Warnings",maintPage.asText().contains("Warnings for this Section:"));
115                    Assert.assertFalse("Maintenance Page contains Warning message",maintPage.asText().contains("There is a newer version of this Department Earn Code."));
116                    
117                    this.createDuplicateEarnCodeSecurity();
118                    deptEarnCodeLookup = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), TkTestConstants.Urls.EARN_CODE_SECURITY_MAINT_URL);
119                    this.setFieldValue(deptEarnCodeLookup, "earnCode", EARN_CODE);
120                    this.setFieldValue(deptEarnCodeLookup, "dept", DEPT);
121                    this.setFieldValue(deptEarnCodeLookup, "effectiveDate", "08/01/2010");
122                    this.setFieldValue(deptEarnCodeLookup, "activeYes", "on");
123                    this.setFieldValue(deptEarnCodeLookup, "historyYes", "on");
124                    
125                    deptEarnCodeLookup = HtmlUnitUtil.clickInputContainingText(deptEarnCodeLookup, "search");
126                    maintPage = HtmlUnitUtil.clickAnchorContainingText(deptEarnCodeLookup, "edit", "hrEarnCodeSecurityId="+hrDeptEarnCodeId);
127                    Assert.assertTrue("Maintenance Page contains Warnings",maintPage.asText().contains("Warnings for this Section:"));
128                    Assert.assertTrue("Maintenance Page contains Warning message",maintPage.asText().contains("There is a newer version of this Department Earn Code."));
129            }
130            
131            public void createNewEarnCodeSecurity() {
132            //clear old earncode securitys:
133            Collection<EarnCodeSecurity> earnCodeSecurities = KRADServiceLocator.getBusinessObjectService().findMatching(EarnCodeSecurity.class, Collections.singletonMap("earnCode", EARN_CODE));
134            KRADServiceLocator.getBusinessObjectService().deleteMatching(EarnCodeSecurity.class, Collections.singletonMap("earnCode", EARN_CODE));
135                    EarnCodeSecurity deptEarnCode = new EarnCodeSecurity();
136                    deptEarnCode.setActive(true);
137                    deptEarnCode.setEarnCode(EARN_CODE);
138                    deptEarnCode.setDept(DEPT);
139                    deptEarnCode.setHrSalGroup(SAL_GROUP);
140                    deptEarnCode.setEmployee(false);
141                    deptEarnCode.setEffectiveDate(TEST_DATE_OLD);
142                    deptEarnCode.setLocation("test");
143                    deptEarnCode.setEarnCodeType("T");
144                    deptEarnCode = KRADServiceLocator.getBusinessObjectService().save(deptEarnCode);
145                    hrDeptEarnCodeId = deptEarnCode.getHrEarnCodeSecurityId();
146            }
147            
148            public void createDuplicateEarnCodeSecurity() {
149                    EarnCodeSecurity deptEarnCode = new EarnCodeSecurity();
150                    deptEarnCode.setActive(true);
151                    deptEarnCode.setEarnCode(EARN_CODE);
152                    deptEarnCode.setDept(DEPT);
153                    deptEarnCode.setHrSalGroup(SAL_GROUP);
154                    deptEarnCode.setEmployee(false);
155                    deptEarnCode.setEffectiveDate(TEST_DATE_NEW);
156                    deptEarnCode.setLocation("test");
157                    deptEarnCode.setEarnCodeType("T");
158            deptEarnCode = KRADServiceLocator.getBusinessObjectService().save(deptEarnCode);
159                    dupTkDeptEarnCodeId = deptEarnCode.getHrEarnCodeSecurityId();
160            }
161            
162            @Override
163            public void tearDown() throws Exception {
164                    EarnCodeSecurity deptEarnCodeObj = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(EarnCodeSecurity.class, hrDeptEarnCodeId);
165                    KRADServiceLocator.getBusinessObjectService().delete(deptEarnCodeObj);
166            if (StringUtils.isNotBlank(dupTkDeptEarnCodeId)) {
167                        deptEarnCodeObj = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(EarnCodeSecurity.class, dupTkDeptEarnCodeId);
168                        KRADServiceLocator.getBusinessObjectService().delete(deptEarnCodeObj);
169            }
170                    super.tearDown();
171            }
172            
173    
174            @Override
175            public void setUp() throws Exception {
176                    super.setUp();
177                    this.createNewEarnCodeSecurity();               
178            }
179    }