fix: postman导入缺失 (#1929)

* feat: 脑图支持给模块打标签,批量操作

* fix: 国际化

* fix: postman导入缺失

* refactor: 代码优化

Co-authored-by: chenjianxing <jianxing.chen@fit2cloud.com>
This commit is contained in:
metersphere-bot 2021-04-07 15:06:49 +08:00 committed by GitHub
parent dbd5711c10
commit 1587d74b53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 227 additions and 46 deletions

View File

@ -9,10 +9,14 @@ import io.metersphere.api.dto.parse.postman.PostmanKeyValue;
import io.metersphere.api.parse.PostmanAbstractParserParser;
import io.metersphere.base.domain.ApiDefinitionWithBLOBs;
import io.metersphere.base.domain.ApiModule;
import io.metersphere.base.domain.ApiTestCaseWithBLOBs;
import io.metersphere.base.domain.Project;
import io.metersphere.base.mapper.ProjectMapper;
import io.metersphere.commons.utils.BeanUtils;
import io.metersphere.commons.utils.CommonBeanFactory;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
public class PostmanDefinitionParser extends PostmanAbstractParserParser<ApiDefinitionImport> {
@ -24,34 +28,48 @@ public class PostmanDefinitionParser extends PostmanAbstractParserParser<ApiDefi
List<PostmanKeyValue> variables = postmanCollection.getVariable();
ApiDefinitionImport apiImport = new ApiDefinitionImport();
List<ApiDefinitionWithBLOBs> results = new ArrayList<>();
List<ApiTestCaseWithBLOBs> cases = new ArrayList<>();
Map<String, String> repeatMap = new HashMap();
ProjectMapper projectMapper = CommonBeanFactory.getBean(ProjectMapper.class);
Project project = projectMapper.selectByPrimaryKey(request.getProjectId());
parseItem(postmanCollection.getItem(), variables, results,
ApiDefinitionImportUtil.buildModule(ApiDefinitionImportUtil.getSelectModule(request.getModuleId()), postmanCollection.getInfo().getName(), this.projectId), true);
ApiDefinitionImportUtil.buildModule(ApiDefinitionImportUtil.getSelectModule(request.getModuleId()),
postmanCollection.getInfo().getName(), this.projectId), cases, repeatMap, project.getRepeatable());
apiImport.setData(results);
apiImport.setCases(cases);
return apiImport;
}
protected void parseItem(List<PostmanItem> items, List<PostmanKeyValue> variables, List<ApiDefinitionWithBLOBs> results, ApiModule parentModule, Boolean isCreateModule) {
protected void parseItem(List<PostmanItem> items, List<PostmanKeyValue> variables, List<ApiDefinitionWithBLOBs> results,
ApiModule parentModule, List<ApiTestCaseWithBLOBs> cases, Map<String, String> repeatMap, Boolean repeatable) {
for (PostmanItem item : items) {
List<PostmanItem> childItems = item.getItem();
if (childItems != null) {
ApiModule module = null;
if (isCreateModule) {
module = ApiDefinitionImportUtil.buildModule(parentModule, item.getName(), this.projectId);
}
parseItem(childItems, variables, results, module, isCreateModule);
parseItem(childItems, variables, results, module, cases, repeatMap, repeatable);
} else {
MsHTTPSamplerProxy msHTTPSamplerProxy = parsePostman(item);
ApiDefinitionWithBLOBs request = buildApiDefinition(msHTTPSamplerProxy.getId(), msHTTPSamplerProxy.getName(),
msHTTPSamplerProxy.getPath(), msHTTPSamplerProxy.getMethod(), new ApiTestImportRequest());
request.setPath(msHTTPSamplerProxy.getPath());
request.setRequest(JSON.toJSONString(msHTTPSamplerProxy));
if (request != null) {
results.add(request);
}
if (parentModule != null) {
request.setModuleId(parentModule.getId());
}
if (request != null) {
if (repeatMap.keySet().contains(request.getMethod() + request.getPath())
&& (repeatable == null || repeatable == false)) {
ApiTestCaseWithBLOBs apiTestCase = new ApiTestCaseWithBLOBs();
BeanUtils.copyBean(apiTestCase, request);
apiTestCase.setApiDefinitionId(repeatMap.get(request.getMethod() + request.getPath()));
apiTestCase.setPriority("P0");
cases.add(apiTestCase);
} else {
repeatMap.put(request.getMethod() + request.getPath(), request.getId());
results.add(request);
}
}
}
}
}

View File

@ -240,6 +240,19 @@ public class ApiDefinitionService {
}
}
private List<ApiDefinition> getSameRequestWithName(SaveApiDefinitionRequest request) {
ApiDefinitionExample example = new ApiDefinitionExample();
example.createCriteria()
.andMethodEqualTo(request.getMethod())
.andStatusNotEqualTo("Trash")
.andPathEqualTo(request.getPath())
.andNameEqualTo(request.getName())
.andProjectIdEqualTo(request.getProjectId())
.andIdNotEqualTo(request.getId());
return apiDefinitionMapper.selectByExample(example);
}
private ApiDefinition updateTest(SaveApiDefinitionRequest request) {
checkNameExist(request);
if (StringUtils.equals(request.getMethod(), "ESB")) {
@ -322,7 +335,8 @@ public class ApiDefinitionService {
}
private ApiDefinition importCreate(ApiDefinitionWithBLOBs apiDefinition, ApiDefinitionMapper batchMapper,
ApiTestCaseMapper apiTestCaseMapper, ApiTestImportRequest apiTestImportRequest, List<ApiTestCaseWithBLOBs> cases) {
ApiTestCaseMapper apiTestCaseMapper, ApiTestImportRequest apiTestImportRequest, List<ApiTestCaseWithBLOBs> cases,
Boolean repeatable) {
SaveApiDefinitionRequest saveReq = new SaveApiDefinitionRequest();
BeanUtils.copyBean(saveReq, apiDefinition);
apiDefinition.setCreateTime(System.currentTimeMillis());
@ -335,7 +349,13 @@ public class ApiDefinitionService {
}
apiDefinition.setDescription(apiDefinition.getDescription());
List<ApiDefinition> sameRequest = getSameRequest(saveReq);
List<ApiDefinition> sameRequest;
if (repeatable == null || repeatable == false) {
sameRequest = getSameRequest(saveReq);
} else {
// 如果勾选了允许重复则判断更新要加上name字段
sameRequest = getSameRequestWithName(saveReq);
}
if (StringUtils.equals("fullCoverage", apiTestImportRequest.getModeId())) {
_importCreate(sameRequest, batchMapper, apiDefinition, apiTestCaseMapper, apiTestImportRequest, cases);
} else if (StringUtils.equals("incrementalMerge", apiTestImportRequest.getModeId())) {
@ -415,15 +435,13 @@ public class ApiDefinitionService {
private void importMsCase(ApiDefinitionImport apiImport, SqlSession sqlSession, ApiTestCaseMapper apiTestCaseMapper) {
List<ApiTestCaseWithBLOBs> cases = apiImport.getCases();
List<String> caseNames = apiTestCaseService.listPorjectAllCaseName(SessionUtils.getCurrentProjectId());
Set<String> existCaseName = new HashSet<>();
caseNames.forEach(item -> {
existCaseName.add(item);
});
SaveApiTestCaseRequest checkRequest = new SaveApiTestCaseRequest();
if (CollectionUtils.isNotEmpty(cases)) {
int batchCount = 0;
cases.forEach(item -> {
if (!existCaseName.contains(item.getName())) {
checkRequest.setName(item.getName());
checkRequest.setApiDefinitionId(item.getApiDefinitionId());
if (!apiTestCaseService.hasSameCase(checkRequest)) {
item.setId(UUID.randomUUID().toString());
item.setCreateTime(System.currentTimeMillis());
item.setUpdateTime(System.currentTimeMillis());
@ -613,6 +631,7 @@ public class ApiDefinitionService {
List<ApiDefinitionWithBLOBs> data = apiImport.getData();
ApiDefinitionMapper batchMapper = sqlSession.getMapper(ApiDefinitionMapper.class);
ApiTestCaseMapper apiTestCaseMapper = sqlSession.getMapper(ApiTestCaseMapper.class);
Project project = projectMapper.selectByPrimaryKey(request.getProjectId());
int num = 0;
if (!CollectionUtils.isEmpty(data) && data.get(0) != null && data.get(0).getProjectId() != null) {
num = getNextNum(data.get(0).getProjectId());
@ -628,14 +647,14 @@ public class ApiDefinitionService {
if (apiImport.getEsbApiParamsMap() != null) {
String apiId = item.getId();
EsbApiParamsWithBLOBs model = apiImport.getEsbApiParamsMap().get(apiId);
importCreate(item, batchMapper, apiTestCaseMapper, request, apiImport.getCases());
importCreate(item, batchMapper, apiTestCaseMapper, request, apiImport.getCases(), project.getRepeatable());
if (model != null) {
apiImport.getEsbApiParamsMap().remove(apiId);
model.setResourceId(item.getId());
apiImport.getEsbApiParamsMap().put(item.getId(), model);
}
} else {
importCreate(item, batchMapper, apiTestCaseMapper, request, apiImport.getCases());
importCreate(item, batchMapper, apiTestCaseMapper, request, apiImport.getCases(), project.getRepeatable());
}
if (i % 300 == 0) {
sqlSession.flushStatements();

View File

@ -81,10 +81,6 @@ public class ApiTestCaseService {
private static final String BODY_FILE_DIR = FileUtils.BODY_FILE_DIR;
public List<String> listPorjectAllCaseName(String projectId) {
return extApiTestCaseMapper.listPorjectAllCaseName(projectId);
}
public List<ApiTestCaseResult> list(ApiTestCaseRequest request) {
request.setOrders(ServiceUtils.getDefaultOrder(request.getOrders()));
List<ApiTestCaseResult> returnList = extApiTestCaseMapper.list(request);
@ -233,14 +229,24 @@ public class ApiTestCaseService {
}
}
private void checkNameExist(SaveApiTestCaseRequest request) {
ApiTestCaseExample example = new ApiTestCaseExample();
example.createCriteria().andNameEqualTo(request.getName()).andApiDefinitionIdEqualTo(request.getApiDefinitionId()).andIdNotEqualTo(request.getId());
if (apiTestCaseMapper.countByExample(example) > 0) {
public void checkNameExist(SaveApiTestCaseRequest request) {
if (hasSameCase(request)) {
MSException.throwException(Translator.get("load_test_already_exists"));
}
}
public Boolean hasSameCase(SaveApiTestCaseRequest request) {
ApiTestCaseExample example = new ApiTestCaseExample();
ApiTestCaseExample.Criteria criteria = example.createCriteria();
criteria.andNameEqualTo(request.getName()).andApiDefinitionIdEqualTo(request.getApiDefinitionId());
if (StringUtils.isNotBlank(request.getId())) {
criteria.andIdNotEqualTo(request.getId());
}
if (apiTestCaseMapper.countByExample(example) > 0) {
return true;
}
return false;
}
private ApiTestCase updateTest(SaveApiTestCaseRequest request) {
checkNameExist(request);

View File

@ -12,8 +12,6 @@ import java.util.List;
public interface ExtApiTestCaseMapper {
List<String> listPorjectAllCaseName(@Param("projectId") String projectId);
List<ApiTestCaseResult> list(@Param("request") ApiTestCaseRequest request);
List<ApiTestCaseDTO> listSimple(@Param("request") ApiTestCaseRequest request);

View File

@ -391,9 +391,6 @@
<select id="getNextNum" resultType="io.metersphere.base.domain.ApiTestCase">
SELECT * FROM api_test_case WHERE api_test_case.api_definition_id = #{definitionId} ORDER BY num DESC LIMIT 1;
</select>
<select id="listPorjectAllCaseName" resultType="java.lang.String">
select name from api_test_case where project_id = #{projectId}
</select>
<select id="selectEffectiveTestCaseByProjectId" resultType="io.metersphere.base.domain.ApiTestCase">
select id,api_definition_id from api_test_case where project_id = #{projectId}
</select>

View File

@ -49,7 +49,7 @@
"vue-float-action-button": "^0.6.6",
"vue-i18n": "^8.15.3",
"vue-jsonpath-picker": "^1.1.5",
"vue-minder-editor-plus": "^1.0.19",
"vue-minder-editor-plus": "^1.0.21",
"vue-papa-parse": "^2.0.0",
"vue-pdf": "^4.2.0",
"vue-router": "^3.1.3",

View File

@ -9,6 +9,7 @@
:tags="tags"
:height="height"
:distinct-tags="distinctTags"
@afterMount="$emit('afterMount')"
@save="save"
/>
</div>
@ -17,6 +18,7 @@
<script>
import MsFullScreenButton from "@/business/components/common/components/MsFullScreenButton";
import {listenNodeSelected} from "@/business/components/track/common/minder/minderUtils";
export default {
name: "MsModuleMinder",
components: {MsFullScreenButton},
@ -39,6 +41,12 @@ export default {
return []
}
},
tagEnable: {
type: Boolean,
default() {
return false;
}
},
distinctTags: {
type: Array,
default() {
@ -56,7 +64,10 @@ export default {
data: {
text: this.$t('test_track.review_view.all_case'),
disable: true,
id: 'root',
id: "root",
type: 'node',
path: "",
tagEnable: this.tagEnable
},
children: []
},
@ -115,9 +126,14 @@ export default {
text: item.name,
id: item.id,
disable: true,
type: 'node',
path: root.data.path + "/" + item.name,
expandState:"collapse"
},
}
if (this.tagEnable) {
node.data.tagEnable = this.tagEnable;
}
root.children.push(node);
this.parse(node, item.children);
});
@ -127,6 +143,9 @@ export default {
this.$nextTick(() => {
this.isActive = true;
})
this.$nextTick(() => {
listenNodeSelected();
})
},
setJsonImport(data) {
this.importJson = data;
@ -147,6 +166,8 @@ export default {
text: nodeData.name,
id: nodeData.id,
disable: true,
tagEnable: this.tagEnable,
type: 'node'
},
children: []
},

View File

@ -4,8 +4,10 @@
:tree-nodes="treeNodes"
:data-map="dataMap"
:tags="tags"
:tag-enable="true"
:select-node="selectNode"
:distinct-tags="[...tags, $t('test_track.plan.plan_status_prepare')]"
:distinct-tags="[...tags, this.$t('test_track.plan.plan_status_prepare')]"
@afterMount="handleAfterMount"
@save="save"
ref="minder"
/>
@ -13,7 +15,10 @@
<script>
import MsModuleMinder from "@/business/components/common/components/MsModuleMinder";
import {getTestCaseDataMap} from "@/business/components/track/common/minder/minderUtils";
import {
getTestCaseDataMap,
tagBatch,
} from "@/business/components/track/common/minder/minderUtils";
export default {
name: "TestPlanMinder",
components: {MsModuleMinder},
@ -64,6 +69,9 @@ name: "TestPlanMinder",
}
},
methods: {
handleAfterMount() {
tagBatch([...this.tags, this.$t('test_track.plan.plan_status_prepare')]);
},
getTestCases() {
if (this.projectId) {
this.result = this.$get('/test/plan/case/list/minder/' + this.planId, response => {

View File

@ -4,8 +4,10 @@
:tree-nodes="treeNodes"
:data-map="dataMap"
:tags="tags"
:tag-enable="true"
:select-node="selectNode"
:distinct-tags="[...tags, $t('test_track.plan.plan_status_prepare')]"
@afterMount="handleAfterMount"
@save="save"
ref="minder"
/>
@ -13,7 +15,7 @@
<script>
import MsModuleMinder from "@/business/components/common/components/MsModuleMinder";
import {getTestCaseDataMap} from "@/business/components/track/common/minder/minderUtils";
import {getTestCaseDataMap, tagBatch} from "@/business/components/track/common/minder/minderUtils";
export default {
name: "TestReviewMinder",
components: {MsModuleMinder},
@ -64,6 +66,9 @@ name: "TestReviewMinder",
}
},
methods: {
handleAfterMount() {
tagBatch([...this.tags, this.$t('test_track.plan.plan_status_prepare')]);
},
getTestCases() {
if (this.projectId) {
let param = {

View File

@ -1,4 +1,4 @@
import {getUUID} from "@/common/js/utils";
import i18n from "@/i18n/i18n";
export function getTestCaseDataMap(testCase, isDisable, setParamCallback) {
let dataMap = new Map();
@ -25,7 +25,7 @@ export function parseCase(item, dataMap, isDisable, setParamCallback) {
id: item.id,
text: item.name,
priority: Number.parseInt(item.priority.substring(item.priority.length - 1 )) + 1,
resource: ["用例"],
resource: [i18n.t('api_test.definition.request.case')],
type: item.type,
method: item.method,
maintainer: item.maintainer
@ -53,7 +53,7 @@ export function parseCase(item, dataMap, isDisable, setParamCallback) {
function parseChildren(nodeItem, item, isDisable) {
nodeItem.children = [];
let children = [];
_parseChildren(children, item.prerequisite, "前置条件", isDisable);
_parseChildren(children, item.prerequisite, i18n.t('test_track.case.prerequisite'), isDisable);
if (item.steps) {
item.steps.forEach((step) => {
let descNode = _parseChildren(children, step.desc, undefined, isDisable);
@ -64,7 +64,7 @@ function parseChildren(nodeItem, item, isDisable) {
}
});
}
_parseChildren(children, item.remark, "备注", isDisable);
_parseChildren(children, item.remark, i18n.t('commons.remark'), isDisable);
nodeItem.children = children;
}
@ -85,6 +85,33 @@ function _parseChildren(children, k, v, isDisable) {
}
}
export function listenNodeSelected(callback) {
let minder = window.minder;
minder.on('selectionchange ', function (even) {
if (callback) {
callback(even);
}
});
}
export function listenNodeChange(callback) {
let minder = window.minder;
minder.on('contentchange ', function (even) {
if (callback) {
callback(even);
}
});
}
export function listenBeforeExecCommand(callback) {
let minder = window.minder;
minder.on('beforeExecCommand ', function (even) {
if (callback) {
callback(even);
}
});
}
export function appendChild(appendPid, root, node) {
if (root.data.id === appendPid) {
root.children.push(node);
@ -121,3 +148,85 @@ export function updateNode(root, node) {
}
}
}
export function tagChildren(node, resourceName, distinctTags) {
let children = node.children;
if (!children) {
children = [];
}
if (!resourceName || !/\S/.test(resourceName)) {return;}
children.forEach((item) => {
let isCaseNode = item.data.resource && item.data.resource.indexOf(i18n.t('api_test.definition.request.case')) > -1;
if (item.data.type === 'node' || isCaseNode) {
let origin = item.data.resource;
if (!origin) {
origin = [];
}
let index = origin.indexOf(resourceName);
// 先删除排他的标签
if (distinctTags.indexOf(resourceName) > -1) {
for (let i = 0; i < origin.length; i++) {
if (distinctTags.indexOf(origin[i]) > -1) {
origin.splice(i, 1);
i--;
}
}
}
if (index !== -1) {
origin.splice(index, 1);
} else {
origin.push(resourceName);
}
item.data.resource = origin;
if (isCaseNode) {
item.data.changed = true;
}
tagChildren(item, resourceName, distinctTags);
}
});
}
function modifyParentNodeTag(node, resourceName) {
let topNode = null;
while (node.parent) {
let pNode = node.parent;
let pResource = pNode.data.resource;
if (pResource && pResource.length > 0 && pResource.indexOf(resourceName) < 0) {
pNode.data.resource = [];
topNode = pNode;
}
node = pNode;
}
return topNode;
}
export function tagBatch(distinctTags) {
listenBeforeExecCommand((even) => {
let minder = window.minder;
let selectNodes = window.minder.getSelectedNodes();
let args = even.commandArgs;
if (selectNodes) {
selectNodes.forEach((node) => {
if (node.data.type === 'node' && even.commandName === 'resource') {
// let origin = minder.queryCommandValue('resource');
if (args && args.length > 0) {
let origin = args[0];
if (origin && origin.length > 0) {
let resourceName = origin[0];
tagChildren(node, resourceName, distinctTags);
let modifyTopNode = modifyParentNodeTag(node, resourceName);
if (modifyTopNode) {
modifyTopNode.renderTree();
} else {
node.renderTree();
}
minder.layout(600);
}
}
}
});
}
});
}