refactor(接口测试): 优化mock文件转存

This commit is contained in:
wxg0103 2024-05-06 18:25:25 +08:00 committed by Craftsman
parent ecae5a4d99
commit 8980be4af5
16 changed files with 109 additions and 54 deletions

View File

@ -44,6 +44,7 @@ public class DefaultRepositoryDir {
* project/{projectId}/apiCase/{apiCaseId}
*/
private static final String PROJECT_API_CASE_DIR = PROJECT_DIR + "/api-case/%s";
private static final String PROJECT_API_MOCK_DIR = PROJECT_DIR + "/api-mock/%s";
private static final String PROJECT_ENV_SSL_DIR = PROJECT_DIR + "/environment/%s";
private static final String PROJECT_FUNCTIONAL_CASE_DIR = PROJECT_DIR + "/functional-case/%s";
private static final String PROJECT_FUNCTIONAL_CASE_PREVIEW_DIR = PROJECT_DIR + "/functional-case/preview/%s";
@ -72,6 +73,10 @@ public class DefaultRepositoryDir {
return String.format(PROJECT_API_CASE_DIR, projectId, apiCaseId);
}
public static String getApiMockDir(String projectId, String apiMockId) {
return String.format(PROJECT_API_MOCK_DIR, projectId, apiMockId);
}
public static String getEnvSslDir(String projectId, String envId) {
return String.format(PROJECT_ENV_SSL_DIR, projectId, envId);
}

View File

@ -1,9 +1,11 @@
package io.metersphere.api.controller.debug;
import io.metersphere.api.constants.ApiResourceType;
import io.metersphere.api.domain.ApiDebug;
import io.metersphere.api.dto.debug.*;
import io.metersphere.api.dto.request.ApiEditPosRequest;
import io.metersphere.api.dto.request.ApiTransferRequest;
import io.metersphere.api.service.ApiFileResourceService;
import io.metersphere.api.service.debug.ApiDebugLogService;
import io.metersphere.api.service.debug.ApiDebugService;
import io.metersphere.project.service.FileModuleService;
@ -38,6 +40,8 @@ public class ApiDebugController {
private ApiDebugService apiDebugService;
@Resource
private FileModuleService fileModuleService;
@Resource
private ApiFileResourceService apiFileResourceService;
@GetMapping("/list/{protocol}")
@Operation(summary = "获取接口调试列表")
@ -65,7 +69,7 @@ public class ApiDebugController {
@Operation(summary = "上传接口调试所需的文件资源并返回文件ID")
@RequiresPermissions(logical = Logical.OR, value = {PermissionConstants.PROJECT_API_DEBUG_ADD, PermissionConstants.PROJECT_API_DEBUG_UPDATE})
public String uploadTempFile(@RequestParam("file") MultipartFile file) {
return apiDebugService.uploadTempFile(file);
return apiFileResourceService.uploadTempFile(file);
}
@PostMapping("/update")
@ -102,15 +106,15 @@ public class ApiDebugController {
@PostMapping("/transfer")
@Operation(summary = "接口测试-接口调试-附件-文件转存")
@RequiresPermissions(PermissionConstants.PROJECT_API_DEBUG_READ)
@CheckOwner(resourceId = "#request.getProjectId()", resourceType = "project")
@RequiresPermissions(PermissionConstants.PROJECT_FILE_MANAGEMENT_READ_ADD)
public String transfer(@Validated @RequestBody ApiTransferRequest request) {
return apiDebugService.transfer(request, SessionUtils.getUserId());
return apiFileResourceService.transfer(request, SessionUtils.getUserId(), ApiResourceType.API_DEBUG.name());
}
@GetMapping("/transfer/options/{projectId}")
@Operation(summary = "接口测试-接口调试-附件-转存目录下拉框")
@RequiresPermissions(PermissionConstants.PROJECT_API_DEBUG_READ)
@RequiresPermissions(PermissionConstants.PROJECT_FILE_MANAGEMENT_READ_ADD)
@CheckOwner(resourceId = "#projectId", resourceType = "project")
public List<BaseTreeNode> options(@PathVariable String projectId) {
return fileModuleService.getTree(projectId);

View File

@ -3,6 +3,7 @@ package io.metersphere.api.controller.definition;
import com.fasterxml.jackson.databind.node.TextNode;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.metersphere.api.constants.ApiResourceType;
import io.metersphere.api.domain.ApiDefinition;
import io.metersphere.api.dto.ReferenceDTO;
import io.metersphere.api.dto.ReferenceRequest;
@ -10,6 +11,7 @@ import io.metersphere.api.dto.definition.*;
import io.metersphere.api.dto.request.ApiEditPosRequest;
import io.metersphere.api.dto.request.ApiTransferRequest;
import io.metersphere.api.dto.request.ImportRequest;
import io.metersphere.api.service.ApiFileResourceService;
import io.metersphere.api.service.definition.ApiDefinitionLogService;
import io.metersphere.api.service.definition.ApiDefinitionNoticeService;
import io.metersphere.api.service.definition.ApiDefinitionService;
@ -54,6 +56,8 @@ public class ApiDefinitionController {
private ApiDefinitionService apiDefinitionService;
@Resource
private FileModuleService fileModuleService;
@Resource
private ApiFileResourceService apiFileResourceService;
@PostMapping(value = "/add")
@Operation(summary = "接口测试-接口管理-添加接口定义")
@ -201,7 +205,7 @@ public class ApiDefinitionController {
@Operation(summary = "上传接口定义所需的文件资源并返回文件ID")
@RequiresPermissions(logical = Logical.OR, value = {PermissionConstants.PROJECT_API_DEFINITION_ADD, PermissionConstants.PROJECT_API_DEFINITION_UPDATE})
public String uploadTempFile(@RequestParam("file") MultipartFile file) {
return apiDefinitionService.uploadTempFile(file);
return apiFileResourceService.uploadTempFile(file);
}
@PostMapping("/doc")
@ -257,7 +261,7 @@ public class ApiDefinitionController {
@GetMapping("/transfer/options/{projectId}")
@Operation(summary = "接口测试-接口管理-接口-附件-转存目录下拉框")
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_READ)
@RequiresPermissions(PermissionConstants.PROJECT_FILE_MANAGEMENT_READ_ADD)
@CheckOwner(resourceId = "#projectId", resourceType = "project")
public List<BaseTreeNode> options(@PathVariable String projectId) {
return fileModuleService.getTree(projectId);
@ -265,10 +269,10 @@ public class ApiDefinitionController {
@PostMapping("/transfer")
@Operation(summary = "接口测试-接口管理-接口-附件-文件转存")
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_READ)
@CheckOwner(resourceId = "#request.getProjectId()", resourceType = "project")
@RequiresPermissions(PermissionConstants.PROJECT_FILE_MANAGEMENT_READ_ADD)
public String transfer(@Validated @RequestBody ApiTransferRequest request) {
return apiDefinitionService.transfer(request, SessionUtils.getUserId());
return apiFileResourceService.transfer(request, SessionUtils.getUserId(), ApiResourceType.API.name());
}
@PostMapping("/preview")

View File

@ -3,18 +3,22 @@ package io.metersphere.api.controller.definition;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import io.metersphere.api.constants.ApiResource;
import io.metersphere.api.constants.ApiResourceType;
import io.metersphere.api.domain.ApiDefinitionMock;
import io.metersphere.api.dto.definition.ApiDefinitionMockDTO;
import io.metersphere.api.dto.definition.request.ApiDefinitionMockAddRequest;
import io.metersphere.api.dto.definition.request.ApiDefinitionMockPageRequest;
import io.metersphere.api.dto.definition.request.ApiDefinitionMockRequest;
import io.metersphere.api.dto.definition.request.ApiDefinitionMockUpdateRequest;
import io.metersphere.api.dto.request.ApiTransferRequest;
import io.metersphere.api.service.ApiFileResourceService;
import io.metersphere.api.service.ApiValidateService;
import io.metersphere.api.service.definition.ApiDefinitionMockLogService;
import io.metersphere.api.service.definition.ApiDefinitionMockNoticeService;
import io.metersphere.api.service.definition.ApiDefinitionMockService;
import io.metersphere.project.service.FileModuleService;
import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.system.dto.sdk.BaseTreeNode;
import io.metersphere.system.log.annotation.Log;
import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.notice.annotation.SendNotice;
@ -50,6 +54,8 @@ public class ApiDefinitionMockController {
private ApiFileResourceService apiFileResourceService;
@Resource
private ApiValidateService apiValidateService;
@Resource
private FileModuleService fileModuleService;
@PostMapping("/page")
@Operation(summary = "接口测试-接口管理-接口 Mock")
@ -130,4 +136,22 @@ public class ApiDefinitionMockController {
public String uploadTempFile(@RequestParam("file") MultipartFile file) {
return apiFileResourceService.uploadTempFile(file);
}
@GetMapping("/transfer/options/{projectId}")
@Operation(summary = "接口测试-接口管理-接口-附件-转存目录下拉框")
@RequiresPermissions(PermissionConstants.PROJECT_FILE_MANAGEMENT_READ_ADD)
@CheckOwner(resourceId = "#projectId", resourceType = "project")
public List<BaseTreeNode> options(@PathVariable String projectId) {
return fileModuleService.getTree(projectId);
}
@PostMapping("/transfer")
@Operation(summary = "接口测试-接口管理-接口-附件-文件转存")
@CheckOwner(resourceId = "#request.getProjectId()", resourceType = "project")
@RequiresPermissions(PermissionConstants.PROJECT_FILE_MANAGEMENT_READ_ADD)
public String transfer(@Validated @RequestBody ApiTransferRequest request) {
return apiFileResourceService.transfer(request, SessionUtils.getUserId(), ApiResourceType.API_MOCK.name());
}
}

View File

@ -2,11 +2,13 @@ package io.metersphere.api.controller.definition;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.metersphere.api.constants.ApiResourceType;
import io.metersphere.api.domain.ApiTestCase;
import io.metersphere.api.dto.ReferenceDTO;
import io.metersphere.api.dto.ReferenceRequest;
import io.metersphere.api.dto.definition.*;
import io.metersphere.api.dto.request.ApiTransferRequest;
import io.metersphere.api.service.ApiFileResourceService;
import io.metersphere.api.service.definition.*;
import io.metersphere.project.service.FileModuleService;
import io.metersphere.sdk.constants.PermissionConstants;
@ -50,6 +52,8 @@ public class ApiTestCaseController {
private FileModuleService fileModuleService;
@Resource
private ApiTestCaseBatchRunService apiTestCaseBatchRunService;
@Resource
private ApiFileResourceService apiFileResourceService;
@PostMapping(value = "/add")
@Operation(summary = "接口测试-接口管理-接口用例-新增")
@ -213,7 +217,7 @@ public class ApiTestCaseController {
@Operation(summary = "上传接口调试所需的文件资源并返回文件ID")
@RequiresPermissions(logical = Logical.OR, value = {PermissionConstants.PROJECT_API_DEFINITION_CASE_ADD, PermissionConstants.PROJECT_API_DEFINITION_CASE_UPDATE})
public String uploadTempFile(@RequestParam("file") MultipartFile file) {
return apiTestCaseService.uploadTempFile(file);
return apiFileResourceService.uploadTempFile(file);
}
@PostMapping("/execute/page")
@ -238,15 +242,15 @@ public class ApiTestCaseController {
@PostMapping("/transfer")
@Operation(summary = "接口测试-接口管理-接口用例-附件-文件转存")
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_READ)
@RequiresPermissions(PermissionConstants.PROJECT_FILE_MANAGEMENT_READ_ADD)
@CheckOwner(resourceId = "#request.getProjectId()", resourceType = "project")
public String transfer(@Validated @RequestBody ApiTransferRequest request) {
return apiTestCaseService.transfer(request, SessionUtils.getUserId());
return apiFileResourceService.transfer(request, SessionUtils.getUserId(), ApiResourceType.API_CASE.name());
}
@GetMapping("/transfer/options/{projectId}")
@Operation(summary = "接口测试-接口管理-接口用例-附件-转存目录下拉框")
@RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_CASE_READ)
@RequiresPermissions(PermissionConstants.PROJECT_FILE_MANAGEMENT_READ_ADD)
@CheckOwner(resourceId = "#projectId", resourceType = "project")
public List<BaseTreeNode> options(@PathVariable String projectId) {
return fileModuleService.getTree(projectId);

View File

@ -3,6 +3,7 @@ package io.metersphere.api.controller.scenario;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.metersphere.api.constants.ApiResource;
import io.metersphere.api.constants.ApiResourceType;
import io.metersphere.api.domain.ApiScenario;
import io.metersphere.api.dto.ReferenceDTO;
import io.metersphere.api.dto.ReferenceRequest;
@ -10,6 +11,7 @@ import io.metersphere.api.dto.definition.ExecutePageRequest;
import io.metersphere.api.dto.definition.ExecuteReportDTO;
import io.metersphere.api.dto.request.ApiTransferRequest;
import io.metersphere.api.dto.scenario.*;
import io.metersphere.api.service.ApiFileResourceService;
import io.metersphere.api.service.ApiValidateService;
import io.metersphere.api.service.scenario.ApiScenarioLogService;
import io.metersphere.api.service.scenario.ApiScenarioNoticeService;
@ -51,6 +53,8 @@ public class ApiScenarioController {
private ApiValidateService apiValidateService;
@Resource
private FileModuleService fileModuleService;
@Resource
private ApiFileResourceService apiFileResourceService;
@PostMapping("/page")
@Operation(summary = "接口测试-接口场景管理-场景列表(deleted 状态为 1 时为回收站数据)")
@ -94,7 +98,7 @@ public class ApiScenarioController {
@Operation(summary = "接口测试-接口场景管理-上传场景所需的文件资源并返回文件ID")
@RequiresPermissions(logical = Logical.OR, value = {PermissionConstants.PROJECT_API_SCENARIO_ADD, PermissionConstants.PROJECT_API_SCENARIO_UPDATE})
public String uploadTempFile(@RequestParam("file") MultipartFile file) {
return apiScenarioService.uploadTempFile(file);
return apiFileResourceService.uploadTempFile(file);
}
@PostMapping("/update")
@ -268,15 +272,15 @@ public class ApiScenarioController {
@PostMapping("/transfer")
@Operation(summary = "接口测试-接口场景管理-场景-附件-文件转存")
@RequiresPermissions(PermissionConstants.PROJECT_API_SCENARIO_READ)
@RequiresPermissions(PermissionConstants.PROJECT_FILE_MANAGEMENT_READ_ADD)
@CheckOwner(resourceId = "#request.getProjectId()", resourceType = "project")
public String transfer(@Validated @RequestBody ApiTransferRequest request) {
return apiScenarioService.transfer(request, SessionUtils.getUserId());
return apiFileResourceService.transfer(request, SessionUtils.getUserId(), ApiResourceType.API_SCENARIO.name());
}
@GetMapping("/transfer/options/{projectId}")
@Operation(summary = "接口测试-接口场景管理-场景-附件-转存目录下拉框")
@RequiresPermissions(PermissionConstants.PROJECT_API_SCENARIO_READ)
@RequiresPermissions(PermissionConstants.PROJECT_FILE_MANAGEMENT_READ_ADD)
@CheckOwner(resourceId = "#projectId", resourceType = "project")
public List<BaseTreeNode> options(@PathVariable String projectId) {
return fileModuleService.getTree(projectId);

View File

@ -392,6 +392,8 @@ public class ApiFileResourceService {
DefaultRepositoryDir.getApiCaseDir(request.getProjectId(), request.getSourceId()) + "/" + request.getFileId();
case API_SCENARIO ->
DefaultRepositoryDir.getApiScenarioDir(request.getProjectId(), request.getSourceId()) + "/" + request.getFileId();
case API_MOCK ->
DefaultRepositoryDir.getApiMockDir(request.getProjectId(), request.getSourceId()) + "/" + request.getFileId();
default -> throw new MSException("file type error!");
};
if (CollectionUtils.isEmpty(apiFileResources)) {

View File

@ -9,7 +9,6 @@ import io.metersphere.api.dto.ApiFile;
import io.metersphere.api.dto.ApiParamConfig;
import io.metersphere.api.dto.debug.*;
import io.metersphere.api.dto.request.ApiEditPosRequest;
import io.metersphere.api.dto.request.ApiTransferRequest;
import io.metersphere.api.mapper.ApiDebugBlobMapper;
import io.metersphere.api.mapper.ApiDebugMapper;
import io.metersphere.api.mapper.ApiDebugModuleMapper;
@ -44,7 +43,6 @@ import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
@ -209,10 +207,6 @@ public class ApiDebugService extends MoveNodeService {
return ServiceUtils.checkResourceExist(apiDebugMapper.selectByPrimaryKey(id), "permission.api_debug.name");
}
public String uploadTempFile(MultipartFile file) {
return apiFileResourceService.uploadTempFile(file);
}
public TaskRequestDTO debug(ApiDebugRunRequest request) {
ApiResourceRunRequest runRequest = apiExecuteService.getApiResourceRunRequest(request);
ApiParamConfig apiParamConfig = apiExecuteService.getApiParamConfig(request.getReportId());
@ -305,8 +299,4 @@ public class ApiDebugService extends MoveNodeService {
apiDebugBlobMapper.updateByPrimaryKeySelective(apiDebugBlob);
}
}
public String transfer(ApiTransferRequest request, String userId) {
return apiFileResourceService.transfer(request, userId, ApiResourceType.API_DEBUG.name());
}
}

View File

@ -141,7 +141,7 @@ public class ApiDefinitionMockService {
}
private static ApiFileResourceUpdateRequest getApiFileResourceRequest(String sourceId, String projectId, String operator) {
String apiDefinitionMockDir = DefaultRepositoryDir.getApiDefinitionDir(projectId, sourceId);
String apiDefinitionMockDir = DefaultRepositoryDir.getApiMockDir(projectId, sourceId);
ApiFileResourceUpdateRequest resourceUpdateRequest = new ApiFileResourceUpdateRequest();
resourceUpdateRequest.setProjectId(projectId);
resourceUpdateRequest.setFolder(apiDefinitionMockDir);
@ -205,7 +205,7 @@ public class ApiDefinitionMockService {
public void delete(ApiDefinitionMockRequest request, String userId) {
checkApiDefinitionMock(request.getId());
String apiDefinitionMockDir = DefaultRepositoryDir.getApiDefinitionDir(request.getProjectId(), request.getId());
String apiDefinitionMockDir = DefaultRepositoryDir.getApiMockDir(request.getProjectId(), request.getId());
apiFileResourceService.deleteByResourceId(apiDefinitionMockDir, request.getId(), request.getProjectId(), userId, OperationLogModule.API_TEST_MANAGEMENT_MOCK);
apiDefinitionMockConfigMapper.deleteByPrimaryKey(request.getId());
apiDefinitionMockMapper.deleteByPrimaryKey(request.getId());
@ -232,8 +232,8 @@ public class ApiDefinitionMockService {
apiDefinitionMockConfigMapper.insertSelective(apiDefinitionMockConfig);
});
String sourceDir = DefaultRepositoryDir.getApiDefinitionDir(apiDefinitionMock.getProjectId(), request.getId());
String targetDir = DefaultRepositoryDir.getApiDefinitionDir(apiDefinitionMock.getProjectId(), apiDefinitionMock.getId());
String sourceDir = DefaultRepositoryDir.getApiMockDir(apiDefinitionMock.getProjectId(), request.getId());
String targetDir = DefaultRepositoryDir.getApiMockDir(apiDefinitionMock.getProjectId(), apiDefinitionMock.getId());
apiFileResourceService.copyFileByResourceId(request.getId(), sourceDir, apiDefinitionMock.getId(), targetDir);
return apiDefinitionMock;
@ -264,7 +264,7 @@ public class ApiDefinitionMockService {
if (!apiDefinitionMocks.isEmpty()) {
List<String> mockIds = apiDefinitionMocks.stream().map(ApiDefinitionMock::getId).toList();
String apiDefinitionMockDir = DefaultRepositoryDir.getApiDefinitionDir(projectId, StringUtils.EMPTY);
String apiDefinitionMockDir = DefaultRepositoryDir.getApiMockDir(projectId, StringUtils.EMPTY);
apiFileResourceService.deleteByResourceIds(apiDefinitionMockDir, mockIds, projectId, userId, OperationLogModule.API_TEST_MANAGEMENT_MOCK);
ApiDefinitionMockConfigExample apiDefinitionMockConfigExample = new ApiDefinitionMockConfigExample();
@ -274,4 +274,5 @@ public class ApiDefinitionMockService {
apiDefinitionMockMapper.deleteByExample(apiDefinitionMockExample);
}
}
}

View File

@ -890,10 +890,6 @@ public class ApiDefinitionService extends MoveNodeService {
}
}
public String uploadTempFile(MultipartFile file) {
return apiFileResourceService.uploadTempFile(file);
}
public ApiDefinitionDTO getApiDefinitionInfo(String id, String userId, ApiDefinition apiDefinition) {
ApiDefinitionDTO apiDefinitionDTO = new ApiDefinitionDTO();
BeanUtils.copyBean(apiDefinitionDTO, apiDefinition);

View File

@ -535,11 +535,6 @@ public class ApiTestCaseService extends MoveNodeService {
mapper.updateByExampleSelective(updateCase, example);
}
public String uploadTempFile(MultipartFile file) {
return apiFileResourceService.uploadTempFile(file);
}
public List<ApiTestCaseBlob> getBlobByIds(List<String> apiCaseIds) {
if (CollectionUtils.isEmpty(apiCaseIds)) {
return Collections.emptyList();

View File

@ -1284,11 +1284,6 @@ public class ApiScenarioService extends MoveNodeService {
private ApiScenario checkResourceExist(String id) {
return ServiceUtils.checkResourceExist(apiScenarioMapper.selectByPrimaryKey(id), "permission.system_api_scenario.name");
}
public String uploadTempFile(MultipartFile file) {
return apiFileResourceService.uploadTempFile(file);
}
public TaskRequestDTO debug(ApiScenarioDebugRequest request) {
ApiScenario apiScenario = apiScenarioMapper.selectByPrimaryKey(request.getId());
boolean hasSave = apiScenario != null;

View File

@ -1,9 +1,6 @@
package io.metersphere.api.controller;
import io.metersphere.api.constants.ApiConstants;
import io.metersphere.api.constants.ApiDefinitionDocType;
import io.metersphere.api.constants.ApiDefinitionStatus;
import io.metersphere.api.constants.ApiImportPlatform;
import io.metersphere.api.constants.*;
import io.metersphere.api.controller.result.ApiResultCode;
import io.metersphere.api.domain.*;
import io.metersphere.api.dto.ApiFile;
@ -210,6 +207,7 @@ public class ApiDefinitionControllerTests extends BaseTest {
@Test
@Order(1)
@Sql(scripts = {"/dml/init_api_definition.sql"}, config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED))
public void testAdd() throws Exception {
LogUtils.info("create api test");
// 创建测试数据
@ -299,6 +297,13 @@ public class ApiDefinitionControllerTests extends BaseTest {
// @@校验日志
checkLogModelList.add(new CheckLogModel(apiDefinition.getId(), OperationLogType.ADD, ADD));
mockMvc.perform(MockMvcRequestBuilders.post(BASE_PATH + "transfer", apiTransferRequest)
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().is5xxServerError())
.andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn();
// @@校验权限
request.setProjectId(DEFAULT_PROJECT_ID);
request.setName("permission-st-6");
@ -385,7 +390,6 @@ public class ApiDefinitionControllerTests extends BaseTest {
@Test
@Order(2)
@Sql(scripts = {"/dml/init_api_definition.sql"}, config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED))
public void get() throws Exception {
if (apiDefinition == null) {
apiDefinition = apiDefinitionMapper.selectByPrimaryKey("1001");

View File

@ -13,6 +13,7 @@ import io.metersphere.api.dto.definition.request.ApiDefinitionMockUpdateRequest;
import io.metersphere.api.dto.mockserver.KeyValueInfo;
import io.metersphere.api.dto.mockserver.MockMatchRule;
import io.metersphere.api.dto.mockserver.MockResponse;
import io.metersphere.api.dto.request.ApiTransferRequest;
import io.metersphere.api.dto.request.http.MsHTTPElement;
import io.metersphere.api.dto.request.http.MsHeader;
import io.metersphere.api.dto.request.http.body.Body;
@ -195,6 +196,14 @@ public class ApiDefinitionMockControllerTests extends BaseTest {
}
}
private static MockMultipartFile getMockMultipartFile(String fileName) {
return new MockMultipartFile(
"file",
fileName,
MediaType.APPLICATION_OCTET_STREAM_VALUE,
"Hello, World!".getBytes()
);
}
@Test
@Order(1)
@ -227,8 +236,23 @@ public class ApiDefinitionMockControllerTests extends BaseTest {
// 校验请求成功数据
ApiDefinitionMock resultData = getResultData(mvcResult, ApiDefinitionMock.class);
apiDefinitionMock = mockServerTestService.assertAddApiDefinitionMock(request, mockMatchRule, resultData.getId());
mockServerTestService.assertUploadFile(apiDefinitionMock.getId(), List.of(uploadFileId));
mockServerTestService.assertLinkFile(apiDefinitionMock.getId(), List.of(fileMetadataId));
MockServerTestService.assertUploadFile(apiDefinitionMock.getId(), List.of(uploadFileId));
MockServerTestService.assertLinkFile(apiDefinitionMock.getId(), List.of(fileMetadataId));
this.requestGetWithOk(BASE_PATH + "transfer/options/" + DEFAULT_PROJECT_ID);
ApiTransferRequest apiTransferRequest = new ApiTransferRequest();
apiTransferRequest.setSourceId(apiDefinitionMock.getId());
apiTransferRequest.setProjectId(DEFAULT_PROJECT_ID);
apiTransferRequest.setModuleId("root");
apiTransferRequest.setLocal(true);
String uploadFileId = doUploadTempFile(getMockMultipartFile("api-mock_upload.JPG"));
apiTransferRequest.setFileId(uploadFileId);
apiTransferRequest.setFileName(org.apache.commons.lang3.StringUtils.EMPTY);
apiTransferRequest.setOriginalName("api-mock_upload.JPG");
this.requestPost(BASE_PATH+ "transfer", apiTransferRequest).andExpect(status().isOk());
//文件不存在
apiTransferRequest.setFileId("111");
this.requestPost(BASE_PATH+ "transfer", apiTransferRequest).andExpect(status().is5xxServerError());
// 再插入一条数据便于修改时重名校验
request.setName("重名接口定义test");

View File

@ -1,6 +1,7 @@
package io.metersphere.api.controller;
import io.metersphere.api.service.ApiExecuteService;
import io.metersphere.api.service.ApiFileResourceService;
import io.metersphere.api.service.debug.ApiDebugService;
import io.metersphere.sdk.constants.DefaultRepositoryDir;
import io.metersphere.sdk.constants.StorageType;
@ -40,6 +41,8 @@ public class ApiExecuteResourceControllerTest extends BaseTest {
private ApiExecuteService apiExecuteService;
@Resource
private ApiDebugService apiDebugService;
@Resource
private ApiFileResourceService apiFileResourceService;
@Override
public String getBasePath() {
@ -55,7 +58,7 @@ public class ApiExecuteResourceControllerTest extends BaseTest {
public void downloadFile() throws Exception {
String fileName = IDGenerator.nextStr() + "_file_upload.JPG";
MockMultipartFile file = new MockMultipartFile("file", fileName, MediaType.APPLICATION_OCTET_STREAM_VALUE, "aa".getBytes());
String fileId = apiDebugService.uploadTempFile(file);
String fileId = apiFileResourceService.uploadTempFile(file);
FileRequest fileRequest = new FileCopyRequest();
fileRequest.setFileName(fileName);
fileRequest.setFolder(DefaultRepositoryDir.getSystemTempDir() + "/" + fileId);

View File

@ -89,7 +89,7 @@ public class MockServerTestService {
List<ApiFileResource> apiFileResources = apiFileResourceService.getByResourceId(id);
Assertions.assertEquals(apiFileResources.size(), fileIds.size());
String apiDefinitionDir = DefaultRepositoryDir.getApiDefinitionDir(DEFAULT_PROJECT_ID, id);
String apiDefinitionDir = DefaultRepositoryDir.getApiMockDir(DEFAULT_PROJECT_ID, id);
FileRequest fileRequest = new FileRequest();
if (!fileIds.isEmpty()) {
for (ApiFileResource apiFileResource : apiFileResources) {