-
Notifications
You must be signed in to change notification settings - Fork 37
[4주차] RateLimit 적용 및 테스트 커버리지 개선 #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
14ce595
[feat] 단일 서버 RateLimit 구현
devops3199 74f84fa
[feat] 분산 환경 RateLimit 구현
devops3199 620e443
[fix] 예약 성공 시 rate limit 적용
devops3199 44841c1
[test] 테스트 코드 작성
devops3199 0103907
[test] @DataJpaTest로 변경
devops3199 2fd597c
[chore] update readme
devops3199 ed9e3f8
[fix] SeatRepositoryTest 테스트 조건 수정
devops3199 3aba743
[refactor] key generator 유틸 생성
devops3199 e5c4f91
[refactor] PR 리뷰 적용
devops3199 f6068e8
[refactor] 테스트 코드 메소드 이름 및 변수명
devops3199 bf10709
[refactor] use-case 로직 -> domain 로직으로 이동
devops3199 47f4f76
[test] domain 테스트 작성
devops3199 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
110 changes: 110 additions & 0 deletions
110
application/src/test/java/com/example/app/booking/service/CreateBookingServiceTest.java
This file contains hidden or 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,110 @@ | ||
| package com.example.app.booking.service; | ||
|
|
||
| import com.example.app.booking.domain.Booking; | ||
| import com.example.app.booking.dto.CreateBookingCommand; | ||
| import com.example.app.booking.out.persistence.adapter.BookingPersistenceAdapter; | ||
| import com.example.app.booking.out.persistence.adapter.SeatPersistenceAdapter; | ||
| import com.example.app.common.exception.APIException; | ||
| import com.example.app.common.function.DistributedLockService; | ||
| import com.example.app.config.EmbeddedRedisConfig; | ||
| import com.example.app.movie.type.TheaterSeat; | ||
| import com.navercorp.fixturemonkey.FixtureMonkey; | ||
| import com.navercorp.fixturemonkey.api.introspector.ConstructorPropertiesArbitraryIntrospector; | ||
| import com.navercorp.fixturemonkey.jakarta.validation.plugin.JakartaValidationPlugin; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.InjectMocks; | ||
| import org.mockito.Mock; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
| import org.springframework.test.context.TestPropertySource; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.util.Set; | ||
| import java.util.function.Supplier; | ||
|
|
||
| import static com.example.app.booking.exception.BookingErrorMessage.SEAT_ROW_NOT_IN_SEQUENCE; | ||
| import static com.navercorp.fixturemonkey.api.instantiator.Instantiator.constructor; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| @SpringBootTest(classes = EmbeddedRedisConfig.class) | ||
| @TestPropertySource(properties = "spring.config.location = classpath:application-test.yml") | ||
| public class CreateBookingServiceTest { | ||
|
|
||
| private FixtureMonkey fixtureMonkey; | ||
|
|
||
| @Mock | ||
| private DistributedLockService distributedLockService; | ||
|
|
||
| @Mock | ||
| private BookingPersistenceAdapter bookingPersistenceAdapter; | ||
|
|
||
| @Mock | ||
| private SeatPersistenceAdapter seatPersistenceAdapter; | ||
|
|
||
| @InjectMocks | ||
| private CreateBookingService sut; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| fixtureMonkey = FixtureMonkey.builder() | ||
| .objectIntrospector(ConstructorPropertiesArbitraryIntrospector.INSTANCE) | ||
| .plugin(new JakartaValidationPlugin()) | ||
| .build(); | ||
| } | ||
|
|
||
| @Test | ||
| public void 예약_테스트() { | ||
| var key = fixtureMonkey.giveMeOne(String.class); | ||
| var continuousSeats = Set.of(TheaterSeat.A3, TheaterSeat.A4, TheaterSeat.A5); | ||
| var bookingCommand = fixtureMonkey.giveMeBuilder(CreateBookingCommand.class) | ||
| .instantiate(constructor() | ||
| .parameter(long.class) | ||
| .parameter(long.class) | ||
| .parameter(long.class) | ||
| .parameter(long.class) | ||
| .parameter(LocalDate.class) | ||
| .parameter(Set.class, "seats")) | ||
| .set("seats", continuousSeats) | ||
| .sample(); | ||
|
|
||
| var booking = fixtureMonkey.giveMeBuilder(Booking.class) | ||
| .instantiate(constructor() | ||
| .parameter(long.class) | ||
| .parameter(long.class) | ||
| .parameter(long.class) | ||
| .parameter(long.class) | ||
| .parameter(long.class) | ||
| .parameter(int.class, "totalSeats") | ||
| .parameter(LocalDate.class)) | ||
| .set("totalSeats", 3) | ||
| .sample(); | ||
|
|
||
| when(distributedLockService.executeWithLockAndReturn(any(Supplier.class), any(String.class), any(Long.class), any(Long.class))) | ||
| .thenReturn(booking); | ||
|
|
||
| var result = sut.createBooking(key, bookingCommand); | ||
|
|
||
| assertEquals(booking, result); | ||
| } | ||
|
|
||
| @Test | ||
| public void 예약_불가_테스트() { | ||
| var key = fixtureMonkey.giveMeOne(String.class); | ||
| var discontinuousSeats = Set.of(TheaterSeat.B1, TheaterSeat.C1, TheaterSeat.D1); | ||
| var bookingCommand = fixtureMonkey.giveMeBuilder(CreateBookingCommand.class) | ||
| .instantiate(constructor() | ||
| .parameter(long.class) | ||
| .parameter(long.class) | ||
| .parameter(long.class) | ||
| .parameter(long.class) | ||
| .parameter(LocalDate.class) | ||
| .parameter(Set.class, "seats")) | ||
| .set("seats", discontinuousSeats) | ||
| .sample(); | ||
|
|
||
| var exception = assertThrows(APIException.class, () -> sut.createBooking(key, bookingCommand)); | ||
| assertEquals(SEAT_ROW_NOT_IN_SEQUENCE.getMessage(), exception.getMessage()); | ||
| } | ||
| } | ||
109 changes: 0 additions & 109 deletions
109
application/src/test/java/com/example/app/booking/usecase/CreateBookingUseCaseTest.java
This file was deleted.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
application/src/test/java/com/example/app/config/EmbeddedRedisConfig.java
This file contains hidden or 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,29 @@ | ||
| package com.example.app.config; | ||
|
|
||
| import jakarta.annotation.PreDestroy; | ||
| import jakarta.annotation.PostConstruct; | ||
| import org.springframework.boot.autoconfigure.data.redis.RedisProperties; | ||
| import org.springframework.boot.test.context.TestConfiguration; | ||
| import redis.embedded.RedisServer; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| @TestConfiguration | ||
| public class EmbeddedRedisConfig { | ||
|
|
||
| private final RedisServer redisServer; | ||
|
|
||
| public EmbeddedRedisConfig(RedisProperties redisProperties) throws IOException { | ||
| this.redisServer = new RedisServer(redisProperties.getPort()); | ||
| } | ||
|
|
||
| @PostConstruct | ||
| public void postConstruct() throws IOException { | ||
| redisServer.start(); | ||
| } | ||
|
|
||
| @PreDestroy | ||
| public void preDestroy() throws IOException { | ||
| redisServer.stop(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Set.of(TheaterSeat.B1, TheaterSeat.C1, TheaterSeat.D1) 부분을 불연속적인 좌석 을 의미하는 변수로 추출하여 전달하면 유지보수성 및 가독성이 더 좋아질 것 같습니다.