refactor(接口自动化): 重构执行方法,按照场景存储

This commit is contained in:
fit2-zhao 2020-12-24 11:38:35 +08:00
parent c49e4c46b8
commit 08dbb9f59a
9 changed files with 249 additions and 28 deletions

View File

@ -44,9 +44,9 @@ public class APIScenarioReportController {
@PostMapping("/add")
@RequiresRoles(value = {RoleConstants.TEST_USER, RoleConstants.TEST_MANAGER}, logical = Logical.OR)
public String add(@RequestBody APIScenarioReportResult node) {
public void add(@RequestBody APIScenarioReportResult node) {
node.setExecuteType(ExecuteType.Saved.name());
return apiReportService.save(node, ApiRunMode.SCENARIO.name());
apiReportService.save(node, ApiRunMode.SCENARIO.name());
}
@PostMapping("/update")

View File

@ -20,7 +20,7 @@ public class TestResult {
private int passAssertions = 0;
private final List<ScenarioResult> scenarios = new ArrayList<>();
private List<ScenarioResult> scenarios = new ArrayList<>();
public void addError(int count) {
this.error += count;

View File

@ -91,6 +91,7 @@ public class ApiScenarioReportService {
}
report.setContent(new String(detail.getContent(), StandardCharsets.UTF_8));
this.save(report, runMode);
// 此方法适用于前端发起
if (!report.getTriggerMode().equals(ReportTriggerMode.SCHEDULE.name())) {
cache.put(report.getId(), report);
}
@ -141,19 +142,21 @@ public class ApiScenarioReportService {
}
}
public ApiScenarioReport createReport(APIScenarioReportResult test) {
public ApiScenarioReport createReport(APIScenarioReportResult test, String scenarioName, String scenarioId, String result) {
checkNameExist(test);
ApiScenarioReport report = new ApiScenarioReport();
report.setId(UUID.randomUUID().toString());
report.setProjectId(test.getProjectId());
report.setName(test.getName());
report.setName(scenarioName + "-" + DateUtils.getTimeStr(System.currentTimeMillis()));
report.setTriggerMode(test.getTriggerMode());
report.setDescription(test.getDescription());
report.setCreateTime(System.currentTimeMillis());
report.setUpdateTime(System.currentTimeMillis());
report.setStatus(test.getStatus());
report.setStatus(result);
report.setUserId(test.getUserId());
report.setExecuteType(test.getExecuteType());
report.setScenarioId(scenarioId);
report.setScenarioName(scenarioName);
apiScenarioReportMapper.insert(report);
return report;
}
@ -171,31 +174,36 @@ public class ApiScenarioReportService {
report.setStatus(test.getStatus());
report.setUserId(test.getUserId());
report.setExecuteType(test.getExecuteType());
report.setScenarioId(test.getScenarioId());
report.setScenarioName(test.getScenarioName());
apiScenarioReportMapper.updateByPrimaryKey(report);
return report;
}
private TestResult createTestResult(TestResult result) {
TestResult newrResult = new TestResult();
newrResult.setTestId(result.getTestId());
newrResult.setTotal(result.getTotal());
newrResult.setError(result.getError());
newrResult.setPassAssertions(result.getPassAssertions());
newrResult.setSuccess(result.getSuccess());
newrResult.setTotalAssertions(result.getTotalAssertions());
return newrResult;
}
public String save(APIScenarioReportResult test, String runModel) {
ApiScenarioReport report = createReport(test);
ApiScenarioReportDetail detail = new ApiScenarioReportDetail();
public void save(APIScenarioReportResult test, String runModel) {
TestResult result = JSON.parseObject(test.getContent(), TestResult.class);
// 更新场景
if (result != null) {
if (StringUtils.equals(runModel, ApiRunMode.SCENARIO_PLAN.name())) {
updatePlanCase(result, test, report);
updatePlanCase(result, test);
} else {
updateScenario(result.getScenarios(), test.getProjectId(), report.getId());
updateScenario(test, result);
}
}
detail.setContent(test.getContent().getBytes(StandardCharsets.UTF_8));
detail.setReportId(report.getId());
detail.setProjectId(test.getProjectId());
apiScenarioReportDetailMapper.insert(detail);
return report.getId();
}
public void updatePlanCase(TestResult result, APIScenarioReportResult test, ApiScenarioReport report) {
public void updatePlanCase(TestResult result, APIScenarioReportResult test) {
TestPlanApiScenario testPlanApiScenario = new TestPlanApiScenario();
testPlanApiScenario.setId(test.getId());
ScenarioResult scenarioResult = result.getScenarios().get(0);
@ -206,14 +214,28 @@ public class ApiScenarioReportService {
}
String passRate = new DecimalFormat("0%").format((float) scenarioResult.getSuccess() / (scenarioResult.getSuccess() + scenarioResult.getError()));
testPlanApiScenario.setPassRate(passRate);
// 存储场景报告
ApiScenarioReport report = createReport(test, scenarioResult.getName(), testPlanApiScenario.getApiScenarioId(), scenarioResult.getError() == 0 ? "Success" : "Error");
// 报告详情内容
ApiScenarioReportDetail detail = new ApiScenarioReportDetail();
TestResult newResult = createTestResult(result);
List<ScenarioResult> scenarioResults = new ArrayList();
scenarioResults.add(scenarioResult);
newResult.setScenarios(scenarioResults);
detail.setContent(JSON.toJSONString(newResult).getBytes(StandardCharsets.UTF_8));
detail.setReportId(report.getId());
detail.setProjectId(test.getProjectId());
apiScenarioReportDetailMapper.insert(detail);
testPlanApiScenario.setReportId(report.getId());
testPlanApiScenarioMapper.updateByPrimaryKeySelective(testPlanApiScenario);
}
public void updateScenario(List<ScenarioResult> Scenarios, String projectId, String reportId) {
Scenarios.forEach(item -> {
public void updateScenario(APIScenarioReportResult test, TestResult result) {
result.getScenarios().forEach(item -> {
ApiScenarioExample example = new ApiScenarioExample();
example.createCriteria().andNameEqualTo(item.getName()).andProjectIdEqualTo(projectId);
example.createCriteria().andNameEqualTo(item.getName()).andProjectIdEqualTo(test.getProjectId());
List<ApiScenario> list = apiScenarioMapper.selectByExample(example);
if (list.size() > 0) {
ApiScenario scenario = list.get(0);
@ -224,7 +246,21 @@ public class ApiScenarioReportService {
}
String passRate = new DecimalFormat("0%").format((float) item.getSuccess() / (item.getSuccess() + item.getError()));
scenario.setPassRate(passRate);
scenario.setReportId(reportId);
// 存储场景报告
ApiScenarioReport report = createReport(test, scenario.getName(), scenario.getId(), scenario.getLastResult());
// 报告详情内容
ApiScenarioReportDetail detail = new ApiScenarioReportDetail();
TestResult newResult = createTestResult(result);
List<ScenarioResult> scenarioResults = new ArrayList();
scenarioResults.add(item);
newResult.setScenarios(scenarioResults);
detail.setContent(JSON.toJSONString(newResult).getBytes(StandardCharsets.UTF_8));
detail.setReportId(report.getId());
detail.setProjectId(scenario.getProjectId());
apiScenarioReportDetailMapper.insert(detail);
// 更新场景状态
scenario.setReportId(report.getId());
apiScenarioMapper.updateByPrimaryKey(scenario);
}
});

View File

@ -24,6 +24,10 @@ public class ApiScenarioReport implements Serializable {
private String executeType;
private String scenarioName;
private String scenarioId;
private String description;
private static final long serialVersionUID = 1L;

View File

@ -713,6 +713,146 @@ public class ApiScenarioReportExample {
addCriterion("execute_type not between", value1, value2, "executeType");
return (Criteria) this;
}
public Criteria andScenarioNameIsNull() {
addCriterion("scenario_name is null");
return (Criteria) this;
}
public Criteria andScenarioNameIsNotNull() {
addCriterion("scenario_name is not null");
return (Criteria) this;
}
public Criteria andScenarioNameEqualTo(String value) {
addCriterion("scenario_name =", value, "scenarioName");
return (Criteria) this;
}
public Criteria andScenarioNameNotEqualTo(String value) {
addCriterion("scenario_name <>", value, "scenarioName");
return (Criteria) this;
}
public Criteria andScenarioNameGreaterThan(String value) {
addCriterion("scenario_name >", value, "scenarioName");
return (Criteria) this;
}
public Criteria andScenarioNameGreaterThanOrEqualTo(String value) {
addCriterion("scenario_name >=", value, "scenarioName");
return (Criteria) this;
}
public Criteria andScenarioNameLessThan(String value) {
addCriterion("scenario_name <", value, "scenarioName");
return (Criteria) this;
}
public Criteria andScenarioNameLessThanOrEqualTo(String value) {
addCriterion("scenario_name <=", value, "scenarioName");
return (Criteria) this;
}
public Criteria andScenarioNameLike(String value) {
addCriterion("scenario_name like", value, "scenarioName");
return (Criteria) this;
}
public Criteria andScenarioNameNotLike(String value) {
addCriterion("scenario_name not like", value, "scenarioName");
return (Criteria) this;
}
public Criteria andScenarioNameIn(List<String> values) {
addCriterion("scenario_name in", values, "scenarioName");
return (Criteria) this;
}
public Criteria andScenarioNameNotIn(List<String> values) {
addCriterion("scenario_name not in", values, "scenarioName");
return (Criteria) this;
}
public Criteria andScenarioNameBetween(String value1, String value2) {
addCriterion("scenario_name between", value1, value2, "scenarioName");
return (Criteria) this;
}
public Criteria andScenarioNameNotBetween(String value1, String value2) {
addCriterion("scenario_name not between", value1, value2, "scenarioName");
return (Criteria) this;
}
public Criteria andScenarioIdIsNull() {
addCriterion("scenario_id is null");
return (Criteria) this;
}
public Criteria andScenarioIdIsNotNull() {
addCriterion("scenario_id is not null");
return (Criteria) this;
}
public Criteria andScenarioIdEqualTo(String value) {
addCriterion("scenario_id =", value, "scenarioId");
return (Criteria) this;
}
public Criteria andScenarioIdNotEqualTo(String value) {
addCriterion("scenario_id <>", value, "scenarioId");
return (Criteria) this;
}
public Criteria andScenarioIdGreaterThan(String value) {
addCriterion("scenario_id >", value, "scenarioId");
return (Criteria) this;
}
public Criteria andScenarioIdGreaterThanOrEqualTo(String value) {
addCriterion("scenario_id >=", value, "scenarioId");
return (Criteria) this;
}
public Criteria andScenarioIdLessThan(String value) {
addCriterion("scenario_id <", value, "scenarioId");
return (Criteria) this;
}
public Criteria andScenarioIdLessThanOrEqualTo(String value) {
addCriterion("scenario_id <=", value, "scenarioId");
return (Criteria) this;
}
public Criteria andScenarioIdLike(String value) {
addCriterion("scenario_id like", value, "scenarioId");
return (Criteria) this;
}
public Criteria andScenarioIdNotLike(String value) {
addCriterion("scenario_id not like", value, "scenarioId");
return (Criteria) this;
}
public Criteria andScenarioIdIn(List<String> values) {
addCriterion("scenario_id in", values, "scenarioId");
return (Criteria) this;
}
public Criteria andScenarioIdNotIn(List<String> values) {
addCriterion("scenario_id not in", values, "scenarioId");
return (Criteria) this;
}
public Criteria andScenarioIdBetween(String value1, String value2) {
addCriterion("scenario_id between", value1, value2, "scenarioId");
return (Criteria) this;
}
public Criteria andScenarioIdNotBetween(String value1, String value2) {
addCriterion("scenario_id not between", value1, value2, "scenarioId");
return (Criteria) this;
}
}
public static class Criteria extends GeneratedCriteria {

View File

@ -2,9 +2,8 @@ package io.metersphere.base.mapper;
import io.metersphere.base.domain.ApiScenarioReport;
import io.metersphere.base.domain.ApiScenarioReportExample;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface ApiScenarioReportMapper {
long countByExample(ApiScenarioReportExample example);

View File

@ -11,6 +11,8 @@
<result column="user_id" jdbcType="VARCHAR" property="userId" />
<result column="trigger_mode" jdbcType="VARCHAR" property="triggerMode" />
<result column="execute_type" jdbcType="VARCHAR" property="executeType" />
<result column="scenario_name" jdbcType="VARCHAR" property="scenarioName" />
<result column="scenario_id" jdbcType="VARCHAR" property="scenarioId" />
</resultMap>
<resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="io.metersphere.base.domain.ApiScenarioReport">
<result column="description" jdbcType="LONGVARCHAR" property="description" />
@ -75,7 +77,7 @@
</sql>
<sql id="Base_Column_List">
id, project_id, `name`, create_time, update_time, `status`, user_id, trigger_mode,
execute_type
execute_type, scenario_name, scenario_id
</sql>
<sql id="Blob_Column_List">
description
@ -132,11 +134,13 @@
insert into api_scenario_report (id, project_id, `name`,
create_time, update_time, `status`,
user_id, trigger_mode, execute_type,
description)
scenario_name, scenario_id, description
)
values (#{id,jdbcType=VARCHAR}, #{projectId,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR},
#{createTime,jdbcType=BIGINT}, #{updateTime,jdbcType=BIGINT}, #{status,jdbcType=VARCHAR},
#{userId,jdbcType=VARCHAR}, #{triggerMode,jdbcType=VARCHAR}, #{executeType,jdbcType=VARCHAR},
#{description,jdbcType=LONGVARCHAR})
#{scenarioName,jdbcType=VARCHAR}, #{scenarioId,jdbcType=VARCHAR}, #{description,jdbcType=LONGVARCHAR}
)
</insert>
<insert id="insertSelective" parameterType="io.metersphere.base.domain.ApiScenarioReport">
insert into api_scenario_report
@ -168,6 +172,12 @@
<if test="executeType != null">
execute_type,
</if>
<if test="scenarioName != null">
scenario_name,
</if>
<if test="scenarioId != null">
scenario_id,
</if>
<if test="description != null">
description,
</if>
@ -200,6 +210,12 @@
<if test="executeType != null">
#{executeType,jdbcType=VARCHAR},
</if>
<if test="scenarioName != null">
#{scenarioName,jdbcType=VARCHAR},
</if>
<if test="scenarioId != null">
#{scenarioId,jdbcType=VARCHAR},
</if>
<if test="description != null">
#{description,jdbcType=LONGVARCHAR},
</if>
@ -241,6 +257,12 @@
<if test="record.executeType != null">
execute_type = #{record.executeType,jdbcType=VARCHAR},
</if>
<if test="record.scenarioName != null">
scenario_name = #{record.scenarioName,jdbcType=VARCHAR},
</if>
<if test="record.scenarioId != null">
scenario_id = #{record.scenarioId,jdbcType=VARCHAR},
</if>
<if test="record.description != null">
description = #{record.description,jdbcType=LONGVARCHAR},
</if>
@ -260,6 +282,8 @@
user_id = #{record.userId,jdbcType=VARCHAR},
trigger_mode = #{record.triggerMode,jdbcType=VARCHAR},
execute_type = #{record.executeType,jdbcType=VARCHAR},
scenario_name = #{record.scenarioName,jdbcType=VARCHAR},
scenario_id = #{record.scenarioId,jdbcType=VARCHAR},
description = #{record.description,jdbcType=LONGVARCHAR}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
@ -275,7 +299,9 @@
`status` = #{record.status,jdbcType=VARCHAR},
user_id = #{record.userId,jdbcType=VARCHAR},
trigger_mode = #{record.triggerMode,jdbcType=VARCHAR},
execute_type = #{record.executeType,jdbcType=VARCHAR}
execute_type = #{record.executeType,jdbcType=VARCHAR},
scenario_name = #{record.scenarioName,jdbcType=VARCHAR},
scenario_id = #{record.scenarioId,jdbcType=VARCHAR}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
@ -307,6 +333,12 @@
<if test="executeType != null">
execute_type = #{executeType,jdbcType=VARCHAR},
</if>
<if test="scenarioName != null">
scenario_name = #{scenarioName,jdbcType=VARCHAR},
</if>
<if test="scenarioId != null">
scenario_id = #{scenarioId,jdbcType=VARCHAR},
</if>
<if test="description != null">
description = #{description,jdbcType=LONGVARCHAR},
</if>
@ -323,6 +355,8 @@
user_id = #{userId,jdbcType=VARCHAR},
trigger_mode = #{triggerMode,jdbcType=VARCHAR},
execute_type = #{executeType,jdbcType=VARCHAR},
scenario_name = #{scenarioName,jdbcType=VARCHAR},
scenario_id = #{scenarioId,jdbcType=VARCHAR},
description = #{description,jdbcType=LONGVARCHAR}
where id = #{id,jdbcType=VARCHAR}
</update>
@ -335,7 +369,9 @@
`status` = #{status,jdbcType=VARCHAR},
user_id = #{userId,jdbcType=VARCHAR},
trigger_mode = #{triggerMode,jdbcType=VARCHAR},
execute_type = #{executeType,jdbcType=VARCHAR}
execute_type = #{executeType,jdbcType=VARCHAR},
scenario_name = #{scenarioName,jdbcType=VARCHAR},
scenario_id = #{scenarioId,jdbcType=VARCHAR}
where id = #{id,jdbcType=VARCHAR}
</update>
</mapper>

View File

@ -41,6 +41,10 @@ public class DateUtils {
return dateFormat.format(timeStamp);
}
public static String getTimeStr(long timeStamp) {
SimpleDateFormat dateFormat = new SimpleDateFormat(TIME_PATTERN);
return dateFormat.format(timeStamp);
}
public static Date dateSum (Date date,int countDays){

View File

@ -0,0 +1,2 @@
ALTER TABLE api_scenario_report ADD scenario_name varchar(255) NULL;
ALTER TABLE api_scenario_report ADD scenario_id varchar(100) NULL;