refactor(接口测试): 优化场景表结构

This commit is contained in:
wxg0103 2024-01-17 17:31:48 +08:00 committed by wxg0103
parent 714efcddf5
commit a9da0370a4
9 changed files with 30 additions and 824 deletions

View File

@ -1,98 +0,0 @@
package io.metersphere.api.domain;
import io.metersphere.validation.groups.*;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import lombok.Data;
@Data
public class ApiScenarioEnvironment implements Serializable {
@Schema(description = "场景fk", requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank(message = "{api_scenario_environment.api_scenario_id.not_blank}", groups = {Created.class})
@Size(min = 1, max = 50, message = "{api_scenario_environment.api_scenario_id.length_range}", groups = {Created.class, Updated.class})
private String apiScenarioId;
@Schema(description = "环境fk")
private String environmentId;
@Schema(description = "环境组fk")
private String environmentGroupId;
private static final long serialVersionUID = 1L;
public enum Column {
apiScenarioId("api_scenario_id", "apiScenarioId", "VARCHAR", false),
environmentId("environment_id", "environmentId", "VARCHAR", false),
environmentGroupId("environment_group_id", "environmentGroupId", "VARCHAR", false);
private static final String BEGINNING_DELIMITER = "`";
private static final String ENDING_DELIMITER = "`";
private final String column;
private final boolean isColumnNameDelimited;
private final String javaProperty;
private final String jdbcType;
public String value() {
return this.column;
}
public String getValue() {
return this.column;
}
public String getJavaProperty() {
return this.javaProperty;
}
public String getJdbcType() {
return this.jdbcType;
}
Column(String column, String javaProperty, String jdbcType, boolean isColumnNameDelimited) {
this.column = column;
this.javaProperty = javaProperty;
this.jdbcType = jdbcType;
this.isColumnNameDelimited = isColumnNameDelimited;
}
public String desc() {
return this.getEscapedColumnName() + " DESC";
}
public String asc() {
return this.getEscapedColumnName() + " ASC";
}
public static Column[] excludes(Column ... excludes) {
ArrayList<Column> columns = new ArrayList<>(Arrays.asList(Column.values()));
if (excludes != null && excludes.length > 0) {
columns.removeAll(new ArrayList<>(Arrays.asList(excludes)));
}
return columns.toArray(new Column[]{});
}
public static Column[] all() {
return Column.values();
}
public String getEscapedColumnName() {
if (this.isColumnNameDelimited) {
return new StringBuilder().append(BEGINNING_DELIMITER).append(this.column).append(ENDING_DELIMITER).toString();
} else {
return this.column;
}
}
public String getAliasedEscapedColumnName() {
return this.getEscapedColumnName();
}
}
}

View File

@ -1,410 +0,0 @@
package io.metersphere.api.domain;
import java.util.ArrayList;
import java.util.List;
public class ApiScenarioEnvironmentExample {
protected String orderByClause;
protected boolean distinct;
protected List<Criteria> oredCriteria;
public ApiScenarioEnvironmentExample() {
oredCriteria = new ArrayList<Criteria>();
}
public void setOrderByClause(String orderByClause) {
this.orderByClause = orderByClause;
}
public String getOrderByClause() {
return orderByClause;
}
public void setDistinct(boolean distinct) {
this.distinct = distinct;
}
public boolean isDistinct() {
return distinct;
}
public List<Criteria> getOredCriteria() {
return oredCriteria;
}
public void or(Criteria criteria) {
oredCriteria.add(criteria);
}
public Criteria or() {
Criteria criteria = createCriteriaInternal();
oredCriteria.add(criteria);
return criteria;
}
public Criteria createCriteria() {
Criteria criteria = createCriteriaInternal();
if (oredCriteria.size() == 0) {
oredCriteria.add(criteria);
}
return criteria;
}
protected Criteria createCriteriaInternal() {
Criteria criteria = new Criteria();
return criteria;
}
public void clear() {
oredCriteria.clear();
orderByClause = null;
distinct = false;
}
protected abstract static class GeneratedCriteria {
protected List<Criterion> criteria;
protected GeneratedCriteria() {
super();
criteria = new ArrayList<Criterion>();
}
public boolean isValid() {
return criteria.size() > 0;
}
public List<Criterion> getAllCriteria() {
return criteria;
}
public List<Criterion> getCriteria() {
return criteria;
}
protected void addCriterion(String condition) {
if (condition == null) {
throw new RuntimeException("Value for condition cannot be null");
}
criteria.add(new Criterion(condition));
}
protected void addCriterion(String condition, Object value, String property) {
if (value == null) {
throw new RuntimeException("Value for " + property + " cannot be null");
}
criteria.add(new Criterion(condition, value));
}
protected void addCriterion(String condition, Object value1, Object value2, String property) {
if (value1 == null || value2 == null) {
throw new RuntimeException("Between values for " + property + " cannot be null");
}
criteria.add(new Criterion(condition, value1, value2));
}
public Criteria andApiScenarioIdIsNull() {
addCriterion("api_scenario_id is null");
return (Criteria) this;
}
public Criteria andApiScenarioIdIsNotNull() {
addCriterion("api_scenario_id is not null");
return (Criteria) this;
}
public Criteria andApiScenarioIdEqualTo(String value) {
addCriterion("api_scenario_id =", value, "apiScenarioId");
return (Criteria) this;
}
public Criteria andApiScenarioIdNotEqualTo(String value) {
addCriterion("api_scenario_id <>", value, "apiScenarioId");
return (Criteria) this;
}
public Criteria andApiScenarioIdGreaterThan(String value) {
addCriterion("api_scenario_id >", value, "apiScenarioId");
return (Criteria) this;
}
public Criteria andApiScenarioIdGreaterThanOrEqualTo(String value) {
addCriterion("api_scenario_id >=", value, "apiScenarioId");
return (Criteria) this;
}
public Criteria andApiScenarioIdLessThan(String value) {
addCriterion("api_scenario_id <", value, "apiScenarioId");
return (Criteria) this;
}
public Criteria andApiScenarioIdLessThanOrEqualTo(String value) {
addCriterion("api_scenario_id <=", value, "apiScenarioId");
return (Criteria) this;
}
public Criteria andApiScenarioIdLike(String value) {
addCriterion("api_scenario_id like", value, "apiScenarioId");
return (Criteria) this;
}
public Criteria andApiScenarioIdNotLike(String value) {
addCriterion("api_scenario_id not like", value, "apiScenarioId");
return (Criteria) this;
}
public Criteria andApiScenarioIdIn(List<String> values) {
addCriterion("api_scenario_id in", values, "apiScenarioId");
return (Criteria) this;
}
public Criteria andApiScenarioIdNotIn(List<String> values) {
addCriterion("api_scenario_id not in", values, "apiScenarioId");
return (Criteria) this;
}
public Criteria andApiScenarioIdBetween(String value1, String value2) {
addCriterion("api_scenario_id between", value1, value2, "apiScenarioId");
return (Criteria) this;
}
public Criteria andApiScenarioIdNotBetween(String value1, String value2) {
addCriterion("api_scenario_id not between", value1, value2, "apiScenarioId");
return (Criteria) this;
}
public Criteria andEnvironmentIdIsNull() {
addCriterion("environment_id is null");
return (Criteria) this;
}
public Criteria andEnvironmentIdIsNotNull() {
addCriterion("environment_id is not null");
return (Criteria) this;
}
public Criteria andEnvironmentIdEqualTo(String value) {
addCriterion("environment_id =", value, "environmentId");
return (Criteria) this;
}
public Criteria andEnvironmentIdNotEqualTo(String value) {
addCriterion("environment_id <>", value, "environmentId");
return (Criteria) this;
}
public Criteria andEnvironmentIdGreaterThan(String value) {
addCriterion("environment_id >", value, "environmentId");
return (Criteria) this;
}
public Criteria andEnvironmentIdGreaterThanOrEqualTo(String value) {
addCriterion("environment_id >=", value, "environmentId");
return (Criteria) this;
}
public Criteria andEnvironmentIdLessThan(String value) {
addCriterion("environment_id <", value, "environmentId");
return (Criteria) this;
}
public Criteria andEnvironmentIdLessThanOrEqualTo(String value) {
addCriterion("environment_id <=", value, "environmentId");
return (Criteria) this;
}
public Criteria andEnvironmentIdLike(String value) {
addCriterion("environment_id like", value, "environmentId");
return (Criteria) this;
}
public Criteria andEnvironmentIdNotLike(String value) {
addCriterion("environment_id not like", value, "environmentId");
return (Criteria) this;
}
public Criteria andEnvironmentIdIn(List<String> values) {
addCriterion("environment_id in", values, "environmentId");
return (Criteria) this;
}
public Criteria andEnvironmentIdNotIn(List<String> values) {
addCriterion("environment_id not in", values, "environmentId");
return (Criteria) this;
}
public Criteria andEnvironmentIdBetween(String value1, String value2) {
addCriterion("environment_id between", value1, value2, "environmentId");
return (Criteria) this;
}
public Criteria andEnvironmentIdNotBetween(String value1, String value2) {
addCriterion("environment_id not between", value1, value2, "environmentId");
return (Criteria) this;
}
public Criteria andEnvironmentGroupIdIsNull() {
addCriterion("environment_group_id is null");
return (Criteria) this;
}
public Criteria andEnvironmentGroupIdIsNotNull() {
addCriterion("environment_group_id is not null");
return (Criteria) this;
}
public Criteria andEnvironmentGroupIdEqualTo(String value) {
addCriterion("environment_group_id =", value, "environmentGroupId");
return (Criteria) this;
}
public Criteria andEnvironmentGroupIdNotEqualTo(String value) {
addCriterion("environment_group_id <>", value, "environmentGroupId");
return (Criteria) this;
}
public Criteria andEnvironmentGroupIdGreaterThan(String value) {
addCriterion("environment_group_id >", value, "environmentGroupId");
return (Criteria) this;
}
public Criteria andEnvironmentGroupIdGreaterThanOrEqualTo(String value) {
addCriterion("environment_group_id >=", value, "environmentGroupId");
return (Criteria) this;
}
public Criteria andEnvironmentGroupIdLessThan(String value) {
addCriterion("environment_group_id <", value, "environmentGroupId");
return (Criteria) this;
}
public Criteria andEnvironmentGroupIdLessThanOrEqualTo(String value) {
addCriterion("environment_group_id <=", value, "environmentGroupId");
return (Criteria) this;
}
public Criteria andEnvironmentGroupIdLike(String value) {
addCriterion("environment_group_id like", value, "environmentGroupId");
return (Criteria) this;
}
public Criteria andEnvironmentGroupIdNotLike(String value) {
addCriterion("environment_group_id not like", value, "environmentGroupId");
return (Criteria) this;
}
public Criteria andEnvironmentGroupIdIn(List<String> values) {
addCriterion("environment_group_id in", values, "environmentGroupId");
return (Criteria) this;
}
public Criteria andEnvironmentGroupIdNotIn(List<String> values) {
addCriterion("environment_group_id not in", values, "environmentGroupId");
return (Criteria) this;
}
public Criteria andEnvironmentGroupIdBetween(String value1, String value2) {
addCriterion("environment_group_id between", value1, value2, "environmentGroupId");
return (Criteria) this;
}
public Criteria andEnvironmentGroupIdNotBetween(String value1, String value2) {
addCriterion("environment_group_id not between", value1, value2, "environmentGroupId");
return (Criteria) this;
}
}
public static class Criteria extends GeneratedCriteria {
protected Criteria() {
super();
}
}
public static class Criterion {
private String condition;
private Object value;
private Object secondValue;
private boolean noValue;
private boolean singleValue;
private boolean betweenValue;
private boolean listValue;
private String typeHandler;
public String getCondition() {
return condition;
}
public Object getValue() {
return value;
}
public Object getSecondValue() {
return secondValue;
}
public boolean isNoValue() {
return noValue;
}
public boolean isSingleValue() {
return singleValue;
}
public boolean isBetweenValue() {
return betweenValue;
}
public boolean isListValue() {
return listValue;
}
public String getTypeHandler() {
return typeHandler;
}
protected Criterion(String condition) {
super();
this.condition = condition;
this.typeHandler = null;
this.noValue = true;
}
protected Criterion(String condition, Object value, String typeHandler) {
super();
this.condition = condition;
this.value = value;
this.typeHandler = typeHandler;
if (value instanceof List<?>) {
this.listValue = true;
} else {
this.singleValue = true;
}
}
protected Criterion(String condition, Object value) {
this(condition, value, null);
}
protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {
super();
this.condition = condition;
this.value = value;
this.secondValue = secondValue;
this.typeHandler = typeHandler;
this.betweenValue = true;
}
protected Criterion(String condition, Object value, Object secondValue) {
this(condition, value, secondValue, null);
}
}
}

View File

@ -1,34 +0,0 @@
package io.metersphere.api.mapper;
import io.metersphere.api.domain.ApiScenarioEnvironment;
import io.metersphere.api.domain.ApiScenarioEnvironmentExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface ApiScenarioEnvironmentMapper {
long countByExample(ApiScenarioEnvironmentExample example);
int deleteByExample(ApiScenarioEnvironmentExample example);
int deleteByPrimaryKey(String apiScenarioId);
int insert(ApiScenarioEnvironment record);
int insertSelective(ApiScenarioEnvironment record);
List<ApiScenarioEnvironment> selectByExample(ApiScenarioEnvironmentExample example);
ApiScenarioEnvironment selectByPrimaryKey(String apiScenarioId);
int updateByExampleSelective(@Param("record") ApiScenarioEnvironment record, @Param("example") ApiScenarioEnvironmentExample example);
int updateByExample(@Param("record") ApiScenarioEnvironment record, @Param("example") ApiScenarioEnvironmentExample example);
int updateByPrimaryKeySelective(ApiScenarioEnvironment record);
int updateByPrimaryKey(ApiScenarioEnvironment record);
int batchInsert(@Param("list") List<ApiScenarioEnvironment> list);
int batchInsertSelective(@Param("list") List<ApiScenarioEnvironment> list, @Param("selective") ApiScenarioEnvironment.Column ... selective);
}

View File

@ -1,213 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="io.metersphere.api.mapper.ApiScenarioEnvironmentMapper">
<resultMap id="BaseResultMap" type="io.metersphere.api.domain.ApiScenarioEnvironment">
<id column="api_scenario_id" jdbcType="VARCHAR" property="apiScenarioId" />
<result column="environment_id" jdbcType="VARCHAR" property="environmentId" />
<result column="environment_group_id" jdbcType="VARCHAR" property="environmentGroupId" />
</resultMap>
<sql id="Example_Where_Clause">
<where>
<foreach collection="oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Update_By_Example_Where_Clause">
<where>
<foreach collection="example.oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Base_Column_List">
api_scenario_id, environment_id, environment_group_id
</sql>
<select id="selectByExample" parameterType="io.metersphere.api.domain.ApiScenarioEnvironmentExample" resultMap="BaseResultMap">
select
<if test="distinct">
distinct
</if>
<include refid="Base_Column_List" />
from api_scenario_environment
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
<select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from api_scenario_environment
where api_scenario_id = #{apiScenarioId,jdbcType=VARCHAR}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String">
delete from api_scenario_environment
where api_scenario_id = #{apiScenarioId,jdbcType=VARCHAR}
</delete>
<delete id="deleteByExample" parameterType="io.metersphere.api.domain.ApiScenarioEnvironmentExample">
delete from api_scenario_environment
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</delete>
<insert id="insert" parameterType="io.metersphere.api.domain.ApiScenarioEnvironment">
insert into api_scenario_environment (api_scenario_id, environment_id, environment_group_id
)
values (#{apiScenarioId,jdbcType=VARCHAR}, #{environmentId,jdbcType=VARCHAR}, #{environmentGroupId,jdbcType=VARCHAR}
)
</insert>
<insert id="insertSelective" parameterType="io.metersphere.api.domain.ApiScenarioEnvironment">
insert into api_scenario_environment
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="apiScenarioId != null">
api_scenario_id,
</if>
<if test="environmentId != null">
environment_id,
</if>
<if test="environmentGroupId != null">
environment_group_id,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="apiScenarioId != null">
#{apiScenarioId,jdbcType=VARCHAR},
</if>
<if test="environmentId != null">
#{environmentId,jdbcType=VARCHAR},
</if>
<if test="environmentGroupId != null">
#{environmentGroupId,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="io.metersphere.api.domain.ApiScenarioEnvironmentExample" resultType="java.lang.Long">
select count(*) from api_scenario_environment
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</select>
<update id="updateByExampleSelective" parameterType="map">
update api_scenario_environment
<set>
<if test="record.apiScenarioId != null">
api_scenario_id = #{record.apiScenarioId,jdbcType=VARCHAR},
</if>
<if test="record.environmentId != null">
environment_id = #{record.environmentId,jdbcType=VARCHAR},
</if>
<if test="record.environmentGroupId != null">
environment_group_id = #{record.environmentGroupId,jdbcType=VARCHAR},
</if>
</set>
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExample" parameterType="map">
update api_scenario_environment
set api_scenario_id = #{record.apiScenarioId,jdbcType=VARCHAR},
environment_id = #{record.environmentId,jdbcType=VARCHAR},
environment_group_id = #{record.environmentGroupId,jdbcType=VARCHAR}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByPrimaryKeySelective" parameterType="io.metersphere.api.domain.ApiScenarioEnvironment">
update api_scenario_environment
<set>
<if test="environmentId != null">
environment_id = #{environmentId,jdbcType=VARCHAR},
</if>
<if test="environmentGroupId != null">
environment_group_id = #{environmentGroupId,jdbcType=VARCHAR},
</if>
</set>
where api_scenario_id = #{apiScenarioId,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKey" parameterType="io.metersphere.api.domain.ApiScenarioEnvironment">
update api_scenario_environment
set environment_id = #{environmentId,jdbcType=VARCHAR},
environment_group_id = #{environmentGroupId,jdbcType=VARCHAR}
where api_scenario_id = #{apiScenarioId,jdbcType=VARCHAR}
</update>
<insert id="batchInsert" parameterType="map">
insert into api_scenario_environment
(api_scenario_id, environment_id, environment_group_id)
values
<foreach collection="list" item="item" separator=",">
(#{item.apiScenarioId,jdbcType=VARCHAR}, #{item.environmentId,jdbcType=VARCHAR},
#{item.environmentGroupId,jdbcType=VARCHAR})
</foreach>
</insert>
<insert id="batchInsertSelective" parameterType="map">
insert into api_scenario_environment (
<foreach collection="selective" item="column" separator=",">
${column.escapedColumnName}
</foreach>
)
values
<foreach collection="list" item="item" separator=",">
(
<foreach collection="selective" item="column" separator=",">
<if test="'api_scenario_id'.toString() == column.value">
#{item.apiScenarioId,jdbcType=VARCHAR}
</if>
<if test="'environment_id'.toString() == column.value">
#{item.environmentId,jdbcType=VARCHAR}
</if>
<if test="'environment_group_id'.toString() == column.value">
#{item.environmentGroupId,jdbcType=VARCHAR}
</if>
</foreach>
)
</foreach>
</insert>
</mapper>

View File

@ -178,7 +178,7 @@ CREATE TABLE IF NOT EXISTS api_report_detail(
COLLATE = utf8mb4_general_ci COMMENT = 'API/CASE执行结果详情';
CREATE INDEX idx_report ON api_report_detail(report_id);
CREATE INDEX idx_resource_id ON api_report_detail(resource_id);
CREATE INDEX idx_step_id ON api_report_detail(step_id);
CREATE TABLE api_report_log(
`id` VARCHAR(50) NOT NULL COMMENT '主键' ,
@ -562,19 +562,6 @@ CREATE TABLE IF NOT EXISTS api_test_case_blob(
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_general_ci COMMENT = '接口用例详情';
CREATE TABLE IF NOT EXISTS api_scenario_environment(
`api_scenario_id` VARCHAR(50) NOT NULL COMMENT '场景fk' ,
`environment_id` VARCHAR(50) COMMENT '环境fk' ,
`environment_group_id` VARCHAR(50) COMMENT '环境组fk' ,
PRIMARY KEY (api_scenario_id)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_general_ci COMMENT = '场景环境';
CREATE INDEX idx_api_scenario_id ON api_scenario_environment(api_scenario_id);
CREATE INDEX idx_environment_id ON api_scenario_environment(environment_id);
CREATE TABLE IF NOT EXISTS api_file_resource(
`resource_id` VARCHAR(50) NOT NULL COMMENT '资源ID(接口用例等)' ,
`file_id` VARCHAR(50) NOT NULL COMMENT '文件ID' ,

View File

@ -13,7 +13,7 @@
select
api_scenario.id, api_scenario.`name`,api_scenario.priority,
api_scenario.step_total,api_scenario.request_pass_rate,api_scenario.last_report_status,
api_scenario.request_execution_rate,
api_scenario.environment_id,
api_scenario.last_report_id,api_scenario.grouped,
api_scenario.`status`, api_scenario.num, api_scenario.tags, api_scenario.pos,
api_scenario.project_id, api_scenario.module_id, api_scenario.latest, api_scenario.version_id,

View File

@ -370,6 +370,7 @@ public class ApiTestCaseService {
}
return ids;
} else {
request.getSelectIds().removeAll(request.getExcludeIds());
return request.getSelectIds();
}
}

View File

@ -5,7 +5,10 @@ import io.metersphere.api.domain.*;
import io.metersphere.api.dto.scenario.ApiScenarioBatchEditRequest;
import io.metersphere.api.dto.scenario.ApiScenarioDTO;
import io.metersphere.api.dto.scenario.ApiScenarioPageRequest;
import io.metersphere.api.mapper.*;
import io.metersphere.api.mapper.ApiScenarioFollowerMapper;
import io.metersphere.api.mapper.ApiScenarioMapper;
import io.metersphere.api.mapper.ApiScenarioModuleMapper;
import io.metersphere.api.mapper.ExtApiScenarioMapper;
import io.metersphere.sdk.domain.Environment;
import io.metersphere.sdk.domain.EnvironmentExample;
import io.metersphere.sdk.domain.EnvironmentGroup;
@ -50,8 +53,6 @@ public class ApiScenarioService {
@Resource
private UserLoginService userLoginService;
@Resource
private ApiScenarioEnvironmentMapper apiScenarioEnvironmentMapper;
@Resource
private ApiScenarioModuleMapper apiScenarioModuleMapper;
@Resource
private EnvironmentMapper environmentMapper;
@ -82,21 +83,13 @@ public class ApiScenarioService {
private void processApiScenario(List<ApiScenarioDTO> scenarioLists) {
Set<String> userIds = extractUserIds(scenarioLists);
Map<String, String> userMap = userLoginService.getUserNameMap(new ArrayList<>(userIds));
//取出所有的apiId
List<String> scenarioIds = scenarioLists.stream().map(ApiScenarioDTO::getId).distinct().toList();
ApiScenarioEnvironmentExample scenarioEnvironmentExample = new ApiScenarioEnvironmentExample();
scenarioEnvironmentExample.createCriteria().andApiScenarioIdIn(scenarioIds);
List<ApiScenarioEnvironment> apiScenarioEnvironments = apiScenarioEnvironmentMapper.selectByExample(scenarioEnvironmentExample);
//生成map key为id value为ApiScenarioEnvironment
Map<String, ApiScenarioEnvironment> environmentMap = apiScenarioEnvironments.stream().collect(Collectors.toMap(ApiScenarioEnvironment::getApiScenarioId, item -> item));
List<String> envIds = apiScenarioEnvironments.stream().map(ApiScenarioEnvironment::getEnvironmentId).toList();
List<String> envIds = scenarioLists.stream().map(ApiScenarioDTO::getEnvironmentId).toList();
EnvironmentExample environmentExample = new EnvironmentExample();
environmentExample.createCriteria().andIdIn(envIds);
List<Environment> environments = environmentMapper.selectByExample(environmentExample);
Map<String, String> envMap = environments.stream().collect(Collectors.toMap(Environment::getId, Environment::getName));
List<String> envGroupIds = apiScenarioEnvironments.stream().map(ApiScenarioEnvironment::getEnvironmentGroupId).toList();
EnvironmentGroupExample groupExample = new EnvironmentGroupExample();
groupExample.createCriteria().andIdIn(envGroupIds);
groupExample.createCriteria().andIdIn(envIds);
List<EnvironmentGroup> environmentGroups = environmentGroupMapper.selectByExample(groupExample);
Map<String, String> groupMap = environmentGroups.stream().collect(Collectors.toMap(EnvironmentGroup::getId, EnvironmentGroup::getName));
//取模块id为新的set
@ -111,14 +104,10 @@ public class ApiScenarioService {
item.setDeleteUserName(userMap.get(item.getDeleteUser()));
item.setUpdateUserName(userMap.get(item.getUpdateUser()));
item.setModulePath(StringUtils.isNotBlank(moduleMap.get(item.getModuleId())) ? moduleMap.get(item.getModuleId()) : Translator.get("api_unplanned_scenario"));
if (!item.getGrouped() && environmentMap.containsKey(item.getId()) &&
StringUtils.isNotBlank(environmentMap.get(item.getId()).getEnvironmentId()) &&
envMap.containsKey(environmentMap.get(item.getId()).getEnvironmentId())) {
item.setEnvironmentName(environmentMap.get(item.getId()).getEnvironmentId());
} else if (item.getGrouped() && environmentMap.containsKey(item.getId()) &&
StringUtils.isNotBlank(environmentMap.get(item.getId()).getEnvironmentGroupId()) &&
groupMap.containsKey(environmentMap.get(item.getId()).getEnvironmentGroupId())) {
item.setEnvironmentName(groupMap.get(item.getId()));
if (!item.getGrouped() && envMap.containsKey(item.getEnvironmentId())) {
item.setEnvironmentName(envMap.get(item.getEnvironmentId()));
} else if (item.getGrouped() && groupMap.containsKey(item.getId())) {
item.setEnvironmentName(groupMap.get(item.getEnvironmentId()));
}
});
}
@ -150,7 +139,7 @@ public class ApiScenarioService {
case PRIORITY -> batchUpdatePriority(example, updateScenario, request.getPriority());
case STATUS -> batchUpdateStatus(example, updateScenario, request.getStatus());
case TAGS -> batchUpdateTags(example, updateScenario, request, ids, sqlSession, mapper);
case ENVIRONMENT -> batchUpdateEnvironment(example, updateScenario, request, ids);
case ENVIRONMENT -> batchUpdateEnvironment(example, updateScenario, request);
default -> throw new MSException(Translator.get("batch_edit_type_error"));
}
List<ApiScenario> scenarioInfoByIds = extApiScenarioMapper.getInfoByIds(ids, false);
@ -158,8 +147,7 @@ public class ApiScenarioService {
}
private void batchUpdateEnvironment(ApiScenarioExample example, ApiScenario updateScenario,
ApiScenarioBatchEditRequest request,
List<String> ids) {
ApiScenarioBatchEditRequest request) {
if (BooleanUtils.isFalse(request.isGrouped())) {
if (StringUtils.isBlank(request.getEnvId())) {
throw new MSException(Translator.get("environment_id_is_null"));
@ -169,11 +157,7 @@ public class ApiScenarioService {
throw new MSException(Translator.get("environment_is_not_exist"));
}
updateScenario.setGrouped(false);
ApiScenarioEnvironment apiScenarioEnvironment = new ApiScenarioEnvironment();
apiScenarioEnvironment.setEnvironmentId(request.getEnvId());
ApiScenarioEnvironmentExample environmentExample = new ApiScenarioEnvironmentExample();
environmentExample.createCriteria().andApiScenarioIdIn(ids);
apiScenarioEnvironmentMapper.updateByExampleSelective(apiScenarioEnvironment, environmentExample);
updateScenario.setEnvironmentId(request.getEnvId());
} else {
if (StringUtils.isBlank(request.getGroupId())) {
throw new MSException(Translator.get("environment_group_id_is_null"));
@ -183,11 +167,7 @@ public class ApiScenarioService {
throw new MSException(Translator.get("environment_group_is_not_exist"));
}
updateScenario.setGrouped(true);
ApiScenarioEnvironment apiScenarioEnvironment = new ApiScenarioEnvironment();
apiScenarioEnvironment.setEnvironmentGroupId(request.getGroupId());
ApiScenarioEnvironmentExample environmentExample = new ApiScenarioEnvironmentExample();
environmentExample.createCriteria().andApiScenarioIdIn(ids);
apiScenarioEnvironmentMapper.updateByExampleSelective(apiScenarioEnvironment, environmentExample);
updateScenario.setEnvironmentId(request.getGroupId());
}
apiScenarioMapper.updateByExampleSelective(updateScenario, example);

View File

@ -4,7 +4,10 @@ import io.metersphere.api.domain.*;
import io.metersphere.api.dto.scenario.ApiScenarioBatchEditRequest;
import io.metersphere.api.dto.scenario.ApiScenarioDTO;
import io.metersphere.api.dto.scenario.ApiScenarioPageRequest;
import io.metersphere.api.mapper.*;
import io.metersphere.api.mapper.ApiScenarioFollowerMapper;
import io.metersphere.api.mapper.ApiScenarioMapper;
import io.metersphere.api.mapper.ApiScenarioModuleMapper;
import io.metersphere.api.mapper.ExtApiScenarioMapper;
import io.metersphere.sdk.constants.ApplicationNumScope;
import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.sdk.constants.SessionConstants;
@ -57,8 +60,6 @@ public class ApiScenarioControllerTests extends BaseTest {
@Resource
private ExtApiScenarioMapper extApiScenarioMapper;
@Resource
private ApiScenarioEnvironmentMapper apiScenarioEnvironmentMapper;
@Resource
private EnvironmentMapper environmentMapper;
@Resource
private ApiScenarioFollowerMapper apiScenarioFollowerMapper;
@ -132,18 +133,15 @@ public class ApiScenarioControllerTests extends BaseTest {
apiScenario.setUpdateTime(System.currentTimeMillis());
apiScenario.setCreateUser("admin");
apiScenario.setUpdateUser("admin");
ApiScenarioEnvironment apiScenarioEnvironment = new ApiScenarioEnvironment();
apiScenarioEnvironment.setApiScenarioId(apiScenario.getId());
if (i % 2 == 0) {
apiScenario.setTags(new ArrayList<>(List.of("tag1", "tag2")));
apiScenario.setGrouped(true);
apiScenarioEnvironment.setEnvironmentGroupId("scenario-environment-group-id");
apiScenario.setEnvironmentId("scenario-environment-group-id");
} else {
apiScenario.setGrouped(false);
apiScenarioEnvironment.setEnvironmentId(environments.get(0).getId());
apiScenario.setEnvironmentId(environments.get(0).getId());
}
apiScenarioMapper.insertSelective(apiScenario);
apiScenarioEnvironmentMapper.insertSelective(apiScenarioEnvironment);
}
}
@ -176,9 +174,6 @@ public class ApiScenarioControllerTests extends BaseTest {
apiScenario.setCreateUser("admin");
apiScenario.setUpdateUser("admin");
apiScenarioMapper.insertSelective(apiScenario);
ApiScenarioEnvironment apiScenarioEnvironment = new ApiScenarioEnvironment();
apiScenarioEnvironment.setApiScenarioId(apiScenario.getId());
apiScenarioEnvironmentMapper.insertSelective(apiScenarioEnvironment);
}
}
@ -362,13 +357,9 @@ public class ApiScenarioControllerTests extends BaseTest {
List<Environment> environments = environmentMapper.selectByExample(environmentExample);
request.setEnvId(environments.get(0).getId());
responsePost(BATCH_EDIT, request);
//取所有的ids
List<String> scenarioIds = apiScenarios.stream().map(ApiScenario::getId).toList();
//判断数据的环境是不是environments.get(0).getId()
ApiScenarioEnvironmentExample apiScenarioEnvironmentExample = new ApiScenarioEnvironmentExample();
apiScenarioEnvironmentExample.createCriteria().andApiScenarioIdIn(scenarioIds);
List<ApiScenarioEnvironment> apiScenarioEnvironments = apiScenarioEnvironmentMapper.selectByExample(apiScenarioEnvironmentExample);
apiScenarioEnvironments.forEach(apiTestCase -> Assertions.assertEquals(apiTestCase.getEnvironmentId(), environments.get(0).getId()));
apiScenarios = apiScenarioMapper.selectByExample(example);
apiScenarios.forEach(apiTestCase -> Assertions.assertEquals(apiTestCase.getEnvironmentId(), environments.get(0).getId()));
//环境数据为空
request.setEnvId(null);
@ -380,10 +371,12 @@ public class ApiScenarioControllerTests extends BaseTest {
request.setGrouped(true);
request.setGroupId("scenario-environment-group-id");
responsePost(BATCH_EDIT, request);
apiScenarioEnvironments = apiScenarioEnvironmentMapper.selectByExample(apiScenarioEnvironmentExample);
apiScenarioEnvironments.forEach(apiTestCase -> Assertions.assertEquals(apiTestCase.getEnvironmentGroupId(), "scenario-environment-group-id"));
apiScenarios = apiScenarioMapper.selectByExample(example);
apiScenarios.forEach(apiTestCase -> Assertions.assertEquals(apiTestCase.getGrouped(), true));
apiScenarios.forEach(apiTestCase -> {
Assertions.assertEquals(apiTestCase.getGrouped(), true);
Assertions.assertEquals(apiTestCase.getEnvironmentId(), "scenario-environment-group-id");
});
//环境组数据为空
request.setGroupId(null);