Skip to content

Commit

Permalink
[BE] refactor: LocalDateTime.now() 사용을 LocalDateTime.now(clock) 형식으로 …
Browse files Browse the repository at this point in the history
…변경 (#395) (#400)

* refactor: LocalDateTime.now(Clock) 형식으로 변경

* test: 현재 시간 -> 고정된 시간으로 변경

* test: Clock의 instant를 생성하는 support class 추가

* test: TimeInstantProvider 메소드 명 변경

---------

Co-authored-by: hyunseo <[email protected]>
  • Loading branch information
seokjin8678 and carsago authored Aug 31, 2023
1 parent 43e9785 commit 93d8aee
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.festago.exception.BadRequestException;
import com.festago.exception.ErrorCode;
import com.festago.exception.NotFoundException;
import java.time.Clock;
import java.time.LocalDateTime;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -23,20 +24,22 @@ public class EntryService {
private final EntryCodeProvider entryCodeProvider;
private final EntryCodeExtractor entryCodeExtractor;
private final MemberTicketRepository memberTicketRepository;
private final Clock clock;

public EntryService(EntryCodeProvider entryCodeProvider, EntryCodeExtractor entryCodeExtractor,
MemberTicketRepository memberTicketRepository) {
MemberTicketRepository memberTicketRepository, Clock clock) {
this.entryCodeProvider = entryCodeProvider;
this.entryCodeExtractor = entryCodeExtractor;
this.memberTicketRepository = memberTicketRepository;
this.clock = clock;
}

public EntryCodeResponse createEntryCode(Long memberId, Long memberTicketId) {
MemberTicket memberTicket = findMemberTicket(memberTicketId);
if (!memberTicket.isOwner(memberId)) {
throw new BadRequestException(ErrorCode.NOT_MEMBER_TICKET_OWNER);
}
if (!memberTicket.canEntry(LocalDateTime.now())) {
if (!memberTicket.canEntry(LocalDateTime.now(clock))) {
throw new BadRequestException(ErrorCode.NOT_ENTRY_TIME);
}
EntryCode entryCode = EntryCode.create(entryCodeProvider, memberTicket);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.festago.exception.BadRequestException;
import com.festago.exception.ErrorCode;
import com.festago.exception.NotFoundException;
import java.time.Clock;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;
Expand All @@ -25,11 +26,13 @@ public class MemberTicketService {

private final MemberTicketRepository memberTicketRepository;
private final MemberRepository memberRepository;
private final Clock clock;

public MemberTicketService(MemberTicketRepository memberTicketRepository,
MemberRepository memberRepository) {
MemberRepository memberRepository, Clock clock) {
this.memberTicketRepository = memberTicketRepository;
this.memberRepository = memberRepository;
this.clock = clock;
}

@Transactional(readOnly = true)
Expand Down Expand Up @@ -59,7 +62,7 @@ public MemberTicketsResponse findCurrent(Long memberId, Pageable pageable) {
}

private List<MemberTicket> filterCurrentMemberTickets(List<MemberTicket> memberTickets) {
LocalDateTime currentTime = LocalDateTime.now();
LocalDateTime currentTime = LocalDateTime.now(clock);
return memberTickets.stream()
.filter(memberTicket -> memberTicket.isBeforeEntry(currentTime) || memberTicket.canEntry(currentTime))
.sorted(comparing((MemberTicket memberTicket) -> memberTicket.isBeforeEntry(currentTime))
Expand Down
52 changes: 41 additions & 11 deletions backend/src/test/java/com/festago/application/EntryServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@
import com.festago.support.MemberFixture;
import com.festago.support.MemberTicketFixture;
import com.festago.support.StageFixture;
import com.festago.support.TimeInstantProvider;
import java.time.Clock;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Optional;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores;
Expand All @@ -34,6 +39,7 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
Expand All @@ -50,6 +56,9 @@ class EntryServiceTest {
@Mock
MemberTicketRepository memberTicketRepository;

@Spy
Clock clock = Clock.systemDefaultZone();

@InjectMocks
EntryService entryService;

Expand All @@ -59,9 +68,15 @@ class 티켓의_QR_생성_요청 {
@Test
void 입장_시간_전_요청하면_예외() {
// given
LocalDateTime entryTime = LocalDateTime.now().plusMinutes(30);
LocalDateTime entryTime = LocalDateTime.parse("2023-07-30T16:00:00");
Festival festival = FestivalFixture.festival()
.startDate(entryTime.toLocalDate())
.endDate(entryTime.toLocalDate())
.build();
Stage stage = StageFixture.stage()
.startTime(LocalDateTime.now().plusHours(1))
.festival(festival)
.startTime(entryTime.plusHours(2))
.ticketOpenTime(entryTime.minusHours(1))
.build();
MemberTicket memberTicket = MemberTicketFixture.memberTicket()
.id(1L)
Expand All @@ -70,9 +85,10 @@ class 티켓의_QR_생성_요청 {
.build();
Long memberId = memberTicket.getOwner().getId();
Long memberTicketId = memberTicket.getId();

given(memberTicketRepository.findById(anyLong()))
.willReturn(Optional.of(memberTicket));
given(clock.instant())
.willReturn(TimeInstantProvider.from(entryTime.minusHours(1)));

// when & then
assertThatThrownBy(() -> entryService.createEntryCode(memberId, memberTicketId))
Expand All @@ -83,15 +99,15 @@ class 티켓의_QR_생성_요청 {
@Test
void 입장_시간이_24시간이_넘은_경우_예외() {
// given
LocalDateTime stageStartTime = LocalDateTime.now().minusHours(24);
LocalDateTime entryTime = stageStartTime.minusSeconds(10);
LocalDateTime entryTime = LocalDateTime.parse("2023-07-30T16:00:00");
Festival festival = FestivalFixture.festival()
.startDate(stageStartTime.toLocalDate())
.endDate(stageStartTime.toLocalDate())
.startDate(entryTime.toLocalDate())
.endDate(entryTime.toLocalDate())
.build();
Stage stage = StageFixture.stage()
.festival(festival)
.startTime(stageStartTime)
.startTime(entryTime.plusHours(2))
.ticketOpenTime(entryTime.minusHours(1))
.build();
MemberTicket memberTicket = MemberTicketFixture.memberTicket()
.id(1L)
Expand All @@ -100,9 +116,10 @@ class 티켓의_QR_생성_요청 {
.build();
Long memberId = memberTicket.getOwner().getId();
Long memberTicketId = memberTicket.getId();

given(memberTicketRepository.findById(anyLong()))
.willReturn(Optional.of(memberTicket));
given(clock.instant())
.willReturn(TimeInstantProvider.from((entryTime.plusHours(24))));

// when & then
assertThatThrownBy(() -> entryService.createEntryCode(memberId, memberTicketId))
Expand All @@ -113,14 +130,12 @@ class 티켓의_QR_생성_요청 {
@Test
void 자신의_티켓이_아니면_예외() {
// given
LocalDateTime entryTime = LocalDateTime.now().minusMinutes(30);
Long memberId = 1L;
Member other = MemberFixture.member()
.id(2L)
.build();
MemberTicket otherTicket = MemberTicketFixture.memberTicket()
.id(1L)
.entryTime(entryTime)
.owner(other)
.build();
Long memberTicketId = otherTicket.getId();
Expand Down Expand Up @@ -150,8 +165,21 @@ class 티켓의_QR_생성_요청 {
@Test
void 성공() {
// given
LocalDateTime entryTime = LocalDateTime.parse("2023-07-30T16:00:00");
Instant now = Instant.from(ZonedDateTime.of(entryTime, ZoneId.systemDefault()));
Festival festival = FestivalFixture.festival()
.startDate(entryTime.toLocalDate())
.endDate(entryTime.toLocalDate())
.build();
Stage stage = StageFixture.stage()
.festival(festival)
.startTime(entryTime.plusHours(2))
.ticketOpenTime(entryTime.minusHours(1))
.build();
MemberTicket memberTicket = MemberTicketFixture.memberTicket()
.id(1L)
.stage(stage)
.entryTime(entryTime)
.build();
String code = "3112321312123";
Long memberId = memberTicket.getOwner().getId();
Expand All @@ -161,6 +189,8 @@ class 티켓의_QR_생성_요청 {
.willReturn(Optional.of(memberTicket));
given(entryCodeProvider.provide(any(), any()))
.willReturn(code);
given(clock.instant())
.willReturn(now);

// when
EntryCodeResponse entryCode = entryService.createEntryCode(memberId, memberTicketId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.festago.support.MemberFixture;
import com.festago.support.MemberTicketFixture;
import com.festago.support.StageFixture;
import java.time.Clock;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
Expand All @@ -29,6 +30,7 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
Expand All @@ -44,6 +46,9 @@ class MemberTicketServiceTest {
@Mock
MemberRepository memberRepository;

@Spy
Clock clock = Clock.systemDefaultZone();

@InjectMocks
MemberTicketService memberTicketService;

Expand Down
17 changes: 17 additions & 0 deletions backend/src/test/java/com/festago/support/TimeInstantProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.festago.support;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class TimeInstantProvider {

public static Instant from(String localDateTime) {
return from(LocalDateTime.parse(localDateTime));
}

public static Instant from(LocalDateTime localDateTime) {
return Instant.from(ZonedDateTime.of(localDateTime, ZoneId.systemDefault()));
}
}

0 comments on commit 93d8aee

Please sign in to comment.