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