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.earncode.service;
017    
018    import java.sql.Date;
019    import java.util.List;
020    import java.util.Map;
021    
022    import org.apache.log4j.Logger;
023    import org.junit.Assert;
024    import org.junit.Before;
025    import org.junit.Test;
026    import org.kuali.hr.test.KPMETestCase;
027    import org.kuali.hr.time.assignment.Assignment;
028    import org.kuali.hr.time.earncode.EarnCode;
029    import org.kuali.hr.time.service.base.TkServiceLocator;
030    import org.kuali.hr.time.test.HtmlUnitUtil;
031    import org.kuali.hr.time.test.TkTestConstants;
032    import org.kuali.hr.time.util.TKUtils;
033    
034    import com.gargoylesoftware.htmlunit.html.HtmlInput;
035    import com.gargoylesoftware.htmlunit.html.HtmlPage;
036    import com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput;
037    import com.gargoylesoftware.htmlunit.html.HtmlSelect;
038    import org.kuali.rice.kim.api.identity.principal.Principal;
039    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
040    
041    public class EarnCodeServiceImplTest extends KPMETestCase {
042    
043    
044            public static final String TEST_USER = "admin";
045            public static final Long TEST_ASSIGNMENT_JOB_NUMBER = 1L;
046            public static final Long TEST_ASSIGNMENT_JOB_NUMBER_2 = 2L;
047            public static final Long TEST_ASSIGNMENT_JOB_NUMBER_3 = 3L;
048            public static final Long TEST_ASSIGNMENT_JOB_NUMBER_4 = 4L;
049    
050            @SuppressWarnings("unused")
051            private static final Logger LOG = Logger.getLogger(EarnCodeServiceImplTest.class);
052    
053            EarnCodeService earnCodeService = null;
054    
055    
056            @Before
057            public void setUp() throws Exception {
058                    super.setUp();
059                    earnCodeService=TkServiceLocator.getEarnCodeService();
060            }
061    
062            @Test
063            public void getEarnCodes() throws Exception {
064            Date asOfDate = TKUtils.getTimelessDate(null);
065                    List<Assignment> assignments = TkServiceLocator.getAssignmentService().getAssignments(TEST_USER, asOfDate);
066                    Assert.assertNotNull(assignments);
067                    Assert.assertTrue("Emtpy assignment list", !assignments.isEmpty());
068    
069                    Assignment assignment1 = null;
070                    Assignment assignment2 = null;
071                    Assignment assignment3 = null;
072                    Assignment assignment4 = null;
073                    for (Assignment a : assignments) {
074                            if (a.getJobNumber().equals(TEST_ASSIGNMENT_JOB_NUMBER)) {
075                                    assignment1 = a;
076                            } else if (a.getJobNumber().equals(TEST_ASSIGNMENT_JOB_NUMBER_2)) {
077                                    assignment2 = a;
078                            } else if (a.getJobNumber().equals(TEST_ASSIGNMENT_JOB_NUMBER_3)) {
079                                    assignment3 = a;
080                            } else if (a.getJobNumber().equals(TEST_ASSIGNMENT_JOB_NUMBER_4)) {
081                                    assignment4 = a;
082                            }
083                    }
084    
085                    // one for each test scenario involving wildcards at least...
086                    Assert.assertNotNull("Test assignment not found.", assignment1);
087                    Assert.assertNotNull("Test assignment not found.", assignment2);
088                    Assert.assertNotNull("Test assignment not found.", assignment3);
089                    Assert.assertNotNull("Test assignment not found.", assignment4);
090    
091            // Testing standard lookup.
092                    List<EarnCode> earnCodes = earnCodeService.getEarnCodes(assignment1, asOfDate);
093                    Assert.assertEquals("Wrong number of earn codes returned.", 8, earnCodes.size());
094    
095                    // Wildcard on SalGroup
096                    earnCodes = earnCodeService.getEarnCodes(assignment2, asOfDate);
097                    Assert.assertEquals("Wrong number of earn codes returned.", 3, earnCodes.size());
098    
099                    // Dual Wildcards
100                    earnCodes = earnCodeService.getEarnCodes(assignment3, asOfDate);
101                    Assert.assertEquals("Wrong number of earn codes returned.",2, earnCodes.size());
102            }
103    
104            @Test
105            public void testEarnCodeMaintenancePage() throws Exception{
106    
107                    HtmlPage earnCodeLookUp = HtmlUnitUtil.gotoPageAndLogin(TkTestConstants.Urls.EARN_CODE_MAINT_URL);
108                    earnCodeLookUp = HtmlUnitUtil.clickInputContainingText(earnCodeLookUp, "search");
109                    Assert.assertTrue("Page contains SDR entry", earnCodeLookUp.asText().contains("SDR"));
110                    HtmlPage maintPage = HtmlUnitUtil.clickAnchorContainingText(earnCodeLookUp, "edit","1");
111    
112                    //Sai - confirm that the error is throw by not selecting a record type
113                    HtmlSelect inputText= maintPage.getHtmlElementById("document.newMaintainableObject.recordMethod");
114                    inputText.setDefaultValue("H");
115    
116                    HtmlInput inputForDescription = HtmlUnitUtil.getInputContainingText(
117                                    maintPage, "* Document Description");
118                    inputForDescription.setValueAttribute("Description");
119    //              setFieldValue(maintPage, "document.newMaintainableObject.fractionalTimeAllowed", "99");
120    //              setFieldValue(maintPage, "document.newMaintainableObject.roundingOption", "T");
121    
122                    HtmlPage resultantPageAfterEdit = HtmlUnitUtil
123                                    .clickInputContainingText(maintPage, "submit");
124                    System.out.println(resultantPageAfterEdit.asText());
125    
126    //              assertTrue("Error message for not selecting any record type",
127    //                              resultantPageAfterEdit.asText().contains("For this earn code you must specify Record Hours or Record Time or Record Amount"));
128    
129                    //Sai - confirm that the error is thrown if more than one record type is selected
130    //              checkBox  = maintPage.getHtmlElementById("document.newMaintainableObject.recordTime");
131    //              checkBox.setChecked(true);
132    //              checkBox  = maintPage.getHtmlElementById("document.newMaintainableObject.recordHours");
133    //              checkBox.setChecked(true);
134    
135                    inputForDescription = HtmlUnitUtil.getInputContainingText(
136                                    maintPage, "* Document Description");
137                    inputForDescription.setValueAttribute("Description");
138                    resultantPageAfterEdit = HtmlUnitUtil
139                                    .clickInputContainingText(maintPage, "submit");
140    //              assertTrue("Error message for selecting more than one record type",
141    //                              resultantPageAfterEdit.asText().contains("For this earn code you can only specify one of the Record types"));
142            }
143    
144    }