Browse Source

[Improvement] Dataquality code style enhance (#14592)

* code style enhance

* update

---------

Co-authored-by: xiangzihao <460888207@qq.com>
3.2.1-prepare
旺阳 12 months ago committed by GitHub
parent
commit
3d033da55d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 105
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java
  2. 21
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java
  3. 114
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java
  4. 69
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java

105
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java

@ -35,8 +35,10 @@ import org.apache.dolphinscheduler.api.aspect.AccessLogAnnotation;
import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.exceptions.ApiException; import org.apache.dolphinscheduler.api.exceptions.ApiException;
import org.apache.dolphinscheduler.api.service.DataSourceService; import org.apache.dolphinscheduler.api.service.DataSourceService;
import org.apache.dolphinscheduler.api.utils.PageInfo;
import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.constants.Constants; import org.apache.dolphinscheduler.common.constants.Constants;
import org.apache.dolphinscheduler.dao.entity.DataSource;
import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.plugin.datasource.api.datasource.BaseDataSourceParamDTO; import org.apache.dolphinscheduler.plugin.datasource.api.datasource.BaseDataSourceParamDTO;
import org.apache.dolphinscheduler.plugin.datasource.api.utils.CommonUtils; import org.apache.dolphinscheduler.plugin.datasource.api.utils.CommonUtils;
@ -44,8 +46,9 @@ import org.apache.dolphinscheduler.plugin.datasource.api.utils.DataSourceUtils;
import org.apache.dolphinscheduler.plugin.task.api.utils.ParameterUtils; import org.apache.dolphinscheduler.plugin.task.api.utils.ParameterUtils;
import org.apache.dolphinscheduler.spi.datasource.ConnectionParam; import org.apache.dolphinscheduler.spi.datasource.ConnectionParam;
import org.apache.dolphinscheduler.spi.enums.DbType; import org.apache.dolphinscheduler.spi.enums.DbType;
import org.apache.dolphinscheduler.spi.params.base.ParamsOptions;
import java.util.Map; import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
@ -91,8 +94,8 @@ public class DataSourceController extends BaseController {
@ResponseStatus(HttpStatus.CREATED) @ResponseStatus(HttpStatus.CREATED)
@ApiException(CREATE_DATASOURCE_ERROR) @ApiException(CREATE_DATASOURCE_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser") @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result createDataSource(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, public Result<Object> createDataSource(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@Parameter(name = "dataSourceParam", description = "DATA_SOURCE_PARAM", required = true) @RequestBody String jsonStr) { @Parameter(name = "dataSourceParam", description = "DATA_SOURCE_PARAM", required = true) @RequestBody String jsonStr) {
BaseDataSourceParamDTO dataSourceParam = DataSourceUtils.buildDatasourceParam(jsonStr); BaseDataSourceParamDTO dataSourceParam = DataSourceUtils.buildDatasourceParam(jsonStr);
return dataSourceService.createDataSource(loginUser, dataSourceParam); return dataSourceService.createDataSource(loginUser, dataSourceParam);
} }
@ -115,9 +118,9 @@ public class DataSourceController extends BaseController {
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
@ApiException(UPDATE_DATASOURCE_ERROR) @ApiException(UPDATE_DATASOURCE_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser") @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result updateDataSource(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, public Result<Object> updateDataSource(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@PathVariable(value = "id") Integer id, @PathVariable(value = "id") Integer id,
@RequestBody String jsonStr) { @RequestBody String jsonStr) {
BaseDataSourceParamDTO dataSourceParam = DataSourceUtils.buildDatasourceParam(jsonStr); BaseDataSourceParamDTO dataSourceParam = DataSourceUtils.buildDatasourceParam(jsonStr);
dataSourceParam.setId(id); dataSourceParam.setId(id);
return dataSourceService.updateDataSource(dataSourceParam.getId(), loginUser, dataSourceParam); return dataSourceService.updateDataSource(dataSourceParam.getId(), loginUser, dataSourceParam);
@ -139,11 +142,10 @@ public class DataSourceController extends BaseController {
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
@ApiException(QUERY_DATASOURCE_ERROR) @ApiException(QUERY_DATASOURCE_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser") @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result queryDataSource(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, public Result<Object> queryDataSource(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@PathVariable("id") int id) { @PathVariable("id") int id) {
BaseDataSourceParamDTO dataSource = dataSourceService.queryDataSource(id, loginUser);
Map<String, Object> result = dataSourceService.queryDataSource(id, loginUser); return Result.success(dataSource);
return returnDataList(result);
} }
/** /**
@ -161,10 +163,10 @@ public class DataSourceController extends BaseController {
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
@ApiException(QUERY_DATASOURCE_ERROR) @ApiException(QUERY_DATASOURCE_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser") @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result queryDataSourceList(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, public Result<Object> queryDataSourceList(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestParam("type") DbType type) { @RequestParam("type") DbType type) {
Map<String, Object> result = dataSourceService.queryDataSourceList(loginUser, type.ordinal()); List<DataSource> datasourceList = dataSourceService.queryDataSourceList(loginUser, type.ordinal());
return returnDataList(result); return Result.success(datasourceList);
} }
/** /**
@ -186,16 +188,18 @@ public class DataSourceController extends BaseController {
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
@ApiException(QUERY_DATASOURCE_ERROR) @ApiException(QUERY_DATASOURCE_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser") @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result queryDataSourceListPaging(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, public Result<Object> queryDataSourceListPaging(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestParam(value = "searchVal", required = false) String searchVal, @RequestParam(value = "searchVal", required = false) String searchVal,
@RequestParam("pageNo") Integer pageNo, @RequestParam("pageNo") Integer pageNo,
@RequestParam("pageSize") Integer pageSize) { @RequestParam("pageSize") Integer pageSize) {
Result result = checkPageParams(pageNo, pageSize); Result<Object> result = checkPageParams(pageNo, pageSize);
if (!result.checkResult()) { if (!result.checkResult()) {
return result; return result;
} }
searchVal = ParameterUtils.handleEscapes(searchVal); searchVal = ParameterUtils.handleEscapes(searchVal);
return dataSourceService.queryDataSourceListPaging(loginUser, searchVal, pageNo, pageSize); PageInfo<DataSource> pageInfo =
dataSourceService.queryDataSourceListPaging(loginUser, searchVal, pageNo, pageSize);
return Result.success(pageInfo);
} }
/** /**
@ -211,8 +215,8 @@ public class DataSourceController extends BaseController {
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
@ApiException(CONNECT_DATASOURCE_FAILURE) @ApiException(CONNECT_DATASOURCE_FAILURE)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser") @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result connectDataSource(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, public Result<Object> connectDataSource(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "dataSourceParam") @RequestBody String jsonStr) { @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "dataSourceParam") @RequestBody String jsonStr) {
BaseDataSourceParamDTO dataSourceParam = DataSourceUtils.buildDatasourceParam(jsonStr); BaseDataSourceParamDTO dataSourceParam = DataSourceUtils.buildDatasourceParam(jsonStr);
DataSourceUtils.checkDatasourceParam(dataSourceParam); DataSourceUtils.checkDatasourceParam(dataSourceParam);
ConnectionParam connectionParams = DataSourceUtils.buildConnectionParams(dataSourceParam); ConnectionParam connectionParams = DataSourceUtils.buildConnectionParams(dataSourceParam);
@ -234,8 +238,8 @@ public class DataSourceController extends BaseController {
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
@ApiException(CONNECTION_TEST_FAILURE) @ApiException(CONNECTION_TEST_FAILURE)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser") @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result connectionTest(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, public Result<Object> connectionTest(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@PathVariable("id") int id) { @PathVariable("id") int id) {
return dataSourceService.connectionTest(id); return dataSourceService.connectionTest(id);
} }
@ -254,8 +258,8 @@ public class DataSourceController extends BaseController {
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
@ApiException(DELETE_DATA_SOURCE_FAILURE) @ApiException(DELETE_DATA_SOURCE_FAILURE)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser") @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result deleteDataSource(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, public Result<Object> deleteDataSource(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@PathVariable("id") int id) { @PathVariable("id") int id) {
return dataSourceService.delete(loginUser, id); return dataSourceService.delete(loginUser, id);
} }
@ -274,8 +278,8 @@ public class DataSourceController extends BaseController {
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
@ApiException(VERIFY_DATASOURCE_NAME_FAILURE) @ApiException(VERIFY_DATASOURCE_NAME_FAILURE)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser") @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result verifyDataSourceName(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, public Result<Object> verifyDataSourceName(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestParam(value = "name") String name) { @RequestParam(value = "name") String name) {
return dataSourceService.verifyDataSourceName(name); return dataSourceService.verifyDataSourceName(name);
} }
@ -294,11 +298,11 @@ public class DataSourceController extends BaseController {
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
@ApiException(UNAUTHORIZED_DATASOURCE) @ApiException(UNAUTHORIZED_DATASOURCE)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser") @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result unauthDatasource(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, public Result<Object> unAuthDatasource(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestParam("userId") Integer userId) { @RequestParam("userId") Integer userId) {
Map<String, Object> result = dataSourceService.unauthDatasource(loginUser, userId); List<DataSource> unAuthDatasourceList = dataSourceService.unAuthDatasource(loginUser, userId);
return returnDataList(result); return Result.success(unAuthDatasourceList);
} }
/** /**
@ -316,11 +320,10 @@ public class DataSourceController extends BaseController {
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
@ApiException(AUTHORIZED_DATA_SOURCE) @ApiException(AUTHORIZED_DATA_SOURCE)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser") @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result authedDatasource(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, public Result<Object> authedDatasource(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestParam("userId") Integer userId) { @RequestParam("userId") Integer userId) {
List<DataSource> authedDatasourceList = dataSourceService.authedDatasource(loginUser, userId);
Map<String, Object> result = dataSourceService.authedDatasource(loginUser, userId); return Result.success(authedDatasourceList);
return returnDataList(result);
} }
/** /**
@ -334,7 +337,7 @@ public class DataSourceController extends BaseController {
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
@ApiException(KERBEROS_STARTUP_STATE) @ApiException(KERBEROS_STARTUP_STATE)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser") @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result getKerberosStartupState(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { public Result<Object> getKerberosStartupState(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) {
// if upload resource is HDFS and kerberos startup is true , else false // if upload resource is HDFS and kerberos startup is true , else false
return success(Status.SUCCESS.getMsg(), CommonUtils.getKerberosStartupState()); return success(Status.SUCCESS.getMsg(), CommonUtils.getKerberosStartupState());
} }
@ -347,10 +350,10 @@ public class DataSourceController extends BaseController {
@GetMapping(value = "/tables") @GetMapping(value = "/tables")
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
@ApiException(GET_DATASOURCE_TABLES_ERROR) @ApiException(GET_DATASOURCE_TABLES_ERROR)
public Result getTables(@RequestParam("datasourceId") Integer datasourceId, public Result<Object> getTables(@RequestParam("datasourceId") Integer datasourceId,
@RequestParam(value = "database") String database) { @RequestParam(value = "database") String database) {
Map<String, Object> result = dataSourceService.getTables(datasourceId, database); List<ParamsOptions> options = dataSourceService.getTables(datasourceId, database);
return returnDataList(result); return Result.success(options);
} }
@Operation(summary = "tableColumns", description = "GET_DATASOURCE_TABLE_COLUMNS_NOTES") @Operation(summary = "tableColumns", description = "GET_DATASOURCE_TABLE_COLUMNS_NOTES")
@ -362,11 +365,11 @@ public class DataSourceController extends BaseController {
@GetMapping(value = "/tableColumns") @GetMapping(value = "/tableColumns")
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
@ApiException(GET_DATASOURCE_TABLE_COLUMNS_ERROR) @ApiException(GET_DATASOURCE_TABLE_COLUMNS_ERROR)
public Result getTableColumns(@RequestParam("datasourceId") Integer datasourceId, public Result<Object> getTableColumns(@RequestParam("datasourceId") Integer datasourceId,
@RequestParam("tableName") String tableName, @RequestParam("tableName") String tableName,
@RequestParam(value = "database") String database) { @RequestParam(value = "database") String database) {
Map<String, Object> result = dataSourceService.getTableColumns(datasourceId, database, tableName); List<ParamsOptions> options = dataSourceService.getTableColumns(datasourceId, database, tableName);
return returnDataList(result); return Result.success(options);
} }
@Operation(summary = "databases", description = "GET_DATASOURCE_DATABASE_NOTES") @Operation(summary = "databases", description = "GET_DATASOURCE_DATABASE_NOTES")
@ -376,8 +379,8 @@ public class DataSourceController extends BaseController {
@GetMapping(value = "/databases") @GetMapping(value = "/databases")
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
@ApiException(GET_DATASOURCE_DATABASES_ERROR) @ApiException(GET_DATASOURCE_DATABASES_ERROR)
public Result getDatabases(@RequestParam("datasourceId") Integer datasourceId) { public Result<Object> getDatabases(@RequestParam("datasourceId") Integer datasourceId) {
Map<String, Object> result = dataSourceService.getDatabases(datasourceId); List<ParamsOptions> options = dataSourceService.getDatabases(datasourceId);
return returnDataList(result); return Result.success(options);
} }
} }

21
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java

@ -17,13 +17,16 @@
package org.apache.dolphinscheduler.api.service; package org.apache.dolphinscheduler.api.service;
import org.apache.dolphinscheduler.api.utils.PageInfo;
import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.dao.entity.DataSource;
import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.plugin.datasource.api.datasource.BaseDataSourceParamDTO; import org.apache.dolphinscheduler.plugin.datasource.api.datasource.BaseDataSourceParamDTO;
import org.apache.dolphinscheduler.spi.datasource.ConnectionParam; import org.apache.dolphinscheduler.spi.datasource.ConnectionParam;
import org.apache.dolphinscheduler.spi.enums.DbType; import org.apache.dolphinscheduler.spi.enums.DbType;
import org.apache.dolphinscheduler.spi.params.base.ParamsOptions;
import java.util.Map; import java.util.List;
/** /**
* data source service * data source service
@ -55,7 +58,7 @@ public interface DataSourceService {
* @param id datasource id * @param id datasource id
* @return data source detail * @return data source detail
*/ */
Map<String, Object> queryDataSource(int id, User loginUser); BaseDataSourceParamDTO queryDataSource(int id, User loginUser);
/** /**
* query datasource list by keyword * query datasource list by keyword
@ -66,7 +69,7 @@ public interface DataSourceService {
* @param pageSize page size * @param pageSize page size
* @return data source list page * @return data source list page
*/ */
Result queryDataSourceListPaging(User loginUser, String searchVal, Integer pageNo, Integer pageSize); PageInfo<DataSource> queryDataSourceListPaging(User loginUser, String searchVal, Integer pageNo, Integer pageSize);
/** /**
* query data resource list * query data resource list
@ -75,7 +78,7 @@ public interface DataSourceService {
* @param type data source type * @param type data source type
* @return data source list page * @return data source list page
*/ */
Map<String, Object> queryDataSourceList(User loginUser, Integer type); List<DataSource> queryDataSourceList(User loginUser, Integer type);
/** /**
* verify datasource exists * verify datasource exists
@ -118,7 +121,7 @@ public interface DataSourceService {
* @param userId user id * @param userId user id
* @return unauthed data source result code * @return unauthed data source result code
*/ */
Map<String, Object> unauthDatasource(User loginUser, Integer userId); List<DataSource> unAuthDatasource(User loginUser, Integer userId);
/** /**
* authorized datasource * authorized datasource
@ -127,7 +130,7 @@ public interface DataSourceService {
* @param userId user id * @param userId user id
* @return authorized result code * @return authorized result code
*/ */
Map<String, Object> authedDatasource(User loginUser, Integer userId); List<DataSource> authedDatasource(User loginUser, Integer userId);
/** /**
* get tables * get tables
@ -135,7 +138,7 @@ public interface DataSourceService {
* @param database * @param database
* @return * @return
*/ */
Map<String, Object> getTables(Integer datasourceId, String database); List<ParamsOptions> getTables(Integer datasourceId, String database);
/** /**
* get table columns * get table columns
@ -144,12 +147,12 @@ public interface DataSourceService {
* @param tableName * @param tableName
* @return * @return
*/ */
Map<String, Object> getTableColumns(Integer datasourceId, String database, String tableName); List<ParamsOptions> getTableColumns(Integer datasourceId, String database, String tableName);
/** /**
* get databases * get databases
* @param datasourceId * @param datasourceId
* @return * @return
*/ */
Map<String, Object> getDatabases(Integer datasourceId); List<ParamsOptions> getDatabases(Integer datasourceId);
} }

114
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java

@ -53,10 +53,8 @@ import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -228,20 +226,16 @@ public class DataSourceServiceImpl extends BaseServiceImpl implements DataSource
* @return data source detail * @return data source detail
*/ */
@Override @Override
public Map<String, Object> queryDataSource(int id, User loginUser) { public BaseDataSourceParamDTO queryDataSource(int id, User loginUser) {
Map<String, Object> result = new HashMap<>();
DataSource dataSource = dataSourceMapper.selectById(id); DataSource dataSource = dataSourceMapper.selectById(id);
if (dataSource == null) { if (dataSource == null) {
log.error("Datasource does not exist, id:{}.", id); log.error("Datasource does not exist, id:{}.", id);
putMsg(result, Status.RESOURCE_NOT_EXIST); throw new ServiceException(Status.RESOURCE_NOT_EXIST);
return result;
} }
if (!canOperatorPermissions(loginUser, new Object[]{dataSource.getId()}, AuthorizationType.DATASOURCE, if (!canOperatorPermissions(loginUser, new Object[]{dataSource.getId()}, AuthorizationType.DATASOURCE,
ApiFuncIdentificationConstant.DATASOURCE)) { ApiFuncIdentificationConstant.DATASOURCE)) {
putMsg(result, Status.USER_NO_OPERATION_PERM); throw new ServiceException(Status.USER_NO_OPERATION_PERM);
return result;
} }
// type // type
@ -251,9 +245,7 @@ public class DataSourceServiceImpl extends BaseServiceImpl implements DataSource
baseDataSourceParamDTO.setName(dataSource.getName()); baseDataSourceParamDTO.setName(dataSource.getName());
baseDataSourceParamDTO.setNote(dataSource.getNote()); baseDataSourceParamDTO.setNote(dataSource.getNote());
result.put(Constants.DATA_LIST, baseDataSourceParamDTO); return baseDataSourceParamDTO;
putMsg(result, Status.SUCCESS);
return result;
} }
/** /**
@ -266,8 +258,8 @@ public class DataSourceServiceImpl extends BaseServiceImpl implements DataSource
* @return data source list page * @return data source list page
*/ */
@Override @Override
public Result queryDataSourceListPaging(User loginUser, String searchVal, Integer pageNo, Integer pageSize) { public PageInfo<DataSource> queryDataSourceListPaging(User loginUser, String searchVal, Integer pageNo,
Result result = new Result(); Integer pageSize) {
IPage<DataSource> dataSourceList = null; IPage<DataSource> dataSourceList = null;
Page<DataSource> dataSourcePage = new Page<>(pageNo, pageSize); Page<DataSource> dataSourcePage = new Page<>(pageNo, pageSize);
PageInfo<DataSource> pageInfo = new PageInfo<>(pageNo, pageSize); PageInfo<DataSource> pageInfo = new PageInfo<>(pageNo, pageSize);
@ -277,9 +269,7 @@ public class DataSourceServiceImpl extends BaseServiceImpl implements DataSource
Set<Integer> ids = resourcePermissionCheckService Set<Integer> ids = resourcePermissionCheckService
.userOwnedResourceIdsAcquisition(AuthorizationType.DATASOURCE, loginUser.getId(), log); .userOwnedResourceIdsAcquisition(AuthorizationType.DATASOURCE, loginUser.getId(), log);
if (ids.isEmpty()) { if (ids.isEmpty()) {
result.setData(pageInfo); return pageInfo;
putMsg(result, Status.SUCCESS);
return result;
} }
dataSourceList = dataSourceMapper.selectPagingByIds(dataSourcePage, new ArrayList<>(ids), searchVal); dataSourceList = dataSourceMapper.selectPagingByIds(dataSourcePage, new ArrayList<>(ids), searchVal);
} }
@ -288,9 +278,7 @@ public class DataSourceServiceImpl extends BaseServiceImpl implements DataSource
handlePasswd(dataSources); handlePasswd(dataSources);
pageInfo.setTotal((int) (dataSourceList != null ? dataSourceList.getTotal() : 0L)); pageInfo.setTotal((int) (dataSourceList != null ? dataSourceList.getTotal() : 0L));
pageInfo.setTotalList(dataSources); pageInfo.setTotalList(dataSources);
result.setData(pageInfo); return pageInfo;
putMsg(result, Status.SUCCESS);
return result;
} }
/** /**
@ -322,8 +310,7 @@ public class DataSourceServiceImpl extends BaseServiceImpl implements DataSource
* @return data source list page * @return data source list page
*/ */
@Override @Override
public Map<String, Object> queryDataSourceList(User loginUser, Integer type) { public List<DataSource> queryDataSourceList(User loginUser, Integer type) {
Map<String, Object> result = new HashMap<>();
List<DataSource> datasourceList = null; List<DataSource> datasourceList = null;
if (loginUser.getUserType().equals(UserType.ADMIN_USER)) { if (loginUser.getUserType().equals(UserType.ADMIN_USER)) {
@ -332,16 +319,13 @@ public class DataSourceServiceImpl extends BaseServiceImpl implements DataSource
Set<Integer> ids = resourcePermissionCheckService Set<Integer> ids = resourcePermissionCheckService
.userOwnedResourceIdsAcquisition(AuthorizationType.DATASOURCE, loginUser.getId(), log); .userOwnedResourceIdsAcquisition(AuthorizationType.DATASOURCE, loginUser.getId(), log);
if (ids.isEmpty()) { if (ids.isEmpty()) {
result.put(Constants.DATA_LIST, Collections.emptyList()); return Collections.emptyList();
putMsg(result, Status.SUCCESS);
return result;
} }
datasourceList = dataSourceMapper.selectBatchIds(ids).stream() datasourceList = dataSourceMapper.selectBatchIds(ids).stream()
.filter(dataSource -> dataSource.getType().getCode() == type).collect(Collectors.toList()); .filter(dataSource -> dataSource.getType().getCode() == type).collect(Collectors.toList());
} }
result.put(Constants.DATA_LIST, datasourceList);
putMsg(result, Status.SUCCESS); return datasourceList;
return result;
} }
/** /**
@ -468,8 +452,7 @@ public class DataSourceServiceImpl extends BaseServiceImpl implements DataSource
* @return unauthed data source result code * @return unauthed data source result code
*/ */
@Override @Override
public Map<String, Object> unauthDatasource(User loginUser, Integer userId) { public List<DataSource> unAuthDatasource(User loginUser, Integer userId) {
Map<String, Object> result = new HashMap<>();
List<DataSource> datasourceList; List<DataSource> datasourceList;
if (canOperatorPermissions(loginUser, null, AuthorizationType.DATASOURCE, null)) { if (canOperatorPermissions(loginUser, null, AuthorizationType.DATASOURCE, null)) {
// admin gets all data sources except userId // admin gets all data sources except userId
@ -492,9 +475,7 @@ public class DataSourceServiceImpl extends BaseServiceImpl implements DataSource
} }
resultList = new ArrayList<>(datasourceSet); resultList = new ArrayList<>(datasourceSet);
} }
result.put(Constants.DATA_LIST, resultList); return resultList;
putMsg(result, Status.SUCCESS);
return result;
} }
/** /**
@ -505,19 +486,13 @@ public class DataSourceServiceImpl extends BaseServiceImpl implements DataSource
* @return authorized result code * @return authorized result code
*/ */
@Override @Override
public Map<String, Object> authedDatasource(User loginUser, Integer userId) { public List<DataSource> authedDatasource(User loginUser, Integer userId) {
Map<String, Object> result = new HashMap<>();
List<DataSource> authedDatasourceList = dataSourceMapper.queryAuthedDatasource(userId); List<DataSource> authedDatasourceList = dataSourceMapper.queryAuthedDatasource(userId);
result.put(Constants.DATA_LIST, authedDatasourceList); return authedDatasourceList;
putMsg(result, Status.SUCCESS);
return result;
} }
@Override @Override
public Map<String, Object> getTables(Integer datasourceId, String database) { public List<ParamsOptions> getTables(Integer datasourceId, String database) {
Map<String, Object> result = new HashMap<>();
DataSource dataSource = dataSourceMapper.selectById(datasourceId); DataSource dataSource = dataSourceMapper.selectById(datasourceId);
List<String> tableList = null; List<String> tableList = null;
@ -527,8 +502,7 @@ public class DataSourceServiceImpl extends BaseServiceImpl implements DataSource
dataSource.getConnectionParams()); dataSource.getConnectionParams());
if (null == connectionParam) { if (null == connectionParam) {
putMsg(result, Status.DATASOURCE_CONNECT_FAILED); throw new ServiceException(Status.DATASOURCE_CONNECT_FAILED);
return result;
} }
Connection connection = Connection connection =
@ -538,8 +512,7 @@ public class DataSourceServiceImpl extends BaseServiceImpl implements DataSource
try { try {
if (null == connection) { if (null == connection) {
putMsg(result, Status.DATASOURCE_CONNECT_FAILED); throw new ServiceException(Status.DATASOURCE_CONNECT_FAILED);
return result;
} }
DatabaseMetaData metaData = connection.getMetaData(); DatabaseMetaData metaData = connection.getMetaData();
@ -548,6 +521,7 @@ public class DataSourceServiceImpl extends BaseServiceImpl implements DataSource
schema = metaData.getConnection().getSchema(); schema = metaData.getConnection().getSchema();
} catch (SQLException e) { } catch (SQLException e) {
log.error("Cant not get the schema, datasourceId:{}.", datasourceId, e); log.error("Cant not get the schema, datasourceId:{}.", datasourceId, e);
throw new ServiceException(Status.GET_DATASOURCE_TABLES_ERROR);
} }
tables = metaData.getTables( tables = metaData.getTables(
@ -556,8 +530,7 @@ public class DataSourceServiceImpl extends BaseServiceImpl implements DataSource
"%", TABLE_TYPES); "%", TABLE_TYPES);
if (null == tables) { if (null == tables) {
log.error("Get datasource tables error, datasourceId:{}.", datasourceId); log.error("Get datasource tables error, datasourceId:{}.", datasourceId);
putMsg(result, Status.GET_DATASOURCE_TABLES_ERROR); throw new ServiceException(Status.GET_DATASOURCE_TABLES_ERROR);
return result;
} }
tableList = new ArrayList<>(); tableList = new ArrayList<>();
@ -568,24 +541,18 @@ public class DataSourceServiceImpl extends BaseServiceImpl implements DataSource
} catch (Exception e) { } catch (Exception e) {
log.error("Get datasource tables error, datasourceId:{}.", datasourceId, e); log.error("Get datasource tables error, datasourceId:{}.", datasourceId, e);
putMsg(result, Status.GET_DATASOURCE_TABLES_ERROR); throw new ServiceException(Status.GET_DATASOURCE_TABLES_ERROR);
return result;
} finally { } finally {
closeResult(tables); closeResult(tables);
releaseConnection(connection); releaseConnection(connection);
} }
List<ParamsOptions> options = getParamsOptions(tableList); List<ParamsOptions> options = getParamsOptions(tableList);
return options;
result.put(Constants.DATA_LIST, options);
putMsg(result, Status.SUCCESS);
return result;
} }
@Override @Override
public Map<String, Object> getTableColumns(Integer datasourceId, String database, String tableName) { public List<ParamsOptions> getTableColumns(Integer datasourceId, String database, String tableName) {
Map<String, Object> result = new HashMap<>();
DataSource dataSource = dataSourceMapper.selectById(datasourceId); DataSource dataSource = dataSourceMapper.selectById(datasourceId);
BaseConnectionParam connectionParam = BaseConnectionParam connectionParam =
(BaseConnectionParam) DataSourceUtils.buildConnectionParams( (BaseConnectionParam) DataSourceUtils.buildConnectionParams(
@ -593,8 +560,7 @@ public class DataSourceServiceImpl extends BaseServiceImpl implements DataSource
dataSource.getConnectionParams()); dataSource.getConnectionParams());
if (null == connectionParam) { if (null == connectionParam) {
putMsg(result, Status.DATASOURCE_CONNECT_FAILED); throw new ServiceException(Status.DATASOURCE_CONNECT_FAILED);
return result;
} }
Connection connection = Connection connection =
@ -604,7 +570,7 @@ public class DataSourceServiceImpl extends BaseServiceImpl implements DataSource
try { try {
if (null == connection) { if (null == connection) {
return result; throw new ServiceException(Status.DATASOURCE_CONNECT_FAILED);
} }
DatabaseMetaData metaData = connection.getMetaData(); DatabaseMetaData metaData = connection.getMetaData();
@ -614,34 +580,30 @@ public class DataSourceServiceImpl extends BaseServiceImpl implements DataSource
} }
rs = metaData.getColumns(database, null, tableName, "%"); rs = metaData.getColumns(database, null, tableName, "%");
if (rs == null) { if (rs == null) {
return result; throw new ServiceException(Status.DATASOURCE_CONNECT_FAILED);
} }
while (rs.next()) { while (rs.next()) {
columnList.add(rs.getString(COLUMN_NAME)); columnList.add(rs.getString(COLUMN_NAME));
} }
} catch (Exception e) { } catch (Exception e) {
log.error("Get datasource table columns error, datasourceId:{}.", dataSource.getId(), e); log.error("Get datasource table columns error, datasourceId:{}.", dataSource.getId(), e);
throw new ServiceException(Status.DATASOURCE_CONNECT_FAILED);
} finally { } finally {
closeResult(rs); closeResult(rs);
releaseConnection(connection); releaseConnection(connection);
} }
List<ParamsOptions> options = getParamsOptions(columnList); List<ParamsOptions> options = getParamsOptions(columnList);
return options;
result.put(Constants.DATA_LIST, options);
putMsg(result, Status.SUCCESS);
return result;
} }
@Override @Override
public Map<String, Object> getDatabases(Integer datasourceId) { public List<ParamsOptions> getDatabases(Integer datasourceId) {
Map<String, Object> result = new HashMap<>();
DataSource dataSource = dataSourceMapper.selectById(datasourceId); DataSource dataSource = dataSourceMapper.selectById(datasourceId);
if (dataSource == null) { if (dataSource == null) {
putMsg(result, Status.QUERY_DATASOURCE_ERROR); throw new ServiceException(Status.QUERY_DATASOURCE_ERROR);
return result;
} }
List<String> tableList; List<String> tableList;
@ -651,8 +613,7 @@ public class DataSourceServiceImpl extends BaseServiceImpl implements DataSource
dataSource.getConnectionParams()); dataSource.getConnectionParams());
if (null == connectionParam) { if (null == connectionParam) {
putMsg(result, Status.DATASOURCE_CONNECT_FAILED); throw new ServiceException(Status.DATASOURCE_CONNECT_FAILED);
return result;
} }
Connection connection = Connection connection =
@ -661,8 +622,7 @@ public class DataSourceServiceImpl extends BaseServiceImpl implements DataSource
try { try {
if (null == connection) { if (null == connection) {
putMsg(result, Status.DATASOURCE_CONNECT_FAILED); throw new ServiceException(Status.DATASOURCE_CONNECT_FAILED);
return result;
} }
if (dataSource.getType() == DbType.POSTGRESQL) { if (dataSource.getType() == DbType.POSTGRESQL) {
rs = connection.createStatement().executeQuery(Constants.DATABASES_QUERY_PG); rs = connection.createStatement().executeQuery(Constants.DATABASES_QUERY_PG);
@ -676,18 +636,14 @@ public class DataSourceServiceImpl extends BaseServiceImpl implements DataSource
} }
} catch (Exception e) { } catch (Exception e) {
log.error("Get databases error, datasourceId:{}.", datasourceId, e); log.error("Get databases error, datasourceId:{}.", datasourceId, e);
putMsg(result, Status.GET_DATASOURCE_TABLES_ERROR); throw new ServiceException(Status.GET_DATASOURCE_TABLES_ERROR);
return result;
} finally { } finally {
closeResult(rs); closeResult(rs);
releaseConnection(connection); releaseConnection(connection);
} }
List<ParamsOptions> options = getParamsOptions(tableList); List<ParamsOptions> options = getParamsOptions(tableList);
return options;
result.put(Constants.DATA_LIST, options);
putMsg(result, Status.SUCCESS);
return result;
} }
private List<ParamsOptions> getParamsOptions(List<String> columnList) { private List<ParamsOptions> getParamsOptions(List<String> columnList) {

69
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java

@ -23,8 +23,8 @@ import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.permission.ResourcePermissionCheckService; import org.apache.dolphinscheduler.api.permission.ResourcePermissionCheckService;
import org.apache.dolphinscheduler.api.service.impl.BaseServiceImpl; import org.apache.dolphinscheduler.api.service.impl.BaseServiceImpl;
import org.apache.dolphinscheduler.api.service.impl.DataSourceServiceImpl; import org.apache.dolphinscheduler.api.service.impl.DataSourceServiceImpl;
import org.apache.dolphinscheduler.api.utils.PageInfo;
import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.constants.Constants;
import org.apache.dolphinscheduler.common.constants.DataSourceConstants; import org.apache.dolphinscheduler.common.constants.DataSourceConstants;
import org.apache.dolphinscheduler.common.enums.AuthorizationType; import org.apache.dolphinscheduler.common.enums.AuthorizationType;
import org.apache.dolphinscheduler.common.enums.UserType; import org.apache.dolphinscheduler.common.enums.UserType;
@ -34,6 +34,7 @@ import org.apache.dolphinscheduler.dao.entity.DataSource;
import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.dao.mapper.DataSourceMapper; import org.apache.dolphinscheduler.dao.mapper.DataSourceMapper;
import org.apache.dolphinscheduler.dao.mapper.DataSourceUserMapper; import org.apache.dolphinscheduler.dao.mapper.DataSourceUserMapper;
import org.apache.dolphinscheduler.plugin.datasource.api.datasource.BaseDataSourceParamDTO;
import org.apache.dolphinscheduler.plugin.datasource.api.plugin.DataSourceClientProvider; import org.apache.dolphinscheduler.plugin.datasource.api.plugin.DataSourceClientProvider;
import org.apache.dolphinscheduler.plugin.datasource.api.utils.CommonUtils; import org.apache.dolphinscheduler.plugin.datasource.api.utils.CommonUtils;
import org.apache.dolphinscheduler.plugin.datasource.api.utils.DataSourceUtils; import org.apache.dolphinscheduler.plugin.datasource.api.utils.DataSourceUtils;
@ -216,8 +217,9 @@ public class DataSourceServiceTest {
int pageNo = 1; int pageNo = 1;
int pageSize = 10; int pageSize = 10;
Result result = dataSourceService.queryDataSourceListPaging(loginUser, searchVal, pageNo, pageSize); PageInfo<DataSource> pageInfo =
Assertions.assertEquals(Status.SUCCESS.getCode(), (int) result.getCode()); dataSourceService.queryDataSourceListPaging(loginUser, searchVal, pageNo, pageSize);
Assertions.assertNotNull(pageInfo);
} }
@Test @Test
@ -270,9 +272,8 @@ public class DataSourceServiceTest {
// test admin user // test admin user
Mockito.when(dataSourceMapper.queryAuthedDatasource(userId)).thenReturn(getSingleDataSourceList()); Mockito.when(dataSourceMapper.queryAuthedDatasource(userId)).thenReturn(getSingleDataSourceList());
Mockito.when(dataSourceMapper.queryDatasourceExceptUserId(userId)).thenReturn(getDataSourceList()); Mockito.when(dataSourceMapper.queryDatasourceExceptUserId(userId)).thenReturn(getDataSourceList());
Map<String, Object> result = dataSourceService.unauthDatasource(loginUser, userId); List<DataSource> dataSources = dataSourceService.unAuthDatasource(loginUser, userId);
logger.info(result.toString()); logger.info(dataSources.toString());
List<DataSource> dataSources = (List<DataSource>) result.get(Constants.DATA_LIST);
Assertions.assertTrue(CollectionUtils.isNotEmpty(dataSources)); Assertions.assertTrue(CollectionUtils.isNotEmpty(dataSources));
// test non-admin user // test non-admin user
@ -280,9 +281,8 @@ public class DataSourceServiceTest {
loginUser.setUserType(UserType.GENERAL_USER); loginUser.setUserType(UserType.GENERAL_USER);
Mockito.when(dataSourceMapper.selectByMap(Collections.singletonMap("user_id", loginUser.getId()))) Mockito.when(dataSourceMapper.selectByMap(Collections.singletonMap("user_id", loginUser.getId())))
.thenReturn(getDataSourceList()); .thenReturn(getDataSourceList());
result = dataSourceService.unauthDatasource(loginUser, userId); dataSources = dataSourceService.unAuthDatasource(loginUser, userId);
logger.info(result.toString()); logger.info(dataSources.toString());
dataSources = (List<DataSource>) result.get(Constants.DATA_LIST);
Assertions.assertTrue(CollectionUtils.isNotEmpty(dataSources)); Assertions.assertTrue(CollectionUtils.isNotEmpty(dataSources));
} }
@ -295,17 +295,16 @@ public class DataSourceServiceTest {
// test admin user // test admin user
Mockito.when(dataSourceMapper.queryAuthedDatasource(userId)).thenReturn(getSingleDataSourceList()); Mockito.when(dataSourceMapper.queryAuthedDatasource(userId)).thenReturn(getSingleDataSourceList());
Map<String, Object> result = dataSourceService.authedDatasource(loginUser, userId); List<DataSource> dataSources = dataSourceService.authedDatasource(loginUser, userId);
logger.info(result.toString()); logger.info(dataSources.toString());
List<DataSource> dataSources = (List<DataSource>) result.get(Constants.DATA_LIST);
Assertions.assertTrue(CollectionUtils.isNotEmpty(dataSources)); Assertions.assertTrue(CollectionUtils.isNotEmpty(dataSources));
// test non-admin user // test non-admin user
loginUser.setId(2); loginUser.setId(2);
loginUser.setUserType(UserType.GENERAL_USER); loginUser.setUserType(UserType.GENERAL_USER);
Map<String, Object> success = dataSourceService.authedDatasource(loginUser, userId); dataSources = dataSourceService.authedDatasource(loginUser, userId);
logger.info(result.toString()); logger.info(dataSources.toString());
Assertions.assertEquals(Status.SUCCESS, success.get(Constants.STATUS)); Assertions.assertNotNull(dataSources);
} }
@Test @Test
@ -320,9 +319,9 @@ public class DataSourceServiceTest {
DataSource dataSource = new DataSource(); DataSource dataSource = new DataSource();
dataSource.setType(DbType.MYSQL); dataSource.setType(DbType.MYSQL);
Mockito.when(dataSourceMapper.selectBatchIds(dataSourceIds)).thenReturn(Collections.singletonList(dataSource)); Mockito.when(dataSourceMapper.selectBatchIds(dataSourceIds)).thenReturn(Collections.singletonList(dataSource));
Map<String, Object> map = List<DataSource> list =
dataSourceService.queryDataSourceList(loginUser, DbType.MYSQL.ordinal()); dataSourceService.queryDataSourceList(loginUser, DbType.MYSQL.ordinal());
Assertions.assertEquals(Status.SUCCESS, map.get(Constants.STATUS)); Assertions.assertNotNull(list);
} }
@Test @Test
@ -341,8 +340,11 @@ public class DataSourceServiceTest {
User loginUser = new User(); User loginUser = new User();
loginUser.setUserType(UserType.GENERAL_USER); loginUser.setUserType(UserType.GENERAL_USER);
loginUser.setId(2); loginUser.setId(2);
Map<String, Object> result = dataSourceService.queryDataSource(Mockito.anyInt(), loginUser); try {
Assertions.assertEquals(((Status) result.get(Constants.STATUS)).getCode(), Status.RESOURCE_NOT_EXIST.getCode()); dataSourceService.queryDataSource(Mockito.anyInt(), loginUser);
} catch (Exception e) {
Assertions.assertTrue(e.getMessage().contains(Status.RESOURCE_NOT_EXIST.getMsg()));
}
DataSource dataSource = getOracleDataSource(1); DataSource dataSource = getOracleDataSource(1);
Mockito.when(dataSourceMapper.selectById(Mockito.anyInt())).thenReturn(dataSource); Mockito.when(dataSourceMapper.selectById(Mockito.anyInt())).thenReturn(dataSource);
@ -350,8 +352,8 @@ public class DataSourceServiceTest {
loginUser.getId(), DATASOURCE, baseServiceLogger)).thenReturn(true); loginUser.getId(), DATASOURCE, baseServiceLogger)).thenReturn(true);
Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.DATASOURCE, Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.DATASOURCE,
new Object[]{dataSource.getId()}, loginUser.getId(), baseServiceLogger)).thenReturn(true); new Object[]{dataSource.getId()}, loginUser.getId(), baseServiceLogger)).thenReturn(true);
result = dataSourceService.queryDataSource(dataSource.getId(), loginUser); BaseDataSourceParamDTO paramDTO = dataSourceService.queryDataSource(dataSource.getId(), loginUser);
Assertions.assertEquals(((Status) result.get(Constants.STATUS)).getCode(), Status.SUCCESS.getCode()); Assertions.assertNotNull(paramDTO);
} }
private List<DataSource> getDataSourceList() { private List<DataSource> getDataSourceList() {
@ -524,10 +526,13 @@ public class DataSourceServiceTest {
DataSource dataSource = getOracleDataSource(); DataSource dataSource = getOracleDataSource();
int datasourceId = 1; int datasourceId = 1;
dataSource.setId(datasourceId); dataSource.setId(datasourceId);
Map<String, Object> result;
Mockito.when(dataSourceMapper.selectById(datasourceId)).thenReturn(null); Mockito.when(dataSourceMapper.selectById(datasourceId)).thenReturn(null);
result = dataSourceService.getDatabases(datasourceId);
Assertions.assertEquals(Status.QUERY_DATASOURCE_ERROR, result.get(Constants.STATUS)); try {
dataSourceService.getDatabases(datasourceId);
} catch (Exception e) {
Assertions.assertTrue(e.getMessage().contains(Status.QUERY_DATASOURCE_ERROR.getMsg()));
}
Mockito.when(dataSourceMapper.selectById(datasourceId)).thenReturn(dataSource); Mockito.when(dataSourceMapper.selectById(datasourceId)).thenReturn(dataSource);
MySQLConnectionParam connectionParam = new MySQLConnectionParam(); MySQLConnectionParam connectionParam = new MySQLConnectionParam();
@ -536,13 +541,21 @@ public class DataSourceServiceTest {
dataSourceUtils.when(() -> DataSourceUtils.getConnection(Mockito.any(), Mockito.any())).thenReturn(connection); dataSourceUtils.when(() -> DataSourceUtils.getConnection(Mockito.any(), Mockito.any())).thenReturn(connection);
dataSourceUtils.when(() -> DataSourceUtils.buildConnectionParams(Mockito.any(), Mockito.any())) dataSourceUtils.when(() -> DataSourceUtils.buildConnectionParams(Mockito.any(), Mockito.any()))
.thenReturn(connectionParam); .thenReturn(connectionParam);
result = dataSourceService.getDatabases(datasourceId);
Assertions.assertEquals(Status.GET_DATASOURCE_TABLES_ERROR, result.get(Constants.STATUS)); try {
dataSourceService.getDatabases(datasourceId);
} catch (Exception e) {
Assertions.assertTrue(e.getMessage().contains(Status.GET_DATASOURCE_TABLES_ERROR.getMsg()));
}
dataSourceUtils.when(() -> DataSourceUtils.buildConnectionParams(Mockito.any(), Mockito.any())) dataSourceUtils.when(() -> DataSourceUtils.buildConnectionParams(Mockito.any(), Mockito.any()))
.thenReturn(null); .thenReturn(null);
result = dataSourceService.getDatabases(datasourceId);
Assertions.assertEquals(Status.DATASOURCE_CONNECT_FAILED, result.get(Constants.STATUS)); try {
dataSourceService.getDatabases(datasourceId);
} catch (Exception e) {
Assertions.assertTrue(e.getMessage().contains(Status.DATASOURCE_CONNECT_FAILED.getMsg()));
}
connection.close(); connection.close();
dataSourceUtils.close(); dataSourceUtils.close();
} }

Loading…
Cancel
Save