|
|
@@ -0,0 +1,237 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
+import com.etotem.cfc.controller.admin.AdminArticleController;
|
|
|
+import com.etotem.cfc.entity.Article;
|
|
|
+import com.etotem.cfc.service.ArticleKbCheckService;
|
|
|
+import com.etotem.cfc.service.ArticleKbCheckService.KbCheckOutcome;
|
|
|
+import com.etotem.cfc.service.ArticleService;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.mockito.ArgumentCaptor;
|
|
|
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.Callable;
|
|
|
+import java.util.concurrent.CompletableFuture;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+import static org.mockito.ArgumentMatchers.eq;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.never;
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * AdminArticleController 知识库校验挂载流测试。
|
|
|
+ *
|
|
|
+ * <p>覆盖(Review Focus #2/#3):create/update 挂载校验(主题词命中才校验)、
|
|
|
+ * REJECTED 强制 draft、APPROVED/SKIP/非主题保持状态不阻断、publish REJECTED
|
|
|
+ * 不硬拦(code=200 + kbCheckRejected=true,且仍执行 toggleStatus)。</p>
|
|
|
+ */
|
|
|
+class AdminArticleKbCheckFlowTest {
|
|
|
+
|
|
|
+ private AdminArticleController controller;
|
|
|
+ private ArticleService articleService;
|
|
|
+ private ArticleKbCheckService kbCheckService;
|
|
|
+ private ThreadPoolTaskExecutor taskExecutor;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() throws Exception {
|
|
|
+ controller = new AdminArticleController();
|
|
|
+ articleService = mock(ArticleService.class);
|
|
|
+ kbCheckService = mock(ArticleKbCheckService.class);
|
|
|
+ taskExecutor = mock(ThreadPoolTaskExecutor.class);
|
|
|
+ inject(controller, "articleService", articleService);
|
|
|
+ inject(controller, "kbCheckService", kbCheckService);
|
|
|
+ inject(controller, "taskExecutor", taskExecutor);
|
|
|
+ // 文本内容安全检测走 taskExecutor:立即成功返回,避免该路径影响被测逻辑
|
|
|
+ when(taskExecutor.submit(any(Callable.class)))
|
|
|
+ .thenAnswer(inv -> CompletableFuture.completedFuture(true));
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void inject(Object target, String field, Object value) throws Exception {
|
|
|
+ Field f = target.getClass().getDeclaredField(field);
|
|
|
+ f.setAccessible(true);
|
|
|
+ f.set(target, value);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> createBody(String title, String content, String status) {
|
|
|
+ Map<String, Object> body = new HashMap<>();
|
|
|
+ body.put("title", title);
|
|
|
+ body.put("content", content);
|
|
|
+ body.put("summary", "摘要");
|
|
|
+ body.put("tags", "");
|
|
|
+ body.put("status", status);
|
|
|
+ return body;
|
|
|
+ }
|
|
|
+
|
|
|
+ // ---------- create ----------
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("create 非主题词:不触发校验,状态保持 published")
|
|
|
+ void create_非主题词_不触发校验_状态保持() throws Exception {
|
|
|
+ when(kbCheckService.isProbioticTopic(any(), any(), any())).thenReturn(false);
|
|
|
+
|
|
|
+ Result<?> r = controller.create(
|
|
|
+ createBody("今日新闻速览", "行业数字化转型升级新机遇", "published"), 1L);
|
|
|
+
|
|
|
+ assertEquals(200, r.getCode());
|
|
|
+ ArgumentCaptor<Article> captor = ArgumentCaptor.forClass(Article.class);
|
|
|
+ verify(articleService).create(captor.capture(), eq(1L));
|
|
|
+ assertEquals("published", captor.getValue().getStatus());
|
|
|
+ assertNull(captor.getValue().getKbCheckStatus(), "非主题词不应留下校验状态");
|
|
|
+ verify(kbCheckService, never()).checkSentences(any());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("create 主题词 REJECTED:请求 published 强制落 draft,data.kbCheckStatus=REJECTED")
|
|
|
+ void create_主题词_REJECTED_强制draft() throws Exception {
|
|
|
+ when(kbCheckService.isProbioticTopic(any(), any(), any())).thenReturn(true);
|
|
|
+ when(kbCheckService.splitSentences(any()))
|
|
|
+ .thenReturn(Arrays.asList("第一句足够长", "第二句没出处"));
|
|
|
+ when(kbCheckService.checkSentences(any()))
|
|
|
+ .thenReturn(new KbCheckOutcome("REJECTED", "{\"all_passed\":false}"));
|
|
|
+
|
|
|
+ Result<?> r = controller.create(
|
|
|
+ createBody("益生菌选购指南", "双歧杆菌有助于维持肠道菌群平衡,菌株 LGG 有研究支持。", "published"), 1L);
|
|
|
+
|
|
|
+ ArgumentCaptor<Article> captor = ArgumentCaptor.forClass(Article.class);
|
|
|
+ verify(articleService).create(captor.capture(), eq(1L));
|
|
|
+ assertEquals("draft", captor.getValue().getStatus(), "REJECTED 必须强制 draft(Review Focus #2)");
|
|
|
+ assertEquals("REJECTED", captor.getValue().getKbCheckStatus());
|
|
|
+ assertEquals(200, r.getCode());
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ Map<String, Object> data = (Map<String, Object>) r.getData();
|
|
|
+ assertEquals("REJECTED", data.get("kbCheckStatus"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("create 主题词 APPROVED:状态保持 published")
|
|
|
+ void create_主题词_APPROVED_状态保持() throws Exception {
|
|
|
+ when(kbCheckService.isProbioticTopic(any(), any(), any())).thenReturn(true);
|
|
|
+ when(kbCheckService.checkSentences(any()))
|
|
|
+ .thenReturn(new KbCheckOutcome("APPROVED", "{\"all_passed\":true}"));
|
|
|
+
|
|
|
+ Result<?> r = controller.create(
|
|
|
+ createBody("益生菌怎么选", "双歧杆菌有助于维持肠道菌群平衡。", "published"), 1L);
|
|
|
+
|
|
|
+ ArgumentCaptor<Article> captor = ArgumentCaptor.forClass(Article.class);
|
|
|
+ verify(articleService).create(captor.capture(), eq(1L));
|
|
|
+ assertEquals("published", captor.getValue().getStatus());
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ Map<String, Object> data = (Map<String, Object>) r.getData();
|
|
|
+ assertEquals("APPROVED", data.get("kbCheckStatus"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("create 主题词 SKIP:不阻断,状态保持 published")
|
|
|
+ void create_主题词_SKIP_不阻断_状态保持() throws Exception {
|
|
|
+ when(kbCheckService.isProbioticTopic(any(), any(), any())).thenReturn(true);
|
|
|
+ when(kbCheckService.checkSentences(any()))
|
|
|
+ .thenReturn(new KbCheckOutcome("SKIP", "{\"reason\":\"timeout\"}"));
|
|
|
+
|
|
|
+ Result<?> r = controller.create(
|
|
|
+ createBody("益生菌选购", "双歧杆菌有助于维持肠道菌群平衡。", "published"), 1L);
|
|
|
+
|
|
|
+ ArgumentCaptor<Article> captor = ArgumentCaptor.forClass(Article.class);
|
|
|
+ verify(articleService).create(captor.capture(), eq(1L));
|
|
|
+ assertEquals("published", captor.getValue().getStatus(), "SKIP 不阻断保存");
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ Map<String, Object> data = (Map<String, Object>) r.getData();
|
|
|
+ assertEquals("SKIP", data.get("kbCheckStatus"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("create 主题词但校验抛异常:兜底 SKIP,状态保持 published,不阻断")
|
|
|
+ void create_校验异常_兜底SKIP() throws Exception {
|
|
|
+ when(kbCheckService.isProbioticTopic(any(), any(), any())).thenReturn(true);
|
|
|
+ when(kbCheckService.checkSentences(any())).thenThrow(new RuntimeException("boom"));
|
|
|
+
|
|
|
+ Result<?> r = controller.create(
|
|
|
+ createBody("益生菌选购", "双歧杆菌有助于维持肠道菌群平衡。", "published"), 1L);
|
|
|
+
|
|
|
+ assertEquals(200, r.getCode());
|
|
|
+ ArgumentCaptor<Article> captor = ArgumentCaptor.forClass(Article.class);
|
|
|
+ verify(articleService).create(captor.capture(), eq(1L));
|
|
|
+ assertEquals("published", captor.getValue().getStatus());
|
|
|
+ assertEquals("SKIP", captor.getValue().getKbCheckStatus(), "校验异常必须兜底 SKIP 而非阻断");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ---------- update ----------
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("update 主题词 REJECTED:强制 draft")
|
|
|
+ void update_主题词_REJECTED_强制draft() throws Exception {
|
|
|
+ Article existing = new Article();
|
|
|
+ existing.setId(9L);
|
|
|
+ existing.setStatus("draft");
|
|
|
+ when(articleService.getById(9L)).thenReturn(existing);
|
|
|
+ when(kbCheckService.isProbioticTopic(any(), any(), any())).thenReturn(true);
|
|
|
+ when(kbCheckService.splitSentences(any()))
|
|
|
+ .thenReturn(Arrays.asList("第一句足够长", "第二句没出处"));
|
|
|
+ when(kbCheckService.checkSentences(any()))
|
|
|
+ .thenReturn(new KbCheckOutcome("REJECTED", "{\"all_passed\":false}"));
|
|
|
+ Map<String, Object> body = createBody("益生菌选购", "双歧杆菌有助于维持肠道菌群平衡,此句无出处。", "draft");
|
|
|
+ body.put("id", 9L);
|
|
|
+
|
|
|
+ Result<?> r = controller.update(body);
|
|
|
+
|
|
|
+ assertEquals(200, r.getCode());
|
|
|
+ ArgumentCaptor<Article> captor = ArgumentCaptor.forClass(Article.class);
|
|
|
+ verify(articleService).update(captor.capture());
|
|
|
+ assertEquals("draft", captor.getValue().getStatus());
|
|
|
+ assertEquals("REJECTED", captor.getValue().getKbCheckStatus());
|
|
|
+ }
|
|
|
+
|
|
|
+ // ---------- publish(Review Focus #3:不硬拦) ----------
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("publish REJECTED:仍发布成功,code=200 + kbCheckRejected=true")
|
|
|
+ void publish_REJECTED_仍可发布_返回警告() {
|
|
|
+ Article article = new Article();
|
|
|
+ article.setId(1L);
|
|
|
+ article.setStatus("draft");
|
|
|
+ article.setKbCheckStatus("REJECTED");
|
|
|
+ when(articleService.getById(1L)).thenReturn(article);
|
|
|
+ Map<String, Object> body = new HashMap<>();
|
|
|
+ body.put("id", 1L);
|
|
|
+ body.put("status", "published");
|
|
|
+
|
|
|
+ Result<?> r = controller.publish(body);
|
|
|
+
|
|
|
+ assertEquals(200, r.getCode(), "REJECTED 发布不得硬拦(Review Focus #3)");
|
|
|
+ Object dataObj = r.getData();
|
|
|
+ assertTrue(dataObj instanceof Map, "REJECTED 路径应返回带 kbCheckRejected 标志的响应体");
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ Map<String, Object> data = (Map<String, Object>) dataObj;
|
|
|
+ assertEquals(Boolean.TRUE, data.get("kbCheckRejected"), "REJECTED 发布应返回 kbCheckRejected=true");
|
|
|
+ verify(articleService).toggleStatus(1L, "published");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("publish 非 REJECTED:正常返回,无 kbCheckRejected 标志")
|
|
|
+ void publish_正常_无警告() {
|
|
|
+ Article article = new Article();
|
|
|
+ article.setId(2L);
|
|
|
+ article.setStatus("draft");
|
|
|
+ when(articleService.getById(2L)).thenReturn(article);
|
|
|
+ Map<String, Object> body = new HashMap<>();
|
|
|
+ body.put("id", 2L);
|
|
|
+ body.put("status", "published");
|
|
|
+
|
|
|
+ Result<?> r = controller.publish(body);
|
|
|
+
|
|
|
+ assertEquals(200, r.getCode());
|
|
|
+ Object dataObj = r.getData();
|
|
|
+ assertTrue(dataObj instanceof String, "非 REJECTED 路径保留原始「操作成功」字符串");
|
|
|
+ verify(articleService).toggleStatus(2L, "published");
|
|
|
+ }
|
|
|
+}
|