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