|
|
@@ -0,0 +1,116 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+import com.etotem.cfc.entity.FamilyMember;
|
|
|
+import com.etotem.cfc.entity.HealthStatus;
|
|
|
+import com.etotem.cfc.entity.SurveyRecord;
|
|
|
+import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
|
+import com.etotem.cfc.mapper.SurveyRecordMapper;
|
|
|
+import com.etotem.cfc.mapper.SurveyTemplateMapper;
|
|
|
+import com.etotem.cfc.service.AiGateway;
|
|
|
+import com.etotem.cfc.service.HealthStatusService;
|
|
|
+import com.etotem.cfc.service.impl.SurveyServiceImpl;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.mockito.ArgumentCaptor;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
+import static org.mockito.ArgumentMatchers.*;
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 健康阅卷提交后同步 health_status 契约测试。
|
|
|
+ *
|
|
|
+ * <p>需求:/api/survey/submit(source=health_review)保存的数据,
|
|
|
+ * 应同步写入 health_status,使 /api/health-status/get 能读到,读写一致。</p>
|
|
|
+ *
|
|
|
+ * <p><b>RED 预期</b>:现状 doSubmit 只写 survey_records,不同步 health_status →
|
|
|
+ * verify healthStatusService.saveForMember 失败(红)。</p>
|
|
|
+ */
|
|
|
+class HealthReviewSyncStatusContractTest {
|
|
|
+
|
|
|
+ private SurveyServiceImpl service;
|
|
|
+ private SurveyRecordMapper recordMapper;
|
|
|
+ private FamilyMemberMapper familyMemberMapper;
|
|
|
+ private HealthStatusService healthStatusService;
|
|
|
+
|
|
|
+ private static final String ANSWERS = "{\"form\":{"
|
|
|
+ + "\"heightCm\":159,\"weightKg\":45,\"bloodPressure\":\"110/60\","
|
|
|
+ + "\"bloodGlucose\":5.6,"
|
|
|
+ + "\"bloodLipids\":\"[{\\\"name\\\":\\\"总胆固醇\\\",\\\"value\\\":4.5}]\","
|
|
|
+ + "\"diseaseHistory\":\"[]\",\"medications\":\"[]\","
|
|
|
+ + "\"lifestyle\":\"{\\\"currentStatus\\\":\\\"正常\\\"}\",\"notes\":\"测试\"},"
|
|
|
+ + "\"diet\":{\"healthGoals\":[\"减脂\"]}}";
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() throws Exception {
|
|
|
+ service = new SurveyServiceImpl();
|
|
|
+ recordMapper = mock(SurveyRecordMapper.class);
|
|
|
+ familyMemberMapper = mock(FamilyMemberMapper.class);
|
|
|
+ healthStatusService = mock(HealthStatusService.class);
|
|
|
+
|
|
|
+ inject(service, "surveyRecordMapper", recordMapper);
|
|
|
+ inject(service, "familyMemberMapper", familyMemberMapper);
|
|
|
+ inject(service, "surveyTemplateMapper", mock(SurveyTemplateMapper.class));
|
|
|
+ inject(service, "aiGateway", mock(AiGateway.class));
|
|
|
+ inject(service, "healthStatusService", healthStatusService);
|
|
|
+
|
|
|
+ // insert 后 selectById 返回带 id 的 record
|
|
|
+ when(recordMapper.insert(any(SurveyRecord.class))).thenAnswer(inv -> {
|
|
|
+ SurveyRecord r = inv.getArgument(0);
|
|
|
+ r.setId(100L);
|
|
|
+ return 1;
|
|
|
+ });
|
|
|
+ when(recordMapper.selectById(100L)).thenAnswer(inv -> {
|
|
|
+ SurveyRecord r = new SurveyRecord();
|
|
|
+ r.setId(100L);
|
|
|
+ return r;
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("health_review 提交 → 同步写 health_status(用成员归属 userId,字段映射正确)")
|
|
|
+ void healthReview提交_同步写healthStatus() {
|
|
|
+ FamilyMember member = new FamilyMember();
|
|
|
+ member.setId(34L);
|
|
|
+ member.setUserId(72L);
|
|
|
+ member.setFamilyId(83L);
|
|
|
+ when(familyMemberMapper.selectById(34L)).thenReturn(member);
|
|
|
+
|
|
|
+ service.submitSurvey(72L, 34L, null, ANSWERS, null, null, null, "health_review");
|
|
|
+
|
|
|
+ ArgumentCaptor<HealthStatus> cap = ArgumentCaptor.forClass(HealthStatus.class);
|
|
|
+ verify(healthStatusService).saveForMember(eq(72L), eq(34L), cap.capture());
|
|
|
+ HealthStatus hs = cap.getValue();
|
|
|
+ assertEquals(0, new java.math.BigDecimal("159").compareTo(hs.getHeightCm()), "身高应映射");
|
|
|
+ assertEquals(0, new java.math.BigDecimal("45").compareTo(hs.getWeightKg()), "体重应映射");
|
|
|
+ assertEquals("110/60", hs.getBloodPressure(), "血压应映射");
|
|
|
+ assertEquals(0, new java.math.BigDecimal("5.6").compareTo(hs.getBloodGlucose()), "血糖应映射");
|
|
|
+ assertTrue(hs.getBloodLipids().contains("总胆固醇"), "血脂应映射");
|
|
|
+ assertEquals("测试", hs.getNotes(), "备注应映射");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("非 health_review(定期调研)→ 不同步 health_status")
|
|
|
+ void 非healthReview_不同步() {
|
|
|
+ FamilyMember member = new FamilyMember();
|
|
|
+ member.setId(34L);
|
|
|
+ member.setUserId(72L);
|
|
|
+ when(familyMemberMapper.selectById(34L)).thenReturn(member);
|
|
|
+ // periodic 分支需 templateId(null 会抛异常),这里用一个不存在的模板触发异常后验证未同步
|
|
|
+ try {
|
|
|
+ service.submitSurvey(72L, 34L, null, ANSWERS, null, null, null, "other_source");
|
|
|
+ } catch (Exception ignore) {
|
|
|
+ }
|
|
|
+ // other_source 非 health_review,不应同步
|
|
|
+ verify(healthStatusService, never()).saveForMember(anyLong(), anyLong(), any(HealthStatus.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+}
|