|
@@ -0,0 +1,156 @@
|
|
|
|
|
+package com.etotem.cfc.controller;
|
|
|
|
|
+
|
|
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
|
|
+import com.etotem.cfc.entity.Product;
|
|
|
|
|
+import com.etotem.cfc.entity.ReportTypeRegistry;
|
|
|
|
|
+import com.etotem.cfc.mapper.ProductMapper;
|
|
|
|
|
+import com.etotem.cfc.mapper.ReportTypeRegistryMapper;
|
|
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
|
+import org.junit.jupiter.api.extension.ExtendWith;
|
|
|
|
|
+import org.mockito.InjectMocks;
|
|
|
|
|
+import org.mockito.Mock;
|
|
|
|
|
+import org.mockito.junit.jupiter.MockitoExtension;
|
|
|
|
|
+import org.mockito.junit.jupiter.MockitoSettings;
|
|
|
|
|
+import org.mockito.quality.Strictness;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.Arrays;
|
|
|
|
|
+import java.util.Collections;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+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.Mockito.when;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * /api/report-parser/types/links 契约测试(小程序上传页「支持的报告类型」配置化)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * 目标行为:
|
|
|
|
|
+ * 1) 返回所有 is_active=1 的类型(不再过滤 linked_product_id 非空);
|
|
|
|
|
+ * 2) 返回字段含 typeId/displayName/description/icon/sortOrder/linkedProductId/productName;
|
|
|
|
|
+ * 无商品时 linkedProductId 与 productName 为 null;
|
|
|
|
|
+ * 3) 按 sortOrder 升序排序,sortOrder 相等时按 id 升序稳定排序;
|
|
|
|
|
+ * 4) is_active=0 的类型不返回。
|
|
|
|
|
+ */
|
|
|
|
|
+@ExtendWith(MockitoExtension.class)
|
|
|
|
|
+@MockitoSettings(strictness = Strictness.LENIENT)
|
|
|
|
|
+class ReportParserTypeLinksTest {
|
|
|
|
|
+
|
|
|
|
|
+ @Mock
|
|
|
|
|
+ private ReportTypeRegistryMapper typeRegistryMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Mock
|
|
|
|
|
+ private ProductMapper productMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @InjectMocks
|
|
|
|
|
+ private ReportParserController controller;
|
|
|
|
|
+
|
|
|
|
|
+ private ReportTypeRegistry type(Long id, String typeId, String displayName, String description,
|
|
|
|
|
+ String icon, Integer sortOrder, Long linkedProductId, boolean active) {
|
|
|
|
|
+ ReportTypeRegistry t = new ReportTypeRegistry();
|
|
|
|
|
+ t.setId(id);
|
|
|
|
|
+ t.setTypeId(typeId);
|
|
|
|
|
+ t.setDisplayName(displayName);
|
|
|
|
|
+ t.setDescription(description);
|
|
|
|
|
+ t.setIcon(icon);
|
|
|
|
|
+ t.setSortOrder(sortOrder);
|
|
|
|
|
+ t.setLinkedProductId(linkedProductId);
|
|
|
|
|
+ t.setIsActive(active);
|
|
|
|
|
+ return t;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Product product(Long id, String name) {
|
|
|
|
|
+ Product p = new Product();
|
|
|
|
|
+ p.setId(id);
|
|
|
|
|
+ p.setName(name);
|
|
|
|
|
+ return p;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
|
+ private List<Map<String, Object>> invoke() {
|
|
|
|
|
+ Result<List<Map<String, Object>>> r = controller.listTypeLinks();
|
|
|
|
|
+ assertEquals(200, r.getCode(), "接口应返回成功");
|
|
|
|
|
+ return (List<Map<String, Object>>) r.getData();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 契约1:返回所有激活类型,包含未配置商品链接(linked_product_id 为 null)的类型 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void returnsAllActiveTypesIncludingOnesWithoutProductLink() {
|
|
|
|
|
+ ReportTypeRegistry withLink = type(1L, "gut_flora", "菌群检测报告", "肠道菌群 PDF", "📄", 1, 101L, true);
|
|
|
|
|
+ ReportTypeRegistry noLink = type(2L, "physical_exam", "体检报告", "血常规、尿常规等", "📋", 3, null, true);
|
|
|
|
|
+ when(typeRegistryMapper.selectList(any())).thenReturn(new ArrayList<>(Arrays.asList(withLink, noLink)));
|
|
|
|
|
+ when(productMapper.selectBatchIds(any())).thenReturn(Collections.singletonList(product(101L, "菌群检测服务")));
|
|
|
|
|
+
|
|
|
|
|
+ List<Map<String, Object>> data = invoke();
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(2, data.size(), "未配置商品链接的激活类型也应返回");
|
|
|
|
|
+ assertTrue(data.stream().anyMatch(m -> "physical_exam".equals(m.get("typeId"))),
|
|
|
|
|
+ "linked_product_id 为 null 的类型应出现在结果中");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 契约2:返回字段齐全;无商品时 linkedProductId 与 productName 为 null */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void responseContainsAllFieldsAndNullProductWhenMissing() {
|
|
|
|
|
+ ReportTypeRegistry withLink = type(1L, "gut_flora", "菌群检测报告", "肠道菌群 PDF", "📄", 1, 101L, true);
|
|
|
|
|
+ ReportTypeRegistry noLink = type(2L, "tongue", "舌象照片", "拍摄舌象照片,AI 辅助舌诊分析", "👅", 4, null, true);
|
|
|
|
|
+ when(typeRegistryMapper.selectList(any())).thenReturn(new ArrayList<>(Arrays.asList(withLink, noLink)));
|
|
|
|
|
+ when(productMapper.selectBatchIds(any())).thenReturn(Collections.singletonList(product(101L, "菌群检测服务")));
|
|
|
|
|
+
|
|
|
|
|
+ List<Map<String, Object>> data = invoke();
|
|
|
|
|
+ Map<String, Object> linked = data.stream().filter(m -> "gut_flora".equals(m.get("typeId"))).findFirst().get();
|
|
|
|
|
+ assertTrue(linked.containsKey("typeId"), "应含 typeId");
|
|
|
|
|
+ assertTrue(linked.containsKey("displayName"), "应含 displayName");
|
|
|
|
|
+ assertTrue(linked.containsKey("description"), "应含 description");
|
|
|
|
|
+ assertTrue(linked.containsKey("icon"), "应含 icon");
|
|
|
|
|
+ assertTrue(linked.containsKey("sortOrder"), "应含 sortOrder");
|
|
|
|
|
+ assertTrue(linked.containsKey("linkedProductId"), "应含 linkedProductId");
|
|
|
|
|
+ assertTrue(linked.containsKey("productName"), "应含 productName");
|
|
|
|
|
+ assertEquals("菌群检测报告", linked.get("displayName"));
|
|
|
|
|
+ assertEquals("肠道菌群 PDF", linked.get("description"));
|
|
|
|
|
+ assertEquals("📄", linked.get("icon"));
|
|
|
|
|
+ assertEquals(1, linked.get("sortOrder"));
|
|
|
|
|
+ assertEquals(101L, linked.get("linkedProductId"));
|
|
|
|
|
+ assertEquals("菌群检测服务", linked.get("productName"));
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> unlinked = data.stream().filter(m -> "tongue".equals(m.get("typeId"))).findFirst().get();
|
|
|
|
|
+ assertNull(unlinked.get("linkedProductId"), "无商品时 linkedProductId 应为 null");
|
|
|
|
|
+ assertNull(unlinked.get("productName"), "无商品时 productName 应为 null");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 契约3:按 sortOrder 升序排序,sortOrder 相等时按 id 升序稳定排序 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void sortedBySortOrderAscThenIdAsc() {
|
|
|
|
|
+ ReportTypeRegistry a = type(3L, "body_fat", "体脂秤报告", "d5", "⚖️", 5, null, true);
|
|
|
|
|
+ ReportTypeRegistry b = type(1L, "gut_flora", "菌群检测报告", "d1", "📄", 1, null, true);
|
|
|
|
|
+ ReportTypeRegistry c = type(2L, "physical_exam", "体检报告", "d3", "📋", 3, null, true);
|
|
|
|
|
+ ReportTypeRegistry d = type(10L, "dan_a2", "素质测评A2", "d2b", "🧠", 2, null, true);
|
|
|
|
|
+ ReportTypeRegistry e = type(5L, "dan_a1", "素质测评A1", "d2a", "🧠", 2, null, true);
|
|
|
|
|
+ // 乱序 + 两组 sortOrder=2(id 5 < 10)
|
|
|
|
|
+ when(typeRegistryMapper.selectList(any()))
|
|
|
|
|
+ .thenReturn(new ArrayList<>(Arrays.asList(a, b, c, d, e)));
|
|
|
|
|
+
|
|
|
|
|
+ List<Map<String, Object>> data = invoke();
|
|
|
|
|
+
|
|
|
|
|
+ List<String> order = new ArrayList<>();
|
|
|
|
|
+ for (Map<String, Object> m : data) order.add((String) m.get("typeId"));
|
|
|
|
|
+ assertEquals(Arrays.asList("gut_flora", "dan_a1", "dan_a2", "physical_exam", "body_fat"), order,
|
|
|
|
|
+ "应按 sortOrder 升序,相等时按 id 升序(dan_a1 id=5 先于 dan_a2 id=10)");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 契约4:is_active=0 的类型不返回 */
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void inactiveTypeIsNotReturned() {
|
|
|
|
|
+ ReportTypeRegistry active = type(1L, "gut_flora", "菌群检测报告", "肠道菌群 PDF", "📄", 1, 101L, true);
|
|
|
|
|
+ ReportTypeRegistry inactive = type(2L, "dan_b6", "素质测评B6", "已停用", "🧠", 6, null, false);
|
|
|
|
|
+ when(typeRegistryMapper.selectList(any())).thenReturn(new ArrayList<>(Arrays.asList(active, inactive)));
|
|
|
|
|
+ when(productMapper.selectBatchIds(any())).thenReturn(Collections.singletonList(product(101L, "菌群检测服务")));
|
|
|
|
|
+
|
|
|
|
|
+ List<Map<String, Object>> data = invoke();
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(1, data.size(), "is_active=0 的类型不应返回");
|
|
|
|
|
+ assertEquals("gut_flora", data.get(0).get("typeId"));
|
|
|
|
|
+ }
|
|
|
|
|
+}
|