001 /**
002 * Copyright 2004-2013 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 getEarnCodes* - these routines are separated among Leave and Time calendars. Run both, then run a combined routine that may not get used in practice.
092 // As the testing data gets better, the Time and Leave results should have little to no overlap, and the assertions will need to be correspondingly updated.
093 // Testing standard lookup.
094 List<EarnCode> earnCodes1t = earnCodeService.getEarnCodesForTime(assignment1, asOfDate);
095 Assert.assertEquals("Wrong number of earn codes returned.", 7, earnCodes1t.size());
096 List<EarnCode> earnCodes1l = earnCodeService.getEarnCodesForLeave(assignment1, asOfDate, false);
097 Assert.assertEquals("Wrong number of earn codes returned.", 0, earnCodes1l.size());
098 List<EarnCode> earnCodes1lt= earnCodeService.getEarnCodesForLeaveAndTime(assignment1, asOfDate, false);
099 Assert.assertEquals("Wrong number of earn codes returned.", 7, earnCodes1lt.size());
100
101 // Wildcard on SalGroup
102 List<EarnCode> earnCodes2t = earnCodeService.getEarnCodesForTime(assignment2, asOfDate);
103 Assert.assertEquals("Wrong number of earn codes returned.", 2, earnCodes2t.size());
104 List<EarnCode> earnCodes2l = earnCodeService.getEarnCodesForLeave(assignment2, asOfDate, false);
105 Assert.assertEquals("Wrong number of earn codes returned.", 0, earnCodes2l.size());
106 List<EarnCode> earnCodes2lt = earnCodeService.getEarnCodesForLeaveAndTime(assignment2, asOfDate, false);
107 Assert.assertEquals("Wrong number of earn codes returned.", 2, earnCodes2lt.size());
108
109 // Dual Wildcards
110 List<EarnCode> earnCodes3t = earnCodeService.getEarnCodesForTime(assignment3, asOfDate);
111 Assert.assertEquals("Wrong number of earn codes returned.",1, earnCodes3t.size());
112 List<EarnCode> earnCodes3l = earnCodeService.getEarnCodesForLeave(assignment3, asOfDate, false);
113 Assert.assertEquals("Wrong number of earn codes returned.",0, earnCodes3l.size());
114 List<EarnCode> earnCodes3lt = earnCodeService.getEarnCodesForLeaveAndTime(assignment3, asOfDate, false);
115 Assert.assertEquals("Wrong number of earn codes returned.",1, earnCodes3lt.size());
116 }
117
118 @Test
119 public void testEarnCodeMaintenancePage() throws Exception{
120
121 HtmlPage earnCodeLookUp = HtmlUnitUtil.gotoPageAndLogin(getWebClient(), TkTestConstants.Urls.EARN_CODE_MAINT_URL);
122 earnCodeLookUp = HtmlUnitUtil.clickInputContainingText(earnCodeLookUp, "search");
123 Assert.assertTrue("Page contains SDR entry", earnCodeLookUp.asText().contains("SDR"));
124 HtmlPage maintPage = HtmlUnitUtil.clickAnchorContainingText(earnCodeLookUp, "edit","1");
125
126 //Sai - confirm that the error is throw by not selecting a record type
127 HtmlSelect inputText= maintPage.getHtmlElementById("document.newMaintainableObject.recordMethod");
128 inputText.setDefaultValue("H");
129
130 HtmlInput inputForDescription = HtmlUnitUtil.getInputContainingText(
131 maintPage, "* Document Description");
132 inputForDescription.setValueAttribute("Description");
133 // setFieldValue(maintPage, "document.newMaintainableObject.fractionalTimeAllowed", "99");
134 // setFieldValue(maintPage, "document.newMaintainableObject.roundingOption", "T");
135
136 HtmlRadioButtonInput hb = maintPage.getHtmlElementById("document.newMaintainableObject.fractionalTimeAllowed99");
137 hb.setChecked(true);
138
139 HtmlPage resultantPageAfterEdit = HtmlUnitUtil
140 .clickInputContainingText(maintPage, "submit");
141 LOG.debug(resultantPageAfterEdit.asText());
142
143 // assertTrue("Error message for not selecting any record type",
144 // resultantPageAfterEdit.asText().contains("For this earn code you must specify Record Hours or Record Time or Record Amount"));
145
146 //Sai - confirm that the error is thrown if more than one record type is selected
147 // checkBox = maintPage.getHtmlElementById("document.newMaintainableObject.recordTime");
148 // checkBox.setChecked(true);
149 // checkBox = maintPage.getHtmlElementById("document.newMaintainableObject.recordHours");
150 // checkBox.setChecked(true);
151
152 inputForDescription = HtmlUnitUtil.getInputContainingText(
153 maintPage, "* Document Description");
154 inputForDescription.setValueAttribute("Description");
155 resultantPageAfterEdit = HtmlUnitUtil
156 .clickInputContainingText(maintPage, "submit");
157 // assertTrue("Error message for selecting more than one record type",
158 // resultantPageAfterEdit.asText().contains("For this earn code you can only specify one of the Record types"));
159 }
160
161 @Test
162 public void testGetEarnCodesForDisplay() throws Exception{
163 //create the testPrincipal object for the earn code service parm, from the TEST_USER string
164 Principal testPrincipal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName("testUser");
165 Map<String, String> earnCodesDisplay = earnCodeService.getEarnCodesForDisplay(testPrincipal.getPrincipalId(), false);
166 // assertions commented out until earn code service finished. 20120918tv
167 // Assert.assertNotNull("earnCodesDisplay should not be null", earnCodesDisplay);
168 // Assert.assertEquals("There should be 2 earnCode found for principal_id 'testUser', not " + earnCodesDisplay.size(), earnCodesDisplay.size(), 2);
169 // Assert.assertTrue("earnCodesDisplay should contain Value 'EC1 : test1'", earnCodesDisplay.containsValue("EC1 : test1"));
170 }
171 }