|
@@ -0,0 +1,110 @@
|
|
|
|
|
+package com.etotem.cfc.unit;
|
|
|
|
|
+
|
|
|
|
|
+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 java.lang.reflect.Field;
|
|
|
|
|
+
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
|
|
+
|
|
|
|
|
+class HealthPlanReviewGuardTest {
|
|
|
|
|
+
|
|
|
|
|
+ private HealthPlanServiceImpl service;
|
|
|
|
|
+ private HealthPlanMapper mapper;
|
|
|
|
|
+
|
|
|
|
|
+ @BeforeEach
|
|
|
|
|
+ void setUp() throws Exception {
|
|
|
|
|
+ service = new HealthPlanServiceImpl();
|
|
|
|
|
+ mapper = mock(HealthPlanMapper.class);
|
|
|
|
|
+ Field f = HealthPlanServiceImpl.class.getDeclaredField("healthPlanMapper");
|
|
|
|
|
+ f.setAccessible(true);
|
|
|
|
|
+ f.set(service, mapper);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private HealthPlan planWithStatus(String status) {
|
|
|
|
|
+ HealthPlan p = new HealthPlan();
|
|
|
|
|
+ p.setId(5L);
|
|
|
|
|
+ p.setFamilyId(10L);
|
|
|
|
|
+ p.setStatus(status);
|
|
|
|
|
+ return p;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ---- rejectPlan 状态守卫 ----
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void 已发布方案不可再次驳回() {
|
|
|
|
|
+ when(mapper.selectById(5L)).thenReturn(planWithStatus("published"));
|
|
|
|
|
+ RuntimeException ex = assertThrows(RuntimeException.class,
|
|
|
|
|
+ () -> service.rejectPlan(5L, 1L, "x"));
|
|
|
|
|
+ assertEquals("方案状态不允许审核", ex.getMessage());
|
|
|
|
|
+ verify(mapper, never()).updateById(any(HealthPlan.class));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void 已驳回方案不可再次驳回() {
|
|
|
|
|
+ when(mapper.selectById(5L)).thenReturn(planWithStatus("rejected"));
|
|
|
|
|
+ RuntimeException ex = assertThrows(RuntimeException.class,
|
|
|
|
|
+ () -> service.rejectPlan(5L, 1L, "x"));
|
|
|
|
|
+ assertEquals("方案状态不允许审核", ex.getMessage());
|
|
|
|
|
+ verify(mapper, never()).updateById(any(HealthPlan.class));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void pending_review方案可驳回() {
|
|
|
|
|
+ HealthPlan p = planWithStatus("pending_review");
|
|
|
|
|
+ when(mapper.selectById(5L)).thenReturn(p);
|
|
|
|
|
+ when(mapper.updateById(any(HealthPlan.class))).thenReturn(1);
|
|
|
|
|
+
|
|
|
|
|
+ HealthPlan r = service.rejectPlan(5L, 1L, "内容需调整");
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals("rejected", r.getStatus());
|
|
|
|
|
+ assertEquals(Long.valueOf(1L), r.getReviewedBy());
|
|
|
|
|
+ assertEquals("内容需调整", r.getReviewComment());
|
|
|
|
|
+ verify(mapper).updateById(p);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void draft方案可驳回() {
|
|
|
|
|
+ HealthPlan p = planWithStatus("draft");
|
|
|
|
|
+ when(mapper.selectById(5L)).thenReturn(p);
|
|
|
|
|
+ when(mapper.updateById(any(HealthPlan.class))).thenReturn(1);
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals("rejected", service.rejectPlan(5L, 1L, "c").getStatus());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void 方案不存在抛方案不存在() {
|
|
|
|
|
+ when(mapper.selectById(5L)).thenReturn(null);
|
|
|
|
|
+ RuntimeException ex = assertThrows(RuntimeException.class,
|
|
|
|
|
+ () -> service.rejectPlan(5L, 1L, "x"));
|
|
|
|
|
+ assertEquals("方案不存在", ex.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ---- getPlanForFamily IDOR 反查 ----
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void getPlanForFamily_匹配返回方案() {
|
|
|
|
|
+ when(mapper.selectById(99L)).thenReturn(planWithStatus("pending_review"));
|
|
|
|
|
+ HealthPlan p = service.getPlanForFamily(99L, 10L);
|
|
|
|
|
+ assertNotNull(p);
|
|
|
|
|
+ assertEquals(Long.valueOf(99L), p.getId());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void getPlanForFamily_不属于该家庭返回null() {
|
|
|
|
|
+ when(mapper.selectById(99L)).thenReturn(planWithStatus("pending_review"));
|
|
|
|
|
+ assertNull(service.getPlanForFamily(99L, 20L));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void getPlanForFamily_方案不存在抛异常() {
|
|
|
|
|
+ when(mapper.selectById(99L)).thenReturn(null);
|
|
|
|
|
+ assertEquals("方案不存在",
|
|
|
|
|
+ assertThrows(RuntimeException.class, () -> service.getPlanForFamily(99L, 10L)).getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+}
|