提交 592c8117 编写于 作者: Q q4speed

接口测试复制功能

上级 34d2fac5
...@@ -61,6 +61,11 @@ public class APITestController { ...@@ -61,6 +61,11 @@ public class APITestController {
apiTestService.update(request, files); apiTestService.update(request, files);
} }
@PostMapping(value = "/copy")
public void copy(@RequestBody SaveAPITestRequest request) {
apiTestService.copy(request);
}
@GetMapping("/get/{testId}") @GetMapping("/get/{testId}")
public ApiTestWithBLOBs get(@PathVariable String testId) { public ApiTestWithBLOBs get(@PathVariable String testId) {
return apiTestService.get(testId); return apiTestService.get(testId);
......
...@@ -23,6 +23,7 @@ import java.io.ByteArrayInputStream; ...@@ -23,6 +23,7 @@ import java.io.ByteArrayInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.annotation.Resource; import javax.annotation.Resource;
...@@ -69,6 +70,26 @@ public class APITestService { ...@@ -69,6 +70,26 @@ public class APITestService {
saveFile(test.getId(), files); saveFile(test.getId(), files);
} }
public void copy(SaveAPITestRequest request) {
// copy test
ApiTestWithBLOBs copy = get(request.getId());
copy.setId(UUID.randomUUID().toString());
copy.setName(copy.getName() + " Copy");
copy.setCreateTime(System.currentTimeMillis());
copy.setUpdateTime(System.currentTimeMillis());
copy.setStatus(APITestStatus.Saved.name());
copy.setUserId(Objects.requireNonNull(SessionUtils.getUser()).getId());
apiTestMapper.insert(copy);
// copy test file
ApiTestFile apiTestFile = getFileByTestId(request.getId());
if (apiTestFile != null) {
FileMetadata fileMetadata = fileService.copyFile(apiTestFile.getFileId());
apiTestFile.setTestId(copy.getId());
apiTestFile.setFileId(fileMetadata.getId());
apiTestFileMapper.insert(apiTestFile);
}
}
public ApiTestWithBLOBs get(String id) { public ApiTestWithBLOBs get(String id) {
return apiTestMapper.selectByPrimaryKey(id); return apiTestMapper.selectByPrimaryKey(id);
} }
......
...@@ -3,22 +3,20 @@ package io.metersphere.service; ...@@ -3,22 +3,20 @@ package io.metersphere.service;
import io.metersphere.base.domain.*; import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.FileContentMapper; import io.metersphere.base.mapper.FileContentMapper;
import io.metersphere.base.mapper.FileMetadataMapper; import io.metersphere.base.mapper.FileMetadataMapper;
import io.metersphere.base.mapper.ApiTestFileMapper;
import io.metersphere.base.mapper.LoadTestFileMapper; import io.metersphere.base.mapper.LoadTestFileMapper;
import io.metersphere.commons.constants.FileType; import io.metersphere.commons.constants.FileType;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.annotation.Resource;
@Service @Service
public class FileService { public class FileService {
@Resource @Resource
...@@ -26,8 +24,6 @@ public class FileService { ...@@ -26,8 +24,6 @@ public class FileService {
@Resource @Resource
private LoadTestFileMapper loadTestFileMapper; private LoadTestFileMapper loadTestFileMapper;
@Resource @Resource
private ApiTestFileMapper ApiTestFileMapper;
@Resource
private FileContentMapper fileContentMapper; private FileContentMapper fileContentMapper;
public byte[] loadFileAsBytes(String id) { public byte[] loadFileAsBytes(String id) {
...@@ -50,17 +46,6 @@ public class FileService { ...@@ -50,17 +46,6 @@ public class FileService {
return fileMetadataMapper.selectByExample(example); return fileMetadataMapper.selectByExample(example);
} }
public FileMetadata getApiFileMetadataByTestId(String testId) {
ApiTestFileExample ApiTestFileExample = new ApiTestFileExample();
ApiTestFileExample.createCriteria().andTestIdEqualTo(testId);
final List<ApiTestFile> loadTestFiles = ApiTestFileMapper.selectByExample(ApiTestFileExample);
if (CollectionUtils.isEmpty(loadTestFiles)) {
return null;
}
return fileMetadataMapper.selectByPrimaryKey(loadTestFiles.get(0).getFileId());
}
public FileContent getFileContent(String fileId) { public FileContent getFileContent(String fileId) {
return fileContentMapper.selectByPrimaryKey(fileId); return fileContentMapper.selectByPrimaryKey(fileId);
} }
...@@ -101,6 +86,21 @@ public class FileService { ...@@ -101,6 +86,21 @@ public class FileService {
return fileMetadata; return fileMetadata;
} }
public FileMetadata copyFile(String fileId) {
FileMetadata fileMetadata = fileMetadataMapper.selectByPrimaryKey(fileId);
FileContent fileContent = getFileContent(fileId);
if (fileMetadata != null && fileContent != null) {
fileMetadata.setId(UUID.randomUUID().toString());
fileMetadata.setCreateTime(System.currentTimeMillis());
fileMetadata.setUpdateTime(System.currentTimeMillis());
fileMetadataMapper.insert(fileMetadata);
fileContent.setFileId(fileMetadata.getId());
fileContentMapper.insert(fileContent);
}
return fileMetadata;
}
private FileType getFileType(String filename) { private FileType getFileType(String filename) {
int s = filename.lastIndexOf(".") + 1; int s = filename.lastIndexOf(".") + 1;
String type = filename.substring(s); String type = filename.substring(s);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册