diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java b/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java index 8a9c73ec86..b29605e905 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java @@ -2,12 +2,11 @@ package io.metersphere.api.controller; import io.metersphere.api.dto.ApiDefinitionDTO; import io.metersphere.api.service.ApiDefinitionService; +import io.metersphere.validation.groups.Created; import jakarta.annotation.Resource; import org.springframework.http.MediaType; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestPart; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.util.List; @@ -19,9 +18,9 @@ public class ApiDefinitionController { @Resource private ApiDefinitionService apiDefinitionService; - @PostMapping(value = "/create", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE}) - public ApiDefinitionDTO create(@RequestPart("request") ApiDefinitionDTO request, - @RequestPart(value = "files", required = false) List bodyFiles) { + @PostMapping(value = "/add", consumes = MediaType.APPLICATION_JSON_VALUE) + public ApiDefinitionDTO create(@Validated({Created.class}) @RequestBody ApiDefinitionDTO request, + @RequestParam(value = "files") List bodyFiles) { return apiDefinitionService.create(request, bodyFiles); } } diff --git a/backend/services/api-test/src/main/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java b/backend/services/api-test/src/main/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java index 63e8ff27a0..9c3f286afa 100644 --- a/backend/services/api-test/src/main/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java +++ b/backend/services/api-test/src/main/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java @@ -1,9 +1,10 @@ package io.metersphere.api.controller; -import com.fasterxml.jackson.databind.ObjectMapper; import io.metersphere.api.dto.ApiDefinitionDTO; import io.metersphere.sdk.util.LogUtils; +import io.metersphere.utils.JsonUtils; import jakarta.annotation.Resource; +import org.junit.Before; import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; @@ -16,6 +17,8 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; + @SpringBootTest @TestMethodOrder(MethodOrderer.OrderAnnotation.class) @@ -25,24 +28,44 @@ public class ApiDefinitionControllerTests { private MockMvc mockMvc; private final static String prefix = "/api/definition"; + @Before + public void init() { + LogUtils.info("init base api test"); + } + @Test @Order(1) public void testCreate() throws Exception { LogUtils.info("create api test"); // 创建一个 MockMultipartFile 对象,用于模拟文件上传 - MockMultipartFile file = new MockMultipartFile("files", "test.txt", "application/octet-stream", "Test content".getBytes()); + MockMultipartFile file = new MockMultipartFile("files", "files", MediaType.APPLICATION_OCTET_STREAM_VALUE, "Test content".getBytes()); // 创建一个 ApiDefinitionDTO 对象,用于模拟请求的一部分 ApiDefinitionDTO request = new ApiDefinitionDTO(); // 补充属性内容 request.setId("test-api-id"); + request.setCreateUser("test-user"); + request.setCreateTime(System.currentTimeMillis()); + request.setUpdateUser("test-api-id"); + request.setUpdateTime(System.currentTimeMillis()); + request.setProjectId("test-project-id"); + request.setName("test-api-name"); + request.setMethod("test-api-method"); + request.setPath("test-api-path"); + request.setProtocol("test-api-protocol"); + request.setPos(1l); + request.setLatest(true); + request.setSyncEnable(true); + request.setStatus("test-api-status"); + request.setVersionId("test-api-version"); + request.setDeleted(false); - mockMvc.perform(MockMvcRequestBuilders.multipart(prefix + "/create") + mockMvc.perform(MockMvcRequestBuilders.multipart(prefix + "/add") .file(file) - .contentType(MediaType.MULTIPART_FORM_DATA) - .param("request", new ObjectMapper().writeValueAsString(request))) - .andExpect(MockMvcResultMatchers.status().is(400)); - //.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON)) - //.andExpect(MockMvcResultMatchers.jsonPath("$.id").value("test-api-id")); + .contentType(MediaType.APPLICATION_JSON_VALUE) + .content(JsonUtils.toJSONString(request))) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.data.id").value("test-api-id")); } }