feat(接口测试): 接口管理模块接口定义接口文档查看

This commit is contained in:
lan-yonghui 2023-12-06 19:14:26 +08:00 committed by Craftsman
parent 359d03662d
commit 6a1e0f8576
5 changed files with 65 additions and 53 deletions

View File

@ -3,8 +3,6 @@ package io.metersphere.api.dto.definition;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.util.List;
/**
* @author lan
*/
@ -18,6 +16,6 @@ public class ApiDefinitionDocDTO{
private String type;
@Schema(description = "接口文档内容")
List<ApiDefinitionDTO> docList;
ApiDefinitionDTO docInfo;
}

View File

@ -0,0 +1,10 @@
package io.metersphere.api.enums;
/**
* @author: LAN
* @date: 2023/11/16 10:42
* @version: 1.0
*/
public enum ApiDefinitionDocType {
ALL, MODULE, API
}

View File

@ -364,8 +364,8 @@ public class ApiDefinitionLogService {
if(null != apiDefinition){
// 2. 使用Optional避免空指针异常
apiDefinitionService.handleBlob(id, apiDefinitionDTO);
BeanUtils.copyBean(apiDefinitionDTO, apiDefinition);
}
BeanUtils.copyBean(apiDefinitionDTO, apiDefinition);
return apiDefinitionDTO;
}

View File

@ -5,6 +5,7 @@ import io.metersphere.api.controller.result.ApiResultCode;
import io.metersphere.api.domain.*;
import io.metersphere.api.dto.debug.ApiFileResourceUpdateRequest;
import io.metersphere.api.dto.definition.*;
import io.metersphere.api.enums.ApiDefinitionDocType;
import io.metersphere.api.enums.ApiReportStatus;
import io.metersphere.api.mapper.*;
import io.metersphere.api.service.ApiFileResourceService;
@ -671,29 +672,30 @@ public class ApiDefinitionService {
ApiDefinitionDocDTO apiDefinitionDocDTO = new ApiDefinitionDocDTO();
apiDefinitionDocDTO.setType(request.getType());
// @@TODO 下载所有/一个模块接口文档时不做分页数据量大的时候会不会有性能问题
if ("ALL".equals(request.getType()) || "MODULE".equals(request.getType())) {
if (ApiDefinitionDocType.ALL.name().equals(request.getType()) || ApiDefinitionDocType.MODULE.name().equals(request.getType())) {
List<ApiDefinitionDTO> list = extApiDefinitionMapper.listDoc(request);
if(null != list){
list.forEach(item-> handleBlob(item.getId(), item));
apiDefinitionDocDTO.setDocTitle("ALL".equals(request.getType()) ? Translator.get(ALL_API) : getModuleTitle(list));
apiDefinitionDocDTO.setDocList(list);
if (!list.isEmpty()) {
ApiDefinitionDTO first = list.get(0);
handleBlob(first.getId(), first);
if(ApiDefinitionDocType.ALL.name().equals(request.getType())){
apiDefinitionDocDTO.setDocTitle(Translator.get(ALL_API));
} else {
ApiDefinitionModule apiDefinitionModule = apiDefinitionModuleMapper.selectByPrimaryKey(first.getModuleId());
if (StringUtils.isNotBlank(apiDefinitionModule.getName())) {
apiDefinitionDocDTO.setDocTitle(apiDefinitionModule.getName());
} else {
throw new MSException(API_DEFINITION_MODULE_NOT_EXIST);
}
}
apiDefinitionDocDTO.setDocInfo(first);
}
} else if ("API".equals(request.getType())) {
} else if (ApiDefinitionDocType.API.name().equals(request.getType())) {
ApiDefinitionDTO apiDefinitionDTO = get(request.getApiId(), userId);
apiDefinitionDocDTO.setDocTitle(apiDefinitionDTO.getName());
apiDefinitionDocDTO.setDocList(Collections.singletonList(apiDefinitionDTO));
apiDefinitionDocDTO.setDocInfo(apiDefinitionDTO);
}
return apiDefinitionDocDTO;
}
private String getModuleTitle(List<ApiDefinitionDTO> list) {
ApiDefinitionDTO first = list.stream().findFirst().orElseThrow(() -> new MSException(API_DEFINITION_NOT_EXIST));
ApiDefinitionModule apiDefinitionModule = apiDefinitionModuleMapper.selectByPrimaryKey(first.getModuleId());
if (StringUtils.isNotBlank(apiDefinitionModule.getName())) {
return apiDefinitionModule.getName();
} else {
throw new MSException(API_DEFINITION_MODULE_NOT_EXIST);
}
}
}

View File

@ -3,9 +3,11 @@ package io.metersphere.api.controller;
import io.metersphere.api.controller.result.ApiResultCode;
import io.metersphere.api.domain.*;
import io.metersphere.api.dto.definition.*;
import io.metersphere.api.enums.ApiDefinitionDocType;
import io.metersphere.api.enums.ApiDefinitionStatus;
import io.metersphere.api.mapper.*;
import io.metersphere.api.service.ApiFileResourceService;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.ApiDataUtils;
import io.metersphere.plugin.api.spi.AbstractMsTestElement;
import io.metersphere.project.dto.filemanagement.FileInfo;
@ -39,6 +41,7 @@ import org.springframework.test.web.servlet.MvcResult;
import java.nio.charset.StandardCharsets;
import java.util.*;
import static io.metersphere.api.controller.result.ApiResultCode.API_DEFINITION_NOT_EXIST;
import static io.metersphere.system.controller.handler.result.MsHttpResultCode.NOT_FOUND;
@ -681,7 +684,7 @@ public class ApiDefinitionControllerTests extends BaseTest {
apiDefinition = apiDefinitionMapper.selectByPrimaryKey("1001");
request.setApiId(apiDefinition.getId());
request.setProjectId(DEFAULT_PROJECT_ID);
request.setType("API");
request.setType(ApiDefinitionDocType.API.name());
// @@请求成功
MvcResult mvcResult = this.requestPostWithOkAndReturn(DOC, request);
ApiDataUtils.setResolver(MsHTTPElement.class);
@ -695,11 +698,11 @@ public class ApiDefinitionControllerTests extends BaseTest {
copyApiDefinitionDTO.setResponse(ApiDataUtils.parseArray(new String(apiDefinitionBlob.getResponse()), HttpResponse.class));
}
copyApiDefinitionDocDTO.setDocTitle(apiDefinition.getName());
copyApiDefinitionDocDTO.setType("API");
copyApiDefinitionDocDTO.setDocList(Collections.singletonList(copyApiDefinitionDTO));
copyApiDefinitionDocDTO.setType(ApiDefinitionDocType.API.name());
copyApiDefinitionDocDTO.setDocInfo(copyApiDefinitionDTO);
Assertions.assertEquals(apiDefinitionDocDTO.getType(), copyApiDefinitionDocDTO.getType());
Assertions.assertEquals(apiDefinitionDocDTO.getDocTitle(), copyApiDefinitionDocDTO.getDocTitle());
Assertions.assertEquals(apiDefinitionDocDTO.getDocList().size(), copyApiDefinitionDocDTO.getDocList().size());
Assertions.assertEquals(apiDefinitionDocDTO.getDocInfo().getId(), copyApiDefinitionDocDTO.getDocInfo().getId());
request.setApiId("111");
assertErrorCode(this.requestPost(DOC, request), ApiResultCode.API_DEFINITION_NOT_EXIST);
@ -707,7 +710,7 @@ public class ApiDefinitionControllerTests extends BaseTest {
// @@模块查看文档
request.setApiId(null);
request.setProjectId(DEFAULT_PROJECT_ID);
request.setType("MODULE");
request.setType(ApiDefinitionDocType.MODULE.name());
request.setModuleIds(List.of("10001"));
MvcResult mvcResultModule = this.requestPostWithOkAndReturn(DOC, request);
ApiDataUtils.setResolver(MsHTTPElement.class);
@ -716,30 +719,29 @@ public class ApiDefinitionControllerTests extends BaseTest {
ApiDefinitionDocDTO copyModuleApiDefinitionDocDTO = new ApiDefinitionDocDTO();
List<ApiDefinitionDTO> list = extApiDefinitionMapper.listDoc(request);
if(null != list){
list.forEach(item-> {
ApiDefinitionBlob moduleApiDefinitionBlob = apiDefinitionBlobMapper.selectByPrimaryKey(item.getId());
if(moduleApiDefinitionBlob != null){
item.setRequest(ApiDataUtils.parseObject(new String(moduleApiDefinitionBlob.getRequest()), AbstractMsTestElement.class));
item.setResponse(ApiDataUtils.parseArray(new String(moduleApiDefinitionBlob.getResponse()), HttpResponse.class));
}
ApiDefinitionModule apiDefinitionModule = apiDefinitionModuleMapper.selectByPrimaryKey(item.getModuleId());
if(StringUtils.isBlank(copyModuleApiDefinitionDocDTO.getDocTitle())){
copyModuleApiDefinitionDocDTO.setDocTitle(apiDefinitionModule.getName());
}
});
ApiDefinitionDTO first = list.stream().findFirst().orElseThrow(() -> new MSException(API_DEFINITION_NOT_EXIST));
ApiDefinitionBlob moduleApiDefinitionBlob = apiDefinitionBlobMapper.selectByPrimaryKey(first.getId());
if(moduleApiDefinitionBlob != null){
first.setRequest(ApiDataUtils.parseObject(new String(moduleApiDefinitionBlob.getRequest()), AbstractMsTestElement.class));
first.setResponse(ApiDataUtils.parseArray(new String(moduleApiDefinitionBlob.getResponse()), HttpResponse.class));
}
ApiDefinitionModule apiDefinitionModule = apiDefinitionModuleMapper.selectByPrimaryKey(first.getModuleId());
if(StringUtils.isBlank(copyModuleApiDefinitionDocDTO.getDocTitle())){
copyModuleApiDefinitionDocDTO.setDocTitle(apiDefinitionModule.getName());
}
copyModuleApiDefinitionDocDTO.setDocInfo(first);
copyModuleApiDefinitionDocDTO.setType(ApiDefinitionDocType.MODULE.name());
}
copyModuleApiDefinitionDocDTO.setType("MODULE");
copyModuleApiDefinitionDocDTO.setDocList(list);
Assertions.assertEquals(moduleApiDefinitionDocDTO.getType(), copyModuleApiDefinitionDocDTO.getType());
Assertions.assertEquals(moduleApiDefinitionDocDTO.getDocTitle(), copyModuleApiDefinitionDocDTO.getDocTitle());
Assertions.assertEquals(moduleApiDefinitionDocDTO.getDocList().size(), copyModuleApiDefinitionDocDTO.getDocList().size());
Assertions.assertEquals(moduleApiDefinitionDocDTO.getDocInfo().getId(), copyModuleApiDefinitionDocDTO.getDocInfo().getId());
// @@查看全部文档
request.setApiId(null);
request.setModuleIds(null);
request.setProjectId(DEFAULT_PROJECT_ID);
request.setType("ALL");
request.setType(ApiDefinitionDocType.ALL.name());
MvcResult mvcResultAll = this.requestPostWithOkAndReturn(DOC, request);
ApiDataUtils.setResolver(MsHTTPElement.class);
ApiDefinitionDocDTO allApiDefinitionDocDTO = ApiDataUtils.parseObject(JSON.toJSONString(parseResponse(mvcResultAll).get("data")), ApiDefinitionDocDTO.class);
@ -747,22 +749,22 @@ public class ApiDefinitionControllerTests extends BaseTest {
ApiDefinitionDocDTO copyAllApiDefinitionDocDTO = new ApiDefinitionDocDTO();
List<ApiDefinitionDTO> allList = extApiDefinitionMapper.listDoc(request);
if(null != allList){
allList.forEach(item-> {
ApiDefinitionBlob allApiDefinitionBlob = apiDefinitionBlobMapper.selectByPrimaryKey(item.getId());
if(allApiDefinitionBlob != null){
item.setRequest(ApiDataUtils.parseObject(new String(allApiDefinitionBlob.getRequest()), AbstractMsTestElement.class));
item.setResponse(ApiDataUtils.parseArray(new String(allApiDefinitionBlob.getResponse()), HttpResponse.class));
}
if(StringUtils.isBlank(copyAllApiDefinitionDocDTO.getDocTitle())){
copyAllApiDefinitionDocDTO.setDocTitle(Translator.get(ALL_API));
}
});
ApiDefinitionDTO info = allList.stream().findFirst().orElseThrow(() -> new MSException(API_DEFINITION_NOT_EXIST));
ApiDefinitionBlob allApiDefinitionBlob = apiDefinitionBlobMapper.selectByPrimaryKey(info.getId());
if(allApiDefinitionBlob != null){
info.setRequest(ApiDataUtils.parseObject(new String(allApiDefinitionBlob.getRequest()), AbstractMsTestElement.class));
info.setResponse(ApiDataUtils.parseArray(new String(allApiDefinitionBlob.getResponse()), HttpResponse.class));
}
if(StringUtils.isBlank(copyAllApiDefinitionDocDTO.getDocTitle())){
copyAllApiDefinitionDocDTO.setDocTitle(Translator.get(ALL_API));
}
copyAllApiDefinitionDocDTO.setType(ApiDefinitionDocType.ALL.name());
copyAllApiDefinitionDocDTO.setDocInfo(info);
}
copyAllApiDefinitionDocDTO.setType("ALL");
copyAllApiDefinitionDocDTO.setDocList(allList);
Assertions.assertEquals(allApiDefinitionDocDTO.getType(), copyAllApiDefinitionDocDTO.getType());
Assertions.assertEquals(allApiDefinitionDocDTO.getDocTitle(), copyAllApiDefinitionDocDTO.getDocTitle());
Assertions.assertEquals(allApiDefinitionDocDTO.getDocList().size(), copyAllApiDefinitionDocDTO.getDocList().size());
Assertions.assertEquals(allApiDefinitionDocDTO.getDocInfo().getId(), copyAllApiDefinitionDocDTO.getDocInfo().getId());
// @@校验权限
requestPostPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_READ, DOC, request);