|
|
@@ -10,13 +10,18 @@ import com.etotem.cfc.mapper.TaskPlanItemMapper;
|
|
|
import com.etotem.cfc.mapper.TaskTemplateItemMapper;
|
|
|
import com.etotem.cfc.service.TaskPlanService;
|
|
|
import com.etotem.cfc.service.TaskTemplatePackageService;
|
|
|
+import com.etotem.cfc.dto.ApplyPackageDTO;
|
|
|
+import com.etotem.cfc.entity.TaskTemplatePackage;
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
import java.util.Collections;
|
|
|
+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.ArgumentMatchers.eq;
|
|
|
import static org.mockito.Mockito.*;
|
|
|
@@ -110,4 +115,44 @@ class TaskPlanServiceTest {
|
|
|
assertEquals(true, ok);
|
|
|
verify(taskMapper, times(1)).updateById(any(Task.class));
|
|
|
}
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void applyAndActivate_无active实例时新建并置active() {
|
|
|
+ ApplyPackageDTO dto = new ApplyPackageDTO();
|
|
|
+ dto.setPackageId(30L);
|
|
|
+ dto.setMemberId(20L);
|
|
|
+
|
|
|
+ // findActiveByFamilyAndPackage → null(无 active 实例)
|
|
|
+ when(instanceMapper.selectOne(any())).thenReturn(null);
|
|
|
+
|
|
|
+ // applyPackage 内部:packageService.getPackageById → 返回 mock package
|
|
|
+ TaskTemplatePackage pkg = new TaskTemplatePackage();
|
|
|
+ pkg.setId(30L);
|
|
|
+ pkg.setName("测试套餐");
|
|
|
+ pkg.setCycleDays(7);
|
|
|
+ when(packageService.getPackageById(30L)).thenReturn(pkg);
|
|
|
+ // templateItemMapper.selectList → 空列表(避免 NPE)
|
|
|
+ when(templateItemMapper.selectList(any())).thenReturn(Collections.emptyList());
|
|
|
+ // instanceMapper.insert → 返回1,用 Answer 捕获实例并设 ID
|
|
|
+ TaskPlanInstance capturedInstance = new TaskPlanInstance();
|
|
|
+ when(instanceMapper.insert(any(TaskPlanInstance.class))).thenAnswer(invocation -> {
|
|
|
+ capturedInstance.setId(100L);
|
|
|
+ return 1;
|
|
|
+ });
|
|
|
+
|
|
|
+ // generateTasksFromTemplate → 幂等检查返回 0(无旧任务),生成 0 条模板任务
|
|
|
+ when(taskMapper.selectCount(any())).thenReturn(0L);
|
|
|
+ // buildTodayDeadline 用 Calendar,测试中不直接调用(private),但 generateTasksFromTemplate 内部会调用
|
|
|
+ // 由于 templateItemMapper 已 mock 返回空,generateTasksFromTemplate 只会生成引导任务(1条)
|
|
|
+ // 但引导任务也调 taskMapper.insert,需要 mock
|
|
|
+ when(taskMapper.insert(any(Task.class))).thenReturn(1);
|
|
|
+
|
|
|
+ Map<String, Object> result = service.applyAndActivate(dto, 10L, 99L);
|
|
|
+
|
|
|
+ assertNotNull(result);
|
|
|
+ assertEquals(100L, result.get("planId"));
|
|
|
+ assertEquals(1, result.get("taskCount")); // 引导任务 1 条
|
|
|
+ // 状态置 active:instanceMapper.updateById 被调用一次
|
|
|
+ verify(instanceMapper, times(1)).updateById(any(TaskPlanInstance.class));
|
|
|
+ }
|
|
|
}
|