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.roles.service;
017    
018    import java.math.BigDecimal;
019    import java.sql.Date;
020    import java.sql.Timestamp;
021    import java.util.List;
022    
023    import org.joda.time.DateTime;
024    import org.junit.Assert;
025    import org.junit.Test;
026    import org.kuali.hr.job.Job;
027    import org.kuali.hr.test.KPMETestCase;
028    import org.kuali.hr.time.position.Position;
029    import org.kuali.hr.time.roles.TkRole;
030    import org.kuali.hr.time.service.base.TkServiceLocator;
031    import org.kuali.hr.time.util.TKUtils;
032    import org.kuali.hr.time.util.TkConstants;
033    import org.kuali.rice.krad.service.KRADServiceLocator;
034    
035    public class TkRoleServiceTest  extends KPMETestCase {
036    
037            public static final String ADMIN = "admin";
038            public static final String TEST_USER = "eric";
039            public static final Long WORK_AREA = 999L;
040    
041            private String posRoleId = null;
042    
043            @Test
044            public void testGetAnyWorkAreaRoles() throws Exception {
045                    Date asOfDate = new Date((new DateTime(2010, 8, 25, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
046                    List<TkRole> roles = TkServiceLocator.getTkRoleService().getWorkAreaRoles(WORK_AREA, asOfDate);
047                    Assert.assertNotNull(roles);
048                    Assert.assertEquals("Incorrect number of roles.", 4, roles.size());
049                    for (TkRole role : roles) {
050                            Assert.assertTrue("Incorrect values.", 
051                                            (role.getPrincipalId().equals(ADMIN) && role.getRoleName().equals(TkConstants.ROLE_TK_LOCATION_ADMIN))
052                                     || (role.getPrincipalId().equals(ADMIN) && role.getRoleName().equals(TkConstants.ROLE_TK_APPROVER))
053                                     || (role.getPrincipalId().equals(TEST_USER) && role.getRoleName().equals(TkConstants.ROLE_TK_LOCATION_ADMIN))
054                                     || (role.getPrincipalId().equals(TEST_USER) && role.getRoleName().equals(TkConstants.ROLE_TK_APPROVER)));
055                    }
056            }
057            
058            @Test
059            public void testGetApproverWorkAreaRoles() throws Exception {
060                    Date asOfDate = new Date((new DateTime(2010, 8, 25, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
061                    List<TkRole> roles = TkServiceLocator.getTkRoleService().getWorkAreaRoles(WORK_AREA, TkConstants.ROLE_TK_APPROVER, asOfDate);
062                    Assert.assertNotNull(roles);
063                    Assert.assertEquals("Incorrect number of roles.", 2, roles.size());
064                    for (TkRole role : roles) {
065                            Assert.assertTrue("Incorrect values.", role.getPrincipalId().equals(ADMIN) || role.getPrincipalId().equals(TEST_USER));
066                    }
067            }
068    
069            @Test
070            public void testGetRolesForPrincipal() throws Exception {
071                    Date asOfDate = new Date((new DateTime(2010, 8, 21, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
072                    List<TkRole> roles = TkServiceLocator.getTkRoleService().getRoles(TEST_USER, TkConstants.ROLE_TK_APPROVER, asOfDate);
073                    Assert.assertNotNull(roles);
074                    Assert.assertEquals("Incorrect number of roles.", 1, roles.size());
075                    for (TkRole role: roles) {
076                            Assert.assertTrue("Incorrect values.", 
077                                       role.getPrincipalId().equals(TEST_USER) 
078                                    && role.getRoleName().equals(TkConstants.ROLE_TK_APPROVER)
079                                    && role.getEffectiveDate().before(asOfDate));
080                    }
081            }
082            
083            @Test
084            public void testGetEffectiveDateRolesForPrincipal() throws Exception {
085                    Date asOfDate = new Date((new DateTime(2010, 8, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
086                    List<TkRole> roles = TkServiceLocator.getTkRoleService().getRoles(TEST_USER, asOfDate);
087                    Assert.assertNotNull(roles);
088                    Assert.assertEquals("Incorrect number of roles.", 2, roles.size());
089                    for (TkRole role: roles) {
090                            Assert.assertTrue("Incorrect values.", 
091                                            role.getPrincipalId().equals(TEST_USER) 
092                                    && (role.getRoleName().equals(TkConstants.ROLE_TK_LOCATION_ADMIN) || role.getRoleName().equals(TkConstants.ROLE_TK_APPROVER)
093                                    && role.getEffectiveDate().before(asOfDate)));
094                    }
095            }
096    
097            @Test
098            public void testPositionRole() throws Exception {
099                    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    }