feat(测试用例): 下载导出文件

This commit is contained in:
WangXu10 2024-08-06 10:54:25 +08:00 committed by Craftsman
parent 8b38fd8878
commit ab326b3d73
3 changed files with 42 additions and 1 deletions

View File

@ -35,6 +35,7 @@ import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotBlank;
import org.apache.shiro.authz.annotation.Logical; import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions; import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@ -272,4 +273,12 @@ public class FunctionalCaseController {
public FunctionalCaseExportColumns getExportColumns(@PathVariable String projectId) { public FunctionalCaseExportColumns getExportColumns(@PathVariable String projectId) {
return functionalCaseFileService.getExportColumns(projectId); return functionalCaseFileService.getExportColumns(projectId);
} }
@GetMapping(value = "/download/file/{projectId}/{fileId}")
@Operation(summary = "用例管理-功能用例-下载文件")
@RequiresPermissions(PermissionConstants.FUNCTIONAL_CASE_READ_EXPORT)
public ResponseEntity<byte[]> downloadImgById(@PathVariable String projectId, @PathVariable String fileId) {
return functionalCaseFileService.downloadFile(projectId, fileId);
}
} }

View File

@ -54,6 +54,9 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.IndexedColors;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -99,6 +102,7 @@ public class FunctionalCaseFileService {
private FunctionalCaseLogService functionalCaseLogService; private FunctionalCaseLogService functionalCaseLogService;
@Resource @Resource
private SystemParameterMapper systemParameterMapper; private SystemParameterMapper systemParameterMapper;
private static final String EXPORT_FILE_NAME = "case_export";
/** /**
* 下载excel导入模板 * 下载excel导入模板
@ -364,7 +368,7 @@ public class FunctionalCaseFileService {
private void uploadFileToMinio(File file, String fileId) { private void uploadFileToMinio(File file, String fileId) {
FileRequest fileRequest = new FileRequest(); FileRequest fileRequest = new FileRequest();
fileRequest.setFileName(file.getName()); fileRequest.setFileName(EXPORT_FILE_NAME);
fileRequest.setFolder(DefaultRepositoryDir.getExportExcelTempDir() + "/" + fileId); fileRequest.setFolder(DefaultRepositoryDir.getExportExcelTempDir() + "/" + fileId);
fileRequest.setStorage(StorageType.MINIO.name()); fileRequest.setStorage(StorageType.MINIO.name());
try { try {
@ -734,4 +738,23 @@ public class FunctionalCaseFileService {
functionalCaseExportColumns.initCustomColumns(headerCustomFields); functionalCaseExportColumns.initCustomColumns(headerCustomFields);
return functionalCaseExportColumns; return functionalCaseExportColumns;
} }
public ResponseEntity<byte[]> downloadFile(String projectId, String fileId) {
Project project = projectMapper.selectByPrimaryKey(projectId);
byte[] bytes;
FileRequest fileRequest = new FileRequest();
fileRequest.setFileName(EXPORT_FILE_NAME);
fileRequest.setFolder(DefaultRepositoryDir.getExportExcelTempDir() + "/" + fileId);
fileRequest.setStorage(StorageType.MINIO.name());
try {
bytes = fileService.download(fileRequest);
} catch (Exception e) {
throw new MSException("get file error");
}
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType("application/octet-stream"))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + "Metersphere_case_" + project.getName() + "\"")
.body(bytes);
}
} }

View File

@ -89,6 +89,7 @@ public class FunctionalCaseControllerTests extends BaseTest {
public static final String EXPORT_EXCEL_URL = "/functional/case/export/excel"; public static final String EXPORT_EXCEL_URL = "/functional/case/export/excel";
public static final String DOWNLOAD_XMIND_TEMPLATE_URL = "/functional/case/download/xmind/template/"; public static final String DOWNLOAD_XMIND_TEMPLATE_URL = "/functional/case/download/xmind/template/";
public static final String EXPORT_COLUMNS_URL = "/functional/case/export/columns/"; public static final String EXPORT_COLUMNS_URL = "/functional/case/export/columns/";
public static final String DOWNLOAD_FILE_URL = "/functional/case/download/file/";
@Resource @Resource
private NotificationMapper notificationMapper; private NotificationMapper notificationMapper;
@ -849,4 +850,12 @@ public class FunctionalCaseControllerTests extends BaseTest {
public void getExportColumns() throws Exception { public void getExportColumns() throws Exception {
this.requestGetExcel(EXPORT_COLUMNS_URL + DEFAULT_PROJECT_ID); this.requestGetExcel(EXPORT_COLUMNS_URL + DEFAULT_PROJECT_ID);
} }
@Test
@Order(24)
public void downloadFile() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get(DOWNLOAD_FILE_URL + DEFAULT_PROJECT_ID + "/" + "123142342")
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken));
}
} }