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