feat(接口测试): 增加用例报告列表

This commit is contained in:
wxg0103 2024-01-17 20:47:51 +08:00 committed by wxg0103
parent 615e26a0c9
commit 402e74c854
15 changed files with 514 additions and 30 deletions

View File

@ -11,8 +11,9 @@ import lombok.Data;
@Data
public class ApiReportStep implements Serializable {
@Schema(description = "步骤id", requiredMode = Schema.RequiredMode.REQUIRED)
@NotNull(message = "{api_report_step.step_id.not_blank}", groups = {Created.class})
private Long stepId;
@NotBlank(message = "{api_report_step.step_id.not_blank}", groups = {Created.class})
@Size(min = 1, max = 50, message = "{api_report_step.step_id.length_range}", groups = {Created.class, Updated.class})
private String stepId;
@Schema(description = "报告id", requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank(message = "{api_report_step.report_id.not_blank}", groups = {Created.class})
@ -34,7 +35,7 @@ public class ApiReportStep implements Serializable {
private static final long serialVersionUID = 1L;
public enum Column {
stepId("step_id", "stepId", "BIGINT", false),
stepId("step_id", "stepId", "VARCHAR", false),
reportId("report_id", "reportId", "VARCHAR", false),
name("name", "name", "VARCHAR", true),
sort("sort", "sort", "BIGINT", false),

View File

@ -114,52 +114,62 @@ public class ApiReportStepExample {
return (Criteria) this;
}
public Criteria andStepIdEqualTo(Long value) {
public Criteria andStepIdEqualTo(String value) {
addCriterion("step_id =", value, "stepId");
return (Criteria) this;
}
public Criteria andStepIdNotEqualTo(Long value) {
public Criteria andStepIdNotEqualTo(String value) {
addCriterion("step_id <>", value, "stepId");
return (Criteria) this;
}
public Criteria andStepIdGreaterThan(Long value) {
public Criteria andStepIdGreaterThan(String value) {
addCriterion("step_id >", value, "stepId");
return (Criteria) this;
}
public Criteria andStepIdGreaterThanOrEqualTo(Long value) {
public Criteria andStepIdGreaterThanOrEqualTo(String value) {
addCriterion("step_id >=", value, "stepId");
return (Criteria) this;
}
public Criteria andStepIdLessThan(Long value) {
public Criteria andStepIdLessThan(String value) {
addCriterion("step_id <", value, "stepId");
return (Criteria) this;
}
public Criteria andStepIdLessThanOrEqualTo(Long value) {
public Criteria andStepIdLessThanOrEqualTo(String value) {
addCriterion("step_id <=", value, "stepId");
return (Criteria) this;
}
public Criteria andStepIdIn(List<Long> values) {
public Criteria andStepIdLike(String value) {
addCriterion("step_id like", value, "stepId");
return (Criteria) this;
}
public Criteria andStepIdNotLike(String value) {
addCriterion("step_id not like", value, "stepId");
return (Criteria) this;
}
public Criteria andStepIdIn(List<String> values) {
addCriterion("step_id in", values, "stepId");
return (Criteria) this;
}
public Criteria andStepIdNotIn(List<Long> values) {
public Criteria andStepIdNotIn(List<String> values) {
addCriterion("step_id not in", values, "stepId");
return (Criteria) this;
}
public Criteria andStepIdBetween(Long value1, Long value2) {
public Criteria andStepIdBetween(String value1, String value2) {
addCriterion("step_id between", value1, value2, "stepId");
return (Criteria) this;
}
public Criteria andStepIdNotBetween(Long value1, Long value2) {
public Criteria andStepIdNotBetween(String value1, String value2) {
addCriterion("step_id not between", value1, value2, "stepId");
return (Criteria) this;
}

View File

@ -10,7 +10,7 @@ public interface ApiReportStepMapper {
int deleteByExample(ApiReportStepExample example);
int deleteByPrimaryKey(@Param("stepId") Long stepId, @Param("reportId") String reportId);
int deleteByPrimaryKey(@Param("stepId") String stepId, @Param("reportId") String reportId);
int insert(ApiReportStep record);
@ -18,7 +18,7 @@ public interface ApiReportStepMapper {
List<ApiReportStep> selectByExample(ApiReportStepExample example);
ApiReportStep selectByPrimaryKey(@Param("stepId") Long stepId, @Param("reportId") String reportId);
ApiReportStep selectByPrimaryKey(@Param("stepId") String stepId, @Param("reportId") String reportId);
int updateByExampleSelective(@Param("record") ApiReportStep record, @Param("example") ApiReportStepExample example);

View File

@ -2,7 +2,7 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="io.metersphere.api.mapper.ApiReportStepMapper">
<resultMap id="BaseResultMap" type="io.metersphere.api.domain.ApiReportStep">
<id column="step_id" jdbcType="BIGINT" property="stepId" />
<id column="step_id" jdbcType="VARCHAR" property="stepId" />
<id column="report_id" jdbcType="VARCHAR" property="reportId" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="sort" jdbcType="BIGINT" property="sort" />
@ -87,12 +87,12 @@
select
<include refid="Base_Column_List" />
from api_report_step
where step_id = #{stepId,jdbcType=BIGINT}
where step_id = #{stepId,jdbcType=VARCHAR}
and report_id = #{reportId,jdbcType=VARCHAR}
</select>
<delete id="deleteByPrimaryKey" parameterType="map">
delete from api_report_step
where step_id = #{stepId,jdbcType=BIGINT}
where step_id = #{stepId,jdbcType=VARCHAR}
and report_id = #{reportId,jdbcType=VARCHAR}
</delete>
<delete id="deleteByExample" parameterType="io.metersphere.api.domain.ApiReportStepExample">
@ -104,7 +104,7 @@
<insert id="insert" parameterType="io.metersphere.api.domain.ApiReportStep">
insert into api_report_step (step_id, report_id, `name`,
sort, step_type)
values (#{stepId,jdbcType=BIGINT}, #{reportId,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR},
values (#{stepId,jdbcType=VARCHAR}, #{reportId,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR},
#{sort,jdbcType=BIGINT}, #{stepType,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="io.metersphere.api.domain.ApiReportStep">
@ -128,7 +128,7 @@
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="stepId != null">
#{stepId,jdbcType=BIGINT},
#{stepId,jdbcType=VARCHAR},
</if>
<if test="reportId != null">
#{reportId,jdbcType=VARCHAR},
@ -154,7 +154,7 @@
update api_report_step
<set>
<if test="record.stepId != null">
step_id = #{record.stepId,jdbcType=BIGINT},
step_id = #{record.stepId,jdbcType=VARCHAR},
</if>
<if test="record.reportId != null">
report_id = #{record.reportId,jdbcType=VARCHAR},
@ -175,7 +175,7 @@
</update>
<update id="updateByExample" parameterType="map">
update api_report_step
set step_id = #{record.stepId,jdbcType=BIGINT},
set step_id = #{record.stepId,jdbcType=VARCHAR},
report_id = #{record.reportId,jdbcType=VARCHAR},
`name` = #{record.name,jdbcType=VARCHAR},
sort = #{record.sort,jdbcType=BIGINT},
@ -197,7 +197,7 @@
step_type = #{stepType,jdbcType=VARCHAR},
</if>
</set>
where step_id = #{stepId,jdbcType=BIGINT}
where step_id = #{stepId,jdbcType=VARCHAR}
and report_id = #{reportId,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKey" parameterType="io.metersphere.api.domain.ApiReportStep">
@ -205,7 +205,7 @@
set `name` = #{name,jdbcType=VARCHAR},
sort = #{sort,jdbcType=BIGINT},
step_type = #{stepType,jdbcType=VARCHAR}
where step_id = #{stepId,jdbcType=BIGINT}
where step_id = #{stepId,jdbcType=VARCHAR}
and report_id = #{reportId,jdbcType=VARCHAR}
</update>
<insert id="batchInsert" parameterType="map">
@ -213,7 +213,7 @@
(step_id, report_id, `name`, sort, step_type)
values
<foreach collection="list" item="item" separator=",">
(#{item.stepId,jdbcType=BIGINT}, #{item.reportId,jdbcType=VARCHAR}, #{item.name,jdbcType=VARCHAR},
(#{item.stepId,jdbcType=VARCHAR}, #{item.reportId,jdbcType=VARCHAR}, #{item.name,jdbcType=VARCHAR},
#{item.sort,jdbcType=BIGINT}, #{item.stepType,jdbcType=VARCHAR})
</foreach>
</insert>
@ -228,7 +228,7 @@
(
<foreach collection="selective" item="column" separator=",">
<if test="'step_id'.toString() == column.value">
#{item.stepId,jdbcType=BIGINT}
#{item.stepId,jdbcType=VARCHAR}
</if>
<if test="'report_id'.toString() == column.value">
#{item.reportId,jdbcType=VARCHAR}

View File

@ -146,7 +146,7 @@ CREATE INDEX idx_name ON api_report(name);
CREATE INDEX idx_pool_id ON api_report(pool_id);
CREATE TABLE IF NOT EXISTS api_report_step(
`step_id` BIGINT NOT NULL COMMENT '步骤id' ,
`step_id` VARCHAR(50) NOT NULL COMMENT '步骤id' ,
`report_id` VARCHAR(50) NOT NULL COMMENT '报告id' ,
`name` VARCHAR(255) COMMENT '步骤名称' ,
`sort` BIGINT NOT NULL COMMENT '序号' ,

View File

@ -0,0 +1,43 @@
package io.metersphere.api.controller.definition;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.metersphere.api.domain.ApiReport;
import io.metersphere.api.service.definition.ApiReportService;
import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.system.dto.sdk.BasePageRequest;
import io.metersphere.system.security.CheckOwner;
import io.metersphere.system.utils.PageUtils;
import io.metersphere.system.utils.Pager;
import io.metersphere.system.utils.SessionUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping(value = "/api/report/case")
@Tag(name = "接口测试-接口报告-用例")
public class ApiReportController {
@Resource
private ApiReportService apiReportService;
@PostMapping("/page")
@Operation(summary = "接口测试-接口报告-用例()")
@RequiresPermissions(PermissionConstants.PROJECT_API_SCENARIO_READ)
@CheckOwner(resourceId = "#request.getProjectId()", resourceType = "project")
public Pager<List<ApiReport>> getPage(@Validated @RequestBody BasePageRequest request) {
Page<Object> page = PageHelper.startPage(request.getCurrent(), request.getPageSize(),
StringUtils.isNotBlank(request.getSortString()) ? request.getSortString() : "start_time desc");
return PageUtils.setPageInfo(page, apiReportService.getPage(request, SessionUtils.getCurrentProjectId()));
}
}

View File

@ -0,0 +1,12 @@
package io.metersphere.api.mapper;
import io.metersphere.api.domain.ApiReport;
import io.metersphere.system.dto.sdk.BasePageRequest;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface ExtApiReportMapper {
List<ApiReport> list(@Param("request") BasePageRequest request, @Param("projectId") String projectId);
}

View File

@ -0,0 +1,47 @@
<?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.ExtApiReportMapper">
<select id="list" resultType="io.metersphere.api.domain.ApiReport">
select
api_report.id,
api_report.name,
api_report.integrated,
api_report.status,
api_report.start_time,
api_report.update_time,
api_report.create_user,
api_report.update_user,
api_report.trigger_mode
from api_report where api_report.project_id = #{projectId}
<if test="request.keyword != null and request.keyword != ''">
and (
api_report.name like concat('%', #{request.keyword},'%')
)
</if>
<include refid="filters">
<property name="filter" value="request.filter"/>
</include>
</select>
<sql id="filters">
<if test="${filter} != null and ${filter}.size() > 0">
<foreach collection="${filter}.entrySet()" index="key" item="values">
<if test="values != null and values.size() > 0">
<choose>
<when test="key=='integrated'">
and api_report.integrated in
<include refid="io.metersphere.system.mapper.BaseMapper.filterInWrapper"/>
</when>
<when test="key=='status'">
and api_report.status in
<include refid="io.metersphere.system.mapper.BaseMapper.filterInWrapper"/>
</when>
</choose>
</if>
</foreach>
</if>
</sql>
</mapper>

View File

@ -0,0 +1,90 @@
package io.metersphere.api.service.definition;
import io.metersphere.api.domain.ApiReport;
import io.metersphere.api.domain.ApiReportStep;
import io.metersphere.api.mapper.ApiReportMapper;
import io.metersphere.api.mapper.ApiReportStepMapper;
import io.metersphere.api.mapper.ExtApiReportMapper;
import io.metersphere.sdk.util.SubListUtils;
import io.metersphere.system.dto.sdk.BasePageRequest;
import io.metersphere.system.service.UserLoginService;
import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service
@Transactional(rollbackFor = Exception.class)
public class ApiReportService {
@Resource
private SqlSessionFactory sqlSessionFactory;
@Resource
private ApiReportMapper apiReportMapper;
@Resource
private ExtApiReportMapper extApiReportMapper;
@Resource
private UserLoginService userLoginService;
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
public void insertApiReport(List<ApiReport> reports) {
if (CollectionUtils.isNotEmpty(reports)) {
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
ApiReportMapper reportMapper = sqlSession.getMapper(ApiReportMapper.class);
SubListUtils.dealForSubList(reports, 1000, subList -> {
subList.forEach(reportMapper::insertSelective);
sqlSession.flushStatements();
sqlSession.clearCache();
});
sqlSession.flushStatements();
if (sqlSessionFactory != null) {
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
}
}
}
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
public void insertApiReportStep(List<ApiReportStep> reportSteps) {
if (CollectionUtils.isNotEmpty(reportSteps)) {
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
ApiReportStepMapper stepMapper = sqlSession.getMapper(ApiReportStepMapper.class);
SubListUtils.dealForSubList(reportSteps, 1000, subList -> {
subList.forEach(stepMapper::insertSelective);
sqlSession.flushStatements();
sqlSession.clearCache();
});
sqlSession.flushStatements();
if (sqlSessionFactory != null) {
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
}
}
}
public List<ApiReport> getPage(BasePageRequest request, String projectId) {
List<ApiReport> list = extApiReportMapper.list(request, projectId);
//取所有的userid
Set<String> userSet = list.stream()
.flatMap(apiReport -> Stream.of(apiReport.getUpdateUser(), apiReport.getDeleteUser(), apiReport.getCreateUser()))
.collect(Collectors.toSet());
Map<String, String> userMap = userLoginService.getUserNameMap(new ArrayList<>(userSet));
list.forEach(apiReport -> {
apiReport.setCreateUser(userMap.get(apiReport.getCreateUser()));
apiReport.setUpdateUser(userMap.get(apiReport.getUpdateUser()));
apiReport.setDeleteUser(userMap.get(apiReport.getDeleteUser()));
});
return list;
}
}

View File

@ -50,7 +50,12 @@ public class ApiExecutionQueueService {
}
if (StringUtils.isNotBlank(queueDetail)) {
// 将节点重新放回列表尾部实现轮询
Long size = size(queueId);
if (size == null || size == 0) {
// 最后一个节点清理队列
redisTemplate.delete(queueKey);
redisTemplate.delete(QUEUE_PREFIX + queueId);
}
return JSON.parseObject(queueDetail, ExecutionQueueDetail.class);
}

View File

@ -0,0 +1,62 @@
package io.metersphere.api.service.scenario;
import io.metersphere.api.domain.ApiScenarioReport;
import io.metersphere.api.domain.ApiScenarioReportStep;
import io.metersphere.api.mapper.ApiScenarioReportMapper;
import io.metersphere.api.mapper.ApiScenarioReportStepMapper;
import io.metersphere.sdk.util.SubListUtils;
import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional(rollbackFor = Exception.class)
public class ApiScenarioReportService {
@Resource
private SqlSessionFactory sqlSessionFactory;
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
public void insertApiScenarioReport(List<ApiScenarioReport> reports) {
if (CollectionUtils.isNotEmpty(reports)) {
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
ApiScenarioReportMapper reportMapper = sqlSession.getMapper(ApiScenarioReportMapper.class);
SubListUtils.dealForSubList(reports, 1000, subList -> {
subList.forEach(reportMapper::insertSelective);
sqlSession.flushStatements();
sqlSession.clearCache();
});
sqlSession.flushStatements();
if (sqlSessionFactory != null) {
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
}
}
}
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
public void insertApiScenarioReportStep(List<ApiScenarioReportStep> reportSteps) {
if (CollectionUtils.isNotEmpty(reportSteps)) {
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
ApiScenarioReportStepMapper stepMapper = sqlSession.getMapper(ApiScenarioReportStepMapper.class);
SubListUtils.dealForSubList(reportSteps, 1000, subList -> {
subList.forEach(stepMapper::insertSelective);
sqlSession.flushStatements();
sqlSession.clearCache();
});
sqlSession.flushStatements();
if (sqlSessionFactory != null) {
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
}
}
}
}

View File

@ -114,7 +114,7 @@ public class ApiScenarioService {
private Set<String> extractUserIds(List<ApiScenarioDTO> list) {
return list.stream()
.flatMap(apiDefinition -> Stream.of(apiDefinition.getUpdateUser(), apiDefinition.getDeleteUser(), apiDefinition.getCreateUser()))
.flatMap(apiScenario -> Stream.of(apiScenario.getUpdateUser(), apiScenario.getDeleteUser(), apiScenario.getCreateUser()))
.collect(Collectors.toSet());
}

View File

@ -3,7 +3,7 @@
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration>
<!--配置数据库连接的位置-->
<properties url="file:///opt/metersphere/conf/metersphere-v3.properties"/>
<properties url="file:///opt/metersphere/conf/metersphere.properties"/>
<!-- 设置mysql驱动路径 -->
<!--<classPathEntry location="/Users/liuruibin/.m2/repository/mysql/mysql-connector-java/5.1.34/mysql-connector-java-5.1.34.jar"/>-->
<!-- 此处指定生成针对MyBatis3的DAO -->

View File

@ -0,0 +1,144 @@
package io.metersphere.api.controller;
import io.metersphere.api.domain.ApiReport;
import io.metersphere.api.domain.ApiReportExample;
import io.metersphere.api.domain.ApiReportStep;
import io.metersphere.api.domain.ApiReportStepExample;
import io.metersphere.api.dto.scenario.ApiScenarioDTO;
import io.metersphere.api.mapper.ApiReportMapper;
import io.metersphere.api.mapper.ApiReportStepMapper;
import io.metersphere.api.service.definition.ApiReportService;
import io.metersphere.sdk.constants.ApiReportStatus;
import io.metersphere.sdk.constants.SessionConstants;
import io.metersphere.sdk.util.JSON;
import io.metersphere.system.base.BaseTest;
import io.metersphere.system.controller.handler.ResultHolder;
import io.metersphere.system.dto.sdk.BasePageRequest;
import io.metersphere.system.utils.Pager;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.*;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@AutoConfigureMockMvc
public class ApiReportControllerTests extends BaseTest {
@Resource
private ApiReportService apiReportService;
@Resource
private ApiReportMapper apiReportMapper;
@Resource
private ApiReportStepMapper apiReportStepMapper;
@Test
@Order(1)
public void testInsert() {
List<ApiReport> reports = new ArrayList<>();
for (int i = 0; i < 2515; i++) {
ApiReport apiReport = new ApiReport();
apiReport.setId("api-report-id" + i);
apiReport.setProjectId(DEFAULT_PROJECT_ID);
apiReport.setName("api-report-name" + i);
apiReport.setStartTime(System.currentTimeMillis());
apiReport.setResourceId("api-resource-id" + i);
apiReport.setCreateUser("admin");
apiReport.setUpdateUser("admin");
apiReport.setUpdateTime(System.currentTimeMillis());
apiReport.setPoolId("api-pool-id" + i);
apiReport.setEnvironmentId("api-environment-id" + i);
apiReport.setRunMode("api-run-mode" + i);
if (i % 50 == 0) {
apiReport.setStatus(ApiReportStatus.SUCCESS.name());
} else if (i % 39 == 0) {
apiReport.setStatus(ApiReportStatus.ERROR.name());
}
apiReport.setTriggerMode("api-trigger-mode" + i);
apiReport.setVersionId("api-version-id" + i);
reports.add(apiReport);
}
apiReportService.insertApiReport(reports);
List<ApiReport> reports1 = apiReportMapper.selectByExample(new ApiReportExample());
Assertions.assertEquals(reports1.size(), 2515);
List<ApiReportStep> steps = new ArrayList<>();
for (int i = 0; i < 1515; i++) {
ApiReportStep apiReportStep = new ApiReportStep();
apiReportStep.setStepId("api-report-step-id" + i);
apiReportStep.setReportId("api-report-id" + i);
apiReportStep.setSort(0L);
apiReportStep.setStepType("case");
steps.add(apiReportStep);
}
apiReportService.insertApiReportStep(steps);
List<ApiReportStep> steps1 = apiReportStepMapper.selectByExample(new ApiReportStepExample());
Assertions.assertEquals(steps1.size(), 1515);
}
private MvcResult responsePost(String url, Object param) throws Exception {
return mockMvc.perform(MockMvcRequestBuilders.post(url)
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken)
.header(SessionConstants.CURRENT_PROJECT, DEFAULT_PROJECT_ID)
.content(JSON.toJSONString(param))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn();
}
public static <T> T parseObjectFromMvcResult(MvcResult mvcResult, Class<T> parseClass) {
try {
String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class);
//返回请求正常
Assertions.assertNotNull(resultHolder);
return JSON.parseObject(JSON.toJSONString(resultHolder.getData()), parseClass);
} catch (Exception ignore) {
}
return null;
}
@Test
@Order(2)
public void testGetPage() throws Exception {
BasePageRequest request = new BasePageRequest();
request.setCurrent(1);
request.setPageSize(10);
MvcResult mvcResult = responsePost("/api/report/case/page", request);
Pager<?> returnPager = parseObjectFromMvcResult(mvcResult, Pager.class);
//返回值不为空
Assertions.assertNotNull(returnPager);
//返回值的页码和当前页码相同
Assertions.assertEquals(returnPager.getCurrent(), request.getCurrent());
;
//返回的数据量不超过规定要返回的数据量相同
Assertions.assertTrue(((List<ApiScenarioDTO>) returnPager.getList()).size() <= request.getPageSize());
//过滤
request.setFilter(new HashMap<>() {{
put("status", List.of("SUCCESS", "ERROR"));
}});
mvcResult = responsePost("/api/report/case/page", request);
returnPager = parseObjectFromMvcResult(mvcResult, Pager.class);
//返回值不为空
Assertions.assertNotNull(returnPager);
Assertions.assertTrue(((List<ApiReport>) returnPager.getList()).size() <= request.getPageSize());
List<ApiReport> list = JSON.parseArray(JSON.toJSONString(returnPager.getList()), ApiReport.class);
list.forEach(apiReport -> {
Assertions.assertTrue(apiReport.getStatus().equals("SUCCESS") || apiReport.getStatus().equals("ERROR"));
});
}
}

View File

@ -0,0 +1,70 @@
package io.metersphere.api.controller;
import io.metersphere.api.domain.ApiScenarioReport;
import io.metersphere.api.domain.ApiScenarioReportExample;
import io.metersphere.api.domain.ApiScenarioReportStep;
import io.metersphere.api.domain.ApiScenarioReportStepExample;
import io.metersphere.api.mapper.ApiScenarioReportMapper;
import io.metersphere.api.mapper.ApiScenarioReportStepMapper;
import io.metersphere.api.service.scenario.ApiScenarioReportService;
import io.metersphere.system.base.BaseTest;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.*;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@AutoConfigureMockMvc
public class ApiScenarioReportControllerTests extends BaseTest {
@Resource
private ApiScenarioReportService apiScenarioReportService;
@Resource
private ApiScenarioReportMapper apiScenarioReportMapper;
@Resource
private ApiScenarioReportStepMapper apiScenarioReportStepMapper;
@Test
@Order(1)
public void testInsert() {
List<ApiScenarioReport> reports = new ArrayList<>();
for (int i = 0; i < 2515; i++) {
ApiScenarioReport scenarioReport = new ApiScenarioReport();
scenarioReport.setId("api-report-id" + i);
scenarioReport.setProjectId(DEFAULT_PROJECT_ID);
scenarioReport.setName("api-report-name" + i);
scenarioReport.setStartTime(System.currentTimeMillis());
scenarioReport.setScenarioId("api-scenario-id" + i);
scenarioReport.setCreateUser("admin");
scenarioReport.setUpdateUser("admin");
scenarioReport.setUpdateTime(System.currentTimeMillis());
scenarioReport.setPoolId("api-pool-id" + i);
scenarioReport.setEnvironmentId("api-environment-id" + i);
scenarioReport.setRunMode("api-run-mode" + i);
scenarioReport.setTriggerMode("api-trigger-mode" + i);
scenarioReport.setVersionId("api-version-id" + i);
reports.add(scenarioReport);
}
apiScenarioReportService.insertApiScenarioReport(reports);
List<ApiScenarioReport> reports1 = apiScenarioReportMapper.selectByExample(new ApiScenarioReportExample());
Assertions.assertEquals(reports1.size(), 2515);
List<ApiScenarioReportStep> steps = new ArrayList<>();
for (int i = 0; i < 1515; i++) {
ApiScenarioReportStep apiScenarioReportStep = new ApiScenarioReportStep();
apiScenarioReportStep.setStepId("api-report-step-id" + i);
apiScenarioReportStep.setReportId("api-report-id" + i);
apiScenarioReportStep.setSort(0L);
apiScenarioReportStep.setStepType("case");
steps.add(apiScenarioReportStep);
}
apiScenarioReportService.insertApiScenarioReportStep(steps);
List<ApiScenarioReportStep> steps1 = apiScenarioReportStepMapper.selectByExample(new ApiScenarioReportStepExample());
Assertions.assertEquals(steps1.size(), 1515);
}
}