View Javadoc

1   /**
2    * Copyright 2004-2012 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.roles.service;
17  
18  import java.math.BigDecimal;
19  import java.sql.Date;
20  import java.sql.Timestamp;
21  import java.util.List;
22  
23  import org.joda.time.DateTime;
24  import org.junit.Assert;
25  import org.junit.Test;
26  import org.kuali.hr.job.Job;
27  import org.kuali.hr.test.KPMETestCase;
28  import org.kuali.hr.time.position.Position;
29  import org.kuali.hr.time.roles.TkRole;
30  import org.kuali.hr.time.service.base.TkServiceLocator;
31  import org.kuali.hr.time.util.TKUtils;
32  import org.kuali.hr.time.util.TkConstants;
33  import org.kuali.rice.krad.service.KRADServiceLocator;
34  
35  public class TkRoleServiceTest  extends KPMETestCase {
36  
37  	public static final String ADMIN = "admin";
38  	public static final String TEST_USER = "eric";
39  	public static final Long WORK_AREA = 999L;
40  
41  	private String posRoleId = null;
42  
43  	@Test
44  	public void testGetAnyWorkAreaRoles() throws Exception {
45  		Date asOfDate = new Date((new DateTime(2010, 8, 25, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
46  		List<TkRole> roles = TkServiceLocator.getTkRoleService().getWorkAreaRoles(WORK_AREA, asOfDate);
47  		Assert.assertNotNull(roles);
48  		Assert.assertEquals("Incorrect number of roles.", 4, roles.size());
49  		for (TkRole role : roles) {
50  			Assert.assertTrue("Incorrect values.", 
51  					(role.getPrincipalId().equals(ADMIN) && role.getRoleName().equals(TkConstants.ROLE_TK_LOCATION_ADMIN))
52  				 || (role.getPrincipalId().equals(ADMIN) && role.getRoleName().equals(TkConstants.ROLE_TK_APPROVER))
53  				 || (role.getPrincipalId().equals(TEST_USER) && role.getRoleName().equals(TkConstants.ROLE_TK_LOCATION_ADMIN))
54  				 || (role.getPrincipalId().equals(TEST_USER) && role.getRoleName().equals(TkConstants.ROLE_TK_APPROVER)));
55  		}
56  	}
57  	
58  	@Test
59  	public void testGetApproverWorkAreaRoles() throws Exception {
60  		Date asOfDate = new Date((new DateTime(2010, 8, 25, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
61  		List<TkRole> roles = TkServiceLocator.getTkRoleService().getWorkAreaRoles(WORK_AREA, TkConstants.ROLE_TK_APPROVER, asOfDate);
62  		Assert.assertNotNull(roles);
63  		Assert.assertEquals("Incorrect number of roles.", 2, roles.size());
64  		for (TkRole role : roles) {
65  			Assert.assertTrue("Incorrect values.", role.getPrincipalId().equals(ADMIN) || role.getPrincipalId().equals(TEST_USER));
66  		}
67  	}
68  
69  	@Test
70  	public void testGetRolesForPrincipal() throws Exception {
71  		Date asOfDate = new Date((new DateTime(2010, 8, 21, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
72  		List<TkRole> roles = TkServiceLocator.getTkRoleService().getRoles(TEST_USER, TkConstants.ROLE_TK_APPROVER, asOfDate);
73  		Assert.assertNotNull(roles);
74  		Assert.assertEquals("Incorrect number of roles.", 1, roles.size());
75  		for (TkRole role: roles) {
76  			Assert.assertTrue("Incorrect values.", 
77  				   role.getPrincipalId().equals(TEST_USER) 
78  				&& role.getRoleName().equals(TkConstants.ROLE_TK_APPROVER)
79  				&& role.getEffectiveDate().before(asOfDate));
80  		}
81  	}
82  	
83  	@Test
84  	public void testGetEffectiveDateRolesForPrincipal() throws Exception {
85  		Date asOfDate = new Date((new DateTime(2010, 8, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
86  		List<TkRole> roles = TkServiceLocator.getTkRoleService().getRoles(TEST_USER, asOfDate);
87  		Assert.assertNotNull(roles);
88  		Assert.assertEquals("Incorrect number of roles.", 2, roles.size());
89  		for (TkRole role: roles) {
90  			Assert.assertTrue("Incorrect values.", 
91  					role.getPrincipalId().equals(TEST_USER) 
92  				&& (role.getRoleName().equals(TkConstants.ROLE_TK_LOCATION_ADMIN) || role.getRoleName().equals(TkConstants.ROLE_TK_APPROVER)
93  				&& role.getEffectiveDate().before(asOfDate)));
94  		}
95  	}
96  
97  	@Test
98  	public void testPositionRole() throws Exception {
99  		List<TkRole> lstRoles = TkServiceLocator.getTkRoleService().getRoles("earl", TKUtils.getCurrentDate());
100 		Assert.assertTrue(lstRoles!=null && !lstRoles.isEmpty());
101 	}
102 
103 	@Override
104 	public void setUp() throws Exception {
105 		super.setUp();
106 		Position pos = new Position();
107 		pos.setActive(true);
108 		pos.setTimestamp(new Timestamp(System.currentTimeMillis()));
109 		pos.setDescription("Advising");
110 		pos.setEffectiveDate(new Date((new DateTime(2010, 8, 20, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
111 		pos.setPositionNumber("123456");
112 		KRADServiceLocator.getBusinessObjectService().save(pos);
113 		String posNumber = pos.getPositionNumber();
114 
115 		//setup a job with that position
116 		Job job = new Job();
117 		job.setPositionNumber(posNumber);
118 		job.setPrincipalId("earl");
119 		job.setJobNumber(0L);
120 		job.setEffectiveDate(new Date((new DateTime(2010, 8, 20, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
121 		job.setDept("BL-CHEM");
122 		job.setHrSalGroup("BW1");
123 		job.setPayGrade("2IT");
124 		job.setTimestamp(new Timestamp(System.currentTimeMillis()));
125 		job.setCompRate(BigDecimal.ZERO);
126 		job.setLocation("BL");
127 		job.setStandardHours(BigDecimal.ZERO);
128 		job.setHrPayType("BW1");
129 		job.setActive(true);
130 		job.setPrimaryIndicator(true);
131 		job.setHrJobId("9999");
132 
133 		// This is causing all the unit tests to have an error - needs to be looked into later.
134 		TkServiceLocator.getJobService().saveOrUpdate(job);
135 
136 		TkRole tkRole = new TkRole();
137 		tkRole.setPrincipalId(null);
138 		tkRole.setRoleName(TkConstants.ROLE_TK_APPROVER);
139 		tkRole.setUserPrincipalId("admin");
140 		tkRole.setWorkArea(1234L);
141 		tkRole.setEffectiveDate(new Date((new DateTime(2010, 8, 20, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
142 		tkRole.setTimestamp(new Timestamp(System.currentTimeMillis()));
143 		tkRole.setActive(true);
144 		tkRole.setPositionNumber(posNumber);
145 
146 		TkServiceLocator.getTkRoleService().saveOrUpdate(tkRole);
147 		posRoleId = tkRole.getHrRolesId();
148 	}
149 
150 	@Override
151 	public void tearDown() throws Exception {
152 		TkRole tkRole = TkServiceLocator.getTkRoleService().getRole(posRoleId);
153 		KRADServiceLocator.getBusinessObjectService().delete(tkRole);
154 		super.tearDown();
155 	}
156 }