fix(接口自动化) 场景批量下载jmx打包成一个zip下载

--bug=1006480 --user=赵勇 【github#6012】导出接口场景(jmeter格式)导出场景数量不正确 https://www.tapd.cn/55049933/s/1048981
This commit is contained in:
fit2-zhao 2021-09-22 10:58:28 +08:00 committed by fit2-zhao
parent 605694bd41
commit bccdc59fd2
4 changed files with 72 additions and 20 deletions

View File

@ -320,13 +320,6 @@ public class ApiAutomationController {
return apiAutomationService.export(request);
}
@PostMapping(value = "/export/jmx")
@RequiresPermissions(PermissionConstants.PROJECT_API_SCENARIO_READ_EXPORT_SCENARIO)
@MsAuditLog(module = "api_automation", type = OperLogConstants.EXPORT, sourceId = "#request.id", title = "#request.name", project = "#request.projectId")
public List<ApiScenrioExportJmx> exportJmx(@RequestBody ApiScenarioBatchRequest request) {
return apiAutomationService.exportJmx(request);
}
@GetMapping(value = "/stop/{reportId}")
public void stop(@PathVariable String reportId) {
new LocalRunner().stop(reportId);
@ -342,5 +335,15 @@ public class ApiAutomationController {
return apiAutomationService.setDomain(request.getDefinition());
}
@PostMapping(value = "/export/jmx")
@RequiresPermissions(PermissionConstants.PROJECT_API_SCENARIO_READ_EXPORT_SCENARIO)
@MsAuditLog(module = "api_automation", type = OperLogConstants.EXPORT, sourceId = "#request.id", title = "#request.name", project = "#request.projectId")
public ResponseEntity<byte[]> downloadBodyFiles(@RequestBody ApiScenarioBatchRequest request) {
byte[] bytes = apiAutomationService.exportZip(request);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType("application/octet-stream"))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + "场景JMX文件集.zip")
.body(bytes);
}
}

View File

@ -68,6 +68,7 @@ import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@ -2018,6 +2019,30 @@ public class ApiAutomationService {
return resList;
}
public byte[] exportZip(ApiScenarioBatchRequest request) {
List<ApiScenarioWithBLOBs> apiScenarioWithBLOBs = getExportResult(request);
// 生成jmx
Map<String, byte[]> files = new LinkedHashMap<>();
apiScenarioWithBLOBs.forEach(item -> {
if (StringUtils.isNotEmpty(item.getScenarioDefinition())) {
String jmx = generateJmx(item);
if (StringUtils.isNotEmpty(jmx)) {
ApiScenrioExportJmx scenrioExportJmx = new ApiScenrioExportJmx(item.getName(), apiTestService.updateJmxString(jmx, null, true).getXml());
String fileName = item.getName() + ".jmx";
String jmxStr = scenrioExportJmx.getJmx();
files.put(fileName, jmxStr.getBytes(StandardCharsets.UTF_8));
}
}
});
if (CollectionUtils.isNotEmpty(apiScenarioWithBLOBs)) {
List<String> names = apiScenarioWithBLOBs.stream().map(ApiScenarioWithBLOBs::getName).collect(Collectors.toList());
request.setName(String.join(",", names));
List<String> ids = apiScenarioWithBLOBs.stream().map(ApiScenarioWithBLOBs::getId).collect(Collectors.toList());
request.setId(JSON.toJSONString(ids));
}
return FileUtils.listBytesToZip(files);
}
public void batchUpdateEnv(ApiScenarioBatchRequest request) {
Map<String, String> envMap = request.getEnvMap();
Map<String, List<String>> mapping = request.getMapping();

View File

@ -17,15 +17,35 @@ import org.springframework.core.io.FileSystemResource;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class FileUtils {
public static final String BODY_FILE_DIR = "/opt/metersphere/data/body";
public static final String MD_IMAGE_DIR = "/opt/metersphere/data/image/markdown";
public static byte[] listBytesToZip(Map<String, byte[]> mapReport) {
try {
if (!mapReport.isEmpty()) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
for (Map.Entry<String, byte[]> report : mapReport.entrySet()) {
ZipEntry entry = new ZipEntry(report.getKey());
entry.setSize(report.getValue().length);
zos.putNextEntry(entry);
zos.write(report.getValue());
}
zos.closeEntry();
zos.close();
return baos.toByteArray();
}
} catch (Exception e) {
return new byte[10];
}
return new byte[10];
}
private static void create(List<String> bodyUploadIds, List<MultipartFile> bodyFiles, String path) {
String filePath = BODY_FILE_DIR;
if (StringUtils.isNotEmpty(path)) {

View File

@ -252,6 +252,7 @@ import MsTableColumn from "@/business/components/common/components/table/MsTable
import HeaderLabelOperate from "@/business/components/common/head/HeaderLabelOperate";
import {editApiScenarioCaseOrder} from "@/business/components/api/automation/api-automation";
import {TYPE_TO_C} from "@/business/components/api/automation/scenario/Setting";
import axios from "axios";
export default {
name: "MsApiScenarioList",
@ -1022,6 +1023,17 @@ export default {
}
});
},
fileDownload(url, param) {
axios.post(url, param, {responseType: 'blob'})
.then(response => {
let link = document.createElement("a");
link.href = window.URL.createObjectURL(new Blob([response.data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"}));
link.download = "场景JMX文件集.zip";
this.result.loading = false;
link.click();
});
},
exportJmx() {
let param = {};
this.buildBatchParam(param);
@ -1030,15 +1042,7 @@ export default {
return;
}
this.result.loading = true;
this.result = this.$post("/api/automation/export/jmx", param, response => {
this.result.loading = false;
let obj = response.data;
if (obj && obj.length > 0) {
obj.forEach(item => {
downloadFile(item.name + ".jmx", item.jmx);
});
}
});
this.fileDownload("/api/automation/export/jmx", param);
},
getConditions() {
return this.condition;