From 3b442ccc9c9f8e353f21d98e3132a0507284867f Mon Sep 17 00:00:00 2001 From: eckrin Date: Thu, 8 Aug 2024 18:13:42 +0900 Subject: [PATCH 01/13] =?UTF-8?q?chore:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BB=A4=EB=B2=84=EB=A6=AC=EC=A7=80=20=ED=88=B4=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 - jacoco --- build.gradle | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/build.gradle b/build.gradle index 27ea0df..2bd2891 100644 --- a/build.gradle +++ b/build.gradle @@ -2,8 +2,44 @@ plugins { id 'java' id 'org.springframework.boot' version '3.3.2' id 'io.spring.dependency-management' version '1.1.6' + id 'jacoco' } +jacoco { + toolVersion = "0.8.9" +} + +/** Jacoco start **/ +test { + finalizedBy jacocoTestReport +} + +jacocoTestReport { + dependsOn test + + reports { + xml.required.set(true) + html.required.set(true) + + // QueryDSL Q클래스 제외 + def Qdomains = [] + for (qPattern in "**/QA".."**/QZ") { + Qdomains.add(qPattern + "*") + } + + afterEvaluate { + classDirectories.setFrom(files(classDirectories.files.collect { + fileTree(dir: it, + exclude: [] + Qdomains) + })) + } + + xml.destination file("${buildDir}/jacoco/index.xml") + html.destination file("${buildDir}/jacoco/index.html") + } +} +/** Jacoco end **/ + group = 'com.softeer' version = '0.0.1-SNAPSHOT' @@ -47,6 +83,7 @@ dependencies { implementation 'com.nimbusds:nimbus-jose-jwt:9.37.3' // lombok annotationProcessor 'org.projectlombok:lombok' + // junit testImplementation 'org.springframework.boot:spring-boot-starter-test' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' //redission From 67c11a998c7708e93527d9dd5523137baf31e3e1 Mon Sep 17 00:00:00 2001 From: eckrin Date: Fri, 9 Aug 2024 10:02:09 +0900 Subject: [PATCH 02/13] =?UTF-8?q?test:=20QuizService=20=EB=8B=A8=EC=9C=84?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../podoarrival/event/model/entity/Quiz.java | 2 + .../domain/base/QuizServiceBase.java | 54 +++++++++++ .../domain/quiz/GetQuizListTest.java | 91 +++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 src/test/java/com/softeer/podoarrival/domain/base/QuizServiceBase.java create mode 100644 src/test/java/com/softeer/podoarrival/domain/quiz/GetQuizListTest.java diff --git a/src/main/java/com/softeer/podoarrival/event/model/entity/Quiz.java b/src/main/java/com/softeer/podoarrival/event/model/entity/Quiz.java index 657ea38..a35fb42 100644 --- a/src/main/java/com/softeer/podoarrival/event/model/entity/Quiz.java +++ b/src/main/java/com/softeer/podoarrival/event/model/entity/Quiz.java @@ -2,6 +2,7 @@ import jakarta.persistence.*; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; @@ -10,6 +11,7 @@ @Entity @Table(name = "quizzes") @Getter +@Builder @AllArgsConstructor @NoArgsConstructor public class Quiz { diff --git a/src/test/java/com/softeer/podoarrival/domain/base/QuizServiceBase.java b/src/test/java/com/softeer/podoarrival/domain/base/QuizServiceBase.java new file mode 100644 index 0000000..cff1528 --- /dev/null +++ b/src/test/java/com/softeer/podoarrival/domain/base/QuizServiceBase.java @@ -0,0 +1,54 @@ +package com.softeer.podoarrival.domain.base; + +import com.softeer.podoarrival.domain.quiz.GetQuizListTest; +import com.softeer.podoarrival.event.repository.QuizRepository; +import com.softeer.podoarrival.event.repository.RedisRepository; +import com.softeer.podoarrival.event.service.QuizService; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.time.LocalDate; + +import static org.mockito.Mockito.mockStatic; + +@ExtendWith(MockitoExtension.class) +public class QuizServiceBase { + + protected String QUESTION_KEY; + protected String[] CHOICE_KEYS; + protected String ANSWER_KEYS; + + @Mock + protected QuizRepository quizRepository; + + @Mock + protected RedisRepository redisRepository; + + @InjectMocks + protected QuizService quizService; + + protected MockedStatic localDateMock; + + @BeforeEach + public void setUp() { + QUESTION_KEY = "quiz::question"; + CHOICE_KEYS = new String[]{"quiz::choice1", "quiz::choice2", "quiz::choice3", "quiz::choice4"}; + ANSWER_KEYS = "quiz::answer"; + +// localDateMock = mockStatic(LocalDate.class); +// localDateMock.when(LocalDate::now).thenReturn(LocalDate.of(2023, 12, 2)); + } + + @AfterEach + public void tearDown() { + // Close the mocked static after each test to avoid side effects + if (localDateMock != null) { + localDateMock.close(); + } + } +} diff --git a/src/test/java/com/softeer/podoarrival/domain/quiz/GetQuizListTest.java b/src/test/java/com/softeer/podoarrival/domain/quiz/GetQuizListTest.java new file mode 100644 index 0000000..72f1533 --- /dev/null +++ b/src/test/java/com/softeer/podoarrival/domain/quiz/GetQuizListTest.java @@ -0,0 +1,91 @@ +package com.softeer.podoarrival.domain.quiz; + +import com.softeer.podoarrival.domain.base.QuizServiceBase; +import com.softeer.podoarrival.event.model.dto.GetQuizResponseDto; +import com.softeer.podoarrival.event.model.entity.Quiz; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.time.LocalDate; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.Mockito.when; + +public class GetQuizListTest extends QuizServiceBase{ + + @Test + @DisplayName("퀴즈 리스트 불러오기 (성공 - 레디스에 정보가 존재한 경우)") + public void getQuizListSuccess_CacheHit() { + // given + when(redisRepository.getValues(QUESTION_KEY)).thenReturn("질문입니다"); + when(redisRepository.getValues(CHOICE_KEYS[0])).thenReturn("선택지 1"); + when(redisRepository.getValues(CHOICE_KEYS[1])).thenReturn("선택지 2"); + when(redisRepository.getValues(CHOICE_KEYS[2])).thenReturn("선택지 3"); + when(redisRepository.getValues(CHOICE_KEYS[3])).thenReturn("선택지 4"); + when(redisRepository.getValues(ANSWER_KEYS)).thenReturn("A"); + + // when + GetQuizResponseDto result = quizService.getQuizInfo(); + + // then + assertThat(result.getQuestion()).isEqualTo("질문입니다"); + assertThat(result.getChoice1()).isEqualTo("선택지 1"); + assertThat(result.getChoice2()).isEqualTo("선택지 2"); + assertThat(result.getChoice3()).isEqualTo("선택지 3"); + assertThat(result.getChoice4()).isEqualTo("선택지 4"); + assertThat(result.getAnswer()).isEqualTo("A"); + } + + @Test + @DisplayName("퀴즈 리스트 불러오기 (성공 - 레디스에 정보가 존재하지 않은 경우)") + public void getQuizListSuccess_CacheMiss() { + // given + when(redisRepository.getValues(QUESTION_KEY)).thenReturn(null); + when(redisRepository.getValues(CHOICE_KEYS[0])).thenReturn(null); + when(redisRepository.getValues(CHOICE_KEYS[1])).thenReturn(null); + when(redisRepository.getValues(CHOICE_KEYS[2])).thenReturn(null); + when(redisRepository.getValues(CHOICE_KEYS[3])).thenReturn(null); + when(redisRepository.getValues(ANSWER_KEYS)).thenReturn(null); + when(quizRepository.findByEventDate(LocalDate.now())) + .thenReturn(Quiz.builder() + .question("질문입니다") + .choice1("선택지 1") + .choice2("선택지 2") + .choice3("선택지 3") + .choice4("선택지 4") + .answer("A") + .build() + ); + + // when + GetQuizResponseDto result = quizService.getQuizInfo(); + + // then + assertThat(result.getQuestion()).isEqualTo("질문입니다"); + assertThat(result.getChoice1()).isEqualTo("선택지 1"); + assertThat(result.getChoice2()).isEqualTo("선택지 2"); + assertThat(result.getChoice3()).isEqualTo("선택지 3"); + assertThat(result.getChoice4()).isEqualTo("선택지 4"); + assertThat(result.getAnswer()).isEqualTo("A"); + } + + @Test + @DisplayName("퀴즈 리스트 불러오기 (실패 - 데이터베이스에도 정보가 존재하지 않는 경우)") + public void getQuizListFail_CacheMiss() { + // given + when(redisRepository.getValues(QUESTION_KEY)).thenReturn(null); + when(redisRepository.getValues(CHOICE_KEYS[0])).thenReturn(null); + when(redisRepository.getValues(CHOICE_KEYS[1])).thenReturn(null); + when(redisRepository.getValues(CHOICE_KEYS[2])).thenReturn(null); + when(redisRepository.getValues(CHOICE_KEYS[3])).thenReturn(null); + when(redisRepository.getValues(ANSWER_KEYS)).thenReturn(null); + when(quizRepository.findByEventDate(LocalDate.now())) + .thenReturn(null); + + // when-then + assertThatThrownBy(() -> quizService.getQuizInfo()) + .isInstanceOf(NullPointerException.class); + } + +} From 94c8b0a604c651a9cd6e8693057896b2afd3e29a Mon Sep 17 00:00:00 2001 From: eckrin Date: Fri, 9 Aug 2024 15:05:27 +0900 Subject: [PATCH 03/13] =?UTF-8?q?test:=20ArrivalEventService=20=EB=8B=A8?= =?UTF-8?q?=EC=9C=84=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - thenAnswer을 사용하여 결과값 사용처마다 분리 --- .../service/ArrivalEventReleaseService.java | 2 +- .../event}/ArrivalEventServiceTest.java | 4 +- .../unit/base/ArrivalEventBase.java | 45 +++++++++ .../unit/event/ApplyArrivalEventTest.java | 99 +++++++++++++++++++ 4 files changed, 147 insertions(+), 3 deletions(-) rename src/test/java/com/softeer/podoarrival/{event/service => integration/event}/ArrivalEventServiceTest.java (95%) create mode 100644 src/test/java/com/softeer/podoarrival/unit/base/ArrivalEventBase.java create mode 100644 src/test/java/com/softeer/podoarrival/unit/event/ApplyArrivalEventTest.java 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 3918e9e..6202036 100644 --- a/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventReleaseService.java +++ b/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventReleaseService.java @@ -43,7 +43,6 @@ public CompletableFuture applyEvent(AuthInfo auth if(CHECK){ return new ArrivalApplicationResponseDto("선착순 응모에 실패했습니다.", -1); } - log.info("전화번호 = {}", authInfo.getPhoneNum()); RBatch batch = redissonClient.createBatch(); batch.getSet(now + ARRIVAL_SET).addAsync(authInfo.getPhoneNum()); @@ -66,6 +65,7 @@ public CompletableFuture applyEvent(AuthInfo auth .arrivalRank(grade) .build() ); + log.info("전화번호 = {}", authInfo.getPhoneNum()); return new ArrivalApplicationResponseDto("선착순 응모에 성공했습니다.", grade); } else { CHECK = true; diff --git a/src/test/java/com/softeer/podoarrival/event/service/ArrivalEventServiceTest.java b/src/test/java/com/softeer/podoarrival/integration/event/ArrivalEventServiceTest.java similarity index 95% rename from src/test/java/com/softeer/podoarrival/event/service/ArrivalEventServiceTest.java rename to src/test/java/com/softeer/podoarrival/integration/event/ArrivalEventServiceTest.java index f94df03..a5c1d8b 100644 --- a/src/test/java/com/softeer/podoarrival/event/service/ArrivalEventServiceTest.java +++ b/src/test/java/com/softeer/podoarrival/integration/event/ArrivalEventServiceTest.java @@ -1,7 +1,8 @@ -package com.softeer.podoarrival.event.service; +package com.softeer.podoarrival.integration.event; import com.softeer.podoarrival.PodoArrivalApplication; import com.softeer.podoarrival.event.model.dto.ArrivalApplicationResponseDto; +import com.softeer.podoarrival.event.service.ArrivalEventService; import com.softeer.podoarrival.security.AuthInfo; import com.softeer.podoarrival.event.model.entity.Role; import org.junit.jupiter.api.AfterEach; @@ -18,7 +19,6 @@ import static org.junit.jupiter.api.Assertions.*; - @SpringBootTest @ContextConfiguration(classes = PodoArrivalApplication.class) class ArrivalEventServiceTest { diff --git a/src/test/java/com/softeer/podoarrival/unit/base/ArrivalEventBase.java b/src/test/java/com/softeer/podoarrival/unit/base/ArrivalEventBase.java new file mode 100644 index 0000000..454a0da --- /dev/null +++ b/src/test/java/com/softeer/podoarrival/unit/base/ArrivalEventBase.java @@ -0,0 +1,45 @@ +package com.softeer.podoarrival.unit.base; + +import com.softeer.podoarrival.event.model.entity.Role; +import com.softeer.podoarrival.event.repository.ArrivalUserRepository; +import com.softeer.podoarrival.event.service.ArrivalEventReleaseService; +import com.softeer.podoarrival.security.AuthInfo; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.redisson.api.BatchResult; +import org.redisson.api.RBatch; +import org.redisson.api.RSetAsync; +import org.redisson.api.RedissonClient; + +@ExtendWith(MockitoExtension.class) +public class ArrivalEventBase { + + @Mock + protected RedissonClient redissonClient; + + @Mock + protected ArrivalUserRepository arrivalUserRepository; + + @Mock + protected RBatch batch; + + @Mock + protected RSetAsync setAsync; + + @Mock + protected BatchResult batchResult; + + @InjectMocks + protected ArrivalEventReleaseService arrivalEventReleaseService; + + protected AuthInfo authInfo; + + @BeforeEach + public void setUp() { + authInfo = new AuthInfo("user", "01012345678", Role.ROLE_USER); + ArrivalEventReleaseService.setMaxArrival(5); + } +} diff --git a/src/test/java/com/softeer/podoarrival/unit/event/ApplyArrivalEventTest.java b/src/test/java/com/softeer/podoarrival/unit/event/ApplyArrivalEventTest.java new file mode 100644 index 0000000..d75a503 --- /dev/null +++ b/src/test/java/com/softeer/podoarrival/unit/event/ApplyArrivalEventTest.java @@ -0,0 +1,99 @@ +package com.softeer.podoarrival.unit.event; + +import com.softeer.podoarrival.unit.base.ArrivalEventBase; +import com.softeer.podoarrival.event.model.dto.ArrivalApplicationResponseDto; +import com.softeer.podoarrival.event.service.ArrivalEventReleaseService; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.stubbing.Answer; +import org.redisson.api.BatchResult; +import org.redisson.api.RSetAsync; + +import java.util.Arrays; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class ApplyArrivalEventTest extends ArrivalEventBase { + + @Test + @DisplayName("선착순 이벤트 응모 (성공)") + public void applyArrivalEventSuccess() throws ExecutionException, InterruptedException { + // given + when(redissonClient.createBatch()).thenReturn(batch); + + when(batch.getSet(anyString())).thenAnswer((Answer) invocation -> { + RSetAsync result = mock(RSetAsync.class); + when(result.addAsync(anyString())).thenReturn(null); + when(result.sizeAsync()).thenReturn(null); + return result; + }); + + when(batch.execute()).thenAnswer((Answer>) invocation -> { + BatchResult result = mock(BatchResult.class); + when(result.getResponses()).thenReturn(Arrays.asList(true, 1)); + return result; + }); + + // when + CompletableFuture responseFuture = arrivalEventReleaseService.applyEvent(authInfo); + ArrivalApplicationResponseDto response = responseFuture.get(); // 비동기 결과를 기다림 + + // then + assertThat(response.getResponse()).isEqualTo("선착순 응모에 성공했습니다."); + assertThat(response.getGrade()).isEqualTo(1); + } + + @Test + @DisplayName("선착순 이벤트 응모 (실패 - 선착순 순위 안에 들지 못했을 경우)") + public void applyArrivalEventFail_Late() throws ExecutionException, InterruptedException { + // given + try { + java.lang.reflect.Field checkField = ArrivalEventReleaseService.class.getDeclaredField("CHECK"); + checkField.setAccessible(true); // private 필드 접근 허용 + checkField.set(arrivalEventReleaseService, true); // 값 설정 + } catch (NoSuchFieldException | IllegalAccessException e) { + e.printStackTrace(); + } + + // when + CompletableFuture responseFuture = arrivalEventReleaseService.applyEvent(authInfo); + ArrivalApplicationResponseDto response = responseFuture.get(); // 비동기 결과를 기다림 + + // then + assertThat(response.getResponse()).isEqualTo("선착순 응모에 실패했습니다."); + assertThat(response.getGrade()).isEqualTo(-1); + } + + @Test + @DisplayName("선착순 이벤트 응모 (실패 - 중복 참여)") + public void applyArrivalEventFail_Duplicate() { + // given + when(redissonClient.createBatch()).thenReturn(batch); + + when(batch.getSet(anyString())).thenAnswer((Answer) invocation -> { + RSetAsync result = mock(RSetAsync.class); + when(result.addAsync(anyString())).thenReturn(null); + when(result.sizeAsync()).thenReturn(null); + return result; + }); + + when(batch.execute()).thenAnswer((Answer>) invocation -> { + BatchResult result = mock(BatchResult.class); + when(result.getResponses()).thenReturn(Arrays.asList(false, 1)); + return result; + }); + + // when + CompletableFuture responseFuture = arrivalEventReleaseService.applyEvent(authInfo); + + // then + assertThatThrownBy(() -> responseFuture.get()) + .isInstanceOf(ExecutionException.class); + } +} From 65290a09a2d033cccb949e42dede659b29ff3582 Mon Sep 17 00:00:00 2001 From: eckrin Date: Fri, 9 Aug 2024 15:05:58 +0900 Subject: [PATCH 04/13] =?UTF-8?q?refactor:=20=ED=8C=A8=ED=82=A4=EC=A7=80?= =?UTF-8?q?=20=EA=B5=AC=EC=A1=B0=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../podoarrival/{domain => unit}/base/QuizServiceBase.java | 3 +-- .../podoarrival/{domain => unit}/quiz/GetQuizListTest.java | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) rename src/test/java/com/softeer/podoarrival/{domain => unit}/base/QuizServiceBase.java (93%) rename src/test/java/com/softeer/podoarrival/{domain => unit}/quiz/GetQuizListTest.java (97%) diff --git a/src/test/java/com/softeer/podoarrival/domain/base/QuizServiceBase.java b/src/test/java/com/softeer/podoarrival/unit/base/QuizServiceBase.java similarity index 93% rename from src/test/java/com/softeer/podoarrival/domain/base/QuizServiceBase.java rename to src/test/java/com/softeer/podoarrival/unit/base/QuizServiceBase.java index cff1528..28d5527 100644 --- a/src/test/java/com/softeer/podoarrival/domain/base/QuizServiceBase.java +++ b/src/test/java/com/softeer/podoarrival/unit/base/QuizServiceBase.java @@ -1,6 +1,5 @@ -package com.softeer.podoarrival.domain.base; +package com.softeer.podoarrival.unit.base; -import com.softeer.podoarrival.domain.quiz.GetQuizListTest; import com.softeer.podoarrival.event.repository.QuizRepository; import com.softeer.podoarrival.event.repository.RedisRepository; import com.softeer.podoarrival.event.service.QuizService; diff --git a/src/test/java/com/softeer/podoarrival/domain/quiz/GetQuizListTest.java b/src/test/java/com/softeer/podoarrival/unit/quiz/GetQuizListTest.java similarity index 97% rename from src/test/java/com/softeer/podoarrival/domain/quiz/GetQuizListTest.java rename to src/test/java/com/softeer/podoarrival/unit/quiz/GetQuizListTest.java index 72f1533..0d772a1 100644 --- a/src/test/java/com/softeer/podoarrival/domain/quiz/GetQuizListTest.java +++ b/src/test/java/com/softeer/podoarrival/unit/quiz/GetQuizListTest.java @@ -1,6 +1,6 @@ -package com.softeer.podoarrival.domain.quiz; +package com.softeer.podoarrival.unit.quiz; -import com.softeer.podoarrival.domain.base.QuizServiceBase; +import com.softeer.podoarrival.unit.base.QuizServiceBase; import com.softeer.podoarrival.event.model.dto.GetQuizResponseDto; import com.softeer.podoarrival.event.model.entity.Quiz; import org.junit.jupiter.api.DisplayName; From 656f0bc83fd9c18c855c2f8211fb6b3cdc029305 Mon Sep 17 00:00:00 2001 From: eckrin Date: Mon, 12 Aug 2024 15:25:38 +0900 Subject: [PATCH 05/13] =?UTF-8?q?test:=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EC=9A=A9=20=EC=9D=B8=EB=A9=94=EB=AA=A8=EB=A6=AC=20h2=20?= =?UTF-8?q?=EB=8D=B0=EC=9D=B4=ED=84=B0=EB=B2=A0=EC=9D=B4=EC=8A=A4=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/resources/application.yml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/test/resources/application.yml diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml new file mode 100644 index 0000000..d5622a4 --- /dev/null +++ b/src/test/resources/application.yml @@ -0,0 +1,17 @@ +spring: + datasource: + driver-class-name: org.h2.Driver + url: jdbc:h2:mem:test;MODE=MYSQL;DATABASE_TO_UPPER=false + username: sa + password: + jpa: + open-in-view: true + hibernate: + ddl-auto: create + show-sql: true + database: h2 + database-platform: org.hibernate.dialect.H2Dialect + +secret: + jwt: ${SECRET_JWT} + redis-url: ${REDIS_URL} \ No newline at end of file From 5796686dbe1dd3d77795e08a27a8772abf644e06 Mon Sep 17 00:00:00 2001 From: eckrin Date: Mon, 12 Aug 2024 15:26:32 +0900 Subject: [PATCH 06/13] =?UTF-8?q?test:=20redis=EC=97=90=20=ED=80=B4?= =?UTF-8?q?=EC=A6=88=20=EB=A1=9C=EB=94=A9=EC=9D=84=20=EC=9C=84=ED=95=9C=20?= =?UTF-8?q?=EC=8A=A4=EC=BC=80=EC=A4=84=EB=9F=AC=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../unit/base/DailyQuizSchedulerBase.java | 37 ++++++++++++++++ .../scheduler/DailyQuizSchedulerTest.java | 44 +++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 src/test/java/com/softeer/podoarrival/unit/base/DailyQuizSchedulerBase.java create mode 100644 src/test/java/com/softeer/podoarrival/unit/scheduler/DailyQuizSchedulerTest.java diff --git a/src/test/java/com/softeer/podoarrival/unit/base/DailyQuizSchedulerBase.java b/src/test/java/com/softeer/podoarrival/unit/base/DailyQuizSchedulerBase.java new file mode 100644 index 0000000..458eabb --- /dev/null +++ b/src/test/java/com/softeer/podoarrival/unit/base/DailyQuizSchedulerBase.java @@ -0,0 +1,37 @@ +package com.softeer.podoarrival.unit.base; + +import com.softeer.podoarrival.event.repository.QuizRepository; +import com.softeer.podoarrival.event.repository.RedisRepository; +import com.softeer.podoarrival.event.scheduler.DailyQuizScheduler; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.time.Clock; + +@ExtendWith(MockitoExtension.class) +public class DailyQuizSchedulerBase { + + @Mock + protected Clock clock; + + @Mock + protected RedisRepository redisRepository; + + @Mock + protected QuizRepository quizRepository; + + @InjectMocks + protected DailyQuizScheduler quizScheduler; + + @BeforeEach + public void setUp() { + } + + @AfterEach + public void tearDown() { + } +} diff --git a/src/test/java/com/softeer/podoarrival/unit/scheduler/DailyQuizSchedulerTest.java b/src/test/java/com/softeer/podoarrival/unit/scheduler/DailyQuizSchedulerTest.java new file mode 100644 index 0000000..7305892 --- /dev/null +++ b/src/test/java/com/softeer/podoarrival/unit/scheduler/DailyQuizSchedulerTest.java @@ -0,0 +1,44 @@ +package com.softeer.podoarrival.unit.scheduler; + +import com.softeer.podoarrival.event.model.entity.Quiz; +import com.softeer.podoarrival.unit.base.DailyQuizSchedulerBase; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.time.LocalDate; + +import static org.mockito.Mockito.when; + +public class DailyQuizSchedulerTest extends DailyQuizSchedulerBase { + + @Test + @DisplayName("퀴즈 스케줄러 동작 테스트 (성공 - Mysql에 데이터가 존재하는 경우)") + public void getQuizListSuccess_CacheHit() { + // given + when(quizRepository.findByEventDate(LocalDate.now())) + .thenReturn(Quiz.builder() + .id(1L) + .question("질문입니다.") + .choice1("선택지 1") + .choice2("선택지 2") + .choice3("선택지 3") + .choice4("선택지 4") + .answer("A") + .build() + ); + + // when + quizScheduler.setDailyQuiz(); + } + + @Test + @DisplayName("퀴즈 스케줄러 동작 테스트 (성공 - Mysql에 데이터가 존재하지 않은 경우)") + public void getQuizListFail_CacheMiss() { + // given + when(quizRepository.findByEventDate(LocalDate.now())) + .thenReturn(null); + + // when + quizScheduler.setDailyQuiz(); + } +} From 2df52ba499c03851197558700db8627e2649e399 Mon Sep 17 00:00:00 2001 From: eckrin Date: Mon, 12 Aug 2024 15:26:57 +0900 Subject: [PATCH 07/13] =?UTF-8?q?chore:=20gradle=20=EC=9D=98=EC=A1=B4?= =?UTF-8?q?=EC=84=B1=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 ++ 1 file changed, 2 insertions(+) diff --git a/build.gradle b/build.gradle index 2bd2891..9f8618d 100644 --- a/build.gradle +++ b/build.gradle @@ -86,6 +86,8 @@ dependencies { // junit testImplementation 'org.springframework.boot:spring-boot-starter-test' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' + // test h2 database + testRuntimeOnly 'com.h2database:h2' //redission implementation 'org.redisson:redisson-spring-boot-starter:3.33.0' implementation "org.redisson:redisson-spring-data-27:3.33.0" From 7789e7afdb5eccbab9b20a252e6b9daba9af3d43 Mon Sep 17 00:00:00 2001 From: eckrin Date: Mon, 12 Aug 2024 15:45:37 +0900 Subject: [PATCH 08/13] =?UTF-8?q?test:=20=ED=86=B5=ED=95=A9=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=EC=8B=9C=20flushing=20=EB=8C=80=EC=8B=A0=20?= =?UTF-8?q?=EC=84=A0=EC=B0=A9=EC=88=9C=20=EA=B8=B0=EB=8A=A5=EC=9D=84=20?= =?UTF-8?q?=EC=9C=84=ED=95=9C=20=ED=82=A4=EB=93=A4=EB=A7=8C=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../integration/event/ArrivalEventServiceTest.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 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 a5c1d8b..3043d57 100644 --- a/src/test/java/com/softeer/podoarrival/integration/event/ArrivalEventServiceTest.java +++ b/src/test/java/com/softeer/podoarrival/integration/event/ArrivalEventServiceTest.java @@ -2,22 +2,21 @@ import com.softeer.podoarrival.PodoArrivalApplication; import com.softeer.podoarrival.event.model.dto.ArrivalApplicationResponseDto; +import com.softeer.podoarrival.event.model.entity.Role; import com.softeer.podoarrival.event.service.ArrivalEventService; import com.softeer.podoarrival.security.AuthInfo; -import com.softeer.podoarrival.event.model.entity.Role; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.redisson.api.RedissonClient; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ContextConfiguration; import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicInteger; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; @SpringBootTest @ContextConfiguration(classes = PodoArrivalApplication.class) @@ -31,12 +30,11 @@ class ArrivalEventServiceTest { @AfterEach void tearDown() { - redissonClient.getKeys().flushdb(); + redissonClient.getKeys().deleteByPattern("*arrivalset"); redissonClient.shutdown(); } - @Value("${MAX_COUNT}") - private int MAX_COUNT; + private int MAX_COUNT = 100; @Test @DisplayName("선착순 api 정확도 테스트") @@ -70,7 +68,6 @@ void applicationTest() throws InterruptedException { countDownLatch.await(); //then - redissonClient.getSet("arrivalset").clear(); assertEquals(MAX_COUNT, count.get()); } } \ No newline at end of file From 7435eece8e25f8182adc75f8bb1c185c6c42958a Mon Sep 17 00:00:00 2001 From: eckrin Date: Mon, 12 Aug 2024 15:48:55 +0900 Subject: [PATCH 09/13] =?UTF-8?q?fix:=20=EB=B9=84=EB=8F=99=EA=B8=B0=20?= =?UTF-8?q?=ED=98=B8=EC=B6=9C=EC=9E=90=EC=97=90=20=ED=8A=B8=EB=9E=9C?= =?UTF-8?q?=EC=9E=AD=EC=85=98=20=EC=84=A0=EC=96=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../podoarrival/event/service/ArrivalEventService.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) 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 4b7e4b5..2a6f99d 100644 --- a/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventService.java +++ b/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventService.java @@ -1,25 +1,22 @@ package com.softeer.podoarrival.event.service; -import com.softeer.podoarrival.common.response.CommonResponse; -import com.softeer.podoarrival.event.exception.AsyncRequestExecuteException; import com.softeer.podoarrival.event.model.dto.ArrivalApplicationResponseDto; import com.softeer.podoarrival.security.AuthInfo; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.concurrent.*; +import java.util.concurrent.CompletableFuture; @Slf4j @Service @RequiredArgsConstructor public class ArrivalEventService { - private final ExecutorService executorService; private final ArrivalEventReleaseService arrivalEventReleaseService; + @Transactional public CompletableFuture applyEvent(AuthInfo authInfo) { return arrivalEventReleaseService.applyEvent(authInfo); } From 5cfeb5f42bab601096988c1b691189f780cb4d35 Mon Sep 17 00:00:00 2001 From: eckrin Date: Mon, 12 Aug 2024 15:51:27 +0900 Subject: [PATCH 10/13] =?UTF-8?q?fix:=20=EB=B9=84=EB=8F=99=EA=B8=B0=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EC=8B=A4=ED=96=89=20=EC=A4=91=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=EA=B0=80=20=EB=B0=9C=EC=83=9D=ED=95=A0=20=EA=B2=BD?= =?UTF-8?q?=EC=9A=B0=20=EC=98=88=EC=99=B8=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 1. exceptionally를 통해서 예외를 catch - 2. @ControllerAdvice에서 잡을 수 있는 예외로 변환하여 throw - 3. ExceptionHandler가 잡아서 처리 --- .../event/controller/ArrivalEventController.java | 12 ++++++------ .../ArrivalEventMaxArrivalScheduler.java | 2 +- .../event/service/ArrivalEventReleaseService.java | 8 ++++---- 3 files changed, 11 insertions(+), 11 deletions(-) rename src/main/java/com/softeer/podoarrival/event/scheduler/{scheduler => }/ArrivalEventMaxArrivalScheduler.java (97%) diff --git a/src/main/java/com/softeer/podoarrival/event/controller/ArrivalEventController.java b/src/main/java/com/softeer/podoarrival/event/controller/ArrivalEventController.java index 63b0b72..a61d0ca 100644 --- a/src/main/java/com/softeer/podoarrival/event/controller/ArrivalEventController.java +++ b/src/main/java/com/softeer/podoarrival/event/controller/ArrivalEventController.java @@ -2,10 +2,9 @@ import com.softeer.podoarrival.common.response.CommonResponse; -import com.softeer.podoarrival.common.response.ErrorCode; import com.softeer.podoarrival.event.exception.AsyncRequestExecuteException; +import com.softeer.podoarrival.event.exception.ExistingUserException; import com.softeer.podoarrival.event.model.dto.ArrivalApplicationResponseDto; -import com.softeer.podoarrival.event.service.ArrivalEventReleaseService; import com.softeer.podoarrival.event.service.ArrivalEventService; import com.softeer.podoarrival.security.Auth; import com.softeer.podoarrival.security.AuthInfo; @@ -33,10 +32,11 @@ public CompletableFuture> arrivalE return arrivalEventService.applyEvent(authInfo) .thenApply(result -> new CommonResponse<>(result)) .exceptionally(ex -> { - log.error("비동기 처리 중 오류 발생", ex); - // 예외 발생 시 적절한 오류 응답을 반환 - throw new AsyncRequestExecuteException("선착순 요청 중 서버 오류 발생"); + if(ex.getCause() instanceof ExistingUserException) { + throw new ExistingUserException("[비동기 에러] 유저가 이미 존재합니다."); + } else { + throw new AsyncRequestExecuteException("[비동기 에러] 선착순 요청 중 서버 오류 발생"); + } }); } - } diff --git a/src/main/java/com/softeer/podoarrival/event/scheduler/scheduler/ArrivalEventMaxArrivalScheduler.java b/src/main/java/com/softeer/podoarrival/event/scheduler/ArrivalEventMaxArrivalScheduler.java similarity index 97% rename from src/main/java/com/softeer/podoarrival/event/scheduler/scheduler/ArrivalEventMaxArrivalScheduler.java rename to src/main/java/com/softeer/podoarrival/event/scheduler/ArrivalEventMaxArrivalScheduler.java index ae43f56..f6154a0 100644 --- a/src/main/java/com/softeer/podoarrival/event/scheduler/scheduler/ArrivalEventMaxArrivalScheduler.java +++ b/src/main/java/com/softeer/podoarrival/event/scheduler/ArrivalEventMaxArrivalScheduler.java @@ -1,4 +1,4 @@ -package com.softeer.podoarrival.event.scheduler.scheduler; +package com.softeer.podoarrival.event.scheduler; import com.softeer.podoarrival.event.exception.EventTypeNotExistsException; import com.softeer.podoarrival.event.model.entity.Event; 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 6202036..ed492b9 100644 --- a/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventReleaseService.java +++ b/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventReleaseService.java @@ -29,7 +29,7 @@ public class ArrivalEventReleaseService { private final String ARRIVAL_SET = "arrivalset"; private boolean CHECK = false; - private static int MAX_ARRIVAL = 0; + private static int MAX_ARRIVAL = 100; // default /** * 비동기로 Redis 호출하는 메서드 @@ -38,15 +38,15 @@ public class ArrivalEventReleaseService { @Async("arrivalExecutor") public CompletableFuture applyEvent(AuthInfo authInfo) { return CompletableFuture.supplyAsync(() -> { - LocalDate now = LocalDate.now(); + String redisKey = LocalDate.now() + ARRIVAL_SET; if(CHECK){ return new ArrivalApplicationResponseDto("선착순 응모에 실패했습니다.", -1); } RBatch batch = redissonClient.createBatch(); - batch.getSet(now + ARRIVAL_SET).addAsync(authInfo.getPhoneNum()); - batch.getSet(now + ARRIVAL_SET).sizeAsync(); + batch.getSet(redisKey).addAsync(authInfo.getPhoneNum()); + batch.getSet(redisKey).sizeAsync(); BatchResult res = batch.execute(); //첫번째 응답 From 41113d41e13a0d04e3a7158f0ae737becbec6ce9 Mon Sep 17 00:00:00 2001 From: eckrin Date: Mon, 12 Aug 2024 16:27:39 +0900 Subject: [PATCH 11/13] =?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=EC=B5=9C=EB=8C=80=EC=9D=B8?= =?UTF-8?q?=EC=9B=90=20=EC=A0=9C=ED=95=9C=EC=9D=84=20=EC=84=B8=ED=8C=85?= =?UTF-8?q?=ED=95=98=EB=8A=94=20=EC=8A=A4=EC=BC=80=EC=A4=84=EB=9F=AC=20?= =?UTF-8?q?=EB=8B=A8=EC=9C=84=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 불필요한 stubbing 제거 - MAX_ARRIVAL getter 생성 --- .../event/model/entity/EventType.java | 2 - .../service/ArrivalEventReleaseService.java | 4 ++ .../ArrivalEventMaxCountSchedulerBase.java | 36 ++++++++++ .../unit/base/DailyQuizSchedulerBase.java | 5 -- .../ArrivalEventMaxCountSchedulerTest.java | 67 +++++++++++++++++++ 5 files changed, 107 insertions(+), 7 deletions(-) create mode 100644 src/test/java/com/softeer/podoarrival/unit/base/ArrivalEventMaxCountSchedulerBase.java create mode 100644 src/test/java/com/softeer/podoarrival/unit/scheduler/ArrivalEventMaxCountSchedulerTest.java diff --git a/src/main/java/com/softeer/podoarrival/event/model/entity/EventType.java b/src/main/java/com/softeer/podoarrival/event/model/entity/EventType.java index 35e01ad..35e5a80 100644 --- a/src/main/java/com/softeer/podoarrival/event/model/entity/EventType.java +++ b/src/main/java/com/softeer/podoarrival/event/model/entity/EventType.java @@ -4,12 +4,10 @@ import jakarta.persistence.*; import lombok.AllArgsConstructor; import lombok.Builder; -import lombok.Data; import lombok.NoArgsConstructor; @Entity @Table(name = "event_types") -@Data @Builder @AllArgsConstructor @NoArgsConstructor 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 ed492b9..52b161f 100644 --- a/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventReleaseService.java +++ b/src/main/java/com/softeer/podoarrival/event/service/ArrivalEventReleaseService.java @@ -77,4 +77,8 @@ public CompletableFuture applyEvent(AuthInfo auth public static void setMaxArrival(int val) { MAX_ARRIVAL = val; } + + public static int getMaxArrival() { + return MAX_ARRIVAL; + } } diff --git a/src/test/java/com/softeer/podoarrival/unit/base/ArrivalEventMaxCountSchedulerBase.java b/src/test/java/com/softeer/podoarrival/unit/base/ArrivalEventMaxCountSchedulerBase.java new file mode 100644 index 0000000..d92ea90 --- /dev/null +++ b/src/test/java/com/softeer/podoarrival/unit/base/ArrivalEventMaxCountSchedulerBase.java @@ -0,0 +1,36 @@ +package com.softeer.podoarrival.unit.base; + +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.ArrivalEventMaxArrivalScheduler; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +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 ArrivalEventMaxCountSchedulerBase { + + @Mock + protected EventRepository eventRepository; + + @Mock + protected EventRewardRepository eventRewardRepository; + + @Mock + protected EventTypeRepository eventTypeRepository; + + @InjectMocks + protected ArrivalEventMaxArrivalScheduler arrivalEventMaxArrivalScheduler; + + @BeforeEach + public void setUp() { + } + + @AfterEach + public void tearDown() { + } +} diff --git a/src/test/java/com/softeer/podoarrival/unit/base/DailyQuizSchedulerBase.java b/src/test/java/com/softeer/podoarrival/unit/base/DailyQuizSchedulerBase.java index 458eabb..94ff9bb 100644 --- a/src/test/java/com/softeer/podoarrival/unit/base/DailyQuizSchedulerBase.java +++ b/src/test/java/com/softeer/podoarrival/unit/base/DailyQuizSchedulerBase.java @@ -10,14 +10,9 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import java.time.Clock; - @ExtendWith(MockitoExtension.class) public class DailyQuizSchedulerBase { - @Mock - protected Clock clock; - @Mock protected RedisRepository redisRepository; diff --git a/src/test/java/com/softeer/podoarrival/unit/scheduler/ArrivalEventMaxCountSchedulerTest.java b/src/test/java/com/softeer/podoarrival/unit/scheduler/ArrivalEventMaxCountSchedulerTest.java new file mode 100644 index 0000000..5b0edaf --- /dev/null +++ b/src/test/java/com/softeer/podoarrival/unit/scheduler/ArrivalEventMaxCountSchedulerTest.java @@ -0,0 +1,67 @@ +package com.softeer.podoarrival.unit.scheduler; + +import com.softeer.podoarrival.event.model.entity.Event; +import com.softeer.podoarrival.event.model.entity.EventType; +import com.softeer.podoarrival.event.service.ArrivalEventReleaseService; +import com.softeer.podoarrival.unit.base.ArrivalEventMaxCountSchedulerBase; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.Optional; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +public class ArrivalEventMaxCountSchedulerTest extends ArrivalEventMaxCountSchedulerBase { + + @Test + @DisplayName("이벤트 최대인원 세팅 스케줄러 동작 테스트 (성공 - Mysql에 데이터가 존재하는 경우)") + public void setArrivalEventCountSuccess_Data_Exists() { + // given + when(eventTypeRepository.findById(1L)) + .thenReturn(Optional.ofNullable(EventType.builder() + .id(1L) + .type("arrival") + .build()) + ); + when(eventRepository.findFirstByEventTypeAndStartAtBetween(any(), any(), any())) + .thenReturn(Event.builder() + .build() + ); + when(eventRewardRepository.countByEvent(any())) + .thenReturn(50); + + // when + arrivalEventMaxArrivalScheduler.setEventArrivalCount(); + + // then + Assertions.assertThat(ArrivalEventReleaseService.getMaxArrival()) + .isEqualTo(50); + } + + @Test + @DisplayName("이벤트 최대인원 세팅 스케줄러 동작 테스트 (성공 - Mysql에 데이터가 존재하지 않는 경우)") + public void setArrivalEventCountSuccess_Data_Not_Exists() { + // given + when(eventTypeRepository.findById(1L)) + .thenReturn(Optional.ofNullable(EventType.builder() + .id(1L) + .type("arrival") + .build()) + ); + when(eventRepository.findFirstByEventTypeAndStartAtBetween(any(), any(), any())) + .thenReturn(null); + when(eventRewardRepository.countByEvent(any())) + .thenReturn(0); + + // when + arrivalEventMaxArrivalScheduler.setEventArrivalCount(); + + // then + Assertions.assertThat(ArrivalEventReleaseService.getMaxArrival()) + .isEqualTo(0); + } +} From 48415a328f2aa12676fd387010b93e78aca62608 Mon Sep 17 00:00:00 2001 From: eckrin Date: Mon, 12 Aug 2024 17:25:34 +0900 Subject: [PATCH 12/13] =?UTF-8?q?test:=20=ED=98=84=EC=9E=AC=20=EC=8B=9C?= =?UTF-8?q?=EA=B0=84=20=EB=B0=8F=20=EC=9D=B4=EB=B2=A4=ED=8A=B8=20=EA=B0=9D?= =?UTF-8?q?=EC=B2=B4=20=EC=A3=BC=EC=9E=85=EC=A1=B0=EA=B1=B4=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 --- .../ArrivalEventMaxCountSchedulerTest.java | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/test/java/com/softeer/podoarrival/unit/scheduler/ArrivalEventMaxCountSchedulerTest.java b/src/test/java/com/softeer/podoarrival/unit/scheduler/ArrivalEventMaxCountSchedulerTest.java index 5b0edaf..d61547c 100644 --- a/src/test/java/com/softeer/podoarrival/unit/scheduler/ArrivalEventMaxCountSchedulerTest.java +++ b/src/test/java/com/softeer/podoarrival/unit/scheduler/ArrivalEventMaxCountSchedulerTest.java @@ -20,15 +20,23 @@ public class ArrivalEventMaxCountSchedulerTest extends ArrivalEventMaxCountSched @Test @DisplayName("이벤트 최대인원 세팅 스케줄러 동작 테스트 (성공 - Mysql에 데이터가 존재하는 경우)") public void setArrivalEventCountSuccess_Data_Exists() { + LocalDateTime startOfDay = LocalDate.now().atStartOfDay(); + LocalDateTime endOfDay = startOfDay.plusDays(1).minusNanos(1); // given + EventType arrivalType = EventType.builder() + .id(1L) + .type("arrival") + .build(); + when(eventTypeRepository.findById(1L)) - .thenReturn(Optional.ofNullable(EventType.builder() - .id(1L) - .type("arrival") - .build()) - ); - when(eventRepository.findFirstByEventTypeAndStartAtBetween(any(), any(), any())) + .thenReturn(Optional.ofNullable(arrivalType)); + when(eventRepository.findFirstByEventTypeAndStartAtBetween(arrivalType, startOfDay, endOfDay)) .thenReturn(Event.builder() + .id(1L) + .title("셀토스 선착순 이벤트") + .description("The 2025 셀토스 출시 기념 선착순 이벤트") + .startAt(LocalDateTime.now()) + .endAt(LocalDateTime.now().plusDays(5)) .build() ); when(eventRewardRepository.countByEvent(any())) @@ -46,12 +54,13 @@ public void setArrivalEventCountSuccess_Data_Exists() { @DisplayName("이벤트 최대인원 세팅 스케줄러 동작 테스트 (성공 - Mysql에 데이터가 존재하지 않는 경우)") public void setArrivalEventCountSuccess_Data_Not_Exists() { // given + EventType arrivalType = EventType.builder() + .id(1L) + .type("arrival") + .build(); + when(eventTypeRepository.findById(1L)) - .thenReturn(Optional.ofNullable(EventType.builder() - .id(1L) - .type("arrival") - .build()) - ); + .thenReturn(Optional.ofNullable(arrivalType)); when(eventRepository.findFirstByEventTypeAndStartAtBetween(any(), any(), any())) .thenReturn(null); when(eventRewardRepository.countByEvent(any())) From 4a02abd46ce7210bc1edce5dfd0f0e690e6741d6 Mon Sep 17 00:00:00 2001 From: eckrin Date: Mon, 12 Aug 2024 17:31:33 +0900 Subject: [PATCH 13/13] =?UTF-8?q?fix:=20@Auth=EA=B0=80=20=EC=8A=A4?= =?UTF-8?q?=EC=9B=A8=EA=B1=B0=EC=97=90=20=EC=B6=94=EA=B0=80=EB=90=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8F=84=EB=A1=9D=20=ED=95=84=ED=84=B0?= =?UTF-8?q?=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/softeer/podoarrival/security/Auth.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/com/softeer/podoarrival/security/Auth.java b/src/main/java/com/softeer/podoarrival/security/Auth.java index ff80c9e..ea0f466 100644 --- a/src/main/java/com/softeer/podoarrival/security/Auth.java +++ b/src/main/java/com/softeer/podoarrival/security/Auth.java @@ -1,10 +1,13 @@ package com.softeer.podoarrival.security; +import io.swagger.v3.oas.annotations.Parameter; + import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +@Parameter(hidden = true) @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.PARAMETER) public @interface Auth {