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.pm.positionreportgroup;
017
018import java.util.List;
019
020import junit.framework.Assert;
021
022import org.apache.commons.collections.CollectionUtils;
023import org.joda.time.DateTime;
024import org.junit.Test;
025import org.kuali.hr.KPMEWebTestCase;
026import org.kuali.hr.util.HtmlUnitUtil;
027import org.kuali.kpme.core.FunctionalTest;
028import org.kuali.kpme.core.util.TKUtils;
029import org.kuali.kpme.pm.positionreportgroup.PositionReportGroup;
030import org.kuali.kpme.pm.service.base.PmServiceLocator;
031import org.kuali.kpme.pm.utils.PmTestConstants;
032
033import com.gargoylesoftware.htmlunit.html.HtmlElement;
034import com.gargoylesoftware.htmlunit.html.HtmlForm;
035import com.gargoylesoftware.htmlunit.html.HtmlInput;
036import com.gargoylesoftware.htmlunit.html.HtmlPage;
037
038@FunctionalTest
039public class PositionReportGroupMaintTest extends KPMEWebTestCase {
040        
041        @Test
042        public void testRequiredFields() throws Exception {
043                String baseUrl = PmTestConstants.Urls.POSITION_REPORT_GROUP_MAINT_NEW_URL;
044                HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), baseUrl);
045                Assert.assertNotNull(page);
046         
047                HtmlForm form = page.getFormByName("KualiForm");
048                Assert.assertNotNull("Search form was missing from page.", form);
049                
050                HtmlInput  input  = HtmlUnitUtil.getInputContainingText(form, "methodToCall.route");
051                Assert.assertNotNull("Could not locate submit button", input);
052                
053                HtmlElement element = page.getElementByName("methodToCall.route");
054                page = element.click();
055                Assert.assertTrue("page text does not contain:\n" + "Effective Date (Effective Date) is a required field.", 
056                                page.asText().contains("Effective Date (Effective Date) is a required field."));
057                Assert.assertTrue("page text does not contain:\n" + "Position Report Group (Position Report Group) is a required field.", 
058                                page.asText().contains("Position Report Group (Position Report Group) is a required field."));
059                Assert.assertTrue("page text does not contain:\n" + "Institution (Institution) is a required field.",
060                                page.asText().contains("Institution (Institution) is a required field."));
061                Assert.assertTrue("page text does not contain:\n" + "Location (Location) is a required field.", 
062                                page.asText().contains("Location (Location) is a required field."));
063        }
064        
065        @Test
066        public void testAddNew() throws Exception {
067                DateTime effectiveDate =  new DateTime(2012, 4, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone());
068                String prgString = "testPRG";
069                List<PositionReportGroup> prgList = PmServiceLocator.getPositionReportGroupService().getPositionReportGroupList(prgString, "testInst", "TS", effectiveDate.toLocalDate());
070                Assert.assertTrue("There should NOT be Position Report Group with name " + prgString, CollectionUtils.isEmpty(prgList));
071                
072                String baseUrl = PmTestConstants.Urls.POSITION_REPORT_GROUP_MAINT_NEW_URL;
073                HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), baseUrl);
074                Assert.assertNotNull(page);
075         
076                HtmlForm form = page.getFormByName("KualiForm");
077                Assert.assertNotNull("Search form was missing from page.", form);
078                
079                HtmlUnitUtil.setFieldValue(page, "document.documentHeader.documentDescription", "Position Report Group - test");
080            HtmlUnitUtil.setFieldValue(page, "document.newMaintainableObject.effectiveDate", "04/01/2012");
081            HtmlUnitUtil.setFieldValue(page, "document.newMaintainableObject.positionReportGroup", prgString);
082            HtmlUnitUtil.setFieldValue(page, "document.newMaintainableObject.institution", "nonExistInst");     //nonexisting institution
083            HtmlUnitUtil.setFieldValue(page, "document.newMaintainableObject.location", "nonCam");      //nonexisting location
084                
085                HtmlInput  input  = HtmlUnitUtil.getInputContainingText(form, "methodToCall.route");
086                Assert.assertNotNull("Could not locate submit button", input);
087                HtmlElement element = page.getElementByName("methodToCall.route");
088                page = element.click();
089                HtmlUnitUtil.createTempFile(page);
090                Assert.assertTrue("page text contains:\n" + "The specified Instituion 'nonExistInst' does not exist.", 
091                                page.asText().contains("The specified Instituion 'nonExistInst' does not exist."));
092                Assert.assertTrue("page text contains:\n" + "The specified Location 'nonCam' does not exist.", 
093                                page.asText().contains("The specified Location 'nonCam' does not exist."));
094                        
095                HtmlUnitUtil.setFieldValue(page, "document.newMaintainableObject.institution", "testInst"); // existing institution
096                element = page.getElementByName("methodToCall.route");
097                page = element.click();
098                Assert.assertFalse("page text contains:\n" + "The specified Instituion 'testInst' does not exist.", 
099                                page.asText().contains("The specified Instituion 'testInst' does not exist."));
100                
101                HtmlUnitUtil.setFieldValue(page, "document.newMaintainableObject.location", "BL"); // existing location
102                element = page.getElementByName("methodToCall.route");
103                page = element.click();
104                Assert.assertFalse("page text contains error", page.asText().contains("error"));
105                
106                prgList = PmServiceLocator.getPositionReportGroupService().getPositionReportGroupList(prgString, "testInst", "BL", effectiveDate.toLocalDate());
107                Assert.assertTrue("There should be Position Report Group with name " + prgString, CollectionUtils.isNotEmpty(prgList));
108                
109        }
110}