diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectController.java b/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectController.java index 9baebee4d0..90f57fbdb9 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectController.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectController.java @@ -2,10 +2,11 @@ package io.metersphere.project.controller; import io.metersphere.project.domain.Project; import io.metersphere.project.service.ProjectService; +import io.metersphere.validation.groups.Created; +import io.metersphere.validation.groups.Updated; import jakarta.annotation.Resource; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; import java.util.List; @@ -19,4 +20,14 @@ public class ProjectController { public List selectAll() { return projectService.list(); } + + @PostMapping("add") + public Project add(@Validated({Created.class}) @RequestBody Project project) { + return projectService.add(project); + } + + @PostMapping("edit") + public Project edit(@Validated({Updated.class}) @RequestBody Project project) { + return projectService.edit(project); + } } diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectService.java b/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectService.java index 7fe823810f..9f8ca05b6c 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectService.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectService.java @@ -8,6 +8,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; +import java.util.UUID; @Service @Transactional(rollbackFor = Exception.class) @@ -18,4 +19,17 @@ public class ProjectService { public List list() { return projectMapper.selectByExample(new ProjectExample()); } + + public Project add(Project project) { + project.setId(UUID.randomUUID().toString()); + project.setCreateTime(System.currentTimeMillis()); + project.setUpdateTime(System.currentTimeMillis()); + projectMapper.insertSelective(project); + return project; + } + + public Project edit(Project project) { + projectMapper.updateByPrimaryKeySelective(project); + return projectMapper.selectByPrimaryKey(project.getId()); + } } diff --git a/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectControllerTests.java b/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectControllerTests.java index 4f63b65656..0659d57993 100644 --- a/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectControllerTests.java +++ b/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectControllerTests.java @@ -1,8 +1,13 @@ package io.metersphere.project.controller; -import io.metersphere.sdk.util.LogUtils; +import com.jayway.jsonpath.JsonPath; +import io.metersphere.project.domain.Project; +import io.metersphere.sdk.util.JSON; import jakarta.annotation.Resource; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; @@ -16,13 +21,51 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @SpringBootTest @AutoConfigureMockMvc +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) public class ProjectControllerTests { @Resource private MockMvc mockMvc; + private static String projectId; + + // 添加项目 @Test + @Order(1) + public void testAddProject() throws Exception { + Project project = new Project(); + project.setName("test"); + project.setCreateUser("admin"); + project.setWorkspaceId("default"); + + var result = mockMvc.perform(MockMvcRequestBuilders.post("/project/add") + .content(JSON.toJSONString(project)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andReturn(); + projectId = JsonPath.read(result.getResponse().getContentAsString(), "$.data.id"); + } + + @Test + @Order(2) + public void testEditProject() throws Exception { + Project project = new Project(); + project.setId(projectId); + project.setName("test2"); + project.setCreateUser("admin"); + project.setWorkspaceId("default"); + + mockMvc.perform(MockMvcRequestBuilders.post("/project/edit") + .content(JSON.toJSONString(project)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andDo(print()); + } + + @Test + @Order(3) public void testSelectAll() throws Exception { - LogUtils.info("testSelectAll"); mockMvc.perform(MockMvcRequestBuilders.get("/project/list-all")) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) @@ -30,12 +73,4 @@ public class ProjectControllerTests { .andDo(print()); } - @Test - public void testSelectAll2() throws Exception { - mockMvc.perform(MockMvcRequestBuilders.get("/project/list-all")) - .andExpect(status().isOk()) - .andExpect(content().contentType(MediaType.APPLICATION_JSON)) -// .andExpect(jsonPath("$.person.name").value("Jason")) - .andDo(print()); - } }