接口测试复制功能
This commit is contained in:
parent
34d2fac553
commit
592c8117bc
|
@ -61,6 +61,11 @@ public class APITestController {
|
|||
apiTestService.update(request, files);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/copy")
|
||||
public void copy(@RequestBody SaveAPITestRequest request) {
|
||||
apiTestService.copy(request);
|
||||
}
|
||||
|
||||
@GetMapping("/get/{testId}")
|
||||
public ApiTestWithBLOBs get(@PathVariable String testId) {
|
||||
return apiTestService.get(testId);
|
||||
|
|
|
@ -23,6 +23,7 @@ import java.io.ByteArrayInputStream;
|
|||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
@ -69,6 +70,26 @@ public class APITestService {
|
|||
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) {
|
||||
return apiTestMapper.selectByPrimaryKey(id);
|
||||
}
|
||||
|
|
|
@ -3,22 +3,20 @@ package io.metersphere.service;
|
|||
import io.metersphere.base.domain.*;
|
||||
import io.metersphere.base.mapper.FileContentMapper;
|
||||
import io.metersphere.base.mapper.FileMetadataMapper;
|
||||
import io.metersphere.base.mapper.ApiTestFileMapper;
|
||||
import io.metersphere.base.mapper.LoadTestFileMapper;
|
||||
import io.metersphere.commons.constants.FileType;
|
||||
import io.metersphere.commons.exception.MSException;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@Service
|
||||
public class FileService {
|
||||
@Resource
|
||||
|
@ -26,8 +24,6 @@ public class FileService {
|
|||
@Resource
|
||||
private LoadTestFileMapper loadTestFileMapper;
|
||||
@Resource
|
||||
private ApiTestFileMapper ApiTestFileMapper;
|
||||
@Resource
|
||||
private FileContentMapper fileContentMapper;
|
||||
|
||||
public byte[] loadFileAsBytes(String id) {
|
||||
|
@ -50,17 +46,6 @@ public class FileService {
|
|||
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) {
|
||||
return fileContentMapper.selectByPrimaryKey(fileId);
|
||||
}
|
||||
|
@ -101,6 +86,21 @@ public class FileService {
|
|||
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) {
|
||||
int s = filename.lastIndexOf(".") + 1;
|
||||
String type = filename.substring(s);
|
||||
|
|
Loading…
Reference in New Issue