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.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  		List<TkRole> roles = TkServiceLocator.getTkRoleService().getWorkAreaRoles(WORK_AREA);
46  		Assert.assertNotNull(roles);
47  		Assert.assertEquals("Incorrect number of roles.", 6, roles.size());
48  		for (TkRole role : roles) {
49  			Assert.assertTrue("Incorrect values.", 
50  					(role.getPrincipalId().equals(ADMIN) && role.getRoleName().equals(TkConstants.ROLE_TK_LOCATION_ADMIN))
51  				 || (role.getPrincipalId().equals(ADMIN) && role.getRoleName().equals(TkConstants.ROLE_TK_APPROVER))
52  				 || (role.getPrincipalId().equals(TEST_USER) && role.getRoleName().equals(TkConstants.ROLE_TK_LOCATION_ADMIN))
53  				 || (role.getPrincipalId().equals(TEST_USER) && role.getRoleName().equals(TkConstants.ROLE_TK_APPROVER)));
54  		}
55  	}
56  	
57  	@Test
58  	public void testGetApproverWorkAreaRoles() throws Exception {
59  		Date asOfDate = new Date((new DateTime(2010, 8, 25, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
60  		List<TkRole> roles = TkServiceLocator.getTkRoleService().getWorkAreaRoles(WORK_AREA, TkConstants.ROLE_TK_APPROVER, asOfDate);
61  		Assert.assertNotNull(roles);
62  		Assert.assertEquals("Incorrect number of roles.", 2, roles.size());
63  		for (TkRole role : roles) {
64  			Assert.assertTrue("Incorrect values.", role.getPrincipalId().equals(ADMIN) || role.getPrincipalId().equals(TEST_USER));
65  		}
66  	}
67  	
68  	@Test
69  	public void testGetRolesForPrincipal() throws Exception {
70  		Date asOfDate = new Date((new DateTime(2010, 8, 21, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
71  		List<TkRole> roles = TkServiceLocator.getTkRoleService().getRoles(TEST_USER, TkConstants.ROLE_TK_APPROVER, asOfDate);
72  		Assert.assertNotNull(roles);
73  		Assert.assertEquals("Incorrect number of roles.", 1, roles.size());
74  		for (TkRole role: roles) {
75  			Assert.assertTrue("Incorrect values.", 
76  				   role.getPrincipalId().equals(TEST_USER) 
77  				&& role.getRoleName().equals(TkConstants.ROLE_TK_APPROVER)
78  				&& role.getEffectiveDate().before(asOfDate));
79  		}
80  	}
81  	
82  	@Test
83  	public void testGetEffectiveDateRolesForPrincipal() throws Exception {
84  		Date asOfDate = new Date((new DateTime(2010, 8, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
85  		List<TkRole> roles = TkServiceLocator.getTkRoleService().getRoles(TEST_USER, asOfDate);
86  		Assert.assertNotNull(roles);
87  		Assert.assertEquals("Incorrect number of roles.", 2, roles.size());
88  		for (TkRole role: roles) {
89  			Assert.assertTrue("Incorrect values.", 
90  					role.getPrincipalId().equals(TEST_USER) 
91  				&& (role.getRoleName().equals(TkConstants.ROLE_TK_LOCATION_ADMIN) || role.getRoleName().equals(TkConstants.ROLE_TK_APPROVER)
92  				&& role.getEffectiveDate().before(asOfDate)));
93  		}
94  	}
95  	
96  	@Test
97  	public void testPositionRole() throws Exception {
98  		List<TkRole> lstRoles = TkServiceLocator.getTkRoleService().getRoles("earl", TKUtils.getCurrentDate());
99  		Assert.assertTrue(lstRoles!=null && !lstRoles.isEmpty());
100 	}
101 
102 	@Override
103 	public void setUp() throws Exception {
104 		super.setUp();
105 		Position pos = new Position();
106 		pos.setActive(true);
107 		pos.setTimestamp(new Timestamp(System.currentTimeMillis()));
108 		pos.setDescription("Advising");
109 		pos.setEffectiveDate(new Date((new DateTime(2010, 8, 20, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
110 		pos.setPositionNumber("123456");
111 		KRADServiceLocator.getBusinessObjectService().save(pos);
112 		String posNumber = pos.getPositionNumber();
113 		
114 		//setup a job with that position
115 		Job job = new Job();
116 		job.setPositionNumber(posNumber);
117 		job.setPrincipalId("earl");
118 		job.setJobNumber(0L);
119 		job.setEffectiveDate(new Date((new DateTime(2010, 8, 20, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
120 		job.setDept("BL-CHEM");
121 		job.setHrSalGroup("BW1");
122 		job.setPayGrade("2IT");
123 		job.setTimestamp(new Timestamp(System.currentTimeMillis()));
124 		job.setCompRate(BigDecimal.ZERO);
125 		job.setLocation("BL");
126 		job.setStandardHours(BigDecimal.ZERO);
127 		job.setHrPayType("BW1");
128 		job.setActive(true);
129 		job.setPrimaryIndicator(true);
130 		job.setEligibleForLeave(false);
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 }