This commit is contained in:
chenjianxing 2020-10-20 18:44:14 +08:00
commit e3a90f98ca
15 changed files with 224 additions and 59 deletions

View File

@ -13,10 +13,13 @@ import io.metersphere.notice.service.MailService;
import io.metersphere.notice.service.NoticeService;
import io.metersphere.track.service.TestPlanTestCaseService;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.protocol.HTTP;
import org.apache.jmeter.assertions.AssertionResult;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.visualizers.backend.AbstractBackendListenerClient;
import org.apache.jmeter.visualizers.backend.BackendListenerContext;
import org.pac4j.core.context.HttpConstants;
import org.springframework.http.HttpMethod;
import java.io.Serializable;
import java.util.*;
@ -204,7 +207,13 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
return StringUtils.substringBetween(body, start, end).toUpperCase();
} else {
// Http Method
return StringUtils.substringBefore(body, " ");
String method = StringUtils.substringBefore(body, " ");
for (HttpMethod value : HttpMethod.values()) {
if (StringUtils.equals(method, value.name())) {
return method;
}
}
return "Request";
}
}

View File

@ -167,7 +167,7 @@ public class TestCaseController {
}
@PostMapping("/file/download")
public ResponseEntity<byte[]> downloadJmx(@RequestBody FileOperationRequest fileOperationRequest) {
public ResponseEntity<byte[]> download(@RequestBody FileOperationRequest fileOperationRequest) {
byte[] bytes = fileService.loadFileAsBytes(fileOperationRequest.getId());
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType("application/octet-stream"))
@ -175,4 +175,13 @@ public class TestCaseController {
.body(bytes);
}
@GetMapping("/file/preview/{fileId}")
public ResponseEntity<byte[]> preview(@PathVariable String fileId) {
byte[] bytes = fileService.loadFileAsBytes(fileId);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType("application/octet-stream"))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileId + "\"")
.body(bytes);
}
}

View File

@ -263,25 +263,22 @@ public class TestCaseService {
.map(TestCase::getName)
.collect(Collectors.toSet());
List<ExcelErrData<TestCaseExcelData>> errList = null;
if (multipartFile == null)
if (multipartFile == null) {
MSException.throwException(Translator.get("upload_fail"));
}
if (multipartFile.getOriginalFilename().endsWith(".xmind")) {
try {
errList = new ArrayList<>();
XmindCaseParser xmindParser = new XmindCaseParser(this, userId, projectId, testCaseNames);
String processLog = xmindParser.parse(multipartFile);
if (!StringUtils.isEmpty(processLog)) {
excelResponse.setSuccess(false);
ExcelErrData excelErrData = new ExcelErrData(null, 1, Translator.get("upload_fail") + "" + processLog);
errList.add(excelErrData);
excelResponse.setErrList(errList);
} else if (xmindParser.getNodePaths().isEmpty() && xmindParser.getTestCase().isEmpty()) {
excelResponse.setSuccess(false);
errList = xmindParser.parse(multipartFile);
if (xmindParser.getNodePaths().isEmpty() && xmindParser.getTestCase().isEmpty()) {
if (errList == null) {
errList = new ArrayList<>();
}
ExcelErrData excelErrData = new ExcelErrData(null, 1, Translator.get("upload_fail") + "" + Translator.get("upload_content_is_null"));
errList.add(excelErrData);
excelResponse.setErrList(errList);
} else {
}
if (errList.isEmpty()) {
if (!xmindParser.getNodePaths().isEmpty()) {
testCaseNodeService.createNodes(xmindParser.getNodePaths(), projectId);
}
@ -290,7 +287,6 @@ public class TestCaseService {
this.saveImportData(xmindParser.getTestCase(), projectId);
xmindParser.clear();
}
excelResponse.setSuccess(true);
}
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);

View File

@ -6,12 +6,14 @@ import com.google.common.collect.ImmutableMap;
import io.metersphere.base.domain.TestCaseWithBLOBs;
import io.metersphere.commons.constants.TestCaseConstants;
import io.metersphere.commons.utils.BeanUtils;
import io.metersphere.excel.domain.ExcelErrData;
import io.metersphere.excel.domain.TestCaseExcelData;
import io.metersphere.i18n.Translator;
import io.metersphere.track.service.TestCaseService;
import io.metersphere.xmind.parser.XmindParser;
import io.metersphere.xmind.parser.pojo.Attached;
import io.metersphere.xmind.parser.pojo.JsonRootBean;
import io.metersphere.xmind.utils.DetailUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;
@ -32,7 +34,7 @@ public class XmindCaseParser {
/**
* 过程校验记录
*/
private StringBuffer process;
private DetailUtil process;
/**
* 已存在用例名称
*/
@ -57,7 +59,7 @@ public class XmindCaseParser {
this.testCaseNames = testCaseNames;
testCases = new LinkedList<>();
compartDatas = new ArrayList<>();
process = new StringBuffer();
process = new DetailUtil();
nodePaths = new ArrayList<>();
}
@ -90,15 +92,15 @@ public class XmindCaseParser {
nodePaths.forEach(nodePath -> {
String[] nodes = nodePath.split("/");
if (nodes.length > TestCaseConstants.MAX_NODE_DEPTH + 1) {
process.append(Translator.get("test_case_node_level_tip") +
TestCaseConstants.MAX_NODE_DEPTH + Translator.get("test_case_node_level") + "; ");
process.add(Translator.get("test_case_node_level_tip") +
TestCaseConstants.MAX_NODE_DEPTH + Translator.get("test_case_node_level"), nodePath);
}
String path = "";
for (int i = 0; i < nodes.length; i++) {
if (i != 0 && StringUtils.equals(nodes[i].trim(), "")) {
process.append(Translator.get("module") + ":【" + path + "" + Translator.get("module_not_null") + "; ");
process.add(Translator.get("module_not_null"), path);
} else if (nodes[i].trim().length() > 30) {
process.append(nodes[i].trim() + "" + Translator.get("test_track.length_less_than") + "30 ;");
process.add(Translator.get("module") + Translator.get("test_track.length_less_than") + "30", path + nodes[i].trim());
} else {
path += nodes[i].trim() + "/";
}
@ -109,7 +111,7 @@ public class XmindCaseParser {
/**
* 验证用例的合规性
*/
private boolean validate(TestCaseWithBLOBs data) {
private void validate(TestCaseWithBLOBs data) {
String nodePath = data.getNodePath();
if (!nodePath.startsWith("/")) {
nodePath = "/" + nodePath;
@ -119,37 +121,36 @@ public class XmindCaseParser {
}
data.setNodePath(nodePath);
StringBuilder stringBuilder = new StringBuilder();
if (data.getName().length() > 50) {
stringBuilder.append(data.getName() + "" + Translator.get("test_case") + Translator.get("test_track.length_less_than") + "50 ;");
process.add(Translator.get("test_case") + Translator.get("test_track.length_less_than") + "50", nodePath + data.getName());
}
if (!StringUtils.isEmpty(nodePath)) {
String[] nodes = nodePath.split("/");
if (nodes.length > TestCaseConstants.MAX_NODE_DEPTH + 1) {
stringBuilder.append(Translator.get("test_case_node_level_tip") +
TestCaseConstants.MAX_NODE_DEPTH + Translator.get("test_case_node_level") + "; ");
process.add(Translator.get("test_case_node_level_tip") +
TestCaseConstants.MAX_NODE_DEPTH + Translator.get("test_case_node_level"), nodePath);
}
for (int i = 0; i < nodes.length; i++) {
if (i != 0 && StringUtils.equals(nodes[i].trim(), "")) {
stringBuilder.append(Translator.get("test_case") + ":【" + data.getName() + "" + Translator.get("module_not_null") + "; ");
process.add(Translator.get("test_case") + Translator.get("module_not_null"), nodePath + data.getName());
break;
} else if (nodes[i].trim().length() > 30) {
stringBuilder.append(nodes[i].trim() + "" + Translator.get("module") + Translator.get("test_track.length_less_than") + "30 ;");
process.add(Translator.get("module") + Translator.get("test_track.length_less_than") + "30 ", nodes[i].trim());
break;
}
}
}
if (StringUtils.equals(data.getType(), TestCaseConstants.Type.Functional.getValue()) && StringUtils.equals(data.getMethod(), TestCaseConstants.Method.Auto.getValue())) {
stringBuilder.append(Translator.get("functional_method_tip") + "; ");
process.add(Translator.get("functional_method_tip"), nodePath + data.getName());
}
if (testCaseNames.contains(data.getName())) {
boolean dbExist = testCaseService.exist(data);
if (dbExist) {
stringBuilder.append(Translator.get("test_case_already_exists_excel") + "" + data.getName() + "; ");
process.add(Translator.get("test_case_already_exists_excel"), nodePath + "/" + data.getName());
}
} else {
testCaseNames.add(data.getName());
@ -157,26 +158,19 @@ public class XmindCaseParser {
// 用例等级和用例性质处理
if (!priorityList.contains(data.getPriority())) {
stringBuilder.append(data.getName() + "" + Translator.get("test_case_priority") + Translator.get("incorrect_format") + "; ");
process.add(Translator.get("test_case_priority") + Translator.get("incorrect_format"), nodePath + data.getName());
}
if (data.getType() == null) {
stringBuilder.append(data.getName() + "" + Translator.get("test_case_type") + Translator.get("incorrect_format") + "; ");
process.add(Translator.get("test_case_type") + Translator.get("incorrect_format"), nodePath + data.getName());
}
// 重复用例校验
TestCaseExcelData compartData = new TestCaseExcelData();
BeanUtils.copyBean(compartData, data);
if (compartDatas.contains(compartData)) {
stringBuilder.append(Translator.get("test_case_already_exists_excel") + "" + compartData.getName() + "; ");
}
if (!StringUtils.isEmpty(stringBuilder.toString())) {
process.append(stringBuilder.toString());
return false;
process.add(Translator.get("test_case_already_exists_excel"), nodePath + "/" + compartData.getName());
}
compartDatas.add(compartData);
return true;
}
/**
@ -267,7 +261,7 @@ public class XmindCaseParser {
String tc = title.replace("", ":");
String[] tcArr = tc.split(":");
if (tcArr.length != 2) {
process.append(Translator.get("test_case_name") + "" + title + "" + Translator.get("incorrect_format"));
process.add(Translator.get("test_case_name") + Translator.get("incorrect_format"), title);
return;
}
// 用例名称
@ -301,17 +295,15 @@ public class XmindCaseParser {
});
}
testCase.setSteps(this.getSteps(steps));
testCases.add(testCase);
// 校验合规性
if (validate(testCase)) {
testCases.add(testCase);
}
validate(testCase);
}
/**
* 导入思维导图处理
*/
public String parse(MultipartFile multipartFile) {
public List<ExcelErrData<TestCaseExcelData>> parse(MultipartFile multipartFile) {
try {
// 获取思维导图内容
List<JsonRootBean> roots = XmindParser.parseObject(multipartFile);
@ -321,7 +313,7 @@ public class XmindCaseParser {
for (Attached item : root.getRootTopic().getChildren().getAttached()) {
// 用例
if (isAvailable(item.getTitle(), TC_REGEX)) {
return replace(item.getTitle(), TC_REGEX) + "" + Translator.get("test_case_create_module_fail");
return process.parse(replace(item.getTitle(), TC_REGEX) + "" + Translator.get("test_case_create_module_fail"));
} else {
String nodePath = item.getTitle();
item.setPath(nodePath);
@ -344,8 +336,8 @@ public class XmindCaseParser {
//检查目录合规性
this.validate();
} catch (Exception ex) {
return ex.getMessage();
return process.parse(ex.getMessage());
}
return process.toString();
return process.parse();
}
}

View File

@ -73,7 +73,7 @@ public class XmindParser {
JsonRootBean jsonRootBean = JSON.parseObject(content, JsonRootBean.class);
jsonRootBeans.add(jsonRootBean);
}
if (caseCount > 500) {
if (caseCount > 800) {
MSException.throwException(Translator.get("import_xmind_count_error"));
}
}

View File

@ -0,0 +1,59 @@
package io.metersphere.xmind.utils;
import io.metersphere.excel.domain.ExcelErrData;
import io.metersphere.excel.domain.TestCaseExcelData;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@Getter
@Setter
public class DetailUtil implements Serializable {
private Map<String, StringBuilder> process;
public DetailUtil() {
process = new LinkedHashMap<>();
}
public void add(String type, String msContent) {
if (process.containsKey(type)) {
process.get(type).append(msContent + "");
} else {
process.put(type, new StringBuilder(msContent + ""));
}
}
public List<ExcelErrData<TestCaseExcelData>> parse(String content) {
List<ExcelErrData<TestCaseExcelData>> errList = new ArrayList<>();
ExcelErrData excelErrData = new ExcelErrData(null, 1, content);
errList.add(excelErrData);
return errList;
}
public List<ExcelErrData<TestCaseExcelData>> parse() {
List<ExcelErrData<TestCaseExcelData>> errList = new ArrayList<>();
List<String> result = new ArrayList<>();
process.entrySet().parallelStream().reduce(result, (first, second) -> {
first.add(second.getKey() + "" + second.getValue());
return first;
}, (first, second) -> {
if (first == second) {
return first;
}
first.addAll(second);
return first;
});
for (int i = 0; i < result.size(); i++) {
ExcelErrData excelErrData = new ExcelErrData(null, i, result.get(i));
errList.add(excelErrData);
}
return errList;
}
}

@ -1 +1 @@
Subproject commit cf6b06526324326a563d933e07118fac014a63b4
Subproject commit ee74568be0beba46da19616f5832e83f9164c688

View File

@ -35,7 +35,8 @@
"jspdf": "^2.1.1",
"yan-progress": "^1.0.3",
"nprogress": "^0.2.0",
"el-table-infinite-scroll": "^1.0.10"
"el-table-infinite-scroll": "^1.0.10",
"vue-pdf": "^4.2.0"
},
"devDependencies": {
"@vue/cli-plugin-babel": "^4.1.0",

View File

@ -252,6 +252,10 @@
<el-table-column
:label="$t('commons.operating')">
<template v-slot:default="scope">
<el-button @click="preview(scope.row)" :disabled="!scope.row.id || readOnly" type="primary"
v-if="isPreview(scope.row)"
icon="el-icon-view"
size="mini" circle/>
<el-button @click="handleDownload(scope.row)" :disabled="!scope.row.id || readOnly" type="primary"
icon="el-icon-download"
size="mini" circle/>
@ -277,6 +281,7 @@
</el-dialog>
<test-case-file ref="testCaseFile"/>
</div>
@ -289,10 +294,11 @@ import MsDialogFooter from '../../../common/components/MsDialogFooter'
import {listenGoBack, removeGoBackListener} from "../../../../../common/js/utils";
import {LIST_CHANGE, TrackEvent} from "@/business/components/common/head/ListEvent";
import {Message} from "element-ui";
import TestCaseFile from "@/business/components/track/case/components/TestCaseFile";
export default {
name: "TestCaseEdit",
components: {MsDialogFooter},
components: {MsDialogFooter, TestCaseFile},
data() {
return {
result: {},
@ -718,7 +724,13 @@ export default {
/// todo:
return file.size > 0;
},
preview(row) {
this.$refs.testCaseFile.open(row);
},
isPreview(row) {
const fileType = row.type;
return fileType === 'JPG' || fileType === 'JPEG' || fileType === 'PDF' || fileType === 'PNG';
}
}
}
</script>

View File

@ -0,0 +1,48 @@
<template>
<el-dialog :visible.sync="dialogVisible" width="80%" :destroy-on-close="true" :before-close="close">
<div>
<img :src="'/test/case/file/preview/' + file.id" alt="图片显示异常" style="width: 100%;height: 100%;"
v-if="file.type === 'JPG' || file.type === 'JPEG' || file.type === 'PNG'">
<div v-if="file.type === 'PDF'">
<test-case-pdf :file-id="file.id"/>
</div>
</div>
</el-dialog>
</template>
<script>
import TestCasePdf from "@/business/components/track/case/components/TestCasePdf";
export default {
name: "TestCaseFiles",
components: {TestCasePdf},
props: {},
data() {
return {
file: {
id: '',
type: ''
},
dialogVisible: false,
}
},
methods: {
open(file) {
this.file = file;
this.dialogVisible = true;
},
close() {
this.file = {
id: '',
type: ''
};
this.dialogVisible = false;
}
}
}
</script>
<style scoped>
</style>

View File

@ -0,0 +1,38 @@
<template>
<div v-loading="loading">
<pdf ref="pdf" v-for="i in numPages" :key="i" :src="loadingTask" :page="i"/>
</div>
</template>
<script>
import pdf from "vue-pdf";
export default {
name: "TestCasePdf",
components: {pdf},
props: {
fileId: String
},
data() {
return {
loading: false,
numPages: null,
loadingTask: null,
}
},
mounted() {
this.loading = true;
this.loadingTask = pdf.createLoadingTask("/test/case/file/preview/" + this.fileId);
this.loadingTask.promise.then(pdf => {
this.numPages = pdf.numPages
this.loading = false;
}).catch(() => {
this.loading = false;
this.$error("pdf 加载失败");
})
}
}
</script>
<style scoped>
</style>

View File

@ -16,7 +16,7 @@
<el-card>
<el-scrollbar>
<el-header>
<el-header style="height: 100%;">
<el-row type="flex" class="head-bar">
@ -27,7 +27,7 @@
</el-button>
</el-col>
<el-col :span="12" class="head-right">
<el-col :span="14" class="head-right">
<el-button plain size="mini" icon="el-icon-arrow-up"
:disabled="index + 1 <= 1"
@ -425,6 +425,7 @@ export default {
.head-right {
text-align: right;
margin-top: 30px;
}
.el-col:not(.test-detail) {

View File

@ -751,7 +751,7 @@ export default {
click_upload: "Upload",
upload_limit: "Only XLS/XLSX/XMIND files can be uploaded, and no more than 20M",
upload_xmind_format: "Upload files can only be .xmind format",
upload_xmind: "Only xmind files can be uploaded, and no more than 500",
upload_xmind: "Only xmind files can be uploaded, and no more than 800",
upload_limit_count: "Only one file can be uploaded at a time",
upload_limit_format: "Upload files can only be XLS, XLSX format!",
upload_limit_size: "Upload file size cannot exceed 20MB!",

View File

@ -754,7 +754,7 @@ export default {
download_template: "下载模版",
click_upload: "点击上传",
upload_limit: "只能上传xls/xlsx文件且不超过20M",
upload_xmind: "支持文件类型:.xmind一次至多导入500 条用例",
upload_xmind: "支持文件类型:.xmind一次至多导入800 条用例",
upload_xmind_format: "上传文件只能是 .xmind 格式",
upload_limit_other_size: "上传文件大小不能超过",
upload_limit_count: "一次只能上传一个文件",

View File

@ -754,7 +754,7 @@ export default {
download_template: "下載模版",
click_upload: "點擊上傳",
upload_limit: "只能上傳xls/xlsx文件且不超過20M",
upload_xmind: "支持文件類型:.xmind壹次至多導入500 條用例",
upload_xmind: "支持文件類型:.xmind壹次至多導入800 條用例",
upload_xmind_format: "上傳文件只能是 .xmind 格式",
upload_limit_other_size: "上傳文件大小不能超過",
upload_limit_count: "壹次只能上傳壹個文件",