001    /**
002     * Copyright 2004-2013 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.Account;
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 AccountMaintTest 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    
044                    requiredFields.put("accountEffectiveDate", "01/01/2010");
045                    requiredFields.put("chartOfAccountsCode", "UA");
046                    requiredFields.put("accountNumber", "4444");
047                    requiredFields.put("accountName", "4444");
048                    requiredFields.put("organizationCode", "ORG-CODE");
049            }
050            
051            private void before() {
052                    
053                    newUrl = HrTestConstants.Urls.ACCOUNT_MAINT_NEW_URL;
054                    lookupUrl = HrTestConstants.Urls.ACCOUNT_MAINT_URL;
055                    
056                    requiredFields = new HashMap<String,String>();
057                    requiredFields.put("accountEffectiveDate", "Account Effective Date (EffDate) is a required field.");
058                    requiredFields.put("chartOfAccountsCode", "Chart Code (Chart) is a required field.");
059                    requiredFields.put("accountNumber", "Account Number (Account Number) is a required field.");
060                    requiredFields.put("accountName", "Account Name (AcctName) is a required field.");
061                    requiredFields.put("organizationCode", "Organization Code (Org) is a required field.");
062            }
063            
064            private void after() {
065                    requiredFields.clear();
066            }
067            
068            @Test
069            public void testRequiredFields() throws Exception {
070                    HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
071                    assertNotNull("maintenance page is null", maintPage);
072                    
073                    HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
074                    assertNotNull("maintenance page does not contain document description", docDescription);
075                    
076                    docDescription.setValueAttribute("testing submission");
077                    
078                    HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
079                    assertNotNull("no result page returned after submit", resultPage);
080                    
081                    String resultPageAsText = resultPage.asText();
082                    for(Entry<String,String> requiredField : requiredFields.entrySet()) {
083                            assertTrue("page does not contain error message for required field: '" + requiredField.getKey() + "'",
084                                            resultPageAsText.contains(requiredField.getValue()));
085                    }
086            }
087            
088            @Test
089            public void testLookup() throws Exception {
090                    HtmlPage lookupPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), lookupUrl);
091                    assertNotNull("lookup page is null", lookupPage);
092                    
093                    lookupPage = HtmlUnitUtil.clickInputContainingText(lookupPage, "search");
094                    assertNotNull("lookup result page is null", lookupPage);
095                    
096                    assertTrue("lookup result page should contain one account '3333'",
097                                    lookupPage.asText().contains("3333"));
098            }
099            
100            @Test
101            public void testInValidChart() throws Exception {
102                    /**
103                     * TODO: submit sub-object code whose object COA and account COA codes
104                     * match the COA specified on this sub-object, but the account is open.
105                     * 
106                     * This test was changed from asserting a successful submission to asserting a non-successful
107                     * insertion. Test data was added that marked the account used in this test as closed. Validation
108                     * fails for closed accounts.
109                     * 
110                     */
111                    HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
112                    assertNotNull("maintenance page is null", maintPage);
113                    
114                    HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
115                    assertNotNull("maintenance page does not contain document description", docDescription);
116                    
117                    setDefaultTestInputValues();
118                    for(Entry<String,String> entry : requiredFields.entrySet()) {
119                            HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
120                    }
121                    docDescription.setValueAttribute("testing submission");
122                    // use a non-existent chart
123                    HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "chartOfAccountsCode","BP");
124    
125                    HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
126                    assertTrue("page should contain active account existence error", resultPage.asText().contains("No active chart exists for this code"));
127            }
128            
129            @Test
130            public void testValidChart() throws Exception {
131    
132                    HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
133                    assertNotNull("maintenance page is null", maintPage);
134                    
135                    HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
136                    assertNotNull("maintenance page does not contain document description", docDescription);
137                    
138                    setDefaultTestInputValues();
139                    for(Entry<String,String> entry : requiredFields.entrySet()) {
140                            HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
141                    }
142                    docDescription.setValueAttribute("testing submission");
143    
144                    HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
145                    assertTrue("page should contain active account existence error", !resultPage.asText().contains("error(s)"));
146                    
147                    Map<String,String> keys = new HashMap<String,String>();
148                    keys.put("chartOfAccountsCode", "UA");
149                    keys.put("accountNumber", "4444");
150                    
151                    Account account = (Account) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(Account.class, keys);
152                    assertNotNull("newly created sub-object code should exist", account);
153                    //clean up after assertion.
154                    KRADServiceLocator.getBusinessObjectService().delete(account);
155            }
156            
157            @Override
158            public void setUp() throws Exception {
159                    before();
160                    super.setUp();
161            }
162            
163            @Override
164            public void tearDown() throws Exception {
165                    after();
166                    super.tearDown();
167            }
168    }