diff --git a/backend/framework/domain/src/main/java/io/metersphere/api/domain/ApiReportStep.java b/backend/framework/domain/src/main/java/io/metersphere/api/domain/ApiReportStep.java index f6d8782bd1..1a4cdb2b51 100644 --- a/backend/framework/domain/src/main/java/io/metersphere/api/domain/ApiReportStep.java +++ b/backend/framework/domain/src/main/java/io/metersphere/api/domain/ApiReportStep.java @@ -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), diff --git a/backend/framework/domain/src/main/java/io/metersphere/api/domain/ApiReportStepExample.java b/backend/framework/domain/src/main/java/io/metersphere/api/domain/ApiReportStepExample.java index af777979e3..b54e22b3e7 100644 --- a/backend/framework/domain/src/main/java/io/metersphere/api/domain/ApiReportStepExample.java +++ b/backend/framework/domain/src/main/java/io/metersphere/api/domain/ApiReportStepExample.java @@ -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 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 values) { addCriterion("step_id in", values, "stepId"); return (Criteria) this; } - public Criteria andStepIdNotIn(List values) { + public Criteria andStepIdNotIn(List 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; } diff --git a/backend/framework/domain/src/main/java/io/metersphere/api/mapper/ApiReportStepMapper.java b/backend/framework/domain/src/main/java/io/metersphere/api/mapper/ApiReportStepMapper.java index ef592387d2..45f9de2651 100644 --- a/backend/framework/domain/src/main/java/io/metersphere/api/mapper/ApiReportStepMapper.java +++ b/backend/framework/domain/src/main/java/io/metersphere/api/mapper/ApiReportStepMapper.java @@ -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 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); diff --git a/backend/framework/domain/src/main/java/io/metersphere/api/mapper/ApiReportStepMapper.xml b/backend/framework/domain/src/main/java/io/metersphere/api/mapper/ApiReportStepMapper.xml index aebecb548b..b63a3f302b 100644 --- a/backend/framework/domain/src/main/java/io/metersphere/api/mapper/ApiReportStepMapper.xml +++ b/backend/framework/domain/src/main/java/io/metersphere/api/mapper/ApiReportStepMapper.xml @@ -2,7 +2,7 @@ - + @@ -87,12 +87,12 @@ select from api_report_step - where step_id = #{stepId,jdbcType=BIGINT} + where step_id = #{stepId,jdbcType=VARCHAR} and report_id = #{reportId,jdbcType=VARCHAR} delete from api_report_step - where step_id = #{stepId,jdbcType=BIGINT} + where step_id = #{stepId,jdbcType=VARCHAR} and report_id = #{reportId,jdbcType=VARCHAR} @@ -104,7 +104,7 @@ 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}) @@ -128,7 +128,7 @@ - #{stepId,jdbcType=BIGINT}, + #{stepId,jdbcType=VARCHAR}, #{reportId,jdbcType=VARCHAR}, @@ -154,7 +154,7 @@ update api_report_step - step_id = #{record.stepId,jdbcType=BIGINT}, + step_id = #{record.stepId,jdbcType=VARCHAR}, report_id = #{record.reportId,jdbcType=VARCHAR}, @@ -175,7 +175,7 @@ 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}, - where step_id = #{stepId,jdbcType=BIGINT} + where step_id = #{stepId,jdbcType=VARCHAR} and report_id = #{reportId,jdbcType=VARCHAR} @@ -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} @@ -213,7 +213,7 @@ (step_id, report_id, `name`, sort, step_type) values - (#{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}) @@ -228,7 +228,7 @@ ( - #{item.stepId,jdbcType=BIGINT} + #{item.stepId,jdbcType=VARCHAR} #{item.reportId,jdbcType=VARCHAR} diff --git a/backend/framework/domain/src/main/resources/migration/3.0.0/ddl/V3.0.0_5__api_test.sql b/backend/framework/domain/src/main/resources/migration/3.0.0/ddl/V3.0.0_5__api_test.sql index 33c121568f..bd7e23bd56 100644 --- a/backend/framework/domain/src/main/resources/migration/3.0.0/ddl/V3.0.0_5__api_test.sql +++ b/backend/framework/domain/src/main/resources/migration/3.0.0/ddl/V3.0.0_5__api_test.sql @@ -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 '序号' , diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiReportController.java b/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiReportController.java new file mode 100644 index 0000000000..3ba4cdf485 --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiReportController.java @@ -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> getPage(@Validated @RequestBody BasePageRequest request) { + Page 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())); + } + +} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/mapper/ExtApiReportMapper.java b/backend/services/api-test/src/main/java/io/metersphere/api/mapper/ExtApiReportMapper.java new file mode 100644 index 0000000000..237c609c91 --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/mapper/ExtApiReportMapper.java @@ -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 list(@Param("request") BasePageRequest request, @Param("projectId") String projectId); + +} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/mapper/ExtApiReportMapper.xml b/backend/services/api-test/src/main/java/io/metersphere/api/mapper/ExtApiReportMapper.xml new file mode 100644 index 0000000000..87e6b1fb2b --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/mapper/ExtApiReportMapper.xml @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + and api_report.integrated in + + + + and api_report.status in + + + + + + + + + diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiReportService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiReportService.java new file mode 100644 index 0000000000..e841b31046 --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiReportService.java @@ -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 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 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 getPage(BasePageRequest request, String projectId) { + List list = extApiReportMapper.list(request, projectId); + //取所有的userid + Set userSet = list.stream() + .flatMap(apiReport -> Stream.of(apiReport.getUpdateUser(), apiReport.getDeleteUser(), apiReport.getCreateUser())) + .collect(Collectors.toSet()); + Map 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; + } +} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/queue/ApiExecutionQueueService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/queue/ApiExecutionQueueService.java index aacab7ca00..fac7b54ab7 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/queue/ApiExecutionQueueService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/queue/ApiExecutionQueueService.java @@ -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); } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioReportService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioReportService.java new file mode 100644 index 0000000000..0157d3249c --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioReportService.java @@ -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 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 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); + } + } + } + + +} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java index 3aba1d302b..4167c3e80f 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/scenario/ApiScenarioService.java @@ -114,7 +114,7 @@ public class ApiScenarioService { private Set extractUserIds(List 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()); } diff --git a/backend/services/api-test/src/main/resources/apiGeneratorConfig.xml b/backend/services/api-test/src/main/resources/apiGeneratorConfig.xml index 6151913ebf..aaad18e831 100644 --- a/backend/services/api-test/src/main/resources/apiGeneratorConfig.xml +++ b/backend/services/api-test/src/main/resources/apiGeneratorConfig.xml @@ -3,7 +3,7 @@ "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" > - + diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiReportControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiReportControllerTests.java new file mode 100644 index 0000000000..5809995038 --- /dev/null +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiReportControllerTests.java @@ -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 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 reports1 = apiReportMapper.selectByExample(new ApiReportExample()); + Assertions.assertEquals(reports1.size(), 2515); + + List 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 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 parseObjectFromMvcResult(MvcResult mvcResult, Class 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) 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) returnPager.getList()).size() <= request.getPageSize()); + List list = JSON.parseArray(JSON.toJSONString(returnPager.getList()), ApiReport.class); + list.forEach(apiReport -> { + Assertions.assertTrue(apiReport.getStatus().equals("SUCCESS") || apiReport.getStatus().equals("ERROR")); + }); + + } + +} diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioReportControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioReportControllerTests.java new file mode 100644 index 0000000000..220085f6bd --- /dev/null +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioReportControllerTests.java @@ -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 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 reports1 = apiScenarioReportMapper.selectByExample(new ApiScenarioReportExample()); + Assertions.assertEquals(reports1.size(), 2515); + + List 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 steps1 = apiScenarioReportStepMapper.selectByExample(new ApiScenarioReportStepExample()); + Assertions.assertEquals(steps1.size(), 1515); + } + +}