Forráskód Böngészése

feat: 添加自动化测试

zhaoxingrui 5 éve
szülő
commit
0e920fdd64

+ 33 - 0
web/src/main/java/cn/seecoder/web/controller/auto_test/AutoTestController.java

@@ -0,0 +1,33 @@
+package cn.seecoder.web.controller.auto_test;
+
+import cn.seecoder.web.model.vo.APITest.APITestResultVO;
+import cn.seecoder.web.model.vo.Response;
+import cn.seecoder.web.service.auto_test.AutoTestService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiImplicitParam;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.List;
+
+@Api(tags = "自动化测试 API")
+@RestController
+@RequestMapping("/auto_test")
+public class AutoTestController {
+
+    private final AutoTestService autoTestService;
+
+    @Autowired
+    public AutoTestController(AutoTestService autoTestService) { this.autoTestService = autoTestService; }
+
+    @GetMapping("/list/{projectId}")
+    @ApiOperation(value = "获取指定项目的所有黑盒测试结果", httpMethod = "GET")
+    @ApiImplicitParam(name = "projectId", dataType = "int", paramType = "query")
+    public Response<List<APITestResultVO>> getAutoTestResultByProjectId(@PathVariable("projectId") Integer projectId) {
+        return Response.buildSuccess(autoTestService.getAutoTestResultByProjectId(projectId));
+    }
+}

+ 6 - 2
web/src/main/java/cn/seecoder/web/dao/APITest/APITestMapper.java

@@ -1,5 +1,6 @@
 package cn.seecoder.web.dao.APITest;
 
+import cn.seecoder.web.model.vo.APITest.APITestResultVO;
 import org.apache.ibatis.annotations.*;
 import cn.seecoder.web.dao.provider.GeneralInsertUpdateSqlProvider;
 import cn.seecoder.web.model.po.APITest.TestInfo;
@@ -33,12 +34,15 @@ public interface APITestMapper {
     @Options(useGeneratedKeys = true, keyProperty = "param1.id")
     boolean execute(TestResult testResult, String... ignoredCols);
 
-    @Select("select * from test_result where test_id = #{id}")
+    @Select("select * from test_result where test_id = #{id} and pipeline_record_id = -1")
     List<TestResult> selectTestResultByTestId(Integer id);
 
     @Delete("delete from test_result where test_id = #{id}")
     boolean deleteTestResult(Integer id);
 
-    @Select("select * from test_result where test_id = #{id} order by id DESC LIMIT 0,1")
+    @Select("select * from test_result where test_id = #{id} and pipeline_record_id = -1 order by id DESC LIMIT 0,1")
     TestResult selectLatestTestResultById(Integer id);
+
+    @Select("select * from test_result where pipeline_record_id = #{recordId}")
+    List<APITestResultVO> selectByPipelineRecordId(Integer recordId);
 }

+ 1 - 1
web/src/main/java/cn/seecoder/web/model/po/APITest/TestResult.java

@@ -28,5 +28,5 @@ public class TestResult {
     /**
      * 关联的构建历史信息id, 若没有相关部署信息传入值-1
      */
-    private Integer pipelineHistoryId;
+    private Integer pipelineRecordId;
 }

+ 4 - 0
web/src/main/java/cn/seecoder/web/model/vo/APITest/APITestResultVO.java

@@ -53,4 +53,8 @@ public class APITestResultVO {
     public APITestResultVO(APITestResult apiTestResult) {
         BeanUtils.copyProperties(apiTestResult, this);
     }
+
+//    public APITestResultVO() {
+//
+//    }
 }

+ 3 - 0
web/src/main/java/cn/seecoder/web/model/vo/APITest/TestResultVO.java

@@ -46,6 +46,9 @@ public class TestResultVO {
     @ApiModelProperty("执行时间平均值")
     private Double averageTime;
 
+    @ApiModelProperty("关联的构建历史信息id")
+    private Integer pipelineRecordId;
+
     public TestResultVO(TestResult testResult) {
         BeanUtils.copyProperties(testResult, this);
     }

+ 9 - 0
web/src/main/java/cn/seecoder/web/service/auto_test/AutoTestService.java

@@ -0,0 +1,9 @@
+package cn.seecoder.web.service.auto_test;
+
+import cn.seecoder.web.model.vo.APITest.APITestResultVO;
+
+import java.util.List;
+
+public interface AutoTestService {
+    List<APITestResultVO> getAutoTestResultByProjectId(Integer projectId);
+}

+ 4 - 1
web/src/main/java/cn/seecoder/web/service/impl/APITest/APITestServiceImpl.java

@@ -113,6 +113,9 @@ public class APITestServiceImpl implements APITestService {
     @Override
     public APITestResultVO getLatestTestResultByTestId(Integer testId) {
         TestResult testResult = apiTestMapper.selectLatestTestResultById(testId);
+        if (testResult == null) {
+            return null;
+        }
         return new APITestResultVO(transform1(testResult));
     }
     // 将JSON格式的字符串转为 Map类型
@@ -239,7 +242,7 @@ public class APITestServiceImpl implements APITestService {
                 .maxTime(max_time)
                 .minTime(min_time)
                 .averageTime((Double.valueOf(df.format(sum_time / qps))))
-                .pipelineHistoryId(latestRecord==null? -1:latestRecord.getId())
+                .pipelineRecordId(latestRecord==null? -1:latestRecord.getId())
                 .build();
         apiTestMapper.execute(testResult);
     }

+ 50 - 0
web/src/main/java/cn/seecoder/web/service/impl/auto_test/AutoTestServiceImpl.java

@@ -0,0 +1,50 @@
+package cn.seecoder.web.service.impl.auto_test;
+
+import cn.seecoder.web.dao.APITest.APITestMapper;
+import cn.seecoder.web.dao.pipeline.PipelineMapper;
+import cn.seecoder.web.dao.pipeline.PipelineRecordMapper;
+import cn.seecoder.web.model.po.pipeline.PipelinePO;
+import cn.seecoder.web.model.po.pipeline.PipelineRecordPO;
+import cn.seecoder.web.model.vo.APITest.APITestResultVO;
+import cn.seecoder.web.service.auto_test.AutoTestService;
+import org.springframework.beans.BeanUtils;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+
+@Service
+public class AutoTestServiceImpl implements AutoTestService {
+
+    private final PipelineMapper pipelineMapper;
+
+    private final PipelineRecordMapper pipelineRecordMapper;
+
+    private final APITestMapper apiTestMapper;
+
+    public AutoTestServiceImpl(PipelineMapper pipelineMapper, PipelineRecordMapper pipelineRecordMapper, APITestMapper apiTestMapper) {
+        this.pipelineMapper = pipelineMapper;
+        this.pipelineRecordMapper = pipelineRecordMapper;
+        this.apiTestMapper = apiTestMapper;
+    }
+
+    @Override
+    public List<APITestResultVO> getAutoTestResultByProjectId(Integer projectId) {
+        List<Integer> pipelineIds = pipelineMapper.selectByProjectId(projectId).stream().map(PipelinePO::getId).collect(Collectors.toList());
+        if (pipelineIds.size() == 0) {
+            return new ArrayList<>();
+        }
+        PipelineRecordPO latestRecord = pipelineRecordMapper.selectPipelinesLatestRecord(pipelineIds);
+        if (latestRecord == null) {
+            return new ArrayList<>();
+        }
+        return apiTestMapper.selectByPipelineRecordId(latestRecord.getId()).stream()
+                .map(x->{
+                    APITestResultVO vo = new APITestResultVO();
+                    BeanUtils.copyProperties(x, vo);
+                    return vo;
+                })
+                .collect(Collectors.toList());
+    }
+}