View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.time.earncode.service;
17  
18  import java.sql.Date;
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.apache.log4j.Logger;
23  import org.junit.Assert;
24  import org.junit.Before;
25  import org.junit.Test;
26  import org.kuali.hr.test.KPMETestCase;
27  import org.kuali.hr.time.assignment.Assignment;
28  import org.kuali.hr.time.earncode.EarnCode;
29  import org.kuali.hr.time.service.base.TkServiceLocator;
30  import org.kuali.hr.time.test.HtmlUnitUtil;
31  import org.kuali.hr.time.test.TkTestConstants;
32  import org.kuali.hr.time.util.TKUtils;
33  
34  import com.gargoylesoftware.htmlunit.html.HtmlInput;
35  import com.gargoylesoftware.htmlunit.html.HtmlPage;
36  import com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput;
37  import com.gargoylesoftware.htmlunit.html.HtmlSelect;
38  import org.kuali.rice.kim.api.identity.principal.Principal;
39  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
40  
41  public class EarnCodeServiceImplTest extends KPMETestCase {
42  
43  
44  	public static final String TEST_USER = "admin";
45  	public static final Long TEST_ASSIGNMENT_JOB_NUMBER = 1L;
46  	public static final Long TEST_ASSIGNMENT_JOB_NUMBER_2 = 2L;
47  	public static final Long TEST_ASSIGNMENT_JOB_NUMBER_3 = 3L;
48  	public static final Long TEST_ASSIGNMENT_JOB_NUMBER_4 = 4L;
49  
50  	@SuppressWarnings("unused")
51  	private static final Logger LOG = Logger.getLogger(EarnCodeServiceImplTest.class);
52  
53  	EarnCodeService earnCodeService = null;
54  
55  
56  	@Before
57  	public void setUp() throws Exception {
58  		super.setUp();
59  		earnCodeService=TkServiceLocator.getEarnCodeService();
60  	}
61  
62  	@Test
63  	public void getEarnCodes() throws Exception {
64          Date asOfDate = TKUtils.getTimelessDate(null);
65  		List<Assignment> assignments = TkServiceLocator.getAssignmentService().getAssignments(TEST_USER, asOfDate);
66  		Assert.assertNotNull(assignments);
67  		Assert.assertTrue("Emtpy assignment list", !assignments.isEmpty());
68  
69  		Assignment assignment1 = null;
70  		Assignment assignment2 = null;
71  		Assignment assignment3 = null;
72  		Assignment assignment4 = null;
73  		for (Assignment a : assignments) {
74  			if (a.getJobNumber().equals(TEST_ASSIGNMENT_JOB_NUMBER)) {
75  				assignment1 = a;
76  			} else if (a.getJobNumber().equals(TEST_ASSIGNMENT_JOB_NUMBER_2)) {
77  				assignment2 = a;
78  			} else if (a.getJobNumber().equals(TEST_ASSIGNMENT_JOB_NUMBER_3)) {
79  				assignment3 = a;
80  			} else if (a.getJobNumber().equals(TEST_ASSIGNMENT_JOB_NUMBER_4)) {
81  				assignment4 = a;
82  			}
83  		}
84  
85  		// one for each test scenario involving wildcards at least...
86  		Assert.assertNotNull("Test assignment not found.", assignment1);
87  		Assert.assertNotNull("Test assignment not found.", assignment2);
88  		Assert.assertNotNull("Test assignment not found.", assignment3);
89  		Assert.assertNotNull("Test assignment not found.", assignment4);
90  
91          //  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.
92          //  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.
93          // Testing standard lookup.
94  		List<EarnCode> earnCodes1t = earnCodeService.getEarnCodesForTime(assignment1, asOfDate);
95  		Assert.assertEquals("Wrong number of earn codes returned.", 7, earnCodes1t.size());
96          List<EarnCode> earnCodes1l = earnCodeService.getEarnCodesForLeave(assignment1, asOfDate, false);
97          Assert.assertEquals("Wrong number of earn codes returned.", 0, earnCodes1l.size());
98          List<EarnCode> earnCodes1lt= earnCodeService.getEarnCodesForLeaveAndTime(assignment1, asOfDate, false);
99          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 }