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