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.Account;
029import org.kuali.kpme.core.kfs.coa.businessobject.Organization;
030import org.kuali.kpme.core.util.HrTestConstants;
031import org.kuali.rice.krad.service.KRADServiceLocator;
032
033import com.gargoylesoftware.htmlunit.html.HtmlInput;
034import com.gargoylesoftware.htmlunit.html.HtmlPage;
035
036public class OrganizationMaintTest extends KPMEWebTestCase {
037        //TODO: find constant in outside scope, or define one outside of this scope.
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("chartOfAccountsCode", "UA");
059                requiredFields.put("organizationCode", "A-ORG-CD");
060                requiredFields.put("organizationName", "ANOTHER ORGANIZATION");
061        }
062        
063        private void before() {
064                
065                newUrl = HrTestConstants.Urls.ORGANIZATION_MAINT_NEW_URL;
066                lookupUrl = HrTestConstants.Urls.ORGANIZATION_MAINT_URL;
067                
068                requiredFields = new HashMap<String,String>();
069                requiredFields.put("chartOfAccountsCode", "Chart Code (Chart) is a required field.");
070                requiredFields.put("organizationCode", "Organization Code (Org) is a required field.");
071                requiredFields.put("organizationName", "Organization Name (Org Name) is a required field.");
072        }
073        
074        private void after() {
075                requiredFields.clear();
076        }
077        
078        @Test
079        public void testRequiredFields() throws Exception {
080                HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
081                assertNotNull("maintenance page is null", maintPage);
082                
083                HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
084                assertNotNull("maintenance page does not contain document description", docDescription);
085                
086                docDescription.setValueAttribute("testing submission");
087                
088                HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
089                assertNotNull("no result page returned after submit", resultPage);
090                
091                String resultPageAsText = resultPage.asText();
092                for(Entry<String,String> requiredField : requiredFields.entrySet()) {
093                        assertTrue("page does not contain error message for required field: '" + requiredField.getKey() + "'",
094                                        resultPageAsText.contains(requiredField.getValue()));
095                }
096        }
097        
098        @Test
099        public void testLookup() throws Exception {
100                HtmlPage lookupPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), lookupUrl);
101                assertNotNull("lookup page is null", lookupPage);
102                
103                lookupPage = HtmlUnitUtil.clickInputContainingText(lookupPage, "search");
104                assertNotNull("lookup result page is null", lookupPage);
105                
106                assertTrue("lookup result page should contain one record", lookupPage.asText().contains("ORG-CODE"));
107        }
108        
109        @Test
110        public void testInValidChart() throws Exception {
111                /**
112                 * TODO: submit sub-object code whose object COA and account COA codes
113                 * match the COA specified on this sub-object, but the account is open.
114                 * 
115                 * This test was changed from asserting a successful submission to asserting a non-successful
116                 * insertion. Test data was added that marked the account used in this test as closed. Validation
117                 * fails for closed accounts.
118                 * 
119                 */
120                HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
121                assertNotNull("maintenance page is null", maintPage);
122                
123                HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
124                assertNotNull("maintenance page does not contain document description", docDescription);
125                
126                setDefaultTestInputValues();
127                for(Entry<String,String> entry : requiredFields.entrySet()) {
128                        HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
129                }
130                docDescription.setValueAttribute("testing submission");
131                // use a non-existent chart
132                HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "chartOfAccountsCode","BP");
133
134                HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
135                assertTrue("page should contain active account existence error", resultPage.asText().contains("No active chart exists for this code"));
136        }
137        
138        @Test
139        public void testValidChart() throws Exception {
140
141                HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
142                assertNotNull("maintenance page is null", maintPage);
143                
144                HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
145                assertNotNull("maintenance page does not contain document description", docDescription);
146                
147                setDefaultTestInputValues();
148                for(Entry<String,String> entry : requiredFields.entrySet()) {
149                        HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
150                }
151                docDescription.setValueAttribute("testing submission");
152
153                HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
154                assertTrue("page should contain active account existence error", !resultPage.asText().contains("error(s)"));
155                
156                Map<String,String> keys = new HashMap<String,String>();
157                keys.put("chartOfAccountsCode", "UA");
158                keys.put("organizationCode", "A-ORG-CD");
159                
160                Organization organization = (Organization) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(Organization.class, keys);
161                assertNotNull("newly created sub-object code should exist", organization);
162                //clean up after assertion.
163                KRADServiceLocator.getBusinessObjectService().delete(organization);
164        }
165
166}