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.*;
019    
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.SubAccount;
028    import org.kuali.kpme.core.util.HrTestConstants;
029    import org.kuali.rice.krad.service.KRADServiceLocator;
030    
031    import com.gargoylesoftware.htmlunit.html.HtmlInput;
032    import com.gargoylesoftware.htmlunit.html.HtmlPage;
033    
034    public class SubAccountMaintenanceTest 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            private void setDefaultTestInputValues() {
042                    requiredFields = new HashMap<String,String>();
043                    requiredFields.put("active", "on");
044                    requiredFields.put("chartOfAccountsCode", "UA");
045                    requiredFields.put("subAccountNumber", "4444");
046                    requiredFields.put("subAccountName", "4444");
047                    requiredFields.put("accountNumber", "3333");
048            }
049            
050            private void before() {
051                    
052                    newUrl = HrTestConstants.Urls.SUB_ACCOUNT_MAINT_NEW_URL;
053                    lookupUrl = HrTestConstants.Urls.SUB_ACCOUNT_MAINT_URL;
054                    
055                    requiredFields = new HashMap<String,String>();
056                    requiredFields.put("chartOfAccountsCode", "Chart Code (Chart) is a required field.");
057                    requiredFields.put("subAccountNumber", "Sub-Account Number (Sub-Account) is a required field.");
058                    requiredFields.put("subAccountName", "Sub-Account Name (Sub-Acct Name) is a required field.");
059                    requiredFields.put("accountNumber", "Account Number (Account Number) is a required field.");
060            }
061            
062            private void after() {
063                    requiredFields.clear();
064            }
065            
066            @Test
067            public void testRequiredFields() throws Exception {
068                    HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
069                    assertNotNull("maintenance page is null", maintPage);
070                    
071                    HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
072                    assertNotNull("maintenance page does not contain document description", docDescription);
073                    
074                    docDescription.setValueAttribute("testing submission");
075                    
076                    HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
077                    assertNotNull("no result page returned after submit", resultPage);
078                    
079                    String resultPageAsText = resultPage.asText();
080                    for(Entry<String,String> requiredField : requiredFields.entrySet()) {
081                            assertTrue("page does not contain error message for required field: '" + requiredField.getKey() + "'",
082                                            resultPageAsText.contains(requiredField.getValue()));
083                    }
084            }
085            
086            @Test
087            public void testLookup() throws Exception {
088                    HtmlPage lookupPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), lookupUrl);
089                    assertNotNull("lookup page is null", lookupPage);
090                    
091                    lookupPage = HtmlUnitUtil.clickInputContainingText(lookupPage, "search");
092                    assertNotNull("lookup result page is null", lookupPage);
093    
094                    //TODO: add valid test data, verify returned data in lookup result page.
095            }
096            
097            @Test
098            public void testInValidChart() throws Exception {
099                    /**
100                     * TODO: submit sub-object code whose object COA and account COA codes
101                     * match the COA specified on this sub-object, but the account is open.
102                     * 
103                     * This test was changed from asserting a successful submission to asserting a non-successful
104                     * insertion. Test data was added that marked the account used in this test as closed. Validation
105                     * fails for closed accounts.
106                     * 
107                     */
108                    HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
109                    assertNotNull("maintenance page is null", maintPage);
110                    
111                    HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
112                    assertNotNull("maintenance page does not contain document description", docDescription);
113                    
114                    setDefaultTestInputValues();
115                    for(Entry<String,String> entry : requiredFields.entrySet()) {
116                            HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
117                    }
118                    docDescription.setValueAttribute("testing submission");
119                    // use a non-existent chart
120                    HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "chartOfAccountsCode","BP");
121    
122                    HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
123                    assertTrue("page should contain active account existence error", resultPage.asText().contains("No active chart exists for this code"));
124            }
125            
126            @Test
127            public void testValidChart() throws Exception {
128                    /**
129                     * 
130                     * Also tests chart consistency with valid account
131                     * 
132                     */
133                    HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
134                    assertNotNull("maintenance page is null", maintPage);
135                    
136                    HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
137                    assertNotNull("maintenance page does not contain document description", docDescription);
138                    
139                    setDefaultTestInputValues();
140                    for(Entry<String,String> entry : requiredFields.entrySet()) {
141                            HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
142                    }
143                    docDescription.setValueAttribute("testing submission");
144    
145                    HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
146    
147                    assertTrue("page should not contain errors", !resultPage.asText().contains("error(s)"));
148                    
149                    Map<String,String> keys = new HashMap<String,String>();
150                    keys.put("chartOfAccountsCode", "UA");
151                    keys.put("accountNumber", "3333");
152                    keys.put("subAccountNumber", "4444");
153                    
154                    SubAccount subAccount = (SubAccount) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(SubAccount.class, keys);
155                    assertNotNull("newly created sub-object code should exist", subAccount);
156                    //clean up after assertion.
157                    KRADServiceLocator.getBusinessObjectService().delete(subAccount);
158            }
159            
160            @Test
161            public void testInvalidAccount() throws Exception {
162    
163                    HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
164                    assertNotNull("maintenance page is null", maintPage);
165                    
166                    HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
167                    assertNotNull("maintenance page does not contain document description", docDescription);
168                    
169                    setDefaultTestInputValues();
170                    for(Entry<String,String> entry : requiredFields.entrySet()) {
171                            HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
172                    }
173                    docDescription.setValueAttribute("testing submission");
174                    
175                    HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "accountNumber", "9999");
176                    
177                    HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
178                    assertTrue("page should contain active account existence error", resultPage.asText().contains("No such active account exists whose chart matches 'UA'"));
179            }
180            
181            @Override
182            public void setUp() throws Exception {
183                    super.setUp();
184                    before();
185            }
186            
187            @Override
188            public void tearDown() throws Exception {
189                    after();
190                    super.tearDown();
191            }
192    }