refactor(项目设置): 文件管理拖拽接口的重构

修改了接口入参以及的查询逻辑
This commit is contained in:
song-tianyang 2023-10-26 17:14:03 +08:00 committed by f2c-ci-robot[bot]
parent d6aff9f192
commit e9ac9d34e9
19 changed files with 358 additions and 144 deletions

View File

@ -1,24 +1,20 @@
package io.metersphere.sdk.dto.request;
import io.metersphere.sdk.constants.ModuleConstants;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotEmpty;
import lombok.Data;
@Data
public class NodeMoveRequest {
@Schema(description = "模块ID", requiredMode = Schema.RequiredMode.REQUIRED)
@Schema(description = "被拖拽的节点", requiredMode = Schema.RequiredMode.REQUIRED)
@NotEmpty(message = "{node.not_blank}")
private String nodeId;
private String dragNodeId;
@Schema(description = "父模块ID", requiredMode = Schema.RequiredMode.REQUIRED)
@NotEmpty(message = "{parent.node.not_blank}")
private String parentId = ModuleConstants.ROOT_NODE_PARENT_ID;
@Schema(description = "放入的节点", requiredMode = Schema.RequiredMode.REQUIRED)
@NotEmpty(message = "{node.not_blank}")
private String dropNodeId;
@Schema(description = "前一个节点ID", requiredMode = Schema.RequiredMode.REQUIRED)
private String previousNodeId;
@Schema(description = "后一个节点ID", requiredMode = Schema.RequiredMode.REQUIRED)
private String nextNodeId;
@Schema(description = "放入的位置(取值:-1,0,1。 -1dropNodeId节点之前。 0:dropNodeId节点内。 1dropNodeId节点后", requiredMode = Schema.RequiredMode.REQUIRED)
private int dropPosition;
}

View File

@ -414,6 +414,7 @@ resource_pool_not_exist=资源池不存在
#file management
file_module.not.exist=文件模块不存在
file_module.parent.not.exist=文件模块父节点不存在
upload.file.error=上传文件失败
file.not.exist=文件不存在
old.file.not.exist=旧文件不存在

View File

@ -450,6 +450,7 @@ resource_pool_not_exist=Resource pool does not exist
#file management
file_module.not.exist=File module does not exist
file_module.parent.not.exist=File module parent does not exist
upload.file.error=Upload file error
file.not.exist=File does not exist
old.file.not.exist=Old file does not exist

View File

@ -449,6 +449,7 @@ resource_pool_not_exist=资源池不存在
#file management
file_module.not.exist=文件模块不存在
file_module.parent.not.exist=文件模块父节点不存在
upload.file.error=上传文件失败
file.not.exist=文件不存在
old.file.not.exist=旧文件不存在

View File

@ -450,6 +450,7 @@ message.domain.schedule_enable=是否開啟
resource_pool_not_exist=資源池不存在
#file management
file_module.not.exist=文件模塊不存在
file_module.parent.not.exist=文件模塊父節點不存在
upload.file.error=上傳文件失敗
file.not.exist=文件不存在
old.file.not.exist=舊文件不存在

View File

@ -31,6 +31,13 @@ public class FileManagementController {
@Resource
private FileManagementService fileManagementService;
@GetMapping(value = "/type/{projectId}")
@Operation(summary = "项目管理-文件管理-获取已存在的文件类型")
@RequiresPermissions(PermissionConstants.PROJECT_FILE_MANAGEMENT_READ)
public List<String> getFileType(@PathVariable String projectId) {
return fileMetadataService.getFileType(projectId);
}
@PostMapping("/page")
@Operation(summary = "项目管理-文件管理-表格分页查询文件")
@RequiresPermissions(PermissionConstants.PROJECT_FILE_MANAGEMENT_READ)
@ -38,6 +45,7 @@ public class FileManagementController {
return fileMetadataService.page(request);
}
@PostMapping("/module/count")
@Operation(summary = "项目管理-文件管理-表格分页查询文件")
@RequiresPermissions(PermissionConstants.PROJECT_FILE_MANAGEMENT_READ)

View File

@ -60,12 +60,7 @@ public class FileModuleController {
@PostMapping("/move")
@Operation(summary = "项目管理-文件管理-模块-移动模块")
@RequiresPermissions(PermissionConstants.PROJECT_FILE_MANAGEMENT_READ_UPDATE)
public void moveNode(@RequestBody NodeMoveRequest request) {
/**
* 拖拽操作 两种同级移动 跨级移动
* 1.判断移动后的parentID判断是否是移动到其余的目录下
* 2.拖拽后的前后ID 用于排序
*/
public void moveNode(@Validated @RequestBody NodeMoveRequest request) {
fileModuleService.moveNode(request, SessionUtils.getUserId());
}
}

View File

@ -0,0 +1,29 @@
package io.metersphere.project.dto;
import io.metersphere.sdk.dto.BaseModule;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class NodeSortDTO {
@Schema(description = "模块ID", requiredMode = Schema.RequiredMode.REQUIRED)
@NotNull(message = "{file_module.not.exist}")
private BaseModule node;
@Schema(description = "父模块ID", requiredMode = Schema.RequiredMode.REQUIRED)
@NotNull(message = "{file_module.parent.not.exist}")
private BaseModule parent;
@Schema(description = "前一个节点")
private BaseModule previousNode;
@Schema(description = "后一个节点")
private BaseModule nextNode;
}

View File

@ -26,4 +26,5 @@ public interface ExtFileMetadataMapper {
List<FileMetadata> selectRefIdByModuleIds(@Param("moduleIds") List<String> moduleIds);
List<String> selectFileTypeByProjectId(String projectId);
}

View File

@ -117,6 +117,11 @@
#{item}
</foreach>
</select>
<select id="selectFileTypeByProjectId" resultType="java.lang.String">
SELECT DISTINCT f.type
FROM file_metadata f
WHERE f.project_id = #{projectId}
</select>
</mapper>

View File

@ -1,5 +1,6 @@
package io.metersphere.project.mapper;
import io.metersphere.project.domain.FileModule;
import io.metersphere.sdk.dto.BaseTreeNode;
import org.apache.ibatis.annotations.Param;
@ -19,4 +20,10 @@ public interface ExtFileModuleMapper {
Integer getMaxPosByParentId(String parentId);
List<String> selectIdsByProjectId(String projectId);
FileModule getLastModuleByParentId(String id);
FileModule getNextModuleInParentId(@Param("parentId") String parentId, @Param("pos") int pos);
FileModule getPreviousModuleInParentId(@Param("parentId") String parentId, @Param("pos") int pos);
}

View File

@ -37,5 +37,29 @@
SELECT id FROM file_module WHERE parent_id = #{0}
ORDER BY pos ASC
</select>
<select id="getLastModuleByParentId" resultType="io.metersphere.project.domain.FileModule">
SELECT *
FROM file_module
WHERE parent_id = #{0}
ORDER BY pos DESC
LIMIT 1
</select>
<select id="getNextModuleInParentId" resultType="io.metersphere.project.domain.FileModule">
SELECT *
from file_module
WHERE parent_id = #{parentId}
AND pos &gt; #{pos}
order by pos asc
limit 1
</select>
<select id="getPreviousModuleInParentId" resultType="io.metersphere.project.domain.FileModule">
SELECT *
from file_module
WHERE parent_id = #{parentId}
AND pos &lt; #{pos}
order by pos
desc
limit 1
</select>
</mapper>

View File

@ -294,14 +294,14 @@ public class FileMetadataService {
//获取模块统计
public Map<String, Long> moduleCount(FileMetadataTableRequest request, String operator) {
//查出每个模块节点下的资源数量
//查出每个模块节点下的资源数量 不需要按照模块进行筛选
FileManagementPageDTO pageDTO = new FileManagementPageDTO(request);
pageDTO.setModuleIds(null);
List<ModuleCountDTO> moduleCountDTOList = extFileMetadataMapper.countModuleIdByKeywordAndFileType(pageDTO);
long allCount = fileModuleService.getAllCount(moduleCountDTOList);
//查出我的文件数量
pageDTO.setOperator(operator);
long myFileCount = extFileMetadataMapper.countMyFile(pageDTO);
Map<String, Long> moduleCountMap = fileModuleService.getModuleCountMap(request.getProjectId(), moduleCountDTOList);
moduleCountMap.put(FILE_MODULE_COUNT_MY, myFileCount);
moduleCountMap.put(FILE_MODULE_COUNT_ALL, allCount);
@ -331,4 +331,8 @@ public class FileMetadataService {
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + this.getFileName(fileMetadata.getName(), fileMetadata.getType()) + "\"")
.body(bytes);
}
public List<String> getFileType(String projectId) {
return extFileMetadataMapper.selectFileTypeByProjectId(projectId);
}
}

View File

@ -2,12 +2,13 @@ package io.metersphere.project.service;
import io.metersphere.project.domain.FileModule;
import io.metersphere.project.domain.Project;
import io.metersphere.project.dto.NodeSortDTO;
import io.metersphere.project.mapper.FileModuleMapper;
import io.metersphere.project.mapper.ProjectMapper;
import io.metersphere.sdk.constants.HttpMethodConstants;
import io.metersphere.sdk.dto.BaseModule;
import io.metersphere.sdk.dto.LogDTO;
import io.metersphere.sdk.dto.builder.LogDTOBuilder;
import io.metersphere.sdk.dto.request.NodeMoveRequest;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.log.constants.OperationLogModule;
@ -16,6 +17,7 @@ import io.metersphere.system.log.service.OperationLogService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
@Service
@Transactional(rollbackFor = Exception.class)
@ -79,24 +81,11 @@ public class FileModuleLogService {
operationLogService.add(dto);
}
public void saveMoveLog(NodeMoveRequest request, String operator) {
FileModule moveNode;
FileModule previousNode = null;
FileModule nextNode = null;
FileModule parentModule;
moveNode = fileModuleMapper.selectByPrimaryKey(request.getNodeId());
if (request.getPreviousNodeId() != null) {
previousNode = fileModuleMapper.selectByPrimaryKey(request.getPreviousNodeId());
}
if (request.getNextNodeId() != null) {
nextNode = fileModuleMapper.selectByPrimaryKey(request.getNextNodeId());
}
parentModule = fileModuleMapper.selectByPrimaryKey(request.getParentId());
if (parentModule == null) {
parentModule = new FileModule();
parentModule.setName(Translator.get("file.module.root"));
}
public void saveMoveLog(@Validated NodeSortDTO request, String operator) {
BaseModule moveNode = request.getNode();
BaseModule previousNode = request.getPreviousNode();
BaseModule nextNode = request.getNextNode();
BaseModule parentModule = request.getParent();
Project project = projectMapper.selectByPrimaryKey(moveNode.getProjectId());
String logContent;

View File

@ -3,6 +3,7 @@ package io.metersphere.project.service;
import io.metersphere.project.domain.FileModule;
import io.metersphere.project.domain.FileModuleExample;
import io.metersphere.project.dto.ModuleCountDTO;
import io.metersphere.project.dto.NodeSortDTO;
import io.metersphere.project.mapper.ExtFileModuleMapper;
import io.metersphere.project.mapper.FileModuleMapper;
import io.metersphere.project.request.filemanagement.FileModuleCreateRequest;
@ -43,13 +44,13 @@ public class FileModuleService extends ModuleTreeService implements CleanupProje
public List<BaseTreeNode> getTree(String projectId) {
List<BaseTreeNode> fileModuleList = extFileModuleMapper.selectBaseByProjectId(projectId);
return this.traverseToBuildTree(fileModuleList, true);
return super.buildTreeAndCountResource(fileModuleList, true);
}
//节点内容只有Id和parentId
public List<BaseTreeNode> getTreeOnlyIdsAndResourceCount(String projectId, List<ModuleCountDTO> moduleCountDTOList) {
//节点内容只有Id和parentId
List<BaseTreeNode> fileModuleList = extFileModuleMapper.selectIdAndParentIdByProjectId(projectId);
return this.traverseToBuildTree(fileModuleList, moduleCountDTOList, true);
return super.buildTreeAndCountResource(fileModuleList, moduleCountDTOList, true);
}
public String add(FileModuleCreateRequest request, String operator) {
@ -150,28 +151,92 @@ public class FileModuleService extends ModuleTreeService implements CleanupProje
}
public void moveNode(NodeMoveRequest request, String currentUser) {
BaseModule module;
BaseModule parentModule;
BaseModule previousNode = null;
BaseModule nextNode = null;
FileModule dragNode = fileModuleMapper.selectByPrimaryKey(request.getDragNodeId());
if (dragNode == null) {
throw new MSException("file_module.not.exist:" + request.getDragNodeId());
} else {
module = new BaseModule(dragNode.getId(), dragNode.getName(), dragNode.getPos(), dragNode.getProjectId(), dragNode.getParentId());
}
if (StringUtils.equals(request.getDragNodeId(), request.getDropNodeId())) {
//两种节点不能一样
throw new MSException(Translator.get("invalid_parameter"));
}
FileModule dropNode = fileModuleMapper.selectByPrimaryKey(request.getDropNodeId());
if (dropNode == null) {
throw new MSException("file_module.not.exist:" + request.getDropNodeId());
}
if (request.getDropPosition() == 0) {
//dropPosition=0: 放到dropNode节点内最后一个节点之后
parentModule = new BaseModule(dropNode.getId(), dropNode.getName(), dropNode.getPos(), dropNode.getProjectId(), dropNode.getParentId());
FileModule previousModule = extFileModuleMapper.getLastModuleByParentId(parentModule.getId());
if (previousModule != null) {
previousNode = new BaseModule(previousModule.getId(), previousModule.getName(), previousModule.getPos(), previousModule.getProjectId(), previousModule.getParentId());
}
} else {
if (StringUtils.equals(dropNode.getParentId(), ModuleConstants.ROOT_NODE_PARENT_ID)) {
parentModule = new BaseModule(ModuleConstants.ROOT_NODE_PARENT_ID, ModuleConstants.ROOT_NODE_PARENT_ID, 0, module.getProjectId(), ModuleConstants.ROOT_NODE_PARENT_ID);
} else {
FileModule parent = fileModuleMapper.selectByPrimaryKey(dropNode.getParentId());
parentModule = new BaseModule(parent.getId(), parent.getName(), parent.getPos(), parent.getProjectId(), parent.getParentId());
}
if (request.getDropPosition() == 1) {
//dropPosition=1: 放到dropNode节点后原dropNode后面的节点之前
previousNode = new BaseModule(dropNode.getId(), dropNode.getName(), dropNode.getPos(), dropNode.getProjectId(), dropNode.getParentId());
FileModule nextModule = extFileModuleMapper.getNextModuleInParentId(previousNode.getParentId(), previousNode.getPos());
if (nextModule != null) {
nextNode = new BaseModule(nextModule.getId(), nextModule.getName(), nextModule.getPos(), nextModule.getProjectId(), nextModule.getParentId());
}
} else if (request.getDropPosition() == -1) {
//dropPosition=-1: 放到dropNode节点前原dropNode前面的节点之后
nextNode = new BaseModule(dropNode.getId(), dropNode.getName(), dropNode.getPos(), dropNode.getProjectId(), dropNode.getParentId());
FileModule previousModule = extFileModuleMapper.getPreviousModuleInParentId(nextNode.getParentId(), nextNode.getPos());
if (previousModule != null) {
previousNode = new BaseModule(previousModule.getId(), previousModule.getName(), previousModule.getPos(), previousModule.getProjectId(), previousModule.getParentId());
}
} else {
throw new MSException(Translator.get("invalid_parameter"));
}
}
FileModuleExample example = new FileModuleExample();
example.createCriteria().andParentIdEqualTo(request.getParentId()).andIdEqualTo(request.getNodeId());
example.createCriteria().andParentIdEqualTo(parentModule.getId()).andIdEqualTo(module.getId());
//节点换到了别的节点下,要先更新parent节点.
if (fileModuleMapper.countByExample(example) == 0) {
//节点换到了别的节点下
FileModule fileModule = new FileModule();
fileModule.setId(request.getNodeId());
fileModule.setParentId(request.getParentId());
fileModule.setId(module.getId());
fileModule.setParentId(parentModule.getId());
fileModuleMapper.updateByPrimaryKeySelective(fileModule);
}
this.sort(request);
NodeSortDTO nodeMoveDTO = new NodeSortDTO(module, parentModule, previousNode, nextNode);
super.sort(nodeMoveDTO);
//记录日志
fileModuleLogService.saveMoveLog(request, currentUser);
fileModuleLogService.saveMoveLog(nodeMoveDTO, currentUser);
}
/**
* 查找当前项目下模块每个节点对应的资源统计
*
* @param projectId
* @param moduleCountDTOList
* @return
*/
public Map<String, Long> getModuleCountMap(String projectId, List<ModuleCountDTO> moduleCountDTOList) {
//构建模块树然后统计每个节点下的所有数量包含子节点
List<BaseTreeNode> treeNodeList = this.getTreeOnlyIdsAndResourceCount(projectId, moduleCountDTOList);
Map<String, Long> returnMap = new HashMap<>();
List<BaseTreeNode> whileList = new ArrayList<>(treeNodeList);
//构建模块树并计算每个节点下的所有数量包含子节点
List<BaseTreeNode> treeNodeList = this.getTreeOnlyIdsAndResourceCount(projectId, moduleCountDTOList);
//通过广度遍历的方式构建返回值
List<BaseTreeNode> whileList = new ArrayList<>(treeNodeList);
while (CollectionUtils.isNotEmpty(whileList)) {
List<BaseTreeNode> childList = new ArrayList<>();
for (BaseTreeNode treeNode : whileList) {
@ -183,15 +248,6 @@ public class FileModuleService extends ModuleTreeService implements CleanupProje
return returnMap;
}
@Override
public BaseModule getNode(String id) {
FileModule module = fileModuleMapper.selectByPrimaryKey(id);
if (module == null) {
return null;
} else {
return new BaseModule(module.getId(), module.getName(), module.getPos(), module.getProjectId(), module.getParentId());
}
}
@Override
public void updatePos(String id, int pos) {

View File

@ -1,14 +1,15 @@
package io.metersphere.project.service;
import io.metersphere.project.dto.ModuleCountDTO;
import io.metersphere.project.dto.NodeSortDTO;
import io.metersphere.sdk.constants.ModuleConstants;
import io.metersphere.sdk.dto.BaseModule;
import io.metersphere.sdk.dto.BaseTreeNode;
import io.metersphere.sdk.dto.request.NodeMoveRequest;
import io.metersphere.sdk.util.Translator;
import jakarta.validation.constraints.NotNull;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import java.util.ArrayList;
import java.util.HashMap;
@ -26,9 +27,13 @@ public abstract class ModuleTreeService {
}
public List<BaseTreeNode> traverseToBuildTree(List<BaseTreeNode> traverseList, @NotNull List<ModuleCountDTO> moduleCountDTOList, boolean haveVirtualRootNode) {
List<BaseTreeNode> baseTreeNodeList = this.traverseToBuildTree(traverseList, haveVirtualRootNode);
//构建树结构并为每个节点计算资源数量
public List<BaseTreeNode> buildTreeAndCountResource(List<BaseTreeNode> traverseList, @NotNull List<ModuleCountDTO> moduleCountDTOList, boolean haveVirtualRootNode) {
//构建模块树
List<BaseTreeNode> baseTreeNodeList = this.buildTreeAndCountResource(traverseList, haveVirtualRootNode);
//构建模块节点统计的数据结构
Map<String, Integer> resourceCountMap = moduleCountDTOList.stream().collect(Collectors.toMap(ModuleCountDTO::getModuleId, ModuleCountDTO::getDataCount));
//为每个节点赋值资源数量
this.sumModuleResourceCount(baseTreeNodeList, resourceCountMap);
return baseTreeNodeList;
}
@ -38,9 +43,8 @@ public abstract class ModuleTreeService {
*
* @param traverseList 要遍历的节点集合会被清空
* @param haveVirtualRootNode 是否包含虚拟跟节点
* @return
*/
public List<BaseTreeNode> traverseToBuildTree(List<BaseTreeNode> traverseList, boolean haveVirtualRootNode) {
public List<BaseTreeNode> buildTreeAndCountResource(List<BaseTreeNode> traverseList, boolean haveVirtualRootNode) {
List<BaseTreeNode> baseTreeNodeList = new ArrayList<>();
if (haveVirtualRootNode) {
BaseTreeNode defaultNode = this.getDefaultModule();
@ -78,13 +82,14 @@ public abstract class ModuleTreeService {
/**
* 模块树排序
*/
public void sort(NodeMoveRequest nodeMoveRequest) {
public void sort(@Validated NodeSortDTO nodeMoveDTO) {
// 获取相邻节点
BaseModule previousNode = getNode(nodeMoveRequest.getPreviousNodeId());
BaseModule nextNode = getNode(nodeMoveRequest.getNextNodeId());
BaseModule previousNode = nodeMoveDTO.getPreviousNode();
BaseModule nextNode = nodeMoveDTO.getNextNode();
if (previousNode == null && nextNode == null) {
// 没有相邻节点pos为0
updatePos(nodeMoveRequest.getNodeId(), 0);
updatePos(nodeMoveDTO.getNode().getId(), 0);
} else {
boolean refreshPos = false;
int pos;
@ -103,24 +108,19 @@ public abstract class ModuleTreeService {
pos = previousNode.getPos() + quantityDifference;
}
updatePos(nodeMoveRequest.getNodeId(), pos);
updatePos(nodeMoveDTO.getNode().getId(), pos);
if (refreshPos) {
refreshPos(nodeMoveRequest.getParentId());
refreshPos(nodeMoveDTO.getParent().getId());
}
}
}
public abstract BaseModule getNode(String id);
public abstract void updatePos(String id, int pos);
public abstract void refreshPos(String parentId);
/**
* 通过深度遍历的方式将资源数量复制到节点上并将子节点上的资源数量统计到父节点上
*
* @param baseTreeNodeList
* @param resourceCountMap
* 通过深度遍历的方式在为节点赋值资源统计数量的同时同步计算其子节点的资源数量并添加到父节点上
*/
private void sumModuleResourceCount(List<BaseTreeNode> baseTreeNodeList, Map<String, Integer> resourceCountMap) {
for (BaseTreeNode node : baseTreeNodeList) {

View File

@ -114,6 +114,17 @@ public class FileManagementControllerTests extends BaseTest {
Assertions.assertEquals(result.getCurrent(), request.getCurrent());
//返回的数据量不超过规定要返回的数据量相同
Assertions.assertTrue(JSON.parseArray(JSON.toJSONString(result.getList())).size() <= request.getPageSize());
//此时该接口数量应该为空
List<String> fileTypes = this.getFileType();
Assertions.assertTrue(fileTypes.isEmpty());
}
private List<String> getFileType() throws Exception {
MvcResult fileTypeResult = this.requestGetWithOkAndReturn(String.format(FileManagementRequestUtils.URL_FILE_TYPE, project.getId()));
String returnData = fileTypeResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class);
return JSON.parseArray(JSON.toJSONString(resultHolder.getData()), String.class);
}
@Test
@ -362,6 +373,8 @@ public class FileManagementControllerTests extends BaseTest {
public void fileUploadTestSuccess() throws Exception {
this.preliminaryData();
List<String> uploadedFileTypes = new ArrayList<>();
FileUploadRequest fileUploadRequest = new FileUploadRequest();
fileUploadRequest.setProjectId(project.getId());
@ -375,6 +388,14 @@ public class FileManagementControllerTests extends BaseTest {
String returnId = JSON.parseObject(mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8), ResultHolder.class).getData().toString();
checkLog(returnId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_UPLOAD);
FILE_ID_PATH.put(returnId, filePath);
uploadedFileTypes.add("JPG");
//检查文件类型获取接口有没有获取到数据
List<String> fileTypes = this.getFileType();
Assertions.assertEquals(fileTypes.size(), uploadedFileTypes.size());
for (String fileType : fileTypes) {
Assertions.assertTrue(uploadedFileTypes.contains(fileType));
}
//文件上传到a1-a1节点
BaseTreeNode a1a1Node = FileManagementBaseUtils.getNodeByName(preliminaryTreeNodes, "a1-a1");
@ -382,7 +403,7 @@ public class FileManagementControllerTests extends BaseTest {
fileUploadRequest.setProjectId(project.getId());
fileUploadRequest.setModuleId(a1a1Node.getId());
filePath = Objects.requireNonNull(this.getClass().getClassLoader().getResource("file/txtFile.txt")).getPath();
file = new MockMultipartFile("file", "txtFile.JPG", MediaType.APPLICATION_OCTET_STREAM_VALUE, FileManagementBaseUtils.getFileBytes(filePath));
file = new MockMultipartFile("file", "txtFile.txt", MediaType.APPLICATION_OCTET_STREAM_VALUE, FileManagementBaseUtils.getFileBytes(filePath));
paramMap = new LinkedMultiValueMap<>();
paramMap.add("file", file);
paramMap.add("request", JSON.toJSONString(fileUploadRequest));
@ -390,6 +411,15 @@ public class FileManagementControllerTests extends BaseTest {
returnId = JSON.parseObject(mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8), ResultHolder.class).getData().toString();
checkLog(returnId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_UPLOAD);
FILE_ID_PATH.put(returnId, filePath);
uploadedFileTypes.add("txt");
//检查文件类型获取接口有没有获取到数据
fileTypes = this.getFileType();
Assertions.assertEquals(fileTypes.size(), uploadedFileTypes.size());
for (String fileType : fileTypes) {
Assertions.assertTrue(uploadedFileTypes.contains(fileType));
}
//没后缀的文件 (同时上传到a1-a1节点)
filePath = Objects.requireNonNull(this.getClass().getClassLoader().getResource("file/noSuffixFile")).getPath();
file = new MockMultipartFile("file", "noSuffixFile", MediaType.APPLICATION_OCTET_STREAM_VALUE, FileManagementBaseUtils.getFileBytes(filePath));
@ -400,6 +430,14 @@ public class FileManagementControllerTests extends BaseTest {
returnId = JSON.parseObject(mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8), ResultHolder.class).getData().toString();
checkLog(returnId, OperationLogType.ADD, FileManagementRequestUtils.URL_FILE_UPLOAD);
FILE_ID_PATH.put(returnId, filePath);
uploadedFileTypes.add(StringUtils.EMPTY);
//检查文件类型获取接口有没有获取到数据
fileTypes = this.getFileType();
Assertions.assertEquals(fileTypes.size(), uploadedFileTypes.size());
for (String fileType : fileTypes) {
Assertions.assertTrue(uploadedFileTypes.contains(fileType));
}
}
@Test
@ -888,18 +926,18 @@ public class FileManagementControllerTests extends BaseTest {
//父节点内移动-移动到首位 a1挪到a3后面
NodeMoveRequest request = new NodeMoveRequest();
{
request.setNodeId(a1Node.getId());
request.setPreviousNodeId(a3Node.getId());
request.setParentId(ModuleConstants.ROOT_NODE_PARENT_ID);
request.setDragNodeId(a1Node.getId());
request.setDropNodeId(a3Node.getId());
request.setDropPosition(1);
this.requestPostWithOk(FileManagementRequestUtils.URL_MODULE_MOVE, request);
this.checkModulePos(a3Node.getId(), a1Node.getId(), null, false);
}
//父节点内移动-移动到末位 在上面的基础上a1挪到a2上面
{
request = new NodeMoveRequest();
request.setNodeId(a1Node.getId());
request.setNextNodeId(a2Node.getId());
request.setParentId(ModuleConstants.ROOT_NODE_PARENT_ID);
request.setDragNodeId(a1Node.getId());
request.setDropNodeId(a2Node.getId());
request.setDropPosition(-1);
this.requestPostWithOk(FileManagementRequestUtils.URL_MODULE_MOVE, request);
this.checkModulePos(a1Node.getId(), a2Node.getId(), null, false);
}
@ -907,17 +945,16 @@ public class FileManagementControllerTests extends BaseTest {
//父节点内移动-移动到中位 a1移动到a2-a3中间
{
request = new NodeMoveRequest();
request.setNodeId(a1Node.getId());
request.setPreviousNodeId(a2Node.getId());
request.setNextNodeId(a3Node.getId());
request.setParentId(ModuleConstants.ROOT_NODE_PARENT_ID);
request.setDragNodeId(a1Node.getId());
request.setDropNodeId(a2Node.getId());
request.setDropPosition(1);
this.requestPostWithOk(FileManagementRequestUtils.URL_MODULE_MOVE, request);
this.checkModulePos(a2Node.getId(), a1Node.getId(), a3Node.getId(), false);
//移动回去
request = new NodeMoveRequest();
request.setNodeId(a1Node.getId());
request.setNextNodeId(a2Node.getId());
request.setParentId(ModuleConstants.ROOT_NODE_PARENT_ID);
request.setDragNodeId(a1Node.getId());
request.setDropNodeId(a2Node.getId());
request.setDropPosition(-1);
this.requestPostWithOk(FileManagementRequestUtils.URL_MODULE_MOVE, request);
this.checkModulePos(a1Node.getId(), a2Node.getId(), null, false);
}
@ -925,16 +962,16 @@ public class FileManagementControllerTests extends BaseTest {
//跨节点移动-移动到首位 a3移动到a1-b1前面然后移动回来;
{
request = new NodeMoveRequest();
request.setNodeId(a3Node.getId());
request.setNextNodeId(a1b1Node.getId());
request.setParentId(a1Node.getId());
request.setDragNodeId(a3Node.getId());
request.setDropNodeId(a1b1Node.getId());
request.setDropPosition(-1);
this.requestPostWithOk(FileManagementRequestUtils.URL_MODULE_MOVE, request);
this.checkModulePos(a3Node.getId(), a1b1Node.getId(), null, false);
//移动回去
request = new NodeMoveRequest();
request.setNodeId(a3Node.getId());
request.setPreviousNodeId(a2Node.getId());
request.setParentId(ModuleConstants.ROOT_NODE_PARENT_ID);
request.setDragNodeId(a3Node.getId());
request.setDropNodeId(a2Node.getId());
request.setDropPosition(1);
this.requestPostWithOk(FileManagementRequestUtils.URL_MODULE_MOVE, request);
this.checkModulePos(a2Node.getId(), a3Node.getId(), null, false);
}
@ -942,16 +979,16 @@ public class FileManagementControllerTests extends BaseTest {
//跨节点移动-移动到末尾 a3移动到a1-a1后面然后移动回来;
{
request = new NodeMoveRequest();
request.setNodeId(a3Node.getId());
request.setPreviousNodeId(a1a1Node.getId());
request.setParentId(a1Node.getId());
request.setDragNodeId(a3Node.getId());
request.setDropNodeId(a1a1Node.getId());
request.setDropPosition(1);
this.requestPostWithOk(FileManagementRequestUtils.URL_MODULE_MOVE, request);
this.checkModulePos(a1a1Node.getId(), a3Node.getId(), null, false);
//移动回去
request = new NodeMoveRequest();
request.setNodeId(a3Node.getId());
request.setPreviousNodeId(a2Node.getId());
request.setParentId(ModuleConstants.ROOT_NODE_PARENT_ID);
request.setDragNodeId(a3Node.getId());
request.setDropNodeId(a2Node.getId());
request.setDropPosition(1);
this.requestPostWithOk(FileManagementRequestUtils.URL_MODULE_MOVE, request);
this.checkModulePos(a2Node.getId(), a3Node.getId(), null, false);
}
@ -959,17 +996,16 @@ public class FileManagementControllerTests extends BaseTest {
//跨节点移动-移动到中位 a3移动到a1-b1和a1-a1中间然后移动回来;
{
request = new NodeMoveRequest();
request.setNodeId(a3Node.getId());
request.setPreviousNodeId(a1b1Node.getId());
request.setNextNodeId(a1a1Node.getId());
request.setParentId(a1Node.getId());
request.setDragNodeId(a3Node.getId());
request.setDropNodeId(a1b1Node.getId());
request.setDropPosition(1);
this.requestPostWithOk(FileManagementRequestUtils.URL_MODULE_MOVE, request);
this.checkModulePos(a1b1Node.getId(), a3Node.getId(), a1a1Node.getId(), false);
//移动回去
request = new NodeMoveRequest();
request.setNodeId(a3Node.getId());
request.setPreviousNodeId(a2Node.getId());
request.setParentId(ModuleConstants.ROOT_NODE_PARENT_ID);
request.setDragNodeId(a3Node.getId());
request.setDropNodeId(a2Node.getId());
request.setDropPosition(1);
this.requestPostWithOk(FileManagementRequestUtils.URL_MODULE_MOVE, request);
this.checkModulePos(a2Node.getId(), a3Node.getId(), null, false);
}
@ -984,17 +1020,17 @@ public class FileManagementControllerTests extends BaseTest {
//开始移动
request = new NodeMoveRequest();
request.setNodeId(a3Node.getId());
request.setNextNodeId(a1Node.getId());
request.setParentId(ModuleConstants.ROOT_NODE_PARENT_ID);
request.setDragNodeId(a3Node.getId());
request.setDropNodeId(a1Node.getId());
request.setDropPosition(-1);
this.requestPostWithOk(FileManagementRequestUtils.URL_MODULE_MOVE, request);
this.checkModulePos(a3Node.getId(), a1Node.getId(), null, true);
//移动回去
request = new NodeMoveRequest();
request.setNodeId(a3Node.getId());
request.setPreviousNodeId(a2Node.getId());
request.setParentId(ModuleConstants.ROOT_NODE_PARENT_ID);
request.setDragNodeId(a3Node.getId());
request.setDropNodeId(a2Node.getId());
request.setDropPosition(1);
this.requestPostWithOk(FileManagementRequestUtils.URL_MODULE_MOVE, request);
this.checkModulePos(a2Node.getId(), a3Node.getId(), null, false);
}
@ -1012,18 +1048,17 @@ public class FileManagementControllerTests extends BaseTest {
//开始移动
request = new NodeMoveRequest();
request.setNodeId(a3Node.getId());
request.setPreviousNodeId(a1Node.getId());
request.setNextNodeId(a2Node.getId());
request.setParentId(ModuleConstants.ROOT_NODE_PARENT_ID);
request.setDragNodeId(a3Node.getId());
request.setDropNodeId(a1Node.getId());
request.setDropPosition(1);
this.requestPostWithOk(FileManagementRequestUtils.URL_MODULE_MOVE, request);
this.checkModulePos(a1Node.getId(), a3Node.getId(), a2Node.getId(), true);
//移动回去
request = new NodeMoveRequest();
request.setNodeId(a3Node.getId());
request.setPreviousNodeId(a2Node.getId());
request.setParentId(ModuleConstants.ROOT_NODE_PARENT_ID);
request.setDragNodeId(a3Node.getId());
request.setDropNodeId(a2Node.getId());
request.setDropPosition(1);
this.requestPostWithOk(FileManagementRequestUtils.URL_MODULE_MOVE, request);
this.checkModulePos(a2Node.getId(), a3Node.getId(), null, false);
}
@ -1037,17 +1072,17 @@ public class FileManagementControllerTests extends BaseTest {
//开始移动
request = new NodeMoveRequest();
request.setNodeId(a3Node.getId());
request.setNextNodeId(a1b1Node.getId());
request.setParentId(a1Node.getId());
request.setDragNodeId(a3Node.getId());
request.setDropNodeId(a1b1Node.getId());
request.setDropPosition(-1);
this.requestPostWithOk(FileManagementRequestUtils.URL_MODULE_MOVE, request);
this.checkModulePos(a3Node.getId(), a1b1Node.getId(), null, true);
//移动回去
request = new NodeMoveRequest();
request.setNodeId(a3Node.getId());
request.setPreviousNodeId(a2Node.getId());
request.setParentId(ModuleConstants.ROOT_NODE_PARENT_ID);
request.setDragNodeId(a3Node.getId());
request.setDropNodeId(a2Node.getId());
request.setDropPosition(1);
this.requestPostWithOk(FileManagementRequestUtils.URL_MODULE_MOVE, request);
this.checkModulePos(a2Node.getId(), a3Node.getId(), null, false);
}
@ -1064,18 +1099,17 @@ public class FileManagementControllerTests extends BaseTest {
//开始移动
request = new NodeMoveRequest();
request.setNodeId(a3Node.getId());
request.setPreviousNodeId(a1b1Node.getId());
request.setNextNodeId(a1a1Node.getId());
request.setParentId(a1Node.getId());
request.setDragNodeId(a3Node.getId());
request.setDropNodeId(a1a1Node.getId());
request.setDropPosition(-1);
this.requestPostWithOk(FileManagementRequestUtils.URL_MODULE_MOVE, request);
this.checkModulePos(a1b1Node.getId(), a3Node.getId(), a1a1Node.getId(), true);
//移动回去
request = new NodeMoveRequest();
request.setNodeId(a3Node.getId());
request.setPreviousNodeId(a2Node.getId());
request.setParentId(ModuleConstants.ROOT_NODE_PARENT_ID);
request.setDragNodeId(a3Node.getId());
request.setDropNodeId(a2Node.getId());
request.setDropPosition(1);
this.requestPostWithOk(FileManagementRequestUtils.URL_MODULE_MOVE, request);
this.checkModulePos(a2Node.getId(), a3Node.getId(), null, false);
}
@ -1083,17 +1117,18 @@ public class FileManagementControllerTests extends BaseTest {
{
//开始移动
request = new NodeMoveRequest();
request.setNodeId(a3Node.getId());
request.setParentId(a2Node.getId());
request.setDragNodeId(a3Node.getId());
request.setDropNodeId(a2Node.getId());
request.setDropPosition(0);
this.requestPostWithOk(FileManagementRequestUtils.URL_MODULE_MOVE, request);
FileModule a3Module = fileModuleMapper.selectByPrimaryKey(a3Node.getId());
Assertions.assertEquals(a3Module.getParentId(), a2Node.getId());
//移动回去
request = new NodeMoveRequest();
request.setNodeId(a3Node.getId());
request.setPreviousNodeId(a2Node.getId());
request.setParentId(ModuleConstants.ROOT_NODE_PARENT_ID);
request.setDragNodeId(a3Node.getId());
request.setDropNodeId(a2Node.getId());
request.setDropPosition(1);
this.requestPostWithOk(FileManagementRequestUtils.URL_MODULE_MOVE, request);
this.checkModulePos(a2Node.getId(), a3Node.getId(), null, false);
}
@ -1102,6 +1137,60 @@ public class FileManagementControllerTests extends BaseTest {
checkLog(a3Node.getId(), OperationLogType.UPDATE, FileManagementRequestUtils.URL_MODULE_MOVE);
}
@Test
@Order(81)
public void moveTestError() throws Exception {
this.preliminaryData();
BaseTreeNode a1Node = FileManagementBaseUtils.getNodeByName(preliminaryTreeNodes, "a1");
BaseTreeNode a2Node = FileManagementBaseUtils.getNodeByName(preliminaryTreeNodes, "a2");
//drag节点为空
NodeMoveRequest request = new NodeMoveRequest();
request.setDragNodeId(null);
request.setDropNodeId(a1Node.getId());
request.setDropPosition(1);
this.requestPost(FileManagementRequestUtils.URL_MODULE_MOVE, request).andExpect(status().isBadRequest());
//drag节点不存在
request = new NodeMoveRequest();
request.setDragNodeId(IDGenerator.nextStr());
request.setDropNodeId(a1Node.getId());
request.setDropPosition(1);
this.requestPost(FileManagementRequestUtils.URL_MODULE_MOVE, request).andExpect(status().is5xxServerError());
//drop节点为空
request = new NodeMoveRequest();
request.setDragNodeId(a1Node.getId());
request.setDropNodeId(null);
request.setDropPosition(1);
this.requestPost(FileManagementRequestUtils.URL_MODULE_MOVE, request).andExpect(status().isBadRequest());
//drop节点不存在
request = new NodeMoveRequest();
request.setDragNodeId(a1Node.getId());
request.setDropNodeId(IDGenerator.nextStr());
request.setDropPosition(1);
this.requestPost(FileManagementRequestUtils.URL_MODULE_MOVE, request).andExpect(status().is5xxServerError());
//position为0的时候节点不存在
request = new NodeMoveRequest();
request.setDragNodeId(a1Node.getId());
request.setDropNodeId(IDGenerator.nextStr());
request.setDropPosition(0);
this.requestPost(FileManagementRequestUtils.URL_MODULE_MOVE, request).andExpect(status().is5xxServerError());
//dragNode和dropNode一样
request = new NodeMoveRequest();
request.setDragNodeId(a1Node.getId());
request.setDropNodeId(a1Node.getId());
request.setDropPosition(1);
this.requestPost(FileManagementRequestUtils.URL_MODULE_MOVE, request).andExpect(status().is5xxServerError());
//position不是-1 0 1
request = new NodeMoveRequest();
request.setDragNodeId(a1Node.getId());
request.setDropNodeId(a2Node.getId());
request.setDropPosition(4);
this.requestPost(FileManagementRequestUtils.URL_MODULE_MOVE, request).andExpect(status().is5xxServerError());
}
@Test
@Order(90)
public void deleteModuleTestSuccess() throws Exception {
@ -1126,6 +1215,9 @@ public class FileManagementControllerTests extends BaseTest {
//service层判断测试删除空集合
fileModuleService.deleteModule(new ArrayList<>());
//service层判断测试删除项目
fileModuleService.deleteResources(project.getId());
}
private void checkModuleIsEmpty(String id) {

View File

@ -130,8 +130,10 @@ public class FileManagementPermissionControllerTests extends BaseTest {
{
NodeMoveRequest request = new NodeMoveRequest();
request.setNodeId(TEST_ID);
request.setParentId(ModuleConstants.ROOT_NODE_PARENT_ID);
request.setDragNodeId(TEST_ID);
request.setDropNodeId(ModuleConstants.ROOT_NODE_PARENT_ID);
request.setDropPosition(0);
this.requestPostPermissionTest(PermissionConstants.PROJECT_FILE_MANAGEMENT_READ_UPDATE, FileManagementRequestUtils.URL_MODULE_MOVE, request);
}
}

View File

@ -14,6 +14,8 @@ public class FileManagementRequestUtils {
//文件上传
public static final String URL_FILE_UPLOAD = "/project/file/upload";
//获取文件类型
public static final String URL_FILE_TYPE = "/project/file/type/%s";
//文件列表查询
public static final String URL_FILE_PAGE = "/project/file/page";
//文件列表查询对应的模块统计