001    /**
002     * Copyright 2004-2014 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.collection;
017    
018    import java.util.Collections;
019    import java.util.Random;
020    
021    import org.joda.time.LocalDate;
022    import org.junit.Assert;
023    import org.junit.Test;
024    import org.kuali.hr.KPMEWebTestCase;
025    import org.kuali.hr.util.HtmlUnitUtil;
026    import org.kuali.kpme.core.FunctionalTest;
027    import org.kuali.kpme.core.department.Department;
028    import org.kuali.kpme.core.service.HrServiceLocator;
029    import org.kuali.kpme.core.util.TKUtils;
030    import org.kuali.kpme.tklm.time.rules.timecollection.TimeCollectionRule;
031    import org.kuali.kpme.tklm.time.service.TkServiceLocator;
032    import org.kuali.kpme.tklm.utils.TkTestConstants;
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    @FunctionalTest
039    public class TimeCollectionRuleMaintTest extends KPMEWebTestCase {
040    
041            private static final String TEST_CODE = "X";
042            private static final LocalDate TEST_DATE = LocalDate.now();
043            private static final String TEST_CODE_DEPARTMENT_VALID = "_test";
044    
045            private static String timeCollectionRuleId;
046            private static String timeCollectionRuleIdWithInvalidWorkArea;
047    
048            private static String TEST_CODE_INVALID_DEPT = "INVALID_DEPT";
049            private static Long TEST_CODE_INVALID_WORKAREA = 2L;
050            private static String PAY_TYPE_ERROR = "The specified payType '%' does not exist.";
051    
052            /**
053             * Test to check whether it is showing error message on maintenance screen
054             * if we supply non exist deptId
055             *
056             * @throws Exception
057             */
058            @Test
059            public void testTimeCollectionRuleMaintForDeptErrorMessage() throws Exception {
060                    String baseUrl = TkTestConstants.Urls.TIME_COLLECTION_RULE_MAINT_NEW_URL;
061                    HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), baseUrl);
062                    HtmlInput inputForDescription = HtmlUnitUtil.getInputContainingText(page, "* Document Description");
063                    inputForDescription.setValueAttribute("Description");
064                    
065                    HtmlUnitUtil.setFieldValue(page, "document.newMaintainableObject.effectiveDate", "01/01/2010");
066                    HtmlUnitUtil.setFieldValue(page, "document.newMaintainableObject.dept", TEST_CODE_INVALID_DEPT);
067                    HtmlUnitUtil.setFieldValue(page, "document.newMaintainableObject.workArea", "30");
068                    HtmlUnitUtil.setFieldValue(page, "document.newMaintainableObject.payType", "BW");
069                    HtmlPage resultantPageAfterEdit = HtmlUnitUtil
070                                    .clickInputContainingText(page, "submit");
071                    HtmlUnitUtil.createTempFile(resultantPageAfterEdit);
072                    Assert.assertTrue("Maintenance Page contains test timeCollectionRule",
073                                    resultantPageAfterEdit.asText().contains(
074                                                    "The specified department '"
075                                                                    + TEST_CODE_INVALID_DEPT
076                                                                    + "' does not exist."));
077                    
078                    HtmlUnitUtil.setFieldValue(resultantPageAfterEdit, "document.newMaintainableObject.payType", "%");
079                    resultantPageAfterEdit = HtmlUnitUtil.clickInputContainingText(resultantPageAfterEdit, "submit");
080                    Assert.assertFalse("Maintenance Page contains error" + PAY_TYPE_ERROR, 
081                                    resultantPageAfterEdit.asText().contains(PAY_TYPE_ERROR));
082                    
083            }
084    
085    
086            @Test
087            public void testTimeCollectionRuleMaintForWorkAreaErrorMessage() throws Exception {
088                    HtmlPage timeCollectionRuleLookup = HtmlUnitUtil
089                                    .gotoPageAndLogin(getWebClient(), TkTestConstants.Urls.TIME_COLLECTION_RULE_MAINT_URL);
090                    timeCollectionRuleLookup = HtmlUnitUtil.clickInputContainingText(
091                                    timeCollectionRuleLookup, "search");
092                    Assert.assertTrue("Page contains test timeCollectionRule",
093                                    timeCollectionRuleLookup.asText().contains(TEST_CODE));
094                    HtmlPage maintPage = HtmlUnitUtil.clickAnchorContainingText(
095                                    timeCollectionRuleLookup, "edit",
096                                    timeCollectionRuleIdWithInvalidWorkArea.toString());
097                    HtmlUnitUtil.createTempFile(maintPage);
098                    HtmlInput inputForDescription = HtmlUnitUtil.getInputContainingText(
099                                    maintPage, "* Document Description");
100                    inputForDescription.setValueAttribute("Description");
101                    HtmlPage resultantPageAfterEdit = HtmlUnitUtil
102                                    .clickInputContainingText(maintPage, "submit");
103                    Assert.assertTrue("Maintenance Page contains test timeCollectionRule",
104                                    resultantPageAfterEdit.asText().contains(
105                                                    "The specified workarea '"
106                                                                    + TEST_CODE_INVALID_WORKAREA
107                                                                    + "' does not exist."));
108            }
109    
110            /**
111             * Test to load maint. screen
112             *
113             * @throws Exception
114             */
115            @Test
116            public void testTimeCollectionRuleMaint() throws Exception {
117                    HtmlPage timeCollectionRuleLookup = HtmlUnitUtil
118                                    .gotoPageAndLogin(getWebClient(), TkTestConstants.Urls.TIME_COLLECTION_RULE_MAINT_URL);
119                    timeCollectionRuleLookup = HtmlUnitUtil.clickInputContainingText(
120                                    timeCollectionRuleLookup, "search");
121                    Assert.assertTrue("Page contains test timeCollectionRule",
122                                    timeCollectionRuleLookup.asText().contains(TEST_CODE));
123                    HtmlPage maintPage = HtmlUnitUtil.clickAnchorContainingText(
124                                    timeCollectionRuleLookup, "edit",
125                                    timeCollectionRuleId);
126                    Assert.assertTrue("Maintenance Page contains test timeCollectionRule",
127                                    maintPage.asText().contains(TEST_CODE));
128            }
129    
130            @Override
131            public void setUp() throws Exception {
132                    super.setUp();
133                    Department department = new Department();
134                    department.setDept(TEST_CODE_DEPARTMENT_VALID);
135                    department.setChart(TEST_CODE_DEPARTMENT_VALID);
136                    department.setDescription(TEST_CODE_DEPARTMENT_VALID);
137                    department.setOrg(TEST_CODE_DEPARTMENT_VALID);
138                    department.setLocation("BL");
139                    department.setEffectiveLocalDate(TEST_DATE);
140            department.setActive(Boolean.TRUE);
141                    department = KRADServiceLocator.getBusinessObjectService().save(department);
142                    
143                    TimeCollectionRule timeCollectionRule = new TimeCollectionRule();
144                    timeCollectionRule.setDept(TEST_CODE_DEPARTMENT_VALID);
145                    timeCollectionRule.setEffectiveLocalDate(TEST_DATE);
146                    timeCollectionRule.setTimestamp(TKUtils.getCurrentTimestamp());
147                    timeCollectionRule.setUserPrincipalId(TEST_CODE);
148            timeCollectionRule.setActive(true);
149            timeCollectionRule.setPayType("%");
150            timeCollectionRule = KRADServiceLocator.getBusinessObjectService().save(timeCollectionRule);
151                    timeCollectionRuleId = timeCollectionRule.getTkTimeCollectionRuleId();
152    
153                    TimeCollectionRule timeCollectionRuleWIthInvalidWorkArea = new TimeCollectionRule();
154                    // setting workAreaId for which Workarea doesn't exist .
155                    Random randomObj = new Random();
156                    for (;;) {
157                            long workAreaIndex = randomObj.nextInt();
158                            int count = HrServiceLocator.getWorkAreaService().getWorkAreaCount(null, workAreaIndex);
159    
160                            if (count == 0) {
161                                    TEST_CODE_INVALID_WORKAREA = new Long(workAreaIndex);
162                                    break;
163                            }
164                    }
165                    timeCollectionRuleWIthInvalidWorkArea
166                                    .setDept(TEST_CODE_DEPARTMENT_VALID);
167                    timeCollectionRuleWIthInvalidWorkArea.setEffectiveLocalDate(TEST_DATE);
168            timeCollectionRuleWIthInvalidWorkArea.setActive(true);
169            timeCollectionRuleWIthInvalidWorkArea.setPayType("%");
170                    timeCollectionRuleWIthInvalidWorkArea.setTimestamp(TKUtils.getCurrentTimestamp());
171                    timeCollectionRuleWIthInvalidWorkArea.setUserPrincipalId(TEST_CODE);
172                    timeCollectionRuleWIthInvalidWorkArea
173                                    .setWorkArea(TEST_CODE_INVALID_WORKAREA);
174            timeCollectionRuleWIthInvalidWorkArea = KRADServiceLocator.getBusinessObjectService().save(
175                                    timeCollectionRuleWIthInvalidWorkArea);
176                    timeCollectionRuleIdWithInvalidWorkArea = timeCollectionRuleWIthInvalidWorkArea
177                                    .getTkTimeCollectionRuleId();
178    
179            }
180    
181            @Override
182            public void tearDown() throws Exception {
183                    // cleaning up
184                    TimeCollectionRule timeCollectionRuleObj = KRADServiceLocator.getBusinessObjectService()
185                    .findByPrimaryKey(TimeCollectionRule.class, Collections.singletonMap("tkTimeCollectionRuleId", timeCollectionRuleId));
186            //Map<String, String> criteria = new (Collections()).singletonMap("dept")
187            //Collection<TimeCollectionRule> rules = KRADServiceLocator.getBusinessObjectService().findMatching(TimeCollectionRule.class, )
188                    KRADServiceLocator.getBusinessObjectService().delete(
189                                    timeCollectionRuleObj);
190    
191                    timeCollectionRuleObj = TkServiceLocator.getTimeCollectionRuleService().getTimeCollectionRule(TEST_CODE_DEPARTMENT_VALID,
192                                                                            TEST_CODE_INVALID_WORKAREA, "%", LocalDate.now());
193                    //timeCollectionRuleObj = KRADServiceLocator.getBusinessObjectService()
194            //        .findByPrimaryKey(TimeCollectionRule.class, Collections.singletonMap("tkTimeCollectionRuleId", timeCollectionRuleIdWithInvalidWorkArea));
195                    KRADServiceLocator.getBusinessObjectService().delete(
196                                    timeCollectionRuleObj);
197    
198                    Department deptObj = HrServiceLocator.getDepartmentService().getDepartmentWithoutRoles(TEST_CODE_DEPARTMENT_VALID, LocalDate.now());
199                    KRADServiceLocator.getBusinessObjectService().delete(deptObj);
200                    super.tearDown();
201            }
202    
203    }