diff --git a/api-test/backend/src/main/java/io/metersphere/api/exec/api/ApiExecuteService.java b/api-test/backend/src/main/java/io/metersphere/api/exec/api/ApiExecuteService.java index e2d5b23639..10fbfc8064 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/exec/api/ApiExecuteService.java +++ b/api-test/backend/src/main/java/io/metersphere/api/exec/api/ApiExecuteService.java @@ -287,8 +287,16 @@ public class ApiExecuteService { TestPlanApiCaseExample example = new TestPlanApiCaseExample(); example.createCriteria().andTestPlanIdEqualTo(request.getTestPlanId()).andApiCaseIdEqualTo(request.getCaseId()); List list = testPlanApiCaseMapper.selectByExample(example); - request.setEnvironmentId(list.get(0).getEnvironmentId()); - element.setName(list.get(0).getId()); + if (CollectionUtils.isNotEmpty(list)) { + request.setEnvironmentId(list.get(0).getEnvironmentId()); + element.setName(list.get(0).getId()); + } else { + TestPlanApiCase apiCase = testPlanApiCaseMapper.selectByPrimaryKey(request.getCaseId()); + if (apiCase != null) { + request.setEnvironmentId(apiCase.getEnvironmentId()); + element.setName(request.getCaseId()); + } + } } else { element.setName(request.getCaseId()); } diff --git a/api-test/backend/src/main/java/io/metersphere/service/ApiExecutionQueueService.java b/api-test/backend/src/main/java/io/metersphere/service/ApiExecutionQueueService.java index 1c8379ad16..61fce3d6e4 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/ApiExecutionQueueService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/ApiExecutionQueueService.java @@ -11,10 +11,7 @@ import io.metersphere.base.mapper.ext.BaseApiExecutionQueueMapper; import io.metersphere.base.mapper.ext.ExtApiDefinitionExecResultMapper; import io.metersphere.base.mapper.ext.ExtApiScenarioReportMapper; import io.metersphere.base.mapper.plan.ext.ExtTestPlanApiCaseMapper; -import io.metersphere.commons.constants.ApiRunMode; -import io.metersphere.commons.constants.CommonConstants; -import io.metersphere.commons.constants.KafkaTopicConstants; -import io.metersphere.commons.constants.TestPlanReportStatus; +import io.metersphere.commons.constants.*; import io.metersphere.commons.enums.ApiReportStatus; import io.metersphere.commons.utils.BeanUtils; import io.metersphere.commons.utils.JSON; @@ -89,9 +86,11 @@ public class ApiExecutionQueueService { Map detailMap = new HashMap<>(); List queueDetails = new LinkedList<>(); // 初始化API/用例队列 + String redisLockType = TestPlanExecuteCaseType.SCENARIO.name(); if (StringUtils.equalsAnyIgnoreCase(type, ApiRunMode.DEFINITION.name(), ApiRunMode.API_PLAN.name())) { Map runMap = (Map) runObj; initApi(runMap, resQueue, config, detailMap, queueDetails); + redisLockType = TestPlanExecuteCaseType.API_CASE.name(); } // 初始化场景 else { @@ -101,11 +100,16 @@ public class ApiExecutionQueueService { if (CollectionUtils.isNotEmpty(queueDetails)) { extApiExecutionQueueMapper.sqlInsert(queueDetails); } + //redis移除key (执行测试计划时会添加key) + redisTemplateService.unlock(reportId, redisLockType, reportId); resQueue.setDetailMap(detailMap); LoggerUtil.info("报告【" + type + "】生成执行链结束", reportId); return resQueue; } + @Resource + private RedisTemplateService redisTemplateService; + private void initScenario(Map runMap, DBTestQueue resQueue, RunModeConfigDTO config, Map detailMap, List queueDetails) { final int[] sort = {0}; runMap.forEach((k, v) -> { diff --git a/api-test/backend/src/main/java/io/metersphere/service/MockConfigService.java b/api-test/backend/src/main/java/io/metersphere/service/MockConfigService.java index ea395ba200..778985153e 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/MockConfigService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/MockConfigService.java @@ -846,9 +846,8 @@ public class MockConfigService { if (project != null) { RequestMockParams requestMockParams = MockApiUtils.genRequestMockParamsFromHttpRequest(request, true); String urlSuffix = this.getUrlSuffix(project.getSystemId(), request); - LogUtil.info("Mock urlSuffix:{}", urlSuffix); - LogUtil.info("Mock requestHeaderMap:{}", requestHeaderMap); - LogUtil.info("Mock requestMockParams:{}", JSON.toJSONString(requestMockParams)); + LogUtil.info("Mock [" + url + "] Header:{}", requestHeaderMap); + LogUtil.info("Mock [" + url + "] request:{}", JSON.toJSONString(requestMockParams)); List qualifiedApiList = apiDefinitionService.preparedUrl(project.getId(), method, urlSuffix, requestHeaderMap.get(MockApiHeaders.MOCK_API_RESOURCE_ID)); for (ApiDefinitionWithBLOBs api : qualifiedApiList) { if (StringUtils.isEmpty(returnStr)) { @@ -872,6 +871,7 @@ public class MockConfigService { response.setStatus(404); returnStr = Translator.get("mock_warning"); } + LogUtil.info("Mock [" + url + "] response:{}", returnStr); return returnStr; } @@ -883,9 +883,8 @@ public class MockConfigService { RequestMockParams requestMockParams = MockApiUtils.genRequestMockParamsFromHttpRequest(request, false); String urlSuffix = this.getUrlSuffix(project.getSystemId(), request); - LogUtil.info("Mock urlSuffix:{}", urlSuffix); - LogUtil.info("Mock requestHeaderMap:{}", requestHeaderMap); - LogUtil.info("Mock requestMockParams:{}", JSON.toJSONString(requestMockParams)); + LogUtil.info("Mock [" + url + "] Header:{}", requestHeaderMap); + LogUtil.info("Mock [" + url + "] request:{}", JSON.toJSONString(requestMockParams)); List qualifiedApiList = apiDefinitionService.preparedUrl(project.getId(), method, urlSuffix, requestHeaderMap.get(MockApiHeaders.MOCK_API_RESOURCE_ID)); /* GET/DELETE 这种通过url穿参数的接口,在接口路径相同的情况下可能会出现这样的情况: @@ -919,6 +918,7 @@ public class MockConfigService { response.setStatus(404); returnStr = Translator.get("mock_warning"); } + LogUtil.info("Mock [" + url + "] response:{}", returnStr); return returnStr; } diff --git a/api-test/backend/src/main/java/io/metersphere/service/RedisTemplateService.java b/api-test/backend/src/main/java/io/metersphere/service/RedisTemplateService.java index e02d1e677f..509cf570f0 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/RedisTemplateService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/RedisTemplateService.java @@ -99,4 +99,10 @@ public class RedisTemplateService { } return false; } + + public void unlock(String testPlanReportId, String key, String value) { + String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end"; + RedisScript redisScript = new DefaultRedisScript<>(script, Long.class); + this.redisTemplate.execute(redisScript, Collections.singletonList(StringUtils.join(testPlanReportId, key)), new Object[]{value}); + } } diff --git a/api-test/frontend/src/business/automation/scenario/api/ApiRelevance.vue b/api-test/frontend/src/business/automation/scenario/api/ApiRelevance.vue index 90b4e2d306..e6156ec228 100644 --- a/api-test/frontend/src/business/automation/scenario/api/ApiRelevance.vue +++ b/api-test/frontend/src/business/automation/scenario/api/ApiRelevance.vue @@ -1,9 +1,9 @@