View Javadoc
1   /**
2    * Copyright 2004-2015 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.Ignore;
26  import org.junit.Test;
27  import org.kuali.hr.KPMEWebTestCase;
28  import org.kuali.hr.util.HtmlUnitUtil;
29  import org.kuali.kpme.core.kfs.coa.businessobject.Organization;
30  import org.kuali.kpme.core.kfs.coa.businessobject.ProjectCode;
31  import org.kuali.kpme.core.util.HrTestConstants;
32  import org.kuali.rice.krad.service.KRADServiceLocator;
33  
34  import com.gargoylesoftware.htmlunit.html.HtmlInput;
35  import com.gargoylesoftware.htmlunit.html.HtmlPage;
36  
37  public class ProjectCodeMaintTest extends KPMEWebTestCase {
38  	
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  	private void before() {
45  		
46  		newUrl = HrTestConstants.Urls.PROJECT_CODE_MAINT_NEW_URL;
47  		lookupUrl = HrTestConstants.Urls.PROJECT_CODE_MAINT_URL;
48  		
49  		requiredFields = new HashMap<String,String>();
50  		requiredFields.put("code", "Project Code (Project) is a required field.");
51  		requiredFields.put("name", "Project Name (Name) is a required field.");
52  		requiredFields.put("chartOfAccountsCode", "Chart Code (Chart) is a required field.");
53  		requiredFields.put("organizationCode", "Organization Code (Org) is a required field.");
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("code", "PRJ-CODE");
61  		requiredFields.put("name", "PRJ-NAME");
62  		requiredFields.put("organizationCode", "ORG-CODE");
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 should not contain any project codes", lookupPage.asText().contains("No values match this search. "));
98  	}
99  	
100 	@Override
101 	public void setUp() throws Exception {
102 		super.setUp();
103 		before();
104 	}
105 
106 	@Override
107 	public void tearDown() throws Exception {
108 		after();
109 		super.tearDown();
110 	}
111 	
112 	@Test
113 	public void testInvalidChartConsistencyCaseTwo() throws Exception {
114 		/**
115 		 * TODO: submit project code whose specified organization's chart does not match the chart defined
116 		 * on this project code
117 		 */
118 		HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
119 		assertNotNull("maintenance page is null", maintPage);
120 		
121 		HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
122 		assertNotNull("maintenance page does not contain document description", docDescription);
123 
124 		setDefaultTestInputValues();
125 		for(Entry<String,String> entry : requiredFields.entrySet()) {
126 			HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
127 		}
128 
129 		docDescription.setValueAttribute("testing submission");
130 		//HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "accountNumber", "1111");
131 
132 		HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "organizationCode", "ORG-CODE2");
133 		// reset requiredFields map to default error messages.
134 		before();
135 		HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
136 		assertNotNull("no result page returned after submit", resultPage);
137 		
138 		String resultPageAsText = resultPage.asText();
139 		for(Entry<String,String> requiredField : requiredFields.entrySet()) {
140 			if(requiredField.getKey().equals("organizationCode")) {
141 				assertTrue("page does not contain error message for the invalid field: '" + requiredField.getKey() + "'",
142 						resultPageAsText.contains("No such active organization exists whose chart matches 'UA'"));
143 			}
144 		}
145 	}
146 	
147 	@Test
148 	public void testInValidChart() throws Exception {
149 		/**
150 		 * TODO: submit sub-object code whose object COA and account COA codes
151 		 * match the COA specified on this sub-object, but the account is open.
152 		 * 
153 		 * This test was changed from asserting a successful submission to asserting a non-successful
154 		 * insertion. Test data was added that marked the account used in this test as closed. Validation
155 		 * fails for closed accounts.
156 		 * 
157 		 */
158 		HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
159 		assertNotNull("maintenance page is null", maintPage);
160 		
161 		HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
162 		assertNotNull("maintenance page does not contain document description", docDescription);
163 		
164 		setDefaultTestInputValues();
165 		for(Entry<String,String> entry : requiredFields.entrySet()) {
166 			HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
167 		}
168 		docDescription.setValueAttribute("testing submission");
169 		// use a non-existent chart
170 		HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "chartOfAccountsCode","BP");
171 
172 		HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
173 		assertTrue("page should contain active chart existence error", resultPage.asText().contains("No active chart exists for this code"));
174 	}
175 	
176 	@Test
177 	public void testValidChart() throws Exception {
178 
179 		HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
180 		assertNotNull("maintenance page is null", maintPage);
181 		
182 		HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
183 		assertNotNull("maintenance page does not contain document description", docDescription);
184 		
185 		setDefaultTestInputValues();
186 		for(Entry<String,String> entry : requiredFields.entrySet()) {
187 			HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
188 		}
189 		docDescription.setValueAttribute("testing submission");
190 
191 		HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
192 
193 		assertTrue("page should contain active account existence error", !resultPage.asText().contains("error(s)"));
194 		
195 		ProjectCode projectCode = (ProjectCode) KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(ProjectCode.class, "PRJ-CODE");
196 		assertNotNull("newly created sub-object code should exist", projectCode);
197 		//clean up after assertion.
198 		KRADServiceLocator.getBusinessObjectService().delete(projectCode);
199 	}
200 }