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