-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 시험장 예약 스케쥴링 구현 * feat: 차량 예약 스케쥴링 구현 * feat: 시험장 자동반납 스케쥴러 구현
- Loading branch information
Showing
15 changed files
with
262 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.testcar.car.config; | ||
|
||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.EnableScheduling; | ||
|
||
/** 스프링 스케쥴러를 활성화하는 설정 빈 */ | ||
@Configuration | ||
@EnableScheduling | ||
public class SchedulerConfig {} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/testcar/car/domains/carReservation/CarReservationSchedulerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.testcar.car.domains.carReservation; | ||
|
||
|
||
import com.testcar.car.domains.carReservation.entity.CarReservation; | ||
import com.testcar.car.domains.carReservation.repository.CarReservationRepository; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
/** 차량 예약에 대한 스케쥴링 설정 */ | ||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class CarReservationSchedulerService { | ||
private final CarReservationRepository carReservationRepository; | ||
|
||
/** 매일 자정 예약중인 차량의 반납시간이 도래하면 이용완료로 변경한다. */ | ||
@Scheduled(cron = "0 0 0 * * ?") | ||
public void updateAllReturned() { | ||
// 각 차량 예약 건의 expiredAt 시간이 now 와 같다면 이용완료 시킨다. | ||
final LocalDateTime now = LocalDateTime.now().withSecond(0).withNano(0); | ||
final List<CarReservation> carReservations = | ||
carReservationRepository.findAllByExpiredAtAndStatusReserved(now); | ||
|
||
carReservations.forEach(CarReservation::updateReturn); | ||
carReservationRepository.saveAll(carReservations); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/com/testcar/car/domains/trackReservation/TrackReservationSchedulerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.testcar.car.domains.trackReservation; | ||
|
||
|
||
import com.testcar.car.domains.trackReservation.entity.TrackReservation; | ||
import com.testcar.car.domains.trackReservation.entity.TrackReservationSlot; | ||
import com.testcar.car.domains.trackReservation.repository.TrackReservationRepository; | ||
import java.time.LocalDateTime; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
/** 시험장 예약에 대한 스케쥴링 설정 */ | ||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class TrackReservationSchedulerService { | ||
private final TrackReservationRepository trackReservationRepository; | ||
|
||
/** 매 시간 예약중인 시험장의 반납시간이 도래하면 이용완료로 변경한다. */ | ||
@Scheduled(cron = "0 0 * * * ?") | ||
public void updateAllCompleted() { | ||
// 각 시험장 예약의 슬롯 중, 마지막 시간의 expiredAt 시간이 now 와 같다면 이용완료 시킨다. | ||
final LocalDateTime now = LocalDateTime.now().withSecond(0).withNano(0); | ||
final List<TrackReservation> trackReservations = | ||
trackReservationRepository.findAllBySlotExpiredAtAndStatusReserved(now); | ||
|
||
trackReservations.stream() | ||
.filter(trackReservation -> isLastSlotExpiredAtEquals(trackReservation, now)) | ||
.forEach(TrackReservation::completed); | ||
trackReservationRepository.saveAll(trackReservations); | ||
} | ||
|
||
private boolean isLastSlotExpiredAtEquals( | ||
TrackReservation trackReservation, LocalDateTime time) { | ||
return trackReservation.getTrackReservationSlots().stream() | ||
.max(Comparator.comparing(TrackReservationSlot::getExpiredAt)) | ||
.map(TrackReservationSlot::getExpiredAt) | ||
.filter(expiredAt -> expiredAt.equals(time)) | ||
.isPresent(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/test/java/com/testcar/car/domains/carReservation/CarReservationSchedulerServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.testcar.car.domains.carReservation; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.BDDMockito.then; | ||
|
||
import com.testcar.car.common.CarEntityFactory; | ||
import com.testcar.car.domains.carReservation.entity.CarReservation; | ||
import com.testcar.car.domains.carReservation.entity.ReservationStatus; | ||
import com.testcar.car.domains.carReservation.repository.CarReservationRepository; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import org.junit.jupiter.api.BeforeAll; | ||
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; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class CarReservationSchedulerServiceTest { | ||
@Mock private CarReservationRepository carReservationRepository; | ||
@InjectMocks private CarReservationSchedulerService carReservationSchedulerService; | ||
|
||
private static List<CarReservation> carReservations; | ||
|
||
@BeforeAll | ||
public static void setUp() { | ||
carReservations = | ||
List.of( | ||
CarEntityFactory.createCarReservation(), | ||
CarEntityFactory.createCarReservation(), | ||
CarEntityFactory.createCarReservation()); | ||
} | ||
|
||
@Test | ||
void 사용중인_차량예약_마감시간이_되면_모두_반납한다() { | ||
// given | ||
final LocalDateTime now = LocalDateTime.now().withSecond(0).withNano(0); | ||
given(carReservationRepository.findAllByExpiredAtAndStatusReserved(now)) | ||
.willReturn(carReservations.subList(1, 2)); | ||
|
||
// when | ||
carReservationSchedulerService.updateAllReturned(); | ||
|
||
// then | ||
assertEquals(ReservationStatus.RESERVED, carReservations.get(0).getStatus()); | ||
assertEquals(ReservationStatus.RETURNED, carReservations.get(1).getStatus()); | ||
assertEquals(ReservationStatus.RESERVED, carReservations.get(2).getStatus()); | ||
then(carReservationRepository).should().findAllByExpiredAtAndStatusReserved(now); | ||
then(carReservationRepository).should().saveAll(carReservations.subList(1, 2)); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...t/java/com/testcar/car/domains/trackReservation/TrackReservationSchedulerServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.testcar.car.domains.trackReservation; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.doCallRealMethod; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.testcar.car.domains.trackReservation.entity.TrackReservation; | ||
import com.testcar.car.domains.trackReservation.entity.TrackReservationSlot; | ||
import com.testcar.car.domains.trackReservation.repository.TrackReservationRepository; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import java.util.Set; | ||
import org.junit.jupiter.api.BeforeEach; | ||
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; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class TrackReservationSchedulerServiceTest { | ||
@Mock private TrackReservationRepository trackReservationRepository; | ||
@InjectMocks private TrackReservationSchedulerService trackReservationSchedulerService; | ||
|
||
private TrackReservation todayTrackReservation; | ||
private TrackReservation tomorrowTrackReservation; | ||
private static final LocalDateTime now = LocalDateTime.now().withSecond(0).withNano(0); | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
todayTrackReservation = mock(TrackReservation.class); | ||
TrackReservationSlot slot = mock(TrackReservationSlot.class); | ||
when(todayTrackReservation.getTrackReservationSlots()).thenReturn(Set.of(slot)); | ||
when(slot.getExpiredAt()).thenReturn(now); | ||
doCallRealMethod().when(todayTrackReservation).completed(); | ||
|
||
tomorrowTrackReservation = mock(TrackReservation.class); | ||
} | ||
|
||
@Test | ||
void 사용중인_시험장_마지막_슬롯의_마감시간이_도래하면_시험장을_반납한다() { | ||
// given | ||
List<TrackReservation> reservations = | ||
List.of(todayTrackReservation, tomorrowTrackReservation); | ||
when(trackReservationRepository.findAllBySlotExpiredAtAndStatusReserved( | ||
any(LocalDateTime.class))) | ||
.thenReturn(reservations); | ||
when(trackReservationRepository.saveAll(reservations)).thenReturn(reservations); | ||
|
||
// when | ||
trackReservationSchedulerService.updateAllCompleted(); | ||
|
||
// then | ||
verify(todayTrackReservation).completed(); | ||
verify(tomorrowTrackReservation, never()).completed(); | ||
verify(trackReservationRepository).findAllBySlotExpiredAtAndStatusReserved(now); | ||
verify(trackReservationRepository).saveAll(reservations); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters