fix(接口测试): 接口管理模块接口定义日志保存 Blob 内容

This commit is contained in:
lan-yonghui 2023-12-01 11:33:39 +08:00 committed by Craftsman
parent 83ad5af322
commit b7d117ba79
4 changed files with 49 additions and 39 deletions

View File

@ -223,12 +223,6 @@ public class ApiFileResourceService {
return apiFileResourceMapper.selectByExample(example); return apiFileResourceMapper.selectByExample(example);
} }
public List<String> getFileIdsByResourceId(String resourceId) {
return getByResourceId(resourceId).stream()
.map(ApiFileResource::getFileId)
.toList();
}
/** /**
* 上传临时文件 * 上传临时文件
* system/temp/{fileId}/{fileName} * system/temp/{fileId}/{fileName}

View File

@ -63,8 +63,8 @@ public class ApiDefinitionLogService {
* @return * @return
*/ */
public LogDTO updateLog(ApiDefinitionUpdateRequest request) { public LogDTO updateLog(ApiDefinitionUpdateRequest request) {
ApiDefinition apiDefinition = apiDefinitionService.get(request.getId(), SessionUtils.getUserId()); ApiDefinitionDTO apiDefinition = apiDefinitionService.getInfo(request.getId(), SessionUtils.getUserId());
if(apiDefinition != null){ if(apiDefinition.getId() != null){
LogDTO dto = new LogDTO( LogDTO dto = new LogDTO(
request.getProjectId(), request.getProjectId(),
null, null,
@ -89,8 +89,8 @@ public class ApiDefinitionLogService {
* @return * @return
*/ */
public LogDTO delLog(ApiDefinitionDeleteRequest request) { public LogDTO delLog(ApiDefinitionDeleteRequest request) {
ApiDefinition apiDefinition = apiDefinitionService.get(request.getId(), SessionUtils.getUserId()); ApiDefinitionDTO apiDefinition = apiDefinitionService.getInfo(request.getId(), SessionUtils.getUserId());
if(apiDefinition != null){ if(apiDefinition.getId() != null){
LogDTO dto = new LogDTO( LogDTO dto = new LogDTO(
request.getProjectId(), request.getProjectId(),
null, null,
@ -173,8 +173,8 @@ public class ApiDefinitionLogService {
} }
public LogDTO copyLog(ApiDefinitionCopyRequest request) { public LogDTO copyLog(ApiDefinitionCopyRequest request) {
ApiDefinition apiDefinition = apiDefinitionService.get(request.getId(), SessionUtils.getUserId()); ApiDefinitionDTO apiDefinition = apiDefinitionService.getInfo(request.getId(), SessionUtils.getUserId());
if(apiDefinition != null){ if(apiDefinition.getId() != null){
LogDTO dto = new LogDTO( LogDTO dto = new LogDTO(
apiDefinition.getProjectId(), apiDefinition.getProjectId(),
null, null,
@ -219,8 +219,8 @@ public class ApiDefinitionLogService {
} }
public LogDTO followLog(String id) { public LogDTO followLog(String id) {
ApiDefinition apiDefinition = apiDefinitionService.get(id, SessionUtils.getUserId()); ApiDefinitionDTO apiDefinition = apiDefinitionService.getInfo(id, SessionUtils.getUserId());
if(apiDefinition != null){ if(apiDefinition.getId() != null){
Project project = projectMapper.selectByPrimaryKey(apiDefinition.getProjectId()); Project project = projectMapper.selectByPrimaryKey(apiDefinition.getProjectId());
LogDTO dto = new LogDTO( LogDTO dto = new LogDTO(
apiDefinition.getProjectId(), apiDefinition.getProjectId(),
@ -246,8 +246,8 @@ public class ApiDefinitionLogService {
* @return * @return
*/ */
public LogDTO restoreLog(ApiDefinitionDeleteRequest request) { public LogDTO restoreLog(ApiDefinitionDeleteRequest request) {
ApiDefinition apiDefinition = apiDefinitionService.get(request.getId(), SessionUtils.getUserId()); ApiDefinitionDTO apiDefinition = apiDefinitionService.getInfo(request.getId(), SessionUtils.getUserId());
if(apiDefinition != null){ if(apiDefinition.getId() != null){
LogDTO dto = new LogDTO( LogDTO dto = new LogDTO(
request.getProjectId(), request.getProjectId(),
null, null,
@ -304,8 +304,8 @@ public class ApiDefinitionLogService {
* 删除回收站接口定义接口日志 * 删除回收站接口定义接口日志
*/ */
public LogDTO trashDelLog(ApiDefinitionDeleteRequest request) { public LogDTO trashDelLog(ApiDefinitionDeleteRequest request) {
ApiDefinition apiDefinition = apiDefinitionService.get(request.getId(), SessionUtils.getUserId()); ApiDefinitionDTO apiDefinition = apiDefinitionService.getInfo(request.getId(), SessionUtils.getUserId());
if(apiDefinition != null){ if(apiDefinition.getId() != null){
LogDTO dto = new LogDTO( LogDTO dto = new LogDTO(
request.getProjectId(), request.getProjectId(),
null, null,

View File

@ -16,7 +16,10 @@ import io.metersphere.sdk.constants.ApplicationNumScope;
import io.metersphere.sdk.constants.DefaultRepositoryDir; import io.metersphere.sdk.constants.DefaultRepositoryDir;
import io.metersphere.sdk.constants.ModuleConstants; import io.metersphere.sdk.constants.ModuleConstants;
import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.*; import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.FileAssociationSourceUtil;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.SubListUtils;
import io.metersphere.system.dto.table.TableBatchProcessDTO; import io.metersphere.system.dto.table.TableBatchProcessDTO;
import io.metersphere.system.log.constants.OperationLogModule; import io.metersphere.system.log.constants.OperationLogModule;
import io.metersphere.system.service.UserLoginService; import io.metersphere.system.service.UserLoginService;
@ -114,24 +117,9 @@ public class ApiDefinitionService {
} }
public ApiDefinitionDTO get(String id, String userId){ public ApiDefinitionDTO get(String id, String userId){
ApiDefinitionDTO apiDefinitionDTO = new ApiDefinitionDTO();
// 1. 避免重复查询数据库将查询结果传递给get方法 // 1. 避免重复查询数据库将查询结果传递给get方法
ApiDefinition apiDefinition = checkApiDefinition(id); ApiDefinition apiDefinition = checkApiDefinition(id);
// 2. 使用Optional避免空指针异常 return getApiDefinitionInfo(id, userId, apiDefinition);
Optional<ApiDefinitionBlob> apiDefinitionBlobOptional = Optional.ofNullable(apiDefinitionBlobMapper.selectByPrimaryKey(id));
apiDefinitionBlobOptional.ifPresent(blob -> {
apiDefinitionDTO.setRequest(ApiDataUtils.parseObject(new String(blob.getRequest()), AbstractMsTestElement.class));
// blob.getResponse() null 时不进行转换
if (blob.getResponse() != null) {
apiDefinitionDTO.setResponse(ApiDataUtils.parseArray(new String(blob.getResponse()), HttpResponse.class));
}
});
// 3. 使用Stream简化集合操作
ApiDefinitionFollowerExample example = new ApiDefinitionFollowerExample();
example.createCriteria().andApiDefinitionIdEqualTo(id).andUserIdEqualTo(userId);
apiDefinitionDTO.setFollow(apiDefinitionFollowerMapper.countByExample(example) > 0);
BeanUtils.copyBean(apiDefinitionDTO, apiDefinition);
return apiDefinitionDTO;
} }
public ApiDefinition create(ApiDefinitionAddRequest request, String userId) { public ApiDefinition create(ApiDefinitionAddRequest request, String userId) {
@ -652,4 +640,33 @@ public class ApiDefinitionService {
public String uploadTempFile(MultipartFile file) { public String uploadTempFile(MultipartFile file) {
return apiFileResourceService.uploadTempFile(file); return apiFileResourceService.uploadTempFile(file);
} }
public ApiDefinitionDTO getInfo(String id, String userId){
ApiDefinitionDTO apiDefinitionDTO = new ApiDefinitionDTO();
ApiDefinition apiDefinition = apiDefinitionMapper.selectByPrimaryKey(id);
if(apiDefinition != null){
apiDefinitionDTO = getApiDefinitionInfo(id, userId, apiDefinition);
}
return apiDefinitionDTO;
}
public ApiDefinitionDTO getApiDefinitionInfo(String id, String userId, ApiDefinition apiDefinition) {
ApiDefinitionDTO apiDefinitionDTO = new ApiDefinitionDTO();
// 2. 使用Optional避免空指针异常
Optional<ApiDefinitionBlob> apiDefinitionBlobOptional = Optional.ofNullable(apiDefinitionBlobMapper.selectByPrimaryKey(id));
apiDefinitionBlobOptional.ifPresent(blob -> {
apiDefinitionDTO.setRequest(ApiDataUtils.parseObject(new String(blob.getRequest()), AbstractMsTestElement.class));
// blob.getResponse() null 时不进行转换
if (blob.getResponse() != null) {
apiDefinitionDTO.setResponse(ApiDataUtils.parseArray(new String(blob.getResponse()), HttpResponse.class));
}
});
// 3. 使用Stream简化集合操作
ApiDefinitionFollowerExample example = new ApiDefinitionFollowerExample();
example.createCriteria().andApiDefinitionIdEqualTo(id).andUserIdEqualTo(userId);
apiDefinitionDTO.setFollow(apiDefinitionFollowerMapper.countByExample(example) > 0);
BeanUtils.copyBean(apiDefinitionDTO, apiDefinition);
return apiDefinitionDTO;
}
} }

View File

@ -23,7 +23,6 @@ import io.metersphere.system.dto.sdk.BaseCondition;
import io.metersphere.system.file.FileCenter; import io.metersphere.system.file.FileCenter;
import io.metersphere.system.file.FileRequest; import io.metersphere.system.file.FileRequest;
import io.metersphere.system.log.constants.OperationLogType; import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.uid.IDGenerator;
import io.metersphere.system.utils.Pager; import io.metersphere.system.utils.Pager;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
@ -35,10 +34,7 @@ import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.jdbc.Sql; import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.SqlConfig; import org.springframework.test.context.jdbc.SqlConfig;
import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.MvcResult;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import java.io.File;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
@ -647,6 +643,9 @@ public class ApiDefinitionControllerTests extends BaseTest {
}); });
} }
checkLog(apiDefinitionDeleteRequest.getId(), OperationLogType.DELETE); checkLog(apiDefinitionDeleteRequest.getId(), OperationLogType.DELETE);
apiDefinitionDeleteRequest.setId("121");
apiDefinitionDeleteRequest.setDeleteAll(false);
assertErrorCode(this.requestPost(DELETE, apiDefinitionDeleteRequest), ApiResultCode.API_DEFINITION_NOT_EXIST);
// @@校验权限 // @@校验权限
requestPostPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_DELETE, DELETE, apiDefinitionDeleteRequest); requestPostPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_DELETE, DELETE, apiDefinitionDeleteRequest);
} }