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.assertNotNull;
019import static org.junit.Assert.assertTrue;
020
021import java.util.HashMap;
022import java.util.Map;
023import java.util.Map.Entry;
024
025import org.junit.Test;
026import org.kuali.hr.KPMEWebTestCase;
027import org.kuali.hr.util.HtmlUnitUtil;
028import org.kuali.kpme.core.kfs.coa.businessobject.ObjectCode;
029import org.kuali.kpme.core.util.HrTestConstants;
030import org.kuali.rice.krad.service.KRADServiceLocator;
031
032import com.gargoylesoftware.htmlunit.html.HtmlInput;
033import com.gargoylesoftware.htmlunit.html.HtmlPage;
034import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
035
036public class ObjectCodeMaintTest extends KPMEWebTestCase {
037        
038        private static final String NEW_MAINT_DOC_PREFIX = "document.newMaintainableObject.";
039        private String newUrl;
040        private String lookupUrl;
041        private Map<String,String> requiredFields;
042
043        @Override
044        public void setUp() throws Exception {
045                super.setUp();
046                before();
047        }
048
049        @Override
050        public void tearDown() throws Exception {
051                after();
052                super.tearDown();
053        }
054        
055        private void setDefaultTestInputValues() {
056                requiredFields = new HashMap<String,String>();
057                requiredFields.put("active", "on");
058                requiredFields.put("universityFiscalYear", "2013");
059                requiredFields.put("chartOfAccountsCode", "UA");
060                requiredFields.put("financialObjectCode", "3000");
061                requiredFields.put("financialObjectCodeName", "Regular Earnings");
062                requiredFields.put("financialObjectCodeShortName", "Reg Earns");
063        }
064        
065        private void before() {
066                
067                newUrl = HrTestConstants.Urls.OBJECT_CODE_MAINT_NEW_URL;
068                lookupUrl = HrTestConstants.Urls.OBJECT_CODE_MAINT_URL;
069                
070                requiredFields = new HashMap<String,String>();
071                requiredFields.put("universityFiscalYear", "Fiscal Year (FY) is a required field.");
072                requiredFields.put("chartOfAccountsCode", "Chart of Accounts Code (ChartOfAccountsCode) is a required field.");
073                requiredFields.put("financialObjectCode", "Object Code (Object) is a required field.");
074                requiredFields.put("financialObjectCodeName", "Object Code Name (ObjCodeName) is a required field.");
075                requiredFields.put("financialObjectCodeShortName", "Object Code Short Name (ObjCodeShortName) is a required field.");
076        }
077        
078        private void after() {
079                requiredFields.clear();
080        }
081        
082        @Test
083        public void testRequiredFields() throws Exception {
084                HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
085                assertNotNull("maintenance page is null", maintPage);
086                
087                HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
088                assertNotNull("maintenance page does not contain document description", docDescription);
089                
090                docDescription.setValueAttribute("testing submission");
091                
092                HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
093                assertNotNull("no result page returned after submit", resultPage);
094                
095                String resultPageAsText = resultPage.asText();
096                for(Entry<String,String> requiredField : requiredFields.entrySet()) {
097                        assertTrue("page does not contain error message for required field: '" + requiredField.getKey() + "'",
098                                        resultPageAsText.contains(requiredField.getValue()));
099                }
100        }
101        
102        @Test
103        public void testLookup() throws Exception {
104                HtmlPage lookupPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), lookupUrl);
105                assertNotNull("lookup page is null", lookupPage);
106                
107                HtmlUnitUtil.setFieldValue(lookupPage, "universityFiscalYear", "2013");
108                
109                lookupPage = HtmlUnitUtil.clickInputContainingText(lookupPage, "search");
110                assertNotNull("lookup result page is null", lookupPage);
111                
112                String lookupPageAsText = lookupPage.asText();
113                assertTrue("lookup result page should contain three records",
114                                lookupPageAsText.contains("9000") && lookupPageAsText.contains("1000") && lookupPageAsText.contains("2000"));
115                
116        }
117        
118        @Test
119        public void testInValidChart() throws Exception {
120                /**
121                 * TODO: submit sub-object code whose object COA and account COA codes
122                 * match the COA specified on this sub-object, but the account is open.
123                 * 
124                 * This test was changed from asserting a successful submission to asserting a non-successful
125                 * insertion. Test data was added that marked the account used in this test as closed. Validation
126                 * fails for closed accounts.
127                 * 
128                 */
129                HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
130                assertNotNull("maintenance page is null", maintPage);
131                
132                HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
133                assertNotNull("maintenance page does not contain document description", docDescription);
134                
135                setDefaultTestInputValues();
136                for(Entry<String,String> entry : requiredFields.entrySet()) {
137                        HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
138                }
139                docDescription.setValueAttribute("testing submission");
140                // use a non-existent chart
141                HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "chartOfAccountsCode","BP");
142
143                HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
144
145                assertTrue("page should contain active chart existence error", resultPage.asText().contains("No active chart exists for this code"));
146        }
147        
148        @Test
149        public void testValidChart() throws Exception {
150
151                HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
152                assertNotNull("maintenance page is null", maintPage);
153                
154                HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
155                assertNotNull("maintenance page does not contain document description", docDescription);
156                
157                setDefaultTestInputValues();
158                for(Entry<String,String> entry : requiredFields.entrySet()) {
159                        HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
160                }
161                docDescription.setValueAttribute("testing submission");
162
163                HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
164                assertTrue("page should contain active account existence error", !resultPage.asText().contains("error(s)"));
165                
166                Map<String,String> keys = new HashMap<String,String>();
167                keys.put("chartOfAccountsCode", "UA");
168                keys.put("universityFiscalYear", "2013");
169                keys.put("financialObjectCode", "3000");
170                
171                ObjectCode objectCode = (ObjectCode) KRADServiceLocatorWeb.getLegacyDataAdapter().findByPrimaryKey(ObjectCode.class, keys);
172                assertNotNull("newly created sub-object code should exist", objectCode);
173                //clean up after assertion.
174                KRADServiceLocatorWeb.getLegacyDataAdapter().delete(objectCode);
175        }
176
177}