package com.ruoyi; import cn.hutool.http.HttpUtil; import com.alibaba.fastjson2.JSONObject; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.domain.TestRecords; import com.ruoyi.domain.vo.CaseResultVO; import com.ruoyi.domain.vo.StepResultVO; import com.ruoyi.mapper.TestRecordsMapper; import com.ruoyi.service.TestRecordsService; import com.ruoyi.service.impl.TestRecordsServiceImpl; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.*; @SpringBootTest public class TestRecordsTest { @Autowired private TestRecordsService testRecordsService; @Autowired private TestRecordsMapper testRecordsMapper; @Test public void test() { Map<String, Object> map = new HashMap<>(); map.put("id", "project_items;27"); map.put("verbose", "ALL"); //以post形式请求接口 String result= HttpUtil.post("https://10.12.48.78:8090/DescribeProjectTestResult",JSONObject.toJSONString(map)); JSONObject jsonObject = JSONObject.parseObject(result); // 获取关联项目id String projectId = (String) jsonObject.get("id"); // 获取项目用例结果列表 List<CaseResultVO> caseResultVOS = jsonObject.getList("case_result_list", CaseResultVO.class); // 获取本地存储的列表 List<TestRecords> recordsList = testRecordsService.findByTaskId("project_items;29"); List<TestRecords> list = new ArrayList<>(); if (caseResultVOS.size() != 0 && caseResultVOS != null) { for (CaseResultVO caseResultVO : caseResultVOS) { // 检查 usecaseId 是否在 recordsList 中存在 boolean exists = false; if (recordsList.size() != 0 && recordsList != null) { for (TestRecords records : recordsList) { if (Objects.equals(caseResultVO.getId(), records.getUsecaseId())) { exists = true; break; } } } if ((Objects.equals(caseResultVO.getStatus(), "PASSED") || Objects.equals(caseResultVO.getStatus(), "FAILED")) && !exists) { TestRecords testRecords = new TestRecords(); testRecords.setProjectId(projectId); testRecords.setUsecase(caseResultVO.getName()); testRecords.setUsecaseId(caseResultVO.getId()); testRecords.setDescription(caseResultVO.getDescription()); testRecords.setRiskLevel(caseResultVO.getRisk_level()); testRecords.setTestResult(caseResultVO.getStatus()); testRecords.setRemediation(caseResultVO.getRemediation()); List<StepResultVO> stepResultVOS = caseResultVO.getStep_result_list(); if (stepResultVOS.size() != 0 && stepResultVOS != null) { List<String> stepList = new ArrayList<>(); for (StepResultVO stepResultVO :stepResultVOS) { String stepName = stepResultVO.getName(); stepList.add(stepName); } testRecords.setTestMethod(StringUtils.join(stepList,",")); } list.add(testRecords); } } testRecordsService.saveBatch(list); } } }