提交 a59c40b8 authored 作者: Matrix's avatar Matrix

feat(SQL): 增加了测试数据源连接的接口已经获得当前数据源的service

上级 3407e9eb
package org.matrix.actuators.datasource;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Set;
/**
......@@ -11,6 +13,19 @@ import java.util.Set;
*/
public interface IDataSourceService {
/**
* 获得当前线程正在使用的数据源
* @return 当前线程正在使用的数据源
*/
DataSource peek();
/**
* 用于测试是否可以连接该目标数据源
* @param dataSourceDTO 用于连接目标数据源的参数对象
* @return 是否可以连通该数据源
*/
boolean testConnection(DataSourceDTO dataSourceDTO) throws SQLException;
/**
* 切换回主数据源
......
......@@ -4,12 +4,14 @@ import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Set;
/**
......@@ -19,6 +21,7 @@ import java.util.Set;
* @since 2022/1/19 at 5:47 PM
* Suffering is the most powerful teacher of life.
*/
@Slf4j
@Service
public class IDataSourceServiceImpl implements IDataSourceService {
......@@ -30,6 +33,39 @@ public class IDataSourceServiceImpl implements IDataSourceService {
this.dataSourceCreator = dataSourceCreator;
}
/**
* 获得当前线程正在使用的数据源
*
* @return 当前线程正在使用的数据源
*/
@Override
public DataSource peek() {
String dsName = DynamicDataSourceContextHolder.peek();
DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
return ds.getDataSources().get(dsName);
}
/**
* 用于测试是否可以连接该目标数据源
*
* @param dataSourceDTO 用于连接目标数据源的参数对象
* @return 是否可以连通该数据源
*/
@Override
public boolean testConnection(DataSourceDTO dataSourceDTO) throws SQLException {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String url = dataSourceDTO.getUrl();
String username = dataSourceDTO.getUsername();
String password = dataSourceDTO.getPassword();
Connection conn = null;
DriverManager.getConnection(url, username, password);
return true;
}
/**
* 切换回主数据源
*
......
......@@ -31,6 +31,7 @@ import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import javax.management.StringValueExp;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.*;
......@@ -133,6 +134,10 @@ public class CaseActuator implements Actuator {
LogQueueRuntime.setTestData(testData.getId());
//try catch 一下避免发生错误后导致循环进行不下去
try {
for (int i = 0; i < 20 ; i++) {
LogQueueRuntime.addNewLog(getClass(),CASE_ACTUATOR, String.valueOf(i));
Thread.sleep(1000L);
}
changeExecutionHistoryStatus(ExecutionHistoryStatus.RUN);
LogQueueRuntime.addNewLog(this.getClass(), CASE_ACTUATOR, "[用例执行器] 开始执行数据组ID:" + testData.getId());
HttpResponseDetail baseTestCaseResponseDetail = getHttpResponseDetail(
......
......@@ -4,6 +4,8 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.matrix.actuators.datasource.IDataSourceService;
import org.matrix.autotest.utils.PageTools;
import org.matrix.database.entity.Connect;
import org.matrix.database.service.IConnectService;
......@@ -13,8 +15,12 @@ import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.sql.SQLException;
import java.util.Optional;
import static org.springframework.http.HttpStatus.NOT_IMPLEMENTED;
import static org.springframework.http.HttpStatus.NO_CONTENT;
/**
* <p>
* 前端控制器
......@@ -23,6 +29,7 @@ import java.util.Optional;
* @author mry
* @since 2022-01-07
*/
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/connects")
......@@ -31,8 +38,31 @@ public class ConnectController {
private final IConnectService connectService;
public ConnectController(IConnectService connectService) {
private final IDataSourceService dataSourceService;
public ConnectController(IConnectService connectService, IDataSourceService dataSourceService) {
this.connectService = connectService;
this.dataSourceService = dataSourceService;
}
/**
* 测试数据库连接
*
* @param connect 数据库连接参数对象
* @return 返回连接信息, 如果失败会返回具体错误信息
*/
@ApiOperation("测试数据库连接")
@PostMapping("/test")
public ResponseEntity<String> testConnect(@RequestBody Connect connect) {
try {
dataSourceService.testConnection(connect.toDataSourceDTO());
} catch (SQLException e) {
e.printStackTrace();
log.warn("[数据库] 连接目标数据源数据库失败,目标数据源数据 = {}", connect);
return ResponseEntity.status(503).body(String.format("连接失败,SQL状态 = %s 异常信息 = %s ", e.getSQLState(), e.getMessage()));
}
return ResponseEntity.ok("连接成功!");
}
/**
......
......@@ -13,6 +13,8 @@ import org.matrix.database.vo.CommonResult;
import org.matrix.database.vo.CommonResultObj;
import org.matrix.database.vo.MoveAction;
import org.matrix.exception.GlobalException;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
......@@ -47,6 +49,16 @@ public class MoveController {
this.actionService = actionService;
}
@GetMapping
@Cacheable(cacheNames = "moveNames", key = "#p0", condition = "#p0 != null ", unless = "#result.statusCodeValue != 200")
public ResponseEntity<CommonResultObj<String>> findNameById(@RequestParam Integer moveId) {
String name = ofNullable(moveService.getById(moveId))
.orElseThrow(() -> new GlobalException(String.format("没有找到id = %d 的move对象名称", moveId)))
.getName();
return CommonResult.success(name, "查询成功");
}
/**
* 分页查询所有行为
*
......@@ -147,6 +159,7 @@ public class MoveController {
* @param moveAction 行为以及行为下的动作
* @return {@link MoveAction}
*/
@CacheEvict(cacheNames = "moveNames", key = "#p0.move.id")
@ApiOperation(value = "修改行为以及动作")
@PutMapping
@Transactional(rollbackFor = Exception.class)
......@@ -173,6 +186,7 @@ public class MoveController {
* @param moveId 行为id
* @return 是否删除成功
*/
@CacheEvict(cacheNames = "moveNames", key = "#p0")
@ApiOperation(value = "删除行为和动作")
@DeleteMapping("/{moveId}")
@Transactional(rollbackFor = Exception.class)
......
......@@ -62,7 +62,7 @@ public class SwaggerController {
}
reader.close();
} catch (IOException e) {
throw new GlobalException(String.format("请求swagger数据失败,您读的swagger地址ip为 %d", url));
throw new GlobalException(String.format("请求swagger数据失败,您读的swagger地址ip为 %s", url));
}
return json.toString();
}
......
......@@ -64,7 +64,7 @@ public class TestCaseController {
@GetMapping("/{projectId}")
@ApiOperation(value = "分页查询用例")
@Cacheable(cacheNames = "casePageCache",
key = "#pageNum + '_' + #pageSize",
key = "#pageNum + '_' + #pageSize + '_' + #projectId ",
condition = "#pageSize != null && #pageNum !=null",
unless = "#result.statusCodeValue != 200")
public ResponseEntity<CommonResultObj<Page<TestCase>>> findPageTestCase(
......@@ -73,11 +73,11 @@ public class TestCaseController {
String name, @PathVariable Long projectId) {
Page<TestCase> results = Optional.ofNullable(testCaseService.page(Page.of(pageNum, pageSize)
, Wrappers.lambdaQuery(TestCase.class).eq(TestCase::getProjectId, projectId)
.like(StringUtils.hasLength(name)
, TestCase::getName, name))).orElse(new Page<>());
// TODO 暂时注解掉,关于页码超出的范围要额外处理
// PageTools.pageTool(pageSize, pageNum, results);
, Wrappers.lambdaQuery(TestCase.class)
.eq(TestCase::getProjectId, projectId)
.like(StringUtils.hasLength(name), TestCase::getName, name)))
.orElse(new Page<>());
PageTools.pageTool(pageSize, pageNum, results);
return CommonResult.success(results, "查询成功");
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论