From 5f24c79156adca2ca76f658df726693c1d3f7290 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Fri, 2 Jun 2023 15:52:30 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E6=B7=BB=E5=8A=A0=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E6=B7=BB=E5=8A=A0=E6=8E=A5=E5=8F=A3=E5=8D=95?= =?UTF-8?q?=E7=94=A8=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ApiDefinitionController.java | 10 ++++++--- .../{ => definition}/ApiDefinitionDTO.java | 2 +- .../ListRequestDTO.java} | 4 ++-- .../api/dto/scenario/ApiScenarioDTO.java | 22 +++++++++++++++++++ .../api/service/ApiDefinitionService.java | 6 ++--- .../ApiDefinitionControllerTests.java | 21 ++++++++---------- 6 files changed, 44 insertions(+), 21 deletions(-) rename backend/services/api-test/src/main/java/io/metersphere/api/dto/{ => definition}/ApiDefinitionDTO.java (91%) rename backend/services/api-test/src/main/java/io/metersphere/api/dto/{ApiDefinitionListRequest.java => definition/ListRequestDTO.java} (86%) create mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/dto/scenario/ApiScenarioDTO.java 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 132a8419e2..db68cc540f 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 @@ -3,14 +3,16 @@ package io.metersphere.api.controller; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import io.metersphere.api.domain.ApiDefinition; -import io.metersphere.api.dto.ApiDefinitionDTO; -import io.metersphere.api.dto.ApiDefinitionListRequest; +import io.metersphere.api.dto.definition.ApiDefinitionDTO; +import io.metersphere.api.dto.definition.ListRequestDTO; import io.metersphere.api.service.ApiDefinitionService; +import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.sdk.util.PageUtils; import io.metersphere.sdk.util.Pager; import io.metersphere.validation.groups.Created; import jakarta.annotation.Resource; import org.apache.commons.lang3.StringUtils; +import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.http.MediaType; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; @@ -26,13 +28,15 @@ public class ApiDefinitionController { private ApiDefinitionService apiDefinitionService; @PostMapping(value = "/add", consumes = MediaType.APPLICATION_JSON_VALUE) + @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_READ_ADD_API) public ApiDefinitionDTO create(@Validated({Created.class}) @RequestBody ApiDefinitionDTO request, @RequestParam(value = "files") List bodyFiles) { return apiDefinitionService.create(request, bodyFiles); } @PostMapping(value = "/page") - public Pager> list(@Validated @RequestBody ApiDefinitionListRequest request) { + @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_READ) + public Pager> list(@Validated @RequestBody ListRequestDTO request) { Page page = PageHelper.startPage(request.getCurrent(), request.getPageSize(), StringUtils.isNotBlank(request.getOrderColumn()) ? request.getOrderColumn() : "create_time desc"); return PageUtils.setPageInfo(page, apiDefinitionService.list(request)); diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiDefinitionDTO.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionDTO.java similarity index 91% rename from backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiDefinitionDTO.java rename to backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionDTO.java index fe56f450d9..9714359db0 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiDefinitionDTO.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionDTO.java @@ -1,4 +1,4 @@ -package io.metersphere.api.dto; +package io.metersphere.api.dto.definition; import io.metersphere.api.domain.ApiDefinition; import io.swagger.v3.oas.annotations.media.Schema; diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiDefinitionListRequest.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ListRequestDTO.java similarity index 86% rename from backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiDefinitionListRequest.java rename to backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ListRequestDTO.java index 7a681ad330..12efab4ee9 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/ApiDefinitionListRequest.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ListRequestDTO.java @@ -1,4 +1,4 @@ -package io.metersphere.api.dto; +package io.metersphere.api.dto.definition; import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotBlank; @@ -6,7 +6,7 @@ import lombok.Data; @Data -public class ApiDefinitionListRequest { +public class ListRequestDTO { private String name; private String path; private String method; diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/scenario/ApiScenarioDTO.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/scenario/ApiScenarioDTO.java new file mode 100644 index 0000000000..a1c303ea69 --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/scenario/ApiScenarioDTO.java @@ -0,0 +1,22 @@ +package io.metersphere.api.dto.scenario; + +import io.metersphere.api.domain.ApiScenario; +import io.metersphere.validation.groups.Created; +import io.metersphere.validation.groups.Updated; +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Size; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Data +@EqualsAndHashCode(callSuper = false) +public class ApiScenarioDTO extends ApiScenario { + @Schema(title = "场景pk", requiredMode = Schema.RequiredMode.REQUIRED) + @NotBlank(message = "{api_scenario_blob.id.not_blank}", groups = {Updated.class}) + @Size(min = 1, max = 50, message = "{api_scenario_blob.id.length_range}", groups = {Created.class, Updated.class}) + private String id; + + @Schema(title = "场景步骤内容") + private byte[] content; +} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDefinitionService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDefinitionService.java index 8ac1df44ae..969f89beb5 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -2,8 +2,8 @@ package io.metersphere.api.service; import io.metersphere.api.domain.ApiDefinition; import io.metersphere.api.domain.ApiDefinitionExample; -import io.metersphere.api.dto.ApiDefinitionDTO; -import io.metersphere.api.dto.ApiDefinitionListRequest; +import io.metersphere.api.dto.definition.ApiDefinitionDTO; +import io.metersphere.api.dto.definition.ListRequestDTO; import io.metersphere.api.mapper.ApiDefinitionMapper; import jakarta.annotation.Resource; import jakarta.validation.constraints.NotNull; @@ -24,7 +24,7 @@ public class ApiDefinitionService { } - public List list(@NotNull ApiDefinitionListRequest request) { + public List list(@NotNull ListRequestDTO request) { ApiDefinitionExample example = new ApiDefinitionExample(); ApiDefinitionExample.Criteria criteria = example.createCriteria(); criteria.andProjectIdEqualTo(request.getProjectId()); diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java index 0eb5744932..1c2257b351 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java @@ -2,8 +2,8 @@ package io.metersphere.api.controller; import com.jayway.jsonpath.JsonPath; import io.metersphere.api.domain.ApiDefinition; -import io.metersphere.api.dto.ApiDefinitionDTO; -import io.metersphere.api.dto.ApiDefinitionListRequest; +import io.metersphere.api.dto.definition.ApiDefinitionDTO; +import io.metersphere.api.dto.definition.ListRequestDTO; import io.metersphere.sdk.constants.SessionConstants; import io.metersphere.sdk.controller.handler.ResultHolder; import io.metersphere.sdk.util.JSON; @@ -23,7 +23,6 @@ import org.springframework.test.context.jdbc.SqlConfig; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; -import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import java.util.List; @@ -45,8 +44,6 @@ public class ApiDefinitionControllerTests { LogUtils.info("init base api test"); } - - @Test @Order(0) public void login() throws Exception { @@ -62,7 +59,7 @@ public class ApiDefinitionControllerTests { @Test @Order(1) - public void testCreate() throws Exception { + public void testAdd() throws Exception { LogUtils.info("create api test"); // 创建一个 MockMultipartFile 对象,用于模拟文件上传 MockMultipartFile file = new MockMultipartFile("files", "files", MediaType.APPLICATION_OCTET_STREAM_VALUE, "Test content".getBytes()); @@ -104,10 +101,10 @@ public class ApiDefinitionControllerTests { config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED), executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) @Order(2) - public void listSuccess() throws Exception { + public void testPageSuccess() throws Exception { int pageSize = 10; int current = 1; - ApiDefinitionListRequest request = new ApiDefinitionListRequest(); + ListRequestDTO request = new ListRequestDTO(); request.setCurrent(current); request.setPageSize(pageSize); request.setProjectId("test-project-id"); @@ -141,9 +138,9 @@ public class ApiDefinitionControllerTests { //没有传入必填值 @Test @Order(3) - public void listError() throws Exception { + public void testPageError() throws Exception { // projectId为空 - ApiDefinitionListRequest request = new ApiDefinitionListRequest(); + ListRequestDTO request = new ListRequestDTO(); request.setCurrent(1); request.setPageSize(20); mockMvc.perform(MockMvcRequestBuilders.multipart(prefix + "/page") @@ -154,7 +151,7 @@ public class ApiDefinitionControllerTests { .andExpect(status().isBadRequest()); //pageSize为空 - request = new ApiDefinitionListRequest(); + request = new ListRequestDTO(); request.setCurrent(1); request.setProjectId("test-project-id"); mockMvc.perform(MockMvcRequestBuilders.multipart(prefix + "/page") @@ -165,7 +162,7 @@ public class ApiDefinitionControllerTests { .andExpect(status().isBadRequest()); //current为空 - request = new ApiDefinitionListRequest(); + request = new ListRequestDTO(); request.setPageSize(20); request.setProjectId("test-project-id"); mockMvc.perform(MockMvcRequestBuilders.multipart(prefix + "/page")