refactor(接口测试): 优化用例执行接口
This commit is contained in:
parent
f1545c72da
commit
ce94dd62de
|
@ -20,11 +20,11 @@ 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.security.CheckOwner;
|
||||
import io.metersphere.system.uid.IDGenerator;
|
||||
import io.metersphere.system.utils.PageUtils;
|
||||
import io.metersphere.system.utils.Pager;
|
||||
import io.metersphere.system.utils.SessionUtils;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
@ -251,20 +251,6 @@ public class ApiTestCaseController {
|
|||
return fileModuleService.getTree(projectId);
|
||||
}
|
||||
|
||||
@GetMapping("/run/{id}/{reportId}")
|
||||
@Operation(summary = "用例执行,实时获取执行结果")
|
||||
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_EXECUTE)
|
||||
public TaskRequestDTO run(@PathVariable String id, @PathVariable String reportId) {
|
||||
return apiTestCaseService.run(id, reportId, SessionUtils.getUserId());
|
||||
}
|
||||
|
||||
@GetMapping("/run/{id}")
|
||||
@Operation(summary = "用例执行")
|
||||
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_EXECUTE)
|
||||
public TaskRequestDTO run(@PathVariable String id) {
|
||||
return apiTestCaseService.run(id, IDGenerator.nextStr(), SessionUtils.getUserId());
|
||||
}
|
||||
|
||||
@PostMapping("/debug")
|
||||
@Operation(summary = "用例调试")
|
||||
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_EXECUTE)
|
||||
|
@ -272,6 +258,22 @@ public class ApiTestCaseController {
|
|||
return apiTestCaseService.debug(request);
|
||||
}
|
||||
|
||||
@GetMapping("/run/{id}")
|
||||
@Operation(summary = "用例执行, 传ID执行")
|
||||
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_EXECUTE)
|
||||
public TaskRequestDTO run(@PathVariable String id,
|
||||
@Schema(description = "报告ID,传了可以实时获取结果,不传则不支持实时获取")
|
||||
@RequestParam(required = false) String reportId) {
|
||||
return apiTestCaseService.run(id, reportId, SessionUtils.getUserId());
|
||||
}
|
||||
|
||||
@PostMapping("/run")
|
||||
@Operation(summary = "用例执行,传请求详情执行")
|
||||
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_EXECUTE)
|
||||
public TaskRequestDTO run(@Validated @RequestBody ApiRunRequest request) {
|
||||
return apiTestCaseService.run(request, SessionUtils.getUserId());
|
||||
}
|
||||
|
||||
@PostMapping("/batch/run")
|
||||
@Operation(summary = "批量执行")
|
||||
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_EXECUTE)
|
||||
|
|
|
@ -12,8 +12,7 @@ public class ApiDebugRunRequest {
|
|||
@Schema(description = "接口ID")
|
||||
@NotNull
|
||||
private String id;
|
||||
@Schema(description = "报告ID")
|
||||
@NotNull
|
||||
@Schema(description = "报告ID,传了可以实时获取结果,不传则不支持实时获取")
|
||||
private String reportId;
|
||||
/**
|
||||
* 新上传的文件ID
|
||||
|
|
|
@ -626,6 +626,27 @@ public class ApiTestCaseService extends MoveNodeService {
|
|||
return apiFileResourceService.transfer(request, userId, ApiResourceType.API_CASE.name());
|
||||
}
|
||||
|
||||
/**
|
||||
* 接口执行
|
||||
* 传请求详情执行
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
public TaskRequestDTO run(ApiRunRequest request, String userId) {
|
||||
ApiTestCase apiTestCase = checkResourceExist(request.getId());
|
||||
ApiResourceRunRequest runRequest = apiExecuteService.getApiResourceRunRequest(request);
|
||||
apiTestCase.setEnvironmentId(request.getEnvironmentId());
|
||||
return executeRun(runRequest, apiTestCase, request.getReportId(), userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 接口执行
|
||||
* 传ID执行
|
||||
* @param id
|
||||
* @param reportId
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
public TaskRequestDTO run(String id, String reportId, String userId) {
|
||||
ApiTestCase apiTestCase = checkResourceExist(id);
|
||||
ApiTestCaseBlob apiTestCaseBlob = apiTestCaseBlobMapper.selectByPrimaryKey(id);
|
||||
|
@ -633,18 +654,61 @@ public class ApiTestCaseService extends MoveNodeService {
|
|||
ApiResourceRunRequest runRequest = new ApiResourceRunRequest();
|
||||
runRequest.setTestElement(ApiDataUtils.parseObject(new String(apiTestCaseBlob.getRequest()), AbstractMsTestElement.class));
|
||||
|
||||
return executeRun(runRequest, apiTestCase, reportId, userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 接口执行
|
||||
* 保存报告
|
||||
* @param runRequest
|
||||
* @param apiTestCase
|
||||
* @param reportId
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
public TaskRequestDTO executeRun(ApiResourceRunRequest runRequest, ApiTestCase apiTestCase, String reportId, String userId) {
|
||||
String poolId = apiExecuteService.getProjectApiResourcePoolId(apiTestCase.getProjectId());
|
||||
// 初始化报告
|
||||
initApiReport(apiTestCase, reportId, poolId, userId);
|
||||
|
||||
TaskRequestDTO taskRequest = getTaskRequest(reportId, apiTestCase.getId(), apiTestCase.getProjectId(), ApiExecuteRunMode.RUN.name());
|
||||
taskRequest.getRunModeConfig().setPoolId(poolId);
|
||||
taskRequest.setSaveResult(true);
|
||||
|
||||
if (StringUtils.isEmpty(taskRequest.getReportId())) {
|
||||
taskRequest.setRealTime(false);
|
||||
taskRequest.setReportId(IDGenerator.nextStr());
|
||||
} else {
|
||||
// 如果传了报告ID,则实时获取结果
|
||||
taskRequest.setRealTime(true);
|
||||
}
|
||||
|
||||
// 初始化报告
|
||||
initApiReport(apiTestCase, taskRequest.getReportId(), poolId, userId);
|
||||
|
||||
return doExecute(taskRequest, runRequest, apiTestCase.getEnvironmentId());
|
||||
}
|
||||
|
||||
/**
|
||||
* 接口调试
|
||||
* 不存报告,实时获取结果
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
public TaskRequestDTO debug(ApiRunRequest request) {
|
||||
TaskRequestDTO taskRequest = getTaskRequest(request.getReportId(), request.getId(),
|
||||
request.getProjectId(), apiExecuteService.getDebugRunModule(request.getFrontendDebug()));
|
||||
taskRequest.setSaveResult(false);
|
||||
taskRequest.setRealTime(true);
|
||||
|
||||
ApiParamConfig apiParamConfig = apiExecuteService.getApiParamConfig(reportId);
|
||||
ApiResourceRunRequest runRequest = apiExecuteService.getApiResourceRunRequest(request);
|
||||
|
||||
return doExecute(taskRequest, runRequest, request.getEnvironmentId());
|
||||
}
|
||||
|
||||
private TaskRequestDTO doExecute(TaskRequestDTO taskRequest, ApiResourceRunRequest runRequest, String envId) {
|
||||
|
||||
ApiParamConfig apiParamConfig = apiExecuteService.getApiParamConfig(taskRequest.getReportId());
|
||||
// 设置环境
|
||||
apiParamConfig.setEnvConfig(environmentService.get(apiTestCase.getEnvironmentId()));
|
||||
apiParamConfig.setEnvConfig(environmentService.get(envId));
|
||||
|
||||
return apiExecuteService.apiExecute(runRequest, taskRequest, apiParamConfig);
|
||||
}
|
||||
|
@ -699,21 +763,6 @@ public class ApiTestCaseService extends MoveNodeService {
|
|||
return apiReport;
|
||||
}
|
||||
|
||||
public TaskRequestDTO debug(ApiRunRequest request) {
|
||||
ApiResourceRunRequest runRequest = apiExecuteService.getApiResourceRunRequest(request);
|
||||
|
||||
TaskRequestDTO taskRequest = getTaskRequest(request.getReportId(), request.getId(),
|
||||
request.getProjectId(), apiExecuteService.getDebugRunModule(request.getFrontendDebug()));
|
||||
taskRequest.setSaveResult(true);
|
||||
taskRequest.setRealTime(true);
|
||||
|
||||
ApiParamConfig apiParamConfig = apiExecuteService.getApiParamConfig(request.getReportId());
|
||||
// 设置环境
|
||||
apiParamConfig.setEnvConfig(environmentService.get(request.getEnvironmentId()));
|
||||
|
||||
return apiExecuteService.apiExecute(runRequest, taskRequest, apiParamConfig);
|
||||
}
|
||||
|
||||
public TaskRequestDTO getTaskRequest(String reportId, String resourceId, String projectId, String runModule) {
|
||||
TaskRequestDTO taskRequest = apiExecuteService.getTaskRequest(reportId, resourceId, projectId);
|
||||
taskRequest.setResourceType(ApiResourceType.API_CASE.name());
|
||||
|
|
|
@ -97,8 +97,9 @@ public class ApiTestCaseControllerTests extends BaseTest {
|
|||
private static final String EXECUTE = "execute/page";
|
||||
private static final String HISTORY = "operation-history/page";
|
||||
private static final String DEBUG = "debug";
|
||||
private static final String RUN_REAL_TIME = "run/{0}/{1}";
|
||||
private static final String RUN = "run/{0}";
|
||||
private static final String RUN_REAL_TIME = "run/{0}?reportId={1}";
|
||||
private static final String RUN_GET = "run/{0}";
|
||||
private static final String RUN_POST = "run";
|
||||
private static final String BATCH_RUN = "batch/run";
|
||||
|
||||
private static final ResultMatcher ERROR_REQUEST_MATCHER = status().is5xxServerError();
|
||||
|
@ -423,6 +424,13 @@ public class ApiTestCaseControllerTests extends BaseTest {
|
|||
Assertions.assertTrue(resultHolder.getCode() == ApiResultCode.RESOURCE_POOL_EXECUTE_ERROR.getCode() ||
|
||||
resultHolder.getCode() == MsHttpResultCode.SUCCESS.getCode());
|
||||
|
||||
// 测试执行
|
||||
request.setEnvironmentId("envId");
|
||||
mvcResult = this.requestPostAndReturn(RUN_POST, request);
|
||||
resultHolder = JSON.parseObject(mvcResult.getResponse().getContentAsString(Charset.defaultCharset()), ResultHolder.class);
|
||||
Assertions.assertTrue(resultHolder.getCode() == ApiResultCode.RESOURCE_POOL_EXECUTE_ERROR.getCode() ||
|
||||
resultHolder.getCode() == MsHttpResultCode.SUCCESS.getCode());
|
||||
|
||||
// @@校验权限
|
||||
requestPostPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_CASE_EXECUTE, DEBUG, request);
|
||||
}
|
||||
|
@ -435,10 +443,10 @@ public class ApiTestCaseControllerTests extends BaseTest {
|
|||
// @@校验权限
|
||||
requestGetPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_CASE_EXECUTE, RUN_REAL_TIME, apiTestCase.getId(), "11111");
|
||||
|
||||
assertErrorCode(this.requestGet(RUN, apiTestCase.getId()), ApiResultCode.RESOURCE_POOL_EXECUTE_ERROR);
|
||||
assertErrorCode(this.requestGet(RUN_GET, apiTestCase.getId()), ApiResultCode.RESOURCE_POOL_EXECUTE_ERROR);
|
||||
|
||||
// @@校验权限
|
||||
requestGetPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_CASE_EXECUTE, RUN, apiTestCase.getId());
|
||||
requestGetPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_CASE_EXECUTE, RUN_GET, apiTestCase.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue