001/**
002 * Copyright 2004-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.hr.core.kfs;
017
018import static org.junit.Assert.assertNotNull;
019import static org.junit.Assert.assertTrue;
020import java.util.HashMap;
021import java.util.Map;
022import java.util.Map.Entry;
023
024import org.junit.Test;
025import org.kuali.hr.KPMEWebTestCase;
026import org.kuali.hr.util.HtmlUnitUtil;
027import org.kuali.kpme.core.kfs.coa.businessobject.SubObjectCode;
028import org.kuali.kpme.core.util.HrTestConstants;
029import org.kuali.rice.krad.service.KRADServiceLocator;
030import com.gargoylesoftware.htmlunit.html.HtmlInput;
031import com.gargoylesoftware.htmlunit.html.HtmlPage;
032import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
033
034public class SubObjectCodeMaintTest extends KPMEWebTestCase {
035
036        private static final String NEW_MAINT_DOC_PREFIX = "document.newMaintainableObject.";
037        private String newUrl;
038        private String lookupUrl;
039        private Map<String,String> requiredFields;
040
041        @Override
042        public void setUp() throws Exception {
043                // TODO Auto-generated method stub
044                super.setUp();
045                before();
046        }
047
048        @Override
049        public void tearDown() throws Exception {
050                requiredFields.clear();
051                // TODO Auto-generated method stub
052                super.tearDown();
053        }
054        
055        private void before() {
056                
057                newUrl = HrTestConstants.Urls.SUB_OBJECT_CODE_MAINT_NEW_URL;
058                lookupUrl = HrTestConstants.Urls.SUB_OBJECT_CODE_MAINT_URL;
059                
060                requiredFields = new HashMap<String,String>();
061                requiredFields.put("universityFiscalYear", "University Fiscal Year (Year) is a required field.");
062                requiredFields.put("chartOfAccountsCode", "Chart Code (Chart) is a required field.");
063                requiredFields.put("accountNumber", "Account Number (Account Number) is a required field.");
064                requiredFields.put("financialObjectCode", "Object Code (Object) is a required field.");
065                requiredFields.put("financialSubObjectCode", "Sub-Object Code (Sub-Object) is a required field.");
066                requiredFields.put("financialSubObjectCodeName", "Sub-Object Code Name (SubObjCodeName) is a required field.");
067                requiredFields.put("financialSubObjectCdshortNm", "Sub-Object Code Short Name (SubObjCodeShortName) is a required field.");
068        }
069        
070        private void setDefaultTestInputValues() {
071                requiredFields = new HashMap<String,String>();
072                requiredFields.put("active", "on");
073                requiredFields.put("universityFiscalYear", "2013");
074                requiredFields.put("chartOfAccountsCode", "UA");
075                requiredFields.put("accountNumber", "1111");
076                requiredFields.put("financialObjectCode", "1000");
077                requiredFields.put("financialSubObjectCode", "10");
078                requiredFields.put("financialSubObjectCodeName", "Test Sub Obj");
079                requiredFields.put("financialSubObjectCdshortNm", "Sub Obj TST");
080        }
081        
082        @Test
083        public void testRequiredFields() throws Exception {
084                HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
085                assertNotNull("maintenance page is null", maintPage);
086                
087                HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
088                assertNotNull("maintenance page does not contain document description", docDescription);
089                
090                docDescription.setValueAttribute("testing submission");
091                
092                HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
093                assertNotNull("no result page returned after submit", resultPage);
094                
095                String resultPageAsText = resultPage.asText();
096                for(Entry<String,String> requiredField : requiredFields.entrySet()) {
097                        assertTrue("page does not contain error message for required field: '" + requiredField.getKey() + "'",
098                                        resultPageAsText.contains(requiredField.getValue()));
099                }
100        }
101        
102        @Test
103        public void testLookup() throws Exception {
104                HtmlPage lookupPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), lookupUrl);
105                assertNotNull("lookup page is null", lookupPage);
106                
107                lookupPage = HtmlUnitUtil.clickInputContainingText(lookupPage, "search");
108                assertNotNull("lookup result page is null", lookupPage);
109                assertTrue("lookup page should contain 'UA Sub Obj'", lookupPage.asText().contains("UA Sub Obj"));
110        }
111        
112        @Test
113        public void testInvalidChartConsistencyCaseOne() throws Exception {
114                /**
115                 * TODO: submit sub-object code whose account COA code does not
116                 * match the COA specified on this sub-object
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                
131                // this account's COA Code is "UA"
132                HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "accountNumber", "9999");
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("accountNumber")) {
141                                assertTrue("page does not contain error message for the invalid field: '" + requiredField.getKey() + "'",
142                                                resultPageAsText.contains("No such active account exists whose chart matches 'UA'"));
143                        }
144                }
145        }
146        
147        @Test
148        public void testInvalidChartConsistencyCaseTwo() throws Exception {
149                /**
150                 * TODO: submit sub-object code whose object code chart does not
151                 * match the chart specified on the sub-object
152                 */
153                HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
154                assertNotNull("maintenance page is null", maintPage);
155                
156                HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
157                assertNotNull("maintenance page does not contain document description", docDescription);
158
159                setDefaultTestInputValues();
160                for(Entry<String,String> entry : requiredFields.entrySet()) {
161                        HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
162                }
163
164                docDescription.setValueAttribute("testing submission");
165                //HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "accountNumber", "1111");
166
167                HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "financialObjectCode", "9000");
168                // reset requiredFields map to default error messages.
169                before();
170                HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
171                assertNotNull("no result page returned after submit", resultPage);
172                
173                String resultPageAsText = resultPage.asText();
174                for(Entry<String,String> requiredField : requiredFields.entrySet()) {
175                        if(requiredField.getKey().equals("financialObjectCode")) {
176                                assertTrue("page does not contain error message for the invalid field: '" + requiredField.getKey() + "'",
177                                                resultPageAsText.contains("No such active object code exists whose chart matches 'UA'"));
178                        }
179                }
180        }
181        
182        @Test
183        public void testInvalidChartConsistencyCaseThree() throws Exception {
184                /**
185                 * TODO: submit sub-object code whose account chart and object code chart
186                 * do not match the chart specified on the sub-object
187                 */
188                HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
189                assertNotNull("maintenance page is null", maintPage);
190                
191                HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
192                assertNotNull("maintenance page does not contain document description", docDescription);
193
194                setDefaultTestInputValues();
195                for(Entry<String,String> entry : requiredFields.entrySet()) {
196                        HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
197                }
198
199                docDescription.setValueAttribute("testing submission");
200                
201                // this account's COA Code is "UA"
202                HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "accountNumber", "9999");
203                HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "financialObjectCode", "9000");
204                // reset requiredFields map to default error messages.
205                before();
206                HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
207                assertNotNull("no result page returned after submit", resultPage);
208                
209                String resultPageAsText = resultPage.asText();
210                for(Entry<String,String> requiredField : requiredFields.entrySet()) {
211                        if(requiredField.getKey().equals("financialObjectCode")) {
212                                assertTrue("page does not contain error message for the invalid field: '" + requiredField.getKey() + "'",
213                                                resultPageAsText.contains("No such active object code exists whose chart matches 'UA'"));
214                        }
215                        if(requiredField.getKey().equals("accountNumber")) {
216                                assertTrue("page does not contain error message for the invalid field: '" + requiredField.getKey() + "'",
217                                                resultPageAsText.contains("No such active account exists whose chart matches 'UA'"));
218                        }
219                }
220        }
221        
222        @Test
223        public void testValidChartConsistencyWithClosedAccount() throws Exception {
224                /**
225                 * TODO: submit sub-object code whose object COA and account COA codes
226                 * match the COA specified on this sub-object, but the account is open.
227                 * 
228                 * This test was changed from asserting a successful submission to asserting a non-successful
229                 * insertion. Test data was added that marked the account used in this test as closed. Validation
230                 * fails for closed accounts.
231                 * 
232                 */
233                HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
234                assertNotNull("maintenance page is null", maintPage);
235                
236                HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
237                assertNotNull("maintenance page does not contain document description", docDescription);
238                
239                setDefaultTestInputValues();
240                for(Entry<String,String> entry : requiredFields.entrySet()) {
241                        HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
242                }
243                docDescription.setValueAttribute("testing submission");
244                // account 2222 has same chart as default object code, and the chart input value on this form.
245                HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "accountNumber","2222");
246                // primary key includes fin_sub_obj_cd
247                HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "financialSubObjectCode", "20");
248                HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "financialSubObjectCodeName", "TST Sub object code");
249                HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "financialSubObjectCdshortNm", "TST SOC");
250
251                HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
252                assertTrue("page should contain active account existence error", resultPage.asText().contains("No such active account exists whose chart matches 'UA'"));
253                
254                Map<String,String> keys = new HashMap<String,String>();
255                keys.put("universityFiscalYear", "2013");
256                keys.put("chartOfAccountsCode", "UA");
257                keys.put("accountNumber", "2222");
258                keys.put("financialObjectCode", "1000");
259                keys.put("financialSubObjectCode", "20");
260                
261/*              SubObjectCode subObjectCode = KRADServiceLocatorWeb.getLegacyDataAdapter().findByPrimaryKey(SubObjectCode.class, keys);
262                assertNotNull("newly created sub-object code should exist", subObjectCode);
263                //clean up after assertion.
264                KRADServiceLocatorWeb.getLegacyDataAdapter().delete(subObjectCode);*/
265        }
266        
267        @Test
268        public void testValidChartConsistencyWithOpenAccount() throws Exception {
269
270                HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
271                assertNotNull("maintenance page is null", maintPage);
272                
273                HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
274                assertNotNull("maintenance page does not contain document description", docDescription);
275                
276                setDefaultTestInputValues();
277                for(Entry<String,String> entry : requiredFields.entrySet()) {
278                        HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
279                }
280                docDescription.setValueAttribute("testing submission");
281                // account 2222 has same chart as default object code, and the chart input value on this form.
282                HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "accountNumber","3333");
283                // primary key includes fin_sub_obj_cd
284                HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "financialSubObjectCode", "30");
285                HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "financialSubObjectCodeName", "TST Sub object code 3");
286                HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "financialSubObjectCdshortNm", "TST SOC 3");
287
288                HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
289                assertTrue("page should contain active account existence error", !resultPage.asText().contains("error(s)"));
290                
291                Map<String,String> keys = new HashMap<String,String>();
292                keys.put("universityFiscalYear", "2013");
293                keys.put("chartOfAccountsCode", "UA");
294                keys.put("accountNumber", "3333");
295                keys.put("financialObjectCode", "1000");
296                keys.put("financialSubObjectCode", "30");
297                
298                SubObjectCode subObjectCode = KRADServiceLocatorWeb.getLegacyDataAdapter().findByPrimaryKey(SubObjectCode.class, keys);
299                assertNotNull("newly created sub-object code should exist", subObjectCode);
300                //clean up after assertion.
301                KRADServiceLocatorWeb.getLegacyDataAdapter().delete(subObjectCode);
302        }
303        
304        @Test
305        public void testInValidChart() throws Exception {
306                /**
307                 * TODO: submit sub-object code whose object COA and account COA codes
308                 * match the COA specified on this sub-object, but the account is open.
309                 * 
310                 * This test was changed from asserting a successful submission to asserting a non-successful
311                 * insertion. Test data was added that marked the account used in this test as closed. Validation
312                 * fails for closed accounts.
313                 * 
314                 */
315                HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
316                assertNotNull("maintenance page is null", maintPage);
317                
318                HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
319                assertNotNull("maintenance page does not contain document description", docDescription);
320                
321                setDefaultTestInputValues();
322                for(Entry<String,String> entry : requiredFields.entrySet()) {
323                        HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
324                }
325                docDescription.setValueAttribute("testing submission");
326                // use a non-existent chart
327                HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "chartOfAccountsCode","BP");
328
329                HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
330
331                assertTrue("page should contain active chart existence error", resultPage.asText().contains("No active chart exists for this code"));
332        }
333
334}