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