View Javadoc

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