Skip to content

Commit

Permalink
test: 베스트 셀러 크롤링 기능 테스트 추가 (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwooo committed Mar 27, 2024
1 parent 8cc6f05 commit e49b238
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ public void createBook() {
.build();

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

// when
BookResponse response = bookService.createBook(request);
Expand Down
46 changes: 45 additions & 1 deletion src/test/java/com/jisungin/infra/Yes24CrawlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -11,6 +12,9 @@
import com.jisungin.infra.crawler.Yes24Crawler;
import com.jisungin.infra.crawler.Yes24Fetcher;
import com.jisungin.infra.crawler.Yes24Parser;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import org.jsoup.nodes.Document;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -41,7 +45,10 @@ public void crawlingBook() {
Document isbnDocument = mock(Document.class);
Document bookDocument = mock(Document.class);

CrawlingBook crawlingBook = CrawlingBook.of("image url link", "crawling content");
LocalDateTime registeredTime = LocalDateTime.of(2024, 1, 1, 0, 0);

CrawlingBook crawlingBook = CrawlingBook.of("도서 제목", "도서 내용", "도서 ISBN",
"도서 출판사", "도서 이미지 링크", "도서 썸네일", "도서 작가1", registeredTime);

when(fetcher.fetchIsbn(isbn)).thenReturn(isbnDocument);
when(fetcher.fetchBook(bookId)).thenReturn(bookDocument);
Expand Down Expand Up @@ -69,4 +76,41 @@ public void crawlingBookWithInvalidIsbn() {
.hasMessage("책을 찾을 수 없습니다.");
}

@Test
@DisplayName("베스트 셀러 책을 크롤링 한다.")
public void crawlingBestSeller() {
// given
Document bestSellerBookIdsDoc = mock(Document.class);
Document fetchBookDoc1 = mock(Document.class);
Document fetchBookDoc2 = mock(Document.class);

Map<Long, String> bestSellerBookIds = new HashMap<>();
bestSellerBookIds.put(1L, "00001");
bestSellerBookIds.put(2L, "00002");

when(fetcher.fetchBestSellerBookId()).thenReturn(bestSellerBookIdsDoc);
when(parser.parseBestSellerBookId(any(Document.class))).thenReturn(bestSellerBookIds);

when(fetcher.fetchBook("00001")).thenReturn(fetchBookDoc1);
when(fetcher.fetchBook("00002")).thenReturn(fetchBookDoc2);

CrawlingBook book1 = CrawlingBook.of("책 제목1", "책 내용1", "책 ISBN1", "책 출판사1",
"책 이미지 URL1", "책 썸네일1", "책 저자1, 책 저자2",
LocalDateTime.of(2024, 1, 1, 0, 0));
CrawlingBook book2 = CrawlingBook.of("책 제목2", "책 내용2", "책 ISBN2", "책 출판사2",
"책 이미지 URL2", "책 썸네일2", "책 저자3, 책 저자4",
LocalDateTime.of(2024, 1, 1, 0, 0));

when(parser.parseBook(fetchBookDoc1)).thenReturn(book1);
when(parser.parseBook(fetchBookDoc2)).thenReturn(book2);

// when
Map<Long, CrawlingBook> bestSellerBooks = crawler.crawlBestSellerBook();

// then
assertThat(bestSellerBooks.size()).isEqualTo(2);
assertThat(bestSellerBooks.get(1L)).isEqualTo(book1);
assertThat(bestSellerBooks.get(2L)).isEqualTo(book2);
}

}
18 changes: 17 additions & 1 deletion src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,20 @@ spring:
url: jdbc:h2:mem:jisungin
username: sa
password:
driver-class-name: org.h2.Driver
driver-class-name: org.h2.Driver

crawler:
yes24:
fetcher:
isbnUrl: "https://www.yes24.com/Product/Search?domain=BOOK&query="
bookUrl: "https://www.yes24.com/Product/Goods/"
bestBookUrl: "https://www.yes24.com/Product/Category/BestSeller?categoryNumber=001&pageNumber=1&pageSize=100"
userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36"
parser:
isbnCss: "ul#yesSchList > li"
isbnAttr: "data-goods-no"
bookContentCss: "div.infoWrap_txt > div.infoWrap_txtInner"
bookJsonCss: "script[type=application/ld+json]"
bestRankingCss: "div.img_upper > em.ico.rank"
bestIdCss: "ul#yesBestList > li"
bestIdAttrs: "data-goods-no"

0 comments on commit e49b238

Please sign in to comment.