|
|
@@ -0,0 +1,335 @@
|
|
|
+package com.etotem.cfc.controller;
|
|
|
+
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
+import com.etotem.cfc.controller.HealthReportController;
|
|
|
+import com.etotem.cfc.entity.HealthIndicator;
|
|
|
+import com.etotem.cfc.entity.HealthReport;
|
|
|
+import com.etotem.cfc.service.HealthReportService;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
+import org.springframework.boot.test.mock.mockito.MockBean;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
+import static org.mockito.ArgumentMatchers.*;
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 营养调理师 (product_supplier) API 测试
|
|
|
+ *
|
|
|
+ * 角色说明: vendorType = 'product_supplier' 的服务商角色
|
|
|
+ * 可录入/查询健康报告(体检报告/菌群检测报告)
|
|
|
+ * 报告数据用于计算五维能量的"身(body)"维度
|
|
|
+ *
|
|
|
+ * 核心端点: POST /api/health/*
|
|
|
+ */
|
|
|
+@SpringBootTest
|
|
|
+public class HealthReportControllerTest {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private HealthReportController controller;
|
|
|
+
|
|
|
+ @MockBean
|
|
|
+ private HealthReportService healthReportService;
|
|
|
+
|
|
|
+ // ==================== mock helpers ====================
|
|
|
+
|
|
|
+ private HealthReport mockReport(Long id, Long userId, String reportType) {
|
|
|
+ HealthReport r = new HealthReport();
|
|
|
+ r.setId(id);
|
|
|
+ r.setUserId(userId);
|
|
|
+ r.setFamilyId(1L);
|
|
|
+ r.setReportType(reportType); // physical_exam / gut_flora
|
|
|
+ r.setReportDate(new Date());
|
|
|
+ r.setOverallScore(85);
|
|
|
+ r.setGutHealthScore(78);
|
|
|
+ r.setChronicDiseaseScore(90);
|
|
|
+ r.setNutritionScore(82);
|
|
|
+ r.setGutAge("28岁");
|
|
|
+ r.setGutType("II型");
|
|
|
+ r.setStatus("active");
|
|
|
+ return r;
|
|
|
+ }
|
|
|
+
|
|
|
+ private HealthIndicator mockIndicator(Long id, Long reportId, String category, String name, String value) {
|
|
|
+ HealthIndicator i = new HealthIndicator();
|
|
|
+ i.setId(id);
|
|
|
+ i.setReportId(reportId);
|
|
|
+ i.setCategory(category);
|
|
|
+ i.setIndicatorName(name);
|
|
|
+ i.setIndicatorValue(value);
|
|
|
+ i.setUnit("mg/dL");
|
|
|
+ i.setRefRange("0-100");
|
|
|
+ i.setStatus("normal");
|
|
|
+ return i;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> mockReportParams(Long childId, String reportType) {
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("childId", childId);
|
|
|
+ params.put("reportType", reportType);
|
|
|
+ params.put("overallScore", 85);
|
|
|
+ params.put("gutHealthScore", 78);
|
|
|
+ params.put("nutritionScore", 82);
|
|
|
+ params.put("fileUrl", "http://example.com/report.pdf");
|
|
|
+ return params;
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== createReport ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void createReport_PhysicalExam() {
|
|
|
+ Map<String, Object> params = mockReportParams(5L, "physical_exam");
|
|
|
+
|
|
|
+ when(healthReportService.createReport(any(HealthReport.class), anyList()))
|
|
|
+ .thenAnswer(invocation -> {
|
|
|
+ HealthReport r = invocation.getArgument(0);
|
|
|
+ r.setId(100L);
|
|
|
+ return r;
|
|
|
+ });
|
|
|
+
|
|
|
+ Result<HealthReport> result = controller.createReport(params, 5L);
|
|
|
+ assertEquals(200, result.getCode());
|
|
|
+ assertNotNull(result.getData().getId());
|
|
|
+ assertEquals("physical_exam", result.getData().getReportType());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void createReport_GutFlora() {
|
|
|
+ Map<String, Object> params = mockReportParams(5L, "gut_flora");
|
|
|
+ params.put("gutHealthScore", 88);
|
|
|
+ params.put("gutAge", "25岁");
|
|
|
+ params.put("gutType", "III型");
|
|
|
+
|
|
|
+ when(healthReportService.createReport(any(HealthReport.class), anyList()))
|
|
|
+ .thenAnswer(invocation -> {
|
|
|
+ HealthReport r = invocation.getArgument(0);
|
|
|
+ r.setId(101L);
|
|
|
+ return r;
|
|
|
+ });
|
|
|
+
|
|
|
+ Result<HealthReport> result = controller.createReport(params, 5L);
|
|
|
+ assertEquals(200, result.getCode());
|
|
|
+ assertEquals("gut_flora", result.getData().getReportType());
|
|
|
+ assertEquals("25岁", result.getData().getGutAge());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void createReport_WithIndicators() {
|
|
|
+ Map<String, Object> params = mockReportParams(5L, "physical_exam");
|
|
|
+
|
|
|
+ List<Map<String, Object>> indicators = new ArrayList<>();
|
|
|
+ indicators.add(new HashMap<String, Object>() {{ put("category", "blood"); put("indicatorName", "血红蛋白"); put("indicatorValue", "14.5"); put("unit", "g/dL"); put("refRange", "12-16"); put("status", "normal"); }});
|
|
|
+ indicators.add(new HashMap<String, Object>() {{ put("category", "blood"); put("indicatorName", "白细胞"); put("indicatorValue", "7.2"); put("unit", "10^9/L"); put("refRange", "4-10"); put("status", "normal"); }});
|
|
|
+ params.put("indicators", indicators);
|
|
|
+
|
|
|
+ when(healthReportService.createReport(any(HealthReport.class), anyList()))
|
|
|
+ .thenAnswer(invocation -> {
|
|
|
+ HealthReport r = invocation.getArgument(0);
|
|
|
+ r.setId(102L);
|
|
|
+ return r;
|
|
|
+ });
|
|
|
+
|
|
|
+ Result<HealthReport> result = controller.createReport(params, 5L);
|
|
|
+ assertEquals(200, result.getCode());
|
|
|
+ assertNotNull(result.getData().getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void createReport_MissingChildId() {
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("reportType", "physical_exam");
|
|
|
+
|
|
|
+ Result<HealthReport> result = controller.createReport(params, 5L);
|
|
|
+ assertEquals(500, result.getCode());
|
|
|
+ assertTrue(result.getMessage().contains("childId"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void createReport_MissingReportType() {
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("childId", 5L);
|
|
|
+
|
|
|
+ Result<HealthReport> result = controller.createReport(params, 5L);
|
|
|
+ assertEquals(500, result.getCode());
|
|
|
+ assertTrue(result.getMessage().contains("reportType"));
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== getLatestReport ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getLatestReport_Success() {
|
|
|
+ HealthReport report = mockReport(10L, 5L, "physical_exam");
|
|
|
+
|
|
|
+ when(healthReportService.getLatestReport(5L)).thenReturn(report);
|
|
|
+
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("childId", 5);
|
|
|
+
|
|
|
+ Result<HealthReport> result = controller.getLatestReport(params);
|
|
|
+ assertEquals(200, result.getCode());
|
|
|
+ assertEquals(5L, result.getData().getUserId().longValue());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getLatestReport_NotFound() {
|
|
|
+ when(healthReportService.getLatestReport(999L)).thenReturn(null);
|
|
|
+
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("childId", 999);
|
|
|
+
|
|
|
+ Result<HealthReport> result = controller.getLatestReport(params);
|
|
|
+ assertEquals(200, result.getCode());
|
|
|
+ assertNull(result.getData());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getLatestReport_MissingChildId() {
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ Result<HealthReport> result = controller.getLatestReport(params);
|
|
|
+ assertEquals(500, result.getCode());
|
|
|
+ assertTrue(result.getMessage().contains("childId"));
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== listReports ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void listReports_Success() {
|
|
|
+ List<HealthReport> reports = Arrays.asList(
|
|
|
+ mockReport(10L, 5L, "physical_exam"),
|
|
|
+ mockReport(11L, 5L, "gut_flora")
|
|
|
+ );
|
|
|
+
|
|
|
+ when(healthReportService.getUserReports(5L)).thenReturn(reports);
|
|
|
+
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("childId", 5);
|
|
|
+
|
|
|
+ Result<List<HealthReport>> result = controller.listReports(params);
|
|
|
+ assertEquals(200, result.getCode());
|
|
|
+ assertEquals(2, result.getData().size());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void listReports_Empty() {
|
|
|
+ when(healthReportService.getUserReports(5L)).thenReturn(Collections.emptyList());
|
|
|
+
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("childId", 5);
|
|
|
+
|
|
|
+ Result<List<HealthReport>> result = controller.listReports(params);
|
|
|
+ assertEquals(200, result.getCode());
|
|
|
+ assertTrue(result.getData().isEmpty());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void listReports_MissingChildId() {
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ Result<List<HealthReport>> result = controller.listReports(params);
|
|
|
+ assertEquals(500, result.getCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== getReportDetail ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getReportDetail_Success() {
|
|
|
+ Map<String, Object> detail = new HashMap<>();
|
|
|
+ detail.put("report", mockReport(10L, 5L, "physical_exam"));
|
|
|
+ detail.put("indicators", Arrays.asList(
|
|
|
+ mockIndicator(1L, 10L, "blood", "血红蛋白", "14.5")
|
|
|
+ ));
|
|
|
+ detail.put("diseaseRisks", Collections.emptyList());
|
|
|
+
|
|
|
+ when(healthReportService.getReportDetail(10L)).thenReturn(detail);
|
|
|
+
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("reportId", 10);
|
|
|
+
|
|
|
+ Result<Map<String, Object>> result = controller.getReportDetail(params);
|
|
|
+ assertEquals(200, result.getCode());
|
|
|
+ assertNotNull(result.getData().get("report"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getReportDetail_MissingReportId() {
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ Result<Map<String, Object>> result = controller.getReportDetail(params);
|
|
|
+ assertEquals(500, result.getCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== getBodyDetail ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getBodyDetail_Success() {
|
|
|
+ List<HealthIndicator> indicators = Arrays.asList(
|
|
|
+ mockIndicator(1L, 10L, "blood", "血红蛋白", "14.5"),
|
|
|
+ mockIndicator(2L, 10L, "blood", "白细胞", "7.2")
|
|
|
+ );
|
|
|
+
|
|
|
+ when(healthReportService.getBodyDetail(5L)).thenReturn(indicators);
|
|
|
+
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("childId", 5);
|
|
|
+
|
|
|
+ Result<List<HealthIndicator>> result = controller.getBodyDetail(params);
|
|
|
+ assertEquals(200, result.getCode());
|
|
|
+ assertEquals(2, result.getData().size());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getBodyDetail_MissingChildId() {
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ Result<List<HealthIndicator>> result = controller.getBodyDetail(params);
|
|
|
+ assertEquals(500, result.getCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== listIndicators ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void listIndicators_Success() {
|
|
|
+ List<HealthIndicator> indicators = Arrays.asList(
|
|
|
+ mockIndicator(1L, 10L, "blood", "血红蛋白", "14.5"),
|
|
|
+ mockIndicator(2L, 10L, "blood", "白细胞", "7.2"),
|
|
|
+ mockIndicator(3L, 10L, "liver", "ALT", "25")
|
|
|
+ );
|
|
|
+
|
|
|
+ when(healthReportService.getReportIndicators(10L)).thenReturn(indicators);
|
|
|
+
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("reportId", 10);
|
|
|
+
|
|
|
+ Result<List<HealthIndicator>> result = controller.listIndicators(params);
|
|
|
+ assertEquals(200, result.getCode());
|
|
|
+ assertEquals(3, result.getData().size());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void listIndicators_MissingReportId() {
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ Result<List<HealthIndicator>> result = controller.listIndicators(params);
|
|
|
+ assertEquals(500, result.getCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== archiveReport ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void archiveReport_Success() {
|
|
|
+ doNothing().when(healthReportService).archiveReport(10L);
|
|
|
+
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("reportId", 10);
|
|
|
+
|
|
|
+ Result<String> result = controller.archiveReport(params);
|
|
|
+ assertEquals(200, result.getCode());
|
|
|
+ assertTrue(result.getData().contains("归档"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void archiveReport_MissingReportId() {
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ Result<String> result = controller.archiveReport(params);
|
|
|
+ assertEquals(500, result.getCode());
|
|
|
+ }
|
|
|
+}
|