Skip to content

Commit

Permalink
test: Crawler 성인 도서 오류 처리 테스트 추가 (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwooo committed Jun 7, 2024
1 parent b93856a commit 5de0acd
Showing 1 changed file with 84 additions and 18 deletions.
102 changes: 84 additions & 18 deletions src/test/java/com/jisungin/infra/Yes24CrawlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import com.jisungin.exception.BusinessException;
import com.jisungin.exception.ErrorCode;
import com.jisungin.infra.crawler.CrawlingBook;
import com.jisungin.infra.crawler.CrawledBook;
import com.jisungin.infra.crawler.Yes24Crawler;
import com.jisungin.infra.crawler.Yes24Fetcher;
import com.jisungin.infra.crawler.Yes24Parser;
Expand Down Expand Up @@ -45,21 +45,19 @@ public void crawlingBook() {
Document isbnDocument = mock(Document.class);
Document bookDocument = mock(Document.class);

LocalDateTime registeredTime = LocalDateTime.of(2024, 1, 1, 0, 0);

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

when(fetcher.fetchIsbn(isbn)).thenReturn(isbnDocument);
when(fetcher.fetchBook(bookId)).thenReturn(bookDocument);

when(parser.parseIsbn(isbnDocument)).thenReturn(bookId);
when(parser.parseBook(bookDocument)).thenReturn(crawlingBook);
when(parser.parseBook(bookDocument)).thenReturn(crawledBook);

// when
CrawlingBook expectedCrawlingBook = crawler.crawlBook(isbn);
CrawledBook result = crawler.crawlBook(isbn);

// then
assertThat(expectedCrawlingBook).isEqualTo(crawlingBook);
assertThat(result).isEqualTo(crawledBook);
}

@Test
Expand Down Expand Up @@ -94,23 +92,91 @@ public void crawlingBestSeller() {
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));
CrawledBook book1 = createCrawledBookWithIsbn("1");
CrawledBook book2 = createCrawledBookWithIsbn("2");

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

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

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

@Test
@DisplayName("ISBN이 없는 도서를 조회한다.")
public void crawledBookWithoutISBN() {
Document bestSellerBookIdsDoc = mock(Document.class);
Document fetchBookDoc1 = mock(Document.class);
Document fetchBookDoc2 = mock(Document.class);

Map<Long, String> crawledBookIds = new HashMap<>();

crawledBookIds.put(1L, "000001");
crawledBookIds.put(2L, "000002");

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

when(fetcher.fetchBook("000001")).thenReturn(fetchBookDoc1);
when(fetcher.fetchBook("000002")).thenReturn(fetchBookDoc2);

CrawledBook book1 = createCrawledBookWithIsbn("");
CrawledBook book2 = createCrawledBookWithIsbn("");

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

// when
Map<Long, CrawledBook> result = crawler.crawlBestSellerBook();

// then
assertThat(result).hasSize(0);
}

@Test
@DisplayName("19세 이상 도서는 조회할 수 없다.")
public void crawlingBookWithAdultBook() {
// given
Document bestSellerBookIdsDoc = mock(Document.class);
Document fetchBookDoc1 = mock(Document.class);
Document fetchBookDoc2 = mock(Document.class);

Map<Long, String> crawledBookIds = new HashMap<>();
crawledBookIds.put(1L, "000001");
crawledBookIds.put(2L, "000002");

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

when(fetcher.fetchBook("000001")).thenReturn(fetchBookDoc1);
when(fetcher.fetchBook("000002")).thenReturn(fetchBookDoc2);

when(parser.parseBook(fetchBookDoc1)).thenReturn(null);
when(parser.parseBook(fetchBookDoc2)).thenReturn(null);

// when
Map<Long, CrawledBook> result = crawler.crawlBestSellerBook();

// then
assertThat(result).hasSize(0);
}

private static CrawledBook createCrawledBookWithIsbn(String isbn) {
return CrawledBook.builder()
.title("도서 제목" + isbn)
.content("도서 내용" + isbn)
.isbn(isbn)
.publisher("도서 출판사" + isbn)
.imageUrl("www.image-url.com/" + isbn)
.thumbnail("www.image-thumbnail.com/" + isbn)
.authors("도서 저자" + isbn)
.dateTime(LocalDateTime.of(2024, 1, 1, 0, 0))
.build();
}

}

0 comments on commit 5de0acd

Please sign in to comment.