Skip to content

Commit

Permalink
test: BestSellerRepository Zset으로 자료구조 변경으로 인한 테스트 수정 (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwooo committed Jun 7, 2024
1 parent 3c870ec commit c8944a1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,21 @@
import static org.mockito.Mockito.when;

import com.jisungin.RedisTestContainer;
import com.jisungin.application.OffsetLimit;
import com.jisungin.application.PageResponse;
import com.jisungin.application.book.BestSellerService;
import com.jisungin.application.book.event.BestSellerUpdatedEvent;
import com.jisungin.application.book.event.BestSellerUpdatedEventListener;
import com.jisungin.application.book.request.BookServicePageRequest;
import com.jisungin.application.book.response.BestSellerResponse;
import com.jisungin.application.book.response.BookWithRankingResponse;
import com.jisungin.domain.book.repository.BestSellerRedisRepository;
import com.jisungin.domain.book.repository.BookRepository;
import com.jisungin.infra.crawler.CrawledBook;
import com.jisungin.infra.crawler.Crawler;
import com.jisungin.infra.crawler.CrawlingBook;
import com.jisungin.infra.s3.S3FileManager;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -35,7 +33,6 @@

@SpringBootTest
@RecordApplicationEvents
@Slf4j
public class BestSellerServiceTest extends RedisTestContainer {

@Autowired
Expand Down Expand Up @@ -68,15 +65,12 @@ public void tearDown() {
@DisplayName("베스트 셀러 페이지를 조회한다.")
public void getBestSellers() {
// given
Map<Long, CrawlingBook> bestSellers = createSampleBestSellers();

bestSellerRedisRepository.updateAll(bestSellers);
bestSellerRedisRepository.updateAll(createCrawledBookMap());

// when
PageResponse<BestSellerResponse> result = bestSellerService.getBestSellers(BookServicePageRequest.builder()
.page(1)
.size(5)
.build());
PageResponse<BookWithRankingResponse> result = bestSellerService.getBestSellers(OffsetLimit
.ofRange(1, 5));

// then
assertThat(result.getSize()).isEqualTo(5);
assertThat(result.getTotalCount()).isEqualTo(6);
Expand All @@ -95,15 +89,15 @@ public void getBestSellers() {
@DisplayName("베스트 셀러를 갱신 한다.")
public void updateBestSellers() {
// given
Map<Long, CrawlingBook> bestSellers = createSampleBestSellers();
Map<Long, CrawledBook> crawledBookMap = createCrawledBookMap();

when(crawler.crawlBestSellerBook()).thenReturn(bestSellers);
when(crawler.crawlBestSellerBook()).thenReturn(crawledBookMap);

// when
bestSellerService.updateBestSellers();

// then
List<BestSellerResponse> bookResponses = bestSellerRedisRepository.findAll();
List<BookWithRankingResponse> bookResponses = bestSellerRedisRepository.findAll();

assertThat(bookResponses.size()).isEqualTo(6);
assertThat(bookResponses).extracting("title", "isbn", "publisher", "authors")
Expand All @@ -121,8 +115,8 @@ public void updateBestSellers() {
@DisplayName("베스트 셀러를 갱신하면 DB에 새로 등록된 책을 저장하는 이벤트가 발생한다.")
public void updateBestSellerEventRaised() {
// given
Map<Long, CrawlingBook> bestSellers = createSampleBestSellers();
when(crawler.crawlBestSellerBook()).thenReturn(bestSellers);
Map<Long, CrawledBook> crawledBookMap = createCrawledBookMap();
when(crawler.crawlBestSellerBook()).thenReturn(crawledBookMap);

// when
bestSellerService.updateBestSellers();
Expand All @@ -131,11 +125,11 @@ public void updateBestSellerEventRaised() {
verify(eventEventListener).handleBestSellerUpdatedEvent(any(BestSellerUpdatedEvent.class));
}

private static Map<Long, CrawlingBook> createSampleBestSellers() {
private static Map<Long, CrawledBook> createCrawledBookMap() {
return IntStream.rangeClosed(1, 6)
.boxed()
.collect(Collectors.toMap(Long::valueOf,
i -> CrawlingBook.of("title" + i, "content" + i, "isbn" + i,
i -> CrawledBook.of("title" + i, "content" + i, "isbn" + i,
"publisher" + i, "imageUrl" + i, "thumbnail" + i, "author" + i,
LocalDateTime.of(2024, 1, 1, 0, 0))));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import com.jisungin.domain.user.repository.UserRepository;
import com.jisungin.exception.BusinessException;
import com.jisungin.infra.crawler.Crawler;
import com.jisungin.infra.crawler.CrawlingBook;
import com.jisungin.infra.crawler.CrawledBook;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.IntStream;
Expand Down Expand Up @@ -161,7 +161,7 @@ public void createBook() {
.build();

when(crawler.crawlBook(request.getIsbn()))
.thenReturn(CrawlingBook.of("도서 제목", "도서 내용", "123456789X", "도서 출판사",
.thenReturn(CrawledBook.of("도서 제목", "도서 내용", "123456789X", "도서 출판사",
"도서 imageUrl", "도서 썸네일", "도서 작가1,도서 작가2", registeredDateTime));

// when
Expand Down
45 changes: 23 additions & 22 deletions src/test/java/com/jisungin/docs/book/BookControllerDocsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@

import com.jisungin.api.book.BookController;
import com.jisungin.api.book.request.BookCreateRequest;
import com.jisungin.api.book.request.BookPageRequest;
import com.jisungin.application.OffsetLimit;
import com.jisungin.application.PageResponse;
import com.jisungin.application.book.BestSellerService;
import com.jisungin.application.book.BookService;
import com.jisungin.application.book.request.BookCreateServiceRequest;
import com.jisungin.application.book.request.BookServicePageRequest;
import com.jisungin.application.book.response.BestSellerResponse;
import com.jisungin.application.book.response.BookWithRankingResponse;
import com.jisungin.application.book.response.BookFindAllResponse;
import com.jisungin.application.book.response.BookResponse;
import com.jisungin.docs.RestDocsSupport;
Expand All @@ -57,11 +55,13 @@ protected Object initController() {
@Test
@DisplayName("도서 단건 조회 API")
public void getBook() throws Exception {
// given
String isbn = "0000000000001";

given(bookService.getBook(anyString()))
.willReturn(createBookResponseWithIsbn(isbn));

// when // then
mockMvc.perform(get("/v1/books/{isbn}", isbn)
.accept(APPLICATION_JSON))
.andDo(print())
Expand Down Expand Up @@ -93,12 +93,14 @@ public void getBook() throws Exception {
@Test
@DisplayName("도서 페이징 조회 API")
public void getBooks() throws Exception {
List<BookFindAllResponse> queryResponse = createSimpleBookResponse();
PageResponse<BookFindAllResponse> response = PageResponse.of(queryResponse.size(), queryResponse.size(),
queryResponse);
// given
List<BookFindAllResponse> response = createBookFindAllResponse();
PageResponse<BookFindAllResponse> pageResponse = PageResponse.of(response.size(), response.size(),
response);

given(bookService.getBooks(any(OffsetLimit.class))).willReturn(response);
given(bookService.getBooks(any(OffsetLimit.class))).willReturn(pageResponse);

// when // then
mockMvc.perform(get("/v1/books")
.param("page", "1")
.param("size", "10")
Expand Down Expand Up @@ -136,22 +138,19 @@ public void getBooks() throws Exception {
@Test
@DisplayName("베스트 셀러 조회 API")
public void getBestSellers() throws Exception {
BookPageRequest params = BookPageRequest.builder()
.page(1)
.size(5)
.build();

List<BestSellerResponse> queryResponse = createBestSellerResponse();
// given
List<BookWithRankingResponse> response = createBookWithRankingResponse();

PageResponse<BestSellerResponse> response = PageResponse.of(queryResponse.size(),
queryResponse.size(), queryResponse);
PageResponse<BookWithRankingResponse> pageResponse = PageResponse.of(response.size(),
response.size(), response);

given(bestSellerService.getBestSellers(any(BookServicePageRequest.class)))
.willReturn(response);
given(bestSellerService.getBestSellers(any(OffsetLimit.class)))
.willReturn(pageResponse);

// when // then
mockMvc.perform(get("/v1/books/best-seller")
.param("page", String.valueOf(params.getPage()))
.param("size", String.valueOf(params.getSize()))
.param("page", "1")
.param("size", "5")
.accept(APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk())
Expand Down Expand Up @@ -184,6 +183,7 @@ public void getBestSellers() throws Exception {
@Test
@DisplayName("도서 생성 API")
public void createBook() throws Exception {
// given
BookCreateRequest request = BookCreateRequest.builder()
.isbn("0000000000001")
.title("book title")
Expand All @@ -197,6 +197,7 @@ public void createBook() throws Exception {
given(bookService.createBook(any(BookCreateServiceRequest.class)))
.willReturn(createBookResponseWithIsbn(request.getIsbn()));

// when // then
mockMvc.perform(post("/v1/books")
.accept(APPLICATION_JSON)
.contentType(APPLICATION_JSON)
Expand Down Expand Up @@ -247,7 +248,7 @@ private BookResponse createBookResponseWithIsbn(String isbn) {
.build();
}

private List<BookFindAllResponse> createSimpleBookResponse() {
private List<BookFindAllResponse> createBookFindAllResponse() {
return IntStream.rangeClosed(1, 5)
.mapToObj(i -> BookFindAllResponse.builder()
.isbn("000000000000" + i)
Expand All @@ -260,9 +261,9 @@ private List<BookFindAllResponse> createSimpleBookResponse() {
.toList();
}

private List<BestSellerResponse> createBestSellerResponse() {
private List<BookWithRankingResponse> createBookWithRankingResponse() {
return LongStream.rangeClosed(1, 5)
.mapToObj(i -> BestSellerResponse.builder()
.mapToObj(i -> BookWithRankingResponse.builder()
.ranking(i)
.isbn("00000000000" + i)
.title("book title" + i)
Expand Down

0 comments on commit c8944a1

Please sign in to comment.