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.*;
19  
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.SubAccount;
28  import org.kuali.kpme.core.util.HrTestConstants;
29  import org.kuali.rice.krad.service.KRADServiceLocator;
30  
31  import com.gargoylesoftware.htmlunit.html.HtmlInput;
32  import com.gargoylesoftware.htmlunit.html.HtmlPage;
33  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
34  
35  public class SubAccountMaintenanceTest extends KPMEWebTestCase {
36  	
37  	private static final String NEW_MAINT_DOC_PREFIX = "document.newMaintainableObject.";
38  	private String newUrl;
39  	private String lookupUrl;
40  	private Map<String,String> requiredFields;
41  
42  	private void setDefaultTestInputValues() {
43  		requiredFields = new HashMap<String,String>();
44  		requiredFields.put("active", "on");
45  		requiredFields.put("chartOfAccountsCode", "UA");
46  		requiredFields.put("subAccountNumber", "4444");
47  		requiredFields.put("subAccountName", "4444");
48  		requiredFields.put("accountNumber", "3333");
49  	}
50  	
51  	private void before() {
52  		
53  		newUrl = HrTestConstants.Urls.SUB_ACCOUNT_MAINT_NEW_URL;
54  		lookupUrl = HrTestConstants.Urls.SUB_ACCOUNT_MAINT_URL;
55  		
56  		requiredFields = new HashMap<String,String>();
57  		requiredFields.put("chartOfAccountsCode", "Chart Code (Chart) is a required field.");
58  		requiredFields.put("subAccountNumber", "Sub-Account Number (Sub-Account) is a required field.");
59  		requiredFields.put("subAccountName", "Sub-Account Name (Sub-Acct Name) is a required field.");
60  		requiredFields.put("accountNumber", "Account Number (Account Number) is a required field.");
61  	}
62  	
63  	private void after() {
64  		requiredFields.clear();
65  	}
66  	
67  	@Test
68  	public void testRequiredFields() throws Exception {
69  		HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
70  		assertNotNull("maintenance page is null", maintPage);
71  		
72  		HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
73  		assertNotNull("maintenance page does not contain document description", docDescription);
74  		
75  		docDescription.setValueAttribute("testing submission");
76  		
77  		HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
78  		assertNotNull("no result page returned after submit", resultPage);
79  		
80  		String resultPageAsText = resultPage.asText();
81  		for(Entry<String,String> requiredField : requiredFields.entrySet()) {
82  			assertTrue("page does not contain error message for required field: '" + requiredField.getKey() + "'",
83  					resultPageAsText.contains(requiredField.getValue()));
84  		}
85  	}
86  	
87  	@Test
88  	public void testLookup() throws Exception {
89  		HtmlPage lookupPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), lookupUrl);
90  		assertNotNull("lookup page is null", lookupPage);
91  		
92  		lookupPage = HtmlUnitUtil.clickInputContainingText(lookupPage, "search");
93  		assertNotNull("lookup result page is null", lookupPage);
94  
95  		//TODO: add valid test data, verify returned data in lookup result page.
96  	}
97  	
98  	@Test
99  	public void testInValidChart() throws Exception {
100 		/**
101 		 * TODO: submit sub-object code whose object COA and account COA codes
102 		 * match the COA specified on this sub-object, but the account is open.
103 		 * 
104 		 * This test was changed from asserting a successful submission to asserting a non-successful
105 		 * insertion. Test data was added that marked the account used in this test as closed. Validation
106 		 * fails for closed accounts.
107 		 * 
108 		 */
109 		HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
110 		assertNotNull("maintenance page is null", maintPage);
111 		
112 		HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
113 		assertNotNull("maintenance page does not contain document description", docDescription);
114 		
115 		setDefaultTestInputValues();
116 		for(Entry<String,String> entry : requiredFields.entrySet()) {
117 			HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
118 		}
119 		docDescription.setValueAttribute("testing submission");
120 		// use a non-existent chart
121 		HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "chartOfAccountsCode","BP");
122 
123 		HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
124 		assertTrue("page should contain active account existence error", resultPage.asText().contains("No active chart exists for this code"));
125 	}
126 	
127 	@Test
128 	public void testValidChart() throws Exception {
129 		/**
130 		 * 
131 		 * Also tests chart consistency with valid account
132 		 * 
133 		 */
134 		HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
135 		assertNotNull("maintenance page is null", maintPage);
136 		
137 		HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
138 		assertNotNull("maintenance page does not contain document description", docDescription);
139 		
140 		setDefaultTestInputValues();
141 		for(Entry<String,String> entry : requiredFields.entrySet()) {
142 			HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
143 		}
144 		docDescription.setValueAttribute("testing submission");
145 
146 		HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
147 
148 		assertTrue("page should not contain errors", !resultPage.asText().contains("error(s)"));
149 		
150 		Map<String,String> keys = new HashMap<String,String>();
151 		keys.put("chartOfAccountsCode", "UA");
152 		keys.put("accountNumber", "3333");
153 		keys.put("subAccountNumber", "4444");
154 		
155 		SubAccount subAccount = (SubAccount) KRADServiceLocatorWeb.getLegacyDataAdapter().findByPrimaryKey(SubAccount.class, keys);
156 		assertNotNull("newly created sub-object code should exist", subAccount);
157 		//clean up after assertion.
158 		KRADServiceLocatorWeb.getLegacyDataAdapter().delete(subAccount);
159 	}
160 	
161 	@Test
162 	public void testInvalidAccount() throws Exception {
163 
164 		HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
165 		assertNotNull("maintenance page is null", maintPage);
166 		
167 		HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
168 		assertNotNull("maintenance page does not contain document description", docDescription);
169 		
170 		setDefaultTestInputValues();
171 		for(Entry<String,String> entry : requiredFields.entrySet()) {
172 			HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
173 		}
174 		docDescription.setValueAttribute("testing submission");
175 		
176 		HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "accountNumber", "9999");
177 		
178 		HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
179 		assertTrue("page should contain active account existence error", resultPage.asText().contains("No such active account exists whose chart matches 'UA'"));
180 	}
181 	
182 	@Override
183 	public void setUp() throws Exception {
184 		super.setUp();
185 		before();
186 	}
187 	
188 	@Override
189 	public void tearDown() throws Exception {
190 		after();
191 		super.tearDown();
192 	}
193 }