View Javadoc
1   /**
2    * Copyright 2004-2015 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.core.kfs;
17  
18  import static org.junit.Assert.assertNotNull;
19  import static org.junit.Assert.assertTrue;
20  import java.util.HashMap;
21  import java.util.Map;
22  import java.util.Map.Entry;
23  
24  import org.junit.Test;
25  import org.kuali.hr.KPMEWebTestCase;
26  import org.kuali.hr.util.HtmlUnitUtil;
27  import org.kuali.kpme.core.kfs.coa.businessobject.SubObjectCode;
28  import org.kuali.kpme.core.util.HrTestConstants;
29  import org.kuali.rice.krad.service.KRADServiceLocator;
30  import com.gargoylesoftware.htmlunit.html.HtmlInput;
31  import com.gargoylesoftware.htmlunit.html.HtmlPage;
32  
33  public class SubObjectCodeMaintTest extends KPMEWebTestCase {
34  
35  	private static final String NEW_MAINT_DOC_PREFIX = "document.newMaintainableObject.";
36  	private String newUrl;
37  	private String lookupUrl;
38  	private Map<String,String> requiredFields;
39  
40  	@Override
41  	public void setUp() throws Exception {
42  		// TODO Auto-generated method stub
43  		super.setUp();
44  		before();
45  	}
46  
47  	@Override
48  	public void tearDown() throws Exception {
49  		requiredFields.clear();
50  		// TODO Auto-generated method stub
51  		super.tearDown();
52  	}
53  	
54  	private void before() {
55  		
56  		newUrl = HrTestConstants.Urls.SUB_OBJECT_CODE_MAINT_NEW_URL;
57  		lookupUrl = HrTestConstants.Urls.SUB_OBJECT_CODE_MAINT_URL;
58  		
59  		requiredFields = new HashMap<String,String>();
60  		requiredFields.put("universityFiscalYear", "University Fiscal Year (Year) is a required field.");
61  		requiredFields.put("chartOfAccountsCode", "Chart Code (Chart) is a required field.");
62  		requiredFields.put("accountNumber", "Account Number (Account Number) is a required field.");
63  		requiredFields.put("financialObjectCode", "Object Code (Object) is a required field.");
64  		requiredFields.put("financialSubObjectCode", "Sub-Object Code (Sub-Object) is a required field.");
65  		requiredFields.put("financialSubObjectCodeName", "Sub-Object Code Name (SubObjCodeName) is a required field.");
66  		requiredFields.put("financialSubObjectCdshortNm", "Sub-Object Code Short Name (SubObjCodeShortName) is a required field.");
67  	}
68  	
69  	private void setDefaultTestInputValues() {
70  		requiredFields = new HashMap<String,String>();
71  		requiredFields.put("active", "on");
72  		requiredFields.put("universityFiscalYear", "2013");
73  		requiredFields.put("chartOfAccountsCode", "UA");
74  		requiredFields.put("accountNumber", "1111");
75  		requiredFields.put("financialObjectCode", "1000");
76  		requiredFields.put("financialSubObjectCode", "10");
77  		requiredFields.put("financialSubObjectCodeName", "Test Sub Obj");
78  		requiredFields.put("financialSubObjectCdshortNm", "Sub Obj TST");
79  	}
80  	
81  	@Test
82  	public void testRequiredFields() throws Exception {
83  		HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
84  		assertNotNull("maintenance page is null", maintPage);
85  		
86  		HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
87  		assertNotNull("maintenance page does not contain document description", docDescription);
88  		
89  		docDescription.setValueAttribute("testing submission");
90  		
91  		HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
92  		assertNotNull("no result page returned after submit", resultPage);
93  		
94  		String resultPageAsText = resultPage.asText();
95  		for(Entry<String,String> requiredField : requiredFields.entrySet()) {
96  			assertTrue("page does not contain error message for required field: '" + requiredField.getKey() + "'",
97  					resultPageAsText.contains(requiredField.getValue()));
98  		}
99  	}
100 	
101 	@Test
102 	public void testLookup() throws Exception {
103 		HtmlPage lookupPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), lookupUrl);
104 		assertNotNull("lookup page is null", lookupPage);
105 		
106 		lookupPage = HtmlUnitUtil.clickInputContainingText(lookupPage, "search");
107 		assertNotNull("lookup result page is null", lookupPage);
108 		assertTrue("lookup page should contain 'UA Sub Obj'", lookupPage.asText().contains("UA Sub Obj"));
109 	}
110 	
111 	@Test
112 	public void testInvalidChartConsistencyCaseOne() throws Exception {
113 		/**
114 		 * TODO: submit sub-object code whose account COA code does not
115 		 * match the COA specified on this sub-object
116 		 */
117 		HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
118 		assertNotNull("maintenance page is null", maintPage);
119 		
120 		HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
121 		assertNotNull("maintenance page does not contain document description", docDescription);
122 
123 		setDefaultTestInputValues();
124 		for(Entry<String,String> entry : requiredFields.entrySet()) {
125 			HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
126 		}
127 
128 		docDescription.setValueAttribute("testing submission");
129 		
130 		// this account's COA Code is "UA"
131 		HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "accountNumber", "9999");
132 		// reset requiredFields map to default error messages.
133 		before();
134 		HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
135 		assertNotNull("no result page returned after submit", resultPage);
136 		
137 		String resultPageAsText = resultPage.asText();
138 		for(Entry<String,String> requiredField : requiredFields.entrySet()) {
139 			if(requiredField.getKey().equals("accountNumber")) {
140 				assertTrue("page does not contain error message for the invalid field: '" + requiredField.getKey() + "'",
141 						resultPageAsText.contains("No such active account exists whose chart matches 'UA'"));
142 			}
143 		}
144 	}
145 	
146 	@Test
147 	public void testInvalidChartConsistencyCaseTwo() throws Exception {
148 		/**
149 		 * TODO: submit sub-object code whose object code chart does not
150 		 * match the chart specified on the sub-object
151 		 */
152 		HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
153 		assertNotNull("maintenance page is null", maintPage);
154 		
155 		HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
156 		assertNotNull("maintenance page does not contain document description", docDescription);
157 
158 		setDefaultTestInputValues();
159 		for(Entry<String,String> entry : requiredFields.entrySet()) {
160 			HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
161 		}
162 
163 		docDescription.setValueAttribute("testing submission");
164 		//HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "accountNumber", "1111");
165 
166 		HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "financialObjectCode", "9000");
167 		// reset requiredFields map to default error messages.
168 		before();
169 		HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
170 		assertNotNull("no result page returned after submit", resultPage);
171 		
172 		String resultPageAsText = resultPage.asText();
173 		for(Entry<String,String> requiredField : requiredFields.entrySet()) {
174 			if(requiredField.getKey().equals("financialObjectCode")) {
175 				assertTrue("page does not contain error message for the invalid field: '" + requiredField.getKey() + "'",
176 						resultPageAsText.contains("No such active object code exists whose chart matches 'UA'"));
177 			}
178 		}
179 	}
180 	
181 	@Test
182 	public void testInvalidChartConsistencyCaseThree() throws Exception {
183 		/**
184 		 * TODO: submit sub-object code whose account chart and object code chart
185 		 * do not match the chart specified on the sub-object
186 		 */
187 		HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
188 		assertNotNull("maintenance page is null", maintPage);
189 		
190 		HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
191 		assertNotNull("maintenance page does not contain document description", docDescription);
192 
193 		setDefaultTestInputValues();
194 		for(Entry<String,String> entry : requiredFields.entrySet()) {
195 			HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
196 		}
197 
198 		docDescription.setValueAttribute("testing submission");
199 		
200 		// this account's COA Code is "UA"
201 		HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "accountNumber", "9999");
202 		HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "financialObjectCode", "9000");
203 		// reset requiredFields map to default error messages.
204 		before();
205 		HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
206 		assertNotNull("no result page returned after submit", resultPage);
207 		
208 		String resultPageAsText = resultPage.asText();
209 		for(Entry<String,String> requiredField : requiredFields.entrySet()) {
210 			if(requiredField.getKey().equals("financialObjectCode")) {
211 				assertTrue("page does not contain error message for the invalid field: '" + requiredField.getKey() + "'",
212 						resultPageAsText.contains("No such active object code exists whose chart matches 'UA'"));
213 			}
214 			if(requiredField.getKey().equals("accountNumber")) {
215 				assertTrue("page does not contain error message for the invalid field: '" + requiredField.getKey() + "'",
216 						resultPageAsText.contains("No such active account exists whose chart matches 'UA'"));
217 			}
218 		}
219 	}
220 	
221 	@Test
222 	public void testValidChartConsistencyWithClosedAccount() throws Exception {
223 		/**
224 		 * TODO: submit sub-object code whose object COA and account COA codes
225 		 * match the COA specified on this sub-object, but the account is open.
226 		 * 
227 		 * This test was changed from asserting a successful submission to asserting a non-successful
228 		 * insertion. Test data was added that marked the account used in this test as closed. Validation
229 		 * fails for closed accounts.
230 		 * 
231 		 */
232 		HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
233 		assertNotNull("maintenance page is null", maintPage);
234 		
235 		HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
236 		assertNotNull("maintenance page does not contain document description", docDescription);
237 		
238 		setDefaultTestInputValues();
239 		for(Entry<String,String> entry : requiredFields.entrySet()) {
240 			HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
241 		}
242 		docDescription.setValueAttribute("testing submission");
243 		// account 2222 has same chart as default object code, and the chart input value on this form.
244 		HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "accountNumber","2222");
245 		// primary key includes fin_sub_obj_cd
246 		HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "financialSubObjectCode", "20");
247 		HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "financialSubObjectCodeName", "TST Sub object code");
248 		HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "financialSubObjectCdshortNm", "TST SOC");
249 
250 		HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
251 		assertTrue("page should contain active account existence error", resultPage.asText().contains("No such active account exists whose chart matches 'UA'"));
252 		
253 		Map<String,String> keys = new HashMap<String,String>();
254 		keys.put("universityFiscalYear", "2013");
255 		keys.put("chartOfAccountsCode", "UA");
256 		keys.put("accountNumber", "2222");
257 		keys.put("financialObjectCode", "1000");
258 		keys.put("financialSubObjectCode", "20");
259 		
260 /*		SubObjectCode subObjectCode = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(SubObjectCode.class, keys);
261 		assertNotNull("newly created sub-object code should exist", subObjectCode);
262 		//clean up after assertion.
263 		KRADServiceLocator.getBusinessObjectService().delete(subObjectCode);*/
264 	}
265 	
266 	@Test
267 	public void testValidChartConsistencyWithOpenAccount() throws Exception {
268 
269 		HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
270 		assertNotNull("maintenance page is null", maintPage);
271 		
272 		HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
273 		assertNotNull("maintenance page does not contain document description", docDescription);
274 		
275 		setDefaultTestInputValues();
276 		for(Entry<String,String> entry : requiredFields.entrySet()) {
277 			HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
278 		}
279 		docDescription.setValueAttribute("testing submission");
280 		// account 2222 has same chart as default object code, and the chart input value on this form.
281 		HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "accountNumber","3333");
282 		// primary key includes fin_sub_obj_cd
283 		HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "financialSubObjectCode", "30");
284 		HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "financialSubObjectCodeName", "TST Sub object code 3");
285 		HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "financialSubObjectCdshortNm", "TST SOC 3");
286 
287 		HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
288 		assertTrue("page should contain active account existence error", !resultPage.asText().contains("error(s)"));
289 		
290 		Map<String,String> keys = new HashMap<String,String>();
291 		keys.put("universityFiscalYear", "2013");
292 		keys.put("chartOfAccountsCode", "UA");
293 		keys.put("accountNumber", "3333");
294 		keys.put("financialObjectCode", "1000");
295 		keys.put("financialSubObjectCode", "30");
296 		
297 		SubObjectCode subObjectCode = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(SubObjectCode.class, keys);
298 		assertNotNull("newly created sub-object code should exist", subObjectCode);
299 		//clean up after assertion.
300 		KRADServiceLocator.getBusinessObjectService().delete(subObjectCode);
301 	}
302 	
303 	@Test
304 	public void testInValidChart() throws Exception {
305 		/**
306 		 * TODO: submit sub-object code whose object COA and account COA codes
307 		 * match the COA specified on this sub-object, but the account is open.
308 		 * 
309 		 * This test was changed from asserting a successful submission to asserting a non-successful
310 		 * insertion. Test data was added that marked the account used in this test as closed. Validation
311 		 * fails for closed accounts.
312 		 * 
313 		 */
314 		HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
315 		assertNotNull("maintenance page is null", maintPage);
316 		
317 		HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
318 		assertNotNull("maintenance page does not contain document description", docDescription);
319 		
320 		setDefaultTestInputValues();
321 		for(Entry<String,String> entry : requiredFields.entrySet()) {
322 			HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
323 		}
324 		docDescription.setValueAttribute("testing submission");
325 		// use a non-existent chart
326 		HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "chartOfAccountsCode","BP");
327 
328 		HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
329 
330 		assertTrue("page should contain active chart existence error", resultPage.asText().contains("No active chart exists for this code"));
331 	}
332 
333 }