From 7c7d3f2b590f4f0525a9198229b45d9225a000a2 Mon Sep 17 00:00:00 2001 From: jubseok Date: Thu, 26 Dec 2024 11:07:58 +0900 Subject: [PATCH] =?UTF-8?q?[#98]=20test:=20=EB=82=98=EB=88=A0=EC=A7=84=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ai/dto/request/GPTSummaryRequest.java | 9 ++++++ .../itcast/news/application/NewsService.java | 2 +- .../news/application/SendNewsService.java | 2 -- .../news/application/NewsServiceTest.java | 31 +++++++++++++++++-- .../application/SelectNewsServiceTest.java | 2 -- 5 files changed, 39 insertions(+), 7 deletions(-) diff --git a/schedule/src/main/java/itcast/ai/dto/request/GPTSummaryRequest.java b/schedule/src/main/java/itcast/ai/dto/request/GPTSummaryRequest.java index 50e00340..356921b2 100644 --- a/schedule/src/main/java/itcast/ai/dto/request/GPTSummaryRequest.java +++ b/schedule/src/main/java/itcast/ai/dto/request/GPTSummaryRequest.java @@ -1,8 +1,17 @@ package itcast.ai.dto.request; +import lombok.Builder; + + public record GPTSummaryRequest( String model, Message message, float temperature ) { + @Builder + public GPTSummaryRequest(String model, Message message, float temperature) { + this.model = model; + this.message = message; + this.temperature = temperature; + } } diff --git a/schedule/src/main/java/itcast/news/application/NewsService.java b/schedule/src/main/java/itcast/news/application/NewsService.java index c85098ac..a005a023 100644 --- a/schedule/src/main/java/itcast/news/application/NewsService.java +++ b/schedule/src/main/java/itcast/news/application/NewsService.java @@ -76,7 +76,7 @@ public News processNews(String link) { } } - private void updateNewsSummary(News news, String content) { + public void updateNewsSummary(News news, String content) { try { Message message = new Message("user", content); GPTSummaryRequest request = new GPTSummaryRequest("gpt-4o-mini", message, 0.7f); diff --git a/schedule/src/main/java/itcast/news/application/SendNewsService.java b/schedule/src/main/java/itcast/news/application/SendNewsService.java index c0076e73..9996bbd4 100644 --- a/schedule/src/main/java/itcast/news/application/SendNewsService.java +++ b/schedule/src/main/java/itcast/news/application/SendNewsService.java @@ -39,7 +39,6 @@ public void selectNews(LocalDate yesterday) { } LocalDate sendAt = LocalDate.now().plusDays(ALARM_DAY); - newsList.forEach(news -> { news.newsUpdate(sendAt); }); @@ -60,7 +59,6 @@ public void sendNews() { news.getLink(), news.getThumbnail())) .toList(); - List emails = retrieveUserEmails(Interest.NEWS); if (emails == null || emails.isEmpty()) { diff --git a/schedule/src/test/java/itcast/news/application/NewsServiceTest.java b/schedule/src/test/java/itcast/news/application/NewsServiceTest.java index 92ba26b6..3ca2edbd 100644 --- a/schedule/src/test/java/itcast/news/application/NewsServiceTest.java +++ b/schedule/src/test/java/itcast/news/application/NewsServiceTest.java @@ -1,6 +1,7 @@ package itcast.news.application; import itcast.ai.application.GPTService; +import itcast.ai.dto.request.GPTSummaryRequest; import itcast.domain.news.News; import itcast.news.repository.NewsRepository; import org.jsoup.Connection; @@ -11,16 +12,15 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockedStatic; import org.mockito.junit.jupiter.MockitoExtension; import java.io.IOException; -import java.time.LocalDate; import java.time.LocalDateTime; import java.util.Arrays; -import java.util.Collections; import java.util.List; import static org.junit.jupiter.api.Assertions.*; @@ -168,4 +168,31 @@ void findLinksTest() throws IOException { } } + @Test + @DisplayName("updateNewsSummary 메소드 테스트") + void updateNewsSummaryTest() { + // give + News news = News.builder() + .id(1L) + .title("Sample News") + .content("Original Content") + .build(); + String content = "Updated Summary Content"; + + doNothing().when(gptService).updateNewsBySummaryContent(any(GPTSummaryRequest.class), eq(news.getId())); + + // when + assertDoesNotThrow(() -> newsService.updateNewsSummary(news, content)); + + ArgumentCaptor captor = ArgumentCaptor.forClass(GPTSummaryRequest.class); + verify(gptService, times(1)).updateNewsBySummaryContent(captor.capture(), eq(news.getId())); + + // then + GPTSummaryRequest capturedRequest = captor.getValue(); + assertEquals("gpt-4o-mini", capturedRequest.model()); + assertEquals("user", capturedRequest.message().getRole()); + assertEquals(content, capturedRequest.message().getContent()); + assertEquals(0.7f, capturedRequest.temperature()); + } + } diff --git a/schedule/src/test/java/itcast/news/application/SelectNewsServiceTest.java b/schedule/src/test/java/itcast/news/application/SelectNewsServiceTest.java index 8d45ba8d..440fa17f 100644 --- a/schedule/src/test/java/itcast/news/application/SelectNewsServiceTest.java +++ b/schedule/src/test/java/itcast/news/application/SelectNewsServiceTest.java @@ -27,8 +27,6 @@ @ExtendWith(MockitoExtension.class) public class SelectNewsServiceTest { private static final int YESTERDAY = 1; - private static final int ALARM_HOUR = 2; - private static final int ALARM_DAY = 2; @Mock private UserRepository userRepository;