fix(接口测试): 接口导入数据解析错误问题修复

--bug=1017815 --user=赵勇 【接口测试】接口自动化-导入场景报错 https://www.tapd.cn/55049933/s/1258533
This commit is contained in:
fit2-zhao 2022-10-11 16:22:36 +08:00 committed by f2c-ci-robot[bot]
parent de1bbdd03c
commit f8d25edef4
11 changed files with 57 additions and 26 deletions

View File

@ -37,7 +37,7 @@ public class MsDefinitionParser extends MsAbstractParser<ApiDefinitionImport> {
if (this.projectId == null) {
MSException.throwException("wrong format");
}
if (testObject.get("projectName") != null || testObject.get("projectId") != null) {// metersphere 格式导入
if (testObject.opt("projectName") != null || testObject.opt("projectId") != null) {// metersphere 格式导入
return parseMsFormat(testStr, request);
} else { // chrome 插件录制格式导入
request.setPlatform(ApiImportPlatform.Plugin.name());

View File

@ -52,8 +52,6 @@ public class ApiDefinitionController {
@Resource
private ApiDefinitionService apiDefinitionService;
@Resource
private ApiTestCaseService apiTestCaseService;
@Resource
private EsbApiParamService esbApiParamService;
@Resource
private EsbImportService esbImportService;
@ -67,12 +65,6 @@ public class ApiDefinitionController {
@PostMapping("/list/{goPage}/{pageSize}")
@RequiresPermissions("PROJECT_API_DEFINITION:READ")
public Pager<List<ApiDefinitionResult>> list(@PathVariable int goPage, @PathVariable int pageSize, @RequestBody ApiDefinitionRequest request) {
if (request.getToBeUpdated() != null && request.getToBeUpdated()) {
Long toBeUpdatedTime = apiTestCaseService.getToBeUpdatedTime(request.getProjectId());
if (toBeUpdatedTime != null) {
request.setToBeUpdateTime(toBeUpdatedTime);
}
}
apiDefinitionService.checkFilterHasCoverage(request);
Page<Object> page = PageHelper.startPage(goPage, pageSize, true);
return PageUtils.setPageInfo(page, apiDefinitionService.list(request));

View File

@ -16,6 +16,8 @@ import org.apache.commons.lang3.StringUtils;
import org.python.core.Options;
import org.python.util.PythonInterpreter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@ -23,7 +25,7 @@ import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
public class ApiAppStartListener implements ApplicationListener<ApplicationReadyEvent> {
public class ApiAppStartListener implements ApplicationRunner {
@Resource
private BaseScheduleService scheduleService;
@Resource
@ -49,7 +51,7 @@ public class ApiAppStartListener implements ApplicationListener<ApplicationReady
private int quartzThreadCount;
@Override
public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
public void run(ApplicationArguments args) throws Exception {
LogUtil.info("================= API 应用启动 =================");
System.setProperty("jmeter.home", jmeterHome);
@ -69,7 +71,10 @@ public class ApiAppStartListener implements ApplicationListener<ApplicationReady
initPythonEnv();
//检查状态为开启的TCP-Mock服务端口
LogUtil.info("starting check mock ");
mockConfigService.initMockTcpService();
LogUtil.info("starting quartz");
StringBuffer buffer = new StringBuffer("定时任务相关设置:");
buffer.append("quartz.acquireTriggersWithinLock :")
.append(acquireTriggersWithinLock).append("\n")
@ -83,6 +88,7 @@ public class ApiAppStartListener implements ApplicationListener<ApplicationReady
LogUtil.info(buffer.toString());
scheduleService.startEnableSchedules(ScheduleGroup.API_SCENARIO_TEST);
scheduleService.startEnableSchedules(ScheduleGroup.SWAGGER_IMPORT);
LogUtil.info("Startup complete");
}

View File

@ -172,6 +172,13 @@ public class ApiDefinitionService {
private static final String SCHEDULE = "schedule";
public List<ApiDefinitionResult> list(ApiDefinitionRequest request) {
// 来自工作台条件
if (BooleanUtils.isTrue(request.getToBeUpdated())) {
Long toBeUpdatedTime = apiTestCaseService.getToBeUpdatedTime(request.getProjectId());
if (toBeUpdatedTime != null) {
request.setToBeUpdateTime(toBeUpdatedTime);
}
}
request = this.initRequest(request, true, true);
List<ApiDefinitionResult> resList = extApiDefinitionMapper.list(request);
buildUserInfo(resList);
@ -1427,7 +1434,7 @@ public class ApiDefinitionService {
private boolean setImportHashTree(ApiDefinitionWithBLOBs apiDefinition) {
String request = apiDefinition.getRequest();
MsHTTPSamplerProxy msHTTPSamplerProxy = JSON.parseObject(request, MsHTTPSamplerProxy.class);
MsHTTPSamplerProxy msHTTPSamplerProxy = JSONUtil.parseObject(request, MsHTTPSamplerProxy.class);
boolean createCase = CollectionUtils.isNotEmpty(msHTTPSamplerProxy.getHeaders());
if (CollectionUtils.isNotEmpty(msHTTPSamplerProxy.getArguments()) && !createCase) {
createCase = true;
@ -2209,15 +2216,15 @@ public class ApiDefinitionService {
return extApiDefinitionMapper.selectEffectiveIdByProjectIdAndHaveNotCase(projectId);
}
public List<ApiDefinitionWithBLOBs> preparedUrl(String projectId, String method, String baseUrlSuffix,String mockApiResourceId) {
public List<ApiDefinitionWithBLOBs> preparedUrl(String projectId, String method, String baseUrlSuffix, String mockApiResourceId) {
if (StringUtils.isEmpty(baseUrlSuffix)) {
return new ArrayList<>();
} else {
String apiId = this.getApiIdFromMockApiResourceId(mockApiResourceId,projectId);
String apiId = this.getApiIdFromMockApiResourceId(mockApiResourceId, projectId);
ApiDefinitionExample example = new ApiDefinitionExample();
ApiDefinitionExample.Criteria criteria = example.createCriteria();
criteria.andMethodEqualTo(method).andProjectIdEqualTo(projectId).andStatusNotEqualTo("Trash").andProtocolEqualTo("HTTP").andLatestEqualTo(true);
if(StringUtils.isNotBlank(apiId)){
if (StringUtils.isNotBlank(apiId)) {
criteria.andIdEqualTo(apiId);
}
List<ApiDefinition> apiList = apiDefinitionMapper.selectByExample(example);
@ -2274,19 +2281,19 @@ public class ApiDefinitionService {
}
}
private String getApiIdFromMockApiResourceId(String mockApiResourceId,String projectId) {
private String getApiIdFromMockApiResourceId(String mockApiResourceId, String projectId) {
String returnId = null;
ApiDefinition apiDefinition = this.get(mockApiResourceId);
if(apiDefinition == null){
if (apiDefinition == null) {
ApiTestCase testCase = apiTestCaseMapper.selectByPrimaryKey(mockApiResourceId);
if(testCase != null){
if(StringUtils.equals(testCase.getProjectId(),projectId) && !StringUtils.equals(testCase.getStatus(),"Trash")){
if (testCase != null) {
if (StringUtils.equals(testCase.getProjectId(), projectId) && !StringUtils.equals(testCase.getStatus(), "Trash")) {
returnId = testCase.getApiDefinitionId();
}
}
}else {
if(StringUtils.equals(apiDefinition.getProjectId(),projectId) && !StringUtils.equals(apiDefinition.getStatus(),"Trash") && apiDefinition.getLatest()){
returnId = mockApiResourceId;
} else {
if (StringUtils.equals(apiDefinition.getProjectId(), projectId) && !StringUtils.equals(apiDefinition.getStatus(), "Trash") && apiDefinition.getLatest()) {
returnId = mockApiResourceId;
}
}
return returnId;

View File

@ -250,7 +250,7 @@
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<File>${logging.file.path}/ms-jmeter-run-log.log</File>
<File>${logging.file.path}/ms-jmeter-run.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${logging.file.path}/history/ms-jmeter-run-log.%d{yyyyMMdd}-%i.log
</FileNamePattern>

View File

@ -748,6 +748,7 @@ export default {
this.apiDefaultTab = activeName
this.apiTabs = tabs.filter(tab => tab.name !== targetName);
this.$refs.mainTabs.$children = [];
this.$refs.apiDefList[0].refreshTable();
},
//
createRootModel() {

View File

@ -175,8 +175,12 @@ export default {
}
:deep(.el-icon-close:hover) {
font-size: 30px;
font-size: 18px;
font-weight: bold;
}
:deep(.fulls-screen-btn) {
position: fixed !important;
}
</style>

View File

@ -491,7 +491,7 @@ export default {
},
singleRun(data) {
let methods = ["SQL", "DUBBO", "dubbo://", "TCP"];
if (data.apiMethod && methods.indexOf(data.apiMethod) === -1 && !this.environment) {
if (data.apiMethod && methods.indexOf(data.apiMethod) === -1 && this.environment === undefined) {
this.$warning(this.$t('api_test.environment.select_environment'));
return;
}

View File

@ -573,7 +573,7 @@ export default {
},
singleRun(row) {
let methods = ["SQL", "DUBBO", "dubbo://", "TCP"];
if (row.apiMethod && methods.indexOf(row.apiMethod) === -1 && !this.environment) {
if (row.apiMethod && methods.indexOf(row.apiMethod) === -1 && this.environment === undefined) {
this.$warning(this.$t('api_test.environment.select_environment'));
return;
}

View File

@ -746,6 +746,20 @@ export default {
}
});
},
refreshTable() {
this.initTableCondition();
if (this.condition.projectId) {
this.result = getDefinitionPage(this.currentPage, this.pageSize, this.condition).then(response => {
getProtocolFilter(this.condition.protocol);
this.total = response.data.itemCount;
this.tableData = response.data.listObject;
this.historyData();
if (this.$refs.table) {
this.$refs.table.clearSelection();
}
});
}
},
initTable(currentProtocol) {
this.initTableCondition(currentProtocol);
if (this.condition.projectId) {

View File

@ -1,10 +1,13 @@
package io.metersphere.commons.utils;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.TypeFactory;
@ -19,6 +22,10 @@ public class JSON {
static {
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 自动检测所有类的全部属性
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
// 如果一个对象中没有任何的属性那么在序列化的时候就会报错
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
}
public static String toJSONString(Object value) {