File size: 2,308 Bytes
d09b3be 921b03a d09b3be 921b03a d09b3be | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | package com.attendenceSystem.module.attendance.service;
import java.time.LocalDate;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import com.attendenceSystem.config.SystemConfig;
import com.attendenceSystem.module.attendance.entity.LeaveRequest;
import com.attendenceSystem.module.attendance.entity.enums.LeaveStatus;
import com.attendenceSystem.module.attendance.repository.LeaveRequestRepository;
import com.attendenceSystem.module.attendance.service.impl.LeaveScheduleServiceImpl;
import com.attendenceSystem.module.user.entity.User;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class LeaveScheduleServiceImplTest {
@Mock
private LeaveRequestRepository leaveRequestRepository;
@Mock
private SystemConfig systemConfig;
@InjectMocks
private LeaveScheduleServiceImpl leaveScheduleService;
@Test
void testAutoRejectExpiredLeaveRequests() {
LocalDate tomorrow = LocalDate.now().plusDays(1);
User user = User.builder()
.id(1L)
.build();
LeaveRequest leave = LeaveRequest.builder()
.id(1L)
.user(user)
.startDate(tomorrow)
.status(LeaveStatus.PENDING)
.build();
when(leaveRequestRepository.findByStatusAndStartDateLessThanEqual(LeaveStatus.PENDING, tomorrow))
.thenReturn(List.of(leave));
leaveScheduleService.autoRejectExpiredLeaveRequests();
verify(leaveRequestRepository, times(1)).save(leave);
assert leave.getStatus() == LeaveStatus.REJECTED;
}
@Test
void testAutoRejectWithNoExpiredLeaves() {
LocalDate tomorrow = LocalDate.now().plusDays(1);
when(leaveRequestRepository.findByStatusAndStartDateLessThanEqual(LeaveStatus.PENDING, tomorrow))
.thenReturn(List.of());
leaveScheduleService.autoRejectExpiredLeaveRequests();
verify(leaveRequestRepository, times(0)).save(any());
}
} |