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