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.Ignore;
026import org.junit.Test;
027import org.kuali.hr.KPMEWebTestCase;
028import org.kuali.hr.util.HtmlUnitUtil;
029import org.kuali.kpme.core.kfs.coa.businessobject.Organization;
030import org.kuali.kpme.core.kfs.coa.businessobject.ProjectCode;
031import org.kuali.kpme.core.util.HrTestConstants;
032import org.kuali.rice.krad.service.KRADServiceLocator;
033
034import com.gargoylesoftware.htmlunit.html.HtmlInput;
035import com.gargoylesoftware.htmlunit.html.HtmlPage;
036import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
037
038public class ProjectCodeMaintTest extends KPMEWebTestCase {
039        
040        private static final String NEW_MAINT_DOC_PREFIX = "document.newMaintainableObject.";
041        private String newUrl;
042        private String lookupUrl;
043        private Map<String,String> requiredFields;
044
045        private void before() {
046                
047                newUrl = HrTestConstants.Urls.PROJECT_CODE_MAINT_NEW_URL;
048                lookupUrl = HrTestConstants.Urls.PROJECT_CODE_MAINT_URL;
049                
050                requiredFields = new HashMap<String,String>();
051                requiredFields.put("code", "Project Code (Project) is a required field.");
052                requiredFields.put("name", "Project Name (Name) is a required field.");
053                requiredFields.put("chartOfAccountsCode", "Chart Code (Chart) is a required field.");
054                requiredFields.put("organizationCode", "Organization Code (Org) is a required field.");
055        }
056        
057        private void setDefaultTestInputValues() {
058                requiredFields = new HashMap<String,String>();
059                requiredFields.put("active", "on");
060                requiredFields.put("chartOfAccountsCode", "UA");
061                requiredFields.put("code", "PRJ-CODE");
062                requiredFields.put("name", "PRJ-NAME");
063                requiredFields.put("organizationCode", "ORG-CODE");
064        }
065        
066        private void after() {
067                requiredFields.clear();
068        }
069        
070        @Test
071        public void testRequiredFields() throws Exception {
072                HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
073                assertNotNull("maintenance page is null", maintPage);
074                
075                HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
076                assertNotNull("maintenance page does not contain document description", docDescription);
077                
078                docDescription.setValueAttribute("testing submission");
079                
080                HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
081                assertNotNull("no result page returned after submit", resultPage);
082                
083                String resultPageAsText = resultPage.asText();
084                for(Entry<String,String> requiredField : requiredFields.entrySet()) {
085                        assertTrue("page does not contain error message for required field: '" + requiredField.getKey() + "'",
086                                        resultPageAsText.contains(requiredField.getValue()));
087                }
088        }
089        
090        @Test
091        public void testLookup() throws Exception {
092                HtmlPage lookupPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), lookupUrl);
093                assertNotNull("lookup page is null", lookupPage);
094                
095                lookupPage = HtmlUnitUtil.clickInputContainingText(lookupPage, "search");
096                assertNotNull("lookup result page is null", lookupPage);
097                
098                assertTrue("lookup result should not contain any project codes", lookupPage.asText().contains("No values match this search. "));
099        }
100        
101        @Override
102        public void setUp() throws Exception {
103                super.setUp();
104                before();
105        }
106
107        @Override
108        public void tearDown() throws Exception {
109                after();
110                super.tearDown();
111        }
112        
113        @Test
114        public void testInvalidChartConsistencyCaseTwo() throws Exception {
115                /**
116                 * TODO: submit project code whose specified organization's chart does not match the chart defined
117                 * on this project code
118                 */
119                HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
120                assertNotNull("maintenance page is null", maintPage);
121                
122                HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
123                assertNotNull("maintenance page does not contain document description", docDescription);
124
125                setDefaultTestInputValues();
126                for(Entry<String,String> entry : requiredFields.entrySet()) {
127                        HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
128                }
129
130                docDescription.setValueAttribute("testing submission");
131                //HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "accountNumber", "1111");
132
133                HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "organizationCode", "ORG-CODE2");
134                // reset requiredFields map to default error messages.
135                before();
136                HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
137                assertNotNull("no result page returned after submit", resultPage);
138                
139                String resultPageAsText = resultPage.asText();
140                for(Entry<String,String> requiredField : requiredFields.entrySet()) {
141                        if(requiredField.getKey().equals("organizationCode")) {
142                                assertTrue("page does not contain error message for the invalid field: '" + requiredField.getKey() + "'",
143                                                resultPageAsText.contains("No such active organization exists whose chart matches 'UA'"));
144                        }
145                }
146        }
147        
148        @Test
149        public void testInValidChart() throws Exception {
150                /**
151                 * TODO: submit sub-object code whose object COA and account COA codes
152                 * match the COA specified on this sub-object, but the account is open.
153                 * 
154                 * This test was changed from asserting a successful submission to asserting a non-successful
155                 * insertion. Test data was added that marked the account used in this test as closed. Validation
156                 * fails for closed accounts.
157                 * 
158                 */
159                HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
160                assertNotNull("maintenance page is null", maintPage);
161                
162                HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
163                assertNotNull("maintenance page does not contain document description", docDescription);
164                
165                setDefaultTestInputValues();
166                for(Entry<String,String> entry : requiredFields.entrySet()) {
167                        HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
168                }
169                docDescription.setValueAttribute("testing submission");
170                // use a non-existent chart
171                HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + "chartOfAccountsCode","BP");
172
173                HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
174                assertTrue("page should contain active chart existence error", resultPage.asText().contains("No active chart exists for this code"));
175        }
176        
177        @Test
178        public void testValidChart() throws Exception {
179
180                HtmlPage maintPage = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), newUrl);
181                assertNotNull("maintenance page is null", maintPage);
182                
183                HtmlInput docDescription = HtmlUnitUtil.getInputContainingText(maintPage, "* Document Description");
184                assertNotNull("maintenance page does not contain document description", docDescription);
185                
186                setDefaultTestInputValues();
187                for(Entry<String,String> entry : requiredFields.entrySet()) {
188                        HtmlUnitUtil.setFieldValue(maintPage, NEW_MAINT_DOC_PREFIX + entry.getKey(), entry.getValue());
189                }
190                docDescription.setValueAttribute("testing submission");
191
192                HtmlPage resultPage = HtmlUnitUtil.clickInputContainingText(maintPage, "submit");
193
194                assertTrue("page should contain active account existence error", !resultPage.asText().contains("error(s)"));
195                
196                ProjectCode projectCode = (ProjectCode) KRADServiceLocatorWeb.getLegacyDataAdapter().findBySinglePrimaryKey(ProjectCode.class, "PRJ-CODE");
197                assertNotNull("newly created sub-object code should exist", projectCode);
198                //clean up after assertion.
199                KRADServiceLocatorWeb.getLegacyDataAdapter().delete(projectCode);
200        }
201}