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.Account;
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 AccountMaintTest 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  
45  		requiredFields.put("accountEffectiveDate", "01/01/2010");
46  		requiredFields.put("chartOfAccountsCode", "UA");
47  		requiredFields.put("accountNumber", "4444");
48  		requiredFields.put("accountName", "4444");
49  		requiredFields.put("organizationCode", "ORG-CODE");
50  	}
51  	
52  	private void before() {
53  		
54  		newUrl = HrTestConstants.Urls.ACCOUNT_MAINT_NEW_URL;
55  		lookupUrl = HrTestConstants.Urls.ACCOUNT_MAINT_URL;
56  		
57  		requiredFields = new HashMap<String,String>();
58  		requiredFields.put("accountEffectiveDate", "Account Effective Date (EffDate) is a required field.");
59  		requiredFields.put("chartOfAccountsCode", "Chart Code (Chart) is a required field.");
60  		requiredFields.put("accountNumber", "Account Number (Account Number) is a required field.");
61  		requiredFields.put("accountName", "Account Name (AcctName) is a required field.");
62  		requiredFields.put("organizationCode", "Organization Code (Org) is a required field.");
63  	}
64  	
65  	private void after() {
66  		requiredFields.clear();
67  	}
68  	
69  	@Test
70  	public void testRequiredFields() throws Exception {
71  		HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
72  		assertNotNull("maintenance page is null", maintPage);
73  		
74  		HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
75  		assertNotNull("maintenance page does not contain document description", docDescription);
76  		
77  		docDescription.setValueAttribute("testing submission");
78  		
79  		HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
80  		assertNotNull("no result page returned after submit", resultPage);
81  		
82  		String resultPageAsText = resultPage.asText();
83  		for(Entry<String,String> requiredField : requiredFields.entrySet()) {
84  			assertTrue("page does not contain error message for required field: '" + requiredField.getKey() + "'",
85  					resultPageAsText.contains(requiredField.getValue()));
86  		}
87  	}
88  	
89  	@Test
90  	public void testLookup() throws Exception {
91  		HtmlPage lookupPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), lookupUrl);
92  		assertNotNull("lookup page is null", lookupPage);
93  		
94  		lookupPage = HtmlUnitUtil.clickInputContainingText(lookupPage, "search");
95  		assertNotNull("lookup result page is null", lookupPage);
96  		
97  		assertTrue("lookup result page should contain one account '3333'",
98  				lookupPage.asText().contains("3333"));
99  	}
100 	
101 	@Test
102 	public void testInValidChart() throws Exception {
103 		/**
104 		 * TODO: submit sub-object code whose object COA and account COA codes
105 		 * match the COA specified on this sub-object, but the account is open.
106 		 * 
107 		 * This test was changed from asserting a successful submission to asserting a non-successful
108 		 * insertion. Test data was added that marked the account used in this test as closed. Validation
109 		 * fails for closed accounts.
110 		 * 
111 		 */
112 		HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
113 		assertNotNull("maintenance page is null", maintPage);
114 		
115 		HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
116 		assertNotNull("maintenance page does not contain document description", docDescription);
117 		
118 		setDefaultTestInputValues();
119 		for(Entry<String,String> entry : requiredFields.entrySet()) {
120 			HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
121 		}
122 		docDescription.setValueAttribute("testing submission");
123 		// use a non-existent chart
124 		HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "chartOfAccountsCode","BP");
125 
126 		HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
127 		assertTrue("page should contain active account existence error", resultPage.asText().contains("No active chart exists for this code"));
128 	}
129 	
130 	@Test
131 	public void testValidChart() throws Exception {
132 
133 		HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
134 		assertNotNull("maintenance page is null", maintPage);
135 		
136 		HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
137 		assertNotNull("maintenance page does not contain document description", docDescription);
138 		
139 		setDefaultTestInputValues();
140 		for(Entry<String,String> entry : requiredFields.entrySet()) {
141 			HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
142 		}
143 		docDescription.setValueAttribute("testing submission");
144 
145 		HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
146 		assertTrue("page should contain active account existence error", !resultPage.asText().contains("error(s)"));
147 		
148 		Map<String,String> keys = new HashMap<String,String>();
149 		keys.put("chartOfAccountsCode", "UA");
150 		keys.put("accountNumber", "4444");
151 		
152 		Account account = (Account) KRADServiceLocatorWeb.getLegacyDataAdapter().findByPrimaryKey(Account.class, keys);
153 		assertNotNull("newly created sub-object code should exist", account);
154 		//clean up after assertion.
155 		KRADServiceLocatorWeb.getLegacyDataAdapter().delete(account);
156 	}
157 	
158 	@Override
159 	public void setUp() throws Exception {
160 		before();
161 		super.setUp();
162 	}
163 	
164 	@Override
165 	public void tearDown() throws Exception {
166 		after();
167 		super.tearDown();
168 	}
169 }