feat(接口测试): 增加接口用例相关接口
This commit is contained in:
parent
460c1d997c
commit
24fe1dc2c3
|
@ -246,6 +246,9 @@ public class PermissionConstants {
|
|||
public static final String PROJECT_API_DEFINITION_EXECUTE = "PROJECT_API_DEFINITION:READ+EXECUTE";
|
||||
public static final String PROJECT_API_DEFINITION_CASE_READ = "PROJECT_API_DEFINITION_CASE:READ";
|
||||
public static final String PROJECT_API_DEFINITION_CASE_ADD = "PROJECT_API_DEFINITION_CASE:READ+ADD";
|
||||
public static final String PROJECT_API_DEFINITION_CASE_UPDATE = "PROJECT_API_DEFINITION_CASE:READ+UPDATE";
|
||||
public static final String PROJECT_API_DEFINITION_CASE_DELETE = "PROJECT_API_DEFINITION_CASE:READ+DELETE";
|
||||
public static final String PROJECT_API_DEFINITION_CASE_RECOVER = "PROJECT_API_DEFINITION_CASE:READ+RECOVER";
|
||||
|
||||
/*------ end: API_MANAGEMENT ------*/
|
||||
}
|
||||
|
|
|
@ -292,3 +292,5 @@ api_module.not.exist=模块不存在
|
|||
|
||||
permission.api.name=接口测试
|
||||
api_debug_exist=接口已存在
|
||||
follow=关注
|
||||
unfollow=取消关注
|
||||
|
|
|
@ -300,3 +300,5 @@ api_module.not.exist=The module does not exist
|
|||
|
||||
permission.api.name=API Test
|
||||
api_debug_exist=The API already exists
|
||||
follow=Follow
|
||||
unfollow=Unfollow
|
||||
|
|
|
@ -300,3 +300,5 @@ api_module.not.exist=模块不存在
|
|||
|
||||
permission.api.name=接口测试
|
||||
api_debug_exist=接口已存在
|
||||
follow=关注
|
||||
unfollow=取消关注
|
|
@ -300,3 +300,5 @@ api_module.not.exist=模塊不存在
|
|||
|
||||
permission.api.name=接口測試
|
||||
api_debug_exist=接口已存在
|
||||
follow=关注
|
||||
unfollow=取消关注
|
||||
|
|
|
@ -2,21 +2,22 @@ package io.metersphere.api.controller.definition;
|
|||
|
||||
import io.metersphere.api.domain.ApiTestCase;
|
||||
import io.metersphere.api.dto.definition.ApiTestCaseAddRequest;
|
||||
import io.metersphere.api.dto.definition.ApiTestCaseDTO;
|
||||
import io.metersphere.api.service.definition.ApiTestCaseLogService;
|
||||
import io.metersphere.api.service.definition.ApiTestCaseNoticeService;
|
||||
import io.metersphere.api.service.definition.ApiTestCaseService;
|
||||
import io.metersphere.sdk.constants.PermissionConstants;
|
||||
import io.metersphere.system.log.annotation.Log;
|
||||
import io.metersphere.system.log.constants.OperationLogType;
|
||||
import io.metersphere.system.notice.annotation.SendNotice;
|
||||
import io.metersphere.system.notice.constants.NoticeConstants;
|
||||
import io.metersphere.system.utils.SessionUtils;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
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.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -33,9 +34,56 @@ public class ApiTestCaseController {
|
|||
@Operation(summary = "接口测试-接口管理-接口用例-新增")
|
||||
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_ADD)
|
||||
@Log(type = OperationLogType.ADD, expression = "#msClass.addLog(#request)", msClass = ApiTestCaseLogService.class)
|
||||
@SendNotice(taskType = NoticeConstants.TaskType.API_DEFINITION_TASK, event = NoticeConstants.Event.CASE_CREATE, target = "#targetClass.getCaseDTO(#request)", targetClass = ApiTestCaseNoticeService.class)
|
||||
public ApiTestCase add(@Validated @RequestPart("request") ApiTestCaseAddRequest request, @RequestPart(value = "files", required = false) List<MultipartFile> files) {
|
||||
return apiTestCaseService.addCase(request, files, SessionUtils.getUserId());
|
||||
}
|
||||
|
||||
@GetMapping(value = "/get-detail/{id}")
|
||||
@Operation(summary = "接口测试-接口管理-接口用例-获取详情")
|
||||
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_READ)
|
||||
public ApiTestCaseDTO get(@PathVariable String id) {
|
||||
return apiTestCaseService.get(id, SessionUtils.getUserId());
|
||||
}
|
||||
|
||||
@GetMapping("/move-gc/{id}")
|
||||
@Operation(summary = "接口测试-接口管理-接口用例-移动到回收站")
|
||||
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_DELETE)
|
||||
@Log(type = OperationLogType.DELETE, expression = "#msClass.moveToGcLog(#id)", msClass = ApiTestCaseLogService.class)
|
||||
public void deleteToGc(@PathVariable String id) {
|
||||
apiTestCaseService.deleteToGc(id, SessionUtils.getUserId());
|
||||
}
|
||||
|
||||
@GetMapping("recover/{id}")
|
||||
@Operation(summary = "接口测试-接口管理-接口用例-恢复")
|
||||
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_RECOVER)
|
||||
@Log(type = OperationLogType.RECOVER, expression = "#msClass.recoverLog(#id)", msClass = ApiTestCaseLogService.class)
|
||||
public void recover(@PathVariable String id) {
|
||||
apiTestCaseService.recover(id);
|
||||
}
|
||||
|
||||
@GetMapping("follow/{id}")
|
||||
@Operation(summary = "接口测试-接口管理-接口用例-关注")
|
||||
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_UPDATE)
|
||||
@Log(type = OperationLogType.UPDATE, expression = "#msClass.followLog(#id)", msClass = ApiTestCaseLogService.class)
|
||||
public void follow(@PathVariable String id) {
|
||||
apiTestCaseService.follow(id, SessionUtils.getUserId());
|
||||
}
|
||||
|
||||
@GetMapping("unfollow/{id}")
|
||||
@Operation(summary = "接口测试-接口管理-接口用例-取消关注")
|
||||
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_UPDATE)
|
||||
@Log(type = OperationLogType.UPDATE, expression = "#msClass.unfollowLog(#id)", msClass = ApiTestCaseLogService.class)
|
||||
public void unfollow(@PathVariable String id) {
|
||||
apiTestCaseService.unfollow(id, SessionUtils.getUserId());
|
||||
}
|
||||
|
||||
@GetMapping("delete/{id}")
|
||||
@Operation(summary = "接口测试-接口管理-接口用例-删除")
|
||||
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_DELETE)
|
||||
@Log(type = OperationLogType.DELETE, expression = "#msClass.deleteLog(#id)", msClass = ApiTestCaseLogService.class)
|
||||
public void delete(@PathVariable String id) {
|
||||
apiTestCaseService.delete(id);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
package io.metersphere.api.dto.definition;
|
||||
|
||||
import io.metersphere.system.dto.table.TableBatchProcessDTO;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
public class ApiTestCaseBatchRequest extends TableBatchProcessDTO implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Schema(description = "项目ID", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
private String projectId;
|
||||
|
||||
@Schema(description = "接口ID", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
|
||||
private String apiDefinitionId;
|
||||
|
||||
@Schema(description = "模块ID", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
|
||||
private String moduleId;
|
||||
|
||||
@Schema(description = "协议", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
private String protocol;
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package io.metersphere.api.dto.definition;
|
||||
|
||||
import io.metersphere.api.domain.ApiTestCase;
|
||||
import io.metersphere.plugin.api.spi.AbstractMsTestElement;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ApiTestCaseDTO extends ApiTestCase {
|
||||
@Schema(description = "是否关注")
|
||||
private Boolean follow;
|
||||
@Schema(description = "请求内容")
|
||||
private AbstractMsTestElement request;
|
||||
}
|
|
@ -1,11 +1,13 @@
|
|||
package io.metersphere.api.service.definition;
|
||||
|
||||
import io.metersphere.api.domain.ApiTestCase;
|
||||
import io.metersphere.api.dto.definition.ApiTestCaseAddRequest;
|
||||
import io.metersphere.api.mapper.ApiTestCaseMapper;
|
||||
import io.metersphere.project.domain.Project;
|
||||
import io.metersphere.project.mapper.ProjectMapper;
|
||||
import io.metersphere.sdk.constants.HttpMethodConstants;
|
||||
import io.metersphere.sdk.util.JSON;
|
||||
import io.metersphere.sdk.util.Translator;
|
||||
import io.metersphere.system.log.constants.OperationLogModule;
|
||||
import io.metersphere.system.log.constants.OperationLogType;
|
||||
import io.metersphere.system.log.dto.LogDTO;
|
||||
|
@ -34,7 +36,7 @@ public class ApiTestCaseLogService {
|
|||
null,
|
||||
null,
|
||||
OperationLogType.ADD.name(),
|
||||
OperationLogModule.API_DEFINITION,
|
||||
OperationLogModule.API_DEFINITION_CASE,
|
||||
request.getName());
|
||||
|
||||
dto.setPath("/api/testCase/add");
|
||||
|
@ -43,4 +45,94 @@ public class ApiTestCaseLogService {
|
|||
return dto;
|
||||
}
|
||||
|
||||
public LogDTO deleteLog(String id) {
|
||||
ApiTestCase apiTestCase = apiTestCaseMapper.selectByPrimaryKey(id);
|
||||
Project project = projectMapper.selectByPrimaryKey(apiTestCase.getProjectId());
|
||||
LogDTO dto = new LogDTO(
|
||||
apiTestCase.getProjectId(),
|
||||
project.getOrganizationId(),
|
||||
id,
|
||||
null,
|
||||
OperationLogType.DELETE.name(),
|
||||
OperationLogModule.API_DEFINITION_CASE,
|
||||
apiTestCase.getName());
|
||||
|
||||
dto.setPath("/api/testCase/delete/" + id);
|
||||
dto.setMethod(HttpMethodConstants.GET.name());
|
||||
dto.setOriginalValue(JSON.toJSONBytes(apiTestCase));
|
||||
return dto;
|
||||
}
|
||||
|
||||
public LogDTO moveToGcLog(String id) {
|
||||
ApiTestCase apiTestCase = apiTestCaseMapper.selectByPrimaryKey(id);
|
||||
Project project = projectMapper.selectByPrimaryKey(apiTestCase.getProjectId());
|
||||
LogDTO dto = new LogDTO(
|
||||
apiTestCase.getProjectId(),
|
||||
project.getOrganizationId(),
|
||||
id,
|
||||
null,
|
||||
OperationLogType.DELETE.name(),
|
||||
OperationLogModule.API_DEFINITION_CASE,
|
||||
apiTestCase.getName());
|
||||
|
||||
dto.setPath("/api/testCase/move-gc/" + id);
|
||||
dto.setMethod(HttpMethodConstants.GET.name());
|
||||
dto.setOriginalValue(JSON.toJSONBytes(apiTestCase));
|
||||
return dto;
|
||||
}
|
||||
|
||||
public LogDTO recoverLog(String id) {
|
||||
ApiTestCase apiTestCase = apiTestCaseMapper.selectByPrimaryKey(id);
|
||||
Project project = projectMapper.selectByPrimaryKey(apiTestCase.getProjectId());
|
||||
LogDTO dto = new LogDTO(
|
||||
apiTestCase.getProjectId(),
|
||||
project.getOrganizationId(),
|
||||
id,
|
||||
null,
|
||||
OperationLogType.RECOVER.name(),
|
||||
OperationLogModule.API_DEFINITION_CASE,
|
||||
apiTestCase.getName());
|
||||
|
||||
dto.setPath("/api/testCase/recover/" + id);
|
||||
dto.setMethod(HttpMethodConstants.GET.name());
|
||||
dto.setOriginalValue(JSON.toJSONBytes(apiTestCase));
|
||||
return dto;
|
||||
}
|
||||
|
||||
public LogDTO followLog(String id) {
|
||||
ApiTestCase apiTestCase = apiTestCaseMapper.selectByPrimaryKey(id);
|
||||
Project project = projectMapper.selectByPrimaryKey(apiTestCase.getProjectId());
|
||||
LogDTO dto = new LogDTO(
|
||||
apiTestCase.getProjectId(),
|
||||
project.getOrganizationId(),
|
||||
id,
|
||||
null,
|
||||
OperationLogType.UPDATE.name(),
|
||||
OperationLogModule.API_DEFINITION_CASE,
|
||||
Translator.get("follow") + apiTestCase.getName());
|
||||
|
||||
dto.setPath("/api/testCase/follow/" + id);
|
||||
dto.setMethod(HttpMethodConstants.GET.name());
|
||||
dto.setOriginalValue(JSON.toJSONBytes(apiTestCase));
|
||||
return dto;
|
||||
}
|
||||
|
||||
public LogDTO unfollowLog(String id) {
|
||||
ApiTestCase apiTestCase = apiTestCaseMapper.selectByPrimaryKey(id);
|
||||
Project project = projectMapper.selectByPrimaryKey(apiTestCase.getProjectId());
|
||||
LogDTO dto = new LogDTO(
|
||||
apiTestCase.getProjectId(),
|
||||
project.getOrganizationId(),
|
||||
id,
|
||||
null,
|
||||
OperationLogType.UPDATE.name(),
|
||||
OperationLogModule.API_DEFINITION_CASE,
|
||||
Translator.get("unfollow") + apiTestCase.getName());
|
||||
|
||||
dto.setPath("/api/testCase/unfollow/" + id);
|
||||
dto.setMethod(HttpMethodConstants.GET.name());
|
||||
dto.setOriginalValue(JSON.toJSONBytes(apiTestCase));
|
||||
return dto;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
package io.metersphere.api.service.definition;
|
||||
|
||||
import io.metersphere.api.dto.definition.ApiTestCaseAddRequest;
|
||||
import io.metersphere.sdk.util.BeanUtils;
|
||||
import io.metersphere.system.dto.sdk.ApiDefinitionCaseDTO;
|
||||
import io.metersphere.system.utils.SessionUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class ApiTestCaseNoticeService {
|
||||
|
||||
|
||||
public ApiDefinitionCaseDTO getCaseDTO(ApiTestCaseAddRequest request) {
|
||||
String userId = SessionUtils.getUserId();
|
||||
ApiDefinitionCaseDTO caseDTO = new ApiDefinitionCaseDTO();
|
||||
BeanUtils.copyBean(caseDTO, request);
|
||||
caseDTO.setCaseName(request.getName());
|
||||
caseDTO.setCaseStatus(request.getStatus());
|
||||
caseDTO.setCaseCreateUser(userId);
|
||||
caseDTO.setCaseUpdateUser(userId);
|
||||
return caseDTO;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -2,10 +2,10 @@ package io.metersphere.api.service.definition;
|
|||
|
||||
import io.metersphere.api.domain.*;
|
||||
import io.metersphere.api.dto.definition.ApiTestCaseAddRequest;
|
||||
import io.metersphere.api.mapper.ApiDefinitionMapper;
|
||||
import io.metersphere.api.mapper.ApiTestCaseBlobMapper;
|
||||
import io.metersphere.api.mapper.ApiTestCaseMapper;
|
||||
import io.metersphere.api.mapper.ExtApiTestCaseMapper;
|
||||
import io.metersphere.api.dto.definition.ApiTestCaseDTO;
|
||||
import io.metersphere.api.mapper.*;
|
||||
import io.metersphere.api.util.ApiDataUtils;
|
||||
import io.metersphere.plugin.api.spi.AbstractMsTestElement;
|
||||
import io.metersphere.project.domain.Project;
|
||||
import io.metersphere.project.mapper.ProjectMapper;
|
||||
import io.metersphere.sdk.constants.ApplicationNumScope;
|
||||
|
@ -39,6 +39,8 @@ public class ApiTestCaseService {
|
|||
@Resource
|
||||
private ProjectMapper projectMapper;
|
||||
@Resource
|
||||
private ApiTestCaseFollowerMapper apiTestCaseFollowerMapper;
|
||||
@Resource
|
||||
private MinioRepository minioRepository;
|
||||
|
||||
public ApiTestCase addCase(ApiTestCaseAddRequest request, List<MultipartFile> files, String userId) {
|
||||
|
@ -125,4 +127,82 @@ public class ApiTestCaseService {
|
|||
}
|
||||
}
|
||||
|
||||
private ApiTestCase checkResourceExist(String id) {
|
||||
ApiTestCase testCase = apiTestCaseMapper.selectByPrimaryKey(id);
|
||||
if (testCase == null) {
|
||||
throw new MSException(Translator.get("api_test_case_not_exist"));
|
||||
}
|
||||
return testCase;
|
||||
}
|
||||
|
||||
public ApiTestCaseDTO get(String id, String userId) {
|
||||
ApiTestCaseDTO apiTestCaseDTO = new ApiTestCaseDTO();
|
||||
ApiTestCase testCase = checkResourceExist(id);
|
||||
ApiTestCaseBlob testCaseBlob = apiTestCaseBlobMapper.selectByPrimaryKey(id);
|
||||
BeanUtils.copyBean(apiTestCaseDTO, testCase);
|
||||
ApiTestCaseFollowerExample example = new ApiTestCaseFollowerExample();
|
||||
example.createCriteria().andCaseIdEqualTo(id).andUserIdEqualTo(userId);
|
||||
List<ApiTestCaseFollower> followers = apiTestCaseFollowerMapper.selectByExample(example);
|
||||
apiTestCaseDTO.setFollow(CollectionUtils.isNotEmpty(followers));
|
||||
apiTestCaseDTO.setRequest(ApiDataUtils.parseObject(new String(testCaseBlob.getRequest()), AbstractMsTestElement.class));
|
||||
return apiTestCaseDTO;
|
||||
}
|
||||
|
||||
public void deleteToGc(String id, String userId) {
|
||||
checkResourceExist(id);
|
||||
ApiTestCase apiTestCase = new ApiTestCase();
|
||||
apiTestCase.setId(id);
|
||||
apiTestCase.setDeleted(true);
|
||||
apiTestCase.setDeleteUser(userId);
|
||||
apiTestCase.setDeleteTime(System.currentTimeMillis());
|
||||
apiTestCaseMapper.updateByPrimaryKeySelective(apiTestCase);
|
||||
}
|
||||
|
||||
public void recover(String id) {
|
||||
checkResourceExist(id);
|
||||
ApiTestCase apiTestCase = new ApiTestCase();
|
||||
apiTestCase.setId(id);
|
||||
apiTestCase.setDeleted(false);
|
||||
apiTestCase.setDeleteUser(null);
|
||||
apiTestCase.setDeleteTime(null);
|
||||
apiTestCaseMapper.updateByPrimaryKeySelective(apiTestCase);
|
||||
}
|
||||
|
||||
public void follow(String id, String userId) {
|
||||
checkResourceExist(id);
|
||||
ApiTestCaseFollowerExample example = new ApiTestCaseFollowerExample();
|
||||
example.createCriteria().andCaseIdEqualTo(id).andUserIdEqualTo(userId);
|
||||
List<ApiTestCaseFollower> followers = apiTestCaseFollowerMapper.selectByExample(example);
|
||||
if (CollectionUtils.isEmpty(followers)) {
|
||||
ApiTestCaseFollower follower = new ApiTestCaseFollower();
|
||||
follower.setCaseId(id);
|
||||
follower.setUserId(userId);
|
||||
apiTestCaseFollowerMapper.insert(follower);
|
||||
}
|
||||
}
|
||||
|
||||
public void unfollow(String id, String userId) {
|
||||
checkResourceExist(id);
|
||||
ApiTestCaseFollowerExample example = new ApiTestCaseFollowerExample();
|
||||
example.createCriteria().andCaseIdEqualTo(id).andUserIdEqualTo(userId);
|
||||
apiTestCaseFollowerMapper.deleteByExample(example);
|
||||
}
|
||||
|
||||
public void delete(String id) {
|
||||
ApiTestCase apiCase = checkResourceExist(id);
|
||||
apiTestCaseMapper.deleteByPrimaryKey(id);
|
||||
apiTestCaseBlobMapper.deleteByPrimaryKey(id);
|
||||
ApiTestCaseFollowerExample example = new ApiTestCaseFollowerExample();
|
||||
example.createCriteria().andCaseIdEqualTo(id);
|
||||
apiTestCaseFollowerMapper.deleteByExample(example);
|
||||
try {
|
||||
FileRequest request = new FileRequest();
|
||||
request.setProjectId(StringUtils.join(MsFileUtils.API_CASE_DIR, id));
|
||||
request.setProjectId(StringUtils.join(MsFileUtils.API_CASE_DIR, apiCase.getProjectId()));
|
||||
request.setResourceId(id);
|
||||
minioRepository.deleteFolder(request);
|
||||
} catch (Exception e) {
|
||||
LogUtils.info("删除body文件失败: 文件名称:" + id, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,26 +1,23 @@
|
|||
package io.metersphere.api.controller;
|
||||
|
||||
import io.metersphere.api.controller.param.ApiTestCaseAddRequestDefinition;
|
||||
import io.metersphere.api.domain.ApiDefinition;
|
||||
import io.metersphere.api.domain.ApiDefinitionBlob;
|
||||
import io.metersphere.api.domain.ApiTestCase;
|
||||
import io.metersphere.api.domain.ApiTestCaseBlob;
|
||||
import io.metersphere.api.domain.*;
|
||||
import io.metersphere.api.dto.definition.ApiTestCaseAddRequest;
|
||||
import io.metersphere.api.mapper.ApiDefinitionBlobMapper;
|
||||
import io.metersphere.api.mapper.ApiDefinitionMapper;
|
||||
import io.metersphere.api.mapper.ApiTestCaseBlobMapper;
|
||||
import io.metersphere.api.mapper.ApiTestCaseMapper;
|
||||
import io.metersphere.api.dto.definition.ApiTestCaseDTO;
|
||||
import io.metersphere.api.mapper.*;
|
||||
import io.metersphere.api.util.ApiDataUtils;
|
||||
import io.metersphere.plugin.api.spi.AbstractMsTestElement;
|
||||
import io.metersphere.sdk.constants.ApplicationNumScope;
|
||||
import io.metersphere.sdk.constants.PermissionConstants;
|
||||
import io.metersphere.sdk.dto.api.request.http.MsHTTPElement;
|
||||
import io.metersphere.sdk.mapper.OperationLogMapper;
|
||||
import io.metersphere.sdk.util.BeanUtils;
|
||||
import io.metersphere.sdk.util.JSON;
|
||||
import io.metersphere.system.base.BaseTest;
|
||||
import io.metersphere.system.log.constants.OperationLogType;
|
||||
import io.metersphere.system.uid.NumGenerator;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
@ -45,14 +42,16 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||
@AutoConfigureMockMvc
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
public class ApiTestCaseControllerTests extends BaseTest {
|
||||
protected static final String DEFAULT_LIST = "list/{0}";
|
||||
protected static final String HTTP_PROTOCOL = "HTTP";
|
||||
private static final String BASE_PATH = "/api/testCase/";
|
||||
private static final String ADD = BASE_PATH + "add";
|
||||
private static final ResultMatcher BAD_REQUEST_MATCHER = status().isBadRequest();
|
||||
private static final String GET = BASE_PATH + "get-detail/";
|
||||
private static final String MOVE_TO_GC = BASE_PATH + "move-gc/";
|
||||
private static final String RECOVER = BASE_PATH + "recover/";
|
||||
private static final String FOLLOW = BASE_PATH + "follow/";
|
||||
private static final String UNFOLLOW = BASE_PATH + "unfollow/";
|
||||
private static final String DELETE = BASE_PATH + "delete/";
|
||||
private static final ResultMatcher ERROR_REQUEST_MATCHER = status().is5xxServerError();
|
||||
private static ApiTestCase apiTestCase;
|
||||
private static ApiTestCase anotheraddapidebug;
|
||||
@Resource
|
||||
private ApiDefinitionMapper apiDefinitionMapper;
|
||||
@Resource
|
||||
|
@ -61,6 +60,10 @@ public class ApiTestCaseControllerTests extends BaseTest {
|
|||
private ApiTestCaseMapper apiTestCaseMapper;
|
||||
@Resource
|
||||
private ApiTestCaseBlobMapper apiTestCaseBlobMapper;
|
||||
@Resource
|
||||
private ApiTestCaseFollowerMapper apiTestCaseFollowerMapper;
|
||||
@Resource
|
||||
private OperationLogMapper operationLogMapper;
|
||||
|
||||
public void initApiData() {
|
||||
ApiDefinition apiDefinition = new ApiDefinition();
|
||||
|
@ -114,7 +117,7 @@ public class ApiTestCaseControllerTests extends BaseTest {
|
|||
MvcResult mvcResult = this.requestMultipartWithOkAndReturn(ADD, paramMap);
|
||||
// 校验请求成功数据
|
||||
ApiTestCase resultData = getResultData(mvcResult, ApiTestCase.class);
|
||||
this.apiTestCase = assertUpdateApiDebug(request, msHttpElement, resultData.getId());
|
||||
apiTestCase = assertUpdateApiDebug(request, msHttpElement, resultData.getId());
|
||||
|
||||
// 再插入一条数据,便于修改时重名校验
|
||||
request.setName("test1");
|
||||
|
@ -122,7 +125,7 @@ public class ApiTestCaseControllerTests extends BaseTest {
|
|||
paramMap.add("request", JSON.toJSONString(request));
|
||||
mvcResult = this.requestMultipartWithOkAndReturn(ADD, paramMap);
|
||||
resultData = getResultData(mvcResult, ApiTestCase.class);
|
||||
this.anotheraddapidebug = assertUpdateApiDebug(request, msHttpElement, resultData.getId());
|
||||
assertUpdateApiDebug(request, msHttpElement, resultData.getId());
|
||||
|
||||
// @@重名校验异常
|
||||
this.requestMultipart(ADD, paramMap).andExpect(ERROR_REQUEST_MATCHER);
|
||||
|
@ -141,7 +144,7 @@ public class ApiTestCaseControllerTests extends BaseTest {
|
|||
this.requestMultipart(ADD, paramMap).andExpect(ERROR_REQUEST_MATCHER);
|
||||
|
||||
// @@校验日志
|
||||
checkLog(this.apiTestCase.getId(), OperationLogType.ADD);
|
||||
checkLog(apiTestCase.getId(), OperationLogType.ADD);
|
||||
// @@异常参数校验
|
||||
createdGroupParamValidateTest(ApiTestCaseAddRequestDefinition.class, ADD);
|
||||
// @@校验权限
|
||||
|
@ -157,11 +160,117 @@ public class ApiTestCaseControllerTests extends BaseTest {
|
|||
ApiTestCase apiCase = apiTestCaseMapper.selectByPrimaryKey(id);
|
||||
ApiTestCaseBlob apiTestCaseBlob = apiTestCaseBlobMapper.selectByPrimaryKey(id);
|
||||
ApiTestCase copyApiDebug = BeanUtils.copyBean(new ApiTestCase(), apiCase);
|
||||
copyApiDebug = BeanUtils.copyBean(copyApiDebug, request);
|
||||
BeanUtils.copyBean(copyApiDebug, request);
|
||||
Assertions.assertEquals(apiCase, copyApiDebug);
|
||||
ApiDataUtils.setResolver(MsHTTPElement.class);
|
||||
Assertions.assertEquals(msHttpElement, ApiDataUtils.parseObject(new String(apiTestCaseBlob.getRequest()), AbstractMsTestElement.class));
|
||||
return apiCase;
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
public void get() throws Exception {
|
||||
// @@请求成功
|
||||
MvcResult mvcResult = this.requestGetWithOk(GET + apiTestCase.getId())
|
||||
.andReturn();
|
||||
ApiDataUtils.setResolver(MsHTTPElement.class);
|
||||
ApiTestCaseDTO apiDebugDTO = ApiDataUtils.parseObject(JSON.toJSONString(parseResponse(mvcResult).get("data")), ApiTestCaseDTO.class);
|
||||
// 校验数据是否正确
|
||||
ApiTestCaseDTO copyApiDebugDTO = BeanUtils.copyBean(new ApiTestCaseDTO(), apiTestCaseMapper.selectByPrimaryKey(apiTestCase.getId()));
|
||||
ApiTestCaseBlob apiDebugBlob = apiTestCaseBlobMapper.selectByPrimaryKey(apiTestCase.getId());
|
||||
ApiTestCaseFollowerExample example = new ApiTestCaseFollowerExample();
|
||||
example.createCriteria().andCaseIdEqualTo(apiTestCase.getId()).andUserIdEqualTo("admin");
|
||||
List<ApiTestCaseFollower> followers = apiTestCaseFollowerMapper.selectByExample(example);
|
||||
copyApiDebugDTO.setFollow(CollectionUtils.isNotEmpty(followers));
|
||||
copyApiDebugDTO.setRequest(ApiDataUtils.parseObject(new String(apiDebugBlob.getRequest()), AbstractMsTestElement.class));
|
||||
Assertions.assertEquals(apiDebugDTO, copyApiDebugDTO);
|
||||
|
||||
this.requestGet(GET + "111").andExpect(ERROR_REQUEST_MATCHER);
|
||||
|
||||
// @@校验权限
|
||||
requestGetPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_CASE_READ, GET + apiTestCase.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
public void moveToGC() throws Exception {
|
||||
// @@请求成功
|
||||
this.requestGetWithOk(MOVE_TO_GC + apiTestCase.getId());
|
||||
ApiTestCase apiCase = apiTestCaseMapper.selectByPrimaryKey(apiTestCase.getId());
|
||||
Assertions.assertTrue(apiCase.getDeleted());
|
||||
Assertions.assertEquals(apiCase.getDeleteUser(), "admin");
|
||||
Assertions.assertNotNull(apiCase.getDeleteTime());
|
||||
// @@校验日志
|
||||
checkLog(apiTestCase.getId(), OperationLogType.DELETE);
|
||||
this.requestGet(MOVE_TO_GC + "111").andExpect(ERROR_REQUEST_MATCHER);
|
||||
// @@校验权限
|
||||
requestGetPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_CASE_DELETE, MOVE_TO_GC + apiTestCase.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
public void recover() throws Exception {
|
||||
// @@请求成功
|
||||
this.requestGetWithOk(RECOVER + apiTestCase.getId());
|
||||
ApiTestCase apiCase = apiTestCaseMapper.selectByPrimaryKey(apiTestCase.getId());
|
||||
Assertions.assertFalse(apiCase.getDeleted());
|
||||
// @@校验日志
|
||||
checkLog(apiTestCase.getId(), OperationLogType.RECOVER);
|
||||
this.requestGet(RECOVER + "111").andExpect(ERROR_REQUEST_MATCHER);
|
||||
// @@校验权限
|
||||
requestGetPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_CASE_RECOVER, RECOVER + apiTestCase.getId());
|
||||
}
|
||||
|
||||
//关注
|
||||
@Test
|
||||
@Order(5)
|
||||
public void follow() throws Exception {
|
||||
// @@请求成功
|
||||
this.requestGetWithOk(FOLLOW + apiTestCase.getId());
|
||||
ApiTestCaseFollowerExample example = new ApiTestCaseFollowerExample();
|
||||
example.createCriteria().andCaseIdEqualTo(apiTestCase.getId()).andUserIdEqualTo("admin");
|
||||
List<ApiTestCaseFollower> followers = apiTestCaseFollowerMapper.selectByExample(example);
|
||||
Assertions.assertTrue(CollectionUtils.isNotEmpty(followers));
|
||||
// @@校验日志
|
||||
checkLog(apiTestCase.getId(), OperationLogType.UPDATE);
|
||||
this.requestGet(FOLLOW + "111").andExpect(ERROR_REQUEST_MATCHER);
|
||||
// @@校验权限
|
||||
requestGetPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_CASE_UPDATE, FOLLOW + apiTestCase.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
public void unfollow() throws Exception {
|
||||
// @@请求成功
|
||||
this.requestGetWithOk(UNFOLLOW + apiTestCase.getId());
|
||||
ApiTestCaseFollowerExample example = new ApiTestCaseFollowerExample();
|
||||
example.createCriteria().andCaseIdEqualTo(apiTestCase.getId()).andUserIdEqualTo("admin");
|
||||
List<ApiTestCaseFollower> followers = apiTestCaseFollowerMapper.selectByExample(example);
|
||||
Assertions.assertTrue(CollectionUtils.isEmpty(followers));
|
||||
// @@校验日志
|
||||
checkLog(apiTestCase.getId(), OperationLogType.UPDATE);
|
||||
this.requestGet(UNFOLLOW + "111").andExpect(ERROR_REQUEST_MATCHER);
|
||||
// @@校验权限
|
||||
requestGetPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_CASE_UPDATE, UNFOLLOW + apiTestCase.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
public void delete() throws Exception {
|
||||
// @@请求成功
|
||||
this.requestGetWithOk(DELETE + apiTestCase.getId());
|
||||
ApiTestCase apiCase = apiTestCaseMapper.selectByPrimaryKey(apiTestCase.getId());
|
||||
Assertions.assertNull(apiCase);
|
||||
Assertions.assertNull(apiTestCaseBlobMapper.selectByPrimaryKey(apiTestCase.getId()));
|
||||
ApiTestCaseFollowerExample example = new ApiTestCaseFollowerExample();
|
||||
example.createCriteria().andCaseIdEqualTo(apiTestCase.getId());
|
||||
List<ApiTestCaseFollower> followers = apiTestCaseFollowerMapper.selectByExample(example);
|
||||
Assertions.assertTrue(CollectionUtils.isEmpty(followers));
|
||||
// @@校验日志
|
||||
checkLog(apiTestCase.getId(), OperationLogType.DELETE);
|
||||
this.requestGet(DELETE + "111").andExpect(ERROR_REQUEST_MATCHER);
|
||||
// @@校验权限
|
||||
requestGetPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_CASE_DELETE, DELETE + apiTestCase.getId());
|
||||
}
|
||||
|
||||
}
|
|
@ -52,7 +52,7 @@ public class OrganizationProjectLogService {
|
|||
OperationLogConstants.ORGANIZATION,
|
||||
project.getOrganizationId(),
|
||||
project.getId(),
|
||||
project.getCreateUser(),
|
||||
null,
|
||||
OperationLogType.UPDATE.name(),
|
||||
OperationLogModule.SETTING_ORGANIZATION_PROJECT,
|
||||
request.getName());
|
||||
|
@ -70,7 +70,7 @@ public class OrganizationProjectLogService {
|
|||
OperationLogConstants.ORGANIZATION,
|
||||
project.getOrganizationId(),
|
||||
project.getId(),
|
||||
project.getCreateUser(),
|
||||
null,
|
||||
OperationLogType.UPDATE.name(),
|
||||
OperationLogModule.SETTING_ORGANIZATION_PROJECT,
|
||||
request.getName());
|
||||
|
@ -88,9 +88,9 @@ public class OrganizationProjectLogService {
|
|||
OperationLogConstants.ORGANIZATION,
|
||||
project.getOrganizationId(),
|
||||
project.getId(),
|
||||
project.getCreateUser(),
|
||||
null,
|
||||
OperationLogType.UPDATE.name(),
|
||||
OperationLogModule.SYSTEM_PROJECT,
|
||||
OperationLogModule.SETTING_ORGANIZATION_PROJECT,
|
||||
project.getName());
|
||||
dto.setMethod(HttpMethodConstants.GET.name());
|
||||
|
||||
|
@ -113,7 +113,7 @@ public class OrganizationProjectLogService {
|
|||
OperationLogConstants.ORGANIZATION,
|
||||
project.getOrganizationId(),
|
||||
id,
|
||||
project.getCreateUser(),
|
||||
null,
|
||||
OperationLogType.DELETE.name(),
|
||||
OperationLogModule.SETTING_ORGANIZATION_PROJECT,
|
||||
project.getName());
|
||||
|
|
|
@ -52,7 +52,7 @@ public class SystemProjectLogService {
|
|||
OperationLogConstants.SYSTEM,
|
||||
OperationLogConstants.SYSTEM,
|
||||
project.getId(),
|
||||
project.getCreateUser(),
|
||||
null,
|
||||
OperationLogType.UPDATE.name(),
|
||||
OperationLogModule.SETTING_SYSTEM_ORGANIZATION,
|
||||
request.getName());
|
||||
|
@ -70,7 +70,7 @@ public class SystemProjectLogService {
|
|||
OperationLogConstants.SYSTEM,
|
||||
OperationLogConstants.SYSTEM,
|
||||
project.getId(),
|
||||
project.getCreateUser(),
|
||||
null,
|
||||
OperationLogType.UPDATE.name(),
|
||||
OperationLogModule.SETTING_SYSTEM_ORGANIZATION,
|
||||
request.getName());
|
||||
|
@ -88,7 +88,7 @@ public class SystemProjectLogService {
|
|||
OperationLogConstants.SYSTEM,
|
||||
OperationLogConstants.SYSTEM,
|
||||
project.getId(),
|
||||
project.getCreateUser(),
|
||||
null,
|
||||
OperationLogType.UPDATE.name(),
|
||||
OperationLogModule.SETTING_SYSTEM_ORGANIZATION,
|
||||
project.getName());
|
||||
|
@ -114,7 +114,7 @@ public class SystemProjectLogService {
|
|||
OperationLogConstants.SYSTEM,
|
||||
OperationLogConstants.SYSTEM,
|
||||
id,
|
||||
project.getCreateUser(),
|
||||
null,
|
||||
OperationLogType.DELETE.name(),
|
||||
OperationLogModule.SETTING_SYSTEM_ORGANIZATION,
|
||||
project.getName());
|
||||
|
|
Loading…
Reference in New Issue