feat: 接口测试模块-首页开发Service方法

接口测试模块-首页开发,包含接口、用例、场景、定时任务、失败案例和运行中任务等功能的开发
This commit is contained in:
song.tianyang 2020-12-17 17:35:27 +08:00
parent e05657fe11
commit 7707f9902d
10 changed files with 379 additions and 157 deletions

View File

@ -4,17 +4,17 @@ import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.metersphere.api.dto.*;
import io.metersphere.api.dto.scenario.request.dubbo.RegistryCenter;
import io.metersphere.api.service.APITestService;
import io.metersphere.api.service.*;
import io.metersphere.base.domain.ApiTest;
import io.metersphere.base.domain.Schedule;
import io.metersphere.commons.constants.RoleConstants;
import io.metersphere.commons.utils.PageUtils;
import io.metersphere.commons.utils.Pager;
import io.metersphere.commons.utils.SessionUtils;
import io.metersphere.commons.constants.ScheduleGroup;
import io.metersphere.commons.utils.*;
import io.metersphere.controller.request.QueryScheduleRequest;
import io.metersphere.dto.ScheduleDao;
import io.metersphere.service.CheckOwnerService;
import io.metersphere.service.ScheduleService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;
@ -22,6 +22,8 @@ import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
@ -35,7 +37,21 @@ public class APITestController {
@Resource
private APITestService apiTestService;
@Resource
private ApiDefinitionService apiDefinitionService;
@Resource
private CheckOwnerService checkownerService;
@Resource
private ApiTestCaseService apiTestCaseService;
@Resource
private ApiDefinitionExecResultService apiDefinitionExecResultService;
@Resource
private ApiAutomationService apiAutomationService;
@Resource
private ApiScenarioReportService apiScenarioReportService;
@Resource
private ScheduleService scheduleService;
@Resource
private APIReportService apiReportService;
@GetMapping("recent/{count}")
public List<APITestResult> recentTest(@PathVariable int count) {

View File

@ -0,0 +1,18 @@
package io.metersphere.api.dto.dataCount;
import lombok.Getter;
import lombok.Setter;
/**
* 已执行的案例
*/
@Getter
@Setter
public class ExecutedCaseInfoResult {
//案例名称
private String caseName;
//所属测试计划
private String testPlan;
//失败次数
private Long failureTimes;
}

View File

@ -0,0 +1,16 @@
package io.metersphere.api.dto.dataCount.request;
import lombok.Getter;
import lombok.Setter;
/**
* @author song.tianyang
* @Date 2020/12/17 5:04 下午
* @Description
*/
@Getter
@Setter
public class ScheduleInfoRequest {
private String taskID;
private boolean enable;
}

View File

@ -0,0 +1,20 @@
package io.metersphere.api.dto.dataCount.response;
import lombok.Getter;
import lombok.Setter;
/**
* 已执行的案例
*/
@Getter
@Setter
public class ExecutedCaseInfoDTO {
//排名
private int sortIndex;
//案例名称
private String caseName;
//所属测试计划
private String testPlan;
//失败次数
private Long failureTimes;
}

View File

@ -15,6 +15,7 @@ import io.metersphere.base.mapper.ext.ExtApiTestReportMapper;
import io.metersphere.commons.constants.APITestStatus;
import io.metersphere.commons.constants.ReportTriggerMode;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.DateUtils;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.ServiceUtils;
import io.metersphere.commons.utils.SessionUtils;
@ -30,10 +31,7 @@ import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.*;
@Service
@Transactional(rollbackFor = Exception.class)
@ -212,4 +210,21 @@ public class APIReportService {
apiTestReportExample.createCriteria().andIdIn(reportRequest.getIds());
apiTestReportMapper.deleteByExample(apiTestReportExample);
}
public long countByWorkspaceIdAndGroupAndCreateInThisWeek(String workspaceID, String group) {
Map<String, Date> startAndEndDateInWeek = DateUtils.getWeedFirstTimeAndLastTime(new Date());
Date firstTime = startAndEndDateInWeek.get("firstTime");
Date lastTime = startAndEndDateInWeek.get("lastTime");
if(firstTime==null || lastTime == null){
return 0;
}else {
return apiTestReportMapper.countByProjectIDAndCreateInThisWeek(workspaceID,group,firstTime.getTime(),lastTime.getTime());
}
}
public long countByWorkspaceIdAndGroup(String workspaceID, String group) {
return apiTestReportMapper.countByWorkspaceIdAndGroup(workspaceID,group);
}
}

View File

@ -4,6 +4,8 @@ import com.alibaba.fastjson.JSON;
import io.metersphere.api.jmeter.TestResult;
import io.metersphere.base.domain.ApiDefinitionExecResult;
import io.metersphere.base.mapper.ApiDefinitionExecResultMapper;
import io.metersphere.base.mapper.ext.ExtApiDefinitionExecResultMapper;
import io.metersphere.commons.utils.DateUtils;
import io.metersphere.commons.utils.SessionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -17,9 +19,14 @@ import java.util.UUID;
public class ApiDefinitionExecResultService {
@Resource
private ApiDefinitionExecResultMapper apiDefinitionExecResultMapper;
@Resource
private ExtApiDefinitionExecResultMapper extApiDefinitionExecResultMapper;
public void saveApiResult(TestResult result) {
result.getScenarios().get(0).getRequestResults().forEach(item -> {
// 清理原始资源每个执行 保留一条结果
extApiDefinitionExecResultMapper.deleteByResourceId(item.getName());
ApiDefinitionExecResult saveResult = new ApiDefinitionExecResult();
saveResult.setId(UUID.randomUUID().toString());
saveResult.setUserId(Objects.requireNonNull(SessionUtils.getUser()).getId());

View File

@ -4,6 +4,7 @@ import io.metersphere.base.domain.ApiTestReport;
import io.metersphere.base.domain.ApiTestReportExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
public interface ApiTestReportMapper {
long countByExample(ApiTestReportExample example);
@ -27,4 +28,21 @@ public interface ApiTestReportMapper {
int updateByPrimaryKeySelective(ApiTestReport record);
int updateByPrimaryKey(ApiTestReport record);
@Select({
"SELECT COUNT(testReportDetail.report_id) AS countNumber FROM api_test_report_detail testReportDetail ",
"INNER JOIN `schedule` sch ON sch.resource_id = testReportDetail.test_id ",
"INNER JOIN api_test_report testReport ON testReportDetail.report_id = testReport.id ",
"WHERE workspace_id = #{workspaceID} AND `group` = #{group} ",
})
long countByWorkspaceIdAndGroup(@Param("workspaceID") String workspaceID, @Param("group")String group);
@Select({
"SELECT COUNT(testReportDetail.report_id) AS countNumber FROM api_test_report_detail testReportDetail ",
"INNER JOIN `schedule` sch ON sch.resource_id = testReportDetail.test_id ",
"INNER JOIN api_test_report testReport ON testReportDetail.report_id = testReport.id ",
"WHERE workspace_id = #{workspaceID} AND `group` = #{group} ",
"AND testReport.create_time BETWEEN #{startTime} and #{endTime} ",
})
long countByProjectIDAndCreateInThisWeek(@Param("workspaceID") String workspaceID, @Param("group")String group, @Param("startTime") long startTime, @Param("endTime")long endTime);
}

View File

@ -1,9 +1,13 @@
package io.metersphere.base.mapper;
import io.metersphere.api.dto.dataCount.response.TaskInfoResult;
import io.metersphere.base.domain.Schedule;
import io.metersphere.base.domain.ScheduleExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.python.antlr.ast.Str;
public interface ScheduleMapper {
long countByExample(ScheduleExample example);
@ -33,4 +37,25 @@ public interface ScheduleMapper {
int updateByPrimaryKeyWithBLOBs(Schedule record);
int updateByPrimaryKey(Schedule record);
@Select("SELECT COUNT(id) AS countNumber FROM `schedule` WHERE `workspace_id` = #{workspaceId} AND `group` = #{group} ")
long countTaskByWorkspaceIdAndGroup(@Param("workspaceId") String workspaceId,@Param("group") String group);
@Select({
"SELECT COUNT(id) AS countNumber FROM `schedule` ",
"WHERE workspace_id = #{workspaceId} ",
"AND `group` = #{group} ",
"AND create_time BETWEEN #{startTime} and #{endTime}; "
})
long countTaskByWorkspaceIdAndGroupAndCreateTimeRange(@Param("workspaceId")String workspaceId,@Param("group") String group, @Param("startTime") long startTime, @Param("endTime") long endTime);
@Select({
"SELECT apiTest.`name` AS scenario,sch.id AS taskID,sch.`value` AS rule,sch.`enable` AS `taskStatus`,u.`name` AS creator,sch.update_time AS updateTime ",
"FROM api_test apiTest ",
"INNER JOIN `schedule` sch ON apiTest.id = sch.resource_id ",
"INNER JOIN `user` u ON u.id = sch.user_id ",
"WHERE sch.`enable` = true AND sch.workspace_id = #{0,jdbcType=VARCHAR}"
})
List<TaskInfoResult> findRunningTaskInfoByWorkspaceID(String workspaceID);
}

View File

@ -0,0 +1,62 @@
package io.metersphere.commons.utils;
import org.quartz.CronExpression;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.TriggerBuilder;
import java.util.Date;
/**
* @author song.tianyang
* @Date 2020/12/17 4:06 下午
* @Description CRON解析类
*/
public class CronUtils {
/**
* 解析表达式获取CronTrigger
* @param cron
* @return
*/
public static CronTrigger getCronTrigger(String cron) {
if (!CronExpression.isValidExpression(cron)) {
throw new RuntimeException("cron :" + cron + "表达式解析错误");
}
return TriggerBuilder.newTrigger().withIdentity("Caclulate Date").withSchedule(CronScheduleBuilder.cronSchedule(cron)).build();
}
/**
* 获取以指定时间为开始时间的下一次执行时间
* @param cron
* @param start
* @return
*/
public static Date getNextTriggerTime(String cron, Date start) {
if (start == null) {
return getNextTriggerTime(cron);
}else{
CronTrigger trigger = getCronTrigger(cron);
return trigger.getFireTimeAfter(start);
}
}
/**
* 获取以当前日期为准的下一次执行时间
* @param cron
* @return
*/
public static Date getNextTriggerTime(String cron) {
Date date = null;
try{
CronTrigger trigger = getCronTrigger(cron);
Date startDate = trigger.getStartTime();
date = trigger.getFireTimeAfter(startDate);
}catch (Exception e){
}
return date;
}
}

View File

@ -1,6 +1,7 @@
package io.metersphere.service;
import com.alibaba.fastjson.JSON;
import io.metersphere.api.dto.dataCount.response.TaskInfoResult;
import io.metersphere.base.domain.Schedule;
import io.metersphere.base.domain.ScheduleExample;
import io.metersphere.base.domain.User;
@ -9,6 +10,7 @@ import io.metersphere.base.mapper.ScheduleMapper;
import io.metersphere.base.mapper.UserMapper;
import io.metersphere.base.mapper.ext.ExtScheduleMapper;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.DateUtils;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.ServiceUtils;
import io.metersphere.commons.utils.SessionUtils;
@ -25,6 +27,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@ -162,4 +165,26 @@ public class ScheduleService {
schedule.setUserName(userMap.get(schedule.getUserId()));
});
}
public long countTaskByWorkspaceIdAndGroup(String workspaceId,String group) {
return scheduleMapper.countTaskByWorkspaceIdAndGroup(workspaceId,group);
}
public long countTaskByWorkspaceIdAndGroupInThisWeek(String workspaceID, String group) {
Map<String, Date> startAndEndDateInWeek = DateUtils.getWeedFirstTimeAndLastTime(new Date());
Date firstTime = startAndEndDateInWeek.get("firstTime");
Date lastTime = startAndEndDateInWeek.get("lastTime");
if(firstTime==null || lastTime == null){
return 0;
}else {
return scheduleMapper.countTaskByWorkspaceIdAndGroupAndCreateTimeRange(workspaceID,group,firstTime.getTime(),lastTime.getTime());
}
}
public List<TaskInfoResult> findRunningTaskInfoByWorkspaceID(String workspaceID) {
List<TaskInfoResult> runningTaskInfoList = scheduleMapper.findRunningTaskInfoByWorkspaceID(workspaceID);
return runningTaskInfoList;
}
}