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