test: 添加接口定义添加接口单用测试

This commit is contained in:
fit2-zhao 2023-06-02 15:52:30 +08:00 committed by fit2-zhao
parent 1416c7bd62
commit 5f24c79156
6 changed files with 44 additions and 21 deletions

View File

@ -3,14 +3,16 @@ package io.metersphere.api.controller;
import com.github.pagehelper.Page; import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageHelper;
import io.metersphere.api.domain.ApiDefinition; import io.metersphere.api.domain.ApiDefinition;
import io.metersphere.api.dto.ApiDefinitionDTO; import io.metersphere.api.dto.definition.ApiDefinitionDTO;
import io.metersphere.api.dto.ApiDefinitionListRequest; import io.metersphere.api.dto.definition.ListRequestDTO;
import io.metersphere.api.service.ApiDefinitionService; import io.metersphere.api.service.ApiDefinitionService;
import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.sdk.util.PageUtils; import io.metersphere.sdk.util.PageUtils;
import io.metersphere.sdk.util.Pager; import io.metersphere.sdk.util.Pager;
import io.metersphere.validation.groups.Created; import io.metersphere.validation.groups.Created;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -26,13 +28,15 @@ public class ApiDefinitionController {
private ApiDefinitionService apiDefinitionService; private ApiDefinitionService apiDefinitionService;
@PostMapping(value = "/add", consumes = MediaType.APPLICATION_JSON_VALUE) @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, public ApiDefinitionDTO create(@Validated({Created.class}) @RequestBody ApiDefinitionDTO request,
@RequestParam(value = "files") List<MultipartFile> bodyFiles) { @RequestParam(value = "files") List<MultipartFile> bodyFiles) {
return apiDefinitionService.create(request, bodyFiles); return apiDefinitionService.create(request, bodyFiles);
} }
@PostMapping(value = "/page") @PostMapping(value = "/page")
public Pager<List<ApiDefinition>> list(@Validated @RequestBody ApiDefinitionListRequest request) { @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_READ)
public Pager<List<ApiDefinition>> list(@Validated @RequestBody ListRequestDTO request) {
Page<Object> page = PageHelper.startPage(request.getCurrent(), request.getPageSize(), Page<Object> page = PageHelper.startPage(request.getCurrent(), request.getPageSize(),
StringUtils.isNotBlank(request.getOrderColumn()) ? request.getOrderColumn() : "create_time desc"); StringUtils.isNotBlank(request.getOrderColumn()) ? request.getOrderColumn() : "create_time desc");
return PageUtils.setPageInfo(page, apiDefinitionService.list(request)); return PageUtils.setPageInfo(page, apiDefinitionService.list(request));

View File

@ -1,4 +1,4 @@
package io.metersphere.api.dto; package io.metersphere.api.dto.definition;
import io.metersphere.api.domain.ApiDefinition; import io.metersphere.api.domain.ApiDefinition;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;

View File

@ -1,4 +1,4 @@
package io.metersphere.api.dto; package io.metersphere.api.dto.definition;
import jakarta.validation.constraints.Min; import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotBlank;
@ -6,7 +6,7 @@ import lombok.Data;
@Data @Data
public class ApiDefinitionListRequest { public class ListRequestDTO {
private String name; private String name;
private String path; private String path;
private String method; private String method;

View File

@ -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;
}

View File

@ -2,8 +2,8 @@ package io.metersphere.api.service;
import io.metersphere.api.domain.ApiDefinition; import io.metersphere.api.domain.ApiDefinition;
import io.metersphere.api.domain.ApiDefinitionExample; import io.metersphere.api.domain.ApiDefinitionExample;
import io.metersphere.api.dto.ApiDefinitionDTO; import io.metersphere.api.dto.definition.ApiDefinitionDTO;
import io.metersphere.api.dto.ApiDefinitionListRequest; import io.metersphere.api.dto.definition.ListRequestDTO;
import io.metersphere.api.mapper.ApiDefinitionMapper; import io.metersphere.api.mapper.ApiDefinitionMapper;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
@ -24,7 +24,7 @@ public class ApiDefinitionService {
} }
public List<ApiDefinition> list(@NotNull ApiDefinitionListRequest request) { public List<ApiDefinition> list(@NotNull ListRequestDTO request) {
ApiDefinitionExample example = new ApiDefinitionExample(); ApiDefinitionExample example = new ApiDefinitionExample();
ApiDefinitionExample.Criteria criteria = example.createCriteria(); ApiDefinitionExample.Criteria criteria = example.createCriteria();
criteria.andProjectIdEqualTo(request.getProjectId()); criteria.andProjectIdEqualTo(request.getProjectId());

View File

@ -2,8 +2,8 @@ package io.metersphere.api.controller;
import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.JsonPath;
import io.metersphere.api.domain.ApiDefinition; import io.metersphere.api.domain.ApiDefinition;
import io.metersphere.api.dto.ApiDefinitionDTO; import io.metersphere.api.dto.definition.ApiDefinitionDTO;
import io.metersphere.api.dto.ApiDefinitionListRequest; import io.metersphere.api.dto.definition.ListRequestDTO;
import io.metersphere.sdk.constants.SessionConstants; import io.metersphere.sdk.constants.SessionConstants;
import io.metersphere.sdk.controller.handler.ResultHolder; import io.metersphere.sdk.controller.handler.ResultHolder;
import io.metersphere.sdk.util.JSON; 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.MockMvc;
import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import java.util.List; import java.util.List;
@ -45,8 +44,6 @@ public class ApiDefinitionControllerTests {
LogUtils.info("init base api test"); LogUtils.info("init base api test");
} }
@Test @Test
@Order(0) @Order(0)
public void login() throws Exception { public void login() throws Exception {
@ -62,7 +59,7 @@ public class ApiDefinitionControllerTests {
@Test @Test
@Order(1) @Order(1)
public void testCreate() throws Exception { public void testAdd() throws Exception {
LogUtils.info("create api test"); LogUtils.info("create api test");
// 创建一个 MockMultipartFile 对象用于模拟文件上传 // 创建一个 MockMultipartFile 对象用于模拟文件上传
MockMultipartFile file = new MockMultipartFile("files", "files", MediaType.APPLICATION_OCTET_STREAM_VALUE, "Test content".getBytes()); 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), config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED),
executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Order(2) @Order(2)
public void listSuccess() throws Exception { public void testPageSuccess() throws Exception {
int pageSize = 10; int pageSize = 10;
int current = 1; int current = 1;
ApiDefinitionListRequest request = new ApiDefinitionListRequest(); ListRequestDTO request = new ListRequestDTO();
request.setCurrent(current); request.setCurrent(current);
request.setPageSize(pageSize); request.setPageSize(pageSize);
request.setProjectId("test-project-id"); request.setProjectId("test-project-id");
@ -141,9 +138,9 @@ public class ApiDefinitionControllerTests {
//没有传入必填值 //没有传入必填值
@Test @Test
@Order(3) @Order(3)
public void listError() throws Exception { public void testPageError() throws Exception {
// projectId为空 // projectId为空
ApiDefinitionListRequest request = new ApiDefinitionListRequest(); ListRequestDTO request = new ListRequestDTO();
request.setCurrent(1); request.setCurrent(1);
request.setPageSize(20); request.setPageSize(20);
mockMvc.perform(MockMvcRequestBuilders.multipart(prefix + "/page") mockMvc.perform(MockMvcRequestBuilders.multipart(prefix + "/page")
@ -154,7 +151,7 @@ public class ApiDefinitionControllerTests {
.andExpect(status().isBadRequest()); .andExpect(status().isBadRequest());
//pageSize为空 //pageSize为空
request = new ApiDefinitionListRequest(); request = new ListRequestDTO();
request.setCurrent(1); request.setCurrent(1);
request.setProjectId("test-project-id"); request.setProjectId("test-project-id");
mockMvc.perform(MockMvcRequestBuilders.multipart(prefix + "/page") mockMvc.perform(MockMvcRequestBuilders.multipart(prefix + "/page")
@ -165,7 +162,7 @@ public class ApiDefinitionControllerTests {
.andExpect(status().isBadRequest()); .andExpect(status().isBadRequest());
//current为空 //current为空
request = new ApiDefinitionListRequest(); request = new ListRequestDTO();
request.setPageSize(20); request.setPageSize(20);
request.setProjectId("test-project-id"); request.setProjectId("test-project-id");
mockMvc.perform(MockMvcRequestBuilders.multipart(prefix + "/page") mockMvc.perform(MockMvcRequestBuilders.multipart(prefix + "/page")