1. 配置数据库日志输出 2. 任务更新API与测试类 3. 任务删除API与测试类
This commit is contained in:
@@ -16,6 +16,8 @@ import org.springframework.web.bind.annotation.PutMapping;
|
|||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.rmi.ServerException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 任务控制层
|
* 任务控制层
|
||||||
* @author guo
|
* @author guo
|
||||||
@@ -49,9 +51,9 @@ public class TaskController {
|
|||||||
|
|
||||||
@DeleteMapping("/tasks/{taskId}")
|
@DeleteMapping("/tasks/{taskId}")
|
||||||
@Operation(summary = "删除指定任务")
|
@Operation(summary = "删除指定任务")
|
||||||
public CommonResult deleteTask(@PathVariable("taskId") String taskId) {
|
public CommonResult deleteTask(@PathVariable("taskId") String taskId) throws ServerException {
|
||||||
// return CommonResult.success(tasksService.deleteTask(taskId));
|
tasksService.deleteTask(taskId);
|
||||||
return null;
|
return CommonResult.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/tasks")
|
@GetMapping("/tasks")
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ public class TaskRequest {
|
|||||||
|
|
||||||
public interface Create{}
|
public interface Create{}
|
||||||
@Null(message = "创建时不可指定任务ID",groups = Ops.CreateG.class)
|
@Null(message = "创建时不可指定任务ID",groups = Ops.CreateG.class)
|
||||||
@NotEmpty(message = "更新时必须指定任务ID",groups = Ops.UpdateG.class)
|
@NotNull(message = "更新时必须指定任务ID",groups = Ops.UpdateG.class)
|
||||||
private Integer id;
|
private Integer id;
|
||||||
/**
|
/**
|
||||||
* 学习任务的名称
|
* 学习任务的名称
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ import com.guo.learningprogresstracker.dto.response.TasksListInfoResponse;
|
|||||||
import com.guo.learningprogresstracker.entity.TaskEntity;
|
import com.guo.learningprogresstracker.entity.TaskEntity;
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
|
||||||
|
import java.rmi.ServerException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author guo
|
* @author guo
|
||||||
* @description 针对表【tasks(存储学习任务的基本信息,包括优先级的多维度计算)】的数据库操作Service
|
* @description 针对表【tasks(存储学习任务的基本信息,包括优先级的多维度计算)】的数据库操作Service
|
||||||
@@ -20,4 +22,6 @@ public interface TasksService extends IService<TaskEntity> {
|
|||||||
TaskInfoResponse getTask(String taskId);
|
TaskInfoResponse getTask(String taskId);
|
||||||
|
|
||||||
void updateTask(String taskId, TaskRequest updatedTask);
|
void updateTask(String taskId, TaskRequest updatedTask);
|
||||||
|
|
||||||
|
void deleteTask(String taskId) throws ServerException;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,8 +13,11 @@ import com.guo.learningprogresstracker.mapper.TasksMapper;
|
|||||||
import com.guo.learningprogresstracker.utils.CalculatedPriorityTool;
|
import com.guo.learningprogresstracker.utils.CalculatedPriorityTool;
|
||||||
import com.guo.learningprogresstracker.utils.GenerateNumTool;
|
import com.guo.learningprogresstracker.utils.GenerateNumTool;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.rmi.ServerException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author guo
|
* @author guo
|
||||||
* @description 针对表【tasks(存储学习任务的基本信息,包括优先级的多维度计算)】的数据库操作Service实现
|
* @description 针对表【tasks(存储学习任务的基本信息,包括优先级的多维度计算)】的数据库操作Service实现
|
||||||
@@ -22,6 +25,7 @@ import org.springframework.stereotype.Service;
|
|||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
|
@Slf4j
|
||||||
public class TasksServiceImpl extends ServiceImpl<TasksMapper, TaskEntity>
|
public class TasksServiceImpl extends ServiceImpl<TasksMapper, TaskEntity>
|
||||||
implements TasksService{
|
implements TasksService{
|
||||||
|
|
||||||
@@ -58,6 +62,14 @@ public class TasksServiceImpl extends ServiceImpl<TasksMapper, TaskEntity>
|
|||||||
this.updateById(taskEntity);
|
this.updateById(taskEntity);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteTask(String taskId) throws ServerException {
|
||||||
|
boolean b = this.removeById(taskId);
|
||||||
|
if(!b){
|
||||||
|
throw new ServerException("未能正常删除任务");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
package com.guo.learningprogresstracker.controller;
|
package com.guo.learningprogresstracker.controller;
|
||||||
|
|
||||||
import cn.dev33.satoken.stp.StpUtil;
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
|
|
||||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.guo.learningprogresstracker.dto.request.TaskRequest;
|
import com.guo.learningprogresstracker.dto.request.TaskRequest;
|
||||||
import com.guo.learningprogresstracker.entity.CommonResult;
|
import com.guo.learningprogresstracker.entity.CommonResult;
|
||||||
import com.guo.learningprogresstracker.entity.TaskEntity;
|
import com.guo.learningprogresstracker.entity.TaskEntity;
|
||||||
import com.guo.learningprogresstracker.service.impl.TasksServiceImpl;
|
import com.guo.learningprogresstracker.service.impl.TasksServiceImpl;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -23,11 +23,13 @@ import static org.mockito.Mockito.when;
|
|||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
@AutoConfigureMockMvc
|
@AutoConfigureMockMvc
|
||||||
|
@Slf4j
|
||||||
class TaskControllerTest {
|
class TaskControllerTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@@ -49,7 +51,7 @@ class TaskControllerTest {
|
|||||||
@Test
|
@Test
|
||||||
void addTask() throws Exception {
|
void addTask() throws Exception {
|
||||||
TaskRequest taskRequest = getTaskRequest();
|
TaskRequest taskRequest = getTaskRequest();
|
||||||
|
log.info("testInfo");
|
||||||
String s = jacksonObjectMapper.writeValueAsString(taskRequest);
|
String s = jacksonObjectMapper.writeValueAsString(taskRequest);
|
||||||
System.out.println("Token Value: " + StpUtil.getTokenValue());
|
System.out.println("Token Value: " + StpUtil.getTokenValue());
|
||||||
mockMvc.perform(post("/tasks")
|
mockMvc.perform(post("/tasks")
|
||||||
@@ -104,23 +106,32 @@ class TaskControllerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void updateTask() {
|
void updateTask() throws Exception {
|
||||||
|
|
||||||
TaskRequest taskRequest = mock(TaskRequest.class);
|
TaskRequest taskRequest = mock(TaskRequest.class);
|
||||||
when(taskRequest.getTaskName()).thenReturn("测试数据名称");
|
when(taskRequest.getTaskName()).thenReturn("测试数据名称");
|
||||||
when(taskRequest.getMaterialUrl()).thenReturn("http://example.com/material");
|
when(taskRequest.getMaterialUrl()).thenReturn("http://example.com/material");
|
||||||
CommonResult commonResult = taskController.updateTask("0", taskRequest);
|
when(taskRequest.getId()).thenReturn(1);
|
||||||
assertEquals(commonResult.getData(), 200, "任务信息更新失败");
|
String s = jacksonObjectMapper.writeValueAsString(taskRequest);
|
||||||
|
|
||||||
|
mockMvc.perform(put("/tasks/"+taskRequest.getId())
|
||||||
|
.header("satoken",StpUtil.getTokenValue())
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.content(s))
|
||||||
|
.andDo(print())
|
||||||
|
.andExpect(jsonPath("$.code").value(200))
|
||||||
|
.andExpect(jsonPath("$.data").isEmpty());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void deleteTask() throws Exception {
|
void deleteTask() throws Exception {
|
||||||
CommonResult commonResult = taskController.addTask(getTaskRequest());
|
CommonResult commonResult = taskController.addTask(getTaskRequest());
|
||||||
assertEquals(ObjectUtils.isNotEmpty(commonResult.getMessage()),"任务创建失败");
|
assertEquals(commonResult.getCode(),200);
|
||||||
TaskEntity taskEntity = tasksServiceImpl.getOne(Wrappers.<TaskEntity>lambdaQuery().eq(TaskEntity::getTaskNum, commonResult.getMessage()));
|
TaskEntity taskEntity = tasksServiceImpl.getOne(Wrappers.<TaskEntity>lambdaQuery().eq(TaskEntity::getTaskNum, commonResult.getMessage()));
|
||||||
assertNotNull(taskEntity,"未能找到新创建的任务实体");
|
assertNotNull(taskEntity,"未能找到新创建的任务实体");
|
||||||
mockMvc.perform(delete("/tasks/"+taskEntity.getId()))
|
mockMvc.perform(delete("/tasks/"+taskEntity.getId())
|
||||||
|
.header("satoken",StpUtil.getTokenValue()))
|
||||||
.andDo(print())
|
.andDo(print())
|
||||||
.andExpect(jsonPath("$.code").value(200))
|
.andExpect(jsonPath("$.code").value(200))
|
||||||
.andExpect(jsonPath("$.data").isEmpty());
|
.andExpect(jsonPath("$.data").isEmpty());
|
||||||
|
|||||||
Reference in New Issue
Block a user