| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- package com.etotem.cfc.service;
- import com.etotem.cfc.dto.FamilyPlanCreateDTO;
- import com.etotem.cfc.entity.HealthPlan;
- import com.etotem.cfc.mapper.HealthPlanMapper;
- import com.etotem.cfc.service.impl.HealthPlanServiceImpl;
- import org.junit.jupiter.api.BeforeEach;
- import org.junit.jupiter.api.Test;
- import org.mockito.ArgumentCaptor;
- import org.mockito.InjectMocks;
- import org.mockito.Mock;
- import org.mockito.MockitoAnnotations;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import static org.junit.jupiter.api.Assertions.assertThrows;
- import static org.junit.jupiter.api.Assertions.assertTrue;
- import static org.mockito.ArgumentMatchers.any;
- import static org.mockito.Mockito.verify;
- import static org.mockito.Mockito.when;
- /**
- * 方案新增/编辑契约:create 固定 family_id/status 默认 active;update 校验归属(防跨家庭越权)。
- */
- class FamilyWorkbenchPlanWriteTest {
- @Mock private HealthPlanMapper planMapper;
- @InjectMocks private HealthPlanServiceImpl service;
- @BeforeEach
- void setUp() {
- MockitoAnnotations.openMocks(this);
- }
- private FamilyPlanCreateDTO dto() {
- FamilyPlanCreateDTO d = new FamilyPlanCreateDTO();
- d.setPlanType("gut");
- d.setMemberName("小明");
- d.setGoal("改善肠道菌群");
- d.setTotalDays(30);
- return d;
- }
- @Test
- void 创建方案_familyId固定_status默认active_返回id() {
- when(planMapper.insert(any(HealthPlan.class))).thenAnswer(inv -> {
- HealthPlan p = inv.getArgument(0);
- p.setId(321L);
- return 1;
- });
- Long id = service.createFamilyPlan(5L, 88L, dto());
- ArgumentCaptor<HealthPlan> cap = ArgumentCaptor.forClass(HealthPlan.class);
- verify(planMapper).insert(cap.capture());
- HealthPlan p = cap.getValue();
- assertEquals(5L, p.getFamilyId());
- assertEquals("active", p.getStatus());
- assertEquals("gut", p.getPlanType());
- assertEquals("改善肠道菌群", p.getGoal());
- assertEquals(Integer.valueOf(30), p.getTotalDays());
- assertEquals(321L, id);
- }
- @Test
- void 编辑方案_跨家庭_拒绝() {
- HealthPlan other = new HealthPlan();
- other.setId(55L);
- other.setFamilyId(9L);
- when(planMapper.selectById(55L)).thenReturn(other);
- RuntimeException e = assertThrows(RuntimeException.class,
- () -> service.updateFamilyPlan(55L, 5L, dto()));
- assertTrue(e.getMessage().contains("方案不属于该家庭"));
- }
- @Test
- void 编辑方案_同家庭_字段更新() {
- HealthPlan p = new HealthPlan();
- p.setId(55L);
- p.setFamilyId(5L);
- p.setGoal("旧目标");
- when(planMapper.selectById(55L)).thenReturn(p);
- when(planMapper.updateById(any(HealthPlan.class))).thenReturn(1);
- boolean ok = service.updateFamilyPlan(55L, 5L, dto());
- verify(planMapper).updateById(p);
- assertTrue(ok);
- assertEquals("改善肠道菌群", p.getGoal());
- }
- }
|