|
|
@@ -0,0 +1,237 @@
|
|
|
+package com.etotem.cfc.controller.guide;
|
|
|
+
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
+import com.etotem.cfc.entity.TrainingPlan;
|
|
|
+import com.etotem.cfc.service.TrainingPlanService;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.mockito.ArgumentCaptor;
|
|
|
+import org.mockito.Mock;
|
|
|
+import org.mockito.MockitoAnnotations;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.test.util.ReflectionTestUtils;
|
|
|
+import org.springframework.test.web.servlet.MockMvc;
|
|
|
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertSame;
|
|
|
+import static org.mockito.ArgumentMatchers.anyLong;
|
|
|
+import static org.mockito.ArgumentMatchers.eq;
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
|
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
|
|
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
|
+
|
|
|
+/**
|
|
|
+ * TDD 覆盖驱动(S-cfc-4 / 能力键 guide.trainingPlan.getPlanList):
|
|
|
+ * POST /api/guide/training-plans/list 获取规划师训练方案列表 的契约级单测。
|
|
|
+ *
|
|
|
+ * 契约(对应 TrainingPlanController.getPlanList):
|
|
|
+ * - memberId 解析顺序:body.memberId(ParamUtils.getLong)优先,缺失/非法则回退 currentMemberId
|
|
|
+ * - memberId != null → trainingPlanService.getPlansByGuideAndChild(guideId, memberId)
|
|
|
+ * - memberId == null → trainingPlanService.getPlansByGuide(guideId)
|
|
|
+ * - 返回统一 Result.success(plans),code=200,data=服务返回列表
|
|
|
+ *
|
|
|
+ * 角色/权限口径:getPlanList 处理器无 @RequestAttribute("role"/"roles"),授权交由 JwtInterceptor
|
|
|
+ * (匿名请求注入 userId=-1 被 401 拦截)承担;本控制器唯一身份边界是 @RequestAttribute("userId")
|
|
|
+ * required=true(guideId=登录账号),本测试覆盖该 guideId 传递正确性而非角色分支。
|
|
|
+ * 测试不依赖真实微信/外部服务:TrainingPlanService 全 mock。
|
|
|
+ */
|
|
|
+public class TrainingPlanControllerGetPlanListTest {
|
|
|
+
|
|
|
+ @Mock private TrainingPlanService trainingPlanService;
|
|
|
+
|
|
|
+ private MockMvc mockMvc;
|
|
|
+ private TrainingPlanController controller;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ public void setUp() {
|
|
|
+ MockitoAnnotations.openMocks(this);
|
|
|
+ controller = new TrainingPlanController();
|
|
|
+ ReflectionTestUtils.setField(controller, "trainingPlanService", trainingPlanService);
|
|
|
+ mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
|
|
|
+ }
|
|
|
+
|
|
|
+ private TrainingPlan plan(long id, String title) {
|
|
|
+ TrainingPlan p = new TrainingPlan();
|
|
|
+ p.setId(id);
|
|
|
+ p.setTitle(title);
|
|
|
+ return p;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getPlanList_withoutMemberId_usesGetPlansByGuide() throws Exception {
|
|
|
+ Long guideId = 9L;
|
|
|
+ List<TrainingPlan> plans = Arrays.asList(plan(1L, "方案A"), plan(2L, "方案B"));
|
|
|
+ when(trainingPlanService.getPlansByGuide(guideId)).thenReturn(plans);
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/guide/training-plans/list")
|
|
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
|
+ .content("{}")
|
|
|
+ .requestAttr("userId", guideId))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(200))
|
|
|
+ .andExpect(jsonPath("$.data[0].id").value(1))
|
|
|
+ .andExpect(jsonPath("$.data[0].title").value("方案A"))
|
|
|
+ .andExpect(jsonPath("$.data[1].id").value(2));
|
|
|
+
|
|
|
+ verify(trainingPlanService).getPlansByGuide(guideId);
|
|
|
+ verify(trainingPlanService, never()).getPlansByGuideAndChild(anyLong(), anyLong());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getPlanList_withBodyMemberId_usesGetPlansByGuideAndChild() throws Exception {
|
|
|
+ Long guideId = 9L;
|
|
|
+ Long memberId = 55L;
|
|
|
+ List<TrainingPlan> plans = Collections.singletonList(plan(3L, "针对成员"));
|
|
|
+ when(trainingPlanService.getPlansByGuideAndChild(guideId, memberId)).thenReturn(plans);
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/guide/training-plans/list")
|
|
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
|
+ .content("{\"memberId\":55}")
|
|
|
+ .requestAttr("userId", guideId))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(200))
|
|
|
+ .andExpect(jsonPath("$.data[0].id").value(3));
|
|
|
+
|
|
|
+ verify(trainingPlanService).getPlansByGuideAndChild(guideId, memberId);
|
|
|
+ verify(trainingPlanService, never()).getPlansByGuide(anyLong());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getPlanList_withoutBodyMemberId_fallsBackToCurrentMemberId() throws Exception {
|
|
|
+ Long guideId = 9L;
|
|
|
+ Long currentMemberId = 42L;
|
|
|
+ List<TrainingPlan> plans = Collections.singletonList(plan(4L, "回退成员"));
|
|
|
+ when(trainingPlanService.getPlansByGuideAndChild(guideId, currentMemberId)).thenReturn(plans);
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/guide/training-plans/list")
|
|
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
|
+ .content("{}")
|
|
|
+ .requestAttr("userId", guideId)
|
|
|
+ .requestAttr("currentMemberId", currentMemberId))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(200))
|
|
|
+ .andExpect(jsonPath("$.data[0].id").value(4));
|
|
|
+
|
|
|
+ verify(trainingPlanService).getPlansByGuideAndChild(guideId, currentMemberId);
|
|
|
+ verify(trainingPlanService, never()).getPlansByGuide(anyLong());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getPlanList_bodyMemberIdTakesPrecedenceOverCurrentMemberId() throws Exception {
|
|
|
+ Long guideId = 9L;
|
|
|
+ Long bodyMemberId = 7L;
|
|
|
+ Long currentMemberId = 42L;
|
|
|
+ List<TrainingPlan> plans = Collections.singletonList(plan(5L, "body优先"));
|
|
|
+ when(trainingPlanService.getPlansByGuideAndChild(guideId, bodyMemberId)).thenReturn(plans);
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/guide/training-plans/list")
|
|
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
|
+ .content("{\"memberId\":7}")
|
|
|
+ .requestAttr("userId", guideId)
|
|
|
+ .requestAttr("currentMemberId", currentMemberId))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(200))
|
|
|
+ .andExpect(jsonPath("$.data[0].id").value(5));
|
|
|
+
|
|
|
+ verify(trainingPlanService).getPlansByGuideAndChild(guideId, bodyMemberId);
|
|
|
+ verify(trainingPlanService, never()).getPlansByGuide(anyLong());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getPlanList_nonNumericBodyMemberId_fallsBackToCurrentMemberId() throws Exception {
|
|
|
+ Long guideId = 9L;
|
|
|
+ Long currentMemberId = 42L;
|
|
|
+ List<TrainingPlan> plans = Collections.singletonList(plan(6L, "非法回退"));
|
|
|
+ when(trainingPlanService.getPlansByGuideAndChild(guideId, currentMemberId)).thenReturn(plans);
|
|
|
+
|
|
|
+ // ParamUtils.getLong 对非数字返回 null,应回退 currentMemberId
|
|
|
+ mockMvc.perform(post("/api/guide/training-plans/list")
|
|
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
|
+ .content("{\"memberId\":\"abc\"}")
|
|
|
+ .requestAttr("userId", guideId)
|
|
|
+ .requestAttr("currentMemberId", currentMemberId))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(200))
|
|
|
+ .andExpect(jsonPath("$.data[0].id").value(6));
|
|
|
+
|
|
|
+ verify(trainingPlanService).getPlansByGuideAndChild(guideId, currentMemberId);
|
|
|
+ verify(trainingPlanService, never()).getPlansByGuide(anyLong());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getPlanList_emptyBodyMemberId_fallsBackToCurrentMemberId() throws Exception {
|
|
|
+ Long guideId = 9L;
|
|
|
+ Long currentMemberId = 42L;
|
|
|
+ List<TrainingPlan> plans = Collections.singletonList(plan(7L, "空串回退"));
|
|
|
+ when(trainingPlanService.getPlansByGuideAndChild(guideId, currentMemberId)).thenReturn(plans);
|
|
|
+
|
|
|
+ // ParamUtils.getLong 对空字符串返回 null,应回退 currentMemberId
|
|
|
+ mockMvc.perform(post("/api/guide/training-plans/list")
|
|
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
|
+ .content("{\"memberId\":\"\"}")
|
|
|
+ .requestAttr("userId", guideId)
|
|
|
+ .requestAttr("currentMemberId", currentMemberId))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(200))
|
|
|
+ .andExpect(jsonPath("$.data[0].id").value(7));
|
|
|
+
|
|
|
+ verify(trainingPlanService).getPlansByGuideAndChild(guideId, currentMemberId);
|
|
|
+ verify(trainingPlanService, never()).getPlansByGuide(anyLong());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getPlanList_serviceReturnsEmptyList_returns200EmptyData() throws Exception {
|
|
|
+ Long guideId = 9L;
|
|
|
+ when(trainingPlanService.getPlansByGuide(guideId)).thenReturn(Collections.emptyList());
|
|
|
+
|
|
|
+ mockMvc.perform(post("/api/guide/training-plans/list")
|
|
|
+ .contentType(MediaType.APPLICATION_JSON)
|
|
|
+ .content("{}")
|
|
|
+ .requestAttr("userId", guideId))
|
|
|
+ .andExpect(status().isOk())
|
|
|
+ .andExpect(jsonPath("$.code").value(200))
|
|
|
+ .andExpect(jsonPath("$.data").isArray())
|
|
|
+ .andExpect(jsonPath("$.data.length()").value(0));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getPlanList_directInvocation_propagatesServiceResult() {
|
|
|
+ Long guideId = 9L;
|
|
|
+ List<TrainingPlan> plans = new ArrayList<>();
|
|
|
+ plans.add(plan(11L, "直接调用"));
|
|
|
+ when(trainingPlanService.getPlansByGuide(guideId)).thenReturn(plans);
|
|
|
+
|
|
|
+ Map<String, Object> body = new HashMap<>();
|
|
|
+ Result<List<TrainingPlan>> result = controller.getPlanList(guideId, body, null);
|
|
|
+
|
|
|
+ assertEquals(Integer.valueOf(200), result.getCode());
|
|
|
+ assertSame(plans, result.getData());
|
|
|
+ verify(trainingPlanService).getPlansByGuide(guideId);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getPlanList_directInvocation_withMemberId_passesGuideIdAndMemberId() {
|
|
|
+ Long guideId = 9L;
|
|
|
+ Long memberId = 88L;
|
|
|
+ List<TrainingPlan> plans = Collections.singletonList(plan(12L, "成员"));
|
|
|
+ when(trainingPlanService.getPlansByGuideAndChild(guideId, memberId)).thenReturn(plans);
|
|
|
+
|
|
|
+ Map<String, Object> body = new HashMap<>();
|
|
|
+ body.put("memberId", memberId);
|
|
|
+ Result<List<TrainingPlan>> result = controller.getPlanList(guideId, body, null);
|
|
|
+
|
|
|
+ assertEquals(Integer.valueOf(200), result.getCode());
|
|
|
+ assertSame(plans, result.getData());
|
|
|
+ verify(trainingPlanService).getPlansByGuideAndChild(guideId, memberId);
|
|
|
+ verify(trainingPlanService, never()).getPlansByGuide(anyLong());
|
|
|
+ }
|
|
|
+}
|