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.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                    List<TkRole> roles = TkServiceLocator.getTkRoleService().getWorkAreaRoles(WORK_AREA);
046                    Assert.assertNotNull(roles);
047                    Assert.assertEquals("Incorrect number of roles.", 6, roles.size());
048                    for (TkRole role : roles) {
049                            Assert.assertTrue("Incorrect values.", 
050                                            (role.getPrincipalId().equals(ADMIN) && role.getRoleName().equals(TkConstants.ROLE_TK_LOCATION_ADMIN))
051                                     || (role.getPrincipalId().equals(ADMIN) && role.getRoleName().equals(TkConstants.ROLE_TK_APPROVER))
052                                     || (role.getPrincipalId().equals(TEST_USER) && role.getRoleName().equals(TkConstants.ROLE_TK_LOCATION_ADMIN))
053                                     || (role.getPrincipalId().equals(TEST_USER) && role.getRoleName().equals(TkConstants.ROLE_TK_APPROVER)));
054                    }
055            }
056            
057            @Test
058            public void testGetApproverWorkAreaRoles() throws Exception {
059                    Date asOfDate = new Date((new DateTime(2010, 8, 25, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
060                    List<TkRole> roles = TkServiceLocator.getTkRoleService().getWorkAreaRoles(WORK_AREA, TkConstants.ROLE_TK_APPROVER, asOfDate);
061                    Assert.assertNotNull(roles);
062                    Assert.assertEquals("Incorrect number of roles.", 2, roles.size());
063                    for (TkRole role : roles) {
064                            Assert.assertTrue("Incorrect values.", role.getPrincipalId().equals(ADMIN) || role.getPrincipalId().equals(TEST_USER));
065                    }
066            }
067    
068            @Test
069            public void testGetRolesForPrincipal() throws Exception {
070                    Date asOfDate = new Date((new DateTime(2010, 8, 21, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
071                    List<TkRole> roles = TkServiceLocator.getTkRoleService().getRoles(TEST_USER, TkConstants.ROLE_TK_APPROVER, asOfDate);
072                    Assert.assertNotNull(roles);
073                    Assert.assertEquals("Incorrect number of roles.", 1, roles.size());
074                    for (TkRole role: roles) {
075                            Assert.assertTrue("Incorrect values.", 
076                                       role.getPrincipalId().equals(TEST_USER) 
077                                    && role.getRoleName().equals(TkConstants.ROLE_TK_APPROVER)
078                                    && role.getEffectiveDate().before(asOfDate));
079                    }
080            }
081            
082            @Test
083            public void testGetEffectiveDateRolesForPrincipal() throws Exception {
084                    Date asOfDate = new Date((new DateTime(2010, 8, 1, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis());
085                    List<TkRole> roles = TkServiceLocator.getTkRoleService().getRoles(TEST_USER, asOfDate);
086                    Assert.assertNotNull(roles);
087                    Assert.assertEquals("Incorrect number of roles.", 2, roles.size());
088                    for (TkRole role: roles) {
089                            Assert.assertTrue("Incorrect values.", 
090                                            role.getPrincipalId().equals(TEST_USER) 
091                                    && (role.getRoleName().equals(TkConstants.ROLE_TK_LOCATION_ADMIN) || role.getRoleName().equals(TkConstants.ROLE_TK_APPROVER)
092                                    && role.getEffectiveDate().before(asOfDate)));
093                    }
094            }
095    
096            @Test
097            public void testPositionRole() throws Exception {
098                    List<TkRole> lstRoles = TkServiceLocator.getTkRoleService().getRoles("earl", TKUtils.getCurrentDate());
099                    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.setHrJobId("9999");
131    
132                    // This is causing all the unit tests to have an error - needs to be looked into later.
133                    TkServiceLocator.getJobService().saveOrUpdate(job);
134    
135                    TkRole tkRole = new TkRole();
136                    tkRole.setPrincipalId(null);
137                    tkRole.setRoleName(TkConstants.ROLE_TK_APPROVER);
138                    tkRole.setUserPrincipalId("admin");
139                    tkRole.setWorkArea(1234L);
140                    tkRole.setEffectiveDate(new Date((new DateTime(2010, 8, 20, 12, 0, 0, 0, TKUtils.getSystemDateTimeZone())).getMillis()));
141                    tkRole.setTimestamp(new Timestamp(System.currentTimeMillis()));
142                    tkRole.setActive(true);
143                    tkRole.setPositionNumber(posNumber);
144    
145                    TkServiceLocator.getTkRoleService().saveOrUpdate(tkRole);
146                    posRoleId = tkRole.getHrRolesId();
147            }
148    
149            @Override
150            public void tearDown() throws Exception {
151                    TkRole tkRole = TkServiceLocator.getTkRoleService().getRole(posRoleId);
152                    KRADServiceLocator.getBusinessObjectService().delete(tkRole);
153                    super.tearDown();
154            }
155    }