|
@@ -1,22 +1,26 @@
|
|
|
package com.zxyj.controller;
|
|
package com.zxyj.controller;
|
|
|
|
|
|
|
|
-import javax.annotation.Resource;
|
|
|
|
|
-
|
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.zxyj.common.Result;
|
|
import com.zxyj.common.Result;
|
|
|
import com.zxyj.controller.guide.GuideFamilyTaskController;
|
|
import com.zxyj.controller.guide.GuideFamilyTaskController;
|
|
|
-import com.zxyj.service.TaskService;
|
|
|
|
|
|
|
+import com.zxyj.dto.TaskReviewDTO;
|
|
|
|
|
+import com.zxyj.entity.Child;
|
|
|
|
|
+import com.zxyj.entity.Task;
|
|
|
|
|
+import com.zxyj.entity.User;
|
|
|
import com.zxyj.mapper.ChildMapper;
|
|
import com.zxyj.mapper.ChildMapper;
|
|
|
-import com.zxyj.mapper.UserMapper;
|
|
|
|
|
import com.zxyj.mapper.TaskMapper;
|
|
import com.zxyj.mapper.TaskMapper;
|
|
|
|
|
+import com.zxyj.mapper.UserMapper;
|
|
|
|
|
+import com.zxyj.service.TaskService;
|
|
|
import org.junit.jupiter.api.Test;
|
|
import org.junit.jupiter.api.Test;
|
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
|
|
|
|
|
|
|
-import java.util.Collections;
|
|
|
|
|
-import java.util.List;
|
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
-import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.*;
|
|
|
import static org.mockito.Mockito.when;
|
|
import static org.mockito.Mockito.when;
|
|
|
|
|
|
|
|
@SpringBootTest
|
|
@SpringBootTest
|
|
@@ -37,6 +41,37 @@ public class GuideFamilyTaskControllerTest {
|
|
|
@MockBean
|
|
@MockBean
|
|
|
private TaskMapper taskMapper;
|
|
private TaskMapper taskMapper;
|
|
|
|
|
|
|
|
|
|
+ private User mockUser(Long id, String role, String nickname) {
|
|
|
|
|
+ User user = new User();
|
|
|
|
|
+ user.setId(id);
|
|
|
|
|
+ user.setRole(role);
|
|
|
|
|
+ user.setNickname(nickname);
|
|
|
|
|
+ return user;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Child mockChild(Long id, Long familyId, String nickname) {
|
|
|
|
|
+ Child child = new Child();
|
|
|
|
|
+ child.setId(id);
|
|
|
|
|
+ child.setFamilyId(familyId);
|
|
|
|
|
+ child.setNickname(nickname);
|
|
|
|
|
+ child.setTotalPoints(100);
|
|
|
|
|
+ child.setStreakDays(5);
|
|
|
|
|
+ return child;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Task mockTask(Long id, Long childId, Long familyId, String status) {
|
|
|
|
|
+ Task task = new Task();
|
|
|
|
|
+ task.setId(id);
|
|
|
|
|
+ task.setChildId(childId);
|
|
|
|
|
+ task.setFamilyId(familyId);
|
|
|
|
|
+ task.setTitle("测试任务" + id);
|
|
|
|
|
+ task.setStatus(status);
|
|
|
|
|
+ task.setPoints(10);
|
|
|
|
|
+ return task;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // --- Existing tests ---
|
|
|
|
|
+
|
|
|
@Test
|
|
@Test
|
|
|
public void getBoundFamilies_TeacherRole_Success() {
|
|
public void getBoundFamilies_TeacherRole_Success() {
|
|
|
when(userMapper.selectList(any())).thenReturn(Collections.emptyList());
|
|
when(userMapper.selectList(any())).thenReturn(Collections.emptyList());
|
|
@@ -49,4 +84,176 @@ public class GuideFamilyTaskControllerTest {
|
|
|
Result<?> result = controller.getBoundFamilies(2L, "parent");
|
|
Result<?> result = controller.getBoundFamilies(2L, "parent");
|
|
|
assertNotNull(result);
|
|
assertNotNull(result);
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // --- New tests ---
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void getBoundFamilies_WithFamilies() {
|
|
|
|
|
+ Long guideId = 1L;
|
|
|
|
|
+ User guide = mockUser(guideId, "teacher", "指导师");
|
|
|
|
|
+ guide.setTeacherFamilyIds("100,101");
|
|
|
|
|
+ when(userMapper.selectById(guideId)).thenReturn(guide);
|
|
|
|
|
+
|
|
|
|
|
+ User parent = mockUser(10L, "parent", "家长");
|
|
|
|
|
+ when(userMapper.selectList(any(QueryWrapper.class))).thenReturn(Collections.singletonList(parent));
|
|
|
|
|
+
|
|
|
|
|
+ Child child = mockChild(20L, 100L, "孩子");
|
|
|
|
|
+ when(childMapper.selectList(any(QueryWrapper.class))).thenReturn(Collections.singletonList(child));
|
|
|
|
|
+ when(taskService.getPendingReviewTasksForChild(20L)).thenReturn(new ArrayList<>());
|
|
|
|
|
+
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
|
+ Result<List<Map<String, Object>>> result = (Result<List<Map<String, Object>>>) (Result<?>) controller.getBoundFamilies(guideId, "teacher");
|
|
|
|
|
+ assertNotNull(result);
|
|
|
|
|
+ assertEquals(200, result.getCode());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void getFamilyTaskOverview_WithChildren() {
|
|
|
|
|
+ Long guideId = 1L;
|
|
|
|
|
+ Long familyId = 100L;
|
|
|
|
|
+ Child child = mockChild(20L, familyId, "孩子A");
|
|
|
|
|
+ when(childMapper.selectList(any(QueryWrapper.class))).thenReturn(Collections.singletonList(child));
|
|
|
|
|
+ when(taskService.getTodayTasks(20L)).thenReturn(new ArrayList<>());
|
|
|
|
|
+ when(taskService.getPendingReviewTasksForChild(20L)).thenReturn(new ArrayList<>());
|
|
|
|
|
+ when(taskService.getAllTasksByChild(20L)).thenReturn(new ArrayList<>());
|
|
|
|
|
+
|
|
|
|
|
+ Result<Map<String, Object>> result = controller.getFamilyTaskOverview(guideId, "teacher", familyId);
|
|
|
|
|
+ Map<String, Object> data = result.getData();
|
|
|
|
|
+ assertNotNull(data);
|
|
|
|
|
+ assertEquals(familyId, data.get("familyId"));
|
|
|
|
|
+ assertEquals(1, data.get("totalChildren"));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void getFamilyTaskOverview_NonTeacher_AccessDenied() {
|
|
|
|
|
+ Result<Map<String, Object>> result = controller.getFamilyTaskOverview(1L, "parent", 100L);
|
|
|
|
|
+ assertEquals("Access denied", result.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void getChildTasks_WithPagination() {
|
|
|
|
|
+ Long familyId = 100L;
|
|
|
|
|
+ Long childId = 20L;
|
|
|
|
|
+ List<Task> tasks = Arrays.asList(
|
|
|
|
|
+ mockTask(1L, childId, familyId, "pending"),
|
|
|
|
|
+ mockTask(2L, childId, familyId, "completed")
|
|
|
|
|
+ );
|
|
|
|
|
+ Page<Task> taskPage = new Page<>(1, 10);
|
|
|
|
|
+ taskPage.setRecords(tasks);
|
|
|
|
|
+ taskPage.setTotal(2);
|
|
|
|
|
+
|
|
|
|
|
+ when(taskService.getTaskHistoryByChild(eq(childId), eq(1), eq(10), eq(null))).thenReturn(taskPage);
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
|
|
+ params.put("page", 1);
|
|
|
|
|
+ params.put("size", 10);
|
|
|
|
|
+
|
|
|
|
|
+ Result<Page<Task>> result = controller.getChildTasks(familyId, childId, "teacher", params);
|
|
|
|
|
+ Page<Task> page = result.getData();
|
|
|
|
|
+ assertEquals(2, page.getTotal());
|
|
|
|
|
+ assertEquals(2, page.getRecords().size());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void getPendingReviewTasks_Success() {
|
|
|
|
|
+ Long familyId = 100L;
|
|
|
|
|
+ Long childId = 20L;
|
|
|
|
|
+ List<Task> pendingTasks = Collections.singletonList(
|
|
|
|
|
+ mockTask(5L, childId, familyId, "pending_review")
|
|
|
|
|
+ );
|
|
|
|
|
+ when(taskService.getPendingReviewTasksForChild(childId)).thenReturn(pendingTasks);
|
|
|
|
|
+
|
|
|
|
|
+ Result<List<Task>> result = controller.getPendingReviewTasks(familyId, childId, "teacher");
|
|
|
|
|
+ assertEquals(1, result.getData().size());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void reviewTask_Success() {
|
|
|
|
|
+ Long familyId = 100L;
|
|
|
|
|
+ Long taskId = 1L;
|
|
|
|
|
+ TaskReviewDTO dto = new TaskReviewDTO();
|
|
|
|
|
+ dto.setApproved(true);
|
|
|
|
|
+ dto.setComment("做得好");
|
|
|
|
|
+
|
|
|
|
|
+ when(taskService.reviewTask(eq(taskId), any(TaskReviewDTO.class))).thenReturn(true);
|
|
|
|
|
+
|
|
|
|
|
+ Result<Boolean> result = controller.reviewTask(familyId, taskId, "teacher", dto);
|
|
|
|
|
+ assertTrue(result.getData());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void batchReviewTasks_Success() {
|
|
|
|
|
+ Long familyId = 100L;
|
|
|
|
|
+ List<Long> taskIds = Arrays.asList(1L, 2L, 3L);
|
|
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
|
|
+ params.put("taskIds", taskIds);
|
|
|
|
|
+ params.put("approved", true);
|
|
|
|
|
+
|
|
|
|
|
+ when(taskService.reviewTask(anyLong(), any(TaskReviewDTO.class))).thenReturn(true);
|
|
|
|
|
+
|
|
|
|
|
+ Result<Map<String, Object>> result = controller.batchReviewTasks(familyId, "teacher", params);
|
|
|
|
|
+ Map<String, Object> data = result.getData();
|
|
|
|
|
+ assertEquals(3, data.get("successCount"));
|
|
|
|
|
+ assertEquals(0, data.get("failCount"));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void updateTask_Success() {
|
|
|
|
|
+ Long familyId = 100L;
|
|
|
|
|
+ Long taskId = 1L;
|
|
|
|
|
+ Task task = mockTask(taskId, 20L, familyId, "pending");
|
|
|
|
|
+ when(taskMapper.selectById(taskId)).thenReturn(task);
|
|
|
|
|
+ when(taskMapper.updateById(any(Task.class))).thenReturn(1);
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> taskData = new HashMap<>();
|
|
|
|
|
+ taskData.put("title", "新标题");
|
|
|
|
|
+ taskData.put("description", "新描述");
|
|
|
|
|
+ taskData.put("points", 20);
|
|
|
|
|
+
|
|
|
|
|
+ Result<Boolean> result = controller.updateTask(familyId, taskId, "teacher", taskData);
|
|
|
|
|
+ assertTrue(result.getData());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void updateTask_NotFound_ReturnsError() {
|
|
|
|
|
+ when(taskMapper.selectById(999L)).thenReturn(null);
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> taskData = new HashMap<>();
|
|
|
|
|
+ taskData.put("title", "新标题");
|
|
|
|
|
+ Result<Boolean> result = controller.updateTask(100L, 999L, "teacher", taskData);
|
|
|
|
|
+ assertEquals("任务不存在", result.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void updateTask_WrongFamily_ReturnsError() {
|
|
|
|
|
+ Long taskId = 1L;
|
|
|
|
|
+ Task task = mockTask(taskId, 20L, 999L, "pending"); // different familyId
|
|
|
|
|
+ when(taskMapper.selectById(taskId)).thenReturn(task);
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> taskData = new HashMap<>();
|
|
|
|
|
+ taskData.put("title", "新标题");
|
|
|
|
|
+ Result<Boolean> result = controller.updateTask(100L, taskId, "teacher", taskData);
|
|
|
|
|
+ assertEquals("任务不属于该家庭", result.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void deleteTask_Success() {
|
|
|
|
|
+ Long familyId = 100L;
|
|
|
|
|
+ Long taskId = 1L;
|
|
|
|
|
+ Task task = mockTask(taskId, 20L, familyId, "pending");
|
|
|
|
|
+ when(taskMapper.selectById(taskId)).thenReturn(task);
|
|
|
|
|
+ when(taskMapper.updateById(any(Task.class))).thenReturn(1);
|
|
|
|
|
+
|
|
|
|
|
+ Result<Boolean> result = controller.deleteTask(familyId, taskId, "teacher");
|
|
|
|
|
+ assertTrue(result.getData());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ public void deleteTask_NotFound_ReturnsError() {
|
|
|
|
|
+ when(taskMapper.selectById(999L)).thenReturn(null);
|
|
|
|
|
+ Result<Boolean> result = controller.deleteTask(100L, 999L, "teacher");
|
|
|
|
|
+ assertEquals("任务不存在", result.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+}
|