From 7f2e446aff52b89d732a3d690b5d4e7387e637d9 Mon Sep 17 00:00:00 2001 From: eckrin Date: Mon, 26 Aug 2024 12:06:24 +0900 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20=EC=84=A0=EC=B0=A9=EC=88=9C=20?= =?UTF-8?q?=EC=9D=B4=EB=B2=A4=ED=8A=B8=20=ED=94=8C=EB=9E=98=EA=B7=B8=20?= =?UTF-8?q?=EC=B4=88=EA=B8=B0=ED=99=94=20=EB=A1=9C=EC=A7=81=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ArrivalEventInformationScheduler.java | 84 ------------------- .../service/ArrivalEventReleaseService.java | 16 +++- .../event/service/ArrivalEventService.java | 62 ++++++++++++++ 3 files changed, 77 insertions(+), 85 deletions(-) delete mode 100644 src/main/java/com/softeer/podoarrival/event/scheduler/ArrivalEventInformationScheduler.java diff --git a/src/main/java/com/softeer/podoarrival/event/scheduler/ArrivalEventInformationScheduler.java b/src/main/java/com/softeer/podoarrival/event/scheduler/ArrivalEventInformationScheduler.java deleted file mode 100644 index 416ee0c..0000000 --- a/src/main/java/com/softeer/podoarrival/event/scheduler/ArrivalEventInformationScheduler.java +++ /dev/null @@ -1,84 +0,0 @@ -package com.softeer.podoarrival.event.scheduler; - -import com.softeer.podoarrival.event.exception.EventTypeNotExistsException; -import com.softeer.podoarrival.event.model.entity.Event; -import com.softeer.podoarrival.event.model.entity.EventReward; -import com.softeer.podoarrival.event.model.entity.EventType; -import com.softeer.podoarrival.event.repository.EventRepository; -import com.softeer.podoarrival.event.repository.EventRewardRepository; -import com.softeer.podoarrival.event.repository.EventTypeRepository; -import com.softeer.podoarrival.event.service.ArrivalEventReleaseServiceJavaImpl; -import com.softeer.podoarrival.event.service.ArrivalEventReleaseServiceRedisImpl; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.scheduling.annotation.Scheduled; -import org.springframework.stereotype.Component; - -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.LocalTime; -import java.util.List; - -/** - * 선착순 이벤트의 당첨자 수를 세팅하는 스케줄러 - */ -@Slf4j -@Component -@RequiredArgsConstructor -public class ArrivalEventInformationScheduler { - - private final EventRepository eventRepository; - private final EventTypeRepository eventTypeRepository; - private final EventRewardRepository eventRewardRepository; - - /** - * 특정 시간에 Mysql에서 금일 진행될 선착순 이벤트의 절보를 세팅함. - * 당첨자 수, 이벤트 시작 시간, 이벤트 요일 설정 - * - */ - @Scheduled(cron = "0 25 03 * * *") - public void setArrivalEventInformation() { - // 시작일자, 이벤트 종류만 고려하여 이벤트 추출 - LocalDateTime startOfDay = LocalDate.now().atStartOfDay(); - LocalDateTime endOfDay = startOfDay.plusDays(1).minusNanos(1); - - // 선착순 이벤트 - EventType eventType = eventTypeRepository.findById(1L).orElseThrow(() -> new EventTypeNotExistsException("이벤트 타입이 존재하지 않습니다.")); - Event findEvent = eventRepository.findFirstByEventTypeAndStartAtBetween(eventType, startOfDay, endOfDay); - - if(findEvent == null) { - log.warn("오늘 날짜에 이벤트가 없습니다."); - ArrivalEventReleaseServiceRedisImpl.setMaxArrival(0); - ArrivalEventReleaseServiceJavaImpl.setMaxArrival(0); - return; - } - - // 찾은 이벤트에 해당하는 reward개수 조회 - int rewardCount = 0; - List eventRewards = eventRewardRepository.findAllByEvent(findEvent); - for (EventReward eventReward : eventRewards) { - rewardCount += eventReward.getNumWinners(); - } - - // 찾은 이벤트에 해당하는 반복 시간 확인 - LocalTime repeatTime = findEvent.getRepeatTime(); - - // 찾은 이벤트에 해당하는 반복 요일 확인 및 저장 - String repeatDate = findEvent.getRepeatDay(); - int today = startOfDay.getDayOfWeek().getValue(); - if(repeatDate.length() >= 7 && repeatDate.charAt(today - 1) == '1') { - ArrivalEventReleaseServiceRedisImpl.setStartDate(true); - ArrivalEventReleaseServiceJavaImpl.setStartDate(true); - }else{ - ArrivalEventReleaseServiceRedisImpl.setStartDate(false); - ArrivalEventReleaseServiceJavaImpl.setStartDate(false); - } - - // service에 이벤트 내용 저장 - ArrivalEventReleaseServiceRedisImpl.setMaxArrival(rewardCount); - ArrivalEventReleaseServiceJavaImpl.setMaxArrival(rewardCount); - - ArrivalEventReleaseServiceRedisImpl.setStartTime(LocalDateTime.of(LocalDate.now(), repeatTime)); - ArrivalEventReleaseServiceJavaImpl.setStartTime(LocalDateTime.of(LocalDate.now(), repeatTime)); - } -} diff --git a/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventReleaseService.java b/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventReleaseService.java index 524287c..b3e1b5c 100644 --- a/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventReleaseService.java +++ b/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventReleaseService.java @@ -10,5 +10,19 @@ public interface ArrivalEventReleaseService { CompletableFuture applyEvent(AuthInfo authInfo); - public LocalDateTime getStartTime(); + void setMaxArrival(int val); + + void setStartTime(LocalDateTime val); + + void setStartDate(Boolean val); + + int getMaxArrival(); + + void setCheckFlag(boolean flag); + + LocalDateTime getStartTime(); + + LocalDateTime getStartTimeStatic(); + + boolean getStartDate(); } diff --git a/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventService.java b/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventService.java index d59264d..b57456f 100644 --- a/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventService.java +++ b/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventService.java @@ -1,14 +1,25 @@ package com.softeer.podoarrival.event.service; +import com.softeer.podoarrival.event.exception.EventTypeNotExistsException; import com.softeer.podoarrival.event.model.dto.ArrivalApplicationResponseDto; +import com.softeer.podoarrival.event.model.entity.Event; +import com.softeer.podoarrival.event.model.entity.EventReward; +import com.softeer.podoarrival.event.model.entity.EventType; +import com.softeer.podoarrival.event.repository.EventRepository; +import com.softeer.podoarrival.event.repository.EventRewardRepository; +import com.softeer.podoarrival.event.repository.EventTypeRepository; import com.softeer.podoarrival.security.AuthInfo; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import reactor.core.publisher.Flux; import java.time.Duration; +import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.LocalTime; +import java.util.List; import java.util.concurrent.CompletableFuture; @Slf4j @@ -17,6 +28,9 @@ public class ArrivalEventService { private final ArrivalEventReleaseService arrivalEventReleaseServiceRedisImpl; + private final EventTypeRepository eventTypeRepository; + private final EventRewardRepository eventRewardRepository; + private final EventRepository eventRepository; /** * 선착순 응모용 service. @@ -42,4 +56,52 @@ public Flux streamLeftSecondsToEventTime() { return Math.max(seconds, 0); }); } + + @Scheduled(cron = "0 25 03 * * *") + public void setArrivalEventInformation() { + // Check Flag 초기화 + arrivalEventReleaseServiceRedisImpl.setCheckFlag(false); + + // START DATE, START TIME 초기화 + arrivalEventReleaseServiceRedisImpl.setStartDate(true); + arrivalEventReleaseServiceRedisImpl.setStartTime(LocalDateTime.of(LocalDate.now(), LocalTime.of(13, 0))); + + // 시작일자, 이벤트 종류만 고려하여 이벤트 추출 + LocalDateTime startOfDay = LocalDate.now().atStartOfDay(); + LocalDateTime endOfDay = startOfDay.plusDays(1).minusNanos(1); + + // 선착순 이벤트 + EventType eventType = eventTypeRepository.findById(1L).orElseThrow(() -> new EventTypeNotExistsException("이벤트 타입이 존재하지 않습니다.")); + Event findEvent = eventRepository.findFirstByEventTypeAndStartAtBetween(eventType, startOfDay, endOfDay); + + if(findEvent == null) { + log.warn("오늘 날짜에 이벤트가 없습니다."); + arrivalEventReleaseServiceRedisImpl.setMaxArrival(0); + return; + } + + // 찾은 이벤트에 해당하는 reward개수 조회 + int rewardCount = 0; + List eventRewards = eventRewardRepository.findAllByEvent(findEvent); + for (EventReward eventReward : eventRewards) { + rewardCount += eventReward.getNumWinners(); + } + + // 찾은 이벤트에 해당하는 반복 시간 확인 + LocalTime repeatTime = findEvent.getRepeatTime(); + + // 찾은 이벤트에 해당하는 반복 요일 확인 및 저장 + String repeatDate = findEvent.getRepeatDay(); + int today = startOfDay.getDayOfWeek().getValue(); + if(repeatDate.length() >= 7 && repeatDate.charAt(today - 1) == '1') { + arrivalEventReleaseServiceRedisImpl.setStartDate(true); + }else{ + arrivalEventReleaseServiceRedisImpl.setStartDate(false); + } + + // service에 이벤트 내용 저장 + arrivalEventReleaseServiceRedisImpl.setMaxArrival(rewardCount); + + arrivalEventReleaseServiceRedisImpl.setStartTime(LocalDateTime.of(LocalDate.now(), repeatTime)); + } } From 764bab069a5620a3e5824f521f2663d7632ec1a9 Mon Sep 17 00:00:00 2001 From: eckrin Date: Mon, 26 Aug 2024 12:06:38 +0900 Subject: [PATCH 2/5] =?UTF-8?q?feat:=20=ED=94=8C=EB=9E=98=EA=B7=B8=20non-s?= =?UTF-8?q?tatic=20=EB=B3=80=EC=88=98=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ArrivalEventReleaseServiceJavaImpl.java | 24 +++++++++++-------- .../ArrivalEventReleaseServiceRedisImpl.java | 22 ++++++++++------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventReleaseServiceJavaImpl.java b/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventReleaseServiceJavaImpl.java index 3cf82f9..808048f 100644 --- a/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventReleaseServiceJavaImpl.java +++ b/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventReleaseServiceJavaImpl.java @@ -28,10 +28,10 @@ public class ArrivalEventReleaseServiceJavaImpl implements ArrivalEventReleaseSe private final ArrivalUserRepository arrivalUserRepository; - private static int MAX_ARRIVAL = 100; // default + private int MAX_ARRIVAL = 100; // default private boolean CHECK = false; - private static LocalDateTime START_TIME = LocalDateTime.of(LocalDate.now(), LocalTime.of(0, 0)); - private static boolean START_DATE = true; + private LocalDateTime START_TIME = LocalDateTime.of(LocalDate.now(), LocalTime.of(13, 0)); + private boolean START_DATE = true; private static AtomicInteger count = new AtomicInteger(1); private static ConcurrentHashMap hashMap = new ConcurrentHashMap<>(); @@ -77,32 +77,36 @@ public CompletableFuture applyEvent(AuthInfo auth }); } - public static void setMaxArrival(int val) { + public void setMaxArrival(int val) { MAX_ARRIVAL = val; } - public static void setStartTime(LocalDateTime val) { + public void setStartTime(LocalDateTime val) { START_TIME = val; } - public static void setStartDate(Boolean val) { + public void setStartDate(Boolean val) { START_DATE = val; } - public static int getMaxArrival() { - return MAX_ARRIVAL; + public void setCheckFlag(boolean flag) { + CHECK = flag; } + public int getMaxArrival() { + return MAX_ARRIVAL; + } public LocalDateTime getStartTime() { return START_TIME; } - public static LocalDateTime getStartTimeStatic(){ + public LocalDateTime getStartTimeStatic(){ return START_TIME; } - public static boolean getStartDate() { + public boolean getStartDate() { return START_DATE; } + } diff --git a/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventReleaseServiceRedisImpl.java b/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventReleaseServiceRedisImpl.java index d0dedbd..51ef67f 100644 --- a/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventReleaseServiceRedisImpl.java +++ b/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventReleaseServiceRedisImpl.java @@ -34,9 +34,9 @@ public class ArrivalEventReleaseServiceRedisImpl implements ArrivalEventReleaseS private final String ARRIVAL_SET = "arrivalset"; private boolean CHECK = false; - private static int MAX_ARRIVAL = 100; // default - private static LocalDateTime START_TIME = LocalDateTime.of(LocalDate.now(), LocalTime.of(0, 0)); - private static boolean START_DATE = true; + private int MAX_ARRIVAL = 100; // default + private LocalDateTime START_TIME = LocalDateTime.of(LocalDate.now(), LocalTime.of(13, 0)); + private boolean START_DATE = true; /** * 비동기로 Redis 호출하는 메서드 @@ -87,19 +87,23 @@ public CompletableFuture applyEvent(AuthInfo auth }); } - public static void setMaxArrival(int val) { + public void setMaxArrival(int val) { MAX_ARRIVAL = val; } - public static void setStartTime(LocalDateTime val) { + public void setStartTime(LocalDateTime val) { START_TIME = val; } - public static void setStartDate(Boolean val) { + public void setStartDate(Boolean val) { START_DATE = val; } - public static int getMaxArrival() { + public void setCheckFlag(boolean flag) { + CHECK = flag; + } + + public int getMaxArrival() { return MAX_ARRIVAL; } @@ -107,11 +111,11 @@ public LocalDateTime getStartTime() { return START_TIME; } - public static LocalDateTime getStartTimeStatic(){ + public LocalDateTime getStartTimeStatic(){ return START_TIME; } - public static boolean getStartDate() { + public boolean getStartDate() { return START_DATE; } } From 4ab84dbc5f920253654429cbb7b13de2ef8ac941 Mon Sep 17 00:00:00 2001 From: eckrin Date: Mon, 26 Aug 2024 12:06:57 +0900 Subject: [PATCH 3/5] =?UTF-8?q?test:=20=EA=B4=80=EB=A0=A8=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../event/ArrivalEventServiceTest.java | 27 +++++++- .../unit/base/ArrivalEventBase.java | 2 +- .../base/ArrivalEventInformationBase.java | 14 ++++- .../event/ArrivalEventComplexServiceTest.java | 16 ++++- .../ArrivalEventInformationTest.java | 63 +++++++++++++++---- 5 files changed, 103 insertions(+), 19 deletions(-) diff --git a/src/test/java/com/softeer/podoarrival/integration/event/ArrivalEventServiceTest.java b/src/test/java/com/softeer/podoarrival/integration/event/ArrivalEventServiceTest.java index 6a840ea..79e3398 100644 --- a/src/test/java/com/softeer/podoarrival/integration/event/ArrivalEventServiceTest.java +++ b/src/test/java/com/softeer/podoarrival/integration/event/ArrivalEventServiceTest.java @@ -4,6 +4,9 @@ import com.softeer.podoarrival.event.model.dto.ArrivalApplicationResponseDto; import com.softeer.podoarrival.event.model.entity.Role; import com.softeer.podoarrival.event.repository.ArrivalUserRepository; +import com.softeer.podoarrival.event.repository.EventRepository; +import com.softeer.podoarrival.event.repository.EventRewardRepository; +import com.softeer.podoarrival.event.repository.EventTypeRepository; import com.softeer.podoarrival.event.service.ArrivalEventReleaseServiceJavaImpl; import com.softeer.podoarrival.event.service.ArrivalEventReleaseServiceRedisImpl; import com.softeer.podoarrival.event.service.ArrivalEventService; @@ -11,6 +14,7 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.mockito.Mock; import org.mockito.MockedStatic; import org.mockito.Mockito; import org.redisson.Redisson; @@ -169,13 +173,23 @@ static class TestConfig { @Bean @Qualifier("redisEventService") public ArrivalEventService arrivalEventRedisService(ArrivalEventReleaseServiceRedisImpl arrivalEventReleaseServiceRedisImpl) { - return new ArrivalEventService(arrivalEventReleaseServiceRedisImpl); + return new ArrivalEventService( + arrivalEventReleaseServiceRedisImpl, + eventTypeRepository, + eventRewardRepository, + eventRepository + ); } @Bean @Qualifier("javaEventService") public ArrivalEventService arrivalEventJavaService(ArrivalEventReleaseServiceJavaImpl arrivalEventReleaseServiceJavaImpl) { - return new ArrivalEventService(arrivalEventReleaseServiceJavaImpl); + return new ArrivalEventService( + arrivalEventReleaseServiceJavaImpl, + eventTypeRepository, + eventRewardRepository, + eventRepository + ); } @Bean @@ -188,6 +202,15 @@ public ArrivalEventReleaseServiceJavaImpl arrivalEventReleaseServiceJavaImpl() { return new ArrivalEventReleaseServiceJavaImpl(arrivalUserRepository()); } + @MockBean + public EventTypeRepository eventTypeRepository; + + @MockBean + public EventRewardRepository eventRewardRepository; + + @MockBean + public EventRepository eventRepository; + @Bean public RedissonClient redisson() { Config config = new Config(); diff --git a/src/test/java/com/softeer/podoarrival/unit/base/ArrivalEventBase.java b/src/test/java/com/softeer/podoarrival/unit/base/ArrivalEventBase.java index c9a0c04..1698c69 100644 --- a/src/test/java/com/softeer/podoarrival/unit/base/ArrivalEventBase.java +++ b/src/test/java/com/softeer/podoarrival/unit/base/ArrivalEventBase.java @@ -40,6 +40,6 @@ public class ArrivalEventBase { @BeforeEach public void setUp() { authInfo = new AuthInfo("user", "01012345678", Role.ROLE_USER); - ArrivalEventReleaseServiceRedisImpl.setMaxArrival(5); + arrivalEventReleaseServiceRedisImpl.setMaxArrival(5); } } diff --git a/src/test/java/com/softeer/podoarrival/unit/base/ArrivalEventInformationBase.java b/src/test/java/com/softeer/podoarrival/unit/base/ArrivalEventInformationBase.java index dcede5e..f5cad35 100644 --- a/src/test/java/com/softeer/podoarrival/unit/base/ArrivalEventInformationBase.java +++ b/src/test/java/com/softeer/podoarrival/unit/base/ArrivalEventInformationBase.java @@ -5,7 +5,9 @@ import com.softeer.podoarrival.event.repository.EventRepository; import com.softeer.podoarrival.event.repository.EventRewardRepository; import com.softeer.podoarrival.event.repository.EventTypeRepository; -import com.softeer.podoarrival.event.scheduler.ArrivalEventInformationScheduler; +import com.softeer.podoarrival.event.service.ArrivalEventReleaseService; +import com.softeer.podoarrival.event.service.ArrivalEventReleaseServiceRedisImpl; +import com.softeer.podoarrival.event.service.ArrivalEventService; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.extension.ExtendWith; @@ -20,6 +22,11 @@ @ExtendWith(MockitoExtension.class) public class ArrivalEventInformationBase { + protected boolean CHECK; + protected boolean START_DATE; + protected LocalDateTime START_TIME; + protected int REWORD_COUNT; + @Mock protected EventRepository eventRepository; @@ -29,8 +36,11 @@ public class ArrivalEventInformationBase { @Mock protected EventTypeRepository eventTypeRepository; + @Mock + protected ArrivalEventReleaseServiceRedisImpl arrivalEventReleaseServiceImpl; + @InjectMocks - protected ArrivalEventInformationScheduler arrivalEventInformationScheduler; + protected ArrivalEventService arrivalEventService; protected Event eventSample; protected EventReward eventReward1; diff --git a/src/test/java/com/softeer/podoarrival/unit/event/ArrivalEventComplexServiceTest.java b/src/test/java/com/softeer/podoarrival/unit/event/ArrivalEventComplexServiceTest.java index 1187078..387bd37 100644 --- a/src/test/java/com/softeer/podoarrival/unit/event/ArrivalEventComplexServiceTest.java +++ b/src/test/java/com/softeer/podoarrival/unit/event/ArrivalEventComplexServiceTest.java @@ -3,6 +3,9 @@ import com.softeer.podoarrival.event.model.dto.ArrivalApplicationResponseDto; import com.softeer.podoarrival.event.model.entity.Role; import com.softeer.podoarrival.event.repository.ArrivalUserRepository; +import com.softeer.podoarrival.event.repository.EventRepository; +import com.softeer.podoarrival.event.repository.EventRewardRepository; +import com.softeer.podoarrival.event.repository.EventTypeRepository; import com.softeer.podoarrival.event.service.ArrivalEventReleaseServiceJavaImpl; import com.softeer.podoarrival.event.service.ArrivalEventReleaseServiceRedisImpl; import com.softeer.podoarrival.event.service.ArrivalEventService; @@ -39,6 +42,15 @@ public class ArrivalEventComplexServiceTest { @Mock private ArrivalUserRepository arrivalUserRepository; + @Mock + private EventTypeRepository eventTypeRepository; + + @Mock + private EventRewardRepository eventRewardRepository; + + @Mock + private EventRepository eventRepository; + private AuthInfo authInfo; private ArrivalApplicationResponseDto expectedResponse; @@ -53,7 +65,7 @@ public void setup() { public void testApplyEventWithRedisImplementations_Success() { // given doReturn(CompletableFuture.completedFuture(expectedResponse)).when(redisService).applyEvent(authInfo); - arrivalEventService = new ArrivalEventService(redisService); + arrivalEventService = new ArrivalEventService(redisService, eventTypeRepository, eventRewardRepository, eventRepository); // when CompletableFuture result = arrivalEventService.applyEvent(authInfo); @@ -67,7 +79,7 @@ public void testApplyEventWithRedisImplementations_Success() { public void testApplyEventWithJavaImplementations_Success() { // given doReturn(CompletableFuture.completedFuture(expectedResponse)).when(javaService).applyEvent(authInfo); - arrivalEventService = new ArrivalEventService(javaService); + arrivalEventService = new ArrivalEventService(javaService, eventTypeRepository, eventRewardRepository, eventRepository); // when CompletableFuture result = arrivalEventService.applyEvent(authInfo); diff --git a/src/test/java/com/softeer/podoarrival/unit/scheduler/ArrivalEventInformationTest.java b/src/test/java/com/softeer/podoarrival/unit/scheduler/ArrivalEventInformationTest.java index a377765..4a8245d 100644 --- a/src/test/java/com/softeer/podoarrival/unit/scheduler/ArrivalEventInformationTest.java +++ b/src/test/java/com/softeer/podoarrival/unit/scheduler/ArrivalEventInformationTest.java @@ -8,6 +8,8 @@ import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; import java.time.LocalDate; import java.time.LocalDateTime; @@ -16,7 +18,7 @@ import java.util.Optional; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; public class ArrivalEventInformationTest extends ArrivalEventInformationBase { @@ -32,6 +34,30 @@ public void setArrivalEventCountSuccess_Data_Exists() { .type("arrival") .build(); + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + CHECK = false; + return null; + } + }).when(arrivalEventReleaseServiceImpl).setCheckFlag(false); + + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + START_DATE = true; + return null; + } + }).when(arrivalEventReleaseServiceImpl).setStartDate(true); + + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + START_TIME = LocalDateTime.of(LocalDate.now(), LocalTime.of(13, 0)); + return null; + } + }).when(arrivalEventReleaseServiceImpl).setStartTime(LocalDateTime.of(LocalDate.now(), LocalTime.of(13, 0))); + when(eventTypeRepository.findById(1L)) .thenReturn(Optional.ofNullable(arrivalType)); when(eventRepository.findFirstByEventTypeAndStartAtBetween(arrivalType, startOfDay, endOfDay)) @@ -39,19 +65,32 @@ public void setArrivalEventCountSuccess_Data_Exists() { when(eventRewardRepository.findAllByEvent(eventSample)) .thenReturn(List.of(eventReward1, eventReward2)); + doAnswer(new Answer() { + @Override + public Void answer(InvocationOnMock invocation) throws Throwable { + REWORD_COUNT = 60; + return null; + } + }).when(arrivalEventReleaseServiceImpl).setMaxArrival(60); + +// when(arrivalEventReleaseServiceImpl.setMaxArrival(any())) +// .thenAnswer( +// new Answer() { +// @Override +// public Void answer(InvocationOnMock invocation) throws Throwable { +// REWORD_COUNT = (int) invocation.getArguments()[0]; +// } +// } +// ); // when - arrivalEventInformationScheduler.setArrivalEventInformation(); + arrivalEventService.setArrivalEventInformation(); // then - Assertions.assertThat(ArrivalEventReleaseServiceRedisImpl.getMaxArrival()) - .isEqualTo(60); - Assertions.assertThat(ArrivalEventReleaseServiceJavaImpl.getMaxArrival()) + Assertions.assertThat(REWORD_COUNT) .isEqualTo(60); - Assertions.assertThat(ArrivalEventReleaseServiceRedisImpl.getStartTimeStatic()) - .isEqualTo(LocalTime.of(15, 0)); - Assertions.assertThat(ArrivalEventReleaseServiceJavaImpl.getStartTimeStatic()) - .isEqualTo(LocalTime.of(15, 0)); + Assertions.assertThat(START_TIME) + .isEqualTo(LocalDateTime.of(LocalDate.now(), LocalTime.of(13, 0))); } @Test @@ -70,12 +109,12 @@ public void setArrivalEventCountSuccess_Data_Not_Exists() { .thenReturn(null); // when - arrivalEventInformationScheduler.setArrivalEventInformation(); + arrivalEventService.setArrivalEventInformation(); // then - Assertions.assertThat(ArrivalEventReleaseServiceRedisImpl.getMaxArrival()) + Assertions.assertThat(arrivalEventReleaseServiceImpl.getMaxArrival()) .isEqualTo(0); - Assertions.assertThat(ArrivalEventReleaseServiceJavaImpl.getMaxArrival()) + Assertions.assertThat(arrivalEventReleaseServiceImpl.getMaxArrival()) .isEqualTo(0); } } From 61799a2c699df7a8b701ac01624dd3f65b427110 Mon Sep 17 00:00:00 2001 From: eckrin Date: Mon, 26 Aug 2024 14:18:34 +0900 Subject: [PATCH 4/5] =?UTF-8?q?test:=20=EC=84=A0=EC=B0=A9=EC=88=9C=20?= =?UTF-8?q?=EC=9D=B4=EB=B2=A4=ED=8A=B8=20=EB=8B=A8=EC=9C=84=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../podoarrival/unit/event/ApplyArrivalEventTest.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/test/java/com/softeer/podoarrival/unit/event/ApplyArrivalEventTest.java b/src/test/java/com/softeer/podoarrival/unit/event/ApplyArrivalEventTest.java index 366ea77..f2449df 100644 --- a/src/test/java/com/softeer/podoarrival/unit/event/ApplyArrivalEventTest.java +++ b/src/test/java/com/softeer/podoarrival/unit/event/ApplyArrivalEventTest.java @@ -9,6 +9,9 @@ import org.redisson.api.BatchResult; import org.redisson.api.RSetAsync; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; import java.util.Arrays; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; @@ -39,6 +42,8 @@ public void applyArrivalEventSuccess() throws ExecutionException, InterruptedExc when(result.getResponses()).thenReturn(Arrays.asList(true, 1)); return result; }); + arrivalEventReleaseServiceRedisImpl.setStartDate(true); + arrivalEventReleaseServiceRedisImpl.setStartTime(LocalDateTime.of(LocalDate.now(), LocalTime.now())); // when CompletableFuture responseFuture = arrivalEventReleaseServiceRedisImpl.applyEvent(authInfo); @@ -62,6 +67,8 @@ public void applyArrivalEventFail_Late() throws ExecutionException, InterruptedE } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } + arrivalEventReleaseServiceRedisImpl.setStartDate(true); + arrivalEventReleaseServiceRedisImpl.setStartTime(LocalDateTime.of(LocalDate.now(), LocalTime.now())); // when CompletableFuture responseFuture = arrivalEventReleaseServiceRedisImpl.applyEvent(authInfo); @@ -92,6 +99,8 @@ public void applyArrivalEventFail_Duplicate() { when(result.getResponses()).thenReturn(Arrays.asList(false, 1)); return result; }); + arrivalEventReleaseServiceRedisImpl.setStartDate(true); + arrivalEventReleaseServiceRedisImpl.setStartTime(LocalDateTime.of(LocalDate.now(), LocalTime.now())); // when CompletableFuture responseFuture = arrivalEventReleaseServiceRedisImpl.applyEvent(authInfo); From 43f708b12a4f56594e5e8ef6e7fde51a71f69eae Mon Sep 17 00:00:00 2001 From: luna156 Date: Mon, 26 Aug 2024 14:19:44 +0900 Subject: [PATCH 5/5] =?UTF-8?q?test:=20=EC=84=A0=EC=B0=A9=EC=88=9C=20?= =?UTF-8?q?=EC=9D=B4=EB=B2=A4=ED=8A=B8=20=ED=86=B5=ED=95=A9=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 기본 시간 설정 추가 --- .../event/ArrivalEventServiceTest.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/test/java/com/softeer/podoarrival/integration/event/ArrivalEventServiceTest.java b/src/test/java/com/softeer/podoarrival/integration/event/ArrivalEventServiceTest.java index 79e3398..1963074 100644 --- a/src/test/java/com/softeer/podoarrival/integration/event/ArrivalEventServiceTest.java +++ b/src/test/java/com/softeer/podoarrival/integration/event/ArrivalEventServiceTest.java @@ -61,6 +61,12 @@ class ArrivalEventServiceTest { @Autowired private ArrivalUserRepository arrivalUserRepository; + @Autowired + private ArrivalEventReleaseServiceRedisImpl arrivalEventReleaseServiceRedis; + + @Autowired + private ArrivalEventReleaseServiceJavaImpl arrivalEventReleaseServiceJava; + @MockBean private LocalTime localTime; @@ -81,6 +87,7 @@ void redisApplyTest() throws InterruptedException { ExecutorService executorService = Executors.newFixedThreadPool(32); CountDownLatch countDownLatch = new CountDownLatch(threadCount); AtomicInteger count = new AtomicInteger(); + arrivalEventReleaseServiceRedis.setStartTime(LocalDateTime.now()); //when for (int i = 0; i < threadCount; i++) { @@ -116,6 +123,8 @@ void javaApplyTest() throws InterruptedException { ExecutorService executorService = Executors.newFixedThreadPool(32); CountDownLatch countDownLatch = new CountDownLatch(threadCount); AtomicInteger count = new AtomicInteger(); + arrivalEventReleaseServiceJava.setStartTime(LocalDateTime.now()); + //when for (int i = 0; i < threadCount; i++) { @@ -147,12 +156,7 @@ void javaApplyTest() throws InterruptedException { @DisplayName("선착순 api 시간 외 오류 테스트") void eventOutOfTimeTest() throws NoSuchFieldException, IllegalAccessException { //given - Field startDate = ArrivalEventReleaseServiceRedisImpl.class.getDeclaredField("START_DATE"); - Field startTime = ArrivalEventReleaseServiceRedisImpl.class.getDeclaredField("START_TIME"); - startDate.setAccessible(true); // private 필드를 접근 가능하도록 설정 - startTime.setAccessible(true); - startDate.set(redisEventService, true); // private 필드 값을 변경 - startTime.set(redisEventService, LocalTime.now().plusHours(1)); + arrivalEventReleaseServiceRedis.setStartTime(LocalDateTime.now().plusHours(1)); //when CompletableFuture futureResponse = redisEventService.applyEvent(