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.core.kfs;
17  
18  import static org.junit.Assert.assertNotNull;
19  import static org.junit.Assert.assertTrue;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  import java.util.Map.Entry;
24  
25  import org.junit.Test;
26  import org.kuali.hr.KPMEWebTestCase;
27  import org.kuali.hr.util.HtmlUnitUtil;
28  import org.kuali.kpme.core.kfs.coa.businessobject.ObjectCode;
29  import org.kuali.kpme.core.util.HrTestConstants;
30  import org.kuali.rice.krad.service.KRADServiceLocator;
31  
32  import com.gargoylesoftware.htmlunit.html.HtmlInput;
33  import com.gargoylesoftware.htmlunit.html.HtmlPage;
34  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
35  
36  public class ObjectCodeMaintTest extends KPMEWebTestCase {
37  	
38  	private static final String NEW_MAINT_DOC_PREFIX = "document.newMaintainableObject.";
39  	private String newUrl;
40  	private String lookupUrl;
41  	private Map<String,String> requiredFields;
42  
43  	@Override
44  	public void setUp() throws Exception {
45  		super.setUp();
46  		before();
47  	}
48  
49  	@Override
50  	public void tearDown() throws Exception {
51  		after();
52  		super.tearDown();
53  	}
54  	
55  	private void setDefaultTestInputValues() {
56  		requiredFields = new HashMap<String,String>();
57  		requiredFields.put("active", "on");
58  		requiredFields.put("universityFiscalYear", "2013");
59  		requiredFields.put("chartOfAccountsCode", "UA");
60  		requiredFields.put("financialObjectCode", "3000");
61  		requiredFields.put("financialObjectCodeName", "Regular Earnings");
62  		requiredFields.put("financialObjectCodeShortName", "Reg Earns");
63  	}
64  	
65  	private void before() {
66  		
67  		newUrl = HrTestConstants.Urls.OBJECT_CODE_MAINT_NEW_URL;
68  		lookupUrl = HrTestConstants.Urls.OBJECT_CODE_MAINT_URL;
69  		
70  		requiredFields = new HashMap<String,String>();
71  		requiredFields.put("universityFiscalYear", "Fiscal Year (FY) is a required field.");
72  		requiredFields.put("chartOfAccountsCode", "Chart of Accounts Code (ChartOfAccountsCode) is a required field.");
73  		requiredFields.put("financialObjectCode", "Object Code (Object) is a required field.");
74  		requiredFields.put("financialObjectCodeName", "Object Code Name (ObjCodeName) is a required field.");
75  		requiredFields.put("financialObjectCodeShortName", "Object Code Short Name (ObjCodeShortName) is a required field.");
76  	}
77  	
78  	private void after() {
79  		requiredFields.clear();
80  	}
81  	
82  	@Test
83  	public void testRequiredFields() throws Exception {
84  		HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
85  		assertNotNull("maintenance page is null", maintPage);
86  		
87  		HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
88  		assertNotNull("maintenance page does not contain document description", docDescription);
89  		
90  		docDescription.setValueAttribute("testing submission");
91  		
92  		HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
93  		assertNotNull("no result page returned after submit", resultPage);
94  		
95  		String resultPageAsText = resultPage.asText();
96  		for(Entry<String,String> requiredField : requiredFields.entrySet()) {
97  			assertTrue("page does not contain error message for required field: '" + requiredField.getKey() + "'",
98  					resultPageAsText.contains(requiredField.getValue()));
99  		}
100 	}
101 	
102 	@Test
103 	public void testLookup() throws Exception {
104 		HtmlPage lookupPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), lookupUrl);
105 		assertNotNull("lookup page is null", lookupPage);
106 		
107 		HtmlUnitUtil.setFieldValue(lookupPage, "universityFiscalYear", "2013");
108 		
109 		lookupPage = HtmlUnitUtil.clickInputContainingText(lookupPage, "search");
110 		assertNotNull("lookup result page is null", lookupPage);
111 		
112 		String lookupPageAsText = lookupPage.asText();
113 		assertTrue("lookup result page should contain three records",
114 				lookupPageAsText.contains("9000") && lookupPageAsText.contains("1000") && lookupPageAsText.contains("2000"));
115 		
116 	}
117 	
118 	@Test
119 	public void testInValidChart() throws Exception {
120 		/**
121 		 * TODO: submit sub-object code whose object COA and account COA codes
122 		 * match the COA specified on this sub-object, but the account is open.
123 		 * 
124 		 * This test was changed from asserting a successful submission to asserting a non-successful
125 		 * insertion. Test data was added that marked the account used in this test as closed. Validation
126 		 * fails for closed accounts.
127 		 * 
128 		 */
129 		HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
130 		assertNotNull("maintenance page is null", maintPage);
131 		
132 		HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
133 		assertNotNull("maintenance page does not contain document description", docDescription);
134 		
135 		setDefaultTestInputValues();
136 		for(Entry<String,String> entry : requiredFields.entrySet()) {
137 			HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
138 		}
139 		docDescription.setValueAttribute("testing submission");
140 		// use a non-existent chart
141 		HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "chartOfAccountsCode","BP");
142 
143 		HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
144 
145 		assertTrue("page should contain active chart existence error", resultPage.asText().contains("No active chart exists for this code"));
146 	}
147 	
148 	@Test
149 	public void testValidChart() throws Exception {
150 
151 		HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
152 		assertNotNull("maintenance page is null", maintPage);
153 		
154 		HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
155 		assertNotNull("maintenance page does not contain document description", docDescription);
156 		
157 		setDefaultTestInputValues();
158 		for(Entry<String,String> entry : requiredFields.entrySet()) {
159 			HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
160 		}
161 		docDescription.setValueAttribute("testing submission");
162 
163 		HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
164 		assertTrue("page should contain active account existence error", !resultPage.asText().contains("error(s)"));
165 		
166 		Map<String,String> keys = new HashMap<String,String>();
167 		keys.put("chartOfAccountsCode", "UA");
168 		keys.put("universityFiscalYear", "2013");
169 		keys.put("financialObjectCode", "3000");
170 		
171 		ObjectCode objectCode = (ObjectCode) KRADServiceLocatorWeb.getLegacyDataAdapter().findByPrimaryKey(ObjectCode.class, keys);
172 		assertNotNull("newly created sub-object code should exist", objectCode);
173 		//clean up after assertion.
174 		KRADServiceLocatorWeb.getLegacyDataAdapter().delete(objectCode);
175 	}
176 
177 }