Skip to content

Commit 4d87ac0

Browse files
committed
Merge remote-tracking branch 'origin/develop' into OPS-327-BE-자료-등록-LLM-연동
2 parents cdb8ee2 + 79c2b3c commit 4d87ac0

File tree

6 files changed

+77
-34
lines changed

6 files changed

+77
-34
lines changed

src/main/java/org/tuna/zoopzoop/backend/domain/datasource/ai/service/AiService.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
import org.tuna.zoopzoop.backend.domain.datasource.ai.dto.AiExtractorDto;
77
import org.tuna.zoopzoop.backend.domain.datasource.ai.dto.AnalyzeContentDto;
88
import org.tuna.zoopzoop.backend.domain.datasource.ai.prompt.AiPrompt;
9+
import org.tuna.zoopzoop.backend.domain.datasource.entity.Tag;
910
import org.tuna.zoopzoop.backend.domain.datasource.repository.TagRepository;
1011

11-
import java.util.HashSet;
1212
import java.util.List;
13-
import java.util.Set;
1413
import java.util.stream.Collectors;
1514

1615
@Service
@@ -28,17 +27,12 @@ public AiExtractorDto extract(String rawHtml) {
2827
return response;
2928
}
3029

31-
public AnalyzeContentDto analyzeContent(String content) {
32-
// 모든 태그 가져오기
33-
List<String> allTags = tagRepository.findAllTagNames();
34-
35-
// 중복 제거 (Set → 다시 List or String)
36-
Set<String> uniqueTags = new HashSet<>(allTags);
37-
30+
public AnalyzeContentDto analyzeContent(String content, List<Tag> tagList) {
3831
// JSON 배열 문자열로 변환
39-
String tags = uniqueTags.stream()
40-
.map(tag -> "\"" + tag + "\"") // "tagName"
41-
.collect(Collectors.joining(", ", "[", "]")); // ["tag1", "tag2"]
32+
String tags = tagList.stream()
33+
.map(Tag::getTagName) // 태그명만 추출
34+
.map(tagName -> "\"" + tagName + "\"")
35+
.collect(Collectors.joining(", ", "[", "]"));
4236

4337
AnalyzeContentDto response = chatClient.prompt()
4438
.user(AiPrompt.SUMMARY_TAG_CATEGORY.formatted(content, tags))

src/main/java/org/tuna/zoopzoop/backend/domain/datasource/crawler/controller/CrawlerTestController.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@
88
import org.tuna.zoopzoop.backend.domain.datasource.crawler.service.CrawlerManagerService;
99
import org.tuna.zoopzoop.backend.domain.datasource.dataprocessor.service.DataProcessorService;
1010
import org.tuna.zoopzoop.backend.domain.datasource.dto.DataSourceDto;
11+
import org.tuna.zoopzoop.backend.domain.datasource.repository.TagRepository;
12+
13+
import java.util.ArrayList;
1114

1215
@RestController
1316
@RequestMapping("api/v1")
1417
@RequiredArgsConstructor
1518
public class CrawlerTestController {
1619
private final CrawlerManagerService crawlerManagerService;
1720
private final DataProcessorService dataProcessorService;
21+
private final TagRepository tagRepository;
1822

1923
@GetMapping("/crawl")
2024
public DataSourceDto crawl(@RequestParam String url) throws Exception {
21-
return dataProcessorService.process(url);
25+
return dataProcessorService.process(url, new ArrayList<>());
2226
}
2327
}

src/main/java/org/tuna/zoopzoop/backend/domain/datasource/dataprocessor/service/DataProcessorService.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@
1111
import org.tuna.zoopzoop.backend.domain.datasource.crawler.service.CrawlerManagerService;
1212
import org.tuna.zoopzoop.backend.domain.datasource.dto.ArticleData;
1313
import org.tuna.zoopzoop.backend.domain.datasource.dto.DataSourceDto;
14+
import org.tuna.zoopzoop.backend.domain.datasource.entity.Tag;
1415

1516
import java.io.IOException;
17+
import java.util.List;
1618

1719
@Service
1820
@RequiredArgsConstructor
1921
public class DataProcessorService {
2022
public final CrawlerManagerService crawlerManagerService;
2123
public final AiService aiService;
2224

23-
public DataSourceDto process(String url) throws IOException {
25+
public DataSourceDto process(String url, List<Tag> tagList) throws IOException {
2426
CrawlerResult<?> result = crawlerManagerService.extractContent(url);
2527

2628
ArticleData articleData = switch (result.type()) {
@@ -47,7 +49,7 @@ yield new ArticleData(
4749
}
4850
};
4951

50-
AnalyzeContentDto analyzeContentDto = aiService.analyzeContent(articleData.content());
52+
AnalyzeContentDto analyzeContentDto = aiService.analyzeContent(articleData.content(), tagList);
5153

5254
return new DataSourceDto(
5355
articleData.title(),
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
package org.tuna.zoopzoop.backend.domain.datasource.repository;
22

33
import org.springframework.data.jpa.repository.JpaRepository;
4-
import org.springframework.data.jpa.repository.Query;
54
import org.springframework.stereotype.Repository;
65
import org.tuna.zoopzoop.backend.domain.datasource.entity.Tag;
76

8-
import java.util.List;
9-
107
@Repository
118
public interface TagRepository extends JpaRepository<Tag, Integer> {
12-
@Query("select t.tagName from Tag t")
13-
List<String> findAllTagNames();
149
}

src/main/java/org/tuna/zoopzoop/backend/global/initData/BaseInitData.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
import org.springframework.context.annotation.Lazy;
99
import org.springframework.context.annotation.Profile;
1010
import org.springframework.transaction.annotation.Transactional;
11-
import org.tuna.zoopzoop.backend.domain.datasource.entity.Tag;
1211
import org.tuna.zoopzoop.backend.domain.datasource.repository.TagRepository;
13-
import org.tuna.zoopzoop.backend.domain.datasource.ai.service.AiService;
1412
import org.tuna.zoopzoop.backend.domain.member.repository.MemberRepository;
1513
import org.tuna.zoopzoop.backend.domain.space.space.entity.Space;
1614
import org.tuna.zoopzoop.backend.domain.space.space.repository.SpaceRepository;
@@ -40,20 +38,8 @@ public void initalizeData() {
4038

4139
}
4240

43-
private final AiService aiService;
44-
4541
@Transactional
4642
public void initTagData() {
47-
if (tagRepository.count() > 0) {
48-
return;
49-
}
50-
51-
Tag tag1 = new Tag(null,"IT");
52-
Tag tag2 = new Tag(null, "자기소개");
53-
Tag tag3 = new Tag(null, "이름");
5443

55-
tagRepository.save(tag1);
56-
tagRepository.save(tag2);
57-
tagRepository.save(tag3);
5844
}
5945
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.tuna.zoopzoop.backend.domain.datasource.service;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.extension.ExtendWith;
5+
import org.mockito.InjectMocks;
6+
import org.mockito.Mock;
7+
import org.mockito.junit.jupiter.MockitoExtension;
8+
import org.springframework.ai.chat.client.ChatClient;
9+
import org.tuna.zoopzoop.backend.domain.datasource.ai.dto.AnalyzeContentDto;
10+
import org.tuna.zoopzoop.backend.domain.datasource.ai.service.AiService;
11+
import org.tuna.zoopzoop.backend.domain.datasource.entity.Category;
12+
import org.tuna.zoopzoop.backend.domain.datasource.entity.Tag;
13+
14+
import java.util.List;
15+
16+
import static org.assertj.core.api.Assertions.assertThat;
17+
import static org.mockito.ArgumentMatchers.anyString;
18+
import static org.mockito.Mockito.when;
19+
20+
@ExtendWith(MockitoExtension.class)
21+
class AiServiceTest {
22+
@Mock
23+
private ChatClient chatClient;
24+
25+
@Mock
26+
private ChatClient.ChatClientRequestSpec requestSpec;
27+
28+
@Mock
29+
private ChatClient.CallResponseSpec responseSpec;
30+
31+
32+
@InjectMocks
33+
private AiService aiService;
34+
35+
@Test
36+
void analyzeContent_ShouldReturnMockedResponse() {
37+
// given
38+
String content = "테스트 본문";
39+
List<Tag> tagList = List.of(new Tag("Java"), new Tag("Spring"));
40+
41+
AnalyzeContentDto mockResponse = new AnalyzeContentDto(
42+
"요약",
43+
Category.IT,
44+
List.of("Java", "Spring")
45+
);
46+
47+
// 체인 mock 세팅
48+
when(chatClient.prompt()).thenReturn(requestSpec);
49+
when(requestSpec.user(anyString())).thenReturn(requestSpec);
50+
when(requestSpec.call()).thenReturn(responseSpec);
51+
when(responseSpec.entity(AnalyzeContentDto.class)).thenReturn(mockResponse);
52+
53+
// when
54+
AnalyzeContentDto result = aiService.analyzeContent(content, tagList);
55+
56+
// then
57+
assertThat(result).isNotNull();
58+
assertThat(result.summary()).isEqualTo("요약");
59+
assertThat(result.tags()).containsExactly("Java", "Spring");
60+
assertThat(result.category()).isEqualTo(Category.IT);
61+
}
62+
}

0 commit comments

Comments
 (0)