From deca8b05826b707b9630fe1c4fd1b93c69ce84be Mon Sep 17 00:00:00 2001 From: Wenjun Ruan Date: Sat, 18 Nov 2023 11:59:07 +0800 Subject: [PATCH] Remove API Result in Service --- .../api/controller/AccessTokenController.java | 77 ++--- .../api/controller/AlertGroupController.java | 81 +++-- .../AlertPluginInstanceController.java | 79 ++--- .../api/controller/AuditLogController.java | 36 ++- .../api/controller/BaseController.java | 23 +- .../api/controller/ClusterController.java | 80 ++--- .../controller/DataAnalysisController.java | 48 +-- .../api/controller/DataQualityController.java | 76 ++--- .../api/controller/DataSourceController.java | 49 +-- .../api/controller/EnvironmentController.java | 42 ++- .../api/controller/ExecutorController.java | 26 +- .../controller/K8sNamespaceController.java | 8 +- .../api/controller/LoggerController.java | 3 +- .../api/controller/MonitorController.java | 23 +- .../ProcessDefinitionController.java | 14 +- .../controller/ProcessInstanceController.java | 8 +- .../api/controller/ProjectController.java | 17 +- .../ProjectParameterController.java | 6 +- .../api/controller/QueueController.java | 61 ++-- .../api/controller/ResourcesController.java | 13 +- .../api/controller/SchedulerController.java | 8 +- .../controller/TaskDefinitionController.java | 10 +- .../controller/TaskInstanceController.java | 8 +- .../api/controller/TenantController.java | 71 +++-- .../api/controller/UsersController.java | 9 +- .../controller/WorkFlowLineageController.java | 23 +- .../api/controller/WorkerGroupController.java | 9 +- .../v2/AccessTokenV2Controller.java | 8 +- .../controller/v2/ProjectV2Controller.java | 13 +- .../api/controller/v2/QueueV2Controller.java | 54 ++-- .../controller/v2/StatisticsV2Controller.java | 95 +++--- .../v2/TaskInstanceV2Controller.java | 13 +- .../v2/WorkflowInstanceV2Controller.java | 10 +- .../api/dto/queue/QueueCreateResponse.java | 39 --- .../dto/queue/QueueListPagingResponse.java | 40 --- .../api/dto/queue/QueueListResponse.java | 43 --- .../api/dto/queue/QueueUpdateResponse.java | 39 --- .../api/dto/queue/QueueVerifyResponse.java | 39 --- .../dolphinscheduler/api/enums/Status.java | 2 +- .../api/exceptions/ApiExceptionHandler.java | 13 +- .../ResourcePermissionCheckService.java | 8 - .../ResourcePermissionCheckServiceImpl.java | 5 - .../api/service/AccessTokenService.java | 17 +- .../api/service/AlertGroupService.java | 24 +- .../service/AlertPluginInstanceService.java | 30 +- .../api/service/AuditService.java | 11 +- .../api/service/BaseService.java | 14 +- .../api/service/ClusterService.java | 23 +- .../api/service/DataAnalysisService.java | 27 +- .../api/service/DataSourceService.java | 14 +- .../api/service/DqExecuteResultService.java | 17 +- .../api/service/DqRuleService.java | 24 +- .../api/service/EnvironmentService.java | 7 +- .../api/service/ExecutorService.java | 16 +- .../api/service/LoggerService.java | 4 +- .../api/service/MonitorService.java | 9 +- .../api/service/ProjectService.java | 8 + .../api/service/QueueService.java | 29 +- .../api/service/TenantService.java | 45 +-- .../api/service/WorkFlowLineageService.java | 4 +- .../service/impl/AccessTokenServiceImpl.java | 105 +++---- .../service/impl/AlertGroupServiceImpl.java | 172 ++++------- .../impl/AlertPluginInstanceServiceImpl.java | 139 +++------ .../api/service/impl/AuditServiceImpl.java | 38 ++- .../api/service/impl/BaseServiceImpl.java | 22 +- .../api/service/impl/ClusterServiceImpl.java | 218 ++++--------- .../service/impl/DataAnalysisServiceImpl.java | 210 +++++-------- .../service/impl/DataSourceServiceImpl.java | 127 +++----- .../impl/DqExecuteResultServiceImpl.java | 29 +- .../api/service/impl/DqRuleServiceImpl.java | 76 ++--- .../service/impl/EnvironmentServiceImpl.java | 147 +++------ .../api/service/impl/ExecutorServiceImpl.java | 41 +-- .../service/impl/K8SNamespaceServiceImpl.java | 19 +- .../api/service/impl/LoggerServiceImpl.java | 32 +- .../api/service/impl/MonitorServiceImpl.java | 42 +-- .../api/service/impl/ProjectServiceImpl.java | 34 +- .../api/service/impl/QueueServiceImpl.java | 83 ++--- .../service/impl/ResourcesServiceImpl.java | 34 -- .../service/impl/TaskGroupServiceImpl.java | 3 - .../api/service/impl/TenantServiceImpl.java | 110 ++----- .../api/service/impl/UdfFuncServiceImpl.java | 1 - .../impl/WorkFlowLineageServiceImpl.java | 24 +- .../service/impl/WorkerGroupServiceImpl.java | 2 - .../dolphinscheduler/api/utils/PageInfo.java | 77 +---- .../api/AssertionsHelper.java} | 27 +- .../controller/AccessTokenControllerTest.java | 2 +- .../AccessTokenV2ControllerTest.java | 4 +- .../AlertPluginInstanceControllerTest.java | 60 ++-- .../controller/DataQualityControllerTest.java | 53 ++-- .../WorkFlowLineageControllerTest.java | 22 +- .../v2/StatisticsV2ControllerTest.java | 67 ++-- .../api/service/AccessTokenServiceTest.java | 89 +++--- .../api/service/AlertGroupServiceTest.java | 165 +++++----- .../AlertPluginInstanceServiceTest.java | 88 +++--- .../api/service/AuditServiceTest.java | 22 +- .../api/service/ClusterServiceTest.java | 292 ------------------ .../api/service/DataAnalysisServiceTest.java | 151 ++++----- .../api/service/DataSourceServiceTest.java | 123 ++++---- .../service/DqExecuteResultServiceTest.java | 13 +- .../api/service/DqRuleServiceTest.java | 24 +- .../api/service/K8SNamespaceServiceTest.java | 15 +- .../api/service/LoggerServiceTest.java | 45 ++- .../api/service/MonitorServiceTest.java | 33 +- .../api/service/QueueServiceTest.java | 66 ++-- .../api/service/TenantServiceTest.java | 148 ++++----- .../service/WorkFlowLineageServiceTest.java | 7 +- .../api/service/impl/ClusterServiceTest.java | 259 ++++++++++++++++ .../{ => impl}/EnvironmentServiceTest.java | 150 ++++----- .../dolphinscheduler/dao/entity/Tenant.java | 19 -- .../dao/entity/WorkFlowLineage.java | 66 +--- .../src/main/resources/logback-spring.xml | 2 +- .../src/test/resources/logback.xml | 2 +- 112 files changed, 2117 insertions(+), 3334 deletions(-) delete mode 100644 dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/queue/QueueCreateResponse.java delete mode 100644 dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/queue/QueueListPagingResponse.java delete mode 100644 dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/queue/QueueListResponse.java delete mode 100644 dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/queue/QueueUpdateResponse.java delete mode 100644 dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/queue/QueueVerifyResponse.java rename dolphinscheduler-api/src/{main/java/org/apache/dolphinscheduler/api/dto/CreateTokenResponse.java => test/java/org/apache/dolphinscheduler/api/AssertionsHelper.java} (54%) delete mode 100644 dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ClusterServiceTest.java create mode 100644 dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/impl/ClusterServiceTest.java rename dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/{ => impl}/EnvironmentServiceTest.java (61%) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AccessTokenController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AccessTokenController.java index dcc083f4a3b84..781c08ad62d17 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AccessTokenController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AccessTokenController.java @@ -26,12 +26,14 @@ import org.apache.dolphinscheduler.api.exceptions.ApiException; import org.apache.dolphinscheduler.api.service.AccessTokenService; +import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.constants.Constants; +import org.apache.dolphinscheduler.dao.entity.AccessToken; import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.plugin.task.api.utils.ParameterUtils; -import java.util.Map; +import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -81,12 +83,13 @@ public class AccessTokenController extends BaseController { @PostMapping() @ResponseStatus(HttpStatus.CREATED) @ApiException(CREATE_ACCESS_TOKEN_ERROR) - public Result createToken(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam(value = "userId") int userId, - @RequestParam(value = "expireTime") String expireTime, - @RequestParam(value = "token", required = false) String token) { + public Result createToken(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam(value = "userId") int userId, + @RequestParam(value = "expireTime") String expireTime, + @RequestParam(value = "token", required = false) String token) { - return accessTokenService.createToken(loginUser, userId, expireTime, token); + AccessToken accessToken = accessTokenService.createToken(loginUser, userId, expireTime, token); + return Result.success(accessToken); } /** @@ -101,11 +104,11 @@ public Result createToken(@Parameter(hidden = true) @RequestAttribute(value = Co @PostMapping(value = "/generate") @ResponseStatus(HttpStatus.CREATED) @ApiException(GENERATE_TOKEN_ERROR) - public Result generateToken(@RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam(value = "userId") int userId, - @RequestParam(value = "expireTime") String expireTime) { - Map result = accessTokenService.generateToken(loginUser, userId, expireTime); - return returnDataList(result); + public Result generateToken(@RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam(value = "userId") int userId, + @RequestParam(value = "expireTime") String expireTime) { + String token = accessTokenService.generateToken(loginUser, userId, expireTime); + return Result.success(token); } /** @@ -126,18 +129,16 @@ public Result generateToken(@RequestAttribute(value = Constants.SESSION_USER) Us @GetMapping() @ResponseStatus(HttpStatus.OK) @ApiException(QUERY_ACCESSTOKEN_LIST_PAGING_ERROR) - public Result queryAccessTokenList(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam("pageNo") Integer pageNo, - @RequestParam(value = "searchVal", required = false) String searchVal, - @RequestParam("pageSize") Integer pageSize) { - - Result result = checkPageParams(pageNo, pageSize); - if (!result.checkResult()) { - return result; - } + public Result> queryAccessTokenList(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam("pageNo") Integer pageNo, + @RequestParam(value = "searchVal", required = false) String searchVal, + @RequestParam("pageSize") Integer pageSize) { + + checkPageParams(pageNo, pageSize); searchVal = ParameterUtils.handleEscapes(searchVal); - result = accessTokenService.queryAccessTokenList(loginUser, searchVal, pageNo, pageSize); - return result; + PageInfo accessTokenPageInfo = + accessTokenService.queryAccessTokenList(loginUser, searchVal, pageNo, pageSize); + return Result.success(accessTokenPageInfo); } /** @@ -154,10 +155,10 @@ public Result queryAccessTokenList(@Parameter(hidden = true) @RequestAttribute(v @GetMapping(value = "/user/{userId}") @ResponseStatus(HttpStatus.OK) @ApiException(QUERY_ACCESSTOKEN_BY_USER_ERROR) - public Result queryAccessTokenByUser(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @PathVariable("userId") Integer userId) { - Map result = this.accessTokenService.queryAccessTokenByUser(loginUser, userId); - return this.returnDataList(result); + public Result> queryAccessTokenByUser(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @PathVariable("userId") Integer userId) { + List accessTokens = accessTokenService.queryAccessTokenByUser(loginUser, userId); + return Result.success(accessTokens); } /** @@ -171,10 +172,10 @@ public Result queryAccessTokenByUser(@Parameter(hidden = true) @RequestAttribute @DeleteMapping(value = "/{id}") @ResponseStatus(HttpStatus.OK) @ApiException(DELETE_ACCESS_TOKEN_ERROR) - public Result delAccessTokenById(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @PathVariable(value = "id") int id) { - Map result = accessTokenService.delAccessTokenById(loginUser, id); - return returnDataList(result); + public Result delAccessTokenById(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @PathVariable(value = "id") int id) { + accessTokenService.deleteAccessTokenById(loginUser, id); + return Result.success(true); } /** @@ -197,14 +198,14 @@ public Result delAccessTokenById(@Parameter(hidden = true) @RequestAttribute(val @PutMapping(value = "/{id}") @ResponseStatus(HttpStatus.OK) @ApiException(UPDATE_ACCESS_TOKEN_ERROR) - public Result updateToken(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @PathVariable(value = "id") int id, - @RequestParam(value = "userId") int userId, - @RequestParam(value = "expireTime") String expireTime, - @RequestParam(value = "token", required = false) String token) { - - Map result = accessTokenService.updateToken(loginUser, id, userId, expireTime, token); - return returnDataList(result); + public Result updateToken(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @PathVariable(value = "id") int id, + @RequestParam(value = "userId") int userId, + @RequestParam(value = "expireTime") String expireTime, + @RequestParam(value = "token", required = false) String token) { + + AccessToken accessToken = accessTokenService.updateToken(loginUser, id, userId, expireTime, token); + return Result.success(accessToken); } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AlertGroupController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AlertGroupController.java index 9ac7a82db02d6..a1f0b19e3c768 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AlertGroupController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AlertGroupController.java @@ -27,12 +27,14 @@ import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.exceptions.ApiException; import org.apache.dolphinscheduler.api.service.AlertGroupService; +import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.constants.Constants; +import org.apache.dolphinscheduler.dao.entity.AlertGroup; import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.plugin.task.api.utils.ParameterUtils; -import java.util.Map; +import java.util.List; import lombok.extern.slf4j.Slf4j; @@ -84,13 +86,12 @@ public class AlertGroupController extends BaseController { @PostMapping() @ResponseStatus(HttpStatus.CREATED) @ApiException(CREATE_ALERT_GROUP_ERROR) - public Result createAlertgroup(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam(value = "groupName") String groupName, - @RequestParam(value = "description", required = false) String description, - @RequestParam(value = "alertInstanceIds") String alertInstanceIds) { - Map result = - alertGroupService.createAlertgroup(loginUser, groupName, description, alertInstanceIds); - return returnDataList(result); + public Result createAlertGroup(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam(value = "groupName") String groupName, + @RequestParam(value = "description", required = false) String description, + @RequestParam(value = "alertInstanceIds") String alertInstanceIds) { + AlertGroup alertgroup = alertGroupService.createAlertGroup(loginUser, groupName, description, alertInstanceIds); + return Result.success(alertgroup); } /** @@ -103,10 +104,10 @@ public Result createAlertgroup(@Parameter(hidden = true) @RequestAttribute(value @GetMapping(value = "/list") @ResponseStatus(HttpStatus.OK) @ApiException(QUERY_ALL_ALERTGROUP_ERROR) - public Result list(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { + public Result> list(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { - Map result = alertGroupService.queryAlertgroup(loginUser); - return returnDataList(result); + List alertGroups = alertGroupService.queryAllAlertGroup(loginUser); + return Result.success(alertGroups); } /** @@ -119,10 +120,10 @@ public Result list(@Parameter(hidden = true) @RequestAttribute(value = Constants @GetMapping(value = "/normal-list") @ResponseStatus(HttpStatus.OK) @ApiException(QUERY_ALL_ALERTGROUP_ERROR) - public Result normalAlertGroupList(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { + public Result> normalAlertGroupList(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { - Map result = alertGroupService.queryNormalAlertgroup(loginUser); - return returnDataList(result); + List alertGroups = alertGroupService.queryNormalAlertGroups(loginUser); + return Result.success(alertGroups); } /** @@ -143,22 +144,21 @@ public Result normalAlertGroupList(@Parameter(hidden = true) @RequestAttribute(v @GetMapping() @ResponseStatus(HttpStatus.OK) @ApiException(LIST_PAGING_ALERT_GROUP_ERROR) - public Result listPaging(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam(value = "searchVal", required = false) String searchVal, - @RequestParam("pageNo") Integer pageNo, - @RequestParam("pageSize") Integer pageSize) { - Result result = checkPageParams(pageNo, pageSize); - if (!result.checkResult()) { - return result; - } + public Result> listPaging(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam(value = "searchVal", required = false) String searchVal, + @RequestParam("pageNo") Integer pageNo, + @RequestParam("pageSize") Integer pageSize) { + checkPageParams(pageNo, pageSize); searchVal = ParameterUtils.handleEscapes(searchVal); - return alertGroupService.listPaging(loginUser, searchVal, pageNo, pageSize); + PageInfo alertGroupPageInfo = alertGroupService.listPaging(loginUser, searchVal, pageNo, pageSize); + return Result.success(alertGroupPageInfo); } + /** * check alarm group detail by Id * * @param loginUser login user - * @param id alert group id + * @param id alert group id * @return one alert group */ @@ -169,11 +169,11 @@ public Result listPaging(@Parameter(hidden = true) @RequestAttribute(value = Con @PostMapping(value = "/query") @ResponseStatus(HttpStatus.OK) @ApiException(QUERY_ALERT_GROUP_ERROR) - public Result queryAlertGroupById(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam("id") Integer id) { + public Result queryAlertGroupById(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam("id") Integer id) { - Map result = alertGroupService.queryAlertGroupById(loginUser, id); - return returnDataList(result); + AlertGroup alertGroup = alertGroupService.queryAlertGroupById(loginUser, id); + return Result.success(alertGroup); } /** @@ -195,15 +195,14 @@ public Result queryAlertGroupById(@Parameter(hidden = true) @RequestAttribute(va @PutMapping(value = "/{id}") @ResponseStatus(HttpStatus.OK) @ApiException(UPDATE_ALERT_GROUP_ERROR) - public Result updateAlertgroup(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @PathVariable(value = "id") int id, - @RequestParam(value = "groupName") String groupName, - @RequestParam(value = "description", required = false) String description, - @RequestParam(value = "alertInstanceIds") String alertInstanceIds) { - - Map result = - alertGroupService.updateAlertgroup(loginUser, id, groupName, description, alertInstanceIds); - return returnDataList(result); + public Result updateAlertGroupById(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @PathVariable(value = "id") int id, + @RequestParam(value = "groupName") String groupName, + @RequestParam(value = "description", required = false) String description, + @RequestParam(value = "alertInstanceIds") String alertInstanceIds) { + AlertGroup alertGroup = + alertGroupService.updateAlertGroupById(loginUser, id, groupName, description, alertInstanceIds); + return Result.success(alertGroup); } /** @@ -220,10 +219,10 @@ public Result updateAlertgroup(@Parameter(hidden = true) @RequestAttribute(value @DeleteMapping(value = "/{id}") @ResponseStatus(HttpStatus.OK) @ApiException(DELETE_ALERT_GROUP_ERROR) - public Result delAlertgroupById(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @PathVariable(value = "id") int id) { - Map result = alertGroupService.delAlertgroupById(loginUser, id); - return returnDataList(result); + public Result deleteAlertGroupById(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @PathVariable(value = "id") int id) { + alertGroupService.deleteAlertGroupById(loginUser, id); + return Result.success(true); } /** diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AlertPluginInstanceController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AlertPluginInstanceController.java index 688f2c594c23a..3456df0dc56de 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AlertPluginInstanceController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AlertPluginInstanceController.java @@ -27,14 +27,17 @@ import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.exceptions.ApiException; import org.apache.dolphinscheduler.api.service.AlertPluginInstanceService; +import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.Result; +import org.apache.dolphinscheduler.api.vo.AlertPluginInstanceVO; import org.apache.dolphinscheduler.common.constants.Constants; import org.apache.dolphinscheduler.common.enums.AlertPluginInstanceType; import org.apache.dolphinscheduler.common.enums.WarningType; +import org.apache.dolphinscheduler.dao.entity.AlertPluginInstance; import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.plugin.task.api.utils.ParameterUtils; -import java.util.Map; +import java.util.List; import lombok.extern.slf4j.Slf4j; @@ -87,16 +90,15 @@ public class AlertPluginInstanceController extends BaseController { @PostMapping() @ResponseStatus(HttpStatus.CREATED) @ApiException(CREATE_ALERT_PLUGIN_INSTANCE_ERROR) - public Result createAlertPluginInstance(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam(value = "pluginDefineId") int pluginDefineId, - @RequestParam(value = "instanceName") String instanceName, - @RequestParam(value = "instanceType") AlertPluginInstanceType instanceType, - @RequestParam(value = "warningType") WarningType warningType, - @RequestParam(value = "pluginInstanceParams") String pluginInstanceParams) { - Map result = - alertPluginInstanceService.create(loginUser, pluginDefineId, instanceName, instanceType, warningType, - pluginInstanceParams); - return returnDataList(result); + public Result createAlertPluginInstance(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam(value = "pluginDefineId") int pluginDefineId, + @RequestParam(value = "instanceName") String instanceName, + @RequestParam(value = "instanceType") AlertPluginInstanceType instanceType, + @RequestParam(value = "warningType") WarningType warningType, + @RequestParam(value = "pluginInstanceParams") String pluginInstanceParams) { + AlertPluginInstance alertPluginInstance = alertPluginInstanceService.create(loginUser, pluginDefineId, + instanceName, instanceType, warningType, pluginInstanceParams); + return Result.success(alertPluginInstance); } /** @@ -117,14 +119,14 @@ public Result createAlertPluginInstance(@Parameter(hidden = true) @RequestAttrib @PutMapping(value = "/{id}") @ResponseStatus(HttpStatus.OK) @ApiException(UPDATE_ALERT_PLUGIN_INSTANCE_ERROR) - public Result updateAlertPluginInstance(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @PathVariable(value = "id") int id, - @RequestParam(value = "instanceName") String instanceName, - @RequestParam(value = "warningType") WarningType warningType, - @RequestParam(value = "pluginInstanceParams") String pluginInstanceParams) { - Map result = - alertPluginInstanceService.update(loginUser, id, instanceName, warningType, pluginInstanceParams); - return returnDataList(result); + public Result updateAlertPluginInstanceById(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @PathVariable(value = "id") int id, + @RequestParam(value = "instanceName") String instanceName, + @RequestParam(value = "warningType") WarningType warningType, + @RequestParam(value = "pluginInstanceParams") String pluginInstanceParams) { + AlertPluginInstance alertPluginInstance = + alertPluginInstanceService.updateById(loginUser, id, instanceName, warningType, pluginInstanceParams); + return Result.success(alertPluginInstance); } /** @@ -141,11 +143,11 @@ public Result updateAlertPluginInstance(@Parameter(hidden = true) @RequestAttrib @DeleteMapping(value = "/{id}") @ResponseStatus(HttpStatus.OK) @ApiException(DELETE_ALERT_PLUGIN_INSTANCE_ERROR) - public Result deleteAlertPluginInstance(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @PathVariable(value = "id") int id) { + public Result deleteAlertPluginInstance(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @PathVariable(value = "id") int id) { - Map result = alertPluginInstanceService.delete(loginUser, id); - return returnDataList(result); + alertPluginInstanceService.deleteById(loginUser, id); + return Result.success(true); } /** @@ -159,10 +161,10 @@ public Result deleteAlertPluginInstance(@Parameter(hidden = true) @RequestAttrib @GetMapping(value = "/{id}") @ResponseStatus(HttpStatus.OK) @ApiException(GET_ALERT_PLUGIN_INSTANCE_ERROR) - public Result getAlertPluginInstance(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @PathVariable(value = "id") int id) { - Map result = alertPluginInstanceService.get(loginUser, id); - return returnDataList(result); + public Result getAlertPluginInstance(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @PathVariable(value = "id") int id) { + AlertPluginInstance alertPluginInstance = alertPluginInstanceService.getById(loginUser, id); + return Result.success(alertPluginInstance); } /** @@ -175,9 +177,9 @@ public Result getAlertPluginInstance(@Parameter(hidden = true) @RequestAttribute @GetMapping(value = "/list") @ResponseStatus(HttpStatus.OK) @ApiException(QUERY_ALL_ALERT_PLUGIN_INSTANCE_ERROR) - public Result getAlertPluginInstance(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { - Map result = alertPluginInstanceService.queryAll(); - return returnDataList(result); + public Result> getAlertPluginInstance(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { + List alertPluginInstanceVOS = alertPluginInstanceService.queryAll(); + return Result.success(alertPluginInstanceVOS); } /** @@ -223,16 +225,15 @@ public Result verifyGroupName(@Parameter(hidden = true) @RequestAttribute(value @GetMapping() @ResponseStatus(HttpStatus.OK) @ApiException(LIST_PAGING_ALERT_PLUGIN_INSTANCE_ERROR) - public Result listPaging(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam(value = "searchVal", required = false) String searchVal, - @RequestParam("pageNo") Integer pageNo, - @RequestParam("pageSize") Integer pageSize) { - Result result = checkPageParams(pageNo, pageSize); - if (!result.checkResult()) { - return result; - } + public Result> listPaging(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam(value = "searchVal", required = false) String searchVal, + @RequestParam("pageNo") Integer pageNo, + @RequestParam("pageSize") Integer pageSize) { + checkPageParams(pageNo, pageSize); searchVal = ParameterUtils.handleEscapes(searchVal); - return alertPluginInstanceService.listPaging(loginUser, searchVal, pageNo, pageSize); + PageInfo alertPluginInstanceVOPageInfo = + alertPluginInstanceService.listPaging(loginUser, searchVal, pageNo, pageSize); + return Result.success(alertPluginInstanceVOPageInfo); } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AuditLogController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AuditLogController.java index ee258df615df9..6d334951e8658 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AuditLogController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AuditLogController.java @@ -19,8 +19,10 @@ import static org.apache.dolphinscheduler.api.enums.Status.QUERY_AUDIT_LOG_LIST_PAGING; +import org.apache.dolphinscheduler.api.dto.AuditDto; import org.apache.dolphinscheduler.api.exceptions.ApiException; import org.apache.dolphinscheduler.api.service.AuditService; +import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.constants.Constants; import org.apache.dolphinscheduler.common.enums.AuditOperationType; @@ -76,20 +78,24 @@ public class AuditLogController extends BaseController { @GetMapping(value = "/audit-log-list") @ResponseStatus(HttpStatus.OK) @ApiException(QUERY_AUDIT_LOG_LIST_PAGING) - public Result queryAuditLogListPaging(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam("pageNo") Integer pageNo, - @RequestParam("pageSize") Integer pageSize, - @RequestParam(value = "resourceType", required = false) AuditResourceType resourceType, - @RequestParam(value = "operationType", required = false) AuditOperationType operationType, - @RequestParam(value = "startDate", required = false) String startDate, - @RequestParam(value = "endDate", required = false) String endDate, - @RequestParam(value = "userName", required = false) String userName) { - Result result = checkPageParams(pageNo, pageSize); - if (!result.checkResult()) { - return result; - } - result = auditService.queryLogListPaging(loginUser, resourceType, operationType, startDate, endDate, userName, - pageNo, pageSize); - return result; + public Result> queryAuditLogListPaging(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam("pageNo") Integer pageNo, + @RequestParam("pageSize") Integer pageSize, + @RequestParam(value = "resourceType", required = false) AuditResourceType resourceType, + @RequestParam(value = "operationType", required = false) AuditOperationType operationType, + @RequestParam(value = "startDate", required = false) String startDate, + @RequestParam(value = "endDate", required = false) String endDate, + @RequestParam(value = "userName", required = false) String userName) { + checkPageParams(pageNo, pageSize); + PageInfo auditDtoPageInfo = auditService.queryLogListPaging( + loginUser, + resourceType, + operationType, + startDate, + endDate, + userName, + pageNo, + pageSize); + return Result.success(auditDtoPageInfo); } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/BaseController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/BaseController.java index 89205621ad842..f571e2d6b432f 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/BaseController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/BaseController.java @@ -23,6 +23,7 @@ import static org.apache.dolphinscheduler.common.constants.Constants.HTTP_X_REAL_IP; import org.apache.dolphinscheduler.api.enums.Status; +import org.apache.dolphinscheduler.api.exceptions.ServiceException; import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.constants.Constants; @@ -42,25 +43,17 @@ public class BaseController { /** * check params * - * @param pageNo page number + * @param pageNo page number * @param pageSize page size - * @return check result code + * @throws ServiceException exception */ - // todo: directly throw exception - public Result checkPageParams(int pageNo, int pageSize) { - Result result = new Result<>(); - Status resultEnum = Status.SUCCESS; - String msg = Status.SUCCESS.getMsg(); + public void checkPageParams(int pageNo, int pageSize) throws ServiceException { if (pageNo <= 0) { - resultEnum = Status.REQUEST_PARAMS_NOT_VALID_ERROR; - msg = MessageFormat.format(Status.REQUEST_PARAMS_NOT_VALID_ERROR.getMsg(), Constants.PAGE_NUMBER); - } else if (pageSize <= 0) { - resultEnum = Status.REQUEST_PARAMS_NOT_VALID_ERROR; - msg = MessageFormat.format(Status.REQUEST_PARAMS_NOT_VALID_ERROR.getMsg(), Constants.PAGE_SIZE); + throw new ServiceException(Status.REQUEST_PARAMS_NOT_VALID_ERROR, Constants.PAGE_NUMBER); + } + if (pageSize <= 0) { + throw new ServiceException(Status.REQUEST_PARAMS_NOT_VALID_ERROR, Constants.PAGE_SIZE); } - result.setCode(resultEnum.getCode()); - result.setMsg(msg); - return result; } /** diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ClusterController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ClusterController.java index 524eedeefe69c..00ad208d1ca01 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ClusterController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ClusterController.java @@ -24,14 +24,17 @@ import static org.apache.dolphinscheduler.api.enums.Status.UPDATE_CLUSTER_ERROR; import static org.apache.dolphinscheduler.api.enums.Status.VERIFY_CLUSTER_ERROR; +import org.apache.dolphinscheduler.api.dto.ClusterDto; import org.apache.dolphinscheduler.api.exceptions.ApiException; import org.apache.dolphinscheduler.api.service.ClusterService; +import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.constants.Constants; +import org.apache.dolphinscheduler.dao.entity.Cluster; import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.plugin.task.api.utils.ParameterUtils; -import java.util.Map; +import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -78,13 +81,13 @@ public class ClusterController extends BaseController { @PostMapping(value = "/create") @ResponseStatus(HttpStatus.CREATED) @ApiException(CREATE_CLUSTER_ERROR) - public Result createProject(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam("name") String name, - @RequestParam("config") String config, - @RequestParam(value = "description", required = false) String description) { + public Result createProject(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam("name") String name, + @RequestParam("config") String config, + @RequestParam(value = "description", required = false) String description) { - Map result = clusterService.createCluster(loginUser, name, config, description); - return returnDataList(result); + Long clusterCode = clusterService.createCluster(loginUser, name, config, description); + return Result.success(clusterCode); } /** @@ -107,13 +110,13 @@ public Result createProject(@Parameter(hidden = true) @RequestAttribute(value = @PostMapping(value = "/update") @ResponseStatus(HttpStatus.OK) @ApiException(UPDATE_CLUSTER_ERROR) - public Result updateCluster(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam("code") Long code, - @RequestParam("name") String name, - @RequestParam("config") String config, - @RequestParam(value = "description", required = false) String description) { - Map result = clusterService.updateClusterByCode(loginUser, code, name, config, description); - return returnDataList(result); + public Result updateCluster(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam("code") Long code, + @RequestParam("name") String name, + @RequestParam("config") String config, + @RequestParam(value = "description", required = false) String description) { + Cluster cluster = clusterService.updateClusterByCode(loginUser, code, name, config, description); + return Result.success(cluster); } /** @@ -129,11 +132,11 @@ public Result updateCluster(@Parameter(hidden = true) @RequestAttribute(value = @GetMapping(value = "/query-by-code") @ResponseStatus(HttpStatus.OK) @ApiException(QUERY_CLUSTER_BY_CODE_ERROR) - public Result queryClusterByCode(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam("clusterCode") Long clusterCode) { + public Result queryClusterByCode(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam("clusterCode") Long clusterCode) { - Map result = clusterService.queryClusterByCode(clusterCode); - return returnDataList(result); + ClusterDto clusterDto = clusterService.queryClusterByCode(clusterCode); + return Result.success(clusterDto); } /** @@ -153,18 +156,15 @@ public Result queryClusterByCode(@Parameter(hidden = true) @RequestAttribute(val @GetMapping(value = "/list-paging") @ResponseStatus(HttpStatus.OK) @ApiException(QUERY_CLUSTER_ERROR) - public Result queryClusterListPaging(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam(value = "searchVal", required = false) String searchVal, - @RequestParam("pageSize") Integer pageSize, - @RequestParam("pageNo") Integer pageNo) { - - Result result = checkPageParams(pageNo, pageSize); - if (!result.checkResult()) { - return result; - } + public Result> queryClusterListPaging(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam(value = "searchVal", required = false) String searchVal, + @RequestParam("pageSize") Integer pageSize, + @RequestParam("pageNo") Integer pageNo) { + + checkPageParams(pageNo, pageSize); searchVal = ParameterUtils.handleEscapes(searchVal); - result = clusterService.queryClusterListPaging(pageNo, pageSize, searchVal); - return result; + PageInfo clusterDtoPageInfo = clusterService.queryClusterListPaging(pageNo, pageSize, searchVal); + return Result.success(clusterDtoPageInfo); } /** @@ -181,11 +181,11 @@ public Result queryClusterListPaging(@Parameter(hidden = true) @RequestAttribute @PostMapping(value = "/delete") @ResponseStatus(HttpStatus.OK) @ApiException(DELETE_CLUSTER_ERROR) - public Result deleteCluster(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam("clusterCode") Long clusterCode) { + public Result deleteCluster(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam("clusterCode") Long clusterCode) { - Map result = clusterService.deleteClusterByCode(loginUser, clusterCode); - return returnDataList(result); + clusterService.deleteClusterByCode(loginUser, clusterCode); + return Result.success(true); } /** @@ -198,9 +198,9 @@ public Result deleteCluster(@Parameter(hidden = true) @RequestAttribute(value = @GetMapping(value = "/query-cluster-list") @ResponseStatus(HttpStatus.OK) @ApiException(QUERY_CLUSTER_ERROR) - public Result queryAllClusterList(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { - Map result = clusterService.queryAllClusterList(); - return returnDataList(result); + public Result> queryAllClusterList(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { + List clusterDtos = clusterService.queryAllClusterList(); + return Result.success(clusterDtos); } /** @@ -217,9 +217,9 @@ public Result queryAllClusterList(@Parameter(hidden = true) @RequestAttribute(va @PostMapping(value = "/verify-cluster") @ResponseStatus(HttpStatus.OK) @ApiException(VERIFY_CLUSTER_ERROR) - public Result verifyCluster(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam(value = "clusterName") String clusterName) { - Map result = clusterService.verifyCluster(clusterName); - return returnDataList(result); + public Result verifyCluster(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam(value = "clusterName") String clusterName) { + clusterService.verifyCluster(clusterName); + return Result.success(true); } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataAnalysisController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataAnalysisController.java index dcd8ac2d8c0bc..a044b05dbb10d 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataAnalysisController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataAnalysisController.java @@ -23,12 +23,16 @@ import static org.apache.dolphinscheduler.api.enums.Status.QUEUE_COUNT_ERROR; import static org.apache.dolphinscheduler.api.enums.Status.TASK_INSTANCE_STATE_COUNT_ERROR; +import org.apache.dolphinscheduler.api.dto.CommandStateCount; +import org.apache.dolphinscheduler.api.dto.DefineUserDto; +import org.apache.dolphinscheduler.api.dto.TaskCountDto; import org.apache.dolphinscheduler.api.exceptions.ApiException; import org.apache.dolphinscheduler.api.service.DataAnalysisService; import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.constants.Constants; import org.apache.dolphinscheduler.dao.entity.User; +import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; @@ -75,14 +79,14 @@ public class DataAnalysisController extends BaseController { @GetMapping(value = "/task-state-count") @ResponseStatus(HttpStatus.OK) @ApiException(TASK_INSTANCE_STATE_COUNT_ERROR) - public Result countTaskState(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam(value = "startDate", required = false) String startDate, - @RequestParam(value = "endDate", required = false) String endDate, - @RequestParam(value = "projectCode", required = false, defaultValue = "0") long projectCode) { + public Result countTaskState(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam(value = "startDate", required = false) String startDate, + @RequestParam(value = "endDate", required = false) String endDate, + @RequestParam(value = "projectCode", required = false, defaultValue = "0") long projectCode) { - Map result = + TaskCountDto taskCountDto = dataAnalysisService.countTaskStateByProject(loginUser, projectCode, startDate, endDate); - return returnDataList(result); + return Result.success(taskCountDto); } /** @@ -103,14 +107,14 @@ public Result countTaskState(@Parameter(hidden = true) @RequestAttribute(value = @GetMapping(value = "/process-state-count") @ResponseStatus(HttpStatus.OK) @ApiException(COUNT_PROCESS_INSTANCE_STATE_ERROR) - public Result countProcessInstanceState(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam(value = "startDate", required = false) String startDate, - @RequestParam(value = "endDate", required = false) String endDate, - @RequestParam(value = "projectCode", required = false, defaultValue = "0") long projectCode) { + public Result countProcessInstanceState(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam(value = "startDate", required = false) String startDate, + @RequestParam(value = "endDate", required = false) String endDate, + @RequestParam(value = "projectCode", required = false, defaultValue = "0") long projectCode) { - Map result = + TaskCountDto taskCountDto = dataAnalysisService.countProcessInstanceStateByProject(loginUser, projectCode, startDate, endDate); - return returnDataList(result); + return Result.success(taskCountDto); } /** @@ -127,11 +131,11 @@ public Result countProcessInstanceState(@Parameter(hidden = true) @RequestAttrib @GetMapping(value = "/define-user-count") @ResponseStatus(HttpStatus.OK) @ApiException(COUNT_PROCESS_DEFINITION_USER_ERROR) - public Result countDefinitionByUser(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam(value = "projectCode", required = false, defaultValue = "0") long projectCode) { + public Result countDefinitionByUser(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam(value = "projectCode", required = false, defaultValue = "0") long projectCode) { - Map result = dataAnalysisService.countDefinitionByUser(loginUser, projectCode); - return returnDataList(result); + DefineUserDto defineUserDto = dataAnalysisService.countDefinitionByUser(loginUser, projectCode); + return Result.success(defineUserDto); } /** @@ -144,10 +148,10 @@ public Result countDefinitionByUser(@Parameter(hidden = true) @RequestAttribute( @GetMapping(value = "/command-state-count") @ResponseStatus(HttpStatus.OK) @ApiException(COMMAND_STATE_COUNT_ERROR) - public Result countCommandState(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { + public Result> countCommandState(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { - Map result = dataAnalysisService.countCommandState(loginUser); - return returnDataList(result); + List commandStateCounts = dataAnalysisService.countCommandState(loginUser); + return Result.success(commandStateCounts); } /** @@ -160,9 +164,9 @@ public Result countCommandState(@Parameter(hidden = true) @RequestAttribute(valu @GetMapping(value = "/queue-count") @ResponseStatus(HttpStatus.OK) @ApiException(QUEUE_COUNT_ERROR) - public Result countQueueState(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { + public Result> countQueueState(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { - Map result = dataAnalysisService.countQueueState(loginUser); - return returnDataList(result); + Map stringIntegerMap = dataAnalysisService.countQueueState(loginUser); + return Result.success(stringIntegerMap); } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataQualityController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataQualityController.java index fdca568dbebe5..dc4dc32a32966 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataQualityController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataQualityController.java @@ -26,12 +26,16 @@ import org.apache.dolphinscheduler.api.exceptions.ApiException; import org.apache.dolphinscheduler.api.service.DqExecuteResultService; import org.apache.dolphinscheduler.api.service.DqRuleService; +import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.constants.Constants; +import org.apache.dolphinscheduler.dao.entity.DqExecuteResult; +import org.apache.dolphinscheduler.dao.entity.DqRule; import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.plugin.task.api.utils.ParameterUtils; +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.http.HttpStatus; @@ -74,9 +78,9 @@ public class DataQualityController extends BaseController { @GetMapping(value = "/getRuleFormCreateJson") @ResponseStatus(HttpStatus.OK) @ApiException(GET_RULE_FORM_CREATE_JSON_ERROR) - public Result getRuleFormCreateJsonById(@RequestParam(value = "ruleId") int ruleId) { - Map result = dqRuleService.getRuleFormCreateJsonById(ruleId); - return returnDataList(result); + public Result getRuleFormCreateJsonById(@RequestParam(value = "ruleId") int ruleId) { + String ruleFormCreateJsonById = dqRuleService.getRuleFormCreateJsonById(ruleId); + return Result.success(ruleFormCreateJsonById); } /** @@ -100,33 +104,33 @@ public Result getRuleFormCreateJsonById(@RequestParam(value = "ruleId") int rule @GetMapping(value = "/rule/page") @ResponseStatus(HttpStatus.OK) @ApiException(QUERY_RULE_LIST_PAGING_ERROR) - public Result queryRuleListPaging(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam(value = "searchVal", required = false) String searchVal, - @RequestParam(value = "ruleType", required = false) Integer ruleType, - @RequestParam(value = "startDate", required = false) String startTime, - @RequestParam(value = "endDate", required = false) String endTime, - @RequestParam("pageNo") Integer pageNo, - @RequestParam("pageSize") Integer pageSize) { - Result result = checkPageParams(pageNo, pageSize); - if (!result.checkResult()) { - return result; - } + public Result> queryRuleListPaging(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam(value = "searchVal", required = false) String searchVal, + @RequestParam(value = "ruleType", required = false) Integer ruleType, + @RequestParam(value = "startDate", required = false) String startTime, + @RequestParam(value = "endDate", required = false) String endTime, + @RequestParam("pageNo") Integer pageNo, + @RequestParam("pageSize") Integer pageSize) { + checkPageParams(pageNo, pageSize); searchVal = ParameterUtils.handleEscapes(searchVal); - return dqRuleService.queryRuleListPaging(loginUser, searchVal, ruleType, startTime, endTime, pageNo, pageSize); + PageInfo dqRulePageInfo = + dqRuleService.queryRuleListPaging(loginUser, searchVal, ruleType, startTime, endTime, pageNo, pageSize); + return Result.success(dqRulePageInfo); } /** * query all rule list + * * @return rule list */ @Operation(summary = "queryRuleList", description = "QUERY_RULE_LIST_NOTES") @GetMapping(value = "/ruleList") @ResponseStatus(HttpStatus.OK) @ApiException(QUERY_RULE_LIST_ERROR) - public Result queryRuleList() { - Map result = dqRuleService.queryAllRuleList(); - return returnDataList(result); + public Result> queryRuleList() { + List dqRules = dqRuleService.queryAllRuleList(); + return Result.success(dqRules); } /** @@ -155,23 +159,21 @@ public Result queryRuleList() { @GetMapping(value = "/result/page") @ResponseStatus(HttpStatus.OK) @ApiException(QUERY_EXECUTE_RESULT_LIST_PAGING_ERROR) - public Result queryExecuteResultListPaging(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam(value = "searchVal", required = false) String searchVal, - @RequestParam(value = "ruleType", required = false) Integer ruleType, - @RequestParam(value = "state", required = false) Integer state, - @RequestParam(value = "startDate", required = false) String startTime, - @RequestParam(value = "endDate", required = false) String endTime, - @RequestParam("pageNo") Integer pageNo, - @RequestParam("pageSize") Integer pageSize) { - - Result result = checkPageParams(pageNo, pageSize); - if (!result.checkResult()) { - return result; - } + public Result> queryExecuteResultListPaging(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam(value = "searchVal", required = false) String searchVal, + @RequestParam(value = "ruleType", required = false) Integer ruleType, + @RequestParam(value = "state", required = false) Integer state, + @RequestParam(value = "startDate", required = false) String startTime, + @RequestParam(value = "endDate", required = false) String endTime, + @RequestParam("pageNo") Integer pageNo, + @RequestParam("pageSize") Integer pageSize) { + + checkPageParams(pageNo, pageSize); searchVal = ParameterUtils.handleEscapes(searchVal); - return dqExecuteResultService.queryResultListPaging(loginUser, searchVal, state, ruleType, startTime, endTime, - pageNo, pageSize); + PageInfo dqExecuteResultPageInfo = dqExecuteResultService.queryResultListPaging(loginUser, + searchVal, state, ruleType, startTime, endTime, pageNo, pageSize); + return Result.success(dqExecuteResultPageInfo); } /** @@ -186,8 +188,8 @@ public Result queryExecuteResultListPaging(@Parameter(hidden = true) @RequestAtt @GetMapping(value = "/getDatasourceOptionsById") @ResponseStatus(HttpStatus.OK) @ApiException(GET_DATASOURCE_OPTIONS_ERROR) - public Result getDatasourceOptionsById(@RequestParam(value = "datasourceId") int datasourceId) { - Map result = dqRuleService.getDatasourceOptionsById(datasourceId); - return returnDataList(result); + public Result> getDatasourceOptionsById(@RequestParam(value = "datasourceId") int datasourceId) { + List paramsOptions = dqRuleService.getDatasourceOptionsById(datasourceId); + return Result.success(paramsOptions); } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java index 949d21c87488f..c50a6d3da6fc5 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/DataSourceController.java @@ -92,10 +92,11 @@ public class DataSourceController extends BaseController { @PostMapping() @ResponseStatus(HttpStatus.CREATED) @ApiException(CREATE_DATASOURCE_ERROR) - public Result createDataSource(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @Parameter(name = "dataSourceParam", description = "DATA_SOURCE_PARAM", required = true) @RequestBody String jsonStr) { + public Result createDataSource(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @Parameter(name = "dataSourceParam", description = "DATA_SOURCE_PARAM", required = true) @RequestBody String jsonStr) { BaseDataSourceParamDTO dataSourceParam = DataSourceUtils.buildDatasourceParam(jsonStr); - return dataSourceService.createDataSource(loginUser, dataSourceParam); + DataSource dataSource = dataSourceService.createDataSource(loginUser, dataSourceParam); + return Result.success(dataSource); } /** @@ -115,12 +116,13 @@ public Result createDataSource(@Parameter(hidden = true) @RequestAttribu @PutMapping(value = "/{id}") @ResponseStatus(HttpStatus.OK) @ApiException(UPDATE_DATASOURCE_ERROR) - public Result updateDataSource(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @PathVariable(value = "id") Integer id, - @RequestBody String jsonStr) { + public Result updateDataSource(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @PathVariable(value = "id") Integer id, + @RequestBody String jsonStr) { BaseDataSourceParamDTO dataSourceParam = DataSourceUtils.buildDatasourceParam(jsonStr); dataSourceParam.setId(id); - return dataSourceService.updateDataSource(dataSourceParam.getId(), loginUser, dataSourceParam); + DataSource dataSource = dataSourceService.updateDataSource(loginUser, dataSourceParam); + return Result.success(dataSource); } /** @@ -186,10 +188,7 @@ public Result queryDataSourceListPaging(@Parameter(hidden = true) @Reque @RequestParam(value = "searchVal", required = false) String searchVal, @RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize) { - Result result = checkPageParams(pageNo, pageSize); - if (!result.checkResult()) { - return result; - } + checkPageParams(pageNo, pageSize); searchVal = ParameterUtils.handleEscapes(searchVal); PageInfo pageInfo = dataSourceService.queryDataSourceListPaging(loginUser, searchVal, pageNo, pageSize); @@ -208,12 +207,13 @@ public Result queryDataSourceListPaging(@Parameter(hidden = true) @Reque @PostMapping(value = "/connect") @ResponseStatus(HttpStatus.OK) @ApiException(CONNECT_DATASOURCE_FAILURE) - public Result connectDataSource(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "dataSourceParam") @RequestBody String jsonStr) { + public Result connectDataSource(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "dataSourceParam") @RequestBody String jsonStr) { BaseDataSourceParamDTO dataSourceParam = DataSourceUtils.buildDatasourceParam(jsonStr); DataSourceUtils.checkDatasourceParam(dataSourceParam); ConnectionParam connectionParams = DataSourceUtils.buildConnectionParams(dataSourceParam); - return dataSourceService.checkConnection(dataSourceParam.getType(), connectionParams); + dataSourceService.checkConnection(dataSourceParam.getType(), connectionParams); + return Result.success(true); } /** @@ -230,9 +230,10 @@ public Result connectDataSource(@Parameter(hidden = true) @RequestAttrib @GetMapping(value = "/{id}/connect-test") @ResponseStatus(HttpStatus.OK) @ApiException(CONNECTION_TEST_FAILURE) - public Result connectionTest(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @PathVariable("id") int id) { - return dataSourceService.connectionTest(id); + public Result connectionTest(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @PathVariable("id") int id) { + dataSourceService.connectionTest(id); + return Result.success(true); } /** @@ -249,9 +250,10 @@ public Result connectionTest(@Parameter(hidden = true) @RequestAttribute @DeleteMapping(value = "/{id}") @ResponseStatus(HttpStatus.OK) @ApiException(DELETE_DATA_SOURCE_FAILURE) - public Result deleteDataSource(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @PathVariable("id") int id) { - return dataSourceService.delete(loginUser, id); + public Result deleteDataSource(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @PathVariable("id") int id) { + dataSourceService.delete(loginUser, id); + return Result.success(true); } /** @@ -268,9 +270,10 @@ public Result deleteDataSource(@Parameter(hidden = true) @RequestAttribu @GetMapping(value = "/verify-name") @ResponseStatus(HttpStatus.OK) @ApiException(VERIFY_DATASOURCE_NAME_FAILURE) - public Result verifyDataSourceName(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam(value = "name") String name) { - return dataSourceService.verifyDataSourceName(name); + public Result verifyDataSourceName(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam(value = "name") String name) { + dataSourceService.verifyDataSourceName(name); + return Result.success(true); } /** diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/EnvironmentController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/EnvironmentController.java index 47e7d09284c8f..3e33da89090bf 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/EnvironmentController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/EnvironmentController.java @@ -28,6 +28,7 @@ import org.apache.dolphinscheduler.api.service.EnvironmentService; import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.constants.Constants; +import org.apache.dolphinscheduler.dao.entity.Environment; import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.plugin.task.api.utils.ParameterUtils; @@ -79,15 +80,14 @@ public class EnvironmentController extends BaseController { @PostMapping(value = "/create") @ResponseStatus(HttpStatus.CREATED) @ApiException(CREATE_ENVIRONMENT_ERROR) - public Result createEnvironment(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam("name") String name, - @RequestParam("config") String config, - @RequestParam(value = "description", required = false) String description, - @RequestParam(value = "workerGroups", required = false) String workerGroups) { - - Map result = - environmentService.createEnvironment(loginUser, name, config, description, workerGroups); - return returnDataList(result); + public Result createEnvironment(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam("name") String name, + @RequestParam("config") String config, + @RequestParam(value = "description", required = false) String description, + @RequestParam(value = "workerGroups", required = false) String workerGroups) { + + Long environmentCode = environmentService.createEnvironment(loginUser, name, config, description, workerGroups); + return Result.success(environmentCode); } /** @@ -111,15 +111,15 @@ public Result createEnvironment(@Parameter(hidden = true) @RequestAttribute(valu @PostMapping(value = "/update") @ResponseStatus(HttpStatus.OK) @ApiException(UPDATE_ENVIRONMENT_ERROR) - public Result updateEnvironment(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam("code") Long code, - @RequestParam("name") String name, - @RequestParam("config") String config, - @RequestParam(value = "description", required = false) String description, - @RequestParam(value = "workerGroups", required = false) String workerGroups) { - Map result = + public Result updateEnvironment(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam("code") Long code, + @RequestParam("name") String name, + @RequestParam("config") String config, + @RequestParam(value = "description", required = false) String description, + @RequestParam(value = "workerGroups", required = false) String workerGroups) { + Environment environment = environmentService.updateEnvironmentByCode(loginUser, code, name, config, description, workerGroups); - return returnDataList(result); + return Result.success(environment); } /** @@ -164,13 +164,9 @@ public Result queryEnvironmentListPaging(@Parameter(hidden = true) @RequestAttri @RequestParam("pageSize") Integer pageSize, @RequestParam("pageNo") Integer pageNo) { - Result result = checkPageParams(pageNo, pageSize); - if (!result.checkResult()) { - return result; - } + checkPageParams(pageNo, pageSize); searchVal = ParameterUtils.handleEscapes(searchVal); - result = environmentService.queryEnvironmentListPaging(loginUser, pageNo, pageSize, searchVal); - return result; + return environmentService.queryEnvironmentListPaging(loginUser, pageNo, pageSize, searchVal); } /** diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ExecutorController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ExecutorController.java index d6176cfd70cce..22d44a08bf897 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ExecutorController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ExecutorController.java @@ -438,16 +438,16 @@ public Result queryExecutingWorkflow(@RequestParam("id") Integer processInstance @PostMapping(value = "/task-instance/{code}/start") @ResponseStatus(HttpStatus.OK) @ApiException(START_PROCESS_INSTANCE_ERROR) - public Result startStreamTaskInstance(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @Parameter(name = "projectCode", description = "PROJECT_CODE", required = true) @PathVariable long projectCode, - @Parameter(name = "code", description = "TASK_CODE", required = true) @PathVariable long code, - @RequestParam(value = "version", required = true) int version, - @RequestParam(value = "warningGroupId", required = false, defaultValue = "0") Integer warningGroupId, - @RequestParam(value = "workerGroup", required = false, defaultValue = "default") String workerGroup, - @RequestParam(value = "tenantCode", required = false, defaultValue = "default") String tenantCode, - @RequestParam(value = "environmentCode", required = false, defaultValue = "-1") Long environmentCode, - @RequestParam(value = "startParams", required = false) String startParams, - @RequestParam(value = "dryRun", defaultValue = "0", required = false) int dryRun) { + public Result startStreamTaskInstance(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @Parameter(name = "projectCode", description = "PROJECT_CODE", required = true) @PathVariable long projectCode, + @Parameter(name = "code", description = "TASK_CODE", required = true) @PathVariable long code, + @RequestParam(value = "version", required = true) int version, + @RequestParam(value = "warningGroupId", required = false, defaultValue = "0") Integer warningGroupId, + @RequestParam(value = "workerGroup", required = false, defaultValue = "default") String workerGroup, + @RequestParam(value = "tenantCode", required = false, defaultValue = "default") String tenantCode, + @RequestParam(value = "environmentCode", required = false, defaultValue = "-1") Long environmentCode, + @RequestParam(value = "startParams", required = false) String startParams, + @RequestParam(value = "dryRun", defaultValue = "0", required = false) int dryRun) { Map startParamMap = null; if (startParams != null) { @@ -456,9 +456,9 @@ public Result startStreamTaskInstance(@Parameter(hidden = true) @RequestAttribut log.info("Start to execute stream task instance, projectCode:{}, taskDefinitionCode:{}, taskVersion:{}.", projectCode, code, version); - Map result = execService.execStreamTaskInstance(loginUser, projectCode, code, version, - warningGroupId, workerGroup, tenantCode, environmentCode, startParamMap, dryRun); - return returnDataList(result); + execService.execStreamTaskInstance(loginUser, projectCode, code, version, warningGroupId, workerGroup, + tenantCode, environmentCode, startParamMap, dryRun); + return Result.success(true); } /** diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/K8sNamespaceController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/K8sNamespaceController.java index 48ef34dd41b2d..4c8cae74b9ba9 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/K8sNamespaceController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/K8sNamespaceController.java @@ -86,13 +86,9 @@ public Result queryNamespaceListPaging(@Parameter(hidden = true) @RequestAttribu @RequestParam("pageSize") Integer pageSize, @RequestParam("pageNo") Integer pageNo) { - Result result = checkPageParams(pageNo, pageSize); - if (!result.checkResult()) { - return result; - } + checkPageParams(pageNo, pageSize); searchVal = ParameterUtils.handleEscapes(searchVal); - result = k8sNamespaceService.queryListPaging(loginUser, searchVal, pageNo, pageSize); - return result; + return k8sNamespaceService.queryListPaging(loginUser, searchVal, pageNo, pageSize); } /** diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/LoggerController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/LoggerController.java index 435850537de7d..433fade281301 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/LoggerController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/LoggerController.java @@ -131,7 +131,8 @@ public Result queryLog(@Parameter(hidden = true) @RequestAttribute(value @RequestParam(value = "taskInstanceId") int taskInstanceId, @RequestParam(value = "skipLineNum") int skipNum, @RequestParam(value = "limit") int limit) { - return returnDataList(loggerService.queryLog(loginUser, projectCode, taskInstanceId, skipNum, limit)); + String log = loggerService.queryLog(loginUser, projectCode, taskInstanceId, skipNum, limit); + return Result.success(log); } /** diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/MonitorController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/MonitorController.java index 779d230cd4814..26c863b93fbd2 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/MonitorController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/MonitorController.java @@ -25,9 +25,12 @@ import org.apache.dolphinscheduler.api.service.MonitorService; import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.constants.Constants; +import org.apache.dolphinscheduler.common.model.Server; +import org.apache.dolphinscheduler.common.model.WorkerServerModel; import org.apache.dolphinscheduler.dao.entity.User; +import org.apache.dolphinscheduler.dao.plugin.api.monitor.DatabaseMetrics; -import java.util.Map; +import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -62,9 +65,9 @@ public class MonitorController extends BaseController { @GetMapping(value = "/masters") @ResponseStatus(HttpStatus.OK) @ApiException(LIST_MASTERS_ERROR) - public Result listMaster(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { - Map result = monitorService.queryMaster(loginUser); - return returnDataList(result); + public Result> listMaster(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { + List servers = monitorService.queryMaster(loginUser); + return Result.success(servers); } /** @@ -77,9 +80,9 @@ public Result listMaster(@Parameter(hidden = true) @RequestAttribute(value = Con @GetMapping(value = "/workers") @ResponseStatus(HttpStatus.OK) @ApiException(LIST_WORKERS_ERROR) - public Result listWorker(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { - Map result = monitorService.queryWorker(loginUser); - return returnDataList(result); + public Result> listWorker(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { + List workerServerModels = monitorService.queryWorker(loginUser); + return Result.success(workerServerModels); } /** @@ -92,9 +95,9 @@ public Result listWorker(@Parameter(hidden = true) @RequestAttribute(value = Con @GetMapping(value = "/databases") @ResponseStatus(HttpStatus.OK) @ApiException(QUERY_DATABASE_STATE_ERROR) - public Result queryDatabaseState(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { - Map result = monitorService.queryDatabaseState(loginUser); - return returnDataList(result); + public Result> queryDatabaseState(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { + List databaseMetrics = monitorService.queryDatabaseState(loginUser); + return Result.success(databaseMetrics); } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProcessDefinitionController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProcessDefinitionController.java index 65589cea0d6c4..1ccd92dab6a59 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProcessDefinitionController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProcessDefinitionController.java @@ -286,14 +286,9 @@ public Result queryProcessDefinitionVersions(@Parameter(hidden = true) @RequestA @RequestParam(value = "pageSize") int pageSize, @PathVariable(value = "code") long code) { - Result result = checkPageParams(pageNo, pageSize); - if (!result.checkResult()) { - return result; - } - result = processDefinitionService.queryProcessDefinitionVersions(loginUser, projectCode, pageNo, pageSize, + checkPageParams(pageNo, pageSize); + return processDefinitionService.queryProcessDefinitionVersions(loginUser, projectCode, pageNo, pageSize, code); - - return result; } /** @@ -487,10 +482,7 @@ public Result> queryProcessDefinitionListPaging( @RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize) { - Result> result = checkPageParams(pageNo, pageSize); - if (!result.checkResult()) { - return result; - } + checkPageParams(pageNo, pageSize); searchVal = ParameterUtils.handleEscapes(searchVal); PageInfo pageInfo = processDefinitionService.queryProcessDefinitionListPaging( diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProcessInstanceController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProcessInstanceController.java index 09b4001339509..85f9cbcf08170 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProcessInstanceController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProcessInstanceController.java @@ -116,15 +116,11 @@ public Result queryProcessInstanceList(@Parameter(hidden = true) @RequestAttribu @RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize) { - Result result = checkPageParams(pageNo, pageSize); - if (!result.checkResult()) { - return result; - } + checkPageParams(pageNo, pageSize); searchVal = ParameterUtils.handleEscapes(searchVal); - result = processInstanceService.queryProcessInstanceList(loginUser, projectCode, processDefineCode, startTime, + return processInstanceService.queryProcessInstanceList(loginUser, projectCode, processDefineCode, startTime, endTime, searchVal, executorName, stateType, host, otherParamsJson, pageNo, pageSize); - return result; } /** diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProjectController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProjectController.java index cdc15865b3c0b..7618126f91998 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProjectController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProjectController.java @@ -156,14 +156,9 @@ public Result queryProjectListPaging(@Parameter(hidden = true) @RequestAttribute @RequestParam("pageSize") Integer pageSize, @RequestParam("pageNo") Integer pageNo) { - Result result = checkPageParams(pageNo, pageSize); - if (!result.checkResult()) { - log.warn("Pagination parameters check failed, pageNo:{}, pageSize:{}", pageNo, pageSize); - return result; - } + checkPageParams(pageNo, pageSize); searchVal = ParameterUtils.handleEscapes(searchVal); - result = projectService.queryProjectListPaging(loginUser, pageSize, pageNo, searchVal); - return result; + return projectService.queryProjectListPaging(loginUser, pageSize, pageNo, searchVal); } /** @@ -192,14 +187,10 @@ public Result queryProjectWithAuthorizedLevelListPaging(@Parameter(hidden = true @RequestParam("pageSize") Integer pageSize, @RequestParam("pageNo") Integer pageNo) { - Result result = checkPageParams(pageNo, pageSize); - if (!result.checkResult()) { - return result; - } + checkPageParams(pageNo, pageSize); searchVal = ParameterUtils.handleEscapes(searchVal); - result = projectService.queryProjectWithAuthorizedLevelListPaging(userId, loginUser, pageSize, pageNo, + return projectService.queryProjectWithAuthorizedLevelListPaging(userId, loginUser, pageSize, pageNo, searchVal); - return result; } /** diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProjectParameterController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProjectParameterController.java index 0c73801c7ab28..706c7e939aa31 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProjectParameterController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProjectParameterController.java @@ -136,11 +136,7 @@ public Result queryProjectParameterListPaging( @RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize) { - Result result = checkPageParams(pageNo, pageSize); - if (!result.checkResult()) { - log.warn("Pagination parameters check failed, pageNo:{}, pageSize:{}", pageNo, pageSize); - return result; - } + checkPageParams(pageNo, pageSize); searchVal = ParameterUtils.handleEscapes(searchVal); return projectParameterService.queryProjectParameterListPaging(loginUser, projectCode, pageSize, pageNo, searchVal); diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/QueueController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/QueueController.java index 65e23a9069248..dbbc7d858e319 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/QueueController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/QueueController.java @@ -25,12 +25,14 @@ import org.apache.dolphinscheduler.api.exceptions.ApiException; import org.apache.dolphinscheduler.api.service.QueueService; +import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.constants.Constants; +import org.apache.dolphinscheduler.dao.entity.Queue; import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.plugin.task.api.utils.ParameterUtils; -import java.util.Map; +import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -72,8 +74,9 @@ public class QueueController extends BaseController { @GetMapping(value = "/list") @ResponseStatus(HttpStatus.OK) @ApiException(QUERY_QUEUE_LIST_ERROR) - public Result queryList(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { - return queueService.queryList(loginUser); + public Result> queryList(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { + List queues = queueService.queryList(loginUser); + return Result.success(queues); } /** @@ -94,18 +97,15 @@ public Result queryList(@Parameter(hidden = true) @RequestAttribute(value = Cons @GetMapping() @ResponseStatus(HttpStatus.OK) @ApiException(QUERY_QUEUE_LIST_ERROR) - public Result queryQueueListPaging(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam("pageNo") Integer pageNo, - @RequestParam(value = "searchVal", required = false) String searchVal, - @RequestParam("pageSize") Integer pageSize) { - Result result = checkPageParams(pageNo, pageSize); - if (!result.checkResult()) { - return result; - } + public Result> queryQueueListPaging(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam("pageNo") Integer pageNo, + @RequestParam(value = "searchVal", required = false) String searchVal, + @RequestParam("pageSize") Integer pageSize) { + checkPageParams(pageNo, pageSize); searchVal = ParameterUtils.handleEscapes(searchVal); - result = queueService.queryList(loginUser, searchVal, pageNo, pageSize); - return result; + PageInfo queuePageInfo = queueService.queryList(loginUser, searchVal, pageNo, pageSize); + return Result.success(queuePageInfo); } /** @@ -124,10 +124,10 @@ public Result queryQueueListPaging(@Parameter(hidden = true) @RequestAttribute(v @PostMapping() @ResponseStatus(HttpStatus.CREATED) @ApiException(CREATE_QUEUE_ERROR) - public Result createQueue(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam(value = "queue") String queue, - @RequestParam(value = "queueName") String queueName) { - return queueService.createQueue(loginUser, queue, queueName); + public Result createQueue(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam(value = "queue") String queue, + @RequestParam(value = "queueName") String queueName) { + return Result.success(queueService.createQueue(loginUser, queue, queueName)); } /** @@ -148,11 +148,11 @@ public Result createQueue(@Parameter(hidden = true) @RequestAttribute(value = Co @PutMapping(value = "/{id}") @ResponseStatus(HttpStatus.CREATED) @ApiException(UPDATE_QUEUE_ERROR) - public Result updateQueue(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @PathVariable(value = "id") int id, - @RequestParam(value = "queue") String queue, - @RequestParam(value = "queueName") String queueName) { - return queueService.updateQueue(loginUser, id, queue, queueName); + public Result updateQueue(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @PathVariable(value = "id") int id, + @RequestParam(value = "queue") String queue, + @RequestParam(value = "queueName") String queueName) { + return Result.success(queueService.updateQueue(loginUser, id, queue, queueName)); } /** @@ -169,10 +169,10 @@ public Result updateQueue(@Parameter(hidden = true) @RequestAttribute(value = Co @DeleteMapping(value = "/{id}") @ResponseStatus(HttpStatus.OK) @ApiException(DELETE_QUEUE_BY_ID_ERROR) - public Result deleteQueueById(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @PathVariable(value = "id") int id) throws Exception { - Map result = queueService.deleteQueueById(loginUser, id); - return returnDataList(result); + public Result deleteQueueById(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @PathVariable(value = "id") int id) throws Exception { + queueService.deleteQueueById(loginUser, id); + return Result.success(true); } /** @@ -191,9 +191,10 @@ public Result deleteQueueById(@Parameter(hidden = true) @RequestAttribute(value @PostMapping(value = "/verify") @ResponseStatus(HttpStatus.OK) @ApiException(VERIFY_QUEUE_ERROR) - public Result verifyQueue(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam(value = "queue") String queue, - @RequestParam(value = "queueName") String queueName) { - return queueService.verifyQueue(queue, queueName); + public Result verifyQueue(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam(value = "queue") String queue, + @RequestParam(value = "queueName") String queueName) { + queueService.verifyQueue(queue, queueName); + return Result.success(true); } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ResourcesController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ResourcesController.java index 69c19b32b36df..6fff27acafc5e 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ResourcesController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ResourcesController.java @@ -227,15 +227,11 @@ public Result> queryResourceListPaging(@Parameter(hidden @RequestParam("pageNo") Integer pageNo, @RequestParam(value = "searchVal", required = false) String searchVal, @RequestParam("pageSize") Integer pageSize) { - Result> result = checkPageParams(pageNo, pageSize); - if (!result.checkResult()) { - return result; - } + checkPageParams(pageNo, pageSize); searchVal = ParameterUtils.handleEscapes(searchVal); - result = resourceService.queryResourceListPaging(loginUser, fullName, tenantCode, type, searchVal, pageNo, + return resourceService.queryResourceListPaging(loginUser, fullName, tenantCode, type, searchVal, pageNo, pageSize); - return result; } /** @@ -567,10 +563,7 @@ public Result queryUdfFuncListPaging(@Parameter(hidden = true) @RequestA @RequestParam("pageNo") Integer pageNo, @RequestParam(value = "searchVal", required = false) String searchVal, @RequestParam("pageSize") Integer pageSize) { - Result result = checkPageParams(pageNo, pageSize); - if (!result.checkResult()) { - return result; - } + checkPageParams(pageNo, pageSize); return udfFuncService.queryUdfFuncListPaging(loginUser, searchVal, pageNo, pageSize); } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/SchedulerController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/SchedulerController.java index 164127c4e6810..57b505c65096f 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/SchedulerController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/SchedulerController.java @@ -248,14 +248,10 @@ public Result queryScheduleListPaging(@Parameter(hidden = true) @RequestAttribut @RequestParam(value = "searchVal", required = false) String searchVal, @RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize) { - Result result = checkPageParams(pageNo, pageSize); - if (!result.checkResult()) { - return result; - } + checkPageParams(pageNo, pageSize); searchVal = ParameterUtils.handleEscapes(searchVal); - result = schedulerService.querySchedule(loginUser, projectCode, processDefinitionCode, searchVal, pageNo, + return schedulerService.querySchedule(loginUser, projectCode, processDefinitionCode, searchVal, pageNo, pageSize); - return result; } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/TaskDefinitionController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/TaskDefinitionController.java index e22e0eb00d003..458f747255818 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/TaskDefinitionController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/TaskDefinitionController.java @@ -208,10 +208,7 @@ public Result queryTaskDefinitionVersions(@Parameter(hidden = true) @RequestAttr @PathVariable(value = "code") long code, @RequestParam(value = "pageNo") int pageNo, @RequestParam(value = "pageSize") int pageSize) { - Result result = checkPageParams(pageNo, pageSize); - if (!result.checkResult()) { - return result; - } + checkPageParams(pageNo, pageSize); return taskDefinitionService.queryTaskDefinitionVersions(loginUser, projectCode, code, pageNo, pageSize); } @@ -343,10 +340,7 @@ public Result queryTaskDefinitionListPaging(@Parameter(hidden = true) @RequestAt @RequestParam(value = "taskExecuteType", required = false, defaultValue = "BATCH") TaskExecuteType taskExecuteType, @RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize) { - Result result = checkPageParams(pageNo, pageSize); - if (!result.checkResult()) { - return result; - } + checkPageParams(pageNo, pageSize); searchTaskName = ParameterUtils.handleEscapes(searchTaskName); return taskDefinitionService.queryTaskDefinitionListPaging(loginUser, projectCode, searchTaskName, taskType, taskExecuteType, pageNo, pageSize); diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/TaskInstanceController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/TaskInstanceController.java index 1136e06afe0d7..2aa2a5eb72568 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/TaskInstanceController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/TaskInstanceController.java @@ -114,12 +114,9 @@ public Result queryTaskListPaging(@Parameter(hidden = true) @RequestAttribute(va @RequestParam(value = "taskExecuteType", required = false, defaultValue = "BATCH") TaskExecuteType taskExecuteType, @RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize) { - Result result = checkPageParams(pageNo, pageSize); - if (!result.checkResult()) { - return result; - } + checkPageParams(pageNo, pageSize); searchVal = ParameterUtils.handleEscapes(searchVal); - result = taskInstanceService.queryTaskListPaging( + return taskInstanceService.queryTaskListPaging( loginUser, projectCode, processInstanceId, @@ -136,7 +133,6 @@ public Result queryTaskListPaging(@Parameter(hidden = true) @RequestAttribute(va taskExecuteType, pageNo, pageSize); - return result; } /** diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/TenantController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/TenantController.java index 4e7367263af34..7554d6135d069 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/TenantController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/TenantController.java @@ -26,12 +26,14 @@ import org.apache.dolphinscheduler.api.exceptions.ApiException; import org.apache.dolphinscheduler.api.service.TenantService; +import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.constants.Constants; +import org.apache.dolphinscheduler.dao.entity.Tenant; import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.plugin.task.api.utils.ParameterUtils; -import java.util.Map; +import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -81,13 +83,13 @@ public class TenantController extends BaseController { @PostMapping() @ResponseStatus(HttpStatus.CREATED) @ApiException(CREATE_TENANT_ERROR) - public Result createTenant(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam(value = "tenantCode") String tenantCode, - @RequestParam(value = "queueId") int queueId, - @RequestParam(value = "description", required = false) String description) throws Exception { + public Result createTenant(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam(value = "tenantCode") String tenantCode, + @RequestParam(value = "queueId") int queueId, + @RequestParam(value = "description", required = false) String description) throws Exception { - Map result = tenantService.createTenant(loginUser, tenantCode, queueId, description); - return returnDataList(result); + Tenant tenant = tenantService.createTenant(loginUser, tenantCode, queueId, description); + return Result.success(tenant); } /** @@ -108,18 +110,14 @@ public Result createTenant(@Parameter(hidden = true) @RequestAttribute(value = C @GetMapping() @ResponseStatus(HttpStatus.OK) @ApiException(QUERY_TENANT_LIST_PAGING_ERROR) - public Result queryTenantlistPaging(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam(value = "searchVal", required = false) String searchVal, - @RequestParam("pageNo") Integer pageNo, - @RequestParam("pageSize") Integer pageSize) { - Result result = checkPageParams(pageNo, pageSize); - if (!result.checkResult()) { - return result; - - } + public Result> queryTenantlistPaging(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam(value = "searchVal", required = false) String searchVal, + @RequestParam("pageNo") Integer pageNo, + @RequestParam("pageSize") Integer pageSize) { + checkPageParams(pageNo, pageSize); searchVal = ParameterUtils.handleEscapes(searchVal); - result = tenantService.queryTenantList(loginUser, searchVal, pageNo, pageSize); - return result; + PageInfo tenantPageInfo = tenantService.queryTenantList(loginUser, searchVal, pageNo, pageSize); + return Result.success(tenantPageInfo); } /** @@ -132,9 +130,9 @@ public Result queryTenantlistPaging(@Parameter(hidden = true) @RequestAttribute( @GetMapping(value = "/list") @ResponseStatus(HttpStatus.OK) @ApiException(QUERY_TENANT_LIST_ERROR) - public Result queryTenantlist(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { - Map result = tenantService.queryTenantList(loginUser); - return returnDataList(result); + public Result> queryTenantlist(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { + List tenants = tenantService.queryTenantList(loginUser); + return Result.success(tenants); } /** @@ -157,14 +155,14 @@ public Result queryTenantlist(@Parameter(hidden = true) @RequestAttribute(value @PutMapping(value = "/{id}") @ResponseStatus(HttpStatus.OK) @ApiException(UPDATE_TENANT_ERROR) - public Result updateTenant(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @PathVariable(value = "id") int id, - @RequestParam(value = "tenantCode") String tenantCode, - @RequestParam(value = "queueId") int queueId, - @RequestParam(value = "description", required = false) String description) throws Exception { - - Map result = tenantService.updateTenant(loginUser, id, tenantCode, queueId, description); - return returnDataList(result); + public Result updateTenant(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @PathVariable(value = "id") int id, + @RequestParam(value = "tenantCode") String tenantCode, + @RequestParam(value = "queueId") int queueId, + @RequestParam(value = "description", required = false) String description) throws Exception { + + tenantService.updateTenant(loginUser, id, tenantCode, queueId, description); + return Result.success(true); } /** @@ -181,10 +179,10 @@ public Result updateTenant(@Parameter(hidden = true) @RequestAttribute(value = C @DeleteMapping(value = "/{id}") @ResponseStatus(HttpStatus.OK) @ApiException(DELETE_TENANT_BY_ID_ERROR) - public Result deleteTenantById(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @PathVariable(value = "id") int id) throws Exception { - Map result = tenantService.deleteTenantById(loginUser, id); - return returnDataList(result); + public Result deleteTenantById(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @PathVariable(value = "id") int id) throws Exception { + tenantService.deleteTenantById(loginUser, id); + return Result.success(true); } /** @@ -201,9 +199,10 @@ public Result deleteTenantById(@Parameter(hidden = true) @RequestAttribute(value @GetMapping(value = "/verify-code") @ResponseStatus(HttpStatus.OK) @ApiException(VERIFY_OS_TENANT_CODE_ERROR) - public Result verifyTenantCode(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam(value = "tenantCode") String tenantCode) { - return tenantService.verifyTenantCode(tenantCode); + public Result verifyTenantCode(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam(value = "tenantCode") String tenantCode) { + tenantService.verifyTenantCode(tenantCode); + return Result.success(true); } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/UsersController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/UsersController.java index 855d2e37725f3..c376c5a7fc8fb 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/UsersController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/UsersController.java @@ -140,14 +140,9 @@ public Result queryUserList(@Parameter(hidden = true) @RequestAttribute(value = @RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize, @RequestParam(value = "searchVal", required = false) String searchVal) { - Result result = checkPageParams(pageNo, pageSize); - if (!result.checkResult()) { - return result; - - } + checkPageParams(pageNo, pageSize); searchVal = ParameterUtils.handleEscapes(searchVal); - result = usersService.queryUserList(loginUser, searchVal, pageNo, pageSize); - return result; + return usersService.queryUserList(loginUser, searchVal, pageNo, pageSize); } /** diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/WorkFlowLineageController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/WorkFlowLineageController.java index 4dc4973506a01..96e2a200d6bc2 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/WorkFlowLineageController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/WorkFlowLineageController.java @@ -69,32 +69,25 @@ public class WorkFlowLineageController extends BaseController { @Operation(summary = "queryLineageByWorkFlowName", description = "QUERY_WORKFLOW_LINEAGE_BY_NAME_NOTES") @GetMapping(value = "/query-by-name") @ResponseStatus(HttpStatus.OK) + @ApiException(QUERY_WORKFLOW_LINEAGE_ERROR) public Result> queryWorkFlowLineageByName(@Parameter(hidden = true) @RequestAttribute(value = SESSION_USER) User loginUser, @Parameter(name = "projectCode", description = "PROJECT_CODE", required = true) @PathVariable long projectCode, @RequestParam(value = "workFlowName", required = false) String workFlowName) { - try { - workFlowName = ParameterUtils.handleEscapes(workFlowName); - Map result = workFlowLineageService.queryWorkFlowLineageByName(projectCode, workFlowName); - return returnDataList(result); - } catch (Exception e) { - log.error(QUERY_WORKFLOW_LINEAGE_ERROR.getMsg(), e); - return error(QUERY_WORKFLOW_LINEAGE_ERROR.getCode(), QUERY_WORKFLOW_LINEAGE_ERROR.getMsg()); - } + workFlowName = ParameterUtils.handleEscapes(workFlowName); + List workFlowLineages = + workFlowLineageService.queryWorkFlowLineageByName(projectCode, workFlowName); + return Result.success(workFlowLineages); } @Operation(summary = "queryLineageByWorkFlowCode", description = "QUERY_WORKFLOW_LINEAGE_BY_CODE_NOTE") @GetMapping(value = "/{workFlowCode}") @ResponseStatus(HttpStatus.OK) + @ApiException(QUERY_WORKFLOW_LINEAGE_ERROR) public Result> queryWorkFlowLineageByCode(@Parameter(hidden = true) @RequestAttribute(value = SESSION_USER) User loginUser, @Parameter(name = "projectCode", description = "PROJECT_CODE", required = true) @PathVariable long projectCode, @PathVariable(value = "workFlowCode", required = true) long workFlowCode) { - try { - Map result = workFlowLineageService.queryWorkFlowLineageByCode(projectCode, workFlowCode); - return returnDataList(result); - } catch (Exception e) { - log.error(QUERY_WORKFLOW_LINEAGE_ERROR.getMsg(), e); - return error(QUERY_WORKFLOW_LINEAGE_ERROR.getCode(), QUERY_WORKFLOW_LINEAGE_ERROR.getMsg()); - } + Map result = workFlowLineageService.queryWorkFlowLineageByCode(projectCode, workFlowCode); + return Result.success(result); } @Operation(summary = "queryWorkFlowList", description = "QUERY_WORKFLOW_LINEAGE_NOTES") diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/WorkerGroupController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/WorkerGroupController.java index 29be9c61dfe5f..edd6d36879997 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/WorkerGroupController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/WorkerGroupController.java @@ -113,14 +113,9 @@ public Result queryAllWorkerGroupsPaging(@Parameter(hidden = true) @RequestAttri @RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize, @RequestParam(value = "searchVal", required = false) String searchVal) { - Result result = checkPageParams(pageNo, pageSize); - if (!result.checkResult()) { - return result; - - } + checkPageParams(pageNo, pageSize); searchVal = ParameterUtils.handleEscapes(searchVal); - result = workerGroupService.queryAllGroupPaging(loginUser, pageNo, pageSize, searchVal); - return result; + return workerGroupService.queryAllGroupPaging(loginUser, pageNo, pageSize, searchVal); } /** diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/v2/AccessTokenV2Controller.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/v2/AccessTokenV2Controller.java index 6012c7ad58cbe..e7a8115bdd7fe 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/v2/AccessTokenV2Controller.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/v2/AccessTokenV2Controller.java @@ -21,11 +21,11 @@ import org.apache.dolphinscheduler.api.controller.BaseController; import org.apache.dolphinscheduler.api.dto.CreateTokenRequest; -import org.apache.dolphinscheduler.api.dto.CreateTokenResponse; import org.apache.dolphinscheduler.api.exceptions.ApiException; import org.apache.dolphinscheduler.api.service.AccessTokenService; import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.constants.Constants; +import org.apache.dolphinscheduler.dao.entity.AccessToken; import org.apache.dolphinscheduler.dao.entity.User; import org.springframework.beans.factory.annotation.Autowired; @@ -65,12 +65,12 @@ public class AccessTokenV2Controller extends BaseController { @ResponseStatus(HttpStatus.CREATED) @Parameter(name = "createTokenRequest", description = "createTokenRequest", required = true, schema = @Schema(implementation = CreateTokenRequest.class)) @ApiException(CREATE_ACCESS_TOKEN_ERROR) - public CreateTokenResponse createToken(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + public Result createToken(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, @RequestBody CreateTokenRequest createTokenRequest) { - Result result = accessTokenService.createToken(loginUser, + AccessToken accessToken = accessTokenService.createToken(loginUser, createTokenRequest.getUserId(), createTokenRequest.getExpireTime(), createTokenRequest.getToken()); - return new CreateTokenResponse(result); + return Result.success(accessToken); } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/v2/ProjectV2Controller.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/v2/ProjectV2Controller.java index c547cfb9a5626..d01aeb8658ac7 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/v2/ProjectV2Controller.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/v2/ProjectV2Controller.java @@ -31,7 +31,6 @@ import org.apache.dolphinscheduler.api.dto.project.ProjectCreateRequest; import org.apache.dolphinscheduler.api.dto.project.ProjectCreateResponse; import org.apache.dolphinscheduler.api.dto.project.ProjectDeleteResponse; -import org.apache.dolphinscheduler.api.dto.project.ProjectListPagingResponse; import org.apache.dolphinscheduler.api.dto.project.ProjectListResponse; import org.apache.dolphinscheduler.api.dto.project.ProjectQueryRequest; import org.apache.dolphinscheduler.api.dto.project.ProjectQueryResponse; @@ -40,8 +39,10 @@ import org.apache.dolphinscheduler.api.dto.user.UserListResponse; import org.apache.dolphinscheduler.api.exceptions.ApiException; import org.apache.dolphinscheduler.api.service.ProjectService; +import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.constants.Constants; +import org.apache.dolphinscheduler.dao.entity.Project; import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.plugin.task.api.utils.ParameterUtils; @@ -150,16 +151,12 @@ public ProjectQueryResponse queryProjectByCode(@Parameter(hidden = true) @Reques @GetMapping(consumes = {"application/json"}) @ResponseStatus(HttpStatus.OK) @ApiException(LOGIN_USER_QUERY_PROJECT_LIST_PAGING_ERROR) - public ProjectListPagingResponse queryProjectListPaging(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + public Result> queryProjectListPaging(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, ProjectQueryRequest projectQueryReq) { - Result result = checkPageParams(projectQueryReq.getPageNo(), projectQueryReq.getPageSize()); - if (!result.checkResult()) { - return new ProjectListPagingResponse(result); - } + checkPageParams(projectQueryReq.getPageNo(), projectQueryReq.getPageSize()); String searchVal = ParameterUtils.handleEscapes(projectQueryReq.getSearchVal()); - result = projectService.queryProjectListPaging(loginUser, projectQueryReq.getPageSize(), + return projectService.queryProjectListPaging(loginUser, projectQueryReq.getPageSize(), projectQueryReq.getPageNo(), searchVal); - return new ProjectListPagingResponse(result); } /** diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/v2/QueueV2Controller.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/v2/QueueV2Controller.java index 5a7c3329102d5..cd6563e7a26de 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/v2/QueueV2Controller.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/v2/QueueV2Controller.java @@ -24,21 +24,20 @@ import org.apache.dolphinscheduler.api.controller.BaseController; import org.apache.dolphinscheduler.api.dto.queue.QueueCreateRequest; -import org.apache.dolphinscheduler.api.dto.queue.QueueCreateResponse; -import org.apache.dolphinscheduler.api.dto.queue.QueueListPagingResponse; -import org.apache.dolphinscheduler.api.dto.queue.QueueListResponse; import org.apache.dolphinscheduler.api.dto.queue.QueueQueryRequest; import org.apache.dolphinscheduler.api.dto.queue.QueueUpdateRequest; -import org.apache.dolphinscheduler.api.dto.queue.QueueUpdateResponse; import org.apache.dolphinscheduler.api.dto.queue.QueueVerifyRequest; -import org.apache.dolphinscheduler.api.dto.queue.QueueVerifyResponse; import org.apache.dolphinscheduler.api.exceptions.ApiException; import org.apache.dolphinscheduler.api.service.QueueService; +import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.constants.Constants; +import org.apache.dolphinscheduler.dao.entity.Queue; import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.plugin.task.api.utils.ParameterUtils; +import java.util.List; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.GetMapping; @@ -78,9 +77,9 @@ public class QueueV2Controller extends BaseController { @GetMapping(value = "/list") @ResponseStatus(HttpStatus.OK) @ApiException(QUERY_QUEUE_LIST_ERROR) - public QueueListResponse queryList(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { - Result result = queueService.queryList(loginUser); - return new QueueListResponse(result); + public Result> queryList(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { + List queues = queueService.queryList(loginUser); + return Result.success(queues); } /** @@ -99,17 +98,14 @@ public QueueListResponse queryList(@Parameter(hidden = true) @RequestAttribute(v @GetMapping() @ResponseStatus(HttpStatus.OK) @ApiException(QUERY_QUEUE_LIST_ERROR) - public QueueListPagingResponse queryQueueListPaging(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + public Result> queryQueueListPaging(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, QueueQueryRequest queueQueryRequest) { - Result result = checkPageParams(queueQueryRequest.getPageNo(), queueQueryRequest.getPageSize()); - if (!result.checkResult()) { - return new QueueListPagingResponse(result); - } + checkPageParams(queueQueryRequest.getPageNo(), queueQueryRequest.getPageSize()); String searchVal = ParameterUtils.handleEscapes(queueQueryRequest.getSearchVal()); - result = queueService.queryList(loginUser, searchVal, queueQueryRequest.getPageNo(), + PageInfo queuePageInfo = queueService.queryList(loginUser, searchVal, queueQueryRequest.getPageNo(), queueQueryRequest.getPageSize()); - return new QueueListPagingResponse(result); + return Result.success(queuePageInfo); } /** @@ -123,11 +119,10 @@ public QueueListPagingResponse queryQueueListPaging(@Parameter(hidden = true) @R @PostMapping(consumes = {"application/json"}) @ResponseStatus(HttpStatus.CREATED) @ApiException(CREATE_QUEUE_ERROR) - public QueueCreateResponse createQueue(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestBody QueueCreateRequest queueCreateRequest) { - Result result = - queueService.createQueue(loginUser, queueCreateRequest.getQueue(), queueCreateRequest.getQueueName()); - return new QueueCreateResponse(result); + public Result createQueue(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestBody QueueCreateRequest queueCreateRequest) { + return Result.success( + queueService.createQueue(loginUser, queueCreateRequest.getQueue(), queueCreateRequest.getQueueName())); } /** @@ -145,12 +140,11 @@ public QueueCreateResponse createQueue(@Parameter(hidden = true) @RequestAttribu @PutMapping(value = "/{id}", consumes = {"application/json"}) @ResponseStatus(HttpStatus.CREATED) @ApiException(UPDATE_QUEUE_ERROR) - public QueueUpdateResponse updateQueue(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @PathVariable(value = "id") int id, - @RequestBody QueueUpdateRequest queueUpdateRequest) { - Result result = queueService.updateQueue(loginUser, id, queueUpdateRequest.getQueue(), - queueUpdateRequest.getQueueName()); - return new QueueUpdateResponse(result); + public Result updateQueue(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @PathVariable(value = "id") int id, + @RequestBody QueueUpdateRequest queueUpdateRequest) { + return Result.success(queueService.updateQueue(loginUser, id, queueUpdateRequest.getQueue(), + queueUpdateRequest.getQueueName())); } /** @@ -164,9 +158,9 @@ public QueueUpdateResponse updateQueue(@Parameter(hidden = true) @RequestAttribu @PostMapping(value = "/verify", consumes = {"application/json"}) @ResponseStatus(HttpStatus.OK) @ApiException(VERIFY_QUEUE_ERROR) - public QueueVerifyResponse verifyQueue(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestBody QueueVerifyRequest queueVerifyRequest) { - Result result = queueService.verifyQueue(queueVerifyRequest.getQueue(), queueVerifyRequest.getQueueName()); - return new QueueVerifyResponse(result); + public Result verifyQueue(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestBody QueueVerifyRequest queueVerifyRequest) { + queueService.verifyQueue(queueVerifyRequest.getQueue(), queueVerifyRequest.getQueueName()); + return Result.success(true); } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/v2/StatisticsV2Controller.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/v2/StatisticsV2Controller.java index bab04989f3c7a..f23a29671c5c3 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/v2/StatisticsV2Controller.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/v2/StatisticsV2Controller.java @@ -25,6 +25,8 @@ import static org.apache.dolphinscheduler.api.enums.Status.QUERY_WORKFLOW_STATES_COUNT_ERROR; import org.apache.dolphinscheduler.api.controller.BaseController; +import org.apache.dolphinscheduler.api.dto.DefineUserDto; +import org.apache.dolphinscheduler.api.dto.TaskCountDto; import org.apache.dolphinscheduler.api.dto.project.StatisticsStateRequest; import org.apache.dolphinscheduler.api.exceptions.ApiException; import org.apache.dolphinscheduler.api.service.DataAnalysisService; @@ -76,7 +78,8 @@ public Result queryWorkflowInstanceCounts(@Parameter(hidden = true) @RequestAttr /** * query all workflow states count - * @param loginUser login user + * + * @param loginUser login user * @param statisticsStateRequest statisticsStateRequest * @return workflow states count */ @@ -84,16 +87,16 @@ public Result queryWorkflowInstanceCounts(@Parameter(hidden = true) @RequestAttr @GetMapping(value = "/workflows/states/count") @ResponseStatus(HttpStatus.OK) @ApiException(QUERY_WORKFLOW_STATES_COUNT_ERROR) - public Result queryWorkflowStatesCounts(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestBody(required = false) StatisticsStateRequest statisticsStateRequest) { - Map result = - dataAnalysisService.countWorkflowStates(loginUser, statisticsStateRequest); - return returnDataList(result); + public Result queryWorkflowStatesCounts(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestBody(required = false) StatisticsStateRequest statisticsStateRequest) { + TaskCountDto taskCountDto = dataAnalysisService.countWorkflowStates(loginUser, statisticsStateRequest); + return Result.success(taskCountDto); } /** * query one workflow states count - * @param loginUser login user + * + * @param loginUser login user * @param workflowCode workflowCode * @return workflow states count */ @@ -101,16 +104,16 @@ public Result queryWorkflowStatesCounts(@Parameter(hidden = true) @RequestAttrib @GetMapping(value = "/{workflowCode}/states/count") @ResponseStatus(HttpStatus.OK) @ApiException(QUERY_ONE_WORKFLOW_STATE_COUNT_ERROR) - public Result queryOneWorkflowStates(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @PathVariable("workflowCode") Long workflowCode) { - Map result = - dataAnalysisService.countOneWorkflowStates(loginUser, workflowCode); - return returnDataList(result); + public Result queryOneWorkflowStates(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @PathVariable("workflowCode") Long workflowCode) { + TaskCountDto taskCountDto = dataAnalysisService.countOneWorkflowStates(loginUser, workflowCode); + return Result.success(taskCountDto); } /** * query all task states count - * @param loginUser login user + * + * @param loginUser login user * @param statisticsStateRequest statisticsStateRequest * @return tasks states count */ @@ -118,33 +121,33 @@ public Result queryOneWorkflowStates(@Parameter(hidden = true) @RequestAttribute @GetMapping(value = "/tasks/states/count") @ResponseStatus(HttpStatus.OK) @ApiException(QUERY_TASK_STATES_COUNT_ERROR) - public Result queryTaskStatesCounts(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestBody(required = false) StatisticsStateRequest statisticsStateRequest) { - Map result = - dataAnalysisService.countTaskStates(loginUser, statisticsStateRequest); - return returnDataList(result); + public Result queryTaskStatesCounts(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestBody(required = false) StatisticsStateRequest statisticsStateRequest) { + TaskCountDto taskCountDto = dataAnalysisService.countTaskStates(loginUser, statisticsStateRequest); + return Result.success(taskCountDto); } /** * query one task states count + * * @param loginUser login user - * @param taskCode taskCode + * @param taskCode taskCode * @return tasks states count */ @Operation(summary = "queryOneTaskStatesCount", description = "QUERY_ONE_TASK_STATES_COUNT") @GetMapping(value = "/tasks/{taskCode}/states/count") @ResponseStatus(HttpStatus.OK) @ApiException(QUERY_ONE_TASK_STATES_COUNT_ERROR) - public Result queryOneTaskStatesCounts(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @PathVariable("taskCode") Long taskCode) { - Map result = - dataAnalysisService.countOneTaskStates(loginUser, taskCode); - return returnDataList(result); + public Result queryOneTaskStatesCounts(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @PathVariable("taskCode") Long taskCode) { + TaskCountDto taskCountDto = dataAnalysisService.countOneTaskStates(loginUser, taskCode); + return Result.success(taskCountDto); } /** * statistics the workflow quantities of certain user - * @param loginUser login user + * + * @param loginUser login user * @param statisticsStateRequest statisticsStateRequest * @return workflow count in project code */ @@ -152,47 +155,51 @@ public Result queryOneTaskStatesCounts(@Parameter(hidden = true) @RequestAttribu @GetMapping(value = "/workflows/users/count") @ResponseStatus(HttpStatus.OK) @ApiException(COUNT_PROCESS_DEFINITION_USER_ERROR) - public Result countDefinitionByUser(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestBody(required = false) StatisticsStateRequest statisticsStateRequest) { + public Result countDefinitionByUser(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestBody(required = false) StatisticsStateRequest statisticsStateRequest) { String projectName = statisticsStateRequest.getProjectName(); Long projectCode = statisticsStateRequest.getProjectCode(); if (null == projectCode && !StringUtils.isBlank(projectName)) { projectCode = dataAnalysisService.getProjectCodeByName(projectName); } - Map result = dataAnalysisService.countDefinitionByUserV2(loginUser, projectCode, null, null); - return returnDataList(result); + DefineUserDto defineUserDto = dataAnalysisService.countDefinitionByUserV2(loginUser, projectCode, null, null); + return Result.success(defineUserDto); } /** * statistics the workflow quantities of certain userId + * * @param loginUser login user - * @param userId userId + * @param userId userId * @return workflow count in project code */ @Operation(summary = "countDefinitionV2ByUser", description = "COUNT_PROCESS_DEFINITION_V2_BY_USER_NOTES") @GetMapping(value = "/workflows/users/{userId}/count") @ResponseStatus(HttpStatus.OK) @ApiException(COUNT_PROCESS_DEFINITION_USER_ERROR) - public Result countDefinitionByUserId(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @PathVariable("userId") Integer userId) { - Map result = dataAnalysisService.countDefinitionByUserV2(loginUser, null, userId, null); - return returnDataList(result); + public Result countDefinitionByUserId(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @PathVariable("userId") Integer userId) { + DefineUserDto defineUserDto = dataAnalysisService.countDefinitionByUserV2(loginUser, null, userId, null); + return Result.success(defineUserDto); } + /** * statistics the workflow quantities of certain userId and releaseState - * @param loginUser login user - * @param userId userId - * @param releaseState releaseState - * @return workflow count in project code - */ + * + * @param loginUser login user + * @param userId userId + * @param releaseState releaseState + * @return workflow count in project code + */ @Operation(summary = "countDefinitionV2ByUser", description = "COUNT_PROCESS_DEFINITION_V2_BY_USER_NOTES") @GetMapping(value = "/workflows/users/{userId}/{releaseState}/count") @ResponseStatus(HttpStatus.OK) @ApiException(COUNT_PROCESS_DEFINITION_USER_ERROR) - public Result countDefinitionByUserState(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @PathVariable("userId") Integer userId, - @PathVariable("releaseState") Integer releaseState) { - Map result = dataAnalysisService.countDefinitionByUserV2(loginUser, null, userId, releaseState); - return returnDataList(result); + public Result countDefinitionByUserState(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @PathVariable("userId") Integer userId, + @PathVariable("releaseState") Integer releaseState) { + DefineUserDto defineUserDto = + dataAnalysisService.countDefinitionByUserV2(loginUser, null, userId, releaseState); + return Result.success(defineUserDto); } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/v2/TaskInstanceV2Controller.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/v2/TaskInstanceV2Controller.java index ca956374a3908..3e3a87681b69c 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/v2/TaskInstanceV2Controller.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/v2/TaskInstanceV2Controller.java @@ -24,11 +24,11 @@ import static org.apache.dolphinscheduler.api.enums.Status.TASK_STOP_ERROR; import org.apache.dolphinscheduler.api.controller.BaseController; -import org.apache.dolphinscheduler.api.dto.taskInstance.TaskInstanceListPagingResponse; import org.apache.dolphinscheduler.api.dto.taskInstance.TaskInstanceQueryRequest; import org.apache.dolphinscheduler.api.dto.taskInstance.TaskInstanceSuccessResponse; import org.apache.dolphinscheduler.api.exceptions.ApiException; import org.apache.dolphinscheduler.api.service.TaskInstanceService; +import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.constants.Constants; import org.apache.dolphinscheduler.dao.entity.TaskInstance; @@ -90,15 +90,13 @@ public class TaskInstanceV2Controller extends BaseController { @GetMapping(consumes = {"application/json"}) @ResponseStatus(HttpStatus.OK) @ApiException(QUERY_TASK_LIST_PAGING_ERROR) - public TaskInstanceListPagingResponse queryTaskListPaging(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + public Result> queryTaskListPaging(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, @Parameter(name = "projectCode", description = "PROJECT_CODE", required = true) @PathVariable long projectCode, TaskInstanceQueryRequest taskInstanceQueryReq) { - Result result = checkPageParams(taskInstanceQueryReq.getPageNo(), taskInstanceQueryReq.getPageSize()); - if (!result.checkResult()) { - return new TaskInstanceListPagingResponse(result); - } + checkPageParams(taskInstanceQueryReq.getPageNo(), taskInstanceQueryReq.getPageSize()); + String searchVal = ParameterUtils.handleEscapes(taskInstanceQueryReq.getSearchVal()); - result = taskInstanceService.queryTaskListPaging(loginUser, projectCode, + return taskInstanceService.queryTaskListPaging(loginUser, projectCode, taskInstanceQueryReq.getProcessInstanceId(), taskInstanceQueryReq.getProcessInstanceName(), taskInstanceQueryReq.getProcessDefinitionName(), taskInstanceQueryReq.getTaskName(), taskInstanceQueryReq.getTaskCode(), @@ -107,7 +105,6 @@ public TaskInstanceListPagingResponse queryTaskListPaging(@Parameter(hidden = tr taskInstanceQueryReq.getStateType(), taskInstanceQueryReq.getHost(), taskInstanceQueryReq.getTaskExecuteType(), taskInstanceQueryReq.getPageNo(), taskInstanceQueryReq.getPageSize()); - return new TaskInstanceListPagingResponse(result); } /** diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/v2/WorkflowInstanceV2Controller.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/v2/WorkflowInstanceV2Controller.java index eecd0605cec30..c44e0c4f8ad41 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/v2/WorkflowInstanceV2Controller.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/v2/WorkflowInstanceV2Controller.java @@ -74,14 +74,8 @@ public class WorkflowInstanceV2Controller extends BaseController { @ApiException(Status.QUERY_PROCESS_INSTANCE_LIST_PAGING_ERROR) public Result queryWorkflowInstanceListPaging(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, @RequestBody WorkflowInstanceQueryRequest workflowInstanceQueryRequest) { - Result result = - checkPageParams(workflowInstanceQueryRequest.getPageNo(), workflowInstanceQueryRequest.getPageSize()); - if (!result.checkResult()) { - return result; - } - - result = processInstanceService.queryProcessInstanceList(loginUser, workflowInstanceQueryRequest); - return result; + checkPageParams(workflowInstanceQueryRequest.getPageNo(), workflowInstanceQueryRequest.getPageSize()); + return processInstanceService.queryProcessInstanceList(loginUser, workflowInstanceQueryRequest); } /** diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/queue/QueueCreateResponse.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/queue/QueueCreateResponse.java deleted file mode 100644 index 6d508f40d6b12..0000000000000 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/queue/QueueCreateResponse.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.dolphinscheduler.api.dto.queue; - -import org.apache.dolphinscheduler.api.utils.Result; -import org.apache.dolphinscheduler.dao.entity.Queue; - -import lombok.Data; - -/** - * queue create response - */ -@Data -public class QueueCreateResponse extends Result { - - private Queue data; - - public QueueCreateResponse(Result result) { - super(); - this.setCode(result.getCode()); - this.setMsg(result.getMsg()); - this.setData((Queue) result.getData()); - } -} diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/queue/QueueListPagingResponse.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/queue/QueueListPagingResponse.java deleted file mode 100644 index da4f542cbf3a9..0000000000000 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/queue/QueueListPagingResponse.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.dolphinscheduler.api.dto.queue; - -import org.apache.dolphinscheduler.api.utils.PageInfo; -import org.apache.dolphinscheduler.api.utils.Result; -import org.apache.dolphinscheduler.dao.entity.Queue; - -import lombok.Data; - -/** - * queue list paging response - */ -@Data -public class QueueListPagingResponse extends Result { - - private PageInfo data; - - public QueueListPagingResponse(Result result) { - super(); - this.setCode(result.getCode()); - this.setMsg(result.getMsg()); - this.setData(result.getData()); - } -} diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/queue/QueueListResponse.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/queue/QueueListResponse.java deleted file mode 100644 index d0bfbb2abf1ac..0000000000000 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/queue/QueueListResponse.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.dolphinscheduler.api.dto.queue; - -import org.apache.dolphinscheduler.api.utils.Result; -import org.apache.dolphinscheduler.common.utils.JSONUtils; -import org.apache.dolphinscheduler.dao.entity.Project; -import org.apache.dolphinscheduler.dao.entity.Queue; - -import java.util.List; - -import lombok.Data; - -/** - * queue List response - */ -@Data -public class QueueListResponse extends Result { - - private List data; - - public QueueListResponse(Result result) { - super(); - this.setCode(result.getCode()); - this.setMsg(result.getMsg()); - this.setData(JSONUtils.toList(JSONUtils.toJsonString(result.getData()), Project.class)); - } -} diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/queue/QueueUpdateResponse.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/queue/QueueUpdateResponse.java deleted file mode 100644 index a0edf4b315b9f..0000000000000 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/queue/QueueUpdateResponse.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.dolphinscheduler.api.dto.queue; - -import org.apache.dolphinscheduler.api.utils.Result; -import org.apache.dolphinscheduler.dao.entity.Queue; - -import lombok.Data; - -/** - * queue update response - */ -@Data -public class QueueUpdateResponse extends Result { - - private Queue data; - - public QueueUpdateResponse(Result result) { - super(); - this.setCode(result.getCode()); - this.setMsg(result.getMsg()); - this.setData((Queue) result.getData()); - } -} diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/queue/QueueVerifyResponse.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/queue/QueueVerifyResponse.java deleted file mode 100644 index fea6c9079f14e..0000000000000 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/queue/QueueVerifyResponse.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.dolphinscheduler.api.dto.queue; - -import org.apache.dolphinscheduler.api.utils.Result; -import org.apache.dolphinscheduler.dao.entity.Queue; - -import lombok.Data; - -/** - * queue verify response - */ -@Data -public class QueueVerifyResponse extends Result { - - private Queue data; - - public QueueVerifyResponse(Result result) { - super(); - this.setCode(result.getCode()); - this.setMsg(result.getMsg()); - this.setData((Queue) result.getData()); - } -} diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/enums/Status.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/enums/Status.java index e37eca0f3fa5f..80d98664302c3 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/enums/Status.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/enums/Status.java @@ -440,7 +440,7 @@ public enum Status { QUERY_ACCESSTOKEN_LIST_PAGING_ERROR(70012, "query access token list paging error", "分页查询访问token列表错误"), UPDATE_ACCESS_TOKEN_ERROR(70013, "update access token error", "更新访问token错误"), DELETE_ACCESS_TOKEN_ERROR(70014, "delete access token error", "删除访问token错误"), - ACCESS_TOKEN_NOT_EXIST(70015, "access token not exist", "访问token不存在"), + ACCESS_TOKEN_NOT_EXIST(70015, "access token not exist, tokenId {0}", "访问token不存在, {0}"), QUERY_ACCESSTOKEN_BY_USER_ERROR(70016, "query access token by user error", "查询访问指定用户的token错误"), COMMAND_STATE_COUNT_ERROR(80001, "task instance state count error", "查询各状态任务实例数错误"), diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/exceptions/ApiExceptionHandler.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/exceptions/ApiExceptionHandler.java index 7a7b6700d58ab..3c978795ab4b7 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/exceptions/ApiExceptionHandler.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/exceptions/ApiExceptionHandler.java @@ -36,20 +36,19 @@ public class ApiExceptionHandler { @ExceptionHandler(ServiceException.class) - public Result exceptionHandler(ServiceException e, HandlerMethod hm) { - log.error("ServiceException: ", e); - return new Result(e.getCode(), e.getMessage()); + public Result exceptionHandler(ServiceException e, HandlerMethod hm) { + log.error("{} Meet a ServiceException: {}", hm.getShortLogMessage(), e.getMessage()); + return new Result<>(e.getCode(), e.getMessage()); } - @ExceptionHandler(Exception.class) - public Result exceptionHandler(Exception e, HandlerMethod hm) { + @ExceptionHandler(Throwable.class) + public Result exceptionHandler(Throwable e, HandlerMethod hm) { ApiException ce = hm.getMethodAnnotation(ApiException.class); + log.error("Meet en unknown exception: ", e); if (ce == null) { - log.error(e.getMessage(), e); return Result.errorWithArgs(Status.INTERNAL_SERVER_ERROR_ARGS, e.getMessage()); } Status st = ce.value(); - log.error(st.getMsg(), e); return Result.error(st); } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/permission/ResourcePermissionCheckService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/permission/ResourcePermissionCheckService.java index 20df36fa033d7..f3c45daaeae04 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/permission/ResourcePermissionCheckService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/permission/ResourcePermissionCheckService.java @@ -16,7 +16,6 @@ */ package org.apache.dolphinscheduler.api.permission; -import java.util.List; import java.util.Set; import org.slf4j.Logger; @@ -59,11 +58,4 @@ boolean operationPermissionCheck(Object authorizationType, Integer userId, */ boolean functionDisabled(); - /** - * associated with the current user after the resource is created - * @param authorizationType - * @param ids - * @param logger - */ - void postHandle(Object authorizationType, Integer userId, List ids, Logger logger); } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/permission/ResourcePermissionCheckServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/permission/ResourcePermissionCheckServiceImpl.java index 6ec43871cfda4..b35a89cfdf4d5 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/permission/ResourcePermissionCheckServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/permission/ResourcePermissionCheckServiceImpl.java @@ -141,11 +141,6 @@ public boolean functionDisabled() { return false; } - @Override - public void postHandle(Object authorizationType, Integer userId, List ids, Logger logger) { - logger.debug("no post handle"); - } - @Override public Set userOwnedResourceIdsAcquisition(Object authorizationType, Integer userId, Logger logger) { User user = processService.getUserById(userId); diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AccessTokenService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AccessTokenService.java index 2dbfc4dce51de..9322163620cea 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AccessTokenService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AccessTokenService.java @@ -17,10 +17,11 @@ package org.apache.dolphinscheduler.api.service; -import org.apache.dolphinscheduler.api.utils.Result; +import org.apache.dolphinscheduler.api.utils.PageInfo; +import org.apache.dolphinscheduler.dao.entity.AccessToken; import org.apache.dolphinscheduler.dao.entity.User; -import java.util.Map; +import java.util.List; /** * access token service @@ -36,7 +37,7 @@ public interface AccessTokenService { * @param pageSize page size * @return token list for page number and page size */ - Result queryAccessTokenList(User loginUser, String searchVal, Integer pageNo, Integer pageSize); + PageInfo queryAccessTokenList(User loginUser, String searchVal, Integer pageNo, Integer pageSize); /** * query access token for specified user @@ -45,7 +46,7 @@ public interface AccessTokenService { * @param userId user id * @return token list for specified user */ - Map queryAccessTokenByUser(User loginUser, Integer userId); + List queryAccessTokenByUser(User loginUser, Integer userId); /** * create token @@ -55,7 +56,7 @@ public interface AccessTokenService { * @param token token string (if it is absent, it will be automatically generated) * @return create result code */ - Result createToken(User loginUser, int userId, String expireTime, String token); + AccessToken createToken(User loginUser, int userId, String expireTime, String token); /** * generate token @@ -64,7 +65,7 @@ public interface AccessTokenService { * @param expireTime token expire time * @return token string */ - Map generateToken(User loginUser, int userId, String expireTime); + String generateToken(User loginUser, int userId, String expireTime); /** * delete access token @@ -73,7 +74,7 @@ public interface AccessTokenService { * @param id token id * @return delete result code */ - Map delAccessTokenById(User loginUser, int id); + void deleteAccessTokenById(User loginUser, int id); /** * update token by id @@ -84,5 +85,5 @@ public interface AccessTokenService { * @param token token string (if it is absent, it will be automatically generated) * @return updated access token entity */ - Map updateToken(User loginUser, int id, int userId, String expireTime, String token); + AccessToken updateToken(User loginUser, int id, int userId, String expireTime, String token); } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AlertGroupService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AlertGroupService.java index 096d5c3b7b6c8..c56b6e3e85da4 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AlertGroupService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AlertGroupService.java @@ -17,10 +17,11 @@ package org.apache.dolphinscheduler.api.service; -import org.apache.dolphinscheduler.api.utils.Result; +import org.apache.dolphinscheduler.api.utils.PageInfo; +import org.apache.dolphinscheduler.dao.entity.AlertGroup; import org.apache.dolphinscheduler.dao.entity.User; -import java.util.Map; +import java.util.List; /** * alert group service @@ -33,15 +34,15 @@ public interface AlertGroupService { * @param loginUser * @return alert group list */ - Map queryAlertgroup(User loginUser); + List queryAllAlertGroup(User loginUser); /** * query normal alert group list * * @param loginUser - * @return alert group list + * @return alert group list which is is not 2 */ - Map queryNormalAlertgroup(User loginUser); + List queryNormalAlertGroups(User loginUser); /** * query alert group by id @@ -50,7 +51,7 @@ public interface AlertGroupService { * @param id alert group id * @return one alert group */ - Map queryAlertGroupById(User loginUser, Integer id); + AlertGroup queryAlertGroupById(User loginUser, Integer id); /** * paging query alarm group list @@ -61,7 +62,7 @@ public interface AlertGroupService { * @param pageSize page size * @return alert group list page */ - Result listPaging(User loginUser, String searchVal, Integer pageNo, Integer pageSize); + PageInfo listPaging(User loginUser, String searchVal, Integer pageNo, Integer pageSize); /** * create alert group @@ -70,9 +71,9 @@ public interface AlertGroupService { * @param groupName group name * @param desc description * @param alertInstanceIds alertInstanceIds - * @return create result code + * @return alertGroup */ - Map createAlertgroup(User loginUser, String groupName, String desc, String alertInstanceIds); + AlertGroup createAlertGroup(User loginUser, String groupName, String desc, String alertInstanceIds); /** * updateProcessInstance alert group @@ -84,8 +85,7 @@ public interface AlertGroupService { * @param alertInstanceIds alertInstanceIds * @return update result code */ - Map updateAlertgroup(User loginUser, int id, String groupName, String desc, - String alertInstanceIds); + AlertGroup updateAlertGroupById(User loginUser, int id, String groupName, String desc, String alertInstanceIds); /** * delete alert group by id @@ -94,7 +94,7 @@ Map updateAlertgroup(User loginUser, int id, String groupName, S * @param id alert group id * @return delete result code */ - Map delAlertgroupById(User loginUser, int id); + void deleteAlertGroupById(User loginUser, int id); /** * verify group name exists diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AlertPluginInstanceService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AlertPluginInstanceService.java index 156531f160aba..1dc6dcda92270 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AlertPluginInstanceService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AlertPluginInstanceService.java @@ -17,12 +17,14 @@ package org.apache.dolphinscheduler.api.service; -import org.apache.dolphinscheduler.api.utils.Result; +import org.apache.dolphinscheduler.api.utils.PageInfo; +import org.apache.dolphinscheduler.api.vo.AlertPluginInstanceVO; import org.apache.dolphinscheduler.common.enums.AlertPluginInstanceType; import org.apache.dolphinscheduler.common.enums.WarningType; +import org.apache.dolphinscheduler.dao.entity.AlertPluginInstance; import org.apache.dolphinscheduler.dao.entity.User; -import java.util.Map; +import java.util.List; /** * alert plugin instance service @@ -38,8 +40,11 @@ public interface AlertPluginInstanceService { * @param pluginInstanceParams plugin instance params * @return result */ - Map create(User loginUser, int pluginDefineId, String instanceName, - AlertPluginInstanceType instanceType, WarningType warningType, + AlertPluginInstance create(User loginUser, + int pluginDefineId, + String instanceName, + AlertPluginInstanceType instanceType, + WarningType warningType, String pluginInstanceParams); /** @@ -50,17 +55,20 @@ Map create(User loginUser, int pluginDefineId, String instanceNa * @param pluginInstanceParams plugin instance params * @return result */ - Map update(User loginUser, int alertPluginInstanceId, String instanceName, - WarningType warningType, String pluginInstanceParams); + AlertPluginInstance updateById(User loginUser, + int alertPluginInstanceId, + String instanceName, + WarningType warningType, + String pluginInstanceParams); /** * delete alert plugin instance * * @param loginUser login user - * @param id id + * @param alertPluginInstanceId id * @return result */ - Map delete(User loginUser, int id); + void deleteById(User loginUser, int alertPluginInstanceId); /** * get alert plugin instance @@ -69,14 +77,14 @@ Map update(User loginUser, int alertPluginInstanceId, String ins * @param id get id * @return alert plugin */ - Map get(User loginUser, int id); + AlertPluginInstance getById(User loginUser, int id); /** * queryAll * * @return alert plugins */ - Map queryAll(); + List queryAll(); /** * checkExistPluginInstanceName @@ -93,5 +101,5 @@ Map update(User loginUser, int alertPluginInstanceId, String ins * @param pageSize page size * @return plugins */ - Result listPaging(User loginUser, String searchVal, int pageNo, int pageSize); + PageInfo listPaging(User loginUser, String searchVal, int pageNo, int pageSize); } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AuditService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AuditService.java index 3c1c61d07cc17..7b998dccce2b0 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AuditService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AuditService.java @@ -17,7 +17,8 @@ package org.apache.dolphinscheduler.api.service; -import org.apache.dolphinscheduler.api.utils.Result; +import org.apache.dolphinscheduler.api.dto.AuditDto; +import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.common.enums.AuditOperationType; import org.apache.dolphinscheduler.common.enums.AuditResourceType; import org.apache.dolphinscheduler.dao.entity.User; @@ -50,8 +51,8 @@ public interface AuditService { * @param pageSize page size * @return audit log string */ - Result queryLogListPaging(User loginUser, AuditResourceType resourceType, - AuditOperationType operationType, String startTime, - String endTime, String userName, - Integer pageNo, Integer pageSize); + PageInfo queryLogListPaging(User loginUser, AuditResourceType resourceType, + AuditOperationType operationType, String startTime, + String endTime, String userName, + Integer pageNo, Integer pageSize); } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/BaseService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/BaseService.java index 3d5d3197dc7e5..234e163c99024 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/BaseService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/BaseService.java @@ -24,11 +24,8 @@ import org.apache.dolphinscheduler.dao.entity.User; import java.util.Date; -import java.util.List; import java.util.Map; -import org.slf4j.Logger; - /** * base service */ @@ -49,16 +46,7 @@ public interface BaseService { * @param result result code * @return true if not administrator, otherwise false */ - boolean isNotAdmin(User loginUser, Map result); - - /** - * permissionPostHandle - * @param authorizationType - * @param userId - * @param ids - * @param logger - */ - void permissionPostHandle(AuthorizationType authorizationType, Integer userId, List ids, Logger logger); + boolean isNotAdmin(User loginUser); /** * put message to map diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ClusterService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ClusterService.java index 0b542579db324..01d5e1bec13a0 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ClusterService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ClusterService.java @@ -17,10 +17,12 @@ package org.apache.dolphinscheduler.api.service; -import org.apache.dolphinscheduler.api.utils.Result; +import org.apache.dolphinscheduler.api.dto.ClusterDto; +import org.apache.dolphinscheduler.api.utils.PageInfo; +import org.apache.dolphinscheduler.dao.entity.Cluster; import org.apache.dolphinscheduler.dao.entity.User; -import java.util.Map; +import java.util.List; /** * cluster service @@ -34,22 +36,23 @@ public interface ClusterService { * @param name cluster name * @param config cluster config * @param desc cluster desc + * @return cluster code */ - Map createCluster(User loginUser, String name, String config, String desc); + Long createCluster(User loginUser, String name, String config, String desc); /** * query cluster * * @param name cluster name */ - Map queryClusterByName(String name); + ClusterDto queryClusterByName(String name); /** * query cluster * * @param code cluster code */ - Map queryClusterByCode(Long code); + ClusterDto queryClusterByCode(Long code); /** * delete cluster @@ -57,7 +60,7 @@ public interface ClusterService { * @param loginUser login user * @param code cluster code */ - Map deleteClusterByCode(User loginUser, Long code); + void deleteClusterByCode(User loginUser, Long code); /** * update cluster @@ -68,7 +71,7 @@ public interface ClusterService { * @param config cluster config * @param desc cluster desc */ - Map updateClusterByCode(User loginUser, Long code, String name, String config, String desc); + Cluster updateClusterByCode(User loginUser, Long code, String name, String config, String desc); /** * query cluster paging @@ -78,14 +81,14 @@ public interface ClusterService { * @param pageSize page size * @return cluster list page */ - Result queryClusterListPaging(Integer pageNo, Integer pageSize, String searchVal); + PageInfo queryClusterListPaging(Integer pageNo, Integer pageSize, String searchVal); /** * query all cluster * * @return all cluster list */ - Map queryAllClusterList(); + List queryAllClusterList(); /** * verify cluster name @@ -93,6 +96,6 @@ public interface ClusterService { * @param clusterName cluster name * @return true if the cluster name not exists, otherwise return false */ - Map verifyCluster(String clusterName); + void verifyCluster(String clusterName); } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataAnalysisService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataAnalysisService.java index 2417f796ab006..48131412c2109 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataAnalysisService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataAnalysisService.java @@ -17,6 +17,9 @@ package org.apache.dolphinscheduler.api.service; +import org.apache.dolphinscheduler.api.dto.CommandStateCount; +import org.apache.dolphinscheduler.api.dto.DefineUserDto; +import org.apache.dolphinscheduler.api.dto.TaskCountDto; import org.apache.dolphinscheduler.api.dto.project.StatisticsStateRequest; import org.apache.dolphinscheduler.dao.entity.ExecuteStatusCount; import org.apache.dolphinscheduler.dao.entity.User; @@ -41,7 +44,7 @@ public interface DataAnalysisService { * @param endDate end date * @return task state count data */ - Map countTaskStateByProject(User loginUser, long projectCode, String startDate, String endDate); + TaskCountDto countTaskStateByProject(User loginUser, long projectCode, String startDate, String endDate); /** * statistical process instance status data @@ -52,8 +55,8 @@ public interface DataAnalysisService { * @param endDate end date * @return process instance state count data */ - Map countProcessInstanceStateByProject(User loginUser, long projectCode, String startDate, - String endDate); + TaskCountDto countProcessInstanceStateByProject(User loginUser, long projectCode, String startDate, + String endDate); /** * statistics the process definition quantities of a certain person @@ -64,7 +67,7 @@ Map countProcessInstanceStateByProject(User loginUser, long proj * @param projectCode project code * @return workflow count data */ - Map countDefinitionByUser(User loginUser, long projectCode); + DefineUserDto countDefinitionByUser(User loginUser, long projectCode); /** * statistics the workflow quantities of certain user *

@@ -76,7 +79,7 @@ Map countProcessInstanceStateByProject(User loginUser, long proj * @param releaseState releaseState * @return workflow count data */ - Map countDefinitionByUserV2(User loginUser, Long projectCode, Integer userId, Integer releaseState); + DefineUserDto countDefinitionByUserV2(User loginUser, Long projectCode, Integer userId, Integer releaseState); /** * statistical command status data @@ -84,7 +87,7 @@ Map countProcessInstanceStateByProject(User loginUser, long proj * @param loginUser login user * @return command state count data */ - Map countCommandState(User loginUser); + List countCommandState(User loginUser); /** * count queue state @@ -92,7 +95,7 @@ Map countProcessInstanceStateByProject(User loginUser, long proj * @param loginUser login user * @return queue state count data */ - Map countQueueState(User loginUser); + Map countQueueState(User loginUser); /** * Statistics task instance group by given project codes list @@ -121,8 +124,8 @@ List countTaskInstanceAllStatesByProjectCodes(@Param("startT * @param statisticsStateRequest statisticsStateRequest * @return workflow states count */ - Map countWorkflowStates(User loginUser, - StatisticsStateRequest statisticsStateRequest); + TaskCountDto countWorkflowStates(User loginUser, + StatisticsStateRequest statisticsStateRequest); /** * query one workflow states count @@ -130,7 +133,7 @@ Map countWorkflowStates(User loginUser, * @param workflowCode workflowCode * @return workflow states count */ - Map countOneWorkflowStates(User loginUser, Long workflowCode); + TaskCountDto countOneWorkflowStates(User loginUser, Long workflowCode); /** * query all task states count @@ -138,7 +141,7 @@ Map countWorkflowStates(User loginUser, * @param statisticsStateRequest statisticsStateRequest * @return tasks states count */ - Map countTaskStates(User loginUser, StatisticsStateRequest statisticsStateRequest); + TaskCountDto countTaskStates(User loginUser, StatisticsStateRequest statisticsStateRequest); /** * query one task states count @@ -146,7 +149,7 @@ Map countWorkflowStates(User loginUser, * @param taskCode taskCode * @return tasks states count */ - Map countOneTaskStates(User loginUser, Long taskCode); + TaskCountDto countOneTaskStates(User loginUser, Long taskCode); Long getProjectCodeByName(String projectName); } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java index 42ab666aa1fbb..a7382ee51a691 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DataSourceService.java @@ -18,7 +18,6 @@ package org.apache.dolphinscheduler.api.service; import org.apache.dolphinscheduler.api.utils.PageInfo; -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.plugin.datasource.api.datasource.BaseDataSourceParamDTO; @@ -40,17 +39,16 @@ public interface DataSourceService { * @param datasourceParam datasource parameter * @return create result code */ - Result createDataSource(User loginUser, BaseDataSourceParamDTO datasourceParam); + DataSource createDataSource(User loginUser, BaseDataSourceParamDTO datasourceParam); /** * updateProcessInstance datasource * * @param loginUser login user - * @param id data source id * @param dataSourceParam data source params * @return update result code */ - Result updateDataSource(int id, User loginUser, BaseDataSourceParamDTO dataSourceParam); + DataSource updateDataSource(User loginUser, BaseDataSourceParamDTO dataSourceParam); /** * updateProcessInstance datasource @@ -86,7 +84,7 @@ public interface DataSourceService { * @param name datasource name * @return true if data datasource not exists, otherwise return false */ - Result verifyDataSourceName(String name); + void verifyDataSourceName(String name); /** * check connection @@ -95,7 +93,7 @@ public interface DataSourceService { * @param parameter data source parameters * @return true if connect successfully, otherwise false */ - Result checkConnection(DbType type, ConnectionParam parameter); + void checkConnection(DbType type, ConnectionParam parameter); /** * test connection @@ -103,7 +101,7 @@ public interface DataSourceService { * @param id datasource id * @return connect result code */ - Result connectionTest(int id); + void connectionTest(int id); /** * delete datasource @@ -112,7 +110,7 @@ public interface DataSourceService { * @param datasourceId data source id * @return delete result code */ - Result delete(User loginUser, int datasourceId); + void delete(User loginUser, int datasourceId); /** * unauthorized datasource diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DqExecuteResultService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DqExecuteResultService.java index d8e3468bbbb49..02842ae74f321 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DqExecuteResultService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DqExecuteResultService.java @@ -17,7 +17,8 @@ package org.apache.dolphinscheduler.api.service; -import org.apache.dolphinscheduler.api.utils.Result; +import org.apache.dolphinscheduler.api.utils.PageInfo; +import org.apache.dolphinscheduler.dao.entity.DqExecuteResult; import org.apache.dolphinscheduler.dao.entity.User; /** @@ -25,11 +26,11 @@ */ public interface DqExecuteResultService { - Result queryResultListPaging(User loginUser, - String searchVal, - Integer state, - Integer ruleType, - String startTime, - String endTime, - Integer pageNo, Integer pageSize); + PageInfo queryResultListPaging(User loginUser, + String searchVal, + Integer state, + Integer ruleType, + String startTime, + String endTime, + Integer pageNo, Integer pageSize); } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DqRuleService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DqRuleService.java index df6d68d71e178..8b331f4395cc2 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DqRuleService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/DqRuleService.java @@ -17,26 +17,28 @@ package org.apache.dolphinscheduler.api.service; -import org.apache.dolphinscheduler.api.utils.Result; +import org.apache.dolphinscheduler.api.utils.PageInfo; +import org.apache.dolphinscheduler.dao.entity.DqRule; import org.apache.dolphinscheduler.dao.entity.User; +import org.apache.dolphinscheduler.spi.params.base.ParamsOptions; -import java.util.Map; +import java.util.List; /** * DqsRuleService */ public interface DqRuleService { - Map getRuleFormCreateJsonById(int id); + String getRuleFormCreateJsonById(int id); - Map queryAllRuleList(); + List queryAllRuleList(); - Result queryRuleListPaging(User loginUser, - String searchVal, - Integer ruleType, - String startTime, - String endTime, - Integer pageNo, Integer pageSize); + PageInfo queryRuleListPaging(User loginUser, + String searchVal, + Integer ruleType, + String startTime, + String endTime, + Integer pageNo, Integer pageSize); - Map getDatasourceOptionsById(int datasourceId); + List getDatasourceOptionsById(int datasourceId); } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/EnvironmentService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/EnvironmentService.java index ebedea43fea0b..c2a2511f21350 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/EnvironmentService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/EnvironmentService.java @@ -18,6 +18,7 @@ package org.apache.dolphinscheduler.api.service; import org.apache.dolphinscheduler.api.utils.Result; +import org.apache.dolphinscheduler.dao.entity.Environment; import org.apache.dolphinscheduler.dao.entity.User; import java.util.Map; @@ -36,7 +37,7 @@ public interface EnvironmentService { * @param desc environment desc * @param workerGroups worker groups */ - Map createEnvironment(User loginUser, String name, String config, String desc, String workerGroups); + Long createEnvironment(User loginUser, String name, String config, String desc, String workerGroups); /** * query environment @@ -70,8 +71,8 @@ public interface EnvironmentService { * @param desc environment desc * @param workerGroups worker groups */ - Map updateEnvironmentByCode(User loginUser, Long code, String name, String config, String desc, - String workerGroups); + Environment updateEnvironmentByCode(User loginUser, Long code, String name, String config, String desc, + String workerGroups); /** * query environment paging diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ExecutorService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ExecutorService.java index 408fdee7f364c..3166d3e71842b 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ExecutorService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ExecutorService.java @@ -164,12 +164,12 @@ WorkflowExecuteResponse executeTask(User loginUser, long projectCode, Integer pr * @param startParams the global param values which pass to new process instance * @return execute process instance code */ - Map execStreamTaskInstance(User loginUser, long projectCode, - long taskDefinitionCode, int taskDefinitionVersion, - int warningGroupId, - String workerGroup, - String tenantCode, - Long environmentCode, - Map startParams, - int dryRun); + void execStreamTaskInstance(User loginUser, long projectCode, + long taskDefinitionCode, int taskDefinitionVersion, + int warningGroupId, + String workerGroup, + String tenantCode, + Long environmentCode, + Map startParams, + int dryRun); } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/LoggerService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/LoggerService.java index db1cb7c465b34..fe94f7ec43bc6 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/LoggerService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/LoggerService.java @@ -21,8 +21,6 @@ import org.apache.dolphinscheduler.dao.entity.ResponseTaskLog; import org.apache.dolphinscheduler.dao.entity.User; -import java.util.Map; - /** * logger service */ @@ -58,7 +56,7 @@ public interface LoggerService { * @param limit limit * @return log string data */ - Map queryLog(User loginUser, long projectCode, int taskInstId, int skipLineNum, int limit); + String queryLog(User loginUser, long projectCode, int taskInstId, int skipLineNum, int limit); /** * get log bytes diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/MonitorService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/MonitorService.java index e881b2e346dc1..c4a8d94a1f8dd 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/MonitorService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/MonitorService.java @@ -18,10 +18,11 @@ package org.apache.dolphinscheduler.api.service; import org.apache.dolphinscheduler.common.model.Server; +import org.apache.dolphinscheduler.common.model.WorkerServerModel; import org.apache.dolphinscheduler.dao.entity.User; +import org.apache.dolphinscheduler.dao.plugin.api.monitor.DatabaseMetrics; import java.util.List; -import java.util.Map; /** * monitor service @@ -34,7 +35,7 @@ public interface MonitorService { * @param loginUser login user * @return data base state */ - Map queryDatabaseState(User loginUser); + List queryDatabaseState(User loginUser); /** * query master list @@ -42,7 +43,7 @@ public interface MonitorService { * @param loginUser login user * @return master information list */ - Map queryMaster(User loginUser); + List queryMaster(User loginUser); /** * query worker list @@ -50,7 +51,7 @@ public interface MonitorService { * @param loginUser login user * @return worker information list */ - Map queryWorker(User loginUser); + List queryWorker(User loginUser); List getServerListFromRegistry(boolean isMaster); } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ProjectService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ProjectService.java index d1eb4b2ada380..4736633fa15b4 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ProjectService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ProjectService.java @@ -65,6 +65,7 @@ public interface ProjectService { * @param perm String * @return true if the login user have permission to see the project */ + @Deprecated Map checkProjectAndAuth(User loginUser, Project project, long projectCode, String perm); void checkProjectAndAuthThrowException(User loginUser, Project project, String permission) throws ServiceException; @@ -82,12 +83,19 @@ public interface ProjectService { * @param permission String * @return true if the login user have permission to the project */ + @Deprecated boolean hasProjectAndPerm(User loginUser, Project project, Result result, String permission); + @Deprecated boolean hasProjectAndWritePerm(User loginUser, Project project, Result result); + @Deprecated boolean hasProjectAndWritePerm(User loginUser, Project project, Map result); + void checkHasProjectWritePermissionThrowException(User loginUser, long projectCode); + + void checkHasProjectWritePermissionThrowException(User loginUser, Project project); + /** * admin can view all projects * diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/QueueService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/QueueService.java index 69ed5184456a7..0cafe70d5e3c6 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/QueueService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/QueueService.java @@ -17,11 +17,11 @@ package org.apache.dolphinscheduler.api.service; -import org.apache.dolphinscheduler.api.utils.Result; +import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.dao.entity.Queue; import org.apache.dolphinscheduler.dao.entity.User; -import java.util.Map; +import java.util.List; /** * queue service @@ -34,58 +34,57 @@ public interface QueueService { * @param loginUser login user * @return queue list */ - Result queryList(User loginUser); + List queryList(User loginUser); /** * query queue list paging * * @param loginUser login user - * @param pageNo page number + * @param pageNo page number * @param searchVal search value - * @param pageSize page size + * @param pageSize page size * @return queue list */ - Result queryList(User loginUser, String searchVal, Integer pageNo, Integer pageSize); + PageInfo queryList(User loginUser, String searchVal, Integer pageNo, Integer pageSize); /** * create queue * * @param loginUser login user - * @param queue queue + * @param queue queue * @param queueName queue name * @return create result */ - Result createQueue(User loginUser, String queue, String queueName); + Queue createQueue(User loginUser, String queue, String queueName); /** * update queue * * @param loginUser login user - * @param queue queue - * @param id queue id + * @param queue queue + * @param id queue id * @param queueName queue name * @return update result code */ - Result updateQueue(User loginUser, int id, String queue, String queueName); + Queue updateQueue(User loginUser, int id, String queue, String queueName); /** * delete queue * * @param loginUser login user - * @param id queue id + * @param id queue id * @return delete result code * @throws Exception exception */ - Map deleteQueueById(User loginUser, int id) throws Exception; + void deleteQueueById(User loginUser, int id) throws Exception; /** * verify queue and queueName * * @param queue queue * @param queueName queue name - * @return true if the queue name not exists, otherwise return false */ - Result verifyQueue(String queue, String queueName); + void verifyQueue(String queue, String queueName); /** * Make sure queue with given name exists, and create the queue if not exists diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/TenantService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/TenantService.java index fc5abf4b6b19b..6841db986d964 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/TenantService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/TenantService.java @@ -17,10 +17,11 @@ package org.apache.dolphinscheduler.api.service; -import org.apache.dolphinscheduler.api.utils.Result; +import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.dao.entity.Tenant; import org.apache.dolphinscheduler.dao.entity.User; +import java.util.List; import java.util.Map; /** @@ -31,52 +32,54 @@ public interface TenantService { /** * create tenant * - * @param loginUser login user + * @param loginUser login user * @param tenantCode tenant code - * @param queueId queue id - * @param desc description + * @param queueId queue id + * @param desc description * @return create result code * @throws Exception exception */ - Map createTenant(User loginUser, - String tenantCode, - int queueId, - String desc) throws Exception; + Tenant createTenant(User loginUser, + String tenantCode, + int queueId, + String desc) throws Exception; /** * query tenant list paging * * @param loginUser login user * @param searchVal search value - * @param pageNo page number - * @param pageSize page size + * @param pageNo page number + * @param pageSize page size * @return tenant list page */ - Result queryTenantList(User loginUser, String searchVal, Integer pageNo, Integer pageSize); + PageInfo queryTenantList(User loginUser, String searchVal, Integer pageNo, Integer pageSize); /** * updateProcessInstance tenant * - * @param loginUser login user - * @param id tennat id + * @param loginUser login user + * @param id tennat id * @param tenantCode tennat code - * @param queueId queue id - * @param desc description + * @param queueId queue id + * @param desc description * @return update result code * @throws Exception exception */ - Map updateTenant(User loginUser, int id, String tenantCode, int queueId, - String desc) throws Exception; + void updateTenant(User loginUser, + int id, String tenantCode, + int queueId, + String desc) throws Exception; /** * delete tenant * * @param loginUser login user - * @param id tenant id + * @param id tenant id * @return delete result code * @throws Exception exception */ - Map deleteTenantById(User loginUser, int id) throws Exception; + void deleteTenantById(User loginUser, int id) throws Exception; /** * query tenant list @@ -84,7 +87,7 @@ Map updateTenant(User loginUser, int id, String tenantCode, int * @param loginUser login user * @return tenant list */ - Map queryTenantList(User loginUser); + List queryTenantList(User loginUser); /** * verify tenant code @@ -92,7 +95,7 @@ Map updateTenant(User loginUser, int id, String tenantCode, int * @param tenantCode tenant code * @return true if tenant code can user, otherwise return false */ - Result verifyTenantCode(String tenantCode); + void verifyTenantCode(String tenantCode); /** * query tenant by tenant code diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/WorkFlowLineageService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/WorkFlowLineageService.java index 04902a4d0d4cc..5b365553609e8 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/WorkFlowLineageService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/WorkFlowLineageService.java @@ -18,7 +18,9 @@ package org.apache.dolphinscheduler.api.service; import org.apache.dolphinscheduler.dao.entity.TaskMainInfo; +import org.apache.dolphinscheduler.dao.entity.WorkFlowLineage; +import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; @@ -28,7 +30,7 @@ */ public interface WorkFlowLineageService { - Map queryWorkFlowLineageByName(long projectCode, String workFlowName); + List queryWorkFlowLineageByName(long projectCode, String workFlowName); Map queryWorkFlowLineageByCode(long projectCode, long workFlowCode); diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AccessTokenServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AccessTokenServiceImpl.java index 2a264d69481e7..ebb151c604a9b 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AccessTokenServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AccessTokenServiceImpl.java @@ -22,10 +22,9 @@ import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.ACCESS_TOKEN_UPDATE; import org.apache.dolphinscheduler.api.enums.Status; +import org.apache.dolphinscheduler.api.exceptions.ServiceException; import org.apache.dolphinscheduler.api.service.AccessTokenService; import org.apache.dolphinscheduler.api.utils.PageInfo; -import org.apache.dolphinscheduler.api.utils.Result; -import org.apache.dolphinscheduler.common.constants.Constants; import org.apache.dolphinscheduler.common.enums.AuthorizationType; import org.apache.dolphinscheduler.common.enums.UserType; import org.apache.dolphinscheduler.common.utils.DateUtils; @@ -37,9 +36,7 @@ import org.apache.commons.lang3.StringUtils; import java.util.Date; -import java.util.HashMap; import java.util.List; -import java.util.Map; import lombok.extern.slf4j.Slf4j; @@ -64,13 +61,13 @@ public class AccessTokenServiceImpl extends BaseServiceImpl implements AccessTok * * @param loginUser login user * @param searchVal search value - * @param pageNo page number - * @param pageSize page size + * @param pageNo page number + * @param pageSize page size * @return token list for page number and page size */ @Override - public Result queryAccessTokenList(User loginUser, String searchVal, Integer pageNo, Integer pageSize) { - Result result = new Result(); + public PageInfo queryAccessTokenList(User loginUser, String searchVal, Integer pageNo, + Integer pageSize) { PageInfo pageInfo = new PageInfo<>(pageNo, pageSize); Page page = new Page<>(pageNo, pageSize); int userId = loginUser.getId(); @@ -80,33 +77,26 @@ public Result queryAccessTokenList(User loginUser, String searchVal, Integer pag IPage accessTokenList = accessTokenMapper.selectAccessTokenPage(page, searchVal, userId); pageInfo.setTotal((int) accessTokenList.getTotal()); pageInfo.setTotalList(accessTokenList.getRecords()); - result.setData(pageInfo); - putMsg(result, Status.SUCCESS); - return result; + return pageInfo; } /** * query access token for specified user * * @param loginUser login user - * @param userId user id + * @param userId user id * @return token list for specified user */ @Override - public Map queryAccessTokenByUser(User loginUser, Integer userId) { - Map result = new HashMap<>(); - result.put(Constants.STATUS, false); + public List queryAccessTokenByUser(User loginUser, Integer userId) { // no permission if (loginUser.getUserType().equals(UserType.GENERAL_USER) && loginUser.getId() != userId) { - putMsg(result, Status.USER_NO_OPERATION_PERM); - return result; + throw new ServiceException(Status.USER_NO_OPERATION_PERM); } userId = loginUser.getUserType().equals(UserType.ADMIN_USER) ? 0 : userId; // query access token for specified user List accessTokenList = this.accessTokenMapper.queryAccessTokenByUser(userId); - result.put(Constants.DATA_LIST, accessTokenList); - this.putMsg(result, Status.SUCCESS); - return result; + return accessTokenList; } /** @@ -120,22 +110,18 @@ public Map queryAccessTokenByUser(User loginUser, Integer userId */ @SuppressWarnings("checkstyle:WhitespaceAround") @Override - public Result createToken(User loginUser, int userId, String expireTime, String token) { - Result result = new Result(); + public AccessToken createToken(User loginUser, int userId, String expireTime, String token) { // 1. check permission if (!(canOperatorPermissions(loginUser, null, AuthorizationType.ACCESS_TOKEN, ACCESS_TOKEN_CREATE) || loginUser.getId() == userId)) { - putMsg(result, Status.USER_NO_OPERATION_PERM); - return result; + throw new ServiceException(Status.USER_NO_OPERATION_PERM); } // 2. check if user is existed if (userId <= 0) { - String errorMsg = "User id should not less than or equals to 0."; - log.error(errorMsg); - putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, errorMsg); - return result; + throw new ServiceException(Status.REQUEST_PARAMS_NOT_VALID_ERROR, + "User id: " + userId + " should not less than or equals to 0."); } // 3. generate access token if absent @@ -154,94 +140,75 @@ public Result createToken(User loginUser, int userId, String expireTime, String int insert = accessTokenMapper.insert(accessToken); if (insert > 0) { - result.setData(accessToken); - putMsg(result, Status.SUCCESS); - } else { - putMsg(result, Status.CREATE_ACCESS_TOKEN_ERROR); + return accessToken; } - - return result; + throw new ServiceException(Status.CREATE_ACCESS_TOKEN_ERROR); } /** * generate token * * @param loginUser - * @param userId token for user + * @param userId token for user * @param expireTime token expire time * @return token string */ @Override - public Map generateToken(User loginUser, int userId, String expireTime) { - Map result = new HashMap<>(); - String token = EncryptionUtils.getMd5(userId + expireTime + System.currentTimeMillis()); - result.put(Constants.DATA_LIST, token); - putMsg(result, Status.SUCCESS); - return result; + public String generateToken(User loginUser, int userId, String expireTime) { + return EncryptionUtils.getMd5(userId + expireTime + System.currentTimeMillis()); } /** * delete access token * * @param loginUser login user - * @param id token id + * @param id token id * @return delete result code */ @Override - public Map delAccessTokenById(User loginUser, int id) { - Map result = new HashMap<>(); + public void deleteAccessTokenById(User loginUser, int id) { if (!canOperatorPermissions(loginUser, null, AuthorizationType.ACCESS_TOKEN, ACCESS_TOKEN_DELETE)) { - putMsg(result, Status.USER_NO_OPERATION_PERM); - return result; + throw new ServiceException(Status.USER_NO_OPERATION_PERM); } AccessToken accessToken = accessTokenMapper.selectById(id); if (accessToken == null) { - log.error("Access token does not exist, accessTokenId:{}.", id); - putMsg(result, Status.ACCESS_TOKEN_NOT_EXIST); - return result; + throw new ServiceException(Status.ACCESS_TOKEN_NOT_EXIST, id); } // admin can operate all, non-admin can operate their own if (accessToken.getUserId() != loginUser.getId() && !loginUser.getUserType().equals(UserType.ADMIN_USER)) { - putMsg(result, Status.USER_NO_OPERATION_PERM); - return result; + throw new ServiceException(Status.USER_NO_OPERATION_PERM); } accessTokenMapper.deleteById(id); - putMsg(result, Status.SUCCESS); - return result; } /** * update token by id * - * @param id token id - * @param userId token for user + * @param id token id + * @param userId token for user * @param expireTime token expire time - * @param token token string (if it is absent, it will be automatically generated) + * @param token token string (if it is absent, it will be automatically generated) * @return updated access token entity */ @Override - public Map updateToken(User loginUser, int id, int userId, String expireTime, String token) { - Map result = new HashMap<>(); + public AccessToken updateToken(User loginUser, int id, int userId, String expireTime, String token) { // 1. check permission if (!canOperatorPermissions(loginUser, null, AuthorizationType.ACCESS_TOKEN, ACCESS_TOKEN_UPDATE)) { - putMsg(result, Status.USER_NO_OPERATION_PERM); - return result; + throw new ServiceException(Status.USER_NO_OPERATION_PERM); } // 2. check if token is existed AccessToken accessToken = accessTokenMapper.selectById(id); if (accessToken == null) { log.error("Access token does not exist, accessTokenId:{}.", id); - putMsg(result, Status.ACCESS_TOKEN_NOT_EXIST); - return result; + throw new ServiceException(Status.ACCESS_TOKEN_NOT_EXIST, id); } // admin can operate all, non-admin can operate their own if (accessToken.getUserId() != loginUser.getId() && !loginUser.getUserType().equals(UserType.ADMIN_USER)) { - putMsg(result, Status.USER_NO_OPERATION_PERM); - return result; + throw new ServiceException(Status.USER_NO_OPERATION_PERM); } // 3. generate access token if absent @@ -255,10 +222,10 @@ public Map updateToken(User loginUser, int id, int userId, Strin accessToken.setToken(token); accessToken.setUpdateTime(new Date()); - accessTokenMapper.updateById(accessToken); - - result.put(Constants.DATA_LIST, accessToken); - putMsg(result, Status.SUCCESS); - return result; + int i = accessTokenMapper.updateById(accessToken); + if (i <= 0) { + throw new ServiceException(Status.ACCESS_TOKEN_NOT_EXIST, id); + } + return accessToken; } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertGroupServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertGroupServiceImpl.java index 5f04caaaac569..a599eb9ce7dd1 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertGroupServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertGroupServiceImpl.java @@ -23,10 +23,9 @@ import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.ALERT_GROUP_VIEW; import org.apache.dolphinscheduler.api.enums.Status; +import org.apache.dolphinscheduler.api.exceptions.ServiceException; import org.apache.dolphinscheduler.api.service.AlertGroupService; import org.apache.dolphinscheduler.api.utils.PageInfo; -import org.apache.dolphinscheduler.api.utils.Result; -import org.apache.dolphinscheduler.common.constants.Constants; import org.apache.dolphinscheduler.common.enums.AuthorizationType; import org.apache.dolphinscheduler.common.enums.UserType; import org.apache.dolphinscheduler.dao.entity.AlertGroup; @@ -71,74 +70,47 @@ public class AlertGroupServiceImpl extends BaseServiceImpl implements AlertGroup * @return alert group list */ @Override - public Map queryAlertgroup(User loginUser) { - HashMap result = new HashMap<>(); - List alertGroups; + public List queryAllAlertGroup(User loginUser) { if (loginUser.getUserType().equals(UserType.ADMIN_USER)) { - alertGroups = alertGroupMapper.queryAllGroupList(); - } else { - Set ids = resourcePermissionCheckService - .userOwnedResourceIdsAcquisition(AuthorizationType.ALERT_GROUP, loginUser.getId(), log); - if (ids.isEmpty()) { - result.put(Constants.DATA_LIST, Collections.emptyList()); - putMsg(result, Status.SUCCESS); - return result; - } - alertGroups = alertGroupMapper.selectBatchIds(ids); + return alertGroupMapper.queryAllGroupList(); + } + Set ids = resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.ALERT_GROUP, + loginUser.getId(), log); + if (ids.isEmpty()) { + return Collections.emptyList(); } - result.put(Constants.DATA_LIST, alertGroups); - putMsg(result, Status.SUCCESS); - return result; + return alertGroupMapper.selectBatchIds(ids); } @Override - public Map queryNormalAlertgroup(User loginUser) { - HashMap result = new HashMap<>(); - List alertGroups; - if (loginUser.getUserType().equals(UserType.ADMIN_USER)) { - alertGroups = alertGroupMapper.queryAllGroupList(); - } else { - Set ids = resourcePermissionCheckService - .userOwnedResourceIdsAcquisition(AuthorizationType.ALERT_GROUP, loginUser.getId(), log); - if (ids.isEmpty()) { - result.put(Constants.DATA_LIST, Collections.emptyList()); - putMsg(result, Status.SUCCESS); - return result; - } - alertGroups = alertGroupMapper.selectBatchIds(ids); - } - alertGroups = alertGroups.stream().filter(alertGroup -> alertGroup.getId() != 2).collect(Collectors.toList()); - result.put(Constants.DATA_LIST, alertGroups); - putMsg(result, Status.SUCCESS); - return result; + public List queryNormalAlertGroups(User loginUser) { + return queryAllAlertGroup(loginUser) + .stream() + // todo: remove the hardcode + .filter(alertGroup -> alertGroup.getId() != 2) + .collect(Collectors.toList()); } /** * query alert group by id * * @param loginUser login user - * @param id alert group id + * @param id alert group id * @return one alert group */ @Override - public Map queryAlertGroupById(User loginUser, Integer id) { - Map result = new HashMap<>(); - result.put(Constants.STATUS, false); + public AlertGroup queryAlertGroupById(User loginUser, Integer id) { // only admin can operate if (!canOperatorPermissions(loginUser, new Object[]{id}, AuthorizationType.ALERT_GROUP, ALERT_GROUP_VIEW)) { - putMsg(result, Status.USER_NO_OPERATION_PERM); - return result; + throw new ServiceException(Status.USER_NO_OPERATION_PERM); } // check if exist AlertGroup alertGroup = alertGroupMapper.selectById(id); if (alertGroup == null) { - putMsg(result, Status.ALERT_GROUP_NOT_EXIST); - return result; + throw new ServiceException(Status.ALERT_GROUP_NOT_EXIST, id); } - result.put("data", alertGroup); - putMsg(result, Status.SUCCESS); - return result; + return alertGroup; } /** @@ -146,35 +118,27 @@ public Map queryAlertGroupById(User loginUser, Integer id) { * * @param loginUser login user * @param searchVal search value - * @param pageNo page number - * @param pageSize page size + * @param pageNo page number + * @param pageSize page size * @return alert group list page */ @Override - public Result listPaging(User loginUser, String searchVal, Integer pageNo, Integer pageSize) { - - Result result = new Result(); - IPage alertGroupPage; - PageInfo pageInfo = new PageInfo<>(pageNo, pageSize); + public PageInfo listPaging(User loginUser, String searchVal, Integer pageNo, Integer pageSize) { Page page = new Page<>(pageNo, pageSize); if (loginUser.getUserType().equals(UserType.ADMIN_USER)) { - alertGroupPage = alertGroupMapper.queryAlertGroupPage(page, searchVal); - } else { - Set ids = resourcePermissionCheckService - .userOwnedResourceIdsAcquisition(AuthorizationType.ALERT_GROUP, loginUser.getId(), log); - if (ids.isEmpty()) { - result.setData(pageInfo); - putMsg(result, Status.SUCCESS); - return result; - } - alertGroupPage = alertGroupMapper.queryAlertGroupPageByIds(page, new ArrayList<>(ids), searchVal); + IPage alertGroupIPage = alertGroupMapper.queryAlertGroupPage(page, searchVal); + return PageInfo.of(alertGroupIPage); } - pageInfo.setTotal((int) alertGroupPage.getTotal()); - pageInfo.setTotalList(alertGroupPage.getRecords()); - result.setData(pageInfo); - putMsg(result, Status.SUCCESS); - return result; + Set ids = resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.ALERT_GROUP, + loginUser.getId(), log); + if (ids.isEmpty()) { + return PageInfo.of(pageNo, pageSize); + } + + IPage alertGroupIPage = + alertGroupMapper.queryAlertGroupPageByIds(page, new ArrayList<>(ids), searchVal); + return PageInfo.of(alertGroupIPage); } /** @@ -188,18 +152,15 @@ public Result listPaging(User loginUser, String searchVal, Integer pageNo, Integ */ @Override @Transactional - public Map createAlertgroup(User loginUser, String groupName, String desc, - String alertInstanceIds) { + public AlertGroup createAlertGroup(User loginUser, String groupName, String desc, String alertInstanceIds) { Map result = new HashMap<>(); // only admin can operate if (!canOperatorPermissions(loginUser, null, AuthorizationType.ALERT_GROUP, ALERT_GROUP_CREATE)) { - putMsg(result, Status.USER_NO_OPERATION_PERM); - return result; + throw new ServiceException(Status.USER_NO_OPERATION_PERM); } if (checkDescriptionLength(desc)) { log.warn("Parameter description is too long."); - putMsg(result, Status.DESCRIPTION_TOO_LONG_ERROR); - return result; + throw new ServiceException(Status.DESCRIPTION_TOO_LONG_ERROR); } AlertGroup alertGroup = new AlertGroup(); Date now = new Date(); @@ -215,21 +176,15 @@ public Map createAlertgroup(User loginUser, String groupName, St try { int insert = alertGroupMapper.insert(alertGroup); if (insert > 0) { - result.put(Constants.DATA_LIST, alertGroup); - putMsg(result, Status.SUCCESS); - permissionPostHandle(AuthorizationType.ALERT_GROUP, loginUser.getId(), - Collections.singletonList(alertGroup.getId()), log); log.info("Create alert group complete, groupName:{}", alertGroup.getGroupName()); - } else { - log.error("Create alert group error, groupName:{}", alertGroup.getGroupName()); - putMsg(result, Status.CREATE_ALERT_GROUP_ERROR); + return alertGroup; } + log.error("Create alert group error, groupName:{}", alertGroup.getGroupName()); + throw new ServiceException(Status.CREATE_ALERT_GROUP_ERROR); } catch (DuplicateKeyException ex) { log.error("Create alert group error, groupName:{}", alertGroup.getGroupName(), ex); - putMsg(result, Status.ALERT_GROUP_EXIST); + throw new ServiceException(Status.ALERT_GROUP_EXIST); } - - return result; } /** @@ -243,31 +198,24 @@ public Map createAlertgroup(User loginUser, String groupName, St * @return update result code */ @Override - public Map updateAlertgroup(User loginUser, int id, String groupName, String desc, - String alertInstanceIds) { - Map result = new HashMap<>(); + public AlertGroup updateAlertGroupById(User loginUser, int id, String groupName, String desc, + String alertInstanceIds) { // don't allow to update global alert group + // todo: remove hardcode if (id == 2) { - putMsg(result, Status.NOT_ALLOW_TO_UPDATE_GLOBAL_ALARM_GROUP); - return result; + throw new ServiceException(Status.NOT_ALLOW_TO_UPDATE_GLOBAL_ALARM_GROUP); } if (!canOperatorPermissions(loginUser, new Object[]{id}, AuthorizationType.ALERT_GROUP, ALERT_GROUP_UPDATE)) { - putMsg(result, Status.USER_NO_OPERATION_PERM); - return result; + throw new ServiceException(Status.USER_NO_OPERATION_PERM); } if (checkDescriptionLength(desc)) { - log.warn("Parameter description is too long."); - putMsg(result, Status.DESCRIPTION_TOO_LONG_ERROR); - return result; + throw new ServiceException(Status.DESCRIPTION_TOO_LONG_ERROR); } AlertGroup alertGroup = alertGroupMapper.selectById(id); if (alertGroup == null) { - log.error("Alert group does not exist, id:{}.", id); - putMsg(result, Status.ALERT_GROUP_NOT_EXIST); - return result; - + throw new ServiceException(Status.ALERT_GROUP_NOT_EXIST); } Date now = new Date(); @@ -282,52 +230,42 @@ public Map updateAlertgroup(User loginUser, int id, String group try { alertGroupMapper.updateById(alertGroup); log.info("Update alert group complete, groupName:{}", alertGroup.getGroupName()); - putMsg(result, Status.SUCCESS); + return alertGroup; } catch (DuplicateKeyException ex) { log.error("Update alert group error, groupName:{}", alertGroup.getGroupName(), ex); - putMsg(result, Status.ALERT_GROUP_EXIST); + throw new ServiceException(Status.ALERT_GROUP_EXIST); } - return result; } /** * delete alert group by id * * @param loginUser login user - * @param id alert group id + * @param id alert group id * @return delete result code */ @Override - @Transactional - public Map delAlertgroupById(User loginUser, int id) { - Map result = new HashMap<>(); - result.put(Constants.STATUS, false); + public void deleteAlertGroupById(User loginUser, int id) { // only admin can operate if (!canOperatorPermissions(loginUser, new Object[]{id}, AuthorizationType.ALERT_GROUP, ALERT_GROUP_DELETE)) { - putMsg(result, Status.USER_NO_OPERATION_PERM); - return result; + throw new ServiceException(Status.USER_NO_OPERATION_PERM); } // Not allow to delete the default alarm group ,because the module of service need to use it. if (id == 1 || id == 2) { log.warn("Not allow to delete the default alarm group."); - putMsg(result, Status.NOT_ALLOW_TO_DELETE_DEFAULT_ALARM_GROUP); - return result; + throw new ServiceException(Status.NOT_ALLOW_TO_DELETE_DEFAULT_ALARM_GROUP); } // check exist AlertGroup alertGroup = alertGroupMapper.selectById(id); if (alertGroup == null) { - log.error("Alert group does not exist, id:{}.", id); - putMsg(result, Status.ALERT_GROUP_NOT_EXIST); - return result; + throw new ServiceException(Status.ALERT_GROUP_NOT_EXIST); } alertGroupMapper.deleteById(id); log.info("Delete alert group complete, groupId:{}", id); - putMsg(result, Status.SUCCESS); - return result; } /** diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertPluginInstanceServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertPluginInstanceServiceImpl.java index fffb95fbcf3cf..870db0cfcc52d 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertPluginInstanceServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertPluginInstanceServiceImpl.java @@ -23,11 +23,10 @@ import org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant; import org.apache.dolphinscheduler.api.enums.Status; +import org.apache.dolphinscheduler.api.exceptions.ServiceException; import org.apache.dolphinscheduler.api.service.AlertPluginInstanceService; import org.apache.dolphinscheduler.api.utils.PageInfo; -import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.api.vo.AlertPluginInstanceVO; -import org.apache.dolphinscheduler.common.constants.Constants; import org.apache.dolphinscheduler.common.enums.AlertPluginInstanceType; import org.apache.dolphinscheduler.common.enums.AuthorizationType; import org.apache.dolphinscheduler.common.enums.WarningType; @@ -47,7 +46,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Date; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; @@ -59,6 +57,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; @@ -85,15 +84,23 @@ public class AlertPluginInstanceServiceImpl extends BaseServiceImpl implements A /** * creat alert plugin instance * - * @param loginUser login user - * @param pluginDefineId plugin define id - * @param instanceName instance name + * @param loginUser login user + * @param pluginDefineId plugin define id + * @param instanceName instance name * @param pluginInstanceParams plugin instance params */ @Override - public Map create(User loginUser, int pluginDefineId, String instanceName, - AlertPluginInstanceType instanceType, WarningType warningType, + public AlertPluginInstance create(User loginUser, + int pluginDefineId, + String instanceName, + AlertPluginInstanceType instanceType, + WarningType warningType, String pluginInstanceParams) { + + if (!canOperatorPermissions(loginUser, null, AuthorizationType.ALERT_PLUGIN_INSTANCE, ALART_INSTANCE_CREATE)) { + throw new ServiceException(Status.USER_NO_OPERATION_PERM); + } + AlertPluginInstance alertPluginInstance = new AlertPluginInstance(); String paramsMapJson = parsePluginParamsMap(pluginInstanceParams); alertPluginInstance.setPluginInstanceParams(paramsMapJson); @@ -102,16 +109,8 @@ public Map create(User loginUser, int pluginDefineId, String ins alertPluginInstance.setInstanceType(instanceType); alertPluginInstance.setWarningType(warningType); - Map result = new HashMap<>(); - if (!canOperatorPermissions(loginUser, null, AuthorizationType.ALERT_PLUGIN_INSTANCE, ALART_INSTANCE_CREATE)) { - putMsg(result, Status.USER_NO_OPERATION_PERM); - return result; - } if (alertPluginInstanceMapper.existInstanceName(alertPluginInstance.getInstanceName()) == Boolean.TRUE) { - log.error("Plugin instance with the same name already exists, name:{}.", - alertPluginInstance.getInstanceName()); - putMsg(result, Status.PLUGIN_INSTANCE_ALREADY_EXISTS); - return result; + throw new ServiceException(Status.PLUGIN_INSTANCE_ALREADY_EXISTS); } int i = alertPluginInstanceMapper.insert(alertPluginInstance); @@ -131,94 +130,77 @@ public Map create(User loginUser, int pluginDefineId, String ins } alertGroupMapper.updateById(globalAlertGroup); } - result.put(Constants.DATA_LIST, alertPluginInstance); - putMsg(result, Status.SUCCESS); - return result; + return alertPluginInstance; } - log.error("Create alert plugin instance error, name:{}", alertPluginInstance.getInstanceName()); - putMsg(result, Status.SAVE_ERROR); - return result; + throw new ServiceException(Status.SAVE_ERROR); } /** * update alert plugin instance * - * @param loginUser login user - * @param pluginInstanceId plugin instance id - * @param instanceName instance name + * @param loginUser login user + * @param pluginInstanceId plugin instance id + * @param instanceName instance name * @param pluginInstanceParams plugin instance params */ @Override - public Map update(User loginUser, int pluginInstanceId, String instanceName, - WarningType warningType, String pluginInstanceParams) { + public AlertPluginInstance updateById(User loginUser, int pluginInstanceId, String instanceName, + WarningType warningType, String pluginInstanceParams) { + + if (!canOperatorPermissions(loginUser, null, AuthorizationType.ALERT_PLUGIN_INSTANCE, ALERT_PLUGIN_UPDATE)) { + throw new ServiceException(Status.USER_NO_OPERATION_PERM); + } String paramsMapJson = parsePluginParamsMap(pluginInstanceParams); AlertPluginInstance alertPluginInstance = new AlertPluginInstance(pluginInstanceId, paramsMapJson, instanceName, warningType, new Date()); - Map result = new HashMap<>(); - - if (!canOperatorPermissions(loginUser, null, AuthorizationType.ALERT_PLUGIN_INSTANCE, ALERT_PLUGIN_UPDATE)) { - putMsg(result, Status.USER_NO_OPERATION_PERM); - return result; - } int i = alertPluginInstanceMapper.updateById(alertPluginInstance); if (i > 0) { log.info("Update alert plugin instance complete, instanceId:{}, name:{}", alertPluginInstance.getId(), alertPluginInstance.getInstanceName()); - putMsg(result, Status.SUCCESS); - return result; + return alertPluginInstance; } - log.error("Update alert plugin instance error, instanceId:{}, name:{}", alertPluginInstance.getId(), - alertPluginInstance.getInstanceName()); - putMsg(result, Status.SAVE_ERROR); - return result; + throw new ServiceException(Status.SAVE_ERROR); } /** * delete alert plugin instance * - * @param loginUser login user - * @param id id + * @param loginUser login user + * @param alertPluginInstanceId id * @return result */ @Override - public Map delete(User loginUser, int id) { - Map result = new HashMap<>(); - AlertPluginInstance alertPluginInstance = alertPluginInstanceMapper.selectById(id); + @Transactional + public void deleteById(User loginUser, int alertPluginInstanceId) { + if (!canOperatorPermissions(loginUser, null, AuthorizationType.ALERT_PLUGIN_INSTANCE, ALERT_PLUGIN_DELETE)) { + throw new ServiceException(Status.USER_NO_OPERATION_PERM); + } + + AlertPluginInstance alertPluginInstance = alertPluginInstanceMapper.selectById(alertPluginInstanceId); + if (alertPluginInstance.getInstanceType() == AlertPluginInstanceType.GLOBAL) { // global instance will be removed from global alert group automatically AlertGroup globalAlertGroup = alertGroupMapper.selectById(GLOBAL_ALERT_GROUP_ID); List ids = Arrays.stream(globalAlertGroup.getAlertInstanceIds().split(",")) .map(s -> Integer.parseInt(s.trim())) .collect(Collectors.toList()); - ids = ids.stream().filter(x -> x != id).collect(Collectors.toList()); + ids = ids.stream().filter(x -> x != alertPluginInstanceId).collect(Collectors.toList()); globalAlertGroup.setAlertInstanceIds(StringUtils.join(ids, ",")); alertGroupMapper.updateById(globalAlertGroup); log.info("Remove global alert plugin instance from global alert group automatically, name:{}", alertPluginInstance.getInstanceName()); } else { // check if there is an associated alert group - boolean hasAssociatedAlertGroup = checkHasAssociatedAlertGroup(String.valueOf(id)); + boolean hasAssociatedAlertGroup = checkHasAssociatedAlertGroup(String.valueOf(alertPluginInstanceId)); if (hasAssociatedAlertGroup) { - log.warn("Delete alert plugin failed because alert group is using it, pluginId:{}.", id); - putMsg(result, Status.DELETE_ALERT_PLUGIN_INSTANCE_ERROR_HAS_ALERT_GROUP_ASSOCIATED); - return result; + throw new ServiceException(Status.DELETE_ALERT_PLUGIN_INSTANCE_ERROR_HAS_ALERT_GROUP_ASSOCIATED); } } - if (!canOperatorPermissions(loginUser, null, AuthorizationType.ALERT_PLUGIN_INSTANCE, ALERT_PLUGIN_DELETE)) { - putMsg(result, Status.USER_NO_OPERATION_PERM); - return result; - } - int i = alertPluginInstanceMapper.deleteById(id); - if (i > 0) { - log.info("Delete alert plugin instance complete, instanceId:{}", id); - putMsg(result, Status.SUCCESS); - } - log.error("Delete alert plugin instance error, instanceId:{}", id); - return result; + alertPluginInstanceMapper.deleteById(alertPluginInstanceId); } /** @@ -229,33 +211,18 @@ public Map delete(User loginUser, int id) { * @return alert plugin */ @Override - public Map get(User loginUser, int id) { - Map result = new HashMap<>(); - AlertPluginInstance alertPluginInstance = alertPluginInstanceMapper.selectById(id); + public AlertPluginInstance getById(User loginUser, int id) { if (!canOperatorPermissions(loginUser, null, AuthorizationType.ALERT_PLUGIN_INSTANCE, ApiFuncIdentificationConstant.ALARM_INSTANCE_MANAGE)) { - putMsg(result, Status.USER_NO_OPERATION_PERM); - return result; + throw new ServiceException(Status.USER_NO_OPERATION_PERM); } - - if (null != alertPluginInstance) { - putMsg(result, Status.SUCCESS); - result.put(Constants.DATA_LIST, alertPluginInstance); - } - - return result; + return alertPluginInstanceMapper.selectById(id); } @Override - public Map queryAll() { - Map result = new HashMap<>(); + public List queryAll() { List alertPluginInstances = alertPluginInstanceMapper.queryAllAlertPluginInstanceList(); - List alertPluginInstanceVOS = buildPluginInstanceVOList(alertPluginInstances); - if (null != alertPluginInstances) { - putMsg(result, Status.SUCCESS); - result.put(Constants.DATA_LIST, alertPluginInstanceVOS); - } - return result; + return buildPluginInstanceVOList(alertPluginInstances); } @Override @@ -264,19 +231,15 @@ public boolean checkExistPluginInstanceName(String pluginInstanceName) { } @Override - public Result listPaging(User loginUser, String searchVal, int pageNo, int pageSize) { + public PageInfo listPaging(User loginUser, String searchVal, int pageNo, int pageSize) { - Result result = new Result(); - Page page = new Page<>(pageNo, pageSize); IPage alertPluginInstanceIPage = - alertPluginInstanceMapper.queryByInstanceNamePage(page, searchVal); + alertPluginInstanceMapper.queryByInstanceNamePage(new Page<>(pageNo, pageSize), searchVal); PageInfo pageInfo = new PageInfo<>(pageNo, pageSize); pageInfo.setTotal((int) alertPluginInstanceIPage.getTotal()); pageInfo.setTotalList(buildPluginInstanceVOList(alertPluginInstanceIPage.getRecords())); - result.setData(pageInfo); - putMsg(result, Status.SUCCESS); - return result; + return pageInfo; } private List buildPluginInstanceVOList(List alertPluginInstances) { diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AuditServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AuditServiceImpl.java index 4fdf0a169ac66..759e30b0364b7 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AuditServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AuditServiceImpl.java @@ -20,17 +20,14 @@ import org.apache.dolphinscheduler.api.audit.AuditMessage; import org.apache.dolphinscheduler.api.audit.AuditPublishService; import org.apache.dolphinscheduler.api.dto.AuditDto; -import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.service.AuditService; import org.apache.dolphinscheduler.api.utils.PageInfo; -import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.enums.AuditOperationType; import org.apache.dolphinscheduler.common.enums.AuditResourceType; import org.apache.dolphinscheduler.dao.entity.AuditLog; import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.dao.mapper.AuditLogMapper; -import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.stream.Collectors; @@ -74,14 +71,17 @@ public void addAudit(User user, AuditResourceType resourceType, Integer resource * @param userName query user name * @param pageNo page number * @param pageSize page size - * @return audit log string data + * @return audit log string data */ @Override - public Result queryLogListPaging(User loginUser, AuditResourceType resourceType, - AuditOperationType operationType, String startDate, - String endDate, String userName, - Integer pageNo, Integer pageSize) { - Result result = new Result(); + public PageInfo queryLogListPaging(User loginUser, + AuditResourceType resourceType, + AuditOperationType operationType, + String startDate, + String endDate, + String userName, + Integer pageNo, + Integer pageSize) { int[] resourceArray = null; if (resourceType != null) { @@ -93,20 +93,18 @@ public Result queryLogListPaging(User loginUser, AuditResourceType resourceType, opsArray = new int[]{operationType.getCode()}; } - Date start = (Date) checkAndParseDateParameters(startDate); - Date end = (Date) checkAndParseDateParameters(endDate); + Date start = checkAndParseDateParameters(startDate); + Date end = checkAndParseDateParameters(endDate); - Page page = new Page<>(pageNo, pageSize); - IPage logIPage = auditLogMapper.queryAuditLog(page, resourceArray, opsArray, userName, start, end); - List logList = logIPage != null ? logIPage.getRecords() : new ArrayList<>(); - PageInfo pageInfo = new PageInfo<>(pageNo, pageSize); + IPage logIPage = auditLogMapper.queryAuditLog(new Page<>(pageNo, pageSize), resourceArray, opsArray, + userName, start, end); + List auditDtos = + logIPage.getRecords().stream().map(this::transformAuditLog).collect(Collectors.toList()); - List auditDtos = logList.stream().map(this::transformAuditLog).collect(Collectors.toList()); - pageInfo.setTotal((int) (auditDtos != null ? auditDtos.size() : 0L)); + PageInfo pageInfo = new PageInfo<>(pageNo, pageSize); + pageInfo.setTotal((int) auditDtos.size()); pageInfo.setTotalList(auditDtos); - result.setData(pageInfo); - putMsg(result, Status.SUCCESS); - return result; + return pageInfo; } /** diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/BaseServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/BaseServiceImpl.java index b2fb11d3de2a5..6d9d1868a41fa 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/BaseServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/BaseServiceImpl.java @@ -32,13 +32,11 @@ import java.text.MessageFormat; import java.util.Date; -import java.util.List; import java.util.Map; import java.util.Objects; import lombok.extern.slf4j.Slf4j; -import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; /** @@ -50,17 +48,6 @@ public class BaseServiceImpl implements BaseService { @Autowired protected ResourcePermissionCheckService resourcePermissionCheckService; - @Override - public void permissionPostHandle(AuthorizationType authorizationType, Integer userId, List ids, - Logger logger) { - try { - resourcePermissionCheckService.postHandle(authorizationType, userId, ids, logger); - } catch (Exception e) { - log.error("Post handle error, userId:{}.", userId, e); - throw new RuntimeException("Resource association user error", e); - } - } - /** * check admin * @@ -76,17 +63,12 @@ public boolean isAdmin(User user) { * isNotAdmin * * @param loginUser login user - * @param result result code * @return true if not administrator, otherwise false */ @Override - public boolean isNotAdmin(User loginUser, Map result) { + public boolean isNotAdmin(User loginUser) { // only admin can operate - if (!isAdmin(loginUser)) { - putMsg(result, Status.USER_NO_OPERATION_PERM); - return true; - } - return false; + return !isAdmin(loginUser); } /** diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ClusterServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ClusterServiceImpl.java index 8c181c00fe9b6..a5ceb92abc2f6 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ClusterServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ClusterServiceImpl.java @@ -19,13 +19,12 @@ import org.apache.dolphinscheduler.api.dto.ClusterDto; import org.apache.dolphinscheduler.api.enums.Status; +import org.apache.dolphinscheduler.api.exceptions.ServiceException; import org.apache.dolphinscheduler.api.k8s.K8sManager; import org.apache.dolphinscheduler.api.service.ClusterService; import org.apache.dolphinscheduler.api.utils.PageInfo; -import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.constants.Constants; import org.apache.dolphinscheduler.common.utils.CodeGenerateUtils; -import org.apache.dolphinscheduler.common.utils.CodeGenerateUtils.CodeGenerateException; import org.apache.dolphinscheduler.dao.entity.Cluster; import org.apache.dolphinscheduler.dao.entity.K8sNamespace; import org.apache.dolphinscheduler.dao.entity.User; @@ -36,11 +35,9 @@ import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; -import java.util.ArrayList; +import java.util.Collections; import java.util.Date; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.stream.Collectors; import lombok.extern.slf4j.Slf4j; @@ -69,6 +66,7 @@ public class ClusterServiceImpl extends BaseServiceImpl implements ClusterServic @Autowired private K8sNamespaceMapper k8sNamespaceMapper; + /** * create cluster * @@ -79,23 +77,16 @@ public class ClusterServiceImpl extends BaseServiceImpl implements ClusterServic */ @Transactional @Override - public Map createCluster(User loginUser, String name, String config, String desc) { - Map result = new HashMap<>(); - if (isNotAdmin(loginUser, result)) { - log.warn("Only admin can create cluster, current login user name:{}.", loginUser.getUserName()); - return result; + public Long createCluster(User loginUser, String name, String config, String desc) { + if (isNotAdmin(loginUser)) { + throw new ServiceException(Status.USER_NO_OPERATION_PERM); } - Map checkResult = checkParams(name, config); - if (checkResult.get(Constants.STATUS) != Status.SUCCESS) { - return checkResult; - } + checkParams(name, config); Cluster clusterExistByName = clusterMapper.queryByClusterName(name); if (clusterExistByName != null) { - log.warn("Cluster with the same name already exists, clusterName:{}.", clusterExistByName.getName()); - putMsg(result, Status.CLUSTER_NAME_EXISTS, name); - return result; + throw new ServiceException(Status.CLUSTER_NAME_EXISTS, name); } Cluster cluster = new Cluster(); @@ -105,27 +96,12 @@ public Map createCluster(User loginUser, String name, String con cluster.setOperator(loginUser.getId()); cluster.setCreateTime(new Date()); cluster.setUpdateTime(new Date()); - long code = 0L; - try { - code = CodeGenerateUtils.getInstance().genCode(); - cluster.setCode(code); - } catch (CodeGenerateException e) { - log.error("Generate cluster code error.", e); - } - if (code == 0L) { - putMsg(result, Status.INTERNAL_SERVER_ERROR_ARGS, "Error generating cluster code"); - return result; - } + cluster.setCode(CodeGenerateUtils.getInstance().genCode()); if (clusterMapper.insert(cluster) > 0) { - log.info("Cluster create complete, clusterName:{}.", cluster.getName()); - result.put(Constants.DATA_LIST, cluster.getCode()); - putMsg(result, Status.SUCCESS); - } else { - log.error("Cluster create error, clusterName:{}.", cluster.getName()); - putMsg(result, Status.CREATE_CLUSTER_ERROR); + return cluster.getCode(); } - return result; + throw new ServiceException(Status.CREATE_CLUSTER_ERROR); } /** @@ -137,8 +113,7 @@ public Map createCluster(User loginUser, String name, String con * @return cluster list page */ @Override - public Result queryClusterListPaging(Integer pageNo, Integer pageSize, String searchVal) { - Result result = new Result(); + public PageInfo queryClusterListPaging(Integer pageNo, Integer pageSize, String searchVal) { Page page = new Page<>(pageNo, pageSize); @@ -147,22 +122,16 @@ public Result queryClusterListPaging(Integer pageNo, Integer pageSize, String se PageInfo pageInfo = new PageInfo<>(pageNo, pageSize); pageInfo.setTotal((int) clusterIPage.getTotal()); - if (CollectionUtils.isNotEmpty(clusterIPage.getRecords())) { - - List dtoList = clusterIPage.getRecords().stream().map(cluster -> { - ClusterDto dto = new ClusterDto(); - BeanUtils.copyProperties(cluster, dto); - return dto; - }).collect(Collectors.toList()); - - pageInfo.setTotalList(dtoList); - } else { - pageInfo.setTotalList(new ArrayList<>()); + if (CollectionUtils.isEmpty(clusterIPage.getRecords())) { + return pageInfo; } - - result.setData(pageInfo); - putMsg(result, Status.SUCCESS); - return result; + List dtoList = clusterIPage.getRecords().stream().map(cluster -> { + ClusterDto dto = new ClusterDto(); + BeanUtils.copyProperties(cluster, dto); + return dto; + }).collect(Collectors.toList()); + pageInfo.setTotalList(dtoList); + return pageInfo; } /** @@ -171,24 +140,19 @@ public Result queryClusterListPaging(Integer pageNo, Integer pageSize, String se * @return all cluster list */ @Override - public Map queryAllClusterList() { - Map result = new HashMap<>(); + public List queryAllClusterList() { List clusterList = clusterMapper.queryAllClusterList(); - - if (CollectionUtils.isNotEmpty(clusterList)) { - - List dtoList = clusterList.stream().map(cluster -> { - ClusterDto dto = new ClusterDto(); - BeanUtils.copyProperties(cluster, dto); - return dto; - }).collect(Collectors.toList()); - result.put(Constants.DATA_LIST, dtoList); - } else { - result.put(Constants.DATA_LIST, new ArrayList<>()); + if (CollectionUtils.isEmpty(clusterList)) { + return Collections.emptyList(); } - putMsg(result, Status.SUCCESS); - return result; + return clusterList.stream() + .map(cluster -> { + ClusterDto dto = new ClusterDto(); + // todo: Don't use copy + BeanUtils.copyProperties(cluster, dto); + return dto; + }).collect(Collectors.toList()); } /** @@ -197,21 +161,16 @@ public Map queryAllClusterList() { * @param code cluster code */ @Override - public Map queryClusterByCode(Long code) { - Map result = new HashMap<>(); + public ClusterDto queryClusterByCode(Long code) { Cluster cluster = clusterMapper.queryByClusterCode(code); if (cluster == null) { - putMsg(result, Status.QUERY_CLUSTER_BY_CODE_ERROR, code); - } else { - - ClusterDto dto = new ClusterDto(); - BeanUtils.copyProperties(cluster, dto); - result.put(Constants.DATA_LIST, dto); - putMsg(result, Status.SUCCESS); + throw new ServiceException(Status.QUERY_CLUSTER_BY_CODE_ERROR, code); } - return result; + ClusterDto dto = new ClusterDto(); + BeanUtils.copyProperties(cluster, dto); + return dto; } /** @@ -220,21 +179,15 @@ public Map queryClusterByCode(Long code) { * @param name cluster name */ @Override - public Map queryClusterByName(String name) { - Map result = new HashMap<>(); + public ClusterDto queryClusterByName(String name) { Cluster cluster = clusterMapper.queryByClusterName(name); if (cluster == null) { - log.warn("Cluster does not exist, name:{}.", name); - putMsg(result, Status.QUERY_CLUSTER_BY_NAME_ERROR, name); - } else { - - ClusterDto dto = new ClusterDto(); - BeanUtils.copyProperties(cluster, dto); - result.put(Constants.DATA_LIST, dto); - putMsg(result, Status.SUCCESS); + throw new ServiceException(Status.QUERY_CLUSTER_BY_NAME_ERROR, name); } - return result; + ClusterDto dto = new ClusterDto(); + BeanUtils.copyProperties(cluster, dto); + return dto; } /** @@ -243,34 +196,24 @@ public Map queryClusterByName(String name) { * @param loginUser login user * @param code cluster code */ - @Transactional @Override - public Map deleteClusterByCode(User loginUser, Long code) { - Map result = new HashMap<>(); - if (isNotAdmin(loginUser, result)) { - log.warn("Only admin can delete cluster, current login user name:{}.", loginUser.getUserName()); - return result; + public void deleteClusterByCode(User loginUser, Long code) { + if (isNotAdmin(loginUser)) { + throw new ServiceException(Status.USER_NO_OPERATION_PERM); } Long relatedNamespaceNumber = k8sNamespaceMapper .selectCount(new QueryWrapper().lambda().eq(K8sNamespace::getClusterCode, code)); if (relatedNamespaceNumber > 0) { - log.warn("Delete cluster failed because {} namespace(s) is(are) using it, clusterCode:{}.", - relatedNamespaceNumber, code); - putMsg(result, Status.DELETE_CLUSTER_RELATED_NAMESPACE_EXISTS); - return result; + throw new ServiceException(Status.DELETE_CLUSTER_RELATED_NAMESPACE_EXISTS); } int delete = clusterMapper.deleteByCode(code); if (delete > 0) { - log.info("Delete cluster complete, clusterCode:{}.", code); - putMsg(result, Status.SUCCESS); - } else { - log.error("Delete cluster error, clusterCode:{}.", code); - putMsg(result, Status.DELETE_CLUSTER_ERROR); + return; } - return result; + throw new ServiceException(Status.DELETE_CLUSTER_ERROR); } /** @@ -282,38 +225,30 @@ public Map deleteClusterByCode(User loginUser, Long code) { * @param config cluster config * @param desc cluster desc */ - @Transactional @Override - public Map updateClusterByCode(User loginUser, Long code, String name, String config, String desc) { - Map result = new HashMap<>(); - if (isNotAdmin(loginUser, result)) { - log.warn("Only admin can update cluster, current login user name:{}.", loginUser.getUserName()); - return result; + public Cluster updateClusterByCode(User loginUser, + Long code, + String name, + String config, + String desc) { + if (isNotAdmin(loginUser)) { + throw new ServiceException(Status.USER_NO_OPERATION_PERM); } if (checkDescriptionLength(desc)) { - log.warn("Parameter description is too long."); - putMsg(result, Status.DESCRIPTION_TOO_LONG_ERROR); - return result; + throw new ServiceException(Status.DESCRIPTION_TOO_LONG_ERROR); } - Map checkResult = checkParams(name, config); - if (checkResult.get(Constants.STATUS) != Status.SUCCESS) { - return checkResult; - } + checkParams(name, config); Cluster clusterExistByName = clusterMapper.queryByClusterName(name); if (clusterExistByName != null && !clusterExistByName.getCode().equals(code)) { - log.warn("Cluster with the same name already exists, name:{}.", clusterExistByName.getName()); - putMsg(result, Status.CLUSTER_NAME_EXISTS, name); - return result; + throw new ServiceException(Status.CLUSTER_NAME_EXISTS, name); } Cluster clusterExist = clusterMapper.queryByClusterCode(code); if (clusterExist == null) { - log.error("Cluster does not exist, code:{}.", code); - putMsg(result, Status.CLUSTER_NOT_EXISTS, name); - return result; + throw new ServiceException(Status.CLUSTER_NOT_EXISTS, name); } if (!Constants.K8S_LOCAL_TEST_CLUSTER_CODE.equals(clusterExist.getCode()) @@ -321,21 +256,17 @@ public Map updateClusterByCode(User loginUser, Long code, String try { k8sManager.getAndUpdateK8sClient(code, true); } catch (Exception e) { - log.error("Update K8s error.", e); - putMsg(result, Status.K8S_CLIENT_OPS_ERROR, name); - return result; + throw new ServiceException(Status.K8S_CLIENT_OPS_ERROR, name); } } // update cluster + // need not update relation clusterExist.setConfig(config); clusterExist.setName(name); clusterExist.setDescription(desc); clusterMapper.updateById(clusterExist); - // need not update relation - log.info("Cluster update complete, clusterId:{}.", clusterExist.getId()); - putMsg(result, Status.SUCCESS); - return result; + return clusterExist; } /** @@ -345,40 +276,25 @@ public Map updateClusterByCode(User loginUser, Long code, String * @return true if the cluster name not exists, otherwise return false */ @Override - public Map verifyCluster(String clusterName) { - Map result = new HashMap<>(); + public void verifyCluster(String clusterName) { if (StringUtils.isEmpty(clusterName)) { - log.warn("Parameter cluster name is empty."); - putMsg(result, Status.CLUSTER_NAME_IS_NULL); - return result; + throw new ServiceException(Status.CLUSTER_NAME_IS_NULL); } Cluster cluster = clusterMapper.queryByClusterName(clusterName); if (cluster != null) { - log.warn("Cluster with the same name already exists, name:{}.", cluster.getName()); - putMsg(result, Status.CLUSTER_NAME_EXISTS, clusterName); - return result; + throw new ServiceException(Status.CLUSTER_NAME_EXISTS); } - - result.put(Constants.STATUS, Status.SUCCESS); - return result; } - public Map checkParams(String name, String config) { - Map result = new HashMap<>(); + protected void checkParams(String name, String config) { if (StringUtils.isEmpty(name)) { - log.warn("Parameter cluster name is empty."); - putMsg(result, Status.CLUSTER_NAME_IS_NULL); - return result; + throw new ServiceException(Status.CLUSTER_NAME_IS_NULL); } if (StringUtils.isEmpty(config)) { - log.warn("Parameter cluster config is empty."); - putMsg(result, Status.CLUSTER_CONFIG_IS_NULL); - return result; + throw new ServiceException(Status.CLUSTER_CONFIG_IS_NULL); } - result.put(Constants.STATUS, Status.SUCCESS); - return result; } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataAnalysisServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataAnalysisServiceImpl.java index 6ae4977b81414..aa716cc6b27be 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataAnalysisServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataAnalysisServiceImpl.java @@ -24,6 +24,7 @@ import org.apache.dolphinscheduler.api.dto.TaskCountDto; import org.apache.dolphinscheduler.api.dto.project.StatisticsStateRequest; import org.apache.dolphinscheduler.api.enums.Status; +import org.apache.dolphinscheduler.api.exceptions.ServiceException; import org.apache.dolphinscheduler.api.service.DataAnalysisService; import org.apache.dolphinscheduler.api.service.ProjectService; import org.apache.dolphinscheduler.common.constants.Constants; @@ -35,6 +36,7 @@ import org.apache.dolphinscheduler.dao.entity.CommandCount; import org.apache.dolphinscheduler.dao.entity.DefinitionGroupByUser; import org.apache.dolphinscheduler.dao.entity.ExecuteStatusCount; +import org.apache.dolphinscheduler.dao.entity.ProcessDefinition; import org.apache.dolphinscheduler.dao.entity.Project; import org.apache.dolphinscheduler.dao.entity.TaskDefinition; import org.apache.dolphinscheduler.dao.entity.User; @@ -101,6 +103,7 @@ public class DataAnalysisServiceImpl extends BaseServiceImpl implements DataAnal @Autowired private ProcessTaskRelationMapper relationMapper; + /** * statistical task instance status data * @@ -111,8 +114,8 @@ public class DataAnalysisServiceImpl extends BaseServiceImpl implements DataAnal * @return task state count data */ @Override - public Map countTaskStateByProject(User loginUser, long projectCode, String startDate, - String endDate) { + public TaskCountDto countTaskStateByProject(User loginUser, long projectCode, String startDate, + String endDate) { return countStateByProject( loginUser, @@ -132,22 +135,21 @@ public Map countTaskStateByProject(User loginUser, long projectC * @return process instance state count data */ @Override - public Map countProcessInstanceStateByProject(User loginUser, long projectCode, String startDate, - String endDate) { - Map result = this.countStateByProject( + public TaskCountDto countProcessInstanceStateByProject(User loginUser, long projectCode, String startDate, + String endDate) { + TaskCountDto taskCountDto = countStateByProject( loginUser, projectCode, startDate, endDate, - (start, end, projectCodes) -> this.processInstanceMapper.countInstanceStateByProjectCodes(start, end, + (start, end, projectCodes) -> processInstanceMapper.countInstanceStateByProjectCodes(start, end, projectCodes)); // process state count needs to remove state of forced success - if (result.containsKey(Constants.STATUS) && result.get(Constants.STATUS).equals(Status.SUCCESS)) { - ((TaskCountDto) result.get(Constants.DATA_LIST)) - .removeStateFromCountList(TaskExecutionStatus.FORCED_SUCCESS); + if (taskCountDto != null) { + taskCountDto.removeStateFromCountList(TaskExecutionStatus.FORCED_SUCCESS); } - return result; + return taskCountDto; } /** @@ -158,15 +160,13 @@ public Map countProcessInstanceStateByProject(User loginUser, lo * @param startDate start date * @param endDate end date */ - private Map countStateByProject(User loginUser, long projectCode, String startDate, String endDate, - TriFunction> instanceStateCounter) { - Map result = new HashMap<>(); + private TaskCountDto countStateByProject(User loginUser, + long projectCode, + String startDate, + String endDate, + TriFunction> instanceStateCounter) { if (projectCode != 0) { - Project project = projectMapper.queryByCode(projectCode); - result = projectService.checkProjectAndAuth(loginUser, project, projectCode, PROJECT_OVERVIEW); - if (result.get(Constants.STATUS) != Status.SUCCESS) { - return result; - } + projectService.checkProjectAndAuthThrowException(loginUser, projectCode, PROJECT_OVERVIEW); } Date start = null; @@ -175,12 +175,10 @@ private Map countStateByProject(User loginUser, long projectCode start = DateUtils.stringToDate(startDate); end = DateUtils.stringToDate(endDate); if (Objects.isNull(start) || Objects.isNull(end)) { - log.warn("Parameter startDate or endDate is invalid."); - putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, Constants.START_END_DATE); - return result; + throw new ServiceException(Status.REQUEST_PARAMS_NOT_VALID_ERROR, Constants.START_END_DATE); } } - Pair, Map> projectIds = getProjectIds(loginUser, result); + Pair, TaskCountDto> projectIds = getProjectIds(loginUser); if (projectIds.getRight() != null) { return projectIds.getRight(); } @@ -193,11 +191,9 @@ private Map countStateByProject(User loginUser, long projectCode } if (processInstanceStateCounts != null) { - TaskCountDto taskCountResult = new TaskCountDto(processInstanceStateCounts); - result.put(Constants.DATA_LIST, taskCountResult); - putMsg(result, Status.SUCCESS); + return new TaskCountDto(processInstanceStateCounts); } - return result; + return null; } /** @@ -210,35 +206,26 @@ private Map countStateByProject(User loginUser, long projectCode * @return definition count data */ @Override - public Map countDefinitionByUser(User loginUser, long projectCode) { - Map result = new HashMap<>(); + public DefineUserDto countDefinitionByUser(User loginUser, long projectCode) { if (projectCode != 0) { - Project project = projectMapper.queryByCode(projectCode); - result = projectService.checkProjectAndAuth(loginUser, project, projectCode, PROJECT_OVERVIEW); - if (result.get(Constants.STATUS) != Status.SUCCESS) { - return result; - } + projectService.checkProjectAndAuthThrowException(loginUser, projectCode, PROJECT_OVERVIEW); } - List defineGroupByUsers = new ArrayList<>(); - Pair, Map> projectIds = getProjectIds(loginUser, result); + // todo: refactor this method, don't use Pair + Pair, TaskCountDto> projectIds = getProjectIds(loginUser); if (projectIds.getRight() != null) { List emptyList = new ArrayList<>(); - DefineUserDto dto = new DefineUserDto(emptyList); - result.put(Constants.DATA_LIST, dto); - putMsg(result, Status.SUCCESS); - return result; + return new DefineUserDto(emptyList); } + + List defineGroupByUsers = new ArrayList<>(); Long[] projectCodeArray = projectCode == 0 ? getProjectCodesArrays(projectIds.getLeft()) : new Long[]{projectCode}; if (projectCodeArray.length != 0 || loginUser.getUserType() == UserType.ADMIN_USER) { defineGroupByUsers = processDefinitionMapper.countDefinitionByProjectCodes(projectCodeArray); } - DefineUserDto dto = new DefineUserDto(defineGroupByUsers); - result.put(Constants.DATA_LIST, dto); - putMsg(result, Status.SUCCESS); - return result; + return new DefineUserDto(defineGroupByUsers); } /** @@ -248,8 +235,7 @@ public Map countDefinitionByUser(User loginUser, long projectCod * @return command state count data */ @Override - public Map countCommandState(User loginUser) { - Map result = new HashMap<>(); + public List countCommandState(User loginUser) { /** * find all the task lists in the project under the user @@ -257,13 +243,12 @@ public Map countCommandState(User loginUser) { */ Date start = null; Date end = null; - Pair, Map> projectIds = getProjectIds(loginUser, result); + Pair, TaskCountDto> projectIds = getProjectIds(loginUser); if (projectIds.getRight() != null) { List noData = Arrays.stream(CommandType.values()) - .map(commandType -> new CommandStateCount(0, 0, commandType)).collect(Collectors.toList()); - result.put(Constants.DATA_LIST, noData); - putMsg(result, Status.SUCCESS); - return result; + .map(commandType -> new CommandStateCount(0, 0, commandType)) + .collect(Collectors.toList()); + return noData; } Long[] projectCodeArray = getProjectCodesArrays(projectIds.getLeft()); // count normal command state @@ -284,20 +269,15 @@ public Map countCommandState(User loginUser) { normalCountCommandCounts.getOrDefault(commandType, 0), commandType)) .collect(Collectors.toList()); - - result.put(Constants.DATA_LIST, list); - putMsg(result, Status.SUCCESS); - return result; + return list; } - private Pair, Map> getProjectIds(User loginUser, Map result) { + private Pair, TaskCountDto> getProjectIds(User loginUser) { Set projectIds = resourcePermissionCheckService .userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, loginUser.getId(), log); if (projectIds.isEmpty()) { List taskInstanceStateCounts = new ArrayList<>(); - result.put(Constants.DATA_LIST, new TaskCountDto(taskInstanceStateCounts)); - putMsg(result, Status.SUCCESS); - return Pair.of(null, result); + return Pair.of(null, new TaskCountDto(taskInstanceStateCounts)); } return Pair.of(projectIds, null); } @@ -316,16 +296,14 @@ private Long[] getProjectCodesArrays(Set projectIds) { * @return queue state count data */ @Override - public Map countQueueState(User loginUser) { - Map result = new HashMap<>(); + public Map countQueueState(User loginUser) { // TODO need to add detail data info + // todo: refactor this method, don't use Map Map dataMap = new HashMap<>(); dataMap.put("taskQueue", 0); dataMap.put("taskKill", 0); - result.put(Constants.DATA_LIST, dataMap); - putMsg(result, Status.SUCCESS); - return result; + return dataMap; } @Override @@ -375,6 +353,7 @@ public Map queryAllWorkflowCounts(User loginUser) { List projectCodes = projects.stream().map(project -> project.getCode()).collect(Collectors.toList()); count = projectMapper.queryAllWorkflowCounts(projectCodes); } + // todo: refactor this method, don't use Map result.put("data", "AllWorkflowCounts = " + count); putMsg(result, Status.SUCCESS); return result; @@ -382,19 +361,18 @@ public Map queryAllWorkflowCounts(User loginUser) { /** * query all workflow states count - * @param loginUser login user + * + * @param loginUser login user * @param statisticsStateRequest statisticsStateRequest * @return workflow states count */ @Override - public Map countWorkflowStates(User loginUser, - StatisticsStateRequest statisticsStateRequest) { - Map result = new HashMap<>(); + public TaskCountDto countWorkflowStates(User loginUser, + StatisticsStateRequest statisticsStateRequest) { Set projectIds = resourcePermissionCheckService .userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, loginUser.getId(), log); if (projectIds.isEmpty()) { - putMsg(result, Status.SUCCESS); - return result; + return new TaskCountDto(Collections.emptyList()); } String projectName = statisticsStateRequest.getProjectName(); @@ -425,50 +403,42 @@ public Map countWorkflowStates(User loginUser, List executeStatusCounts = processInstanceMapper.countInstanceStateV2( startTime, endTime, projectCode, workflowCode, model, projectIds); - TaskCountDto taskCountResult = new TaskCountDto(executeStatusCounts); - result.put(Constants.DATA_LIST, taskCountResult); - putMsg(result, Status.SUCCESS); - return result; + return new TaskCountDto(executeStatusCounts); } /** * query one workflow states count - * @param loginUser login user + * + * @param loginUser login user * @param workflowCode workflowCode * @return workflow states count */ @Override - public Map countOneWorkflowStates(User loginUser, Long workflowCode) { - Map result = new HashMap<>(); - Project project = projectMapper.queryByCode(workflowCode); - boolean hasProjectAndWritePerm = projectService.hasProjectAndWritePerm(loginUser, project, result); - if (!hasProjectAndWritePerm) { - return result; - } - List executeStatusCounts = processInstanceMapper.countInstanceStateV2( - null, null, null, workflowCode, Constants.QUERY_ALL_ON_WORKFLOW, null); - if (executeStatusCounts != null) { - TaskCountDto taskCountResult = new TaskCountDto(executeStatusCounts); - result.put(Constants.DATA_LIST, taskCountResult); - putMsg(result, Status.SUCCESS); + public TaskCountDto countOneWorkflowStates(User loginUser, Long workflowCode) { + ProcessDefinition processDefinition = processDefinitionMapper.queryByCode(workflowCode); + if (processDefinition == null) { + throw new ServiceException(Status.PROCESS_DEFINE_NOT_EXIST, workflowCode); } - return result; + projectService.checkHasProjectWritePermissionThrowException(loginUser, processDefinition.getProjectCode()); + + List executeStatusCounts = processInstanceMapper.countInstanceStateV2(null, null, null, + workflowCode, Constants.QUERY_ALL_ON_WORKFLOW, null); + return new TaskCountDto(executeStatusCounts); } /** * query all task states count - * @param loginUser login user + * + * @param loginUser login user * @param statisticsStateRequest statisticsStateRequest * @return tasks states count */ @Override - public Map countTaskStates(User loginUser, StatisticsStateRequest statisticsStateRequest) { - Map result = new HashMap<>(); + public TaskCountDto countTaskStates(User loginUser, StatisticsStateRequest statisticsStateRequest) { Set projectIds = resourcePermissionCheckService .userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, loginUser.getId(), log); if (projectIds.isEmpty()) { - putMsg(result, Status.SUCCESS); - return result; + return new TaskCountDto(Collections.emptyList()); } String projectName = statisticsStateRequest.getProjectName(); String workflowName = statisticsStateRequest.getWorkflowName(); @@ -508,10 +478,7 @@ public Map countTaskStates(User loginUser, StatisticsStateReques taskInstanceMapper.countTaskInstanceStateByProjectIdsV2(startTime, endTime, projectIds)); List needRecountState = setOptional(startTimeStates); if (needRecountState.size() == 0) { - TaskCountDto taskCountResult = new TaskCountDto(startTimeStates.get()); - result.put(Constants.DATA_LIST, taskCountResult); - putMsg(result, Status.SUCCESS); - return result; + return new TaskCountDto(startTimeStates.get()); } List recounts = this.taskInstanceMapper .countTaskInstanceStateByProjectCodesAndStatesBySubmitTimeV2(startTime, endTime, projectCode, @@ -519,37 +486,29 @@ public Map countTaskStates(User loginUser, StatisticsStateReques needRecountState); startTimeStates.orElseGet(ArrayList::new).addAll(recounts); List executeStatusCounts = startTimeStates.orElse(null); - TaskCountDto taskCountResult = new TaskCountDto(executeStatusCounts); - result.put(Constants.DATA_LIST, taskCountResult); - putMsg(result, Status.SUCCESS); - return result; + return new TaskCountDto(executeStatusCounts); } /** * query one task states count + * * @param loginUser login user - * @param taskCode taskCode + * @param taskCode taskCode * @return tasks states count */ @Override - public Map countOneTaskStates(User loginUser, Long taskCode) { - Map result = new HashMap<>(); + public TaskCountDto countOneTaskStates(User loginUser, Long taskCode) { TaskDefinition taskDefinition = taskDefinitionMapper.queryByCode(taskCode); long projectCode = taskDefinition.getProjectCode(); Project project = projectMapper.queryByCode(projectCode); - boolean hasProjectAndWritePerm = projectService.hasProjectAndWritePerm(loginUser, project, result); - if (!hasProjectAndWritePerm) { - return result; - } + projectService.checkHasProjectWritePermissionThrowException(loginUser, project); + Set projectId = Collections.singleton(project.getId()); Optional> startTimeStates = Optional.ofNullable( taskInstanceMapper.countTaskInstanceStateByProjectIdsV2(null, null, projectId)); List needRecountState = setOptional(startTimeStates); if (needRecountState.size() == 0) { - TaskCountDto taskCountResult = new TaskCountDto(startTimeStates.get()); - result.put(Constants.DATA_LIST, taskCountResult); - putMsg(result, Status.SUCCESS); - return result; + return new TaskCountDto(startTimeStates.get()); } List recounts = this.taskInstanceMapper .countTaskInstanceStateByProjectCodesAndStatesBySubmitTimeV2(null, null, projectCode, null, taskCode, @@ -557,10 +516,7 @@ public Map countOneTaskStates(User loginUser, Long taskCode) { needRecountState); startTimeStates.orElseGet(ArrayList::new).addAll(recounts); List executeStatusCounts = startTimeStates.orElse(null); - TaskCountDto taskCountResult = new TaskCountDto(executeStatusCounts); - result.put(Constants.DATA_LIST, taskCountResult); - putMsg(result, Status.SUCCESS); - return result; + return new TaskCountDto(executeStatusCounts); } /** @@ -573,25 +529,18 @@ public Map countOneTaskStates(User loginUser, Long taskCode) { * @return definition count data */ @Override - public Map countDefinitionByUserV2(User loginUser, Long projectCode, Integer userId, - Integer releaseState) { - Map result = new HashMap<>(); + public DefineUserDto countDefinitionByUserV2(User loginUser, Long projectCode, Integer userId, + Integer releaseState) { if (null != projectCode) { Project project = projectMapper.queryByCode(projectCode); - result = projectService.checkProjectAndAuth(loginUser, project, projectCode, PROJECT_OVERVIEW); - if (result.get(Constants.STATUS) != Status.SUCCESS) { - return result; - } + projectService.checkProjectAndAuth(loginUser, project, projectCode, PROJECT_OVERVIEW); } List defineGroupByUsers = new ArrayList<>(); - Pair, Map> projectIds = getProjectIds(loginUser, result); + Pair, TaskCountDto> projectIds = getProjectIds(loginUser); if (projectIds.getRight() != null) { List emptyList = new ArrayList<>(); - DefineUserDto dto = new DefineUserDto(emptyList); - result.put(Constants.DATA_LIST, dto); - putMsg(result, Status.SUCCESS); - return result; + return new DefineUserDto(emptyList); } Long[] projectCodeArray = projectCode == null ? getProjectCodesArrays(projectIds.getLeft()) : new Long[]{projectCode}; @@ -600,10 +549,7 @@ public Map countDefinitionByUserV2(User loginUser, Long projectC processDefinitionMapper.countDefinitionByProjectCodesV2(projectCodeArray, userId, releaseState); } - DefineUserDto dto = new DefineUserDto(defineGroupByUsers); - result.put(Constants.DATA_LIST, dto); - putMsg(result, Status.SUCCESS); - return result; + return new DefineUserDto(defineGroupByUsers); } @Override diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java index c095c2bd628cd..0ac7763ba7cd0 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java @@ -25,7 +25,6 @@ import org.apache.dolphinscheduler.api.exceptions.ServiceException; 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.common.constants.Constants; import org.apache.dolphinscheduler.common.enums.AuthorizationType; import org.apache.dolphinscheduler.common.enums.UserType; @@ -90,30 +89,23 @@ public class DataSourceServiceImpl extends BaseServiceImpl implements DataSource /** * create data source * - * @param loginUser login user + * @param loginUser login user * @param datasourceParam datasource parameters * @return create result code */ @Override - @Transactional - public Result createDataSource(User loginUser, BaseDataSourceParamDTO datasourceParam) { + public DataSource createDataSource(User loginUser, BaseDataSourceParamDTO datasourceParam) { DataSourceUtils.checkDatasourceParam(datasourceParam); - Result result = new Result<>(); if (!canOperatorPermissions(loginUser, null, AuthorizationType.DATASOURCE, ApiFuncIdentificationConstant.DATASOURCE_CREATE_DATASOURCE)) { - putMsg(result, Status.USER_NO_OPERATION_PERM); - return result; + throw new ServiceException(Status.USER_NO_OPERATION_PERM); } // check name can use or not if (checkName(datasourceParam.getName())) { - log.warn("Datasource with the same name already exists, name:{}.", datasourceParam.getName()); - putMsg(result, Status.DATASOURCE_EXIST); - return result; + throw new ServiceException(Status.DATASOURCE_EXIST); } if (checkDescriptionLength(datasourceParam.getNote())) { - log.warn("Parameter description is too long, description:{}.", datasourceParam.getNote()); - putMsg(result, Status.DESCRIPTION_TOO_LONG_ERROR); - return result; + throw new ServiceException(Status.DESCRIPTION_TOO_LONG_ERROR); } ConnectionParam connectionParam = DataSourceUtils.buildConnectionParams(datasourceParam); @@ -131,54 +123,38 @@ public Result createDataSource(User loginUser, BaseDataSourceParamDTO da dataSource.setUpdateTime(now); try { dataSourceMapper.insert(dataSource); - putMsg(result, Status.SUCCESS); - permissionPostHandle(AuthorizationType.DATASOURCE, loginUser.getId(), - Collections.singletonList(dataSource.getId()), log); - log.info("Datasource create complete, dbType:{}, datasourceName:{}.", dataSource.getType().getDescp(), - dataSource.getName()); + return dataSource; } catch (DuplicateKeyException ex) { - log.error("Datasource create error.", ex); - putMsg(result, Status.DATASOURCE_EXIST); + throw new ServiceException(Status.DATASOURCE_EXIST); } - - return result; } /** * updateProcessInstance datasource * * @param loginUser login user - * @param id data source id * @return update result code */ @Override - public Result updateDataSource(int id, User loginUser, BaseDataSourceParamDTO dataSourceParam) { + public DataSource updateDataSource(User loginUser, BaseDataSourceParamDTO dataSourceParam) { DataSourceUtils.checkDatasourceParam(dataSourceParam); - Result result = new Result<>(); // determine whether the data source exists - DataSource dataSource = dataSourceMapper.selectById(id); + DataSource dataSource = dataSourceMapper.selectById(dataSourceParam.getId()); if (dataSource == null) { - log.error("Datasource does not exist, id:{}.", id); - putMsg(result, Status.RESOURCE_NOT_EXIST); - return result; + throw new ServiceException(Status.RESOURCE_NOT_EXIST); } if (!canOperatorPermissions(loginUser, new Object[]{dataSource.getId()}, AuthorizationType.DATASOURCE, DATASOURCE_UPDATE)) { - putMsg(result, Status.USER_NO_OPERATION_PERM); - return result; + throw new ServiceException(Status.USER_NO_OPERATION_PERM); } // check name can use or not if (!dataSourceParam.getName().trim().equals(dataSource.getName()) && checkName(dataSourceParam.getName())) { - log.warn("Datasource with the same name already exists, name:{}.", dataSource.getName()); - putMsg(result, Status.DATASOURCE_EXIST); - return result; + throw new ServiceException(Status.DATASOURCE_EXIST); } if (checkDescriptionLength(dataSourceParam.getNote())) { - log.warn("Parameter description is too long, description:{}.", dataSourceParam.getNote()); - putMsg(result, Status.DESCRIPTION_TOO_LONG_ERROR); - return result; + throw new ServiceException(Status.DESCRIPTION_TOO_LONG_ERROR); } // check password,if the password is not updated, set to the old password. ConnectionParam connectionParam = DataSourceUtils.buildConnectionParams(dataSourceParam); @@ -201,15 +177,10 @@ public Result updateDataSource(int id, User loginUser, BaseDataSourcePar dataSource.setUpdateTime(now); try { dataSourceMapper.updateById(dataSource); - log.info("Update datasource complete, datasourceId:{}, datasourceName:{}.", dataSource.getId(), - dataSource.getName()); - putMsg(result, Status.SUCCESS); + return dataSource; } catch (DuplicateKeyException ex) { - log.error("Update datasource error, datasourceId:{}, datasourceName:{}.", dataSource.getId(), - dataSource.getName()); - putMsg(result, Status.DATASOURCE_EXIST); + throw new ServiceException(Status.DATASOURCE_EXIST); } - return result; } private boolean checkName(String name) { @@ -333,40 +304,29 @@ public List queryDataSourceList(User loginUser, Integer type) { * @return true if data datasource not exists, otherwise return false */ @Override - public Result verifyDataSourceName(String name) { - Result result = new Result<>(); + public void verifyDataSourceName(String name) { List dataSourceList = dataSourceMapper.queryDataSourceByName(name); if (dataSourceList != null && !dataSourceList.isEmpty()) { - log.warn("Datasource with the same name already exists, dataSourceName:{}.", - dataSourceList.get(0).getName()); - putMsg(result, Status.DATASOURCE_EXIST); - } else { - putMsg(result, Status.SUCCESS); + throw new ServiceException(Status.DATASOURCE_EXIST); } - - return result; } /** * check connection * - * @param type data source type + * @param type data source type * @param connectionParam connectionParam * @return true if connect successfully, otherwise false * @return true if connect successfully, otherwise false */ @Override - public Result checkConnection(DbType type, ConnectionParam connectionParam) { - Result result = new Result<>(); + public void checkConnection(DbType type, ConnectionParam connectionParam) { DataSourceProcessor sshDataSourceProcessor = DataSourceUtils.getDatasourceProcessor(type); boolean connectivity = sshDataSourceProcessor.checkDataSourceConnectivity(connectionParam); if (connectivity) { - putMsg(result, Status.SUCCESS); - } else { - putMsg(result, Status.CONNECTION_TEST_FAILURE); + return; } - log.info("Connection test to {} datasource success, connectionParam:{}", type.name(), connectionParam); - return result; + throw new ServiceException(Status.CONNECTION_TEST_FAILURE); } /** @@ -376,51 +336,36 @@ public Result checkConnection(DbType type, ConnectionParam connectionPar * @return connect result code */ @Override - public Result connectionTest(int id) { + public void connectionTest(int id) { DataSource dataSource = dataSourceMapper.selectById(id); if (dataSource == null) { - Result result = new Result<>(); - log.error("Datasource does not exist, datasourceId:{}.", id); - putMsg(result, Status.RESOURCE_NOT_EXIST); - return result; + throw new ServiceException(Status.RESOURCE_NOT_EXIST); } - return checkConnection(dataSource.getType(), + checkConnection(dataSource.getType(), DataSourceUtils.buildConnectionParams(dataSource.getType(), dataSource.getConnectionParams())); } /** * delete datasource * - * @param loginUser login user + * @param loginUser login user * @param datasourceId data source id * @return delete result code */ @Override @Transactional - public Result delete(User loginUser, int datasourceId) { - Result result = new Result<>(); - try { - // query datasource by id - DataSource dataSource = dataSourceMapper.selectById(datasourceId); - if (dataSource == null) { - log.warn("Datasource does not exist, datasourceId:{}.", datasourceId); - putMsg(result, Status.RESOURCE_NOT_EXIST); - return result; - } - if (!canOperatorPermissions(loginUser, new Object[]{dataSource.getId()}, AuthorizationType.DATASOURCE, - DATASOURCE_DELETE)) { - putMsg(result, Status.USER_NO_OPERATION_PERM); - return result; - } - dataSourceMapper.deleteById(datasourceId); - datasourceUserMapper.deleteByDatasourceId(datasourceId); - log.info("Delete datasource complete, datasourceId:{}.", datasourceId); - putMsg(result, Status.SUCCESS); - } catch (Exception e) { - log.error("Delete datasource complete, datasourceId:{}.", datasourceId, e); - throw new ServiceException(Status.DELETE_DATA_SOURCE_FAILURE); + public void delete(User loginUser, int datasourceId) { + // query datasource by id + DataSource dataSource = dataSourceMapper.selectById(datasourceId); + if (dataSource == null) { + throw new ServiceException(Status.RESOURCE_NOT_EXIST); + } + if (!canOperatorPermissions(loginUser, new Object[]{dataSource.getId()}, AuthorizationType.DATASOURCE, + DATASOURCE_DELETE)) { + throw new ServiceException(Status.USER_NO_OPERATION_PERM); } - return result; + dataSourceMapper.deleteById(datasourceId); + datasourceUserMapper.deleteByDatasourceId(datasourceId); } /** diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DqExecuteResultServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DqExecuteResultServiceImpl.java index f61a82f76da00..ec1c7d47d1e4e 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DqExecuteResultServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DqExecuteResultServiceImpl.java @@ -18,9 +18,9 @@ package org.apache.dolphinscheduler.api.service.impl; import org.apache.dolphinscheduler.api.enums.Status; +import org.apache.dolphinscheduler.api.exceptions.ServiceException; import org.apache.dolphinscheduler.api.service.DqExecuteResultService; import org.apache.dolphinscheduler.api.utils.PageInfo; -import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.utils.DateUtils; import org.apache.dolphinscheduler.dao.entity.DqExecuteResult; import org.apache.dolphinscheduler.dao.entity.User; @@ -49,16 +49,15 @@ public class DqExecuteResultServiceImpl extends BaseServiceImpl implements DqExe private DqExecuteResultMapper dqExecuteResultMapper; @Override - public Result queryResultListPaging(User loginUser, - String searchVal, - Integer state, - Integer ruleType, - String startTime, - String endTime, - Integer pageNo, - Integer pageSize) { - - Result result = new Result(); + public PageInfo queryResultListPaging(User loginUser, + String searchVal, + Integer state, + Integer ruleType, + String startTime, + String endTime, + Integer pageNo, + Integer pageSize) { + int[] statusArray = null; // filter by state if (state != null) { @@ -75,9 +74,7 @@ public Result queryResultListPaging(User loginUser, end = DateUtils.stringToDate(endTime); } } catch (Exception e) { - log.warn("Parameter startTime or endTime is invalid."); - putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, "startTime,endTime"); - return result; + throw new ServiceException(Status.REQUEST_PARAMS_NOT_VALID_ERROR, "startTime,endTime"); } Page page = new Page<>(pageNo, pageSize); @@ -99,8 +96,6 @@ public Result queryResultListPaging(User loginUser, pageInfo.setTotal((int) dqsResultPage.getTotal()); pageInfo.setTotalList(dqsResultPage.getRecords()); - result.setData(pageInfo); - putMsg(result, Status.SUCCESS); - return result; + return pageInfo; } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DqRuleServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DqRuleServiceImpl.java index 6aca9b88f8711..1be85998870a9 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DqRuleServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DqRuleServiceImpl.java @@ -18,14 +18,13 @@ package org.apache.dolphinscheduler.api.service.impl; import static org.apache.dolphinscheduler.common.constants.Constants.CHANGE; -import static org.apache.dolphinscheduler.common.constants.Constants.DATA_LIST; import static org.apache.dolphinscheduler.common.constants.Constants.SMALL; import org.apache.dolphinscheduler.api.dto.RuleDefinition; import org.apache.dolphinscheduler.api.enums.Status; +import org.apache.dolphinscheduler.api.exceptions.ServiceException; import org.apache.dolphinscheduler.api.service.DqRuleService; import org.apache.dolphinscheduler.api.utils.PageInfo; -import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.utils.DateUtils; import org.apache.dolphinscheduler.common.utils.JSONUtils; import org.apache.dolphinscheduler.dao.entity.DataSource; @@ -59,9 +58,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Date; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.Objects; import lombok.extern.slf4j.Slf4j; @@ -99,66 +96,44 @@ public class DqRuleServiceImpl extends BaseServiceImpl implements DqRuleService private DqComparisonTypeMapper dqComparisonTypeMapper; @Override - public Map getRuleFormCreateJsonById(int id) { - - Map result = new HashMap<>(); + public String getRuleFormCreateJsonById(int id) { List ruleInputEntryList = dqRuleInputEntryMapper.getRuleInputEntryList(id); - if (ruleInputEntryList == null || ruleInputEntryList.isEmpty()) { - putMsg(result, Status.QUERY_RULE_INPUT_ENTRY_LIST_ERROR); - } else { - result.put(DATA_LIST, getRuleFormCreateJson(DqRuleUtils.transformInputEntry(ruleInputEntryList))); - putMsg(result, Status.SUCCESS); + throw new ServiceException(Status.QUERY_RULE_INPUT_ENTRY_LIST_ERROR); } - - return result; + return getRuleFormCreateJson(DqRuleUtils.transformInputEntry(ruleInputEntryList)); } @Override - public Map queryAllRuleList() { - Map result = new HashMap<>(); - - List ruleList = - dqRuleMapper.selectList(new QueryWrapper<>()); - - result.put(DATA_LIST, ruleList); - putMsg(result, Status.SUCCESS); - - return result; + public List queryAllRuleList() { + return dqRuleMapper.selectList(new QueryWrapper<>()); } @Override - public Map getDatasourceOptionsById(int datasourceId) { - Map result = new HashMap<>(); + public List getDatasourceOptionsById(int datasourceId) { List dataSourceList = dataSourceMapper.listAllDataSourceByType(datasourceId); - List options = null; - if (CollectionUtils.isNotEmpty(dataSourceList)) { - options = new ArrayList<>(); - - for (DataSource dataSource : dataSourceList) { - ParamsOptions childrenOption = - new ParamsOptions(dataSource.getName(), dataSource.getId(), false); - options.add(childrenOption); - } + if (CollectionUtils.isEmpty(dataSourceList)) { + return Collections.emptyList(); } - result.put(DATA_LIST, options); - putMsg(result, Status.SUCCESS); - - return result; + List options = new ArrayList<>(); + for (DataSource dataSource : dataSourceList) { + ParamsOptions childrenOption = new ParamsOptions(dataSource.getName(), dataSource.getId(), false); + options.add(childrenOption); + } + return options; } @Override - public Result queryRuleListPaging(User loginUser, - String searchVal, - Integer ruleType, - String startTime, - String endTime, - Integer pageNo, - Integer pageSize) { - Result result = new Result(); + public PageInfo queryRuleListPaging(User loginUser, + String searchVal, + Integer ruleType, + String startTime, + String endTime, + Integer pageNo, + Integer pageSize) { Date start = null; Date end = null; @@ -170,8 +145,7 @@ public Result queryRuleListPaging(User loginUser, end = DateUtils.stringToDate(endTime); } } catch (Exception e) { - putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, "startTime,endTime"); - return result; + throw new ServiceException(Status.REQUEST_PARAMS_NOT_VALID_ERROR, "startTime,endTime"); } Page page = new Page<>(pageNo, pageSize); @@ -203,9 +177,7 @@ public Result queryRuleListPaging(User loginUser, pageInfo.setTotalList(dataList); } - result.setData(pageInfo); - putMsg(result, Status.SUCCESS); - return result; + return pageInfo; } private String getRuleFormCreateJson(List ruleInputEntryList) { diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/EnvironmentServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/EnvironmentServiceImpl.java index 18de9c5412e37..ade9aea483835 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/EnvironmentServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/EnvironmentServiceImpl.java @@ -23,6 +23,7 @@ import org.apache.dolphinscheduler.api.dto.EnvironmentDto; import org.apache.dolphinscheduler.api.enums.Status; +import org.apache.dolphinscheduler.api.exceptions.ServiceException; import org.apache.dolphinscheduler.api.service.EnvironmentService; import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.Result; @@ -95,28 +96,22 @@ public class EnvironmentServiceImpl extends BaseServiceImpl implements Environme */ @Override @Transactional - public Map createEnvironment(User loginUser, String name, String config, String desc, - String workerGroups) { - Map result = new HashMap<>(); + public Long createEnvironment(User loginUser, + String name, + String config, + String desc, + String workerGroups) { if (!canOperatorPermissions(loginUser, null, AuthorizationType.ENVIRONMENT, ENVIRONMENT_CREATE)) { - putMsg(result, Status.USER_NO_OPERATION_PERM); - return result; + throw new ServiceException(Status.USER_NO_OPERATION_PERM); } if (checkDescriptionLength(desc)) { - log.warn("Parameter description is too long."); - putMsg(result, Status.DESCRIPTION_TOO_LONG_ERROR); - return result; - } - Map checkResult = checkParams(name, config, workerGroups); - if (checkResult.get(Constants.STATUS) != Status.SUCCESS) { - return checkResult; + throw new ServiceException(Status.DESCRIPTION_TOO_LONG_ERROR); } + checkParams(name, config, workerGroups); Environment environment = environmentMapper.queryByEnvironmentName(name); if (environment != null) { - log.warn("Environment with the same name already exist, environmentName:{}.", environment.getName()); - putMsg(result, Status.ENVIRONMENT_NAME_EXISTS, name); - return result; + throw new ServiceException(Status.ENVIRONMENT_NAME_EXISTS, name); } Environment env = new Environment(); @@ -134,8 +129,7 @@ public Map createEnvironment(User loginUser, String name, String log.error("Generate environment code error.", e); } if (code == 0L) { - putMsg(result, Status.INTERNAL_SERVER_ERROR_ARGS, "Error generating environment code"); - return result; + throw new ServiceException(Status.INTERNAL_SERVER_ERROR_ARGS, "Error generating environment code"); } if (environmentMapper.insert(env) > 0) { @@ -159,16 +153,9 @@ public Map createEnvironment(User loginUser, String name, String }); } } - result.put(Constants.DATA_LIST, env.getCode()); - putMsg(result, Status.SUCCESS); - permissionPostHandle(AuthorizationType.ENVIRONMENT, loginUser.getId(), - Collections.singletonList(env.getId()), log); - log.info("Environment create complete, name:{}.", env.getName()); - } else { - log.error("Environment create error, name:{}.", env.getName()); - putMsg(result, Status.CREATE_ENVIRONMENT_ERROR); + return env.getCode(); } - return result; + throw new ServiceException(Status.CREATE_ENVIRONMENT_ERROR); } /** @@ -366,29 +353,20 @@ public Map deleteEnvironmentByCode(User loginUser, Long code) { */ @Transactional @Override - public Map updateEnvironmentByCode(User loginUser, Long code, String name, String config, - String desc, String workerGroups) { - Map result = new HashMap<>(); + public Environment updateEnvironmentByCode(User loginUser, Long code, String name, String config, + String desc, String workerGroups) { if (!canOperatorPermissions(loginUser, null, AuthorizationType.ENVIRONMENT, ENVIRONMENT_UPDATE)) { - putMsg(result, Status.USER_NO_OPERATION_PERM); - return result; + throw new ServiceException(Status.USER_NO_OPERATION_PERM); } - Map checkResult = checkParams(name, config, workerGroups); - if (checkResult.get(Constants.STATUS) != Status.SUCCESS) { - return checkResult; - } + checkParams(name, config, workerGroups); if (checkDescriptionLength(desc)) { - log.warn("Parameter description is too long."); - putMsg(result, Status.DESCRIPTION_TOO_LONG_ERROR); - return result; + throw new ServiceException(Status.DESCRIPTION_TOO_LONG_ERROR); } Environment environment = environmentMapper.queryByEnvironmentName(name); if (environment != null && !environment.getCode().equals(code)) { - log.warn("Environment with the same name already exist, name:{}.", environment.getName()); - putMsg(result, Status.ENVIRONMENT_NAME_EXISTS, name); - return result; + throw new ServiceException(Status.ENVIRONMENT_NAME_EXISTS, name); } Set workerGroupSet; @@ -402,17 +380,14 @@ public Map updateEnvironmentByCode(User loginUser, Long code, St Set existWorkerGroupSet = relationMapper .queryByEnvironmentCode(code) .stream() - .map(item -> item.getWorkerGroup()) + .map(EnvironmentWorkerGroupRelation::getWorkerGroup) .collect(Collectors.toSet()); Set deleteWorkerGroupSet = SetUtils.difference(existWorkerGroupSet, workerGroupSet).toSet(); Set addWorkerGroupSet = SetUtils.difference(workerGroupSet, existWorkerGroupSet).toSet(); // verify whether the relation of this environment and worker groups can be adjusted - checkResult = checkUsedEnvironmentWorkerGroupRelation(deleteWorkerGroupSet, name, code); - if (checkResult.get(Constants.STATUS) != Status.SUCCESS) { - return checkResult; - } + checkUsedEnvironmentWorkerGroupRelation(deleteWorkerGroupSet, name, code); Environment env = new Environment(); env.setCode(code); @@ -424,33 +399,29 @@ public Map updateEnvironmentByCode(User loginUser, Long code, St int update = environmentMapper.update(env, new UpdateWrapper().lambda().eq(Environment::getCode, code)); - if (update > 0) { - deleteWorkerGroupSet.stream().forEach(key -> { - if (StringUtils.isNotEmpty(key)) { - relationMapper.delete(new QueryWrapper() - .lambda() - .eq(EnvironmentWorkerGroupRelation::getEnvironmentCode, code) - .eq(EnvironmentWorkerGroupRelation::getWorkerGroup, key)); - } - }); - addWorkerGroupSet.stream().forEach(key -> { - if (StringUtils.isNotEmpty(key)) { - EnvironmentWorkerGroupRelation relation = new EnvironmentWorkerGroupRelation(); - relation.setEnvironmentCode(code); - relation.setWorkerGroup(key); - relation.setUpdateTime(new Date()); - relation.setCreateTime(new Date()); - relation.setOperator(loginUser.getId()); - relationMapper.insert(relation); - } - }); - log.info("Environment and relations update complete, environmentId:{}.", env.getId()); - putMsg(result, Status.SUCCESS); - } else { - log.error("Environment update error, environmentId:{}.", env.getId()); - putMsg(result, Status.UPDATE_ENVIRONMENT_ERROR, name); + if (update <= 0) { + throw new ServiceException(Status.UPDATE_ENVIRONMENT_ERROR, name); } - return result; + deleteWorkerGroupSet.forEach(key -> { + if (StringUtils.isNotEmpty(key)) { + relationMapper.delete(new QueryWrapper() + .lambda() + .eq(EnvironmentWorkerGroupRelation::getEnvironmentCode, code) + .eq(EnvironmentWorkerGroupRelation::getWorkerGroup, key)); + } + }); + addWorkerGroupSet.forEach(key -> { + if (StringUtils.isNotEmpty(key)) { + EnvironmentWorkerGroupRelation relation = new EnvironmentWorkerGroupRelation(); + relation.setEnvironmentCode(code); + relation.setWorkerGroup(key); + relation.setUpdateTime(new Date()); + relation.setCreateTime(new Date()); + relation.setOperator(loginUser.getId()); + relationMapper.insert(relation); + } + }); + return env; } /** @@ -480,9 +451,8 @@ public Map verifyEnvironment(String environmentName) { return result; } - private Map checkUsedEnvironmentWorkerGroupRelation(Set deleteKeySet, - String environmentName, Long environmentCode) { - Map result = new HashMap<>(); + private void checkUsedEnvironmentWorkerGroupRelation(Set deleteKeySet, + String environmentName, Long environmentCode) { for (String workerGroup : deleteKeySet) { List taskDefinitionList = taskDefinitionMapper .selectList(new QueryWrapper().lambda() @@ -492,41 +462,26 @@ private Map checkUsedEnvironmentWorkerGroupRelation(Set if (Objects.nonNull(taskDefinitionList) && taskDefinitionList.size() != 0) { Set collect = taskDefinitionList.stream().map(TaskDefinition::getName).collect(Collectors.toSet()); - log.warn("Environment {} and worker group {} is being used by task {}, so can not update.", - taskDefinitionList.get(0).getEnvironmentCode(), taskDefinitionList.get(0).getWorkerGroup(), - collect); - putMsg(result, Status.UPDATE_ENVIRONMENT_WORKER_GROUP_RELATION_ERROR, workerGroup, environmentName, - collect); - return result; + throw new ServiceException(Status.UPDATE_ENVIRONMENT_WORKER_GROUP_RELATION_ERROR, workerGroup, + environmentName, collect); } } - result.put(Constants.STATUS, Status.SUCCESS); - return result; } - public Map checkParams(String name, String config, String workerGroups) { - Map result = new HashMap<>(); + protected void checkParams(String name, String config, String workerGroups) { if (StringUtils.isEmpty(name)) { - log.warn("parameter environment name is empty."); - putMsg(result, Status.ENVIRONMENT_NAME_IS_NULL); - return result; + throw new ServiceException(Status.ENVIRONMENT_NAME_IS_NULL); } if (StringUtils.isEmpty(config)) { - log.warn("parameter environment config is empty."); - putMsg(result, Status.ENVIRONMENT_CONFIG_IS_NULL); - return result; + throw new ServiceException(Status.ENVIRONMENT_CONFIG_IS_NULL); } if (!StringUtils.isEmpty(workerGroups)) { List workerGroupList = JSONUtils.parseObject(workerGroups, new TypeReference>() { }); if (Objects.isNull(workerGroupList)) { - log.warn("Parameter worker groups list is invalid."); - putMsg(result, Status.ENVIRONMENT_WORKER_GROUPS_IS_INVALID); - return result; + throw new ServiceException(Status.ENVIRONMENT_WORKER_GROUPS_IS_INVALID); } } - result.put(Constants.STATUS, Status.SUCCESS); - return result; } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ExecutorServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ExecutorServiceImpl.java index dc5e7611909f5..53530d64196ec 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ExecutorServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ExecutorServiceImpl.java @@ -227,11 +227,9 @@ public Map execProcessInstance(User loginUser, long projectCode, boolean allLevelDependent, ExecutionOrder executionOrder) { Project project = projectMapper.queryByCode(projectCode); // check user access for project - Map result = - projectService.checkProjectAndAuth(loginUser, project, projectCode, WORKFLOW_START); - if (result.get(Constants.STATUS) != Status.SUCCESS) { - return result; - } + projectService.checkProjectAndAuth(loginUser, project, projectCode, WORKFLOW_START); + + Map result = new HashMap<>(); // timeout is invalid if (timeout <= 0 || timeout > MAX_TASK_TIMEOUT) { log.warn("Parameter timeout is invalid, timeout:{}.", timeout); @@ -464,10 +462,8 @@ public WorkflowExecuteResponse executeTask(User loginUser, long projectCode, Int WorkflowExecuteResponse response = new WorkflowExecuteResponse(); - Project project = projectMapper.queryByCode(projectCode); // check user access for project - - projectService.checkProjectAndAuthThrowException(loginUser, project, + projectService.checkProjectAndAuthThrowException(loginUser, projectCode, ApiFuncIdentificationConstant.map.get(ExecuteType.EXECUTE_TASK)); ProcessInstance processInstance = processService.findProcessInstanceDetailById(processInstanceId) @@ -1158,18 +1154,15 @@ public WorkflowExecuteDto queryExecutingWorkflowByProcessInstanceId(Integer proc } @Override - public Map execStreamTaskInstance(User loginUser, long projectCode, long taskDefinitionCode, - int taskDefinitionVersion, - int warningGroupId, String workerGroup, String tenantCode, - Long environmentCode, - Map startParams, int dryRun) { + public void execStreamTaskInstance(User loginUser, long projectCode, long taskDefinitionCode, + int taskDefinitionVersion, + int warningGroupId, String workerGroup, String tenantCode, + Long environmentCode, + Map startParams, int dryRun) { Project project = projectMapper.queryByCode(projectCode); // check user access for project - Map result = - projectService.checkProjectAndAuth(loginUser, project, projectCode, WORKFLOW_START); - if (result.get(Constants.STATUS) != Status.SUCCESS) { - return result; - } + projectService.checkProjectAndAuth(loginUser, project, projectCode, WORKFLOW_START); + checkValidTenant(tenantCode); checkMasterExists(); // todo dispatch improvement @@ -1195,13 +1188,11 @@ public Map execStreamTaskInstance(User loginUser, long projectCo streamingTaskOperator.triggerStreamingTask(taskExecuteStartMessage); if (streamingTaskTriggerResponse.isSuccess()) { log.info("Send task execute start command complete, response is {}.", streamingTaskOperator); - putMsg(result, Status.SUCCESS); - } else { - log.error( - "Start to execute stream task instance error, projectCode:{}, taskDefinitionCode:{}, taskVersion:{}, response: {}.", - projectCode, taskDefinitionCode, taskDefinitionVersion, streamingTaskTriggerResponse); - putMsg(result, Status.START_TASK_INSTANCE_ERROR); + return; } - return result; + log.error( + "Start to execute stream task instance error, projectCode:{}, taskDefinitionCode:{}, taskVersion:{}, response: {}.", + projectCode, taskDefinitionCode, taskDefinitionVersion, streamingTaskTriggerResponse); + throw new ServiceException(Status.START_TASK_INSTANCE_ERROR); } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/K8SNamespaceServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/K8SNamespaceServiceImpl.java index 4d25725761cc6..f05e69b9db3b2 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/K8SNamespaceServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/K8SNamespaceServiceImpl.java @@ -18,6 +18,7 @@ package org.apache.dolphinscheduler.api.service.impl; import org.apache.dolphinscheduler.api.enums.Status; +import org.apache.dolphinscheduler.api.exceptions.ServiceException; import org.apache.dolphinscheduler.api.k8s.K8sClientService; import org.apache.dolphinscheduler.api.service.K8sNamespaceService; import org.apache.dolphinscheduler.api.utils.PageInfo; @@ -109,9 +110,8 @@ public Result queryListPaging(User loginUser, String searchVal, Integer pageNo, @Override public Map createK8sNamespace(User loginUser, String namespace, Long clusterCode) { Map result = new HashMap<>(); - if (isNotAdmin(loginUser, result)) { - log.warn("Only admin can create K8s namespace, current login user name:{}.", loginUser.getUserName()); - return result; + if (isNotAdmin(loginUser)) { + throw new ServiceException(Status.USER_NO_OPERATION_PERM); } if (StringUtils.isEmpty(namespace)) { @@ -220,9 +220,8 @@ public Result verifyNamespaceK8s(String namespace, Long clusterCode) { @Override public Map deleteNamespaceById(User loginUser, int id) { Map result = new HashMap<>(); - if (isNotAdmin(loginUser, result)) { - log.warn("Only admin can delete K8s namespace, current login user name:{}.", loginUser.getUserName()); - return result; + if (isNotAdmin(loginUser)) { + throw new ServiceException(Status.USER_NO_OPERATION_PERM); } K8sNamespace k8sNamespaceObj = k8sNamespaceMapper.selectById(id); @@ -258,8 +257,8 @@ private boolean checkNamespaceExistInDb(String namespace, Long clusterCode) { @Override public Map queryUnauthorizedNamespace(User loginUser, Integer userId) { Map result = new HashMap<>(); - if (loginUser.getId() != userId && isNotAdmin(loginUser, result)) { - return result; + if (loginUser.getId() != userId && isNotAdmin(loginUser)) { + throw new ServiceException(Status.USER_NO_OPERATION_PERM); } // query all namespace list, this auth does not like project List namespaceList = k8sNamespaceMapper.selectList(null); @@ -286,8 +285,8 @@ public Map queryUnauthorizedNamespace(User loginUser, Integer us public Map queryAuthorizedNamespace(User loginUser, Integer userId) { Map result = new HashMap<>(); - if (loginUser.getId() != userId && isNotAdmin(loginUser, result)) { - return result; + if (loginUser.getId() != userId && isNotAdmin(loginUser)) { + throw new ServiceException(Status.USER_NO_OPERATION_PERM); } List namespaces = k8sNamespaceMapper.queryAuthedNamespaceListByUserId(userId); diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/LoggerServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/LoggerServiceImpl.java index ab4929648ab66..24dc41ce3a0eb 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/LoggerServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/LoggerServiceImpl.java @@ -56,7 +56,6 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.List; -import java.util.Map; import lombok.extern.slf4j.Slf4j; @@ -78,13 +77,13 @@ public class LoggerServiceImpl extends BaseServiceImpl implements LoggerService private TaskInstanceDao taskInstanceDao; @Autowired - ProjectMapper projectMapper; + private ProjectMapper projectMapper; @Autowired - ProjectService projectService; + private ProjectService projectService; @Autowired - TaskDefinitionMapper taskDefinitionMapper; + private TaskDefinitionMapper taskDefinitionMapper; /** * view log @@ -148,28 +147,20 @@ public byte[] getLogBytes(User loginUser, int taskInstId) { */ @Override @SuppressWarnings("unchecked") - public Map queryLog(User loginUser, long projectCode, int taskInstId, int skipLineNum, int limit) { - Project project = projectMapper.queryByCode(projectCode); + public String queryLog(User loginUser, long projectCode, int taskInstId, int skipLineNum, int limit) { // check user access for project - Map result = projectService.checkProjectAndAuth(loginUser, project, projectCode, VIEW_LOG); - if (result.get(Constants.STATUS) != Status.SUCCESS) { - return result; - } + projectService.checkProjectAndAuthThrowException(loginUser, projectCode, VIEW_LOG); // check whether the task instance can be found TaskInstance task = taskInstanceDao.queryById(taskInstId); if (task == null || StringUtils.isBlank(task.getHost())) { - putMsg(result, Status.TASK_INSTANCE_NOT_FOUND); - return result; + throw new ServiceException(Status.TASK_INSTANCE_NOT_FOUND); } TaskDefinition taskDefinition = taskDefinitionMapper.queryByCode(task.getTaskCode()); if (taskDefinition != null && projectCode != taskDefinition.getProjectCode()) { - putMsg(result, Status.TASK_INSTANCE_NOT_FOUND, taskInstId); - return result; + throw new ServiceException(Status.TASK_INSTANCE_NOT_FOUND, taskInstId); } - String log = queryLog(task, skipLineNum, limit); - result.put(Constants.DATA_LIST, log); - return result; + return queryLog(task, skipLineNum, limit); } /** @@ -182,12 +173,9 @@ public Map queryLog(User loginUser, long projectCode, int taskIn */ @Override public byte[] getLogBytes(User loginUser, long projectCode, int taskInstId) { - Project project = projectMapper.queryByCode(projectCode); // check user access for project - Map result = projectService.checkProjectAndAuth(loginUser, project, projectCode, DOWNLOAD_LOG); - if (result.get(Constants.STATUS) != Status.SUCCESS) { - throw new ServiceException("user has no permission"); - } + projectService.checkProjectAndAuthThrowException(loginUser, projectCode, DOWNLOAD_LOG); + // check whether the task instance can be found TaskInstance task = taskInstanceDao.queryById(taskInstId); if (task == null || StringUtils.isBlank(task.getHost())) { diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/MonitorServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/MonitorServiceImpl.java index 5ab42d30890de..907ab2329c88f 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/MonitorServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/MonitorServiceImpl.java @@ -17,20 +17,16 @@ package org.apache.dolphinscheduler.api.service.impl; -import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.service.MonitorService; -import org.apache.dolphinscheduler.common.constants.Constants; import org.apache.dolphinscheduler.common.model.Server; import org.apache.dolphinscheduler.common.model.WorkerServerModel; import org.apache.dolphinscheduler.dao.entity.User; +import org.apache.dolphinscheduler.dao.plugin.api.monitor.DatabaseMetrics; import org.apache.dolphinscheduler.dao.plugin.api.monitor.DatabaseMonitor; import org.apache.dolphinscheduler.registry.api.RegistryClient; import org.apache.dolphinscheduler.registry.api.enums.RegistryNodeType; -import java.util.HashMap; import java.util.List; -import java.util.Map; -import java.util.function.Function; import java.util.stream.Collectors; import lombok.extern.slf4j.Slf4j; @@ -61,11 +57,8 @@ public class MonitorServiceImpl extends BaseServiceImpl implements MonitorServic * @return data base state */ @Override - public Map queryDatabaseState(User loginUser) { - Map result = new HashMap<>(); - result.put(Constants.DATA_LIST, Lists.newArrayList(databaseMonitor.getDatabaseMetrics())); - putMsg(result, Status.SUCCESS); - return result; + public List queryDatabaseState(User loginUser) { + return Lists.newArrayList(databaseMonitor.getDatabaseMetrics()); } /** @@ -75,13 +68,8 @@ public Map queryDatabaseState(User loginUser) { * @return master information list */ @Override - public Map queryMaster(User loginUser) { - Map result = new HashMap<>(); - List masterServers = getServerListFromRegistry(true); - result.put(Constants.DATA_LIST, masterServers); - putMsg(result, Status.SUCCESS); - - return result; + public List queryMaster(User loginUser) { + return registryClient.getServerList(RegistryNodeType.MASTER); } /** @@ -91,10 +79,9 @@ public Map queryMaster(User loginUser) { * @return worker information list */ @Override - public Map queryWorker(User loginUser) { + public List queryWorker(User loginUser) { - Map result = new HashMap<>(); - List workerServers = getServerListFromRegistry(false) + return registryClient.getServerList(RegistryNodeType.WORKER) .stream() .map((Server server) -> { WorkerServerModel model = new WorkerServerModel(); @@ -109,21 +96,6 @@ public Map queryWorker(User loginUser) { }) .collect(Collectors.toList()); - Map workerHostPortServerMapping = workerServers - .stream() - .collect(Collectors.toMap( - (WorkerServerModel worker) -> { - String[] s = worker.getZkDirectories().iterator().next().split("/"); - return s[s.length - 1]; - }, Function.identity(), (WorkerServerModel oldOne, WorkerServerModel newOne) -> { - oldOne.getZkDirectories().addAll(newOne.getZkDirectories()); - return oldOne; - })); - - result.put(Constants.DATA_LIST, workerHostPortServerMapping.values()); - putMsg(result, Status.SUCCESS); - - return result; } @Override diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProjectServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProjectServiceImpl.java index 48846461733a7..0f57e557226fa 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProjectServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProjectServiceImpl.java @@ -43,7 +43,6 @@ import org.apache.commons.lang3.StringUtils; -import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Date; @@ -142,8 +141,6 @@ public Result createProject(User loginUser, String name, String desc) { log.info("Project is created and id is :{}", project.getId()); result.setData(project); putMsg(result, Status.SUCCESS); - permissionPostHandle(AuthorizationType.PROJECTS, loginUser.getId(), - Collections.singletonList(project.getId()), log); } else { log.error("Project create error, projectName:{}.", project.getName()); putMsg(result, Status.CREATE_PROJECT_ERROR); @@ -159,9 +156,8 @@ public Result createProject(User loginUser, String name, String desc) { */ public static void checkDesc(Result result, String desc) { if (!StringUtils.isEmpty(desc) && desc.codePointCount(0, desc.length()) > 255) { - log.warn("Parameter description check failed."); - result.setCode(Status.REQUEST_PARAMS_NOT_VALID_ERROR.getCode()); - result.setMsg(MessageFormat.format(Status.REQUEST_PARAMS_NOT_VALID_ERROR.getMsg(), "desc length")); + result.setCode(Status.DESCRIPTION_TOO_LONG_ERROR.getCode()); + result.setMsg(Status.DESCRIPTION_TOO_LONG_ERROR.getMsg()); } else { result.setCode(Status.SUCCESS.getCode()); } @@ -319,6 +315,32 @@ public boolean hasProjectAndWritePerm(User loginUser, Project project, Map queryList(User loginUser) { Set ids = resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.QUEUE, loginUser.getId(), log); if (loginUser.getUserType().equals(UserType.GENERAL_USER)) { ids = ids.isEmpty() ? new HashSet<>() : ids; ids.add(Constants.DEFAULT_QUEUE_ID); } - List queueList = queueMapper.selectBatchIds(ids); - result.setData(queueList); - putMsg(result, Status.SUCCESS); - return result; + return queueMapper.selectBatchIds(ids); } /** * query queue list paging * * @param loginUser login user - * @param pageNo page number + * @param pageNo page number * @param searchVal search value - * @param pageSize page size + * @param pageSize page size * @return queue list */ @Override - public Result queryList(User loginUser, String searchVal, Integer pageNo, Integer pageSize) { - Result result = new Result(); + public PageInfo queryList(User loginUser, String searchVal, Integer pageNo, Integer pageSize) { PageInfo pageInfo = new PageInfo<>(pageNo, pageSize); Set ids = resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.QUEUE, loginUser.getId(), log); if (ids.isEmpty()) { - result.setData(pageInfo); - putMsg(result, Status.SUCCESS); - return result; + return pageInfo; } Page page = new Page<>(pageNo, pageSize); IPage queueList = queueMapper.queryQueuePaging(page, new ArrayList<>(ids), searchVal); Integer count = (int) queueList.getTotal(); pageInfo.setTotal(count); pageInfo.setTotalList(queueList.getRecords()); - result.setData(pageInfo); - putMsg(result, Status.SUCCESS); - - return result; + return pageInfo; } /** * create queue * * @param loginUser login user - * @param queue queue + * @param queue queue * @param queueName queue name * @return create result */ @Override - @Transactional - public Result createQueue(User loginUser, String queue, String queueName) { - Result result = new Result(); + public Queue createQueue(User loginUser, String queue, String queueName) { if (!canOperatorPermissions(loginUser, null, AuthorizationType.QUEUE, YARN_QUEUE_CREATE)) { throw new ServiceException(Status.USER_NO_OPERATION_PERM); } Queue queueObj = new Queue(queueName, queue); - createQueueValid(queueObj); + validQueue(queueObj); queueMapper.insert(queueObj); - result.setData(queueObj); - log.info("Queue create complete, queueName:{}.", queueObj.getQueueName()); - putMsg(result, Status.SUCCESS); - permissionPostHandle(AuthorizationType.QUEUE, loginUser.getId(), Collections.singletonList(queueObj.getId()), - log); - return result; + return queueObj; } /** * update queue * * @param loginUser login user - * @param queue queue - * @param id queue id + * @param queue queue + * @param id queue id * @param queueName queue name * @return update result code */ @Override - public Result updateQueue(User loginUser, int id, String queue, String queueName) { - Result result = new Result(); + public Queue updateQueue(User loginUser, int id, String queue, String queueName) { if (!canOperatorPermissions(loginUser, new Object[]{id}, AuthorizationType.QUEUE, YARN_QUEUE_UPDATE)) { throw new ServiceException(Status.USER_NO_OPERATION_PERM); } @@ -227,23 +204,19 @@ public Result updateQueue(User loginUser, int id, String queue, String queueName } queueMapper.updateById(updateQueue); - result.setData(updateQueue); - putMsg(result, Status.SUCCESS); - return result; + return updateQueue; } /** * delete queue * * @param loginUser login user - * @param id queue id + * @param id queue id * @return delete result code * @throws Exception exception */ @Override - @Transactional(rollbackFor = Exception.class) - public Map deleteQueueById(User loginUser, int id) throws Exception { - Map result = new HashMap<>(); + public void deleteQueueById(User loginUser, int id) throws Exception { if (!canOperatorPermissions(loginUser, null, AuthorizationType.TENANT, TENANT_DELETE)) { throw new ServiceException(Status.USER_NO_OPERATION_PERM); @@ -268,15 +241,10 @@ public Map deleteQueueById(User loginUser, int id) throws Except } int delete = queueMapper.deleteById(id); - if (delete > 0) { - log.info("Queue is deleted and id is {}.", id); - putMsg(result, Status.SUCCESS); - } else { - log.error("Queue delete failed, queueId:{}.", id); - putMsg(result, Status.DELETE_QUEUE_BY_ID_ERROR); + if (delete <= 0) { + throw new ServiceException(Status.DELETE_QUEUE_BY_ID_ERROR); } - return result; } /** @@ -287,14 +255,9 @@ public Map deleteQueueById(User loginUser, int id) throws Except * @return true if the queue name not exists, otherwise return false */ @Override - public Result verifyQueue(String queue, String queueName) { - Result result = new Result<>(); - + public void verifyQueue(String queue, String queueName) { Queue queueValidator = new Queue(queueName, queue); - createQueueValid(queueValidator); - result.setData(queueValidator); - putMsg(result, Status.SUCCESS); - return result; + validQueue(queueValidator); } /** @@ -349,7 +312,7 @@ public Queue createQueueIfNotExists(String queue, String queueName) { return existsQueue; } Queue queueObj = new Queue(queueName, queue); - createQueueValid(queueObj); + validQueue(queueObj); queueMapper.insert(queueObj); log.info("Queue create complete, queueName:{}.", queueObj.getQueueName()); return queueObj; diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java index d06829bc7e05a..0a8e21071fe32 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java @@ -1218,40 +1218,6 @@ public StorageEntity createOrUpdateResource(String userName, String filepath, return storageOperate.getFileStatus(fullName, defaultPath, user.getTenantCode(), ResourceType.FILE); } - private void permissionPostHandle(ResourceType resourceType, User loginUser, Integer resourceId) { - AuthorizationType authorizationType = - resourceType.equals(ResourceType.FILE) ? AuthorizationType.RESOURCE_FILE_ID - : AuthorizationType.UDF_FILE; - permissionPostHandle(authorizationType, loginUser.getId(), Collections.singletonList(resourceId), log); - } - - private Result verifyResource(User loginUser, ResourceType type, String fullName, int pid) { - Result result = verifyResourceName(fullName, type, loginUser); - if (!result.getCode().equals(Status.SUCCESS.getCode())) { - return result; - } - return verifyPid(loginUser, pid); - } - - private Result verifyPid(User loginUser, int pid) { - Result result = new Result<>(); - putMsg(result, Status.SUCCESS); - if (pid != -1) { - Resource parentResource = resourcesMapper.selectById(pid); - if (parentResource == null) { - log.error("Parent resource does not exist, parentResourceId:{}.", pid); - putMsg(result, Status.PARENT_RESOURCE_NOT_EXIST); - return result; - } - if (!canOperator(loginUser, parentResource.getUserId())) { - log.warn("User does not have operation privilege, loginUserName:{}.", loginUser.getUserName()); - putMsg(result, Status.USER_NO_OPERATION_PERM); - return result; - } - } - return result; - } - /** * updateProcessInstance resource * diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/TaskGroupServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/TaskGroupServiceImpl.java index e65433201029c..3122d090e2d49 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/TaskGroupServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/TaskGroupServiceImpl.java @@ -39,7 +39,6 @@ import org.apache.commons.lang3.StringUtils; import java.util.ArrayList; -import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.List; @@ -129,8 +128,6 @@ public Map createTaskGroup(User loginUser, Long projectCode, Str .build(); if (taskGroupMapper.insert(taskGroup) > 0) { - permissionPostHandle(AuthorizationType.TASK_GROUP, loginUser.getId(), - Collections.singletonList(taskGroup.getId()), log); log.info("Create task group complete, taskGroupName:{}.", taskGroup.getName()); putMsg(result, Status.SUCCESS); } else { diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/TenantServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/TenantServiceImpl.java index fc5fc73159cfb..e77baed1d3d1d 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/TenantServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/TenantServiceImpl.java @@ -28,7 +28,6 @@ import org.apache.dolphinscheduler.api.service.TenantService; import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.RegexUtils; -import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.constants.Constants; import org.apache.dolphinscheduler.common.enums.AuthorizationType; import org.apache.dolphinscheduler.dao.entity.ProcessInstance; @@ -141,30 +140,22 @@ && checkTenantExists(updateTenant.getTenantCode())) { */ @Override @Transactional(rollbackFor = Exception.class) - public Map createTenant(User loginUser, - String tenantCode, - int queueId, - String desc) throws Exception { - Map result = new HashMap<>(); - result.put(Constants.STATUS, false); + public Tenant createTenant(User loginUser, + String tenantCode, + int queueId, + String desc) throws Exception { if (!canOperatorPermissions(loginUser, null, AuthorizationType.TENANT, TENANT_CREATE)) { throw new ServiceException(Status.USER_NO_OPERATION_PERM); } if (checkDescriptionLength(desc)) { - log.warn("Parameter description is too long."); - putMsg(result, Status.DESCRIPTION_TOO_LONG_ERROR); - return result; + throw new ServiceException(Status.DESCRIPTION_TOO_LONG_ERROR); } Tenant tenant = new Tenant(tenantCode, desc, queueId); createTenantValid(tenant); tenantMapper.insert(tenant); storageOperate.createTenantDirIfNotExists(tenantCode); - permissionPostHandle(AuthorizationType.TENANT, loginUser.getId(), Collections.singletonList(tenant.getId()), - log); - result.put(Constants.DATA_LIST, tenant); - putMsg(result, Status.SUCCESS); - return result; + return tenant; } /** @@ -177,25 +168,16 @@ public Map createTenant(User loginUser, * @return tenant list page */ @Override - public Result queryTenantList(User loginUser, String searchVal, Integer pageNo, Integer pageSize) { + public PageInfo queryTenantList(User loginUser, String searchVal, Integer pageNo, Integer pageSize) { - Result result = new Result<>(); - PageInfo pageInfo = new PageInfo<>(pageNo, pageSize); Set ids = resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.TENANT, loginUser.getId(), log); - if (ids.isEmpty()) { - result.setData(pageInfo); - putMsg(result, Status.SUCCESS); - return result; + if (CollectionUtils.isEmpty(ids)) { + return new PageInfo<>(pageNo, pageSize); } Page page = new Page<>(pageNo, pageSize); IPage tenantPage = tenantMapper.queryTenantPaging(page, new ArrayList<>(ids), searchVal); - - pageInfo.setTotal((int) tenantPage.getTotal()); - pageInfo.setTotalList(tenantPage.getRecords()); - result.setData(pageInfo); - putMsg(result, Status.SUCCESS); - return result; + return PageInfo.of(tenantPage); } /** @@ -210,51 +192,45 @@ public Result queryTenantList(User loginUser, String searchVal, Integer * @throws Exception exception */ @Override - public Map updateTenant(User loginUser, int id, String tenantCode, int queueId, - String desc) throws Exception { - - Map result = new HashMap<>(); + public void updateTenant(User loginUser, + int id, + String tenantCode, + int queueId, + String desc) throws Exception { if (!canOperatorPermissions(loginUser, null, AuthorizationType.TENANT, TENANT_UPDATE)) { throw new ServiceException(Status.USER_NO_OPERATION_PERM); } if (checkDescriptionLength(desc)) { - log.warn("Parameter description is too long."); - putMsg(result, Status.DESCRIPTION_TOO_LONG_ERROR); - return result; + throw new ServiceException(Status.DESCRIPTION_TOO_LONG_ERROR); } Tenant updateTenant = new Tenant(id, tenantCode, desc, queueId); Tenant existsTenant = tenantMapper.queryById(id); updateTenantValid(existsTenant, updateTenant); + updateTenant.setCreateTime(existsTenant.getCreateTime()); // updateProcessInstance tenant // if the tenant code is modified, the original resource needs to be copied to the new tenant. if (!Objects.equals(existsTenant.getTenantCode(), updateTenant.getTenantCode())) { storageOperate.createTenantDirIfNotExists(tenantCode); } int update = tenantMapper.updateById(updateTenant); - if (update > 0) { - log.info("Tenant is updated and id is {}.", updateTenant.getId()); - putMsg(result, Status.SUCCESS); - } else { - log.error("Tenant update error, id:{}.", updateTenant.getId()); - putMsg(result, Status.UPDATE_TENANT_ERROR); + if (update <= 0) { + throw new ServiceException(Status.UPDATE_TENANT_ERROR); } - return result; } /** * delete tenant * * @param loginUser login user - * @param id tenant id + * @param id tenant id * @return delete result code * @throws Exception exception */ @Override - @Transactional(rollbackFor = Exception.class) - public Map deleteTenantById(User loginUser, int id) throws Exception { - Map result = new HashMap<>(); + @Transactional() + public void deleteTenantById(User loginUser, int id) throws Exception { if (!canOperatorPermissions(loginUser, null, AuthorizationType.TENANT, TENANT_DELETE)) { throw new ServiceException(Status.USER_NO_OPERATION_PERM); @@ -262,44 +238,31 @@ public Map deleteTenantById(User loginUser, int id) throws Excep Tenant tenant = tenantMapper.queryById(id); if (Objects.isNull(tenant)) { - log.error("Tenant does not exist, userId:{}.", id); throw new ServiceException(Status.TENANT_NOT_EXIST); } List processInstances = getProcessInstancesByTenant(tenant); if (CollectionUtils.isNotEmpty(processInstances)) { - log.warn("Delete tenant failed, because there are {} executing process instances using it.", - processInstances.size()); throw new ServiceException(Status.DELETE_TENANT_BY_ID_FAIL, processInstances.size()); } - List schedules = - scheduleMapper.queryScheduleListByTenant(tenant.getTenantCode()); + List schedules = scheduleMapper.queryScheduleListByTenant(tenant.getTenantCode()); if (CollectionUtils.isNotEmpty(schedules)) { - log.warn("Delete tenant failed, because there are {} schedule using it.", - schedules.size()); throw new ServiceException(Status.DELETE_TENANT_BY_ID_FAIL_DEFINES, schedules.size()); } List userList = userMapper.queryUserListByTenant(tenant.getId()); if (CollectionUtils.isNotEmpty(userList)) { - log.warn("Delete tenant failed, because there are {} users using it.", userList.size()); throw new ServiceException(Status.DELETE_TENANT_BY_ID_FAIL_USERS, userList.size()); } - storageOperate.deleteTenant(tenant.getTenantCode()); - int delete = tenantMapper.deleteById(id); - if (delete > 0) { - processInstanceMapper.updateProcessInstanceByTenantCode(tenant.getTenantCode(), Constants.DEFAULT); - log.info("Tenant is deleted and id is {}.", id); - putMsg(result, Status.SUCCESS); - } else { - log.error("Tenant delete failed, tenantId:{}.", id); - putMsg(result, Status.DELETE_TENANT_BY_ID_ERROR); + if (delete <= 0) { + throw new ServiceException(Status.DELETE_TENANT_BY_ID_ERROR); } - return result; + processInstanceMapper.updateProcessInstanceByTenantCode(tenant.getTenantCode(), Constants.DEFAULT); + storageOperate.deleteTenant(tenant.getTenantCode()); } private List getProcessInstancesByTenant(Tenant tenant) { @@ -314,20 +277,14 @@ private List getProcessInstancesByTenant(Tenant tenant) { * @return tenant list */ @Override - public Map queryTenantList(User loginUser) { + public List queryTenantList(User loginUser) { - Map result = new HashMap<>(); Set ids = resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.TENANT, loginUser.getId(), log); - if (ids.isEmpty()) { - result.put(Constants.DATA_LIST, Collections.emptyList()); - putMsg(result, Status.SUCCESS); - return result; + if (CollectionUtils.isEmpty(ids)) { + return Collections.emptyList(); } - List resourceList = tenantMapper.selectBatchIds(ids); - result.put(Constants.DATA_LIST, resourceList); - putMsg(result, Status.SUCCESS); - return result; + return tenantMapper.selectBatchIds(ids); } /** @@ -337,13 +294,10 @@ public Map queryTenantList(User loginUser) { * @return true if tenant code can use, otherwise return false */ @Override - public Result verifyTenantCode(String tenantCode) { - Result result = new Result<>(); + public void verifyTenantCode(String tenantCode) { if (checkTenantExists(tenantCode)) { throw new ServiceException(Status.OS_TENANT_CODE_EXIST, tenantCode); } - putMsg(result, Status.SUCCESS); - return result; } /** diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UdfFuncServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UdfFuncServiceImpl.java index 2bd9f3845b62b..05f8f6a20ed2b 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UdfFuncServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UdfFuncServiceImpl.java @@ -148,7 +148,6 @@ public Result createUdfFunction(User loginUser, udfFuncMapper.insert(udf); log.info("UDF function create complete, udfFuncName:{}.", udf.getFuncName()); putMsg(result, Status.SUCCESS); - permissionPostHandle(AuthorizationType.UDF, loginUser.getId(), Collections.singletonList(udf.getId()), log); return result; } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkFlowLineageServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkFlowLineageServiceImpl.java index 7966b7f3917c7..15f39d696f9ac 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkFlowLineageServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkFlowLineageServiceImpl.java @@ -20,6 +20,7 @@ import static org.apache.dolphinscheduler.plugin.task.api.TaskConstants.TASK_TYPE_DEPENDENT; import org.apache.dolphinscheduler.api.enums.Status; +import org.apache.dolphinscheduler.api.exceptions.ServiceException; import org.apache.dolphinscheduler.api.service.WorkFlowLineageService; import org.apache.dolphinscheduler.common.constants.Constants; import org.apache.dolphinscheduler.common.utils.JSONUtils; @@ -78,39 +79,28 @@ public class WorkFlowLineageServiceImpl extends BaseServiceImpl implements WorkF private TaskDefinitionMapper taskDefinitionMapper; @Override - public Map queryWorkFlowLineageByName(long projectCode, String workFlowName) { - Map result = new HashMap<>(); + public List queryWorkFlowLineageByName(long projectCode, String workFlowName) { Project project = projectMapper.queryByCode(projectCode); if (project == null) { - log.error("Project does not exist, projectCode:{}.", projectCode); - putMsg(result, Status.PROJECT_NOT_FOUND, projectCode); - return result; + throw new ServiceException(Status.PROJECT_NOT_FOUND, projectCode); } - List workFlowLineageList = - workFlowLineageMapper.queryWorkFlowLineageByName(projectCode, workFlowName); - result.put(Constants.DATA_LIST, workFlowLineageList); - putMsg(result, Status.SUCCESS); - return result; + return workFlowLineageMapper.queryWorkFlowLineageByName(projectCode, workFlowName); } @Override public Map queryWorkFlowLineageByCode(long projectCode, long sourceWorkFlowCode) { - Map result = new HashMap<>(); Project project = projectMapper.queryByCode(projectCode); if (project == null) { - log.error("Project does not exist, projectCode:{}.", projectCode); - putMsg(result, Status.PROJECT_NOT_FOUND, projectCode); - return result; + throw new ServiceException(Status.PROJECT_NOT_FOUND, projectCode); } List workFlowLineages = new ArrayList<>(); Set workFlowRelations = new HashSet<>(); recursiveWorkFlow(projectCode, sourceWorkFlowCode, workFlowLineages, workFlowRelations); Map workFlowLists = new HashMap<>(); + // todo: use vo workFlowLists.put(Constants.WORKFLOW_LIST, workFlowLineages); workFlowLists.put(Constants.WORKFLOW_RELATION_LIST, workFlowRelations); - result.put(Constants.DATA_LIST, workFlowLists); - putMsg(result, Status.SUCCESS); - return result; + return workFlowLists; } private void recursiveWorkFlow(long projectCode, diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkerGroupServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkerGroupServiceImpl.java index e097e2ff5869e..88cd3247ae28f 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkerGroupServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkerGroupServiceImpl.java @@ -166,8 +166,6 @@ protected void handleDefaultWorkGroup(WorkerGroupMapper workerGroupMapper, Worke workerGroupMapper.updateById(workerGroup); } else { workerGroupMapper.insert(workerGroup); - permissionPostHandle(AuthorizationType.WORKER_GROUP, loginUser.getId(), - Collections.singletonList(workerGroup.getId()), log); } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/utils/PageInfo.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/utils/PageInfo.java index ba5735f4023cd..d783fcf3427a4 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/utils/PageInfo.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/utils/PageInfo.java @@ -20,11 +20,11 @@ import java.util.Collections; import java.util.List; -/** - * page info - * - * @param model - */ +import lombok.Data; + +import com.baomidou.mybatisplus.core.metadata.IPage; + +@Data public class PageInfo { /** @@ -65,67 +65,14 @@ public PageInfo(Integer currentPage, Integer pageSize) { this.currentPage = currentPage; } - public Integer getStart() { - return pageNo; - } - - public void setStart(Integer start) { - this.pageNo = start; - } - - public List getTotalList() { - return totalList; - } - - public void setTotalList(List totalList) { - this.totalList = totalList; - } - - public Integer getTotal() { - if (total == null) { - total = 0; - } - return total; - } - - public void setTotal(Integer total) { - this.total = total; + public static PageInfo of(IPage iPage) { + PageInfo pageInfo = new PageInfo<>((int) iPage.getCurrent(), (int) iPage.getSize()); + pageInfo.setTotalList(iPage.getRecords()); + pageInfo.setTotal((int) iPage.getTotal()); + return pageInfo; } - public Integer getTotalPage() { - if (pageSize == null || pageSize == 0) { - pageSize = 7; - } - this.totalPage = - (this.total % this.pageSize) == 0 - ? ((this.total / this.pageSize) == 0 ? 1 : (this.total / this.pageSize)) - : (this.total / this.pageSize + 1); - return this.totalPage; - } - - public void setTotalPage(Integer totalPage) { - this.totalPage = totalPage; - } - - public Integer getPageSize() { - if (pageSize == null || pageSize == 0) { - pageSize = 7; - } - return pageSize; - } - - public void setPageSize(Integer pageSize) { - this.pageSize = pageSize; - } - - public Integer getCurrentPage() { - if (currentPage == null || currentPage <= 0) { - this.currentPage = 1; - } - return currentPage; - } - - public void setCurrentPage(Integer currentPage) { - this.currentPage = currentPage; + public static PageInfo of(Integer currentPage, Integer pageSize) { + return new PageInfo<>(currentPage, pageSize); } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/CreateTokenResponse.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/AssertionsHelper.java similarity index 54% rename from dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/CreateTokenResponse.java rename to dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/AssertionsHelper.java index d7fa93c40cc4b..d2da5bc638c08 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/CreateTokenResponse.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/AssertionsHelper.java @@ -15,28 +15,23 @@ * limitations under the License. */ -package org.apache.dolphinscheduler.api.dto; +package org.apache.dolphinscheduler.api; -import org.apache.dolphinscheduler.api.utils.Result; -import org.apache.dolphinscheduler.dao.entity.AccessToken; +import org.apache.dolphinscheduler.api.enums.Status; +import org.apache.dolphinscheduler.api.exceptions.ServiceException; -public class CreateTokenResponse extends Result { +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.function.Executable; - private AccessToken data; +public class AssertionsHelper extends Assertions { - public CreateTokenResponse(Result result) { - super(); - this.setCode(result.getCode()); - this.setMsg(result.getMsg()); - this.setData((AccessToken) result.getData()); + public static void assertThrowsServiceException(Status status, Executable executable) { + ServiceException exception = Assertions.assertThrows(ServiceException.class, executable); + Assertions.assertEquals(status.getCode(), exception.getCode()); } - @Override - public AccessToken getData() { - return data; + public static void assertDoesNotThrow(Executable executable) { + Assertions.assertDoesNotThrow(executable); } - public void setData(AccessToken data) { - this.data = data; - } } diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AccessTokenControllerTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AccessTokenControllerTest.java index 86e382d2384fc..5d8308760b7c8 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AccessTokenControllerTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AccessTokenControllerTest.java @@ -90,7 +90,7 @@ public void testExceptionHandler() throws Exception { MvcResult mvcResult = mockMvc.perform(post("/access-tokens") .header("sessionId", sessionId) .params(paramsMap)) - .andExpect(status().isCreated()) + .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andReturn(); Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class); diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AccessTokenV2ControllerTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AccessTokenV2ControllerTest.java index 7ade90985a947..905767c3954c1 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AccessTokenV2ControllerTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AccessTokenV2ControllerTest.java @@ -91,11 +91,11 @@ public void testExceptionHandler() throws Exception { .header("sessionId", sessionId) .contentType(MediaType.APPLICATION_JSON) .content(JSONUtils.toJsonString(paramsMap))) - .andExpect(status().isCreated()) + // todo: bad request? + .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andReturn(); Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class); Assertions.assertEquals(Status.REQUEST_PARAMS_NOT_VALID_ERROR.getCode(), result.getCode().intValue()); - logger.info(mvcResult.getResponse().getContentAsString()); } } diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AlertPluginInstanceControllerTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AlertPluginInstanceControllerTest.java index 870fb1f82bfda..8b2907a4feee8 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AlertPluginInstanceControllerTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AlertPluginInstanceControllerTest.java @@ -20,6 +20,7 @@ import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; @@ -28,10 +29,9 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.service.AlertPluginInstanceService; +import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.Result; -import org.apache.dolphinscheduler.common.constants.Constants; import org.apache.dolphinscheduler.common.enums.AlertPluginInstanceType; import org.apache.dolphinscheduler.common.enums.WarningType; import org.apache.dolphinscheduler.common.utils.JSONUtils; @@ -44,8 +44,6 @@ import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; -import com.google.common.collect.ImmutableMap; - /** * alert plugin instance controller test */ @@ -57,9 +55,7 @@ public class AlertPluginInstanceControllerTest extends AbstractControllerTest { private static final AlertPluginInstanceType pluginInstanceType = AlertPluginInstanceType.NORMAL; private static final WarningType warningType = WarningType.ALL; private static final Result expectResponseContent = JSONUtils.parseObject( - "{\"code\":0,\"msg\":\"success\",\"data\":\"Test Data\",\"success\":true,\"failed\":false}", Result.class); - private static final ImmutableMap alertPluginInstanceServiceResult = - ImmutableMap.of(Constants.STATUS, Status.SUCCESS, Constants.DATA_LIST, "Test Data"); + "{\"code\":0,\"msg\":\"success\",\"data\":\"null\",\"success\":true,\"failed\":false}", Result.class); @MockBean(name = "alertPluginInstanceServiceImpl") private AlertPluginInstanceService alertPluginInstanceService; @@ -75,8 +71,7 @@ public void testCreateAlertPluginInstance() throws Exception { paramsMap.add("pluginInstanceParams", pluginInstanceParams); when(alertPluginInstanceService.create(any(User.class), eq(pluginDefineId), eq(instanceName), - eq(pluginInstanceType), eq(warningType), eq(pluginInstanceParams))) - .thenReturn(alertPluginInstanceServiceResult); + eq(pluginInstanceType), eq(warningType), eq(pluginInstanceParams))).thenReturn(null); // When final MvcResult mvcResult = mockMvc.perform(post("/alert-plugin-instances") @@ -89,7 +84,8 @@ public void testCreateAlertPluginInstance() throws Exception { // Then final Result actualResponseContent = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class); - assertThat(actualResponseContent.toString()).isEqualTo(expectResponseContent.toString()); + assertThat(actualResponseContent.getMsg()).isEqualTo(expectResponseContent.getMsg()); + assertThat(actualResponseContent.getCode()).isEqualTo(expectResponseContent.getCode()); } @Test @@ -101,9 +97,8 @@ public void testUpdateAlertPluginInstance() throws Exception { paramsMap.add("warningType", warningType.name()); paramsMap.add("pluginInstanceParams", pluginInstanceParams); - when(alertPluginInstanceService.update(any(User.class), eq(pluginDefineId), eq(instanceName), - eq(warningType), eq(pluginInstanceParams))) - .thenReturn(alertPluginInstanceServiceResult); + when(alertPluginInstanceService.updateById(any(User.class), eq(pluginDefineId), eq(instanceName), + eq(warningType), eq(pluginInstanceParams))).thenReturn(null); // When final MvcResult mvcResult = mockMvc.perform(put("/alert-plugin-instances/{id}", pluginDefineId) @@ -116,7 +111,8 @@ public void testUpdateAlertPluginInstance() throws Exception { // Then final Result actualResponseContent = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class); - assertThat(actualResponseContent.toString()).isEqualTo(expectResponseContent.toString()); + assertThat(actualResponseContent.getMsg()).isEqualTo(expectResponseContent.getMsg()); + assertThat(actualResponseContent.getCode()).isEqualTo(expectResponseContent.getCode()); } @Test @@ -127,8 +123,7 @@ public void testDeleteAlertPluginInstance() throws Exception { paramsMap.add("instanceName", instanceName); paramsMap.add("pluginInstanceParams", pluginInstanceParams); - when(alertPluginInstanceService.delete(any(User.class), eq(pluginDefineId))) - .thenReturn(alertPluginInstanceServiceResult); + doNothing().when(alertPluginInstanceService).deleteById(any(User.class), eq(pluginDefineId)); // When final MvcResult mvcResult = mockMvc.perform(delete("/alert-plugin-instances/{id}", pluginDefineId) @@ -141,7 +136,8 @@ public void testDeleteAlertPluginInstance() throws Exception { // Then final Result actualResponseContent = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class); - assertThat(actualResponseContent.toString()).isEqualTo(expectResponseContent.toString()); + assertThat(actualResponseContent.getMsg()).isEqualTo(expectResponseContent.getMsg()); + assertThat(actualResponseContent.getCode()).isEqualTo(expectResponseContent.getCode()); } @Test @@ -150,8 +146,7 @@ public void testGetAlertPluginInstance() throws Exception { final MultiValueMap paramsMap = new LinkedMultiValueMap<>(); paramsMap.add("pluginDefineId", String.valueOf(pluginDefineId)); - when(alertPluginInstanceService.get(any(User.class), eq(pluginDefineId))) - .thenReturn(alertPluginInstanceServiceResult); + when(alertPluginInstanceService.getById(any(User.class), eq(pluginDefineId))).thenReturn(null); // When final MvcResult mvcResult = mockMvc.perform(get("/alert-plugin-instances/{id}", pluginDefineId) @@ -164,14 +159,14 @@ public void testGetAlertPluginInstance() throws Exception { // Then final Result actualResponseContent = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class); - assertThat(actualResponseContent.toString()).isEqualTo(expectResponseContent.toString()); + assertThat(actualResponseContent.getMsg()).isEqualTo(expectResponseContent.getMsg()); + assertThat(actualResponseContent.getCode()).isEqualTo(expectResponseContent.getCode()); } @Test public void testGetAlertPluginInstanceList() throws Exception { // Given - when(alertPluginInstanceService.queryAll()) - .thenReturn(alertPluginInstanceServiceResult); + when(alertPluginInstanceService.queryAll()).thenReturn(null); // When final MvcResult mvcResult = mockMvc.perform(get("/alert-plugin-instances/list") @@ -183,7 +178,8 @@ public void testGetAlertPluginInstanceList() throws Exception { // Then final Result actualResponseContent = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class); - assertThat(actualResponseContent.toString()).isEqualTo(expectResponseContent.toString()); + assertThat(actualResponseContent.getMsg()).isEqualTo(expectResponseContent.getMsg()); + assertThat(actualResponseContent.getCode()).isEqualTo(expectResponseContent.getCode()); } @Test @@ -210,7 +206,8 @@ public void testVerifyGroupName() throws Exception { // Then final Result actualResponseContent = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class); - assertThat(actualResponseContent.toString()).isEqualTo(expectResponseContent.toString()); + assertThat(actualResponseContent.getMsg()).isEqualTo(expectResponseContent.getMsg()); + assertThat(actualResponseContent.getCode()).isEqualTo(expectResponseContent.getCode()); } @Test @@ -238,16 +235,13 @@ public void testVerifyGroupNamePluginInstanceNameExist() throws Exception { // Then final Result actualResponseContent = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class); - assertThat(actualResponseContent.toString()).isEqualTo(expectResponseContent.toString()); + assertThat(actualResponseContent.getMsg()).isEqualTo(expectResponseContent.getMsg()); + assertThat(actualResponseContent.getCode()).isEqualTo(expectResponseContent.getCode()); } @Test public void testListPaging() throws Exception { // Given - Result result = JSONUtils.parseObject( - "{\"code\":0,\"msg\":\"success\",\"data\":\"Test Data\",\"success\":true,\"failed\":false}", - Result.class); - final MultiValueMap paramsMap = new LinkedMultiValueMap<>(); paramsMap.add("pluginDefineId", String.valueOf(pluginDefineId)); paramsMap.add("searchVal", "searchVal"); @@ -255,7 +249,7 @@ public void testListPaging() throws Exception { paramsMap.add("pageSize", String.valueOf(10)); when(alertPluginInstanceService.listPaging(eq(user), eq("searchVal"), eq(1), eq(10))) - .thenReturn(result); + .thenReturn(PageInfo.of(1, 10)); // When final MvcResult mvcResult = mockMvc.perform(get("/alert-plugin-instances") @@ -268,7 +262,8 @@ public void testListPaging() throws Exception { // Then final Result actualResponseContent = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class); - assertThat(actualResponseContent.toString()).isEqualTo(expectResponseContent.toString()); + assertThat(actualResponseContent.getMsg()).isEqualTo(expectResponseContent.getMsg()); + assertThat(actualResponseContent.getCode()).isEqualTo(expectResponseContent.getCode()); } @Test @@ -295,6 +290,7 @@ public void testListPagingResultFalse() throws Exception { // Then final Result actualResponseContent = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class); - assertThat(actualResponseContent.toString()).isEqualTo(expectResponseContent.toString()); + assertThat(actualResponseContent.getMsg()).isEqualTo(expectResponseContent.getMsg()); + assertThat(actualResponseContent.getCode()).isEqualTo(expectResponseContent.getCode()); } } diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/DataQualityControllerTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/DataQualityControllerTest.java index dbbda17334b90..04464550e9ad9 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/DataQualityControllerTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/DataQualityControllerTest.java @@ -19,6 +19,7 @@ import static org.mockito.Mockito.when; +import org.apache.dolphinscheduler.api.AssertionsHelper; import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.service.impl.DqExecuteResultServiceImpl; import org.apache.dolphinscheduler.api.service.impl.DqRuleServiceImpl; @@ -26,6 +27,7 @@ import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.constants.Constants; import org.apache.dolphinscheduler.common.enums.UserType; +import org.apache.dolphinscheduler.dao.entity.DqExecuteResult; import org.apache.dolphinscheduler.dao.entity.DqRule; import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.plugin.task.api.enums.dp.RuleType; @@ -33,7 +35,6 @@ import java.text.MessageFormat; import java.util.ArrayList; import java.util.Date; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -74,16 +75,10 @@ public void before() { } @Test - public void testGetRuleFormCreateJsonById() throws Exception { + public void testGetRuleFormCreateJsonById() { - Map result = new HashMap<>(); - putMsg(result, Status.SUCCESS); - result.put(Constants.DATA_LIST, 1); - - Mockito.when(dqRuleService.getRuleFormCreateJsonById(1)).thenReturn(result); - - Result response = dataQualityController.getRuleFormCreateJsonById(1); - Assertions.assertEquals(Status.SUCCESS.getCode(), response.getCode().intValue()); + Mockito.when(dqRuleService.getRuleFormCreateJsonById(1)).thenReturn(""); + AssertionsHelper.assertDoesNotThrow(() -> dataQualityController.getRuleFormCreateJsonById(1)); } private void putMsg(Map result, Status status, Object... statusParams) { @@ -132,50 +127,38 @@ public void testQueryRuleListPaging() throws Exception { pageInfo.setTotal(10); pageInfo.setTotalList(getRuleList()); - Result result = new Result(); - result.setData(pageInfo); - putMsg(result, Status.SUCCESS); + when(dqRuleService.queryRuleListPaging(user, searchVal, ruleType, start, end, 1, 10)).thenReturn(pageInfo); - when(dqRuleService.queryRuleListPaging( - user, searchVal, ruleType, start, end, 1, 10)).thenReturn(result); - - Result response = dataQualityController.queryRuleListPaging(user, searchVal, ruleType, start, end, 1, 10); + Result> response = + dataQualityController.queryRuleListPaging(user, searchVal, ruleType, start, end, 1, 10); Assertions.assertEquals(Status.SUCCESS.getCode(), response.getCode().intValue()); } @Test - public void testQueryRuleList() throws Exception { - - Map result = new HashMap<>(); - putMsg(result, Status.SUCCESS); - result.put(Constants.DATA_LIST, getRuleList()); + public void testQueryRuleList() { - when(dqRuleService.queryAllRuleList()).thenReturn(result); + when(dqRuleService.queryAllRuleList()).thenReturn(getRuleList()); - Result response = dataQualityController.queryRuleList(); - Assertions.assertEquals(Status.SUCCESS.getCode(), response.getCode().intValue()); + Result> listResult = dataQualityController.queryRuleList(); + Assertions.assertEquals(Status.SUCCESS.getCode(), listResult.getCode().intValue()); } @Test - public void testQueryResultListPaging() throws Exception { + public void testQueryResultListPaging() { String searchVal = ""; int ruleType = 0; String start = "2020-01-01 00:00:00"; String end = "2020-01-02 00:00:00"; - PageInfo pageInfo = new PageInfo<>(1, 10); + PageInfo pageInfo = new PageInfo<>(1, 10); pageInfo.setTotal(10); - Result result = new Result(); - result.setData(pageInfo); - putMsg(result, Status.SUCCESS); + when(dqExecuteResultService.queryResultListPaging(user, searchVal, 0, ruleType, start, end, 1, 10)) + .thenReturn(pageInfo); - when(dqExecuteResultService.queryResultListPaging( - user, searchVal, 0, ruleType, start, end, 1, 10)).thenReturn(result); - - Result response = + Result> pageInfoResult = dataQualityController.queryExecuteResultListPaging(user, searchVal, ruleType, 0, start, end, 1, 10); - Assertions.assertEquals(Status.SUCCESS.getCode(), response.getCode().intValue()); + Assertions.assertEquals(Status.SUCCESS.getCode(), pageInfoResult.getCode().intValue()); } } diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/WorkFlowLineageControllerTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/WorkFlowLineageControllerTest.java index ead9554f41041..b0f7bbc362fa7 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/WorkFlowLineageControllerTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/WorkFlowLineageControllerTest.java @@ -17,18 +17,19 @@ package org.apache.dolphinscheduler.api.controller; +import static org.apache.dolphinscheduler.api.AssertionsHelper.assertDoesNotThrow; + import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.service.impl.WorkFlowLineageServiceImpl; -import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.constants.Constants; import org.apache.dolphinscheduler.common.enums.UserType; import org.apache.dolphinscheduler.dao.entity.User; import java.text.MessageFormat; +import java.util.Collections; import java.util.HashMap; import java.util.Map; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -73,23 +74,16 @@ private void putMsg(Map result, Status status, Object... statusP public void testQueryWorkFlowLineageByName() { long projectCode = 1L; String searchVal = "test"; - Map result = new HashMap<>(); - putMsg(result, Status.SUCCESS); - result.put(Constants.DATA_LIST, 1); - Mockito.when(workFlowLineageService.queryWorkFlowLineageByName(projectCode, searchVal)).thenReturn(result); - Result response = workFlowLineageController.queryWorkFlowLineageByName(user, projectCode, searchVal); - Assertions.assertEquals(Status.SUCCESS.getCode(), response.getCode().intValue()); + Mockito.when(workFlowLineageService.queryWorkFlowLineageByName(projectCode, searchVal)) + .thenReturn(Collections.emptyList()); + assertDoesNotThrow(() -> workFlowLineageController.queryWorkFlowLineageByName(user, projectCode, searchVal)); } @Test public void testQueryWorkFlowLineageByCode() { long projectCode = 1L; long code = 1L; - Map result = new HashMap<>(); - putMsg(result, Status.SUCCESS); - result.put(Constants.DATA_LIST, 1); - Mockito.when(workFlowLineageService.queryWorkFlowLineageByCode(projectCode, code)).thenReturn(result); - Result response = workFlowLineageController.queryWorkFlowLineageByCode(user, projectCode, code); - Assertions.assertEquals(Status.SUCCESS.getCode(), response.getCode().intValue()); + Mockito.when(workFlowLineageService.queryWorkFlowLineageByCode(projectCode, code)).thenReturn(new HashMap<>()); + assertDoesNotThrow(() -> workFlowLineageController.queryWorkFlowLineageByCode(user, projectCode, code)); } } diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/v2/StatisticsV2ControllerTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/v2/StatisticsV2ControllerTest.java index d7c4c883893bc..f5c488581f7cc 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/v2/StatisticsV2ControllerTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/v2/StatisticsV2ControllerTest.java @@ -17,26 +17,30 @@ package org.apache.dolphinscheduler.api.controller.v2; +import static org.apache.dolphinscheduler.api.AssertionsHelper.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.when; + import org.apache.dolphinscheduler.api.controller.AbstractControllerTest; +import org.apache.dolphinscheduler.api.dto.DefineUserDto; import org.apache.dolphinscheduler.api.dto.TaskCountDto; import org.apache.dolphinscheduler.api.dto.project.StatisticsStateRequest; import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.service.impl.DataAnalysisServiceImpl; import org.apache.dolphinscheduler.api.utils.Result; -import org.apache.dolphinscheduler.common.constants.Constants; +import org.apache.dolphinscheduler.dao.entity.DefinitionGroupByUser; import org.apache.dolphinscheduler.dao.entity.ExecuteStatusCount; import org.apache.dolphinscheduler.dao.entity.User; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.Mockito; public class StatisticsV2ControllerTest extends AbstractControllerTest { @@ -54,113 +58,92 @@ public void testQueryWorkflowInstanceCounts() { result.put("data", "AllWorkflowCounts = " + count); putMsg(result, Status.SUCCESS); - Mockito.when(dataAnalysisService.queryAllWorkflowCounts(loginUser)).thenReturn(result); + when(dataAnalysisService.queryAllWorkflowCounts(loginUser)).thenReturn(result); Result result1 = statisticsV2Controller.queryWorkflowInstanceCounts(loginUser); - Assertions.assertTrue(result1.isSuccess()); + assertTrue(result1.isSuccess()); } @Test public void testQueryWorkflowStatesCounts() { User loginUser = getLoginUser(); - Map result = new HashMap<>(); StatisticsStateRequest statisticsStateRequest = new StatisticsStateRequest(); List executeStatusCounts = new ArrayList<>(); TaskCountDto taskCountResult = new TaskCountDto(executeStatusCounts); - result.put(Constants.DATA_LIST, taskCountResult); - putMsg(result, Status.SUCCESS); - Mockito.when(dataAnalysisService.countWorkflowStates(loginUser, statisticsStateRequest)).thenReturn(result); + when(dataAnalysisService.countWorkflowStates(loginUser, statisticsStateRequest)).thenReturn(taskCountResult); Result result1 = statisticsV2Controller.queryWorkflowStatesCounts(loginUser, statisticsStateRequest); - Assertions.assertTrue(result1.isSuccess()); + assertTrue(result1.isSuccess()); } @Test public void testQueryOneWorkflowStates() { User loginUser = getLoginUser(); Long workflowCode = 1L; - Map result = new HashMap<>(); List executeStatusCounts = new ArrayList<>(); TaskCountDto taskCountResult = new TaskCountDto(executeStatusCounts); - result.put(Constants.DATA_LIST, taskCountResult); - putMsg(result, Status.SUCCESS); - Mockito.when(dataAnalysisService.countOneWorkflowStates(loginUser, workflowCode)).thenReturn(result); + when(dataAnalysisService.countOneWorkflowStates(loginUser, workflowCode)).thenReturn(taskCountResult); Result result1 = statisticsV2Controller.queryOneWorkflowStates(loginUser, workflowCode); - Assertions.assertTrue(result1.isSuccess()); + assertTrue(result1.isSuccess()); } @Test public void testQueryTaskStatesCounts() { User loginUser = getLoginUser(); - Map result = new HashMap<>(); StatisticsStateRequest statisticsStateRequest = new StatisticsStateRequest(); List executeStatusCounts = new ArrayList<>(); TaskCountDto taskCountResult = new TaskCountDto(executeStatusCounts); - result.put(Constants.DATA_LIST, taskCountResult); - putMsg(result, Status.SUCCESS); - Mockito.when(dataAnalysisService.countTaskStates(loginUser, statisticsStateRequest)).thenReturn(result); + when(dataAnalysisService.countTaskStates(loginUser, statisticsStateRequest)).thenReturn(taskCountResult); Result result1 = statisticsV2Controller.queryTaskStatesCounts(loginUser, statisticsStateRequest); - Assertions.assertTrue(result1.isSuccess()); + assertTrue(result1.isSuccess()); } @Test public void testQueryOneTaskStatesCounts() { User loginUser = getLoginUser(); Long taskCode = 1L; - Map result = new HashMap<>(); List executeStatusCounts = new ArrayList<>(); TaskCountDto taskCountResult = new TaskCountDto(executeStatusCounts); - result.put(Constants.DATA_LIST, taskCountResult); - putMsg(result, Status.SUCCESS); - Mockito.when(dataAnalysisService.countOneTaskStates(loginUser, taskCode)).thenReturn(result); + when(dataAnalysisService.countOneTaskStates(loginUser, taskCode)).thenReturn(taskCountResult); Result result1 = statisticsV2Controller.queryOneTaskStatesCounts(loginUser, taskCode); - Assertions.assertTrue(result1.isSuccess()); + assertTrue(result1.isSuccess()); } @Test public void testCountDefinitionByUser() { User loginUser = getLoginUser(); - Map result = new HashMap<>(); StatisticsStateRequest statisticsStateRequest = new StatisticsStateRequest(); - List executeStatusCounts = new ArrayList<>(); - TaskCountDto taskCountResult = new TaskCountDto(executeStatusCounts); - result.put(Constants.DATA_LIST, taskCountResult); - putMsg(result, Status.SUCCESS); - Mockito.when(dataAnalysisService.countDefinitionByUserV2(loginUser, statisticsStateRequest.getProjectCode(), - null, null)).thenReturn(result); - - Result result1 = statisticsV2Controller.countDefinitionByUser(loginUser, statisticsStateRequest); + List definitionGroupByUsers = new ArrayList<>(); + DefineUserDto taskCountResult = new DefineUserDto(definitionGroupByUsers); + when(dataAnalysisService.countDefinitionByUserV2(loginUser, statisticsStateRequest.getProjectCode(), null, + null)).thenReturn(taskCountResult); - Assertions.assertTrue(result1.isSuccess()); + assertDoesNotThrow(() -> statisticsV2Controller.countDefinitionByUser(loginUser, statisticsStateRequest)); } @Test public void testCountDefinitionByUserId() { User loginUser = getLoginUser(); - Map result = new HashMap<>(); Integer userId = 1; - putMsg(result, Status.SUCCESS); - - Mockito.when(dataAnalysisService.countDefinitionByUserV2(loginUser, null, userId, null)).thenReturn(result); - - Result result1 = statisticsV2Controller.countDefinitionByUserId(loginUser, userId); + DefineUserDto defineUserDto = new DefineUserDto(Collections.emptyList()); + when(dataAnalysisService.countDefinitionByUserV2(loginUser, null, userId, null)).thenReturn(defineUserDto); - Assertions.assertTrue(result1.isSuccess()); + assertDoesNotThrow(() -> statisticsV2Controller.countDefinitionByUserId(loginUser, userId)); } private User getLoginUser() { diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AccessTokenServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AccessTokenServiceTest.java index 28eb2bea08ecb..8db009bb71a9d 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AccessTokenServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AccessTokenServiceTest.java @@ -17,8 +17,13 @@ package org.apache.dolphinscheduler.api.service; +import static org.apache.dolphinscheduler.api.AssertionsHelper.assertDoesNotThrow; +import static org.apache.dolphinscheduler.api.AssertionsHelper.assertThrowsServiceException; import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.ACCESS_TOKEN_DELETE; import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.ACCESS_TOKEN_UPDATE; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; @@ -28,8 +33,6 @@ import org.apache.dolphinscheduler.api.service.impl.AccessTokenServiceImpl; import org.apache.dolphinscheduler.api.service.impl.BaseServiceImpl; import org.apache.dolphinscheduler.api.utils.PageInfo; -import org.apache.dolphinscheduler.api.utils.Result; -import org.apache.dolphinscheduler.common.constants.Constants; import org.apache.dolphinscheduler.common.enums.AuthorizationType; import org.apache.dolphinscheduler.common.enums.UserType; import org.apache.dolphinscheduler.common.utils.DateUtils; @@ -41,10 +44,8 @@ import java.util.Calendar; import java.util.Date; import java.util.List; -import java.util.Map; import org.assertj.core.util.Lists; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -67,8 +68,6 @@ public class AccessTokenServiceTest { private static final Logger baseServiceLogger = LoggerFactory.getLogger(BaseServiceImpl.class); - private static final Logger logger = LoggerFactory.getLogger(AccessTokenServiceTest.class); - @InjectMocks private AccessTokenServiceImpl accessTokenService; @@ -87,16 +86,13 @@ public void testQueryAccessTokenList() { user.setId(1); user.setUserType(UserType.ADMIN_USER); when(accessTokenMapper.selectAccessTokenPage(any(Page.class), eq("zhangsan"), eq(0))).thenReturn(tokenPage); - Result result = accessTokenService.queryAccessTokenList(user, "zhangsan", 1, 10); - PageInfo pageInfo = (PageInfo) result.getData(); - Assertions.assertEquals(0, (int) pageInfo.getTotal()); + PageInfo pageInfo = accessTokenService.queryAccessTokenList(user, "zhangsan", 1, 10); + assertEquals(0, (int) pageInfo.getTotal()); tokenPage.setTotal(1L); when(accessTokenMapper.selectAccessTokenPage(any(Page.class), eq("zhangsan"), eq(0))).thenReturn(tokenPage); - result = accessTokenService.queryAccessTokenList(user, "zhangsan", 1, 10); - pageInfo = (PageInfo) result.getData(); - logger.info(result.toString()); - Assertions.assertTrue(pageInfo.getTotal() > 0); + pageInfo = accessTokenService.queryAccessTokenList(user, "zhangsan", 1, 10); + assertTrue(pageInfo.getTotal() > 0); } @Test @@ -104,24 +100,20 @@ public void testQueryAccessTokenByUser() { User user = this.getLoginUser(); user.setUserType(UserType.ADMIN_USER); List accessTokenList = Lists.newArrayList(this.getEntity()); - Mockito.when(this.accessTokenMapper.queryAccessTokenByUser(Mockito.anyInt())).thenReturn(accessTokenList); - Map result = this.accessTokenService.queryAccessTokenByUser(user, 1); - logger.info(result.toString()); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); + when(this.accessTokenMapper.queryAccessTokenByUser(Mockito.anyInt())).thenReturn(accessTokenList); + assertDoesNotThrow(() -> accessTokenService.queryAccessTokenByUser(user, 1)); } @Test public void testCreateToken() { // Given Token when(accessTokenMapper.insert(any(AccessToken.class))).thenReturn(2); - Result result = accessTokenService.createToken(getLoginUser(), 1, getDate(), "AccessTokenServiceTest"); - logger.info(result.toString()); - Assertions.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue()); + assertDoesNotThrow(() -> { + accessTokenService.createToken(getLoginUser(), 1, getDate(), "AccessTokenServiceTest"); + }); // Token is absent - result = this.accessTokenService.createToken(getLoginUser(), 1, getDate(), null); - logger.info(result.toString()); - Assertions.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue()); + assertDoesNotThrow(() -> accessTokenService.createToken(getLoginUser(), 1, getDate(), null)); } @Test @@ -129,11 +121,8 @@ public void testGenerateToken() { User user = new User(); user.setId(1); user.setUserType(UserType.ADMIN_USER); - Map result = accessTokenService.generateToken(getLoginUser(), Integer.MAX_VALUE, getDate()); - logger.info(result.toString()); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); - String token = (String) result.get(Constants.DATA_LIST); - Assertions.assertNotNull(token); + String token = accessTokenService.generateToken(getLoginUser(), Integer.MAX_VALUE, getDate()); + assertNotNull(token); } @Test @@ -143,27 +132,23 @@ public void testDelAccessTokenById() { User userLogin = new User(); userLogin.setId(1); userLogin.setUserType(UserType.ADMIN_USER); - Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ACCESS_TOKEN, 1, + when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ACCESS_TOKEN, 1, ACCESS_TOKEN_DELETE, baseServiceLogger)).thenReturn(true); - Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ACCESS_TOKEN, null, 0, + when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ACCESS_TOKEN, null, 0, baseServiceLogger)).thenReturn(true); // not exist - Map result = accessTokenService.delAccessTokenById(userLogin, 0); - logger.info(result.toString()); - Assertions.assertEquals(Status.ACCESS_TOKEN_NOT_EXIST, result.get(Constants.STATUS)); + assertThrowsServiceException(Status.ACCESS_TOKEN_NOT_EXIST, + () -> accessTokenService.deleteAccessTokenById(userLogin, 0)); // no operate userLogin.setId(2); - result = accessTokenService.delAccessTokenById(userLogin, 1); - logger.info(result.toString()); - Assertions.assertEquals(Status.USER_NO_OPERATION_PERM, result.get(Constants.STATUS)); + assertThrowsServiceException(Status.USER_NO_OPERATION_PERM, + () -> accessTokenService.deleteAccessTokenById(userLogin, 1)); // success userLogin.setId(1); userLogin.setUserType(UserType.ADMIN_USER); - Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ACCESS_TOKEN, null, 0, + when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ACCESS_TOKEN, null, 0, baseServiceLogger)).thenReturn(true); - result = accessTokenService.delAccessTokenById(userLogin, 1); - logger.info(result.toString()); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); + assertDoesNotThrow(() -> accessTokenService.deleteAccessTokenById(userLogin, 1)); } @Test @@ -171,30 +156,26 @@ public void testUpdateToken() { User user = new User(); user.setId(1); user.setUserType(UserType.ADMIN_USER); - Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ACCESS_TOKEN, 1, + when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ACCESS_TOKEN, 1, ACCESS_TOKEN_UPDATE, baseServiceLogger)).thenReturn(true); - Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ACCESS_TOKEN, null, 0, + when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ACCESS_TOKEN, null, 0, baseServiceLogger)).thenReturn(true); // Given Token when(accessTokenMapper.selectById(1)).thenReturn(getEntity()); - Map result = + when(accessTokenMapper.updateById(any())).thenReturn(1); + AccessToken accessToken = accessTokenService.updateToken(getLoginUser(), 1, Integer.MAX_VALUE, getDate(), "token"); - logger.info(result.toString()); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); - Assertions.assertNotNull(result.get(Constants.DATA_LIST)); + assertEquals("token", accessToken.getToken()); // Token is absent - result = accessTokenService.updateToken(getLoginUser(), 1, Integer.MAX_VALUE, getDate(), null); - logger.info(result.toString()); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); - Assertions.assertNotNull(result.get(Constants.DATA_LIST)); + accessToken = accessTokenService.updateToken(getLoginUser(), 1, Integer.MAX_VALUE, getDate(), null); + assertNotNull(accessToken.getToken()); // ACCESS_TOKEN_NOT_EXIST - Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ACCESS_TOKEN, null, 0, + when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ACCESS_TOKEN, null, 0, baseServiceLogger)).thenReturn(true); - result = accessTokenService.updateToken(getLoginUser(), 2, Integer.MAX_VALUE, getDate(), "token"); - logger.info(result.toString()); - Assertions.assertEquals(Status.ACCESS_TOKEN_NOT_EXIST, result.get(Constants.STATUS)); + assertThrowsServiceException(Status.ACCESS_TOKEN_NOT_EXIST, + () -> accessTokenService.updateToken(getLoginUser(), 2, Integer.MAX_VALUE, getDate(), "token")); } private User getLoginUser() { diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertGroupServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertGroupServiceTest.java index 695ee3b5a4efb..85cbcbc833bc7 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertGroupServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertGroupServiceTest.java @@ -17,19 +17,21 @@ package org.apache.dolphinscheduler.api.service; +import static org.apache.dolphinscheduler.api.AssertionsHelper.assertDoesNotThrow; +import static org.apache.dolphinscheduler.api.AssertionsHelper.assertThrowsServiceException; import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.ALERT_GROUP_CREATE; import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.ALERT_GROUP_DELETE; import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.ALERT_GROUP_UPDATE; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.permission.ResourcePermissionCheckService; import org.apache.dolphinscheduler.api.service.impl.AlertGroupServiceImpl; import org.apache.dolphinscheduler.api.service.impl.BaseServiceImpl; import org.apache.dolphinscheduler.api.utils.PageInfo; -import org.apache.dolphinscheduler.api.utils.Result; -import org.apache.dolphinscheduler.common.constants.Constants; import org.apache.dolphinscheduler.common.enums.AuthorizationType; import org.apache.dolphinscheduler.common.enums.UserType; import org.apache.dolphinscheduler.dao.entity.AlertGroup; @@ -40,7 +42,6 @@ import java.util.ArrayList; import java.util.List; -import java.util.Map; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -81,21 +82,17 @@ public class AlertGroupServiceTest { @Test public void testQueryAlertGroup() { - Mockito.when(alertGroupMapper.queryAllGroupList()).thenReturn(getList()); - Map result = alertGroupService.queryAlertgroup(getLoginUser()); - logger.info(result.toString()); - List alertGroups = (List) result.get(Constants.DATA_LIST); - Assertions.assertEquals(alertGroups.size(), 2); + when(alertGroupMapper.queryAllGroupList()).thenReturn(getList()); + List alertGroups = alertGroupService.queryAllAlertGroup(getLoginUser()); + Assertions.assertEquals(2, alertGroups.size()); } @Test public void testQueryNormalAlertGroup() { - Mockito.when(alertGroupMapper.queryAllGroupList()).thenReturn(getList()); - Map result = alertGroupService.queryNormalAlertgroup(getLoginUser()); - logger.info(result.toString()); - List alertGroups = (List) result.get(Constants.DATA_LIST); - Assertions.assertEquals(alertGroups.size(), 1); + when(alertGroupMapper.queryAllGroupList()).thenReturn(getList()); + List alertGroups = alertGroupService.queryNormalAlertGroups(getLoginUser()); + Assertions.assertEquals(1, alertGroups.size()); } @Test @@ -103,64 +100,57 @@ public void testListPaging() { IPage page = new Page<>(1, 10); page.setTotal(2L); page.setRecords(getList()); - Mockito.when(alertGroupMapper.queryAlertGroupPage(any(Page.class), eq(groupName))).thenReturn(page); + when(alertGroupMapper.queryAlertGroupPage(any(Page.class), eq(groupName))).thenReturn(page); User user = new User(); // no operate user.setUserType(UserType.GENERAL_USER); user.setId(88); - Result result = alertGroupService.listPaging(user, groupName, 1, 10); - logger.info(result.toString()); - Assertions.assertEquals(Status.SUCCESS.getCode(), (int) result.getCode()); + PageInfo alertGroupPageInfo = alertGroupService.listPaging(user, groupName, 1, 10); + assertNotNull(alertGroupPageInfo); // success user.setUserType(UserType.ADMIN_USER); user.setId(0); - result = alertGroupService.listPaging(user, groupName, 1, 10); - logger.info(result.toString()); - PageInfo pageInfo = (PageInfo) result.getData(); - Assertions.assertTrue(CollectionUtils.isNotEmpty(pageInfo.getTotalList())); + alertGroupPageInfo = alertGroupService.listPaging(user, groupName, 1, 10); + Assertions.assertTrue(CollectionUtils.isNotEmpty(alertGroupPageInfo.getTotalList())); } @Test public void testCreateAlertgroup() { - Mockito.when(alertGroupMapper.insert(any(AlertGroup.class))).thenReturn(3); + when(alertGroupMapper.insert(any(AlertGroup.class))).thenReturn(3); User user = new User(); user.setId(0); // no operate user.setUserType(UserType.GENERAL_USER); - Map result = alertGroupService.createAlertgroup(user, groupName, groupName, null); - logger.info(result.toString()); - Assertions.assertEquals(Status.USER_NO_OPERATION_PERM, result.get(Constants.STATUS)); + assertThrowsServiceException(Status.USER_NO_OPERATION_PERM, + () -> alertGroupService.createAlertGroup(user, groupName, groupName, null)); + user.setUserType(UserType.ADMIN_USER); user.setId(0); // success - Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ALERT_GROUP, - user.getId(), ALERT_GROUP_CREATE, baseServiceLogger)).thenReturn(true); - Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_GROUP, null, - user.getId(), baseServiceLogger)).thenReturn(true); - result = alertGroupService.createAlertgroup(user, groupName, groupName, null); - logger.info(result.toString()); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); - Assertions.assertNotNull(result.get(Constants.DATA_LIST)); + when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ALERT_GROUP, user.getId(), + ALERT_GROUP_CREATE, baseServiceLogger)).thenReturn(true); + when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_GROUP, null, user.getId(), + baseServiceLogger)).thenReturn(true); + AlertGroup alertGroup = alertGroupService.createAlertGroup(user, groupName, groupName, null); + assertNotNull(alertGroup); } @Test public void testCreateAlertgroupDuplicate() { - Mockito.when(alertGroupMapper.insert(any(AlertGroup.class))) - .thenThrow(new DuplicateKeyException("group name exist")); + when(alertGroupMapper.insert(any(AlertGroup.class))).thenThrow(new DuplicateKeyException("group name exist")); User user = new User(); user.setUserType(UserType.ADMIN_USER); user.setId(0); - Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ALERT_GROUP, - user.getId(), ALERT_GROUP_CREATE, baseServiceLogger)).thenReturn(true); - Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_GROUP, null, - user.getId(), baseServiceLogger)).thenReturn(true); - Map result = alertGroupService.createAlertgroup(user, groupName, groupName, null); - logger.info(result.toString()); - Assertions.assertEquals(Status.ALERT_GROUP_EXIST, result.get(Constants.STATUS)); + when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ALERT_GROUP, user.getId(), + ALERT_GROUP_CREATE, baseServiceLogger)).thenReturn(true); + when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_GROUP, null, user.getId(), + baseServiceLogger)).thenReturn(true); + assertThrowsServiceException(Status.ALERT_GROUP_EXIST, + () -> alertGroupService.createAlertGroup(user, groupName, groupName, null)); } @Test @@ -170,27 +160,22 @@ public void testUpdateAlertgroup() { user.setId(0); // no operate user.setUserType(UserType.GENERAL_USER); - Map result = alertGroupService.updateAlertgroup(user, 1, groupName, groupName, null); - logger.info(result.toString()); - Assertions.assertEquals(Status.USER_NO_OPERATION_PERM, result.get(Constants.STATUS)); + assertThrowsServiceException(Status.USER_NO_OPERATION_PERM, + () -> alertGroupService.updateAlertGroupById(user, 1, groupName, groupName, null)); user.setUserType(UserType.ADMIN_USER); // not exist user.setUserType(UserType.ADMIN_USER); - Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ALERT_GROUP, - user.getId(), ALERT_GROUP_UPDATE, baseServiceLogger)).thenReturn(true); - Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_GROUP, - new Object[]{1}, 0, baseServiceLogger)).thenReturn(true); - result = alertGroupService.updateAlertgroup(user, 1, groupName, groupName, null); - logger.info(result.toString()); - Assertions.assertEquals(Status.ALERT_GROUP_NOT_EXIST, result.get(Constants.STATUS)); + when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ALERT_GROUP, user.getId(), + ALERT_GROUP_UPDATE, baseServiceLogger)).thenReturn(true); + when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_GROUP, new Object[]{1}, 0, + baseServiceLogger)).thenReturn(true); + assertThrowsServiceException(Status.ALERT_GROUP_NOT_EXIST, + () -> alertGroupService.updateAlertGroupById(user, 1, groupName, groupName, null)); // success - Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_GROUP, - new Object[]{3}, user.getId(), baseServiceLogger)).thenReturn(true); - Mockito.when(alertGroupMapper.selectById(3)).thenReturn(getEntity()); - result = alertGroupService.updateAlertgroup(user, 3, groupName, groupName, null); - logger.info(result.toString()); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); - + when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_GROUP, new Object[]{3}, + user.getId(), baseServiceLogger)).thenReturn(true); + when(alertGroupMapper.selectById(3)).thenReturn(getEntity()); + assertDoesNotThrow(() -> alertGroupService.updateAlertGroupById(user, 3, groupName, groupName, null)); } @Test @@ -198,15 +183,15 @@ public void testUpdateAlertgroupDuplicate() { User user = new User(); user.setId(0); user.setUserType(UserType.ADMIN_USER); - Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ALERT_GROUP, + when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ALERT_GROUP, user.getId(), ALERT_GROUP_UPDATE, baseServiceLogger)).thenReturn(true); - Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_GROUP, + when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_GROUP, new Object[]{3}, user.getId(), baseServiceLogger)).thenReturn(true); - Mockito.when(alertGroupMapper.selectById(3)).thenReturn(getEntity()); - Mockito.when(alertGroupMapper.updateById(Mockito.any())) + when(alertGroupMapper.selectById(3)).thenReturn(getEntity()); + when(alertGroupMapper.updateById(Mockito.any())) .thenThrow(new DuplicateKeyException("group name exist")); - Map result = alertGroupService.updateAlertgroup(user, 3, groupName, groupName, null); - Assertions.assertEquals(Status.ALERT_GROUP_EXIST, result.get(Constants.STATUS)); + assertThrowsServiceException(Status.ALERT_GROUP_EXIST, + () -> alertGroupService.updateAlertGroupById(user, 3, groupName, groupName, null)); } @Test @@ -217,8 +202,8 @@ public void testUpdateGlobalAlertgroup() { AlertGroup globalAlertGroup = new AlertGroup(); globalAlertGroup.setId(2); globalAlertGroup.setGroupName("global alert group"); - Map result = alertGroupService.updateAlertgroup(user, 2, groupName, groupName, null); - Assertions.assertEquals(Status.NOT_ALLOW_TO_UPDATE_GLOBAL_ALARM_GROUP, result.get(Constants.STATUS)); + assertThrowsServiceException(Status.NOT_ALLOW_TO_UPDATE_GLOBAL_ALARM_GROUP, + () -> alertGroupService.updateAlertGroupById(user, 2, groupName, groupName, null)); } @Test @@ -227,42 +212,36 @@ public void testDelAlertgroupById() { user.setId(0); // no operate user.setUserType(UserType.GENERAL_USER); - Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ALERT_GROUP, - user.getId(), ALERT_GROUP_DELETE, baseServiceLogger)).thenReturn(true); - Map result = alertGroupService.delAlertgroupById(user, 1); - logger.info(result.toString()); - Assertions.assertEquals(Status.USER_NO_OPERATION_PERM, result.get(Constants.STATUS)); + when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ALERT_GROUP, user.getId(), + ALERT_GROUP_DELETE, baseServiceLogger)).thenReturn(true); + assertThrowsServiceException(Status.USER_NO_OPERATION_PERM, + () -> alertGroupService.deleteAlertGroupById(user, 1)); // not exist user.setUserType(UserType.ADMIN_USER); user.setId(0); - Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ALERT_GROUP, + when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ALERT_GROUP, user.getId(), ALERT_GROUP_DELETE, baseServiceLogger)).thenReturn(true); - Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_GROUP, + when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_GROUP, new Object[]{3}, 0, baseServiceLogger)).thenReturn(true); - result = alertGroupService.delAlertgroupById(user, 3); - logger.info(result.toString()); - Assertions.assertEquals(Status.ALERT_GROUP_NOT_EXIST, result.get(Constants.STATUS)); + assertThrowsServiceException(Status.ALERT_GROUP_NOT_EXIST, + () -> alertGroupService.deleteAlertGroupById(user, 3)); // not allowed1 - Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_GROUP, - new Object[]{1}, 0, baseServiceLogger)).thenReturn(true); - result = alertGroupService.delAlertgroupById(user, 1); - logger.info(result.toString()); - Assertions.assertEquals(Status.NOT_ALLOW_TO_DELETE_DEFAULT_ALARM_GROUP, result.get(Constants.STATUS)); + when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_GROUP, new Object[]{1}, 0, + baseServiceLogger)).thenReturn(true); + assertThrowsServiceException(Status.NOT_ALLOW_TO_DELETE_DEFAULT_ALARM_GROUP, + () -> alertGroupService.deleteAlertGroupById(user, 1)); // not allowed2 - Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_GROUP, + when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_GROUP, new Object[]{2}, 0, baseServiceLogger)).thenReturn(true); - result = alertGroupService.delAlertgroupById(user, 2); - logger.info(result.toString()); - Assertions.assertEquals(Status.NOT_ALLOW_TO_DELETE_DEFAULT_ALARM_GROUP, result.get(Constants.STATUS)); + assertThrowsServiceException(Status.NOT_ALLOW_TO_DELETE_DEFAULT_ALARM_GROUP, + () -> alertGroupService.deleteAlertGroupById(user, 2)); // success - Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_GROUP, - new Object[]{4}, 0, baseServiceLogger)).thenReturn(true); - Mockito.when(alertGroupMapper.selectById(4)).thenReturn(getEntity()); - result = alertGroupService.delAlertgroupById(user, 4); - logger.info(result.toString()); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); + when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_GROUP, new Object[]{4}, 0, + baseServiceLogger)).thenReturn(true); + when(alertGroupMapper.selectById(4)).thenReturn(getEntity()); + assertDoesNotThrow(() -> alertGroupService.deleteAlertGroupById(user, 4)); } @Test @@ -270,7 +249,7 @@ public void testVerifyGroupName() { // group name not exist boolean result = alertGroupService.existGroupName(groupName); Assertions.assertFalse(result); - Mockito.when(alertGroupMapper.existGroupName(groupName)).thenReturn(true); + when(alertGroupMapper.existGroupName(groupName)).thenReturn(true); // group name exist result = alertGroupService.existGroupName(groupName); diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertPluginInstanceServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertPluginInstanceServiceTest.java index a91d2da37f4c8..421a7d03614e5 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertPluginInstanceServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertPluginInstanceServiceTest.java @@ -17,15 +17,17 @@ package org.apache.dolphinscheduler.api.service; +import static org.apache.dolphinscheduler.api.AssertionsHelper.assertThrowsServiceException; import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.ALART_INSTANCE_CREATE; import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.ALERT_PLUGIN_DELETE; import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.ALERT_PLUGIN_UPDATE; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Mockito.when; import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.permission.ResourcePermissionCheckService; import org.apache.dolphinscheduler.api.service.impl.AlertPluginInstanceServiceImpl; import org.apache.dolphinscheduler.api.service.impl.BaseServiceImpl; -import org.apache.dolphinscheduler.common.constants.Constants; import org.apache.dolphinscheduler.common.enums.AlertPluginInstanceType; import org.apache.dolphinscheduler.common.enums.AuthorizationType; import org.apache.dolphinscheduler.common.enums.UserType; @@ -42,7 +44,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.Map; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; @@ -176,63 +177,63 @@ public void before() { @Test public void testCreate() { - Mockito.when(alertPluginInstanceMapper.existInstanceName("test")).thenReturn(true); - Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ALERT_PLUGIN_INSTANCE, + when(alertPluginInstanceMapper.existInstanceName("test")).thenReturn(true); + when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ALERT_PLUGIN_INSTANCE, 1, ALART_INSTANCE_CREATE, baseServiceLogger)).thenReturn(true); - Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_PLUGIN_INSTANCE, + when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_PLUGIN_INSTANCE, null, 0, baseServiceLogger)).thenReturn(true); - Map result = - alertPluginInstanceService.create(user, 1, "test", normalInstanceType, warningType, uiParams); - Assertions.assertEquals(Status.PLUGIN_INSTANCE_ALREADY_EXISTS, result.get(Constants.STATUS)); - Mockito.when(alertPluginInstanceMapper.insert(Mockito.any())).thenReturn(1); - result = alertPluginInstanceService.create(user, 1, "test1", normalInstanceType, warningType, uiParams); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); - Assertions.assertNotNull(result.get(Constants.DATA_LIST)); + assertThrowsServiceException(Status.PLUGIN_INSTANCE_ALREADY_EXISTS, + () -> alertPluginInstanceService.create(user, 1, "test", normalInstanceType, warningType, uiParams)); + when(alertPluginInstanceMapper.insert(Mockito.any())).thenReturn(1); + AlertPluginInstance alertPluginInstance = + alertPluginInstanceService.create(user, 1, "test1", normalInstanceType, warningType, uiParams); + assertNotNull(alertPluginInstance); } @Test public void testDelete() { List ids = Arrays.asList("11,2,3", "5,96", null, "98,1"); - Mockito.when(alertGroupMapper.queryInstanceIdsList()).thenReturn(ids); - Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ALERT_PLUGIN_INSTANCE, + when(alertGroupMapper.queryInstanceIdsList()).thenReturn(ids); + when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ALERT_PLUGIN_INSTANCE, 1, ALERT_PLUGIN_DELETE, baseServiceLogger)).thenReturn(true); - Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_PLUGIN_INSTANCE, + when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_PLUGIN_INSTANCE, null, 0, baseServiceLogger)).thenReturn(true); AlertPluginInstance normalInstanceWithId1 = getAlertPluginInstance(1, normalInstanceType, "test1"); AlertPluginInstance normalInstanceWithId9 = getAlertPluginInstance(9, normalInstanceType, "test9"); AlertPluginInstance globalInstanceWithId5 = getAlertPluginInstance(5, globalInstanceType, "test5"); - Mockito.when(alertPluginInstanceMapper.selectById(1)).thenReturn(normalInstanceWithId1); - Mockito.when(alertPluginInstanceMapper.selectById(9)).thenReturn(normalInstanceWithId9); - Mockito.when(alertPluginInstanceMapper.selectById(5)).thenReturn(globalInstanceWithId5); + when(alertPluginInstanceMapper.selectById(1)).thenReturn(normalInstanceWithId1); + when(alertPluginInstanceMapper.selectById(9)).thenReturn(normalInstanceWithId9); + when(alertPluginInstanceMapper.selectById(5)).thenReturn(globalInstanceWithId5); AlertGroup globalAlertGroup = new AlertGroup(); globalAlertGroup.setId(2); globalAlertGroup.setAlertInstanceIds("5,96"); - Mockito.when(alertGroupMapper.selectById(2)).thenReturn(globalAlertGroup); - Mockito.when(alertGroupMapper.updateById(Mockito.any())).thenReturn(1); - Map result; - result = alertPluginInstanceService.delete(user, 1); - Assertions.assertEquals(Status.DELETE_ALERT_PLUGIN_INSTANCE_ERROR_HAS_ALERT_GROUP_ASSOCIATED, - result.get(Constants.STATUS)); - Mockito.when(alertPluginInstanceMapper.deleteById(9)).thenReturn(1); - result = alertPluginInstanceService.delete(user, 9); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); - Mockito.when(alertPluginInstanceMapper.deleteById(5)).thenReturn(1); - result = alertPluginInstanceService.delete(user, 5); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); + when(alertGroupMapper.selectById(2)).thenReturn(globalAlertGroup); + when(alertGroupMapper.updateById(Mockito.any())).thenReturn(1); + + assertThrowsServiceException(Status.DELETE_ALERT_PLUGIN_INSTANCE_ERROR_HAS_ALERT_GROUP_ASSOCIATED, + () -> alertPluginInstanceService.deleteById(user, 1)); + + when(alertPluginInstanceMapper.deleteById(9)).thenReturn(1); + Assertions.assertDoesNotThrow(() -> alertPluginInstanceService.deleteById(user, 9)); + + when(alertPluginInstanceMapper.deleteById(5)).thenReturn(1); + Assertions.assertDoesNotThrow(() -> alertPluginInstanceService.deleteById(user, 5)); } @Test public void testUpdate() { - Mockito.when(alertPluginInstanceMapper.updateById(Mockito.any())).thenReturn(0); - Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ALERT_PLUGIN_INSTANCE, - 1, ALERT_PLUGIN_UPDATE, baseServiceLogger)).thenReturn(true); - Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_PLUGIN_INSTANCE, - null, 0, baseServiceLogger)).thenReturn(true); - Map result = alertPluginInstanceService.update(user, 1, "testUpdate", warningType, uiParams); - Assertions.assertEquals(Status.SAVE_ERROR, result.get(Constants.STATUS)); - Mockito.when(alertPluginInstanceMapper.updateById(Mockito.any())).thenReturn(1); - result = alertPluginInstanceService.update(user, 1, "testUpdate", warningType, uiParams); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); + when(alertPluginInstanceMapper.updateById(Mockito.any())).thenReturn(0); + when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ALERT_PLUGIN_INSTANCE, 1, + ALERT_PLUGIN_UPDATE, baseServiceLogger)).thenReturn(true); + when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ALERT_PLUGIN_INSTANCE, null, 0, + baseServiceLogger)).thenReturn(true); + assertThrowsServiceException(Status.SAVE_ERROR, + () -> alertPluginInstanceService.updateById(user, 1, "testUpdate", warningType, uiParams)); + + when(alertPluginInstanceMapper.updateById(Mockito.any())).thenReturn(1); + AlertPluginInstance alertPluginInstance = + alertPluginInstanceService.updateById(user, 1, "testUpdate", warningType, uiParams); + Assertions.assertNotNull(alertPluginInstance); } @Test @@ -242,10 +243,9 @@ public void testQueryAll() { pluginDefine.setId(1); List pluginDefines = Collections.singletonList(pluginDefine); List pluginInstanceList = Collections.singletonList(alertPluginInstance); - Mockito.when(alertPluginInstanceMapper.queryAllAlertPluginInstanceList()).thenReturn(pluginInstanceList); - Mockito.when(pluginDefineMapper.queryAllPluginDefineList()).thenReturn(pluginDefines); - Map result = alertPluginInstanceService.queryAll(); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); + when(alertPluginInstanceMapper.queryAllAlertPluginInstanceList()).thenReturn(pluginInstanceList); + when(pluginDefineMapper.queryAllPluginDefineList()).thenReturn(pluginDefines); + Assertions.assertDoesNotThrow(() -> alertPluginInstanceService.queryAll()); } private AlertPluginInstance getAlertPluginInstance(int id, AlertPluginInstanceType instanceType, diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AuditServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AuditServiceTest.java index 64ca0e72c2c20..3bde4a1b8d0c4 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AuditServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AuditServiceTest.java @@ -20,9 +20,7 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; -import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.service.impl.AuditServiceImpl; -import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.utils.DateUtils; import org.apache.dolphinscheduler.dao.entity.AuditLog; import org.apache.dolphinscheduler.dao.entity.User; @@ -67,13 +65,19 @@ public void testQueryLogListPaging() { IPage page = new Page<>(1, 10); page.setRecords(getLists()); page.setTotal(1L); - when(auditLogMapper.queryAuditLog(Mockito.any(Page.class), Mockito.any(), Mockito.any(), - Mockito.eq(""), eq(start), eq(end))) - .thenReturn(page); - Result result = auditService.queryLogListPaging(new User(), null, null, "2020-11-01 00:00:00", - "2020-11-02 00:00:00", "", 1, 10); - logger.info(result.toString()); - Assertions.assertEquals(Status.SUCCESS.getCode(), (int) result.getCode()); + when(auditLogMapper.queryAuditLog(Mockito.any(Page.class), Mockito.any(), Mockito.any(), Mockito.eq(""), + eq(start), eq(end))).thenReturn(page); + Assertions.assertDoesNotThrow(() -> { + auditService.queryLogListPaging( + new User(), + null, + null, + "2020-11-01 00:00:00", + "2020-11-02 00:00:00", + "", + 1, + 10); + }); } private List getLists() { diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ClusterServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ClusterServiceTest.java deleted file mode 100644 index deb04f41995d1..0000000000000 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ClusterServiceTest.java +++ /dev/null @@ -1,292 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.dolphinscheduler.api.service; - -import org.apache.dolphinscheduler.api.enums.Status; -import org.apache.dolphinscheduler.api.k8s.K8sManager; -import org.apache.dolphinscheduler.api.service.impl.ClusterServiceImpl; -import org.apache.dolphinscheduler.api.utils.PageInfo; -import org.apache.dolphinscheduler.api.utils.Result; -import org.apache.dolphinscheduler.common.constants.Constants; -import org.apache.dolphinscheduler.common.enums.UserType; -import org.apache.dolphinscheduler.dao.entity.Cluster; -import org.apache.dolphinscheduler.dao.entity.User; -import org.apache.dolphinscheduler.dao.mapper.ClusterMapper; -import org.apache.dolphinscheduler.dao.mapper.K8sNamespaceMapper; - -import org.apache.commons.collections4.CollectionUtils; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import org.assertj.core.util.Lists; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.junit.jupiter.MockitoExtension; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.baomidou.mybatisplus.core.metadata.IPage; -import com.baomidou.mybatisplus.extension.plugins.pagination.Page; - -/** - * cluster service test - */ -@ExtendWith(MockitoExtension.class) -public class ClusterServiceTest { - - public static final Logger logger = LoggerFactory.getLogger(ClusterServiceTest.class); - - @InjectMocks - private ClusterServiceImpl clusterService; - - @Mock - private ClusterMapper clusterMapper; - - @Mock - private K8sNamespaceMapper k8sNamespaceMapper; - - @Mock - private K8sManager k8sManager; - - public static final String testUserName = "clusterServerTest"; - - public static final String clusterName = "Env1"; - - @BeforeEach - public void setUp() { - } - - @AfterEach - public void after() { - } - - @Test - public void testCreateCluster() { - User loginUser = getGeneralUser(); - Map result = clusterService.createCluster(loginUser, clusterName, getConfig(), getDesc()); - logger.info(result.toString()); - Assertions.assertEquals(Status.USER_NO_OPERATION_PERM, result.get(Constants.STATUS)); - - loginUser = getAdminUser(); - result = clusterService.createCluster(loginUser, clusterName, "", getDesc()); - logger.info(result.toString()); - Assertions.assertEquals(Status.CLUSTER_CONFIG_IS_NULL, result.get(Constants.STATUS)); - - result = clusterService.createCluster(loginUser, "", getConfig(), getDesc()); - logger.info(result.toString()); - Assertions.assertEquals(Status.CLUSTER_NAME_IS_NULL, result.get(Constants.STATUS)); - - Mockito.when(clusterMapper.queryByClusterName(clusterName)).thenReturn(getCluster()); - result = clusterService.createCluster(loginUser, clusterName, getConfig(), getDesc()); - logger.info(result.toString()); - Assertions.assertEquals(Status.CLUSTER_NAME_EXISTS, result.get(Constants.STATUS)); - - Mockito.when(clusterMapper.insert(Mockito.any(Cluster.class))).thenReturn(1); - result = clusterService.createCluster(loginUser, "testName", "testConfig", "testDesc"); - logger.info(result.toString()); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); - } - - @Test - public void testCheckParams() { - Map result = clusterService.checkParams(clusterName, getConfig()); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); - result = clusterService.checkParams("", getConfig()); - Assertions.assertEquals(Status.CLUSTER_NAME_IS_NULL, result.get(Constants.STATUS)); - result = clusterService.checkParams(clusterName, ""); - Assertions.assertEquals(Status.CLUSTER_CONFIG_IS_NULL, result.get(Constants.STATUS)); - } - - @Test - public void testUpdateClusterByCode() { - User loginUser = getGeneralUser(); - Map result = - clusterService.updateClusterByCode(loginUser, 1L, clusterName, getConfig(), getDesc()); - logger.info(result.toString()); - Assertions.assertEquals(Status.USER_NO_OPERATION_PERM, result.get(Constants.STATUS)); - - loginUser = getAdminUser(); - result = clusterService.updateClusterByCode(loginUser, 1L, clusterName, "", getDesc()); - logger.info(result.toString()); - Assertions.assertEquals(Status.CLUSTER_CONFIG_IS_NULL, result.get(Constants.STATUS)); - - result = clusterService.updateClusterByCode(loginUser, 1L, "", getConfig(), getDesc()); - logger.info(result.toString()); - Assertions.assertEquals(Status.CLUSTER_NAME_IS_NULL, result.get(Constants.STATUS)); - - result = clusterService.updateClusterByCode(loginUser, 2L, clusterName, getConfig(), getDesc()); - logger.info(result.toString()); - Assertions.assertEquals(Status.CLUSTER_NOT_EXISTS, result.get(Constants.STATUS)); - - Mockito.when(clusterMapper.queryByClusterName(clusterName)).thenReturn(getCluster()); - result = clusterService.updateClusterByCode(loginUser, 2L, clusterName, getConfig(), getDesc()); - logger.info(result.toString()); - Assertions.assertEquals(Status.CLUSTER_NAME_EXISTS, result.get(Constants.STATUS)); - - Mockito.when(clusterMapper.updateById(Mockito.any(Cluster.class))).thenReturn(1); - Mockito.when(clusterMapper.queryByClusterCode(1L)).thenReturn(getCluster()); - - result = clusterService.updateClusterByCode(loginUser, 1L, "testName", getConfig(), "test"); - logger.info(result.toString()); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); - - } - - @Test - public void testQueryAllClusterList() { - Mockito.when(clusterMapper.queryAllClusterList()).thenReturn(Lists.newArrayList(getCluster())); - Map result = clusterService.queryAllClusterList(); - logger.info(result.toString()); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); - - List list = (List) (result.get(Constants.DATA_LIST)); - Assertions.assertEquals(1, list.size()); - } - - @Test - public void testQueryClusterListPaging() { - IPage page = new Page<>(1, 10); - page.setRecords(getList()); - page.setTotal(1L); - Mockito.when(clusterMapper.queryClusterListPaging(Mockito.any(Page.class), Mockito.eq(clusterName))) - .thenReturn(page); - - Result result = clusterService.queryClusterListPaging(1, 10, clusterName); - logger.info(result.toString()); - PageInfo pageInfo = (PageInfo) result.getData(); - Assertions.assertTrue(CollectionUtils.isNotEmpty(pageInfo.getTotalList())); - } - - @Test - public void testQueryClusterByName() { - Mockito.when(clusterMapper.queryByClusterName(clusterName)).thenReturn(null); - Map result = clusterService.queryClusterByName(clusterName); - logger.info(result.toString()); - Assertions.assertEquals(Status.QUERY_CLUSTER_BY_NAME_ERROR, result.get(Constants.STATUS)); - - Mockito.when(clusterMapper.queryByClusterName(clusterName)).thenReturn(getCluster()); - result = clusterService.queryClusterByName(clusterName); - logger.info(result.toString()); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); - } - - @Test - public void testQueryClusterByCode() { - Mockito.when(clusterMapper.queryByClusterCode(1L)).thenReturn(null); - Map result = clusterService.queryClusterByCode(1L); - logger.info(result.toString()); - Assertions.assertEquals(Status.QUERY_CLUSTER_BY_CODE_ERROR, result.get(Constants.STATUS)); - - Mockito.when(clusterMapper.queryByClusterCode(1L)).thenReturn(getCluster()); - result = clusterService.queryClusterByCode(1L); - logger.info(result.toString()); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); - } - - @Test - public void testDeleteClusterByCode() { - User loginUser = getGeneralUser(); - Map result = clusterService.deleteClusterByCode(loginUser, 1L); - logger.info(result.toString()); - Assertions.assertEquals(Status.USER_NO_OPERATION_PERM, result.get(Constants.STATUS)); - - loginUser = getAdminUser(); - Mockito.when(clusterMapper.deleteByCode(1L)).thenReturn(1); - result = clusterService.deleteClusterByCode(loginUser, 1L); - logger.info(result.toString()); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); - - Mockito.when(k8sNamespaceMapper.selectCount(Mockito.any())).thenReturn(1L); - result = clusterService.deleteClusterByCode(loginUser, 1L); - logger.info(result.toString()); - Assertions.assertEquals(Status.DELETE_CLUSTER_RELATED_NAMESPACE_EXISTS, result.get(Constants.STATUS)); - } - - @Test - public void testVerifyCluster() { - Map result = clusterService.verifyCluster(""); - logger.info(result.toString()); - Assertions.assertEquals(Status.CLUSTER_NAME_IS_NULL, result.get(Constants.STATUS)); - - Mockito.when(clusterMapper.queryByClusterName(clusterName)).thenReturn(getCluster()); - result = clusterService.verifyCluster(clusterName); - logger.info(result.toString()); - Assertions.assertEquals(Status.CLUSTER_NAME_EXISTS, result.get(Constants.STATUS)); - } - - private Cluster getCluster() { - Cluster cluster = new Cluster(); - cluster.setId(1); - cluster.setCode(1L); - cluster.setName(clusterName); - cluster.setConfig(getConfig()); - cluster.setDescription(getDesc()); - cluster.setOperator(1); - return cluster; - } - - /** - * create an cluster description - */ - private String getDesc() { - return "create an cluster to test "; - } - - /** - * create an cluster config - */ - private String getConfig() { - return "{\"k8s\":\"apiVersion: v1\\nclusters:\\n- cluster:\\n certificate-authority-data: LS0tLS1CRUdJTiBDRJUSUZJQ0FURS0tLS0tCg==\\n server: https:\\/\\/127.0.0.1:6443\\n name: kubernetes\\ncontexts:\\n- context:\\n cluster: kubernetes\\n user: kubernetes-admin\\n name: kubernetes-admin@kubernetes\\ncurrent-context: kubernetes-admin@kubernetes\\nkind: Config\\npreferences: {}\\nusers:\\n- name: kubernetes-admin\\n user:\\n client-certificate-data: LS0tLS1CRUdJTiBDRVJJ0cEhYYnBLRVktLS0tLQo=\"}\n"; - } - - /** - * create general user - */ - private User getGeneralUser() { - User loginUser = new User(); - loginUser.setUserType(UserType.GENERAL_USER); - loginUser.setUserName(testUserName); - loginUser.setId(1); - return loginUser; - } - - /** - * create admin user - */ - private User getAdminUser() { - User loginUser = new User(); - loginUser.setUserType(UserType.ADMIN_USER); - loginUser.setUserName(testUserName); - loginUser.setId(1); - return loginUser; - } - - private List getList() { - List list = new ArrayList<>(); - list.add(getCluster()); - return list; - } -} diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataAnalysisServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataAnalysisServiceTest.java index de25d0154f82e..1d15ce6107ed1 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataAnalysisServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataAnalysisServiceTest.java @@ -17,12 +17,19 @@ package org.apache.dolphinscheduler.api.service; +import static org.apache.dolphinscheduler.api.AssertionsHelper.assertDoesNotThrow; +import static org.apache.dolphinscheduler.api.AssertionsHelper.assertThrowsServiceException; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.when; import org.apache.dolphinscheduler.api.dto.CommandStateCount; +import org.apache.dolphinscheduler.api.dto.TaskCountDto; import org.apache.dolphinscheduler.api.enums.Status; +import org.apache.dolphinscheduler.api.exceptions.ServiceException; import org.apache.dolphinscheduler.api.permission.ResourcePermissionCheckService; import org.apache.dolphinscheduler.api.service.impl.BaseServiceImpl; import org.apache.dolphinscheduler.api.service.impl.DataAnalysisServiceImpl; @@ -52,13 +59,11 @@ import java.util.Set; import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; @@ -117,7 +122,7 @@ public void setUp() { project.setName("test"); resultMap = new HashMap<>(); - Mockito.when(projectMapper.queryByCode(1L)).thenReturn(project); + when(projectMapper.queryByCode(1L)).thenReturn(project); } @AfterEach @@ -132,14 +137,11 @@ public void testCountTaskStateByProject_success() { String startDate = "2020-02-11 16:02:18"; String endDate = "2020-02-11 16:03:18"; - Map result = new HashMap<>(); - putMsg(result, Status.SUCCESS, null); - Mockito.when(projectService.checkProjectAndAuth(any(), any(), anyLong(), any())).thenReturn(result); - Mockito.when(projectMapper.queryByCode(1L)).thenReturn(getProject("test")); + doNothing().when(projectService).checkProjectAndAuthThrowException(any(), anyLong(), any()); + when(projectMapper.queryByCode(1L)).thenReturn(getProject("test")); // SUCCESS - result = dataAnalysisServiceImpl.countTaskStateByProject(user, 1, startDate, endDate); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); + assertDoesNotThrow(() -> dataAnalysisServiceImpl.countTaskStateByProject(user, 1, startDate, endDate)); } @Test @@ -148,76 +150,66 @@ public void testCountTaskStateByProject_projectNotFound() { String endDate = "2020-02-11 16:03:18"; // checkProject false - Map failResult = new HashMap<>(); - putMsg(failResult, Status.PROJECT_NOT_FOUND, 1); - Mockito.when(projectService.checkProjectAndAuth(any(), any(), anyLong(), any())).thenReturn(failResult); - failResult = dataAnalysisServiceImpl.countTaskStateByProject(user, 1, startDate, endDate); - Assertions.assertEquals(Status.PROJECT_NOT_FOUND, failResult.get(Constants.STATUS)); + doThrow(new ServiceException(Status.PROJECT_NOT_FOUND, 1)).when(projectService) + .checkProjectAndAuthThrowException(any(), anyLong(), any()); + assertThrowsServiceException(Status.PROJECT_NOT_FOUND, + () -> dataAnalysisServiceImpl.countTaskStateByProject(user, 1, startDate, endDate)); } @Test public void testCountTaskStateByProject_paramValid() { - Map result = new HashMap<>(); - putMsg(result, Status.SUCCESS, null); - Mockito.when(projectService.checkProjectAndAuth(any(), any(), anyLong(), any())).thenReturn(result); - Mockito.when(projectMapper.queryByCode(1L)).thenReturn(getProject("test")); + doNothing().when(projectService).checkProjectAndAuthThrowException(any(), anyLong(), any()); + when(projectMapper.queryByCode(1L)).thenReturn(getProject("test")); // when date in illegal format then return error message String startDate2 = "illegalDateString"; String endDate2 = "illegalDateString"; - result = dataAnalysisServiceImpl.countTaskStateByProject(user, 1, startDate2, endDate2); - Assertions.assertEquals(Status.REQUEST_PARAMS_NOT_VALID_ERROR, result.get(Constants.STATUS)); + assertThrowsServiceException(Status.REQUEST_PARAMS_NOT_VALID_ERROR, + () -> dataAnalysisServiceImpl.countTaskStateByProject(user, 1, startDate2, endDate2)); // when one of date in illegal format then return error message String startDate3 = "2020-08-28 14:13:40"; String endDate3 = "illegalDateString"; - result = dataAnalysisServiceImpl.countTaskStateByProject(user, 1, startDate3, endDate3); - Assertions.assertEquals(Status.REQUEST_PARAMS_NOT_VALID_ERROR, result.get(Constants.STATUS)); + assertThrowsServiceException(Status.REQUEST_PARAMS_NOT_VALID_ERROR, + () -> dataAnalysisServiceImpl.countTaskStateByProject(user, 1, startDate3, endDate3)); // when one of date in illegal format then return error message String startDate4 = "illegalDateString"; String endDate4 = "2020-08-28 14:13:40"; - result = dataAnalysisServiceImpl.countTaskStateByProject(user, 1, startDate4, endDate4); - Assertions.assertEquals(Status.REQUEST_PARAMS_NOT_VALID_ERROR, result.get(Constants.STATUS)); + assertThrowsServiceException(Status.REQUEST_PARAMS_NOT_VALID_ERROR, + () -> dataAnalysisServiceImpl.countTaskStateByProject(user, 1, startDate4, endDate4)); } @Test public void testCountTaskStateByProject_allCountZero() { - Map result = new HashMap<>(); - putMsg(result, Status.SUCCESS, null); - Mockito.when(projectService.checkProjectAndAuth(any(), any(), anyLong(), any())).thenReturn(result); - Mockito.when(projectMapper.queryByCode(1L)).thenReturn(getProject("test")); + doNothing().when(projectService).checkProjectAndAuthThrowException(any(), anyLong(), any()); + when(projectMapper.queryByCode(1L)).thenReturn(getProject("test")); // when general user doesn't have any task then return all count are 0 user.setUserType(UserType.GENERAL_USER); - Mockito.when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, 1, - serviceLogger)) - .thenReturn(projectIds()); - Mockito.when(taskInstanceMapper.countTaskInstanceStateByProjectCodes(any(), any(), any())).thenReturn( - Collections.emptyList()); - result = dataAnalysisServiceImpl.countTaskStateByProject(user, 1, null, null); - assertThat(result.get(Constants.DATA_LIST)).extracting("totalCount").isEqualTo(0); - assertThat(result.get(Constants.DATA_LIST)).extracting("taskCountDtos").asList().hasSameSizeAs( - TaskExecutionStatus.values()); - assertThat(result.get(Constants.DATA_LIST)).extracting("taskCountDtos").asList().extracting( - "count").allMatch(count -> count.equals(0)); + when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, 1, + serviceLogger)).thenReturn(projectIds()); + when(taskInstanceMapper.countTaskInstanceStateByProjectCodes(any(), any(), any())) + .thenReturn(Collections.emptyList()); + TaskCountDto taskCountDto = dataAnalysisServiceImpl.countTaskStateByProject(user, 1, null, null); + assertThat(taskCountDto).extracting("totalCount").isEqualTo(0); + assertThat(taskCountDto).extracting("taskCountDtos").asList().hasSameSizeAs(TaskExecutionStatus.values()); + assertThat(taskCountDto).extracting("taskCountDtos").asList().extracting("count") + .allMatch(count -> count.equals(0)); } @Test public void testCountTaskStateByProject_noData() { - Map result = new HashMap<>(); - putMsg(result, Status.SUCCESS, null); - Mockito.when(projectService.checkProjectAndAuth(any(), any(), anyLong(), any())).thenReturn(result); - Mockito.when(projectMapper.queryByCode(1L)).thenReturn(getProject("test")); - Mockito.when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, 1, + doNothing().when(projectService).checkProjectAndAuthThrowException(any(), anyLong(), any()); + when(projectMapper.queryByCode(1L)).thenReturn(getProject("test")); + when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, 1, serviceLogger)).thenReturn(projectIds()); // when instanceStateCounter return null, then return nothing user.setUserType(UserType.GENERAL_USER); - Mockito.when(taskInstanceMapper.countTaskInstanceStateByProjectCodes(any(), any(), any())) - .thenReturn(null); - result = dataAnalysisServiceImpl.countTaskStateByProject(user, 1, null, null); - Assertions.assertNull(result.get(Constants.DATA_LIST)); + when(taskInstanceMapper.countTaskInstanceStateByProjectCodes(any(), any(), any())).thenReturn(null); + TaskCountDto taskCountDto = dataAnalysisServiceImpl.countTaskStateByProject(user, 1, null, null); + assertThat(taskCountDto).isNull(); } @Test @@ -225,39 +217,31 @@ public void testCountProcessInstanceStateByProject() { String startDate = "2020-02-11 16:02:18"; String endDate = "2020-02-11 16:03:18"; - Mockito.when(projectMapper.queryByCode(1L)).thenReturn(getProject("test")); + when(projectMapper.queryByCode(1L)).thenReturn(getProject("test")); // checkProject false - Map failResult = new HashMap<>(); - putMsg(failResult, Status.PROJECT_NOT_FOUND, 1); - Mockito.when(projectService.checkProjectAndAuth(any(), any(), anyLong(), any())).thenReturn(failResult); - failResult = dataAnalysisServiceImpl.countProcessInstanceStateByProject(user, 1, startDate, endDate); - Assertions.assertEquals(Status.PROJECT_NOT_FOUND, failResult.get(Constants.STATUS)); + doThrow(new ServiceException(Status.PROJECT_NOT_FOUND, 1)).when(projectService) + .checkProjectAndAuthThrowException(any(), anyLong(), any()); + assertThrowsServiceException(Status.PROJECT_NOT_FOUND, + () -> dataAnalysisServiceImpl.countProcessInstanceStateByProject(user, 1, startDate, endDate)); - Map result = new HashMap<>(); - putMsg(result, Status.SUCCESS, null); - Mockito.when(projectService.checkProjectAndAuth(any(), any(), anyLong(), any())).thenReturn(result); + doNothing().when(projectService).checkProjectAndAuthThrowException(any(), anyLong(), any()); // SUCCESS - result = dataAnalysisServiceImpl.countProcessInstanceStateByProject(user, 1, startDate, endDate); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); + assertDoesNotThrow( + () -> dataAnalysisServiceImpl.countProcessInstanceStateByProject(user, 1, startDate, endDate)); } @Test public void testCountDefinitionByUser() { - Map result = new HashMap<>(); - putMsg(result, Status.SUCCESS, null); - - Mockito.when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, 1, + when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, 1, serviceLogger)).thenReturn(projectIds()); - result = dataAnalysisServiceImpl.countDefinitionByUser(user, 0); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); + assertDoesNotThrow(() -> dataAnalysisServiceImpl.countDefinitionByUser(user, 0)); - Mockito.when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, 1, + when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, 1, serviceLogger)).thenReturn(Collections.emptySet()); - result = dataAnalysisServiceImpl.countDefinitionByUser(user, 0); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); + assertDoesNotThrow(() -> dataAnalysisServiceImpl.countDefinitionByUser(user, 0)); } @Test @@ -266,21 +250,17 @@ public void testCountCommandState() { user.setUserType(UserType.ADMIN_USER); user.setId(1); - Map result = dataAnalysisServiceImpl.countCommandState(user); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); + assertDoesNotThrow(() -> dataAnalysisServiceImpl.countCommandState(user)); // when no command found then return all count are 0 - Mockito.when(commandMapper.countCommandState(any(), any(), any())).thenReturn(Collections.emptyList()); - Mockito.when(errorCommandMapper.countCommandState(any(), any(), any())).thenReturn(Collections.emptyList()); - Mockito.when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, 1, + when(commandMapper.countCommandState(any(), any(), any())).thenReturn(Collections.emptyList()); + when(errorCommandMapper.countCommandState(any(), any(), any())).thenReturn(Collections.emptyList()); + when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, 1, serviceLogger)).thenReturn(projectIds()); - Map result5 = dataAnalysisServiceImpl.countCommandState(user); - assertThat(result5).containsEntry(Constants.STATUS, Status.SUCCESS); - assertThat(result5.get(Constants.DATA_LIST)).asList().extracting("errorCount") - .allMatch(count -> count.equals(0)); - assertThat(result5.get(Constants.DATA_LIST)).asList().extracting("normalCount") - .allMatch(count -> count.equals(0)); + List commandStateCounts = dataAnalysisServiceImpl.countCommandState(user); + assertThat(commandStateCounts).asList().extracting("errorCount").allMatch(count -> count.equals(0)); + assertThat(commandStateCounts).asList().extracting("normalCount").allMatch(count -> count.equals(0)); // when command found then return combination result CommandCount normalCommandCount = new CommandCount(); @@ -289,26 +269,25 @@ public void testCountCommandState() { CommandCount errorCommandCount = new CommandCount(); errorCommandCount.setCommandType(CommandType.START_PROCESS); errorCommandCount.setCount(5); - Mockito.when(commandMapper.countCommandState(any(), any(), any())) + when(commandMapper.countCommandState(any(), any(), any())) .thenReturn(Collections.singletonList(normalCommandCount)); - Mockito.when(errorCommandMapper.countCommandState(any(), any(), any())) + when(errorCommandMapper.countCommandState(any(), any(), any())) .thenReturn(Collections.singletonList(errorCommandCount)); - Map result6 = dataAnalysisServiceImpl.countCommandState(user); + commandStateCounts = dataAnalysisServiceImpl.countCommandState(user); - assertThat(result6).containsEntry(Constants.STATUS, Status.SUCCESS); CommandStateCount commandStateCount = new CommandStateCount(); commandStateCount.setCommandState(CommandType.START_PROCESS); commandStateCount.setNormalCount(10); commandStateCount.setErrorCount(5); - assertThat(result6.get(Constants.DATA_LIST)).asList().containsOnlyOnce(commandStateCount); + assertThat(commandStateCounts).asList().containsOnlyOnce(commandStateCount); } @Test public void testCountQueueState() { // when project check success when return all count are 0 - Map result2 = dataAnalysisServiceImpl.countQueueState(user); - assertThat(result2.get(Constants.DATA_LIST)).extracting("taskQueue", "taskKill") + Map stringIntegerMap = dataAnalysisServiceImpl.countQueueState(user); + assertThat(stringIntegerMap).extracting("taskQueue", "taskKill") .isNotEmpty() .allMatch(count -> count.equals(0)); } diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java index 411979542b949..68d4db0ff2ade 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java @@ -17,14 +17,16 @@ package org.apache.dolphinscheduler.api.service; +import static org.apache.dolphinscheduler.api.AssertionsHelper.assertDoesNotThrow; +import static org.apache.dolphinscheduler.api.AssertionsHelper.assertThrowsServiceException; import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.DATASOURCE; +import static org.mockito.Mockito.when; import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.permission.ResourcePermissionCheckService; import org.apache.dolphinscheduler.api.service.impl.BaseServiceImpl; 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.common.constants.DataSourceConstants; import org.apache.dolphinscheduler.common.enums.AuthorizationType; import org.apache.dolphinscheduler.common.enums.UserType; @@ -95,9 +97,9 @@ public class DataSourceServiceTest { private ResourcePermissionCheckService resourcePermissionCheckService; private void passResourcePermissionCheckService() { - Mockito.when(resourcePermissionCheckService.operationPermissionCheck(Mockito.any(), Mockito.anyInt(), + when(resourcePermissionCheckService.operationPermissionCheck(Mockito.any(), Mockito.anyInt(), Mockito.anyString(), Mockito.any())).thenReturn(true); - Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(Mockito.any(), Mockito.any(), + when(resourcePermissionCheckService.resourcePermissionCheck(Mockito.any(), Mockito.any(), Mockito.anyInt(), Mockito.any())).thenReturn(true); } @@ -118,28 +120,28 @@ public void createDataSourceTest() throws ExecutionException { postgreSqlDatasourceParam.setName(dataSourceName); // USER_NO_OPERATION_PERM - Result result = dataSourceService.createDataSource(loginUser, postgreSqlDatasourceParam); - Assertions.assertEquals(Status.USER_NO_OPERATION_PERM.getCode(), result.getCode().intValue()); + assertThrowsServiceException(Status.USER_NO_OPERATION_PERM, + () -> dataSourceService.createDataSource(loginUser, postgreSqlDatasourceParam)); // DATASOURCE_EXIST List dataSourceList = new ArrayList<>(); DataSource dataSource = new DataSource(); dataSource.setName(dataSourceName); dataSourceList.add(dataSource); - Mockito.when(dataSourceMapper.queryDataSourceByName(dataSourceName.trim())).thenReturn(dataSourceList); + when(dataSourceMapper.queryDataSourceByName(dataSourceName.trim())).thenReturn(dataSourceList); passResourcePermissionCheckService(); - Result dataSourceExitsResult = dataSourceService.createDataSource(loginUser, postgreSqlDatasourceParam); - Assertions.assertEquals(Status.DATASOURCE_EXIST.getCode(), dataSourceExitsResult.getCode().intValue()); + + assertThrowsServiceException(Status.DATASOURCE_EXIST, + () -> dataSourceService.createDataSource(loginUser, postgreSqlDatasourceParam)); try ( MockedStatic mockedStaticDataSourceClientProvider = Mockito.mockStatic(DataSourceClientProvider.class)) { - Mockito.when(dataSourceMapper.queryDataSourceByName(dataSourceName.trim())).thenReturn(null); + when(dataSourceMapper.queryDataSourceByName(dataSourceName.trim())).thenReturn(null); // SUCCESS - Result success = dataSourceService.createDataSource(loginUser, postgreSqlDatasourceParam); - Assertions.assertEquals(Status.SUCCESS.getCode(), success.getCode().intValue()); + assertDoesNotThrow(() -> dataSourceService.createDataSource(loginUser, postgreSqlDatasourceParam)); } } @@ -153,6 +155,7 @@ public void updateDataSourceTest() throws ExecutionException { String dataSourceUpdateName = "dataSource01-update"; PostgreSQLDataSourceParamDTO postgreSqlDatasourceParam = new PostgreSQLDataSourceParamDTO(); + postgreSqlDatasourceParam.setId(dataSourceId); postgreSqlDatasourceParam.setDatabase(dataSourceName); postgreSqlDatasourceParam.setNote(dataSourceDesc); postgreSqlDatasourceParam.setHost("172.16.133.200"); @@ -163,18 +166,16 @@ public void updateDataSourceTest() throws ExecutionException { postgreSqlDatasourceParam.setName(dataSourceUpdateName); // RESOURCE_NOT_EXIST - Mockito.when(dataSourceMapper.selectById(dataSourceId)).thenReturn(null); - Result resourceNotExits = - dataSourceService.updateDataSource(dataSourceId, loginUser, postgreSqlDatasourceParam); - Assertions.assertEquals(Status.RESOURCE_NOT_EXIST.getCode(), resourceNotExits.getCode().intValue()); + when(dataSourceMapper.selectById(dataSourceId)).thenReturn(null); + assertThrowsServiceException(Status.RESOURCE_NOT_EXIST, + () -> dataSourceService.updateDataSource(loginUser, postgreSqlDatasourceParam)); // USER_NO_OPERATION_PERM DataSource dataSource = new DataSource(); dataSource.setUserId(0); - Mockito.when(dataSourceMapper.selectById(dataSourceId)).thenReturn(dataSource); - Result userNoOperationPerm = - dataSourceService.updateDataSource(dataSourceId, loginUser, postgreSqlDatasourceParam); - Assertions.assertEquals(Status.USER_NO_OPERATION_PERM.getCode(), userNoOperationPerm.getCode().intValue()); + when(dataSourceMapper.selectById(dataSourceId)).thenReturn(dataSource); + assertThrowsServiceException(Status.USER_NO_OPERATION_PERM, + () -> dataSourceService.updateDataSource(loginUser, postgreSqlDatasourceParam)); // DATASOURCE_EXIST dataSource.setName(dataSourceName); @@ -186,23 +187,21 @@ public void updateDataSourceTest() throws ExecutionException { anotherDataSource.setName(dataSourceUpdateName); List dataSourceList = new ArrayList<>(); dataSourceList.add(anotherDataSource); - Mockito.when(dataSourceMapper.selectById(dataSourceId)).thenReturn(dataSource); - Mockito.when(dataSourceMapper.queryDataSourceByName(postgreSqlDatasourceParam.getName())) + when(dataSourceMapper.selectById(dataSourceId)).thenReturn(dataSource); + when(dataSourceMapper.queryDataSourceByName(postgreSqlDatasourceParam.getName())) .thenReturn(dataSourceList); passResourcePermissionCheckService(); - Result dataSourceNameExist = - dataSourceService.updateDataSource(dataSourceId, loginUser, postgreSqlDatasourceParam); - Assertions.assertEquals(Status.DATASOURCE_EXIST.getCode(), dataSourceNameExist.getCode().intValue()); + assertThrowsServiceException(Status.DATASOURCE_EXIST, + () -> dataSourceService.updateDataSource(loginUser, postgreSqlDatasourceParam)); try ( MockedStatic mockedStaticDataSourceClientProvider = Mockito.mockStatic(DataSourceClientProvider.class)) { // DATASOURCE_CONNECT_FAILED - Mockito.when(dataSourceMapper.queryDataSourceByName(postgreSqlDatasourceParam.getName())).thenReturn(null); + when(dataSourceMapper.queryDataSourceByName(postgreSqlDatasourceParam.getName())).thenReturn(null); // SUCCESS - Result success = dataSourceService.updateDataSource(dataSourceId, loginUser, postgreSqlDatasourceParam); - Assertions.assertEquals(Status.SUCCESS.getCode(), success.getCode().intValue()); + assertDoesNotThrow(() -> dataSourceService.updateDataSource(loginUser, postgreSqlDatasourceParam)); } } @@ -221,37 +220,34 @@ public void queryDataSourceListPagingTest() { @Test public void connectionTest() { int dataSourceId = -1; - Mockito.when(dataSourceMapper.selectById(dataSourceId)).thenReturn(null); - Result result = dataSourceService.connectionTest(dataSourceId); - Assertions.assertEquals(Status.RESOURCE_NOT_EXIST.getCode(), result.getCode().intValue()); + when(dataSourceMapper.selectById(dataSourceId)).thenReturn(null); + assertThrowsServiceException(Status.RESOURCE_NOT_EXIST, () -> dataSourceService.connectionTest(dataSourceId)); } @Test public void deleteTest() { User loginUser = getAdminUser(); int dataSourceId = 1; - Result result = new Result(); // resource not exist - dataSourceService.putMsg(result, Status.RESOURCE_NOT_EXIST); - Mockito.when(dataSourceMapper.selectById(dataSourceId)).thenReturn(null); - Assertions.assertEquals(result.getCode(), dataSourceService.delete(loginUser, dataSourceId).getCode()); + when(dataSourceMapper.selectById(dataSourceId)).thenReturn(null); + assertThrowsServiceException(Status.RESOURCE_NOT_EXIST, + () -> dataSourceService.delete(loginUser, dataSourceId)); // user no operation perm - dataSourceService.putMsg(result, Status.USER_NO_OPERATION_PERM); DataSource dataSource = new DataSource(); dataSource.setUserId(0); - Mockito.when(dataSourceMapper.selectById(dataSourceId)).thenReturn(dataSource); - Assertions.assertEquals(result.getCode(), dataSourceService.delete(loginUser, dataSourceId).getCode()); + when(dataSourceMapper.selectById(dataSourceId)).thenReturn(dataSource); + assertThrowsServiceException(Status.USER_NO_OPERATION_PERM, + () -> dataSourceService.delete(loginUser, dataSourceId)); // success - dataSourceService.putMsg(result, Status.SUCCESS); dataSource.setUserId(-1); loginUser.setUserType(UserType.ADMIN_USER); loginUser.setId(1); dataSource.setId(22); passResourcePermissionCheckService(); - Mockito.when(dataSourceMapper.selectById(dataSourceId)).thenReturn(dataSource); - Assertions.assertEquals(result.getCode(), dataSourceService.delete(loginUser, dataSourceId).getCode()); + when(dataSourceMapper.selectById(dataSourceId)).thenReturn(dataSource); + assertDoesNotThrow(() -> dataSourceService.delete(loginUser, dataSourceId)); } @@ -261,13 +257,13 @@ public void unauthDatasourceTest() { loginUser.setId(1); loginUser.setUserType(UserType.ADMIN_USER); int userId = 3; - Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.DATASOURCE, + when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.DATASOURCE, loginUser.getId(), null, baseServiceLogger)).thenReturn(true); - Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.DATASOURCE, null, 0, + when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.DATASOURCE, null, 0, baseServiceLogger)).thenReturn(true); // test admin user - Mockito.when(dataSourceMapper.queryAuthedDatasource(userId)).thenReturn(getSingleDataSourceList()); - Mockito.when(dataSourceMapper.queryDatasourceExceptUserId(userId)).thenReturn(getDataSourceList()); + when(dataSourceMapper.queryAuthedDatasource(userId)).thenReturn(getSingleDataSourceList()); + when(dataSourceMapper.queryDatasourceExceptUserId(userId)).thenReturn(getDataSourceList()); List dataSources = dataSourceService.unAuthDatasource(loginUser, userId); logger.info(dataSources.toString()); Assertions.assertTrue(CollectionUtils.isNotEmpty(dataSources)); @@ -275,7 +271,7 @@ public void unauthDatasourceTest() { // test non-admin user loginUser.setId(2); loginUser.setUserType(UserType.GENERAL_USER); - Mockito.when(dataSourceMapper.selectByMap(Collections.singletonMap("user_id", loginUser.getId()))) + when(dataSourceMapper.selectByMap(Collections.singletonMap("user_id", loginUser.getId()))) .thenReturn(getDataSourceList()); dataSources = dataSourceService.unAuthDatasource(loginUser, userId); logger.info(dataSources.toString()); @@ -290,7 +286,7 @@ public void authedDatasourceTest() { int userId = 3; // test admin user - Mockito.when(dataSourceMapper.queryAuthedDatasource(userId)).thenReturn(getSingleDataSourceList()); + when(dataSourceMapper.queryAuthedDatasource(userId)).thenReturn(getSingleDataSourceList()); List dataSources = dataSourceService.authedDatasource(loginUser, userId); logger.info(dataSources.toString()); Assertions.assertTrue(CollectionUtils.isNotEmpty(dataSources)); @@ -309,12 +305,12 @@ public void queryDataSourceListTest() { loginUser.setUserType(UserType.GENERAL_USER); Set dataSourceIds = new HashSet<>(); dataSourceIds.add(1); - Mockito.when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.DATASOURCE, + when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.DATASOURCE, loginUser.getId(), dataSourceServiceLogger)).thenReturn(dataSourceIds); DataSource dataSource = new DataSource(); dataSource.setType(DbType.MYSQL); - Mockito.when(dataSourceMapper.selectBatchIds(dataSourceIds)).thenReturn(Collections.singletonList(dataSource)); + when(dataSourceMapper.selectBatchIds(dataSourceIds)).thenReturn(Collections.singletonList(dataSource)); List list = dataSourceService.queryDataSourceList(loginUser, DbType.MYSQL.ordinal()); Assertions.assertNotNull(list); @@ -325,14 +321,14 @@ public void verifyDataSourceNameTest() { User loginUser = new User(); loginUser.setUserType(UserType.GENERAL_USER); String dataSourceName = "dataSource1"; - Mockito.when(dataSourceMapper.queryDataSourceByName(dataSourceName)).thenReturn(getDataSourceList()); - Result result = dataSourceService.verifyDataSourceName(dataSourceName); - Assertions.assertEquals(Status.DATASOURCE_EXIST.getMsg(), result.getMsg()); + when(dataSourceMapper.queryDataSourceByName(dataSourceName)).thenReturn(getDataSourceList()); + assertThrowsServiceException(Status.DATASOURCE_EXIST, + () -> dataSourceService.verifyDataSourceName(dataSourceName)); } @Test public void queryDataSourceTest() { - Mockito.when(dataSourceMapper.selectById(Mockito.anyInt())).thenReturn(null); + when(dataSourceMapper.selectById(Mockito.anyInt())).thenReturn(null); User loginUser = new User(); loginUser.setUserType(UserType.GENERAL_USER); loginUser.setId(2); @@ -343,10 +339,10 @@ public void queryDataSourceTest() { } DataSource dataSource = getOracleDataSource(1); - Mockito.when(dataSourceMapper.selectById(Mockito.anyInt())).thenReturn(dataSource); - Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.DATASOURCE, + when(dataSourceMapper.selectById(Mockito.anyInt())).thenReturn(dataSource); + when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.DATASOURCE, loginUser.getId(), DATASOURCE, baseServiceLogger)).thenReturn(true); - Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.DATASOURCE, + when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.DATASOURCE, new Object[]{dataSource.getId()}, loginUser.getId(), baseServiceLogger)).thenReturn(true); BaseDataSourceParamDTO paramDTO = dataSourceService.queryDataSource(dataSource.getId(), loginUser); Assertions.assertNotNull(paramDTO); @@ -506,15 +502,14 @@ public void testCheckConnection() throws Exception { Mockito.mockStatic(DataSourceUtils.class)) { DataSourceProcessor dataSourceProcessor = Mockito.mock(DataSourceProcessor.class); - Mockito.when(DataSourceUtils.getDatasourceProcessor(Mockito.any())).thenReturn(dataSourceProcessor); - Mockito.when(dataSourceProcessor.checkDataSourceConnectivity(Mockito.any())).thenReturn(false); + when(DataSourceUtils.getDatasourceProcessor(Mockito.any())).thenReturn(dataSourceProcessor); + when(dataSourceProcessor.checkDataSourceConnectivity(Mockito.any())).thenReturn(false); - Result result = dataSourceService.checkConnection(dataSourceType, connectionParam); - Assertions.assertEquals(Status.CONNECTION_TEST_FAILURE.getCode(), result.getCode().intValue()); + assertThrowsServiceException(Status.CONNECTION_TEST_FAILURE, + () -> dataSourceService.checkConnection(dataSourceType, connectionParam)); - Mockito.when(dataSourceProcessor.checkDataSourceConnectivity(Mockito.any())).thenReturn(true); - result = dataSourceService.checkConnection(dataSourceType, connectionParam); - Assertions.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue()); + when(dataSourceProcessor.checkDataSourceConnectivity(Mockito.any())).thenReturn(true); + assertDoesNotThrow(() -> dataSourceService.checkConnection(dataSourceType, connectionParam)); } } @@ -523,7 +518,7 @@ public void testGetDatabases() throws SQLException { DataSource dataSource = getOracleDataSource(); int datasourceId = 1; dataSource.setId(datasourceId); - Mockito.when(dataSourceMapper.selectById(datasourceId)).thenReturn(null); + when(dataSourceMapper.selectById(datasourceId)).thenReturn(null); try { dataSourceService.getDatabases(datasourceId); @@ -531,7 +526,7 @@ public void testGetDatabases() throws SQLException { Assertions.assertTrue(e.getMessage().contains(Status.QUERY_DATASOURCE_ERROR.getMsg())); } - Mockito.when(dataSourceMapper.selectById(datasourceId)).thenReturn(dataSource); + when(dataSourceMapper.selectById(datasourceId)).thenReturn(dataSource); MySQLConnectionParam connectionParam = new MySQLConnectionParam(); Connection connection = Mockito.mock(Connection.class); MockedStatic dataSourceUtils = Mockito.mockStatic(DataSourceUtils.class); diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DqExecuteResultServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DqExecuteResultServiceTest.java index 6bfff0f6a168c..4e43b5886c544 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DqExecuteResultServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DqExecuteResultServiceTest.java @@ -17,16 +17,15 @@ package org.apache.dolphinscheduler.api.service; +import static org.apache.dolphinscheduler.api.AssertionsHelper.assertDoesNotThrow; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; import org.apache.dolphinscheduler.api.ApiApplicationServer; -import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.permission.ResourcePermissionCheckService; import org.apache.dolphinscheduler.api.service.impl.BaseServiceImpl; import org.apache.dolphinscheduler.api.service.impl.DqExecuteResultServiceImpl; -import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.enums.AuthorizationType; import org.apache.dolphinscheduler.common.enums.UserType; import org.apache.dolphinscheduler.common.utils.DateUtils; @@ -39,7 +38,6 @@ import java.util.Date; import java.util.List; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -90,12 +88,11 @@ public void testQueryResultListPaging() { Page page = new Page<>(1, 10); page.setTotal(1); page.setRecords(getExecuteResultList()); - when(dqExecuteResultMapper.queryResultListPaging( - any(IPage.class), eq(""), eq(loginUser), any(), eq(ruleType), eq(start), eq(end))).thenReturn(page); + when(dqExecuteResultMapper.queryResultListPaging(any(IPage.class), eq(""), eq(loginUser), any(), eq(ruleType), + eq(start), eq(end))).thenReturn(page); - Result result = dqExecuteResultService.queryResultListPaging( - loginUser, searchVal, 1, 0, "2020-01-01 00:00:00", "2020-01-02 00:00:00", 1, 10); - Assertions.assertEquals(Integer.valueOf(Status.SUCCESS.getCode()), result.getCode()); + assertDoesNotThrow(() -> dqExecuteResultService.queryResultListPaging(loginUser, searchVal, 1, 0, + "2020-01-01 00:00:00", "2020-01-02 00:00:00", 1, 10)); } public List getExecuteResultList() { diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DqRuleServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DqRuleServiceTest.java index c87b74f05be98..8c3af3e220805 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DqRuleServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DqRuleServiceTest.java @@ -17,17 +17,15 @@ package org.apache.dolphinscheduler.api.service; +import static org.apache.dolphinscheduler.api.AssertionsHelper.assertDoesNotThrow; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; import org.apache.dolphinscheduler.api.ApiApplicationServer; -import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.permission.ResourcePermissionCheckService; import org.apache.dolphinscheduler.api.service.impl.BaseServiceImpl; import org.apache.dolphinscheduler.api.service.impl.DqRuleServiceImpl; -import org.apache.dolphinscheduler.api.utils.Result; -import org.apache.dolphinscheduler.common.constants.Constants; import org.apache.dolphinscheduler.common.enums.AuthorizationType; import org.apache.dolphinscheduler.common.enums.UserType; import org.apache.dolphinscheduler.common.utils.DateUtils; @@ -51,7 +49,6 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; -import java.util.Map; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -109,22 +106,20 @@ public void testGetRuleFormCreateJsonById() { + "\"statistics_execute_sql\",\"name\":\"统计值计算SQL\",\"type\":\"input\",\"title\":" + "\"统计值计算SQL\",\"validate\":[{\"required\":true,\"type\":\"string\",\"trigger\":\"blur\"}]}]"; when(dqRuleInputEntryMapper.getRuleInputEntryList(1)).thenReturn(getRuleInputEntryList()); - Map result = dqRuleService.getRuleFormCreateJsonById(1); - Assertions.assertEquals(json, result.get(Constants.DATA_LIST)); + String ruleFormCreateJsonById = dqRuleService.getRuleFormCreateJsonById(1); + Assertions.assertEquals(json, ruleFormCreateJsonById); } @Test public void testQueryAllRuleList() { when(dqRuleMapper.selectList(new QueryWrapper<>())).thenReturn(getRuleList()); - Map result = dqRuleService.queryAllRuleList(); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); + assertDoesNotThrow(() -> dqRuleService.queryAllRuleList()); } @Test public void testGetDatasourceOptionsById() { when(dataSourceMapper.listAllDataSourceByType(DbType.MYSQL.getCode())).thenReturn(dataSourceList()); - Map result = dqRuleService.queryAllRuleList(); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); + assertDoesNotThrow(() -> dqRuleService.queryAllRuleList()); } @Test @@ -146,15 +141,14 @@ public void testQueryRuleListPaging() { page.setTotal(1); page.setRecords(getRuleList()); - when(dqRuleMapper.queryRuleListPaging( - any(IPage.class), eq(""), eq(ruleType), eq(start), eq(end))).thenReturn(page); + when(dqRuleMapper.queryRuleListPaging(any(IPage.class), eq(""), eq(ruleType), eq(start), eq(end))) + .thenReturn(page); when(dqRuleInputEntryMapper.getRuleInputEntryList(1)).thenReturn(getRuleInputEntryList()); when(dqRuleExecuteSqlMapper.getExecuteSqlList(1)).thenReturn(getRuleExecuteSqlList()); - Result result = dqRuleService.queryRuleListPaging( - loginUser, searchVal, 0, "2020-01-01 00:00:00", "2020-01-02 00:00:00", 1, 10); - Assertions.assertEquals(Integer.valueOf(Status.SUCCESS.getCode()), result.getCode()); + assertDoesNotThrow(() -> dqRuleService.queryRuleListPaging(loginUser, searchVal, 0, "2020-01-01 00:00:00", + "2020-01-02 00:00:00", 1, 10)); } private List dataSourceList() { diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/K8SNamespaceServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/K8SNamespaceServiceTest.java index 06bd0aef98424..b4b95ea497a99 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/K8SNamespaceServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/K8SNamespaceServiceTest.java @@ -17,6 +17,8 @@ package org.apache.dolphinscheduler.api.service; +import static org.apache.dolphinscheduler.api.AssertionsHelper.assertThrowsServiceException; + import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.k8s.K8sClientService; import org.apache.dolphinscheduler.api.service.impl.K8SNamespaceServiceImpl; @@ -171,10 +173,8 @@ public void testQueryAuthorizedNamespace() { // test non-admin user loginUser.setUserType(UserType.GENERAL_USER); loginUser.setId(3); - result = k8sNamespaceService.queryAuthorizedNamespace(loginUser, 2); - Assertions.assertEquals(Status.USER_NO_OPERATION_PERM, result.get(Constants.STATUS)); - namespaces = (List) result.get(Constants.DATA_LIST); - Assertions.assertTrue(CollectionUtils.isEmpty(namespaces)); + assertThrowsServiceException(Status.USER_NO_OPERATION_PERM, + () -> k8sNamespaceService.queryAuthorizedNamespace(loginUser, 2)); } @Test @@ -193,11 +193,8 @@ public void testQueryUnAuthorizedNamespace() { // test non-admin user loginUser.setId(2); loginUser.setUserType(UserType.GENERAL_USER); - result = k8sNamespaceService.queryUnauthorizedNamespace(loginUser, 3); - logger.info(result.toString()); - Assertions.assertEquals(Status.USER_NO_OPERATION_PERM, result.get(Constants.STATUS)); - namespaces = (List) result.get(Constants.DATA_LIST); - Assertions.assertTrue(CollectionUtils.isEmpty(namespaces)); + assertThrowsServiceException(Status.USER_NO_OPERATION_PERM, + () -> k8sNamespaceService.queryUnauthorizedNamespace(loginUser, 3)); } @Test diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/LoggerServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/LoggerServiceTest.java index 0cabfdebd1768..4f93043bb056b 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/LoggerServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/LoggerServiceTest.java @@ -17,8 +17,11 @@ package org.apache.dolphinscheduler.api.service; +import static org.apache.dolphinscheduler.api.AssertionsHelper.assertDoesNotThrow; import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.DOWNLOAD_LOG; import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.VIEW_LOG; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.when; import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.exceptions.ServiceException; @@ -81,7 +84,7 @@ public void testQueryLog() { loginUser.setId(1); TaskInstance taskInstance = new TaskInstance(); taskInstance.setExecutorId(loginUser.getId() + 1); - Mockito.when(taskInstanceDao.queryById(1)).thenReturn(taskInstance); + when(taskInstanceDao.queryById(1)).thenReturn(taskInstance); Result result = loggerService.queryLog(loginUser, 2, 1, 1); // TASK_INSTANCE_NOT_FOUND Assertions.assertEquals(Status.TASK_INSTANCE_NOT_FOUND.getCode(), result.getCode().intValue()); @@ -108,7 +111,7 @@ public void testQueryLog() { // USER_NO_OPERATION_PERM Project project = getProject(1); - Mockito.when(projectMapper.queryProjectByTaskInstanceId(1)).thenReturn(project); + when(projectMapper.queryProjectByTaskInstanceId(1)).thenReturn(project); try { Mockito.doThrow(new ServiceException(Status.USER_NO_OPERATION_PERM)).when(projectService) .checkProjectAndAuthThrowException(loginUser, project, VIEW_LOG); @@ -118,8 +121,8 @@ public void testQueryLog() { } // SUCCESS - Mockito.doNothing().when(projectService).checkProjectAndAuthThrowException(loginUser, project, VIEW_LOG); - Mockito.when(taskInstanceDao.queryById(1)).thenReturn(taskInstance); + doNothing().when(projectService).checkProjectAndAuthThrowException(loginUser, project, VIEW_LOG); + when(taskInstanceDao.queryById(1)).thenReturn(taskInstance); result = loggerService.queryLog(loginUser, 1, 1, 1); Assertions.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue()); } @@ -132,7 +135,7 @@ public void testGetLogBytes() { TaskInstance taskInstance = new TaskInstance(); taskInstance.setId(1); taskInstance.setExecutorId(loginUser.getId() + 1); - Mockito.when(taskInstanceDao.queryById(1)).thenReturn(taskInstance); + when(taskInstanceDao.queryById(1)).thenReturn(taskInstance); // task instance is null try { @@ -165,7 +168,7 @@ public void testGetLogBytes() { // USER_NO_OPERATION_PERM Project project = getProject(1); - Mockito.when(projectMapper.queryProjectByTaskInstanceId(1)).thenReturn(project); + when(projectMapper.queryProjectByTaskInstanceId(1)).thenReturn(project); try { Mockito.doThrow(new ServiceException(Status.USER_NO_OPERATION_PERM)).when(projectService) .checkProjectAndAuthThrowException(loginUser, project, DOWNLOAD_LOG); @@ -175,8 +178,8 @@ public void testGetLogBytes() { } // SUCCESS - Mockito.doNothing().when(projectService).checkProjectAndAuthThrowException(loginUser, project, DOWNLOAD_LOG); - Mockito.when(projectMapper.queryProjectByTaskInstanceId(1)).thenReturn(project); + doNothing().when(projectService).checkProjectAndAuthThrowException(loginUser, project, DOWNLOAD_LOG); + when(projectMapper.queryProjectByTaskInstanceId(1)).thenReturn(project); byte[] result = loggerService.getLogBytes(loginUser, 1); Assertions.assertEquals(47, result.length); } @@ -184,16 +187,12 @@ public void testGetLogBytes() { @Test public void testQueryLogInSpecifiedProject() { long projectCode = 1L; - Mockito.when(projectMapper.queryByCode(projectCode)).thenReturn(getProject(projectCode)); - Project project = getProject(projectCode); User loginUser = new User(); loginUser.setId(-1); loginUser.setUserType(UserType.GENERAL_USER); - Map result = new HashMap<>(); - putMsg(result, Status.SUCCESS, projectCode); TaskInstance taskInstance = new TaskInstance(); - Mockito.when(taskInstanceDao.queryById(1)).thenReturn(taskInstance); + when(taskInstanceDao.queryById(1)).thenReturn(taskInstance); TaskDefinition taskDefinition = new TaskDefinition(); taskDefinition.setProjectCode(projectCode); taskDefinition.setCode(1L); @@ -202,17 +201,16 @@ public void testQueryLogInSpecifiedProject() { taskInstance.setId(1); taskInstance.setHost("127.0.0.1:8080"); taskInstance.setLogPath("/temp/log"); - Mockito.when(projectService.checkProjectAndAuth(loginUser, project, projectCode, VIEW_LOG)).thenReturn(result); - Mockito.when(taskInstanceDao.queryById(1)).thenReturn(taskInstance); - Mockito.when(taskDefinitionMapper.queryByCode(taskInstance.getTaskCode())).thenReturn(taskDefinition); - result = loggerService.queryLog(loginUser, projectCode, 1, 1, 1); - Assertions.assertEquals(Status.SUCCESS.getCode(), ((Status) result.get(Constants.STATUS)).getCode()); + doNothing().when(projectService).checkProjectAndAuthThrowException(loginUser, projectCode, VIEW_LOG); + when(taskInstanceDao.queryById(1)).thenReturn(taskInstance); + when(taskDefinitionMapper.queryByCode(taskInstance.getTaskCode())).thenReturn(taskDefinition); + assertDoesNotThrow(() -> loggerService.queryLog(loginUser, projectCode, 1, 1, 1)); } @Test public void testGetLogBytesInSpecifiedProject() { long projectCode = 1L; - Mockito.when(projectMapper.queryByCode(projectCode)).thenReturn(getProject(projectCode)); + when(projectMapper.queryByCode(projectCode)).thenReturn(getProject(projectCode)); Project project = getProject(projectCode); User loginUser = new User(); @@ -229,11 +227,10 @@ public void testGetLogBytesInSpecifiedProject() { taskInstance.setId(1); taskInstance.setHost("127.0.0.1:8080"); taskInstance.setLogPath("/temp/log"); - Mockito.when(projectService.checkProjectAndAuth(loginUser, project, projectCode, DOWNLOAD_LOG)) - .thenReturn(result); - Mockito.when(taskInstanceDao.queryById(1)).thenReturn(taskInstance); - Mockito.when(taskDefinitionMapper.queryByCode(taskInstance.getTaskCode())).thenReturn(taskDefinition); - loggerService.getLogBytes(loginUser, projectCode, 1); + doNothing().when(projectService).checkProjectAndAuthThrowException(loginUser, projectCode, DOWNLOAD_LOG); + when(taskInstanceDao.queryById(1)).thenReturn(taskInstance); + when(taskDefinitionMapper.queryByCode(taskInstance.getTaskCode())).thenReturn(taskDefinition); + assertDoesNotThrow(() -> loggerService.getLogBytes(loginUser, projectCode, 1)); } /** diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/MonitorServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/MonitorServiceTest.java index 54808a029093f..6bfbffd0936de 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/MonitorServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/MonitorServiceTest.java @@ -17,16 +17,16 @@ package org.apache.dolphinscheduler.api.service; +import static org.apache.dolphinscheduler.api.AssertionsHelper.assertDoesNotThrow; + +import org.apache.dolphinscheduler.api.AssertionsHelper; import org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant; -import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.permission.ResourcePermissionCheckService; import org.apache.dolphinscheduler.api.service.impl.BaseServiceImpl; import org.apache.dolphinscheduler.api.service.impl.MonitorServiceImpl; -import org.apache.dolphinscheduler.common.constants.Constants; import org.apache.dolphinscheduler.common.enums.AuthorizationType; import org.apache.dolphinscheduler.common.enums.UserType; import org.apache.dolphinscheduler.common.model.Server; -import org.apache.dolphinscheduler.dao.entity.MonitorRecord; import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.dao.plugin.api.monitor.DatabaseMetrics; import org.apache.dolphinscheduler.dao.plugin.api.monitor.DatabaseMonitor; @@ -38,7 +38,6 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; -import java.util.Map; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; @@ -91,39 +90,21 @@ public void init() { public void testQueryDatabaseState() { mockPermissionCheck(ApiFuncIdentificationConstant.MONITOR_DATABASES_VIEW, true); Mockito.when(databaseMonitor.getDatabaseMetrics()).thenReturn(getDatabaseMetrics()); - Map result = monitorService.queryDatabaseState(user); - logger.info(result.toString()); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); - List monitorRecordList = (List) result.get(Constants.DATA_LIST); - Assertions.assertTrue(CollectionUtils.isNotEmpty(monitorRecordList)); - - mockPermissionCheck(ApiFuncIdentificationConstant.MONITOR_DATABASES_VIEW, false); - Map noPermission = monitorService.queryDatabaseState(user); - Assertions.assertEquals(Status.SUCCESS, noPermission.get(Constants.STATUS)); + List databaseMetrics = monitorService.queryDatabaseState(user); + Assertions.assertTrue(CollectionUtils.isNotEmpty(databaseMetrics)); } @Test public void testQueryMaster() { mockPermissionCheck(ApiFuncIdentificationConstant.MONITOR_MASTER_VIEW, true); Mockito.when(registryClient.getServerList(RegistryNodeType.MASTER)).thenReturn(getServerList()); - Map result = monitorService.queryMaster(user); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); - - mockPermissionCheck(ApiFuncIdentificationConstant.MONITOR_MASTER_VIEW, false); - Map noPermission = monitorService.queryMaster(user); - Assertions.assertEquals(Status.SUCCESS, noPermission.get(Constants.STATUS)); + assertDoesNotThrow(() -> monitorService.queryMaster(user)); } @Test public void testQueryWorker() { - mockPermissionCheck(ApiFuncIdentificationConstant.MONITOR_WORKER_VIEW, true); Mockito.when(registryClient.getServerList(RegistryNodeType.WORKER)).thenReturn(getServerList()); - Map result = monitorService.queryWorker(user); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); - - mockPermissionCheck(ApiFuncIdentificationConstant.MONITOR_WORKER_VIEW, false); - Map noPermission = monitorService.queryWorker(user); - Assertions.assertEquals(Status.SUCCESS, noPermission.get(Constants.STATUS)); + AssertionsHelper.assertDoesNotThrow(() -> monitorService.queryWorker(user)); } @Test diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/QueueServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/QueueServiceTest.java index ea70cf7f733b1..fbe019098a920 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/QueueServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/QueueServiceTest.java @@ -17,16 +17,18 @@ package org.apache.dolphinscheduler.api.service; +import static org.apache.dolphinscheduler.api.AssertionsHelper.assertDoesNotThrow; import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.YARN_QUEUE_CREATE; import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.YARN_QUEUE_UPDATE; +import static org.mockito.Mockito.when; +import org.apache.dolphinscheduler.api.AssertionsHelper; import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.exceptions.ServiceException; import org.apache.dolphinscheduler.api.permission.ResourcePermissionCheckService; import org.apache.dolphinscheduler.api.service.impl.BaseServiceImpl; import org.apache.dolphinscheduler.api.service.impl.QueueServiceImpl; import org.apache.dolphinscheduler.api.utils.PageInfo; -import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.constants.Constants; import org.apache.dolphinscheduler.common.enums.AuthorizationType; import org.apache.dolphinscheduler.common.enums.UserType; @@ -100,12 +102,10 @@ public void after() { public void testQueryList() { Set ids = new HashSet<>(); ids.add(1); - Mockito.when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.QUEUE, + when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.QUEUE, getLoginUser().getId(), queueServiceImplLogger)).thenReturn(ids); - Mockito.when(queueMapper.selectBatchIds(Mockito.anySet())).thenReturn(getQueueList()); - Result result = queueService.queryList(getLoginUser()); - List queueList = (List) result.getData(); - Assertions.assertTrue(CollectionUtils.isNotEmpty(queueList)); + when(queueMapper.selectBatchIds(Mockito.anySet())).thenReturn(getQueueList()); + assertDoesNotThrow(() -> queueService.queryList(getLoginUser())); } @@ -117,20 +117,19 @@ public void testQueryListPage() { page.setRecords(getQueueList()); Set ids = new HashSet<>(); ids.add(1); - Mockito.when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.QUEUE, + when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.QUEUE, getLoginUser().getId(), queueServiceImplLogger)).thenReturn(ids); - Mockito.when(queueMapper.queryQueuePaging(Mockito.any(Page.class), Mockito.anyList(), Mockito.eq(QUEUE_NAME))) + when(queueMapper.queryQueuePaging(Mockito.any(Page.class), Mockito.anyList(), Mockito.eq(QUEUE_NAME))) .thenReturn(page); - Result result = queueService.queryList(getLoginUser(), QUEUE_NAME, 1, 10); - PageInfo pageInfo = (PageInfo) result.getData(); - Assertions.assertTrue(CollectionUtils.isNotEmpty(pageInfo.getTotalList())); + PageInfo queuePageInfo = queueService.queryList(getLoginUser(), QUEUE_NAME, 1, 10); + Assertions.assertTrue(CollectionUtils.isNotEmpty(queuePageInfo.getTotalList())); } @Test public void testCreateQueue() { - Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.QUEUE, + when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.QUEUE, getLoginUser().getId(), YARN_QUEUE_CREATE, baseServiceLogger)).thenReturn(true); - Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.QUEUE, null, 0, + when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.QUEUE, null, 0, baseServiceLogger)).thenReturn(true); // queue is null @@ -146,18 +145,17 @@ public void testCreateQueue() { Assertions.assertEquals(formatter, exception.getMessage()); // correct - Result result = queueService.createQueue(getLoginUser(), QUEUE_NAME, QUEUE_NAME); - Assertions.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue()); + assertDoesNotThrow(() -> queueService.createQueue(getLoginUser(), QUEUE_NAME, QUEUE_NAME)); } @Test public void testUpdateQueue() { - Mockito.when(queueMapper.selectById(1)).thenReturn(getQUEUE()); - Mockito.when(queueMapper.existQueue(EXISTS, null)).thenReturn(true); - Mockito.when(queueMapper.existQueue(null, EXISTS)).thenReturn(true); - Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.QUEUE, + when(queueMapper.selectById(1)).thenReturn(getQUEUE()); + when(queueMapper.existQueue(EXISTS, null)).thenReturn(true); + when(queueMapper.existQueue(null, EXISTS)).thenReturn(true); + when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.QUEUE, getLoginUser().getId(), YARN_QUEUE_UPDATE, baseServiceLogger)).thenReturn(true); - Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.QUEUE, new Object[]{0}, 0, + when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.QUEUE, new Object[]{0}, 0, baseServiceLogger)).thenReturn(true); // not exist @@ -167,7 +165,7 @@ public void testUpdateQueue() { Assertions.assertEquals(formatter, exception.getMessage()); // no need update - Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.QUEUE, new Object[]{1}, 0, + when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.QUEUE, new Object[]{1}, 0, baseServiceLogger)).thenReturn(true); exception = Assertions.assertThrows(ServiceException.class, () -> queueService.updateQueue(getLoginUser(), 1, QUEUE_NAME, QUEUE_NAME)); @@ -186,19 +184,16 @@ public void testUpdateQueue() { Assertions.assertEquals(formatter, exception.getMessage()); // success - Mockito.when(userMapper.existUser(Mockito.anyString())).thenReturn(false); - Result result = queueService.updateQueue(getLoginUser(), 1, NOT_EXISTS, NOT_EXISTS); - Assertions.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue()); + when(userMapper.existUser(Mockito.anyString())).thenReturn(false); + assertDoesNotThrow(() -> queueService.updateQueue(getLoginUser(), 1, NOT_EXISTS, NOT_EXISTS)); // success update with same queue name - Mockito.when(queueMapper.existQueue(NOT_EXISTS_FINAL, null)).thenReturn(false); - result = queueService.updateQueue(getLoginUser(), 1, NOT_EXISTS_FINAL, NOT_EXISTS); - Assertions.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue()); + when(queueMapper.existQueue(NOT_EXISTS_FINAL, null)).thenReturn(false); + assertDoesNotThrow(() -> queueService.updateQueue(getLoginUser(), 1, NOT_EXISTS_FINAL, NOT_EXISTS)); // success update with same queue value - Mockito.when(queueMapper.existQueue(null, NOT_EXISTS_FINAL)).thenReturn(false); - result = queueService.updateQueue(getLoginUser(), 1, NOT_EXISTS, NOT_EXISTS_FINAL); - Assertions.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue()); + when(queueMapper.existQueue(null, NOT_EXISTS_FINAL)).thenReturn(false); + assertDoesNotThrow(() -> queueService.updateQueue(getLoginUser(), 1, NOT_EXISTS, NOT_EXISTS_FINAL)); } @Test @@ -215,20 +210,19 @@ public void testVerifyQueue() { Assertions.assertEquals(formatter, exception.getMessage()); // exist queueName - Mockito.when(queueMapper.existQueue(EXISTS, null)).thenReturn(true); + when(queueMapper.existQueue(EXISTS, null)).thenReturn(true); exception = Assertions.assertThrows(ServiceException.class, () -> queueService.verifyQueue(EXISTS, QUEUE_NAME)); formatter = MessageFormat.format(Status.QUEUE_VALUE_EXIST.getMsg(), EXISTS); Assertions.assertEquals(formatter, exception.getMessage()); // exist queue - Mockito.when(queueMapper.existQueue(null, EXISTS)).thenReturn(true); + when(queueMapper.existQueue(null, EXISTS)).thenReturn(true); exception = Assertions.assertThrows(ServiceException.class, () -> queueService.verifyQueue(QUEUE, EXISTS)); formatter = MessageFormat.format(Status.QUEUE_NAME_EXIST.getMsg(), EXISTS); Assertions.assertEquals(formatter, exception.getMessage()); // success - Result result = queueService.verifyQueue(NOT_EXISTS, NOT_EXISTS); - Assertions.assertEquals(result.getCode().intValue(), Status.SUCCESS.getCode()); + AssertionsHelper.assertDoesNotThrow(() -> queueService.verifyQueue(NOT_EXISTS, NOT_EXISTS)); } @Test @@ -236,12 +230,12 @@ public void testCreateQueueIfNotExists() { Queue queue; // queue exists - Mockito.when(queueMapper.queryQueueName(QUEUE, QUEUE_NAME)).thenReturn(getQUEUE()); + when(queueMapper.queryQueueName(QUEUE, QUEUE_NAME)).thenReturn(getQUEUE()); queue = queueService.createQueueIfNotExists(QUEUE, QUEUE_NAME); Assertions.assertEquals(getQUEUE(), queue); // queue not exists - Mockito.when(queueMapper.queryQueueName(QUEUE, QUEUE_NAME)).thenReturn(null); + when(queueMapper.queryQueueName(QUEUE, QUEUE_NAME)).thenReturn(null); queue = queueService.createQueueIfNotExists(QUEUE, QUEUE_NAME); Assertions.assertEquals(new Queue(QUEUE_NAME, QUEUE), queue); } diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/TenantServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/TenantServiceTest.java index 832b1f1c2c569..5746840e90b6c 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/TenantServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/TenantServiceTest.java @@ -17,19 +17,19 @@ package org.apache.dolphinscheduler.api.service; +import static org.apache.dolphinscheduler.api.AssertionsHelper.assertDoesNotThrow; +import static org.apache.dolphinscheduler.api.AssertionsHelper.assertThrowsServiceException; import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.TENANT_CREATE; import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.TENANT_DELETE; import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.TENANT_UPDATE; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; import org.apache.dolphinscheduler.api.enums.Status; -import org.apache.dolphinscheduler.api.exceptions.ServiceException; import org.apache.dolphinscheduler.api.permission.ResourcePermissionCheckService; import org.apache.dolphinscheduler.api.service.impl.BaseServiceImpl; import org.apache.dolphinscheduler.api.service.impl.TenantServiceImpl; import org.apache.dolphinscheduler.api.utils.PageInfo; -import org.apache.dolphinscheduler.api.utils.Result; -import org.apache.dolphinscheduler.common.constants.Constants; import org.apache.dolphinscheduler.common.enums.AuthorizationType; import org.apache.dolphinscheduler.common.enums.UserType; import org.apache.dolphinscheduler.dao.entity.ProcessInstance; @@ -45,12 +45,10 @@ import org.apache.commons.collections4.CollectionUtils; -import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Set; import org.junit.jupiter.api.Assertions; @@ -68,9 +66,6 @@ import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; -/** - * tenant service test - */ @ExtendWith(MockitoExtension.class) @MockitoSettings(strictness = Strictness.LENIENT) public class TenantServiceTest { @@ -108,59 +103,47 @@ public class TenantServiceTest { private static final String queueName = "queue_name"; @Test - public void testCreateTenant() throws Exception { + public void testCreateTenant() { User loginUser = getLoginUser(); - Mockito.when(tenantMapper.existTenant(tenantCode)).thenReturn(true); - Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.TENANT, - loginUser.getId(), TENANT_CREATE, baseServiceLogger)).thenReturn(true); - Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.TENANT, null, 0, + when(tenantMapper.existTenant(tenantCode)).thenReturn(true); + when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.TENANT, loginUser.getId(), + TENANT_CREATE, baseServiceLogger)).thenReturn(true); + when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.TENANT, null, 0, baseServiceLogger)).thenReturn(true); - Map result; - // check exist String emptyTenantCode = ""; - Throwable exception = Assertions.assertThrows(ServiceException.class, + assertThrowsServiceException(Status.REQUEST_PARAMS_NOT_VALID_ERROR, () -> tenantService.createTenant(loginUser, emptyTenantCode, 1, tenantDesc)); - String formatter = MessageFormat.format(Status.REQUEST_PARAMS_NOT_VALID_ERROR.getMsg(), emptyTenantCode); - Assertions.assertEquals(formatter, exception.getMessage()); // check tenant code too long String longStr = "this_is_a_very_long_string_this_is_a_very_long_string_this_is_a_very_long_string_this_is_a_very_long_string"; - exception = Assertions.assertThrows(ServiceException.class, + assertThrowsServiceException(Status.TENANT_FULL_NAME_TOO_LONG_ERROR, () -> tenantService.createTenant(loginUser, longStr, 1, tenantDesc)); - Assertions.assertEquals(Status.TENANT_FULL_NAME_TOO_LONG_ERROR.getMsg(), exception.getMessage()); // check tenant code invalid - exception = Assertions.assertThrows(ServiceException.class, + assertThrowsServiceException(Status.CHECK_OS_TENANT_CODE_ERROR, () -> tenantService.createTenant(getLoginUser(), "%!1111", 1, tenantDesc)); - Assertions.assertEquals(Status.CHECK_OS_TENANT_CODE_ERROR.getMsg(), exception.getMessage()); // check exist - exception = Assertions.assertThrows(ServiceException.class, + assertThrowsServiceException(Status.OS_TENANT_CODE_EXIST, () -> tenantService.createTenant(loginUser, tenantCode, 1, tenantDesc)); - formatter = MessageFormat.format(Status.OS_TENANT_CODE_EXIST.getMsg(), tenantCode); - Assertions.assertEquals(formatter, exception.getMessage()); // success - result = tenantService.createTenant(loginUser, "test", 1, tenantDesc); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); + assertDoesNotThrow(() -> tenantService.createTenant(loginUser, "test", 1, tenantDesc)); } @Test public void testCreateTenantError() { - Mockito.when(tenantMapper.existTenant(tenantCode)).thenReturn(true); + when(tenantMapper.existTenant(tenantCode)).thenReturn(true); // tenantCode exist - Throwable exception = Assertions.assertThrows(ServiceException.class, + assertThrowsServiceException(Status.OS_TENANT_CODE_EXIST, () -> tenantService.verifyTenantCode(getTenant().getTenantCode())); - String expect = MessageFormat.format(Status.OS_TENANT_CODE_EXIST.getMsg(), getTenant().getTenantCode()); - Assertions.assertEquals(expect, exception.getMessage()); // success - Result result = tenantService.verifyTenantCode("s00000000000l887888885554444sfjdskfjslakslkdf"); - Assertions.assertEquals(Status.SUCCESS.getMsg(), result.getMsg()); + assertDoesNotThrow(() -> tenantService.verifyTenantCode("s00000000000l887888885554444sfjdskfjslakslkdf")); } @Test @@ -171,100 +154,81 @@ public void testQueryTenantListPage() { page.setTotal(1L); Set ids = new HashSet<>(); ids.add(1); - Mockito.when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.TENANT, + when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.TENANT, getLoginUser().getId(), tenantServiceImplLogger)).thenReturn(ids); - Mockito.when(tenantMapper.queryTenantPaging(any(Page.class), Mockito.anyList(), Mockito.eq(tenantDesc))) + when(tenantMapper.queryTenantPaging(any(Page.class), Mockito.anyList(), Mockito.eq(tenantDesc))) .thenReturn(page); - Result result = tenantService.queryTenantList(getLoginUser(), tenantDesc, 1, 10); - PageInfo pageInfo = (PageInfo) result.getData(); + PageInfo pageInfo = tenantService.queryTenantList(getLoginUser(), tenantDesc, 1, 10); Assertions.assertTrue(CollectionUtils.isNotEmpty(pageInfo.getTotalList())); } @Test - public void testUpdateTenant() throws Exception { - Mockito.when(tenantMapper.queryById(1)).thenReturn(getTenant()); - Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.TENANT, - getLoginUser().getId(), TENANT_UPDATE, baseServiceLogger)).thenReturn(true); - Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.TENANT, null, 0, + public void testUpdateTenant() { + when(tenantMapper.queryById(1)).thenReturn(getTenant()); + when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.TENANT, getLoginUser().getId(), + TENANT_UPDATE, baseServiceLogger)).thenReturn(true); + when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.TENANT, null, 0, baseServiceLogger)).thenReturn(true); - Mockito.when(tenantMapper.updateById(getTenant())).thenReturn(1); + when(tenantMapper.updateById(any())).thenReturn(1); // update not exists tenant - Throwable exception = Assertions.assertThrows(ServiceException.class, + assertThrowsServiceException(Status.TENANT_NOT_EXIST, () -> tenantService.updateTenant(getLoginUser(), 912222, tenantCode, 1, tenantDesc)); - Assertions.assertEquals(Status.TENANT_NOT_EXIST.getMsg(), exception.getMessage()); // success - Map result = tenantService.updateTenant(getLoginUser(), 1, tenantCode, 1, tenantDesc); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); + assertDoesNotThrow(() -> tenantService.updateTenant(getLoginUser(), 1, tenantCode, 1, tenantDesc)); // success update with same tenant code - result = tenantService.updateTenant(getLoginUser(), 1, tenantCode, 1, tenantDesc); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); + assertDoesNotThrow(() -> tenantService.updateTenant(getLoginUser(), 1, tenantCode, 1, tenantDesc)); } @Test - public void testDeleteTenantById() throws Exception { - Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.TENANT, + public void testDeleteTenantById() { + when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.TENANT, getLoginUser().getId(), TENANT_DELETE, baseServiceLogger)).thenReturn(true); - Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.TENANT, null, 0, + when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.TENANT, null, 0, baseServiceLogger)).thenReturn(true); - Mockito.when(tenantMapper.queryById(1)).thenReturn(getTenant()); - Mockito.when(processInstanceMapper.queryByTenantCodeAndStatus(tenantCode, + when(tenantMapper.queryById(1)).thenReturn(getTenant()); + when(processInstanceMapper.queryByTenantCodeAndStatus(tenantCode, org.apache.dolphinscheduler.service.utils.Constants.NOT_TERMINATED_STATES)) - .thenReturn(getInstanceList()); - Mockito.when(scheduleMapper.queryScheduleListByTenant(tenantCode)).thenReturn(getScheduleList()); - Mockito.when(userMapper.queryUserListByTenant(3)).thenReturn(getUserList()); + .thenReturn(getInstanceList()); + when(scheduleMapper.queryScheduleListByTenant(tenantCode)).thenReturn(getScheduleList()); + when(userMapper.queryUserListByTenant(3)).thenReturn(getUserList()); // TENANT_NOT_EXIST - Throwable exception = Assertions.assertThrows(ServiceException.class, - () -> tenantService.deleteTenantById(getLoginUser(), 12)); - Assertions.assertEquals(Status.TENANT_NOT_EXIST.getMsg(), exception.getMessage()); + assertThrowsServiceException(Status.TENANT_NOT_EXIST, () -> tenantService.deleteTenantById(getLoginUser(), 12)); // DELETE_TENANT_BY_ID_FAIL - exception = Assertions.assertThrows(ServiceException.class, + assertThrowsServiceException(Status.DELETE_TENANT_BY_ID_FAIL, () -> tenantService.deleteTenantById(getLoginUser(), 1)); - String prefix = Status.DELETE_TENANT_BY_ID_FAIL.getMsg().substring(1, 5); - Assertions.assertTrue(exception.getMessage().contains(prefix)); // DELETE_TENANT_BY_ID_FAIL_DEFINES - Mockito.when(tenantMapper.queryById(2)).thenReturn(getTenant(2)); - exception = Assertions.assertThrows(ServiceException.class, + when(processInstanceMapper.queryByTenantCodeAndStatus(any(), any())).thenReturn(Collections.emptyList()); + when(tenantMapper.queryById(2)).thenReturn(getTenant(2)); + assertThrowsServiceException(Status.DELETE_TENANT_BY_ID_FAIL_DEFINES, () -> tenantService.deleteTenantById(getLoginUser(), 2)); - prefix = Status.DELETE_TENANT_BY_ID_FAIL_DEFINES.getMsg().substring(1, 5); - Assertions.assertTrue(exception.getMessage().contains(prefix)); // DELETE_TENANT_BY_ID_FAIL_USERS - Mockito.when(tenantMapper.queryById(3)).thenReturn(getTenant(3)); - exception = Assertions.assertThrows(ServiceException.class, + when(tenantMapper.queryById(3)).thenReturn(getTenant(3)); + when(scheduleMapper.queryScheduleListByTenant(tenantCode)).thenReturn(Collections.emptyList()); + assertThrowsServiceException(Status.DELETE_TENANT_BY_ID_FAIL_USERS, () -> tenantService.deleteTenantById(getLoginUser(), 3)); - prefix = Status.DELETE_TENANT_BY_ID_FAIL_USERS.getMsg().substring(1, 5); - Assertions.assertTrue(exception.getMessage().contains(prefix)); // success - Mockito.when(processInstanceMapper.queryByTenantCodeAndStatus(tenantCode, - org.apache.dolphinscheduler.service.utils.Constants.NOT_TERMINATED_STATES)) - .thenReturn(Collections.emptyList()); - Mockito.when(scheduleMapper.queryScheduleListByTenant(tenantCode)).thenReturn(Collections.emptyList()); - Mockito.when(tenantMapper.queryById(4)).thenReturn(getTenant(4)); - Mockito.when(tenantMapper.deleteById(4)).thenReturn(1); - Map result = tenantService.deleteTenantById(getLoginUser(), 4); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); + when(tenantMapper.queryById(4)).thenReturn(getTenant(4)); + when(tenantMapper.deleteById(4)).thenReturn(1); + assertDoesNotThrow(() -> tenantService.deleteTenantById(getLoginUser(), 4)); } @Test public void testVerifyTenantCode() { - Mockito.when(tenantMapper.existTenant(tenantCode)).thenReturn(true); + when(tenantMapper.existTenant(tenantCode)).thenReturn(true); // tenantCode exist - Throwable exception = Assertions.assertThrows(ServiceException.class, + assertThrowsServiceException(Status.OS_TENANT_CODE_EXIST, () -> tenantService.verifyTenantCode(getTenant().getTenantCode())); - String expect = MessageFormat.format(Status.OS_TENANT_CODE_EXIST.getMsg(), getTenant().getTenantCode()); - Assertions.assertEquals(expect, exception.getMessage()); - // success - Result result = tenantService.verifyTenantCode("s00000000000l887888885554444sfjdskfjslakslkdf"); - Assertions.assertEquals(Status.SUCCESS.getMsg(), result.getMsg()); + assertDoesNotThrow(() -> tenantService.verifyTenantCode("s00000000000l887888885554444sfjdskfjslakslkdf")); } @Test @@ -272,16 +236,18 @@ public void testCreateTenantIfNotExists() { Tenant tenant; // Tenant exists - Mockito.when(tenantMapper.existTenant(tenantCode)).thenReturn(true); - Mockito.when(tenantMapper.queryByTenantCode(tenantCode)).thenReturn(getTenant()); + when(tenantMapper.existTenant(tenantCode)).thenReturn(true); + when(tenantMapper.queryByTenantCode(tenantCode)).thenReturn(getTenant()); tenant = tenantService.createTenantIfNotExists(tenantCode, tenantDesc, queue, queueName); Assertions.assertEquals(getTenant(), tenant); // Tenant not exists - Mockito.when(tenantMapper.existTenant(tenantCode)).thenReturn(false); - Mockito.when(queueService.createQueueIfNotExists(queue, queueName)).thenReturn(getQueue()); + when(tenantMapper.existTenant(tenantCode)).thenReturn(false); + when(queueService.createQueueIfNotExists(queue, queueName)).thenReturn(getQueue()); tenant = tenantService.createTenantIfNotExists(tenantCode, tenantDesc, queue, queueName); - Assertions.assertEquals(new Tenant(tenantCode, tenantDesc, getQueue().getId()), tenant); + Assertions.assertEquals(tenantCode, tenant.getTenantCode()); + Assertions.assertEquals(tenantDesc, tenant.getDescription()); + Assertions.assertEquals(getQueue().getId(), tenant.getQueueId()); } /** diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/WorkFlowLineageServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/WorkFlowLineageServiceTest.java index 1a5e97931be21..316ed5d18ebe6 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/WorkFlowLineageServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/WorkFlowLineageServiceTest.java @@ -29,6 +29,8 @@ import org.apache.dolphinscheduler.dao.mapper.TaskDefinitionLogMapper; import org.apache.dolphinscheduler.dao.mapper.WorkFlowLineageMapper; +import org.apache.commons.collections4.CollectionUtils; + import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -83,9 +85,8 @@ public void testQueryWorkFlowLineageByName() { when(projectMapper.queryByCode(1L)).thenReturn(project); when(workFlowLineageMapper.queryWorkFlowLineageByName(Mockito.anyLong(), Mockito.any())) .thenReturn(getWorkFlowLineages()); - Map result = workFlowLineageService.queryWorkFlowLineageByName(1L, name); - List workFlowLineageList = (List) result.get(Constants.DATA_LIST); - Assertions.assertTrue(workFlowLineageList.size() > 0); + List workFlowLineages = workFlowLineageService.queryWorkFlowLineageByName(1L, name); + Assertions.assertTrue(CollectionUtils.isNotEmpty(workFlowLineages)); } @Test diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/impl/ClusterServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/impl/ClusterServiceTest.java new file mode 100644 index 0000000000000..5733c2d480362 --- /dev/null +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/impl/ClusterServiceTest.java @@ -0,0 +1,259 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.dolphinscheduler.api.service.impl; + +import static org.apache.dolphinscheduler.api.AssertionsHelper.assertDoesNotThrow; +import static org.apache.dolphinscheduler.api.AssertionsHelper.assertThrowsServiceException; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Mockito.when; + +import org.apache.dolphinscheduler.api.dto.ClusterDto; +import org.apache.dolphinscheduler.api.enums.Status; +import org.apache.dolphinscheduler.api.k8s.K8sManager; +import org.apache.dolphinscheduler.api.utils.PageInfo; +import org.apache.dolphinscheduler.common.enums.UserType; +import org.apache.dolphinscheduler.dao.entity.Cluster; +import org.apache.dolphinscheduler.dao.entity.User; +import org.apache.dolphinscheduler.dao.mapper.ClusterMapper; +import org.apache.dolphinscheduler.dao.mapper.K8sNamespaceMapper; + +import org.apache.commons.collections4.CollectionUtils; + +import java.util.ArrayList; +import java.util.List; + +import org.assertj.core.util.Lists; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; + +/** + * cluster service test + */ +@ExtendWith(MockitoExtension.class) +public class ClusterServiceTest { + + public static final Logger logger = LoggerFactory.getLogger(ClusterServiceTest.class); + + @InjectMocks + private ClusterServiceImpl clusterService; + + @Mock + private ClusterMapper clusterMapper; + + @Mock + private K8sNamespaceMapper k8sNamespaceMapper; + + @Mock + private K8sManager k8sManager; + + public static final String testUserName = "clusterServerTest"; + + public static final String clusterName = "Env1"; + + @BeforeEach + public void setUp() { + } + + @AfterEach + public void after() { + } + + @Test + public void testCreateCluster() { + assertThrowsServiceException(Status.USER_NO_OPERATION_PERM, () -> { + User loginUser = getGeneralUser(); + clusterService.createCluster(loginUser, clusterName, getConfig(), getDesc()); + }); + + assertThrowsServiceException(Status.CLUSTER_CONFIG_IS_NULL, () -> { + User loginUser = getAdminUser(); + clusterService.createCluster(loginUser, clusterName, "", getDesc()); + }); + + final User loginUser = getAdminUser(); + assertThrowsServiceException(Status.CLUSTER_NAME_IS_NULL, + () -> clusterService.createCluster(loginUser, "", getConfig(), getDesc())); + + when(clusterMapper.queryByClusterName(clusterName)).thenReturn(getCluster()); + assertThrowsServiceException(Status.CLUSTER_NAME_EXISTS, + () -> clusterService.createCluster(loginUser, clusterName, getConfig(), getDesc())); + + when(clusterMapper.insert(Mockito.any(Cluster.class))).thenReturn(1); + Assertions.assertDoesNotThrow( + () -> clusterService.createCluster(loginUser, "testName", "testConfig", "testDesc")); + } + + @Test + public void testCheckParams() { + assertDoesNotThrow(() -> clusterService.checkParams(clusterName, getConfig())); + assertThrowsServiceException(Status.CLUSTER_NAME_IS_NULL, () -> clusterService.checkParams("", getConfig())); + assertThrowsServiceException(Status.CLUSTER_CONFIG_IS_NULL, () -> clusterService.checkParams(clusterName, "")); + } + + @Test + public void testUpdateClusterByCode() { + final User loginUser = getGeneralUser(); + assertThrowsServiceException(Status.USER_NO_OPERATION_PERM, + () -> clusterService.updateClusterByCode(loginUser, 1L, clusterName, getConfig(), getDesc())); + + final User adminUser = getAdminUser(); + assertThrowsServiceException(Status.CLUSTER_CONFIG_IS_NULL, + () -> clusterService.updateClusterByCode(adminUser, 1L, clusterName, "", getDesc())); + assertThrowsServiceException(Status.CLUSTER_NAME_IS_NULL, + () -> clusterService.updateClusterByCode(adminUser, 1L, "", getConfig(), getDesc())); + assertThrowsServiceException(Status.CLUSTER_NOT_EXISTS, + () -> clusterService.updateClusterByCode(adminUser, 2L, clusterName, getConfig(), getDesc())); + + when(clusterMapper.queryByClusterName(clusterName)).thenReturn(getCluster()); + assertThrowsServiceException(Status.CLUSTER_NAME_EXISTS, + () -> clusterService.updateClusterByCode(adminUser, 2L, clusterName, getConfig(), getDesc())); + + when(clusterMapper.updateById(Mockito.any(Cluster.class))).thenReturn(1); + when(clusterMapper.queryByClusterCode(1L)).thenReturn(getCluster()); + Cluster cluster = clusterService.updateClusterByCode(adminUser, 1L, "testName", getConfig(), "test"); + assertNotNull(cluster); + } + + @Test + public void testQueryAllClusterList() { + when(clusterMapper.queryAllClusterList()).thenReturn(Lists.newArrayList(getCluster())); + List clusterDtos = clusterService.queryAllClusterList(); + Assertions.assertEquals(clusterDtos.size(), 1); + } + + @Test + public void testQueryClusterListPaging() { + IPage page = new Page<>(1, 10); + page.setRecords(getList()); + page.setTotal(1L); + when(clusterMapper.queryClusterListPaging(Mockito.any(Page.class), Mockito.eq(clusterName))).thenReturn(page); + + PageInfo clusterDtoPageInfo = clusterService.queryClusterListPaging(1, 10, clusterName); + Assertions.assertTrue(CollectionUtils.isNotEmpty(clusterDtoPageInfo.getTotalList())); + } + + @Test + public void testQueryClusterByName() { + when(clusterMapper.queryByClusterName(clusterName)).thenReturn(null); + assertThrowsServiceException(Status.QUERY_CLUSTER_BY_NAME_ERROR, + () -> clusterService.queryClusterByName(clusterName)); + + when(clusterMapper.queryByClusterName(clusterName)).thenReturn(getCluster()); + ClusterDto clusterDto = clusterService.queryClusterByName(clusterName); + assertNotNull(clusterDto); + } + + @Test + public void testQueryClusterByCode() { + when(clusterMapper.queryByClusterCode(1L)).thenReturn(null); + assertThrowsServiceException(Status.QUERY_CLUSTER_BY_CODE_ERROR, () -> clusterService.queryClusterByCode(1L)); + + when(clusterMapper.queryByClusterCode(1L)).thenReturn(getCluster()); + ClusterDto clusterDto = clusterService.queryClusterByCode(1L); + assertNotNull(clusterDto); + } + + @Test + public void testDeleteClusterByCode() { + assertThrowsServiceException(Status.USER_NO_OPERATION_PERM, () -> { + User loginUser = getGeneralUser(); + clusterService.deleteClusterByCode(loginUser, 1L); + }); + + final User adminUser = getAdminUser(); + when(clusterMapper.deleteByCode(1L)).thenReturn(1); + assertDoesNotThrow(() -> clusterService.deleteClusterByCode(adminUser, 1L)); + + when(k8sNamespaceMapper.selectCount(Mockito.any())).thenReturn(1L); + assertThrowsServiceException(Status.DELETE_CLUSTER_RELATED_NAMESPACE_EXISTS, + () -> clusterService.deleteClusterByCode(adminUser, 1L)); + } + + @Test + public void testVerifyCluster() { + assertThrowsServiceException(Status.CLUSTER_NAME_IS_NULL, () -> clusterService.verifyCluster("")); + + when(clusterMapper.queryByClusterName(clusterName)).thenReturn(getCluster()); + assertThrowsServiceException(Status.CLUSTER_NAME_EXISTS, () -> clusterService.verifyCluster(clusterName)); + } + + private Cluster getCluster() { + Cluster cluster = new Cluster(); + cluster.setId(1); + cluster.setCode(1L); + cluster.setName(clusterName); + cluster.setConfig(getConfig()); + cluster.setDescription(getDesc()); + cluster.setOperator(1); + return cluster; + } + + /** + * create an cluster description + */ + private String getDesc() { + return "create an cluster to test "; + } + + /** + * create an cluster config + */ + private String getConfig() { + return "{\"k8s\":\"apiVersion: v1\\nclusters:\\n- cluster:\\n certificate-authority-data: LS0tLS1CRUdJTiBDRJUSUZJQ0FURS0tLS0tCg==\\n server: https:\\/\\/127.0.0.1:6443\\n name: kubernetes\\ncontexts:\\n- context:\\n cluster: kubernetes\\n user: kubernetes-admin\\n name: kubernetes-admin@kubernetes\\ncurrent-context: kubernetes-admin@kubernetes\\nkind: Config\\npreferences: {}\\nusers:\\n- name: kubernetes-admin\\n user:\\n client-certificate-data: LS0tLS1CRUdJTiBDRVJJ0cEhYYnBLRVktLS0tLQo=\"}\n"; + } + + /** + * create general user + */ + private User getGeneralUser() { + User loginUser = new User(); + loginUser.setUserType(UserType.GENERAL_USER); + loginUser.setUserName(testUserName); + loginUser.setId(1); + return loginUser; + } + + /** + * create admin user + */ + private User getAdminUser() { + User loginUser = new User(); + loginUser.setUserType(UserType.ADMIN_USER); + loginUser.setUserName(testUserName); + loginUser.setId(1); + return loginUser; + } + + private List getList() { + List list = new ArrayList<>(); + list.add(getCluster()); + return list; + } +} diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/EnvironmentServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/impl/EnvironmentServiceTest.java similarity index 61% rename from dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/EnvironmentServiceTest.java rename to dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/impl/EnvironmentServiceTest.java index b6b1eb5b0777c..14e1ed956b473 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/EnvironmentServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/impl/EnvironmentServiceTest.java @@ -15,16 +15,18 @@ * limitations under the License. */ -package org.apache.dolphinscheduler.api.service; +package org.apache.dolphinscheduler.api.service.impl; +import static org.apache.dolphinscheduler.api.AssertionsHelper.assertDoesNotThrow; +import static org.apache.dolphinscheduler.api.AssertionsHelper.assertThrowsServiceException; import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.ENVIRONMENT_CREATE; import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.ENVIRONMENT_DELETE; import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.ENVIRONMENT_UPDATE; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.permission.ResourcePermissionCheckService; -import org.apache.dolphinscheduler.api.service.impl.BaseServiceImpl; -import org.apache.dolphinscheduler.api.service.impl.EnvironmentServiceImpl; import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.constants.Constants; @@ -98,94 +100,74 @@ public class EnvironmentServiceTest { @Test public void testCreateEnvironment() { User loginUser = getGeneralUser(); - Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ENVIRONMENT, + when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ENVIRONMENT, loginUser.getId(), ENVIRONMENT_CREATE, baseServiceLogger)).thenReturn(true); - Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ENVIRONMENT, null, + when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ENVIRONMENT, null, 0, baseServiceLogger)).thenReturn(true); - Map result = - environmentService.createEnvironment(loginUser, environmentName, getConfig(), getDesc(), workerGroups); - logger.info(result.toString()); - Assertions.assertEquals(Status.USER_NO_OPERATION_PERM, result.get(Constants.STATUS)); - - loginUser = getAdminUser(); - result = environmentService.createEnvironment(loginUser, environmentName, "", getDesc(), workerGroups); - logger.info(result.toString()); - Assertions.assertEquals(Status.ENVIRONMENT_CONFIG_IS_NULL, result.get(Constants.STATUS)); - - result = environmentService.createEnvironment(loginUser, "", getConfig(), getDesc(), workerGroups); - logger.info(result.toString()); - Assertions.assertEquals(Status.ENVIRONMENT_NAME_IS_NULL, result.get(Constants.STATUS)); - - result = environmentService.createEnvironment(loginUser, environmentName, getConfig(), getDesc(), "test"); - logger.info(result.toString()); - Assertions.assertEquals(Status.ENVIRONMENT_WORKER_GROUPS_IS_INVALID, result.get(Constants.STATUS)); - - Mockito.when(environmentMapper.queryByEnvironmentName(environmentName)).thenReturn(getEnvironment()); - result = environmentService.createEnvironment(loginUser, environmentName, getConfig(), getDesc(), workerGroups); - logger.info(result.toString()); - Assertions.assertEquals(Status.ENVIRONMENT_NAME_EXISTS, result.get(Constants.STATUS)); - - Mockito.when(environmentMapper.insert(Mockito.any(Environment.class))).thenReturn(1); - Mockito.when(relationMapper.insert(Mockito.any(EnvironmentWorkerGroupRelation.class))).thenReturn(1); - result = environmentService.createEnvironment(loginUser, "testName", "test", "test", workerGroups); - logger.info(result.toString()); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); + assertThrowsServiceException(Status.USER_NO_OPERATION_PERM, () -> environmentService + .createEnvironment(loginUser, environmentName, getConfig(), getDesc(), workerGroups)); + + User adminUser = getAdminUser(); + assertThrowsServiceException(Status.ENVIRONMENT_CONFIG_IS_NULL, + () -> environmentService.createEnvironment(adminUser, environmentName, "", getDesc(), workerGroups)); + assertThrowsServiceException(Status.ENVIRONMENT_NAME_IS_NULL, + () -> environmentService.createEnvironment(adminUser, "", getConfig(), getDesc(), workerGroups)); + assertThrowsServiceException(Status.ENVIRONMENT_WORKER_GROUPS_IS_INVALID, + () -> environmentService.createEnvironment(adminUser, environmentName, getConfig(), getDesc(), "test")); + + when(environmentMapper.queryByEnvironmentName(environmentName)).thenReturn(getEnvironment()); + assertThrowsServiceException(Status.ENVIRONMENT_NAME_EXISTS, () -> environmentService + .createEnvironment(adminUser, environmentName, getConfig(), getDesc(), workerGroups)); + + when(environmentMapper.insert(any(Environment.class))).thenReturn(1); + when(relationMapper.insert(any(EnvironmentWorkerGroupRelation.class))).thenReturn(1); + assertDoesNotThrow( + () -> environmentService.createEnvironment(adminUser, "testName", "test", "test", workerGroups)); } @Test public void testCheckParams() { - Map result = environmentService.checkParams(environmentName, getConfig(), "test"); - Assertions.assertEquals(Status.ENVIRONMENT_WORKER_GROUPS_IS_INVALID, result.get(Constants.STATUS)); + assertThrowsServiceException(Status.ENVIRONMENT_WORKER_GROUPS_IS_INVALID, + () -> environmentService.checkParams(environmentName, getConfig(), "test")); } @Test public void testUpdateEnvironmentByCode() { - User loginUser = getGeneralUser(); - Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ENVIRONMENT, - loginUser.getId(), ENVIRONMENT_UPDATE, baseServiceLogger)).thenReturn(true); - Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ENVIRONMENT, null, - 0, baseServiceLogger)).thenReturn(true); - Map result = environmentService.updateEnvironmentByCode(loginUser, 1L, environmentName, - getConfig(), getDesc(), workerGroups); - logger.info(result.toString()); - Assertions.assertEquals(Status.USER_NO_OPERATION_PERM, result.get(Constants.STATUS)); - - loginUser = getAdminUser(); - result = environmentService.updateEnvironmentByCode(loginUser, 1L, environmentName, "", getDesc(), - workerGroups); - logger.info(result.toString()); - Assertions.assertEquals(Status.ENVIRONMENT_CONFIG_IS_NULL, result.get(Constants.STATUS)); - - result = environmentService.updateEnvironmentByCode(loginUser, 1L, "", getConfig(), getDesc(), workerGroups); - logger.info(result.toString()); - Assertions.assertEquals(Status.ENVIRONMENT_NAME_IS_NULL, result.get(Constants.STATUS)); - - result = environmentService.updateEnvironmentByCode(loginUser, 1L, environmentName, getConfig(), getDesc(), - "test"); - logger.info(result.toString()); - Assertions.assertEquals(Status.ENVIRONMENT_WORKER_GROUPS_IS_INVALID, result.get(Constants.STATUS)); - - Mockito.when(environmentMapper.queryByEnvironmentName(environmentName)).thenReturn(getEnvironment()); - result = environmentService.updateEnvironmentByCode(loginUser, 2L, environmentName, getConfig(), getDesc(), - workerGroups); - logger.info(result.toString()); - Assertions.assertEquals(Status.ENVIRONMENT_NAME_EXISTS, result.get(Constants.STATUS)); - - Mockito.when(environmentMapper.update(Mockito.any(Environment.class), Mockito.any(Wrapper.class))) - .thenReturn(1); - result = environmentService.updateEnvironmentByCode(loginUser, 1L, "testName", "test", "test", workerGroups); - logger.info(result.toString()); - Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); + final User loginUser = getGeneralUser(); + when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ENVIRONMENT, loginUser.getId(), + ENVIRONMENT_UPDATE, baseServiceLogger)).thenReturn(true); + when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ENVIRONMENT, null, 0, + baseServiceLogger)).thenReturn(true); + assertThrowsServiceException(Status.USER_NO_OPERATION_PERM, () -> environmentService + .updateEnvironmentByCode(loginUser, 1L, environmentName, getConfig(), getDesc(), workerGroups)); + + final User adminUser = getAdminUser(); + assertThrowsServiceException(Status.ENVIRONMENT_CONFIG_IS_NULL, () -> environmentService + .updateEnvironmentByCode(adminUser, 1L, environmentName, "", getDesc(), workerGroups)); + + assertThrowsServiceException(Status.ENVIRONMENT_NAME_IS_NULL, () -> environmentService + .updateEnvironmentByCode(adminUser, 1L, "", getConfig(), getDesc(), workerGroups)); + + assertThrowsServiceException(Status.ENVIRONMENT_WORKER_GROUPS_IS_INVALID, () -> environmentService + .updateEnvironmentByCode(adminUser, 1L, environmentName, getConfig(), getDesc(), "test")); + + when(environmentMapper.queryByEnvironmentName(environmentName)).thenReturn(getEnvironment()); + assertThrowsServiceException(Status.ENVIRONMENT_NAME_EXISTS, () -> environmentService + .updateEnvironmentByCode(adminUser, 2L, environmentName, getConfig(), getDesc(), workerGroups)); + + when(environmentMapper.update(any(Environment.class), any(Wrapper.class))).thenReturn(1); + assertDoesNotThrow(() -> environmentService.updateEnvironmentByCode(adminUser, 1L, "testName", "test", "test", + workerGroups)); } @Test public void testQueryAllEnvironmentList() { Set ids = new HashSet<>(); ids.add(1); - Mockito.when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.ENVIRONMENT, + when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.ENVIRONMENT, 1, environmentServiceLogger)).thenReturn(ids); - Mockito.when(environmentMapper.selectBatchIds(ids)).thenReturn(Lists.newArrayList(getEnvironment())); + when(environmentMapper.selectBatchIds(ids)).thenReturn(Lists.newArrayList(getEnvironment())); Map result = environmentService.queryAllEnvironmentList(getAdminUser()); logger.info(result.toString()); @@ -200,7 +182,7 @@ public void testQueryEnvironmentListPaging() { IPage page = new Page<>(1, 10); page.setRecords(getList()); page.setTotal(1L); - Mockito.when(environmentMapper.queryEnvironmentListPaging(Mockito.any(Page.class), Mockito.eq(environmentName))) + when(environmentMapper.queryEnvironmentListPaging(any(Page.class), Mockito.eq(environmentName))) .thenReturn(page); Result result = environmentService.queryEnvironmentListPaging(getAdminUser(), 1, 10, environmentName); @@ -211,12 +193,12 @@ public void testQueryEnvironmentListPaging() { @Test public void testQueryEnvironmentByName() { - Mockito.when(environmentMapper.queryByEnvironmentName(environmentName)).thenReturn(null); + when(environmentMapper.queryByEnvironmentName(environmentName)).thenReturn(null); Map result = environmentService.queryEnvironmentByName(environmentName); logger.info(result.toString()); Assertions.assertEquals(Status.QUERY_ENVIRONMENT_BY_NAME_ERROR, result.get(Constants.STATUS)); - Mockito.when(environmentMapper.queryByEnvironmentName(environmentName)).thenReturn(getEnvironment()); + when(environmentMapper.queryByEnvironmentName(environmentName)).thenReturn(getEnvironment()); result = environmentService.queryEnvironmentByName(environmentName); logger.info(result.toString()); Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); @@ -224,12 +206,12 @@ public void testQueryEnvironmentByName() { @Test public void testQueryEnvironmentByCode() { - Mockito.when(environmentMapper.queryByEnvironmentCode(1L)).thenReturn(null); + when(environmentMapper.queryByEnvironmentCode(1L)).thenReturn(null); Map result = environmentService.queryEnvironmentByCode(1L); logger.info(result.toString()); Assertions.assertEquals(Status.QUERY_ENVIRONMENT_BY_CODE_ERROR, result.get(Constants.STATUS)); - Mockito.when(environmentMapper.queryByEnvironmentCode(1L)).thenReturn(getEnvironment()); + when(environmentMapper.queryByEnvironmentCode(1L)).thenReturn(getEnvironment()); result = environmentService.queryEnvironmentByCode(1L); logger.info(result.toString()); Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); @@ -238,22 +220,22 @@ public void testQueryEnvironmentByCode() { @Test public void testDeleteEnvironmentByCode() { User loginUser = getGeneralUser(); - Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ENVIRONMENT, + when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ENVIRONMENT, loginUser.getId(), ENVIRONMENT_DELETE, baseServiceLogger)).thenReturn(true); - Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ENVIRONMENT, null, + when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ENVIRONMENT, null, 0, baseServiceLogger)).thenReturn(true); Map result = environmentService.deleteEnvironmentByCode(loginUser, 1L); logger.info(result.toString()); Assertions.assertEquals(Status.USER_NO_OPERATION_PERM, result.get(Constants.STATUS)); loginUser = getAdminUser(); - Mockito.when(taskDefinitionMapper.selectCount(Mockito.any(LambdaQueryWrapper.class))).thenReturn(1L); + when(taskDefinitionMapper.selectCount(any(LambdaQueryWrapper.class))).thenReturn(1L); result = environmentService.deleteEnvironmentByCode(loginUser, 1L); logger.info(result.toString()); Assertions.assertEquals(Status.DELETE_ENVIRONMENT_RELATED_TASK_EXISTS, result.get(Constants.STATUS)); - Mockito.when(taskDefinitionMapper.selectCount(Mockito.any(LambdaQueryWrapper.class))).thenReturn(0L); - Mockito.when(environmentMapper.deleteByCode(1L)).thenReturn(1); + when(taskDefinitionMapper.selectCount(any(LambdaQueryWrapper.class))).thenReturn(0L); + when(environmentMapper.deleteByCode(1L)).thenReturn(1); result = environmentService.deleteEnvironmentByCode(loginUser, 1L); logger.info(result.toString()); Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); @@ -265,7 +247,7 @@ public void testVerifyEnvironment() { logger.info(result.toString()); Assertions.assertEquals(Status.ENVIRONMENT_NAME_IS_NULL, result.get(Constants.STATUS)); - Mockito.when(environmentMapper.queryByEnvironmentName(environmentName)).thenReturn(getEnvironment()); + when(environmentMapper.queryByEnvironmentName(environmentName)).thenReturn(getEnvironment()); result = environmentService.verifyEnvironment(environmentName); logger.info(result.toString()); Assertions.assertEquals(Status.ENVIRONMENT_NAME_EXISTS, result.get(Constants.STATUS)); diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/Tenant.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/Tenant.java index 16675df65c877..4775fccd4696d 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/Tenant.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/Tenant.java @@ -17,7 +17,6 @@ package org.apache.dolphinscheduler.dao.entity; import java.util.Date; -import java.util.Objects; import lombok.Data; @@ -94,22 +93,4 @@ public Tenant(int id, String tenantCode, String description, int queueId) { this.updateTime = now; } - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - - Tenant tenant = (Tenant) o; - - return Objects.equals(id, tenant.id); - } - - @Override - public int hashCode() { - return Objects.hash(id); - } } diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/WorkFlowLineage.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/WorkFlowLineage.java index 7eb4cb3820a87..0f2e325da5adc 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/WorkFlowLineage.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/WorkFlowLineage.java @@ -19,6 +19,9 @@ import java.util.Date; +import lombok.Data; + +@Data public class WorkFlowLineage { private long workFlowCode; @@ -30,67 +33,4 @@ public class WorkFlowLineage { private int schedulePublishStatus; private String sourceWorkFlowCode; - public long getWorkFlowCode() { - return workFlowCode; - } - - public void setWorkFlowCode(long workFlowCode) { - this.workFlowCode = workFlowCode; - } - - public String getWorkFlowName() { - return workFlowName; - } - - public void setWorkFlowName(String workFlowName) { - this.workFlowName = workFlowName; - } - - public String getWorkFlowPublishStatus() { - return workFlowPublishStatus; - } - - public void setWorkFlowPublishStatus(String workFlowPublishStatus) { - this.workFlowPublishStatus = workFlowPublishStatus; - } - - public Date getScheduleStartTime() { - return scheduleStartTime; - } - - public void setScheduleStartTime(Date scheduleStartTime) { - this.scheduleStartTime = scheduleStartTime; - } - - public Date getScheduleEndTime() { - return scheduleEndTime; - } - - public void setScheduleEndTime(Date scheduleEndTime) { - this.scheduleEndTime = scheduleEndTime; - } - - public String getCrontab() { - return crontab; - } - - public void setCrontab(String crontab) { - this.crontab = crontab; - } - - public int getSchedulePublishStatus() { - return schedulePublishStatus; - } - - public void setSchedulePublishStatus(int schedulePublishStatus) { - this.schedulePublishStatus = schedulePublishStatus; - } - - public String getSourceWorkFlowCode() { - return sourceWorkFlowCode; - } - - public void setSourceWorkFlowCode(String sourceWorkFlowCode) { - this.sourceWorkFlowCode = sourceWorkFlowCode; - } } diff --git a/dolphinscheduler-standalone-server/src/main/resources/logback-spring.xml b/dolphinscheduler-standalone-server/src/main/resources/logback-spring.xml index bc2d3a405feb8..c58b7951f7d23 100644 --- a/dolphinscheduler-standalone-server/src/main/resources/logback-spring.xml +++ b/dolphinscheduler-standalone-server/src/main/resources/logback-spring.xml @@ -23,7 +23,7 @@ - [%level] %date{yyyy-MM-dd HH:mm:ss.SSS Z} %logger{96}:[%line] - [WorkflowInstance-%X{workflowInstanceId:-0}][TaskInstance-%X{taskInstanceId:-0}] - %msg%n + [WI-%X{workflowInstanceId:-0}][TI-%X{taskInstanceId:-0}] - [%level] %date{yyyy-MM-dd HH:mm:ss.SSS Z} %logger{96}:[%line] - %msg%n UTF-8 diff --git a/dolphinscheduler-tools/src/test/resources/logback.xml b/dolphinscheduler-tools/src/test/resources/logback.xml index d820a8d013ac7..aea53c51c3162 100644 --- a/dolphinscheduler-tools/src/test/resources/logback.xml +++ b/dolphinscheduler-tools/src/test/resources/logback.xml @@ -29,7 +29,7 @@ - +