View Javadoc

1   /**
2    * Copyright 2004-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.time.collection;
17  
18  import java.util.Collections;
19  import java.util.Random;
20  
21  import org.joda.time.LocalDate;
22  import org.junit.Assert;
23  import org.junit.Test;
24  import org.kuali.hr.KPMEWebTestCase;
25  import org.kuali.hr.util.HtmlUnitUtil;
26  import org.kuali.kpme.core.FunctionalTest;
27  import org.kuali.kpme.core.department.Department;
28  import org.kuali.kpme.core.service.HrServiceLocator;
29  import org.kuali.kpme.core.util.TKUtils;
30  import org.kuali.kpme.tklm.time.rules.timecollection.TimeCollectionRule;
31  import org.kuali.kpme.tklm.time.service.TkServiceLocator;
32  import org.kuali.kpme.tklm.utils.TkTestConstants;
33  import org.kuali.rice.krad.service.KRADServiceLocator;
34  
35  import com.gargoylesoftware.htmlunit.html.HtmlInput;
36  import com.gargoylesoftware.htmlunit.html.HtmlPage;
37  
38  @FunctionalTest
39  public class TimeCollectionRuleMaintTest extends KPMEWebTestCase {
40  
41  	private static final String TEST_CODE = "X";
42  	private static final LocalDate TEST_DATE = LocalDate.now();
43  	private static final String TEST_CODE_DEPARTMENT_VALID = "_test";
44  
45  	private static String timeCollectionRuleId;
46  	private static String timeCollectionRuleIdWithInvalidWorkArea;
47  
48  	private static String TEST_CODE_INVALID_DEPT = "INVALID_DEPT";
49  	private static Long TEST_CODE_INVALID_WORKAREA = 2L;
50  	private static String PAY_TYPE_ERROR = "The specified payType '%' does not exist.";
51  
52  	/**
53  	 * Test to check whether it is showing error message on maintenance screen
54  	 * if we supply non exist deptId
55  	 *
56  	 * @throws Exception
57  	 */
58  	@Test
59  	public void testTimeCollectionRuleMaintForDeptErrorMessage() throws Exception {
60  		String baseUrl = TkTestConstants.Urls.TIME_COLLECTION_RULE_MAINT_NEW_URL;
61  	  	HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), baseUrl);
62  		HtmlInput inputForDescription = HtmlUnitUtil.getInputContainingText(page, "* Document Description");
63  		inputForDescription.setValueAttribute("Description");
64  		
65  		HtmlUnitUtil.setFieldValue(page, "document.newMaintainableObject.effectiveDate", "01/01/2010");
66  		HtmlUnitUtil.setFieldValue(page, "document.newMaintainableObject.dept", TEST_CODE_INVALID_DEPT);
67  		HtmlUnitUtil.setFieldValue(page, "document.newMaintainableObject.workArea", "30");
68  		HtmlUnitUtil.setFieldValue(page, "document.newMaintainableObject.payType", "BW");
69  		HtmlPage resultantPageAfterEdit = HtmlUnitUtil
70  				.clickInputContainingText(page, "submit");
71  		HtmlUnitUtil.createTempFile(resultantPageAfterEdit);
72  		Assert.assertTrue("Maintenance Page contains test timeCollectionRule",
73  				resultantPageAfterEdit.asText().contains(
74  						"The specified department '"
75  								+ TEST_CODE_INVALID_DEPT
76  								+ "' does not exist."));
77  		
78  		HtmlUnitUtil.setFieldValue(resultantPageAfterEdit, "document.newMaintainableObject.payType", "%");
79  		resultantPageAfterEdit = HtmlUnitUtil.clickInputContainingText(resultantPageAfterEdit, "submit");
80  		Assert.assertFalse("Maintenance Page contains error" + PAY_TYPE_ERROR, 
81  				resultantPageAfterEdit.asText().contains(PAY_TYPE_ERROR));
82  		
83  	}
84  
85  
86  	@Test
87  	public void testTimeCollectionRuleMaintForWorkAreaErrorMessage() throws Exception {
88  		HtmlPage timeCollectionRuleLookup = HtmlUnitUtil
89  				.gotoPageAndLogin(getWebClient(), TkTestConstants.Urls.TIME_COLLECTION_RULE_MAINT_URL);
90  		timeCollectionRuleLookup = HtmlUnitUtil.clickInputContainingText(
91  				timeCollectionRuleLookup, "search");
92  		Assert.assertTrue("Page contains test timeCollectionRule",
93  				timeCollectionRuleLookup.asText().contains(TEST_CODE));
94  		HtmlPage maintPage = HtmlUnitUtil.clickAnchorContainingText(
95  				timeCollectionRuleLookup, "edit",
96  				timeCollectionRuleIdWithInvalidWorkArea.toString());
97  		HtmlUnitUtil.createTempFile(maintPage);
98  		HtmlInput inputForDescription = HtmlUnitUtil.getInputContainingText(
99  				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 }