001    /**
002     * Copyright 2004-2012 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     */
016    package org.kuali.hr.time.workarea;
017    
018    import java.sql.Date;
019    
020    import org.joda.time.DateTime;
021    import org.joda.time.DateTimeZone;
022    import org.junit.Assert;
023    import org.junit.Before;
024    import org.junit.Test;
025    import org.kuali.hr.test.KPMETestCase;
026    import org.kuali.hr.time.department.Department;
027    import org.kuali.hr.time.service.base.TkServiceLocator;
028    import org.kuali.hr.time.test.HtmlUnitUtil;
029    import org.kuali.hr.time.test.TkTestConstants;
030    import org.kuali.hr.time.util.TKUtils;
031    import org.kuali.hr.time.util.TkConstants;
032    import org.kuali.rice.krad.service.KRADServiceLocator;
033    
034    import com.gargoylesoftware.htmlunit.html.HtmlElement;
035    import com.gargoylesoftware.htmlunit.html.HtmlForm;
036    import com.gargoylesoftware.htmlunit.html.HtmlInput;
037    import com.gargoylesoftware.htmlunit.html.HtmlPage;
038    
039    public class WorkAreaMaintenanceDocumentTest extends KPMETestCase {
040            
041            final String ERROR_MESSAGE = "At least one active role must be defined.";
042            final String ERROR_ROLE_MESSAGE = "Cannot define both Principal Id and Position Number for Role.";
043            final String SUCCESS_MESSAGE = "Document was successfully submitted.";
044            private static final String TEST_CODE_DEPARTMENT_VALID = "_TEST";
045            private static final Date TEST_DATE = new Date((new DateTime(2011,1,1,1,0,0,0,TKUtils.getSystemDateTimeZone())).getMillis());
046    
047        @Before
048        public void setUp() throws Exception {
049            super.setUp();
050            Department department = new Department();
051                    department.setDept(TEST_CODE_DEPARTMENT_VALID);
052                    department.setChart(TEST_CODE_DEPARTMENT_VALID);
053                    department.setDescription(TEST_CODE_DEPARTMENT_VALID);
054                    department.setOrg(TEST_CODE_DEPARTMENT_VALID);
055                    department.setLocation("TST");
056                    department.setEffectiveDate(TEST_DATE);
057                    department.setActive(true);
058                    KRADServiceLocator.getBusinessObjectService().save(department);
059        }
060        
061    
062        @Test
063        public void testCreateNew() throws Exception {
064            String baseUrl = TkTestConstants.Urls.WORK_AREA_MAINT_NEW_URL;
065            Long workArea = this.maxWorkArea()+1;
066            HtmlPage page = HtmlUnitUtil.gotoPageAndLogin(baseUrl);
067            Assert.assertNotNull(page);
068       
069            HtmlForm form = page.getFormByName("KualiForm");
070            Assert.assertNotNull("Search form was missing from page.", form);
071            
072            HtmlInput  input  = HtmlUnitUtil.getInputContainingText(form, "methodToCall.route");
073            Assert.assertNotNull("Could not locate submit button", input);
074            
075            setFieldValue(page, "document.documentHeader.documentDescription", "Work Area - test");
076            setFieldValue(page, "document.newMaintainableObject.effectiveDate", "4/01/2011");
077            setFieldValue(page, "document.newMaintainableObject.active", "on");
078            setFieldValue(page, "document.newMaintainableObject.description", "test");
079            setFieldValue(page, "document.newMaintainableObject.dept", TEST_CODE_DEPARTMENT_VALID);
080            setFieldValue(page, "document.newMaintainableObject.adminDescr", "TEST");
081            setFieldValue(page, "document.newMaintainableObject.overtimeEditRole", "TK_APPROVER");
082            
083            HtmlElement element = page.getElementByName("methodToCall.route");
084            HtmlPage nextPage = element.click();
085            Assert.assertTrue("page does not contain:\n" + ERROR_MESSAGE, nextPage.asText().contains(ERROR_MESSAGE));
086            
087            setFieldValue(page, "document.newMaintainableObject.add.roles.effectiveDate", "04/01/2011");
088            setFieldValue(page, "document.newMaintainableObject.add.roles.principalId", "admin");
089            setFieldValue(page, "document.newMaintainableObject.add.roles.positionNumber", "123");
090            setFieldValue(page, "document.newMaintainableObject.add.roles.active", "on");
091    
092            element = HtmlUnitUtil.getInputContainingText(page,"methodToCall.addLine.roles");
093            nextPage = element.click();
094            Assert.assertTrue("page does not contain:\n" + ERROR_ROLE_MESSAGE, nextPage.asText().contains(ERROR_ROLE_MESSAGE));
095            
096            setFieldValue(nextPage, "document.newMaintainableObject.add.roles.positionNumber", "");
097            element = HtmlUnitUtil.getInputContainingText(nextPage,"methodToCall.addLine.roles");
098            nextPage = element.click();
099            Assert.assertFalse("page contains:\n" + ERROR_MESSAGE, nextPage.asText().contains(ERROR_MESSAGE));
100            Assert.assertFalse("page contains:\n" + ERROR_ROLE_MESSAGE, nextPage.asText().contains(ERROR_ROLE_MESSAGE));
101                    
102            form = nextPage.getFormByName("KualiForm");
103            Assert.assertNotNull("Search form was missing from page.", form);
104            
105            input  = HtmlUnitUtil.getInputContainingText(form, "methodToCall.route");
106            Assert.assertNotNull("Could not locate submit button", input);
107            //work area should be saved successfully and work area field should be populated with the max work area from db
108            element = nextPage.getElementByName("methodToCall.route");
109            HtmlPage lastPage = element.click();
110            Assert.assertFalse("page contains:\n" + ERROR_MESSAGE, lastPage.asText().contains(ERROR_MESSAGE));
111            Assert.assertTrue("page does not contains:\n" + SUCCESS_MESSAGE, lastPage.asText().contains(SUCCESS_MESSAGE));
112            Assert.assertTrue("page does not contains:\n" + workArea.toString(), lastPage.asText().contains(workArea.toString()));
113            
114            // search page should find the new work area
115            HtmlPage searchPage = HtmlUnitUtil.gotoPageAndLogin(TkTestConstants.Urls.WORK_AREA_MAINT_URL);
116            searchPage = HtmlUnitUtil.clickInputContainingText(searchPage, "search");
117            Assert.assertTrue("Page contains test Earn Code", searchPage.asText().contains(workArea.toString()));
118                    
119                    java.sql.Date aDate = new Date((new DateTime(2011, 5, 1, 0, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
120                    WorkArea wa = TkServiceLocator.getWorkAreaService().getWorkArea(workArea, aDate);
121                    String workAreaId = wa.getTkWorkAreaId().toString();
122                    
123                    // when open the new work area, role should be show up
124                    HtmlPage maintPage = HtmlUnitUtil.clickAnchorContainingText(searchPage, "edit", workAreaId);
125                    HtmlElement e = maintPage.getHtmlElementById("document.oldMaintainableObject.roles[0].effectiveDate.div");
126                    Assert.assertNotNull("Maintenance Page does not contain role ", e);
127        }
128        
129            public Long maxWorkArea() {
130                    return TkServiceLocator.getWorkAreaService().getNextWorkAreaKey();
131    
132            }
133            
134            @Override
135            public void tearDown() throws Exception {
136                    Department deptObj = TkServiceLocator.getDepartmentService().getDepartment(TEST_CODE_DEPARTMENT_VALID, TKUtils.getCurrentDate());
137                    KRADServiceLocator.getBusinessObjectService().delete(deptObj);
138                    super.tearDown();
139            }
140    }