From 3524071782ac82d7d7ee39fb0eb91e2899687911 Mon Sep 17 00:00:00 2001 From: Wo-ogie Date: Tue, 18 Jul 2023 05:28:07 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20#12=20=EB=B3=80=EA=B2=BD=EB=90=9C?= =?UTF-8?q?=20=EC=9B=B9=ED=8E=98=EC=9D=B4=EC=A7=80(23.7.17)=EB=A5=BC=20?= =?UTF-8?q?=EA=B8=B0=EC=A4=80=EC=9C=BC=EB=A1=9C=20=EC=98=81=EC=97=85?= =?UTF-8?q?=EC=8B=9C=EA=B0=84=EA=B3=BC=20=ED=9C=B4=EB=AC=B4=EC=9D=BC?= =?UTF-8?q?=EC=9D=84=20=EC=A0=95=EC=83=81=EC=A0=81=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EC=B6=94=EC=B6=9C=ED=95=A0=20=EC=88=98=20=EC=9E=88=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/KakaoPlaceScrapingService.java | 14 +++++------- ...gTest.java => KakaoPlaceScrapingTest.java} | 21 ++++++++++-------- .../KakaoPlaceScrapingServiceTest.java | 22 ++++++++++--------- 3 files changed, 30 insertions(+), 27 deletions(-) rename src/test/java/com/zelusik/scraping/integration/controller/{PlaceScrapingTest.java => KakaoPlaceScrapingTest.java} (92%) diff --git a/src/main/java/com/zelusik/scraping/service/KakaoPlaceScrapingService.java b/src/main/java/com/zelusik/scraping/service/KakaoPlaceScrapingService.java index 965f4c5..e932896 100644 --- a/src/main/java/com/zelusik/scraping/service/KakaoPlaceScrapingService.java +++ b/src/main/java/com/zelusik/scraping/service/KakaoPlaceScrapingService.java @@ -40,9 +40,12 @@ public BusinessHoursDto getBusinessHours(WebElement mArticle) throws NoSuchEleme return createBusinessHours(openingHours, null); } + // TODO: div.inner_floor, div.displayPeriodList ul이 반드시 존재한다고 가정한 코드. 아닌 경우가 있다면 try-catch로 감싸야한다. WebElement operationList = mArticle.findElement(By.cssSelector("div.details_placeinfo div.fold_floor > div.inner_floor")); - String openingHours = operationList.findElement(By.cssSelector("ul:nth-child(2)")).getText(); - return createBusinessHours(openingHours, getClosingHours(operationList)); + String openingHours = operationList.findElement(By.cssSelector("div.displayPeriodList > ul:nth-child(2)")).getText(); + System.out.println(openingHours); + String closingHours = getClosingHours(operationList); + return createBusinessHours(openingHours, closingHours); } private BusinessHoursDto createBusinessHours(String openingHours, String closingHours) { @@ -72,12 +75,7 @@ private String makeClosingHours(List missingDays) { private String getClosingHours(WebElement operationList) { try { - if (operationList.findElement(By.cssSelector("strong:nth-child(3)")).getText().equals("휴무일")) { - return operationList.findElement(By.cssSelector("ul:nth-child(4)")).getText(); - } else if (operationList.findElement(By.cssSelector("strong:nth-child(5)")).getText().equals("휴무일")) { - return operationList.findElement(By.cssSelector("ul:nth-child(6)")).getText(); - } - return null; + return operationList.findElement(By.cssSelector("div.displayOffdayList > ul:nth-child(2)")).getText(); } catch (NoSuchElementException e) { return null; } diff --git a/src/test/java/com/zelusik/scraping/integration/controller/PlaceScrapingTest.java b/src/test/java/com/zelusik/scraping/integration/controller/KakaoPlaceScrapingTest.java similarity index 92% rename from src/test/java/com/zelusik/scraping/integration/controller/PlaceScrapingTest.java rename to src/test/java/com/zelusik/scraping/integration/controller/KakaoPlaceScrapingTest.java index 0f34561..e90b647 100644 --- a/src/test/java/com/zelusik/scraping/integration/controller/PlaceScrapingTest.java +++ b/src/test/java/com/zelusik/scraping/integration/controller/KakaoPlaceScrapingTest.java @@ -4,6 +4,7 @@ import com.zelusik.scraping.dto.place.OpeningHourDto; import com.zelusik.scraping.dto.place.PlaceInfoResponse; import com.zelusik.scraping.dto.place.TimeDto; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -20,8 +21,9 @@ import static org.junit.jupiter.params.provider.Arguments.arguments; @DisplayName("[Integration] Selenium을 활용해 장소 정보 가져오기") +@Disabled("실제 가게 정보는 수시로 바뀔 수 있으므로 기능의 정상적인 동작을 확인하기에 불안정하다. 따라서 평소에는 disabled 해놓고 필요할 때 테스트하는 방식으로 사용한다.") @SpringBootTest -class PlaceScrapingTest { +class KakaoPlaceScrapingTest { @Autowired private KakaoPlaceScrapingController sut; @@ -37,6 +39,7 @@ void getKakaoPlaceInfoTest(String kakaoPid, List openingHours, S // then if (openingHours != null) { + assertThat(actualResult.getOpeningHours()).isNotNull(); assertThat(actualResult.getOpeningHours().size()).isEqualTo(openingHours.size()); for (int i = 0; i < openingHours.size(); i++) { assertThat(actualResult.getOpeningHours().get(i).getDay()).isEqualTo(openingHours.get(i).getDay()); @@ -60,7 +63,7 @@ static Stream kakaoPlaceInfos() { OpeningHourDto.of(FRI, TimeDto.of(LocalTime.of(10, 30), LocalTime.of(15, 30))), OpeningHourDto.of(SAT, TimeDto.of(LocalTime.of(10, 30), LocalTime.of(15, 30))), OpeningHourDto.of(SUN, TimeDto.of(LocalTime.of(10, 30), LocalTime.of(15, 30)))), - "월요일", null), + "월요일", "https://app.catchtable.co.kr/ct/shop/moru"), arguments("308342289", List.of(OpeningHourDto.of(MON, TimeDto.of(LocalTime.of(11, 30), LocalTime.of(22, 0))), OpeningHourDto.of(TUE, TimeDto.of(LocalTime.of(11, 30), LocalTime.of(22, 0))), @@ -71,13 +74,13 @@ static Stream kakaoPlaceInfos() { OpeningHourDto.of(SUN, TimeDto.of(LocalTime.of(11, 30), LocalTime.of(22, 0)))), null, "https://www.instagram.com/toma_wv/"), arguments("25001083", - List.of(OpeningHourDto.of(MON, TimeDto.of(LocalTime.of(7, 30), LocalTime.of(23, 59))), - OpeningHourDto.of(TUE, TimeDto.of(LocalTime.of(7, 30), LocalTime.of(23, 59))), - OpeningHourDto.of(WED, TimeDto.of(LocalTime.of(7, 30), LocalTime.of(23, 59))), - OpeningHourDto.of(THU, TimeDto.of(LocalTime.of(7, 30), LocalTime.of(23, 59))), - OpeningHourDto.of(FRI, TimeDto.of(LocalTime.of(7, 30), LocalTime.of(23, 59))), - OpeningHourDto.of(SAT, TimeDto.of(LocalTime.of(7, 30), LocalTime.of(23, 59))), - OpeningHourDto.of(SUN, TimeDto.of(LocalTime.of(7, 30), LocalTime.of(23, 59)))), + List.of(OpeningHourDto.of(MON, TimeDto.of(LocalTime.of(8, 0), LocalTime.of(0, 0))), + OpeningHourDto.of(TUE, TimeDto.of(LocalTime.of(8, 0), LocalTime.of(0, 0))), + OpeningHourDto.of(WED, TimeDto.of(LocalTime.of(8, 0), LocalTime.of(0, 0))), + OpeningHourDto.of(THU, TimeDto.of(LocalTime.of(8, 0), LocalTime.of(0, 0))), + OpeningHourDto.of(FRI, TimeDto.of(LocalTime.of(8, 0), LocalTime.of(0, 0))), + OpeningHourDto.of(SAT, TimeDto.of(LocalTime.of(8, 0), LocalTime.of(0, 0))), + OpeningHourDto.of(SUN, TimeDto.of(LocalTime.of(8, 0), LocalTime.of(0, 0)))), null, "http://www.mcdonalds.co.kr/"), arguments("24529744", List.of(OpeningHourDto.of(MON, TimeDto.of(LocalTime.of(11, 30), LocalTime.of(22, 0))), diff --git a/src/test/java/com/zelusik/scraping/unit/service/KakaoPlaceScrapingServiceTest.java b/src/test/java/com/zelusik/scraping/unit/service/KakaoPlaceScrapingServiceTest.java index 03c7bc9..76452f9 100644 --- a/src/test/java/com/zelusik/scraping/unit/service/KakaoPlaceScrapingServiceTest.java +++ b/src/test/java/com/zelusik/scraping/unit/service/KakaoPlaceScrapingServiceTest.java @@ -5,6 +5,7 @@ import com.zelusik.scraping.dto.place.TimeDto; import com.zelusik.scraping.service.KakaoPlaceScrapingService; import com.zelusik.scraping.util.OpeningHoursConverter; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -77,19 +78,15 @@ void existsButtonAndOpeningAndClosingHours_whenGetBusinessHours_thenReturnResult String closingHours = "월요일\n화요일"; BusinessHoursDto expectedResult = new BusinessHoursDto(openingHourDtos, closingHours); - given(mArticle.findElement(By.cssSelector("div.cont_essential > div.details_placeinfo " + - "div.location_detail.openhour_wrap > div.location_present a.btn_more"))).willReturn(button); + given(mArticle.findElement(By.cssSelector("div.cont_essential > div.details_placeinfo div.location_detail.openhour_wrap > div.location_present a.btn_more"))).willReturn(button); willDoNothing().given(button).click(); given(mArticle.findElement(By.cssSelector("div.details_placeinfo div.fold_floor > div.inner_floor"))).willReturn(operationList); WebElement openingHoursElem = createWebElemMock("openingHours"); - given(operationList.findElement(By.cssSelector("ul:nth-child(2)"))).willReturn(openingHoursElem); + given(operationList.findElement(By.cssSelector("div.displayPeriodList > ul:nth-child(2)"))).willReturn(openingHoursElem); given(openingHoursElem.getText()).willReturn(openingHours); WebElement closingHoursTitleElem = createWebElemMock("closingHoursTitle"); - given(operationList.findElement(By.cssSelector("strong:nth-child(3)"))).willReturn(closingHoursTitleElem); - given(closingHoursTitleElem.getText()).willReturn("휴무일"); - WebElement closingHoursElem = createWebElemMock("closingHours"); - given(operationList.findElement(By.cssSelector("ul:nth-child(4)"))).willReturn(closingHoursElem); - given(closingHoursElem.getText()).willReturn(closingHours); + given(operationList.findElement(By.cssSelector("div.displayOffdayList > ul:nth-child(2)"))).willReturn(closingHoursTitleElem); + given(closingHoursTitleElem.getText()).willReturn(closingHours); given(converter.parseStrToOHs(openingHours)).willReturn(openingHourDtos); // when @@ -99,6 +96,11 @@ void existsButtonAndOpeningAndClosingHours_whenGetBusinessHours_thenReturnResult verifyGetBusinessHours(openingHours, expectedResult, actualResult); } + @Disabled(""" + 23.7.17 기준 "공휴일" 등의 정보가 div.displayPeriodList에 포함되었다. + 그러므로 현재 코드에서는 "공휴일" 등의 정보를 전혀 추출하지 않고, 그렇기 때문에 바로 위의 test(existsButtonAndOpeningAndClosingHours_whenGetBusinessHours_thenReturnResult)와 로직이 동일하다. + 어차피 "공휴일" 등의 정보는 현재 필요하지 않지만, 추후 필요할 수도 있으니 단지 test case를 남기기 위해 우선 이 test는 disabled 처리한다. + """) @DisplayName("영업시간 조회 - 버튼을 눌러 정보 열람 후, 영업시간과 휴무일 모두 있으나 휴무일 이전에 다른 정보(공휴일 등)가 함께 있는 경우") @Test void existsButtonAndOpeningAndClosingHoursAndAdditionalInfo_whenGetBusinessHours_thenReturnResult() { @@ -153,9 +155,9 @@ void existsButtonAndOpeningHours_whenGetBusinessHours_thenReturnResult() { willDoNothing().given(button).click(); given(mArticle.findElement(By.cssSelector("div.details_placeinfo div.fold_floor > div.inner_floor"))).willReturn(operationList); WebElement openingHoursElem = createWebElemMock("openingHours"); - given(operationList.findElement(By.cssSelector("ul:nth-child(2)"))).willReturn(openingHoursElem); + given(operationList.findElement(By.cssSelector("div.displayPeriodList > ul:nth-child(2)"))).willReturn(openingHoursElem); given(openingHoursElem.getText()).willReturn(openingHours); - given(operationList.findElement(By.cssSelector("strong:nth-child(3)"))).willThrow(NoSuchElementException.class); + given(operationList.findElement(By.cssSelector("div.displayOffdayList > ul:nth-child(2)"))).willThrow(NoSuchElementException.class); given(converter.parseStrToOHs(openingHours)).willReturn(openingHourDtos); // when