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.Account;
29  import org.kuali.kpme.core.kfs.coa.businessobject.Organization;
30  import org.kuali.kpme.core.util.HrTestConstants;
31  import org.kuali.rice.krad.service.KRADServiceLocator;
32  
33  import com.gargoylesoftware.htmlunit.html.HtmlInput;
34  import com.gargoylesoftware.htmlunit.html.HtmlPage;
35  
36  public class OrganizationMaintTest extends KPMEWebTestCase {
37  	//TODO: find constant in outside scope, or define one outside of this scope.
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("chartOfAccountsCode", "UA");
59  		requiredFields.put("organizationCode", "A-ORG-CD");
60  		requiredFields.put("organizationName", "ANOTHER ORGANIZATION");
61  	}
62  	
63  	private void before() {
64  		
65  		newUrl = HrTestConstants.Urls.ORGANIZATION_MAINT_NEW_URL;
66  		lookupUrl = HrTestConstants.Urls.ORGANIZATION_MAINT_URL;
67  		
68  		requiredFields = new HashMap<String,String>();
69  		requiredFields.put("chartOfAccountsCode", "Chart Code (Chart) is a required field.");
70  		requiredFields.put("organizationCode", "Organization Code (Org) is a required field.");
71  		requiredFields.put("organizationName", "Organization Name (Org Name) is a required field.");
72  	}
73  	
74  	private void after() {
75  		requiredFields.clear();
76  	}
77  	
78  	@Test
79  	public void testRequiredFields() throws Exception {
80  		HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
81  		assertNotNull("maintenance page is null", maintPage);
82  		
83  		HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
84  		assertNotNull("maintenance page does not contain document description", docDescription);
85  		
86  		docDescription.setValueAttribute("testing submission");
87  		
88  		HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
89  		assertNotNull("no result page returned after submit", resultPage);
90  		
91  		String resultPageAsText = resultPage.asText();
92  		for(Entry<String,String> requiredField : requiredFields.entrySet()) {
93  			assertTrue("page does not contain error message for required field: '" + requiredField.getKey() + "'",
94  					resultPageAsText.contains(requiredField.getValue()));
95  		}
96  	}
97  	
98  	@Test
99  	public void testLookup() throws Exception {
100 		HtmlPage lookupPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), lookupUrl);
101 		assertNotNull("lookup page is null", lookupPage);
102 		
103 		lookupPage = HtmlUnitUtil.clickInputContainingText(lookupPage, "search");
104 		assertNotNull("lookup result page is null", lookupPage);
105 		
106 		assertTrue("lookup result page should contain one record", lookupPage.asText().contains("ORG-CODE"));
107 	}
108 	
109 	@Test
110 	public void testInValidChart() throws Exception {
111 		/**
112 		 * TODO: submit sub-object code whose object COA and account COA codes
113 		 * match the COA specified on this sub-object, but the account is open.
114 		 * 
115 		 * This test was changed from asserting a successful submission to asserting a non-successful
116 		 * insertion. Test data was added that marked the account used in this test as closed. Validation
117 		 * fails for closed accounts.
118 		 * 
119 		 */
120 		HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
121 		assertNotNull("maintenance page is null", maintPage);
122 		
123 		HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
124 		assertNotNull("maintenance page does not contain document description", docDescription);
125 		
126 		setDefaultTestInputValues();
127 		for(Entry<String,String> entry : requiredFields.entrySet()) {
128 			HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
129 		}
130 		docDescription.setValueAttribute("testing submission");
131 		// use a non-existent chart
132 		HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "chartOfAccountsCode","BP");
133 
134 		HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
135 		assertTrue("page should contain active account existence error", resultPage.asText().contains("No active chart exists for this code"));
136 	}
137 	
138 	@Test
139 	public void testValidChart() throws Exception {
140 
141 		HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
142 		assertNotNull("maintenance page is null", maintPage);
143 		
144 		HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
145 		assertNotNull("maintenance page does not contain document description", docDescription);
146 		
147 		setDefaultTestInputValues();
148 		for(Entry<String,String> entry : requiredFields.entrySet()) {
149 			HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
150 		}
151 		docDescription.setValueAttribute("testing submission");
152 
153 		HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
154 		assertTrue("page should contain active account existence error", !resultPage.asText().contains("error(s)"));
155 		
156 		Map<String,String> keys = new HashMap<String,String>();
157 		keys.put("chartOfAccountsCode", "UA");
158 		keys.put("organizationCode", "A-ORG-CD");
159 		
160 		Organization organization = (Organization) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(Organization.class, keys);
161 		assertNotNull("newly created sub-object code should exist", organization);
162 		//clean up after assertion.
163 		KRADServiceLocator.getBusinessObjectService().delete(organization);
164 	}
165 
166 }