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