refactor(接口测试): 优化查询步骤资源信息接口

This commit is contained in:
AgAngle 2024-04-16 16:12:07 +08:00 committed by 刘瑞斌
parent bc0f1e676a
commit 3d3ad13255
3 changed files with 54 additions and 32 deletions

View File

@ -11,6 +11,8 @@ public class ApiStepResourceInfo {
private Long num; private Long num;
@Schema(description = "资源名称") @Schema(description = "资源名称")
private String name; private String name;
@Schema(description = "是否已删除")
private Boolean delete;
@Schema(description = "所属项目ID") @Schema(description = "所属项目ID")
private String projectId; private String projectId;
@Schema(description = "所属项目名称") @Schema(description = "所属项目名称")

View File

@ -2360,7 +2360,7 @@ public class ApiScenarioService extends MoveNodeService {
apiCommonService.setLinkFileInfo(step.getScenarioId(), msTestElement); apiCommonService.setLinkFileInfo(step.getScenarioId(), msTestElement);
} }
apiCommonService.setEnableCommonScriptProcessorInfo(msTestElement); apiCommonService.setEnableCommonScriptProcessorInfo(msTestElement);
} if (stepDetail instanceof MsScriptElement msScriptElement) { } else if (stepDetail instanceof MsScriptElement msScriptElement) {
apiCommonService.setEnableCommonScriptProcessorInfo(msScriptElement); apiCommonService.setEnableCommonScriptProcessorInfo(msScriptElement);
} }
return JSON.parseObject(JSON.toJSONString(stepDetail)); return JSON.parseObject(JSON.toJSONString(stepDetail));
@ -2871,39 +2871,45 @@ public class ApiScenarioService extends MoveNodeService {
public ApiStepResourceInfo getStepResourceInfo(String resourceId, String resourceType) { public ApiStepResourceInfo getStepResourceInfo(String resourceId, String resourceType) {
ApiResourceType apiResourceType = EnumValidator.validateEnum(ApiResourceType.class, resourceType); ApiResourceType apiResourceType = EnumValidator.validateEnum(ApiResourceType.class, resourceType);
ApiStepResourceInfo apiStepResourceInfo = new ApiStepResourceInfo(); ApiStepResourceInfo apiStepResourceInfo = null;
apiStepResourceInfo.setId(resourceId);
String projectId;
switch (apiResourceType) { switch (apiResourceType) {
case API_SCENARIO -> { case API_SCENARIO ->
ApiScenario apiScenario = apiScenarioMapper.selectByPrimaryKey(resourceId); apiStepResourceInfo = getApiStepResourceInfo(apiScenarioMapper::selectByPrimaryKey, resourceId, (resourceInfo, apiScenario) -> {
apiStepResourceInfo.setId(apiScenario.getId()); resourceInfo.setNum(apiScenario.getNum());
apiStepResourceInfo.setNum(apiScenario.getNum()); resourceInfo.setName(apiScenario.getName());
apiStepResourceInfo.setName(apiScenario.getName()); resourceInfo.setDelete(apiScenario.getDeleted());
projectId = apiScenario.getProjectId(); resourceInfo.setProjectId(apiScenario.getProjectId());
});
case API ->
apiStepResourceInfo = getApiStepResourceInfo(apiDefinitionMapper::selectByPrimaryKey, resourceId, (resourceInfo, apiDefinition) -> {
resourceInfo.setNum(apiDefinition.getNum());
resourceInfo.setName(apiDefinition.getName());
resourceInfo.setDelete(apiDefinition.getDeleted());
resourceInfo.setProjectId(apiDefinition.getProjectId());
});
case API_CASE ->
apiStepResourceInfo = getApiStepResourceInfo(apiTestCaseMapper::selectByPrimaryKey, resourceId, (resourceInfo, apiTestCase) -> {
resourceInfo.setNum(apiTestCase.getNum());
resourceInfo.setName(apiTestCase.getName());
resourceInfo.setDelete(apiTestCase.getDeleted());
resourceInfo.setProjectId(apiTestCase.getProjectId());
});
} }
case API -> { Optional.ofNullable(apiStepResourceInfo).ifPresent(resourceInfo -> {
ApiDefinition apiDefinition = apiDefinitionMapper.selectByPrimaryKey(resourceId); Project project = projectMapper.selectByPrimaryKey(resourceInfo.getProjectId());
apiStepResourceInfo.setId(apiDefinition.getId()); resourceInfo.setProjectName(project.getName());
apiStepResourceInfo.setNum(apiDefinition.getNum()); });
apiStepResourceInfo.setName(apiDefinition.getName());
projectId = apiDefinition.getProjectId();
}
case API_CASE -> {
ApiTestCase apiTestCase = apiTestCaseMapper.selectByPrimaryKey(resourceId);
apiStepResourceInfo.setId(apiTestCase.getId());
apiStepResourceInfo.setNum(apiTestCase.getNum());
apiStepResourceInfo.setName(apiTestCase.getName());
projectId = apiTestCase.getProjectId();
}
default -> {
return apiStepResourceInfo; return apiStepResourceInfo;
} }
private <T> ApiStepResourceInfo getApiStepResourceInfo(Function<String, T> getResourceFunc, String resourceId, BiConsumer<ApiStepResourceInfo, T> setParamFunc) {
T resource = getResourceFunc.apply(resourceId);
if (resource == null) {
return null;
} }
Project project = projectMapper.selectByPrimaryKey(projectId); ApiStepResourceInfo apiStepResourceInfo = new ApiStepResourceInfo();
apiStepResourceInfo.setProjectId(project.getId()); apiStepResourceInfo.setId(resourceId);
apiStepResourceInfo.setProjectName(project.getName()); setParamFunc.accept(apiStepResourceInfo, resource);
return apiStepResourceInfo; return apiStepResourceInfo;
} }

View File

@ -1257,6 +1257,20 @@ public class ApiScenarioControllerTests extends BaseTest {
Assertions.assertEquals(apiStepResourceInfo.getName(), addApiScenario.getName()); Assertions.assertEquals(apiStepResourceInfo.getName(), addApiScenario.getName());
Assertions.assertEquals(apiStepResourceInfo.getProjectName(), projectMapper.selectByPrimaryKey(addApiScenario.getProjectId()).getName()); Assertions.assertEquals(apiStepResourceInfo.getProjectName(), projectMapper.selectByPrimaryKey(addApiScenario.getProjectId()).getName());
// 测试资源在回收站
ApiScenario scenario = new ApiScenario();
scenario.setId(addApiScenario.getId());
scenario.setDeleted(true);
apiScenarioMapper.updateByPrimaryKeySelective(scenario);
mvcResult = this.requestGetAndReturn(STEP_RESOURCE_INFO, addApiScenario.getId(), ApiScenarioStepType.API_SCENARIO.name());
Assertions.assertTrue(getResultData(mvcResult, ApiStepResourceInfo.class).getDelete());
scenario.setDeleted(false);
apiScenarioMapper.updateByPrimaryKeySelective(scenario);
// 测试资源彻底删除
mvcResult = this.requestGetAndReturn(STEP_RESOURCE_INFO, "aaaa", ApiScenarioStepType.API_SCENARIO.name());
Assertions.assertNull(getResultData(mvcResult, ApiStepResourceInfo.class));
this.requestGetAndReturn(STEP_RESOURCE_INFO, addApiScenario.getId(), "AAA"); this.requestGetAndReturn(STEP_RESOURCE_INFO, addApiScenario.getId(), "AAA");
// @@校验权限 // @@校验权限