|
@@ -0,0 +1,103 @@
|
|
|
|
|
+package com.etotem.cfc.unit;
|
|
|
|
|
+
|
|
|
|
|
+import com.etotem.cfc.entity.HealthPlan;
|
|
|
|
|
+import com.etotem.cfc.mapper.HealthPlanMapper;
|
|
|
|
|
+import com.etotem.cfc.mapper.TaskMapper;
|
|
|
|
|
+import com.etotem.cfc.service.impl.HealthPlanServiceImpl;
|
|
|
|
|
+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.any;
|
|
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 方案保存状态契约测试。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>需求:规划师审核不是默认流程,只有用户主动选了规划师(传 teacherId)方案才 pending_review;
|
|
|
|
|
+ * 用户自助制定(teacherId=null)应直接 published 并生成任务。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>RED 预期</b>:现状 savePlan 一律默认 pending_review(无视 teacherId),
|
|
|
|
|
+ * 故「自助方案应 published」用例失败(红);GREEN 阶段改默认状态逻辑后转绿。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+class HealthPlanSaveStatusContractTest {
|
|
|
|
|
+
|
|
|
|
|
+ private HealthPlanServiceImpl service;
|
|
|
|
|
+ private HealthPlanMapper mapper;
|
|
|
|
|
+ private TaskMapper taskMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @BeforeEach
|
|
|
|
|
+ void setUp() throws Exception {
|
|
|
|
|
+ service = new HealthPlanServiceImpl();
|
|
|
|
|
+ mapper = mock(HealthPlanMapper.class);
|
|
|
|
|
+ taskMapper = mock(TaskMapper.class);
|
|
|
|
|
+ inject(service, "healthPlanMapper", mapper);
|
|
|
|
|
+ inject(service, "taskMapper", taskMapper);
|
|
|
|
|
+ when(mapper.insert(any(HealthPlan.class))).thenReturn(1);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("自助方案(无teacherId、无status)→ 默认 published 并触发生成任务")
|
|
|
|
|
+ void 自助方案_默认published() {
|
|
|
|
|
+ HealthPlan plan = basePlan();
|
|
|
|
|
+ plan.setStatus(null); // 前端未传 status
|
|
|
|
|
+ plan.setTeacherId(null); // 未选规划师
|
|
|
|
|
+
|
|
|
|
|
+ service.savePlan(plan);
|
|
|
|
|
+
|
|
|
|
|
+ ArgumentCaptor<HealthPlan> cap = ArgumentCaptor.forClass(HealthPlan.class);
|
|
|
|
|
+ verify(mapper).insert(cap.capture());
|
|
|
|
|
+ assertEquals("published", cap.getValue().getStatus(),
|
|
|
|
|
+ "用户自助方案应直接发布,不应卡在 pending_review");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("指定规划师(有teacherId、无status)→ 默认 pending_review 待审核")
|
|
|
|
|
+ void 指定规划师_默认待审核() {
|
|
|
|
|
+ HealthPlan plan = basePlan();
|
|
|
|
|
+ plan.setStatus(null);
|
|
|
|
|
+ plan.setTeacherId(99L); // 选了规划师
|
|
|
|
|
+
|
|
|
|
|
+ service.savePlan(plan);
|
|
|
|
|
+
|
|
|
|
|
+ ArgumentCaptor<HealthPlan> cap = ArgumentCaptor.forClass(HealthPlan.class);
|
|
|
|
|
+ verify(mapper).insert(cap.capture());
|
|
|
|
|
+ assertEquals("pending_review", cap.getValue().getStatus(),
|
|
|
|
|
+ "选了规划师的方案应待审核");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("前端显式传 status=draft → 不被覆盖(草稿保存场景)")
|
|
|
|
|
+ void 显式草稿状态_不被覆盖() {
|
|
|
|
|
+ HealthPlan plan = basePlan();
|
|
|
|
|
+ plan.setStatus("draft");
|
|
|
|
|
+ plan.setTeacherId(null);
|
|
|
|
|
+
|
|
|
|
|
+ service.savePlan(plan);
|
|
|
|
|
+
|
|
|
|
|
+ ArgumentCaptor<HealthPlan> cap = ArgumentCaptor.forClass(HealthPlan.class);
|
|
|
|
|
+ verify(mapper).insert(cap.capture());
|
|
|
|
|
+ assertEquals("draft", cap.getValue().getStatus(), "显式传入的状态应被尊重");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private HealthPlan basePlan() {
|
|
|
|
|
+ HealthPlan p = new HealthPlan();
|
|
|
|
|
+ p.setFamilyId(72L);
|
|
|
|
|
+ p.setMemberIds("12");
|
|
|
|
|
+ p.setDimensions("body");
|
|
|
|
|
+ p.setGoal("检查");
|
|
|
|
|
+ p.setPlanContent("## 方案概述\n测试");
|
|
|
|
|
+ p.setCreatedAt(new java.util.Date());
|
|
|
|
|
+ return p;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ 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);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|