refactor(接口测试): 场景导入接口兼容历史数据

--bug=1027894 --user=王孝刚 【接口测试】场景详情-UI列表导入-切换项目-选择模块-选择所有数据-复制报500
https://www.tapd.cn/55049933/s/1394138
This commit is contained in:
wxg0103 2023-07-17 17:51:12 +08:00 committed by fit2-zhao
parent ae83bc1675
commit b4bf0c6e63
2 changed files with 40 additions and 27 deletions

View File

@ -180,6 +180,12 @@ public class ApiDefinitionService {
public static final String ARGUMENTS = "arguments"; public static final String ARGUMENTS = "arguments";
public static final String BODY = "body"; public static final String BODY = "body";
private static final String SCHEDULE = "schedule"; private static final String SCHEDULE = "schedule";
public static final String TYPE = "type";
public static final String HTTP = "HTTPSamplerProxy";
public static final String CLAZZ = "className";
public static final String FORMAT = "format";
public static final String RAW = "raw";
public static final String JSONSCHEMA = "jsonSchema";
public List<ApiDefinitionResult> list(ApiDefinitionRequest request) { public List<ApiDefinitionResult> list(ApiDefinitionRequest request) {
@ -330,19 +336,21 @@ public class ApiDefinitionService {
} }
List<ApiDefinitionResult> resList = extApiDefinitionMapper.listByIds(request.getIds()); List<ApiDefinitionResult> resList = extApiDefinitionMapper.listByIds(request.getIds());
resList.forEach(item -> { resList.forEach(item -> {
MsTestElement msTestElement = JSONUtil.parseObject(item.getRequest(), MsTestElement.class); JSONObject jsonObject = JSONUtil.parseObject(item.getRequest());
if (msTestElement instanceof MsHTTPSamplerProxy) { if (jsonObject != null && jsonObject.has(TYPE) && jsonObject.optString(TYPE).equals(HTTP)) {
MsHTTPSamplerProxy requestBody = (MsHTTPSamplerProxy) msTestElement; jsonObject.put(CLAZZ, MsHTTPSamplerProxy.class.getCanonicalName());
Body body = requestBody.getBody(); JSONObject body = jsonObject.optJSONObject(BODY);
if (StringUtils.isNotBlank(body.getType()) && StringUtils.equals(body.getType(), Body.JSON_STR)) { if (StringUtils.isNotBlank(body.optString(TYPE))
if (StringUtils.isNotEmpty(body.getFormat()) && body.getJsonSchema() != null && Body.JSON_SCHEMA.equals(body.getFormat())) { && StringUtils.equals(body.optString(TYPE), Body.JSON_STR)
body.setRaw(JSONSchemaParser.preview(JSONUtil.toJSONString(body.getJsonSchema()))); && StringUtils.isNotEmpty(body.optString(FORMAT))
&& body.optJSONObject(JSONSCHEMA) != null
&& Body.JSON_SCHEMA.equals(body.optString(FORMAT))) {
body.put(RAW, JSONSchemaParser.preview(body.optString(JSONSCHEMA)));
jsonObject.put(BODY, body);
} }
item.setRequest(jsonObject.toString());
} }
item.setRequest(JSONUtil.toJSONString(requestBody)); });
}
}
);
// 排序 // 排序
FixedOrderComparator<String> fixedOrderComparator = new FixedOrderComparator<String>(request.getIds()); FixedOrderComparator<String> fixedOrderComparator = new FixedOrderComparator<String>(request.getIds());
fixedOrderComparator.setUnknownObjectBehavior(FixedOrderComparator.UnknownObjectBehavior.BEFORE); fixedOrderComparator.setUnknownObjectBehavior(FixedOrderComparator.UnknownObjectBehavior.BEFORE);
@ -1563,9 +1571,12 @@ public class ApiDefinitionService {
*/ */
List<ApiDefinitionWithBLOBs> specifyData = new ArrayList<>(); List<ApiDefinitionWithBLOBs> specifyData = new ArrayList<>();
if (StringUtils.isNotBlank(mockApiResourceId)) { if (StringUtils.isNotBlank(mockApiResourceId)) {
ApiDefinitionWithBLOBs apiDefinition = apiDefinitionMapper.selectByPrimaryKey(mockApiResourceId); ApiDefinitionExample apiDefinitionExample = new ApiDefinitionExample();
if (apiDefinition != null) { apiDefinitionExample.createCriteria().andIdEqualTo(mockApiResourceId).andStatusNotEqualTo("Trash");
specifyData.add(apiDefinition); List<ApiDefinitionWithBLOBs> apiDefinitions = apiDefinitionMapper.selectByExampleWithBLOBs(apiDefinitionExample);
ApiDefinitionWithBLOBs apiDefinition;
if (CollectionUtils.isNotEmpty(apiDefinitions)) {
specifyData.add(apiDefinitions.get(0));
} else { } else {
ApiTestCase testCase = apiTestCaseMapper.selectByPrimaryKey(mockApiResourceId); ApiTestCase testCase = apiTestCaseMapper.selectByPrimaryKey(mockApiResourceId);
if (testCase != null) { if (testCase != null) {

View File

@ -851,19 +851,21 @@ public class ApiTestCaseService {
} else { } else {
list = extApiTestCaseMapper.getCaseInfo(request); list = extApiTestCaseMapper.getCaseInfo(request);
list.forEach(item -> { list.forEach(item -> {
MsTestElement msTestElement = JSONUtil.parseObject(item.getRequest(), MsTestElement.class); JSONObject jsonObject = JSONUtil.parseObject(item.getRequest());
if (msTestElement instanceof MsHTTPSamplerProxy) { if (jsonObject != null && jsonObject.has(ApiDefinitionService.TYPE) && jsonObject.optString(ApiDefinitionService.TYPE).equals(ApiDefinitionService.HTTP)) {
MsHTTPSamplerProxy requestBody = (MsHTTPSamplerProxy) JSONUtil.parseObject(item.getRequest(), MsTestElement.class); jsonObject.put(ApiDefinitionService.CLAZZ, MsHTTPSamplerProxy.class.getCanonicalName());
Body body = requestBody.getBody(); JSONObject body = jsonObject.optJSONObject(ApiDefinitionService.BODY);
if (StringUtils.isNotBlank(body.getType()) && StringUtils.equals(body.getType(), Body.JSON_STR)) { if (StringUtils.isNotBlank(body.optString(ApiDefinitionService.TYPE))
if (StringUtils.isNotEmpty(body.getFormat()) && body.getJsonSchema() != null && Body.JSON_SCHEMA.equals(body.getFormat())) { && StringUtils.equals(body.optString(ApiDefinitionService.TYPE), Body.JSON_STR)
body.setRaw(JSONSchemaParser.preview(JSONUtil.toJSONString(body.getJsonSchema()))); && StringUtils.isNotEmpty(body.optString(ApiDefinitionService.FORMAT))
&& body.optJSONObject(ApiDefinitionService.JSONSCHEMA) != null
&& Body.JSON_SCHEMA.equals(body.optString(ApiDefinitionService.FORMAT))) {
body.put(ApiDefinitionService.RAW, JSONSchemaParser.preview(body.optString(ApiDefinitionService.JSONSCHEMA)));
jsonObject.put(ApiDefinitionService.BODY, body);
} }
item.setRequest(jsonObject.toString());
} }
item.setRequest(JSONUtil.toJSONString(requestBody)); });
}
}
);
} }
// 排序 // 排序
FixedOrderComparator<String> fixedOrderComparator = new FixedOrderComparator<String>(request.getIds()); FixedOrderComparator<String> fixedOrderComparator = new FixedOrderComparator<String>(request.getIds());