131 lines
5.0 KiB
Java
131 lines
5.0 KiB
Java
package com.guo.learningprogresstracker.controller;
|
|
|
|
import cn.dev33.satoken.stp.StpUtil;
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.guo.learningprogresstracker.dto.request.TaskRequest;
|
|
import com.guo.learningprogresstracker.entity.CommonResult;
|
|
import com.guo.learningprogresstracker.entity.TaskEntity;
|
|
import com.guo.learningprogresstracker.service.impl.TasksServiceImpl;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.test.web.servlet.MockMvc;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
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.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.MockMvcResultMatchers.jsonPath;
|
|
|
|
@SpringBootTest
|
|
@AutoConfigureMockMvc
|
|
@Slf4j
|
|
class TaskControllerTest {
|
|
|
|
@Autowired
|
|
private TaskController taskController;
|
|
|
|
@Autowired
|
|
private MockMvc mockMvc;
|
|
|
|
@Autowired
|
|
private ObjectMapper jacksonObjectMapper;
|
|
@Autowired
|
|
private TasksServiceImpl tasksServiceImpl;
|
|
|
|
@BeforeEach
|
|
void setUp() {
|
|
StpUtil.login("1", "test_driver");
|
|
}
|
|
|
|
@Test
|
|
void addTask() throws Exception {
|
|
TaskRequest taskRequest = getTaskRequest("测试任务名称-" + System.nanoTime());
|
|
log.info("testInfo");
|
|
String s = jacksonObjectMapper.writeValueAsString(taskRequest);
|
|
System.out.println("Token Value: " + StpUtil.getTokenValue());
|
|
mockMvc.perform(post("/tasks")
|
|
.header("satoken", StpUtil.getTokenValue())
|
|
.contentType(MediaType.APPLICATION_JSON)
|
|
.content(s))
|
|
.andDo(print())
|
|
.andExpect(jsonPath("$.code").value(200));
|
|
|
|
}
|
|
|
|
private static TaskRequest getTaskRequest(String taskName) {
|
|
TaskRequest taskRequest = new TaskRequest();
|
|
taskRequest.setTaskName(taskName);
|
|
taskRequest.setTaskDescription("测试任务描述");
|
|
taskRequest.setMaterialUrl("http://example.com/material");
|
|
taskRequest.setUrgency(5);
|
|
taskRequest.setImportance(3);
|
|
taskRequest.setContentDifficulty(4);
|
|
taskRequest.setFutureValue(4);
|
|
taskRequest.setSubjectivePriority(1);
|
|
return taskRequest;
|
|
}
|
|
|
|
/**
|
|
* 测试获取全部数据
|
|
* @throws Exception
|
|
*/
|
|
@Test
|
|
void getTask() throws Exception {
|
|
String taskId = "1";
|
|
mockMvc.perform(get("/tasks/"+taskId)
|
|
.header("satoken",StpUtil.getTokenValue()))
|
|
.andDo(print())
|
|
.andExpect(jsonPath("$.code").value(200))
|
|
.andExpect(jsonPath("$.data").exists());
|
|
|
|
}
|
|
|
|
@Test
|
|
void updateTask() throws Exception {
|
|
|
|
TaskRequest taskRequest = getTaskRequest("测试数据名称");
|
|
taskRequest.setId(1);
|
|
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
|
|
void deleteTask() throws Exception {
|
|
CommonResult commonResult = taskController.addTask(getTaskRequest("待删除任务-" + System.nanoTime()));
|
|
assertEquals(commonResult.getCode(),200);
|
|
TaskEntity taskEntity = tasksServiceImpl.getOne(Wrappers.<TaskEntity>lambdaQuery().eq(TaskEntity::getTaskNum, commonResult.getData()));
|
|
assertNotNull(taskEntity,"未能找到新创建的任务实体");
|
|
mockMvc.perform(delete("/tasks/"+taskEntity.getId())
|
|
.header("satoken",StpUtil.getTokenValue()))
|
|
.andDo(print())
|
|
.andExpect(jsonPath("$.code").value(200))
|
|
.andExpect(jsonPath("$.data").isEmpty());
|
|
}
|
|
|
|
@Test
|
|
void listTasks() throws Exception {
|
|
mockMvc.perform(get("/tasks")
|
|
.header("satoken",StpUtil.getTokenValue()))
|
|
.andDo(print())
|
|
.andExpect(jsonPath("$.code").value(200))
|
|
.andExpect(jsonPath("$.data").exists());
|
|
}
|
|
}
|