|
|
@@ -0,0 +1,171 @@
|
|
|
+package com.etotem.cfc.controller;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
+import com.etotem.cfc.entity.FamilyMember;
|
|
|
+import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
|
+import com.etotem.cfc.service.IndicatorFavoriteService;
|
|
|
+import org.apache.ibatis.builder.MapperBuilderAssistant;
|
|
|
+import org.junit.jupiter.api.BeforeAll;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.springframework.test.util.ReflectionTestUtils;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.never;
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 指标收藏控制器单测(T03 · §6),覆盖验收要点:
|
|
|
+ * <ul>
|
|
|
+ * <li>toggle/list/remove:正常路径透传 Service 结果</li>
|
|
|
+ * <li>参数缺失(memberId/definitionId/sourceType)→ 400</li>
|
|
|
+ * <li>memberId 不属当前家庭 → 400(越权拦截)</li>
|
|
|
+ * </ul>
|
|
|
+ */
|
|
|
+class IndicatorFavoriteControllerTest {
|
|
|
+
|
|
|
+ private IndicatorFavoriteController controller;
|
|
|
+ private IndicatorFavoriteService indicatorFavoriteService;
|
|
|
+ private FamilyMemberMapper familyMemberMapper;
|
|
|
+
|
|
|
+ @BeforeAll
|
|
|
+ static void initMybatisPlusTableInfo() {
|
|
|
+ // 初始化 MP TableInfo 缓存,使 LambdaQueryWrapper 在无 Spring 上下文可用
|
|
|
+ TableInfoHelper.initTableInfo(
|
|
|
+ new MapperBuilderAssistant(new MybatisConfiguration(), ""), FamilyMember.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() {
|
|
|
+ controller = new IndicatorFavoriteController();
|
|
|
+ indicatorFavoriteService = mock(IndicatorFavoriteService.class);
|
|
|
+ familyMemberMapper = mock(FamilyMemberMapper.class);
|
|
|
+ ReflectionTestUtils.setField(controller, "indicatorFavoriteService", indicatorFavoriteService);
|
|
|
+ ReflectionTestUtils.setField(controller, "familyMemberMapper", familyMemberMapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ private FamilyMember member(Long id, Long familyId) {
|
|
|
+ FamilyMember m = new FamilyMember();
|
|
|
+ m.setId(id);
|
|
|
+ m.setFamilyId(familyId);
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> params(String memberId, String sourceType, String definitionId) {
|
|
|
+ Map<String, Object> p = new HashMap<>();
|
|
|
+ if (memberId != null) p.put("memberId", memberId);
|
|
|
+ if (sourceType != null) p.put("sourceType", sourceType);
|
|
|
+ if (definitionId != null) p.put("definitionId", definitionId);
|
|
|
+ return p;
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== toggle ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void toggle_正常() {
|
|
|
+ when(familyMemberMapper.selectOne(any())).thenReturn(member(42L, 9L));
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("definitionId", 456L);
|
|
|
+ data.put("added", true);
|
|
|
+ when(indicatorFavoriteService.toggle(7L, 9L, 42L, "gut_flora", 456L)).thenReturn(data);
|
|
|
+
|
|
|
+ Result<?> r = controller.toggle(params("42", "gut_flora", "456"), 7L, 9L);
|
|
|
+
|
|
|
+ assertEquals(200, r.getCode());
|
|
|
+ assertNotNull(r.getData());
|
|
|
+ assertEquals(456L, ((Map<?, ?>) r.getData()).get("definitionId"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void toggle_缺memberId_400() {
|
|
|
+ Result<?> r = controller.toggle(params(null, "gut_flora", "456"), 7L, 9L);
|
|
|
+ assertEquals(400, r.getCode());
|
|
|
+ verify(indicatorFavoriteService, never()).toggle(any(), any(), any(), any(), any());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void toggle_缺sourceType_400() {
|
|
|
+ Result<?> r = controller.toggle(params("42", "", "456"), 7L, 9L);
|
|
|
+ assertEquals(400, r.getCode());
|
|
|
+ verify(indicatorFavoriteService, never()).toggle(any(), any(), any(), any(), any());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void toggle_缺definitionId_400() {
|
|
|
+ Result<?> r = controller.toggle(params("42", "gut_flora", null), 7L, 9L);
|
|
|
+ assertEquals(400, r.getCode());
|
|
|
+ verify(indicatorFavoriteService, never()).toggle(any(), any(), any(), any(), any());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void toggle_成员不属家庭_400() {
|
|
|
+ when(familyMemberMapper.selectOne(any())).thenReturn(null);
|
|
|
+
|
|
|
+ Result<?> r = controller.toggle(params("42", "gut_flora", "456"), 7L, 9L);
|
|
|
+
|
|
|
+ assertEquals(400, r.getCode());
|
|
|
+ verify(indicatorFavoriteService, never()).toggle(any(), any(), any(), any(), any());
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== list ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void list_正常() {
|
|
|
+ when(familyMemberMapper.selectOne(any())).thenReturn(member(42L, 9L));
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("memberId", 42L);
|
|
|
+ data.put("sourceType", "gut_flora");
|
|
|
+ data.put("definitionIds", new long[]{456L, 789L});
|
|
|
+ when(indicatorFavoriteService.list(7L, 9L, 42L, "gut_flora")).thenReturn(data);
|
|
|
+
|
|
|
+ Result<?> r = controller.list(params("42", "gut_flora", null), 7L, 9L);
|
|
|
+
|
|
|
+ assertEquals(200, r.getCode());
|
|
|
+ assertNotNull(r.getData());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void list_缺sourceType_400() {
|
|
|
+ Result<?> r = controller.list(params("42", null, null), 7L, 9L);
|
|
|
+ assertEquals(400, r.getCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void list_成员不属家庭_400() {
|
|
|
+ when(familyMemberMapper.selectOne(any())).thenReturn(null);
|
|
|
+ Result<?> r = controller.list(params("42", "gut_flora", null), 7L, 9L);
|
|
|
+ assertEquals(400, r.getCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== remove ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void remove_正常() {
|
|
|
+ when(familyMemberMapper.selectOne(any())).thenReturn(member(42L, 9L));
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("definitionId", 456L);
|
|
|
+ data.put("removed", true);
|
|
|
+ when(indicatorFavoriteService.remove(7L, 9L, 42L, "gut_flora", 456L)).thenReturn(data);
|
|
|
+
|
|
|
+ Result<?> r = controller.remove(params("42", "gut_flora", "456"), 7L, 9L);
|
|
|
+
|
|
|
+ assertEquals(200, r.getCode());
|
|
|
+ assertNotNull(r.getData());
|
|
|
+ assertEquals(true, ((Map<?, ?>) r.getData()).get("removed"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void remove_缺参_400() {
|
|
|
+ Result<?> r = controller.remove(params("42", "gut_flora", null), 7L, 9L);
|
|
|
+ assertEquals(400, r.getCode());
|
|
|
+ }
|
|
|
+}
|