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