From 9be81be32824c5e639cf98fcecdcf49e9b1d23f9 Mon Sep 17 00:00:00 2001 From: Wenjun Ruan Date: Mon, 20 Nov 2023 17:04:03 +0800 Subject: [PATCH] Remove API Result in Service (#15181) --- .../api/controller/AccessTokenController.java | 77 ++--- .../api/controller/AlertGroupController.java | 81 +++-- .../AlertPluginInstanceController.java | 86 +++--- .../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 | 32 +- .../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 | 156 ++++------ .../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 | 70 ++--- .../controller/DataQualityControllerTest.java | 53 ++-- .../WorkFlowLineageControllerTest.java | 22 +- .../v2/StatisticsV2ControllerTest.java | 67 ++-- .../api/service/AccessTokenServiceTest.java | 89 +++--- .../api/service/AlertGroupServiceTest.java | 165 +++++----- .../AlertPluginInstanceServiceTest.java | 102 +++--- .../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, 2134 insertions(+), 3367 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 dcc083f4a3..781c08ad62 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 static org.apache.dolphinscheduler.api.enums.Status.UPDATE_ACCESS_TOKEN_E 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 class AccessTokenController extends BaseController { @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 class AccessTokenController extends BaseController { @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 class AccessTokenController extends BaseController { @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 class AccessTokenController extends BaseController { @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 class AccessTokenController extends BaseController { @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 9ac7a82db0..a1f0b19e3c 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 static org.apache.dolphinscheduler.api.enums.Status.UPDATE_ALERT_GROUP_ER 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 class AlertGroupController extends BaseController { @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 class AlertGroupController extends BaseController { @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 class AlertGroupController extends BaseController { @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 class AlertGroupController extends BaseController { @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 class AlertGroupController extends BaseController { @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 class AlertGroupController extends BaseController { @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 a676bf283f..143116fc8e 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 @@ -28,14 +28,17 @@ import static org.apache.dolphinscheduler.api.enums.Status.UPDATE_ALERT_PLUGIN_I 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; @@ -88,16 +91,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); } @Operation(summary = "testSendAlertPluginInstance", description = "TEST_SEND_ALERT_PLUGIN_INSTANCE") @@ -108,9 +110,10 @@ public class AlertPluginInstanceController extends BaseController { @PostMapping(value = "/test-send") @ResponseStatus(HttpStatus.OK) @ApiException(SEND_TEST_ALERT_PLUGIN_INSTANCE_ERROR) - public Result testSendAlertPluginInstance(@RequestParam(value = "pluginDefineId") int pluginDefineId, - @RequestParam(value = "pluginInstanceParams") String pluginInstanceParams) { - return alertPluginInstanceService.testSend(pluginDefineId, pluginInstanceParams); + public Result testSendAlertPluginInstance(@RequestParam(value = "pluginDefineId") int pluginDefineId, + @RequestParam(value = "pluginInstanceParams") String pluginInstanceParams) { + alertPluginInstanceService.testSend(pluginDefineId, pluginInstanceParams); + return Result.success(true); } /** @@ -131,14 +134,14 @@ public class AlertPluginInstanceController extends BaseController { @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); } /** @@ -155,11 +158,11 @@ public class AlertPluginInstanceController extends BaseController { @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); } /** @@ -173,10 +176,10 @@ public class AlertPluginInstanceController extends BaseController { @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); } /** @@ -189,9 +192,9 @@ public class AlertPluginInstanceController extends BaseController { @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); } /** @@ -237,16 +240,15 @@ public class AlertPluginInstanceController extends BaseController { @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 ee258df615..6d334951e8 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 @@ package org.apache.dolphinscheduler.api.controller; 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 89205621ad..f571e2d6b4 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_FORW 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 524eedeefe..00ad208d1c 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.QUERY_CLUSTER_ERROR; 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 class ClusterController extends BaseController { @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 class ClusterController extends BaseController { @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 class ClusterController extends BaseController { @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 class ClusterController extends BaseController { @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 class ClusterController extends BaseController { @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 class ClusterController extends BaseController { @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 dcd8ac2d8c..a044b05dbb 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.COUNT_PROCESS_INSTANC 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 class DataAnalysisController extends BaseController { @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 class DataAnalysisController extends BaseController { @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 class DataAnalysisController extends BaseController { @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 class DataAnalysisController extends BaseController { @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 fdca568dbe..dc4dc32a32 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 static org.apache.dolphinscheduler.api.enums.Status.QUERY_RULE_LIST_PAGIN 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 class DataQualityController extends BaseController { @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 class DataQualityController extends BaseController { @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 class DataQualityController extends BaseController { @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 949d21c874..c50a6d3da6 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 class DataSourceController extends BaseController { @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 class DataSourceController extends BaseController { @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 class DataSourceController extends BaseController { @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 class DataSourceController extends BaseController { @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 class DataSourceController extends BaseController { @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 class DataSourceController extends BaseController { @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 47e7d09284..3e33da8909 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.exceptions.ApiException; 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 class EnvironmentController extends BaseController { @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 class EnvironmentController extends BaseController { @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 d6176cfd70..22d44a08bf 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 class ExecutorController extends BaseController { @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 class ExecutorController extends BaseController { 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 48ef34dd41..4c8cae74b9 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 class K8sNamespaceController extends BaseController { @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 435850537d..433fade281 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 class LoggerController extends BaseController { @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 779d230cd4..26c863b93f 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.exceptions.ApiException; 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 class MonitorController extends BaseController { @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 class MonitorController extends BaseController { @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 65589cea0d..1ccd92dab6 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 class ProcessDefinitionController extends BaseController { @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 class ProcessDefinitionController extends BaseController { @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 09b4001339..85f9cbcf08 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 class ProcessInstanceController extends BaseController { @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 cdc15865b3..7618126f91 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 class ProjectController extends BaseController { @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 class ProjectController extends BaseController { @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 0c73801c7a..706c7e939a 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 class ProjectParameterController extends BaseController { @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 65e23a9069..dbbc7d858e 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 static org.apache.dolphinscheduler.api.enums.Status.VERIFY_QUEUE_ERROR; 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 class QueueController extends BaseController { @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 class QueueController extends BaseController { @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 class QueueController extends BaseController { @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 class QueueController extends BaseController { @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 class QueueController extends BaseController { @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 69c19b32b3..6fff27acaf 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 class ResourcesController extends BaseController { @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 class ResourcesController extends BaseController { @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 164127c4e6..57b505c650 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 class SchedulerController extends BaseController { @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 e22e0eb00d..458f747255 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 class TaskDefinitionController extends BaseController { @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 class TaskDefinitionController extends BaseController { @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 1136e06afe..2aa2a5eb72 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 class TaskInstanceController extends BaseController { @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 class TaskInstanceController extends BaseController { 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 4e7367263a..7554d6135d 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 static org.apache.dolphinscheduler.api.enums.Status.VERIFY_OS_TENANT_CODE 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 class TenantController extends BaseController { @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 class TenantController extends BaseController { @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 class TenantController extends BaseController { @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 class TenantController extends BaseController { @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 class TenantController extends BaseController { @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 855d2e3772..c376c5a7fc 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 class UsersController extends BaseController { @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 4dc4973506..96e2a200d6 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 29be9c61df..edd6d36879 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 class WorkerGroupController extends BaseController { @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 6012c7ad58..e7a8115bdd 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 static org.apache.dolphinscheduler.api.enums.Status.CREATE_ACCESS_TOKEN_E 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 c547cfb9a5..d01aeb8658 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.controller.BaseController; 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.project.ProjectUpdateResponse; 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 class ProjectV2Controller extends BaseController { @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 5a7c332910..cd6563e7a2 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 static org.apache.dolphinscheduler.api.enums.Status.VERIFY_QUEUE_ERROR; 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 class QueueV2Controller extends BaseController { @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 class QueueV2Controller extends BaseController { @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 class QueueV2Controller extends BaseController { @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 class QueueV2Controller extends BaseController { @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 bab04989f3..f23a29671c 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_TASK_STATES_COU 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 class StatisticsV2Controller extends BaseController { /** * 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 class StatisticsV2Controller extends BaseController { @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 class StatisticsV2Controller extends BaseController { @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 class StatisticsV2Controller extends BaseController { @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 class StatisticsV2Controller extends BaseController { @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 ca956374a3..3e3a87681b 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_SAVEPOINT_ERROR; 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 class TaskInstanceV2Controller extends BaseController { 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 eecd0605ce..c44e0c4f8a 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 6d508f40d6..0000000000 --- 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 da4f542cbf..0000000000 --- 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 d0bfbb2abf..0000000000 --- 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 a0edf4b315..0000000000 --- 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 fea6c9079f..0000000000 --- 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 44f2de8604..ce406519e6 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 7a7b6700d5..3c978795ab 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 @@ import org.springframework.web.method.HandlerMethod; 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 20df36fa03..f3c45daaea 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 @@ public interface ResourcePermissionCheckService { */ 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 6ec43871cf..b35a89cfdf 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 class ResourcePermissionCheckServiceImpl 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 2dbfc4dce5..9322163620 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 096d5c3b7b..c56b6e3e85 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 @@ public interface AlertGroupService { * @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 e8679c27af..91cc00e00a 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 @@ public interface AlertPluginInstanceService { * @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 @@ public interface AlertPluginInstanceService { * @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,7 +101,7 @@ public interface AlertPluginInstanceService { * @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); - Result testSend(int pluginDefineId, String pluginInstanceParams); + void testSend(int pluginDefineId, String pluginInstanceParams); } 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 3c1c61d07c..7b998dccce 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 3d5d3197dc..234e163c99 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.common.enums.AuthorizationType; 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 0b542579db..01d5e1bec1 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 2417f796ab..48131412c2 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 @@ public interface DataAnalysisService { * @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 @@ public interface DataAnalysisService { * @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 @@ public interface DataAnalysisService { * @param loginUser login user * @return command state count data */ - Map countCommandState(User loginUser); + List countCommandState(User loginUser); /** * count queue state @@ -92,7 +95,7 @@ public interface DataAnalysisService { * @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 @@ public interface DataAnalysisService { * @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 @@ public interface DataAnalysisService { * @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 @@ public interface DataAnalysisService { * @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 @@ public interface DataAnalysisService { * @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 42ab666aa1..a7382ee51a 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 d8e3468bbb..02842ae74f 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 @@ import org.apache.dolphinscheduler.dao.entity.User; */ 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 df6d68d71e..8b331f4395 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 ebedea43fe..c2a2511f21 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 408fdee7f3..3166d3e718 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 @@ public interface ExecutorService { * @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 db1cb7c465..fe94f7ec43 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.api.utils.Result; 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 e881b2e346..c4a8d94a1f 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 d1eb4b2ada..4736633fa1 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 69ed518445..0cafe70d5e 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 fc5abf4b6b..6841db986d 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 @@ public interface TenantService { * @param loginUser login user * @return tenant list */ - Map queryTenantList(User loginUser); + List queryTenantList(User loginUser); /** * verify tenant code @@ -92,7 +95,7 @@ public interface TenantService { * @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 04902a4d0d..5b36555360 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 @@ import java.util.Set; */ 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 2a264d6948..ebb151c604 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.ApiFuncIdentificationCon 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.dolphinscheduler.dao.mapper.AccessTokenMapper; 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 class AccessTokenServiceImpl extends BaseServiceImpl implements AccessTok 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 class AccessTokenServiceImpl extends BaseServiceImpl implements AccessTok */ @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 class AccessTokenServiceImpl extends BaseServiceImpl implements AccessTok 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 class AccessTokenServiceImpl extends BaseServiceImpl implements AccessTok 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 5f04caaaac..a599eb9ce7 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.ApiFuncIdentificationCon 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 class AlertGroupServiceImpl extends BaseServiceImpl implements AlertGroup * * @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 class AlertGroupServiceImpl extends BaseServiceImpl implements AlertGroup */ @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 class AlertGroupServiceImpl extends BaseServiceImpl implements AlertGroup 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 class AlertGroupServiceImpl extends BaseServiceImpl implements AlertGroup * @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 class AlertGroupServiceImpl extends BaseServiceImpl implements AlertGroup 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 65ba0c0334..59eaca3c83 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 static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationCon 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; @@ -55,7 +54,6 @@ import org.apache.commons.lang3.StringUtils; 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; @@ -67,6 +65,7 @@ import lombok.extern.slf4j.Slf4j; 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; @@ -96,15 +95,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); @@ -113,16 +120,8 @@ public class AlertPluginInstanceServiceImpl extends BaseServiceImpl implements A 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); @@ -142,94 +141,77 @@ public class AlertPluginInstanceServiceImpl extends BaseServiceImpl implements A } 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); } /** @@ -240,33 +222,18 @@ public class AlertPluginInstanceServiceImpl extends BaseServiceImpl implements A * @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; - } - - if (null != alertPluginInstance) { - putMsg(result, Status.SUCCESS); - result.put(Constants.DATA_LIST, alertPluginInstance); + throw new ServiceException(Status.USER_NO_OPERATION_PERM); } - - 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 @@ -275,19 +242,15 @@ public class AlertPluginInstanceServiceImpl extends BaseServiceImpl implements A } @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) { @@ -373,13 +336,10 @@ public class AlertPluginInstanceServiceImpl extends BaseServiceImpl implements A } @Override - public Result testSend(int pluginDefineId, String pluginInstanceParams) { - Result result = new Result<>(); + public void testSend(int pluginDefineId, String pluginInstanceParams) { Optional alertServerAddressOptional = getAlertServerAddress(); if (!alertServerAddressOptional.isPresent()) { - log.error("Cannot get alert server address, please check the alert server is running"); - putMsg(result, Status.ALERT_SERVER_NOT_EXIST); - return result; + throw new ServiceException(Status.ALERT_SERVER_NOT_EXIST); } Host alertServerAddress = alertServerAddressOptional.get(); @@ -396,16 +356,12 @@ public class AlertPluginInstanceServiceImpl extends BaseServiceImpl implements A log.info("Send alert to: {} successfully, response: {}", alertServerAddress, alertSendResponse); } catch (Exception e) { log.error("Send alert: {} to: {} failed", alertTestSendRequest, alertServerAddress, e); - putMsg(result, Status.ALERT_TEST_SENDING_FAILED, e.getMessage()); - return result; + throw new ServiceException(Status.ALERT_TEST_SENDING_FAILED, e.getMessage()); } if (alertSendResponse.isSuccess()) { - putMsg(result, Status.SUCCESS); - } else { - putMsg(result, Status.ALERT_TEST_SENDING_FAILED, alertSendResponse.getResResults().get(0).getMessage()); + throw new ServiceException(Status.ALERT_TEST_SENDING_FAILED, + alertSendResponse.getResResults().get(0).getMessage()); } - - return result; } } 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 4fdf0a169a..759e30b036 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 @@ package org.apache.dolphinscheduler.api.service.impl; 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 class AuditServiceImpl extends BaseServiceImpl implements AuditService { * @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 class AuditServiceImpl extends BaseServiceImpl implements AuditService { 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 b2fb11d3de..6d9d1868a4 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 org.apache.commons.lang3.StringUtils; 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 class BaseServiceImpl implements BaseService { * 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 8c181c00fe..a5ceb92abc 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 @@ package org.apache.dolphinscheduler.api.service.impl; 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.dolphinscheduler.service.utils.ClusterConfUtils; 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 class ClusterServiceImpl extends BaseServiceImpl implements ClusterServic 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 class ClusterServiceImpl extends BaseServiceImpl implements ClusterServic * @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 class ClusterServiceImpl extends BaseServiceImpl implements ClusterServic 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 class ClusterServiceImpl extends BaseServiceImpl implements ClusterServic * @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 class ClusterServiceImpl extends BaseServiceImpl implements ClusterServic * @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 class ClusterServiceImpl extends BaseServiceImpl implements ClusterServic * @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 class ClusterServiceImpl extends BaseServiceImpl implements ClusterServic * @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 class ClusterServiceImpl extends BaseServiceImpl implements ClusterServic * @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 class ClusterServiceImpl extends BaseServiceImpl implements ClusterServic 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 class ClusterServiceImpl extends BaseServiceImpl implements ClusterServic * @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 6ae4977b81..aa716cc6b2 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.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.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.common.utils.TriFunction; 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 class DataAnalysisServiceImpl extends BaseServiceImpl implements DataAnal * @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 class DataAnalysisServiceImpl extends BaseServiceImpl implements DataAnal * @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 @@ public class DataAnalysisServiceImpl extends BaseServiceImpl implements DataAnal 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 @@ public class DataAnalysisServiceImpl extends BaseServiceImpl implements DataAnal } 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 @@ public class DataAnalysisServiceImpl extends BaseServiceImpl implements DataAnal * @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 class DataAnalysisServiceImpl extends BaseServiceImpl implements DataAnal * @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 class DataAnalysisServiceImpl extends BaseServiceImpl implements DataAnal */ 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 class DataAnalysisServiceImpl extends BaseServiceImpl implements DataAnal 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 @@ public class DataAnalysisServiceImpl extends BaseServiceImpl implements DataAnal * @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 class DataAnalysisServiceImpl extends BaseServiceImpl implements DataAnal 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 class DataAnalysisServiceImpl extends BaseServiceImpl implements DataAnal /** * 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 class DataAnalysisServiceImpl extends BaseServiceImpl implements DataAnal 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 class DataAnalysisServiceImpl extends BaseServiceImpl implements DataAnal 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 class DataAnalysisServiceImpl extends BaseServiceImpl implements DataAnal 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 class DataAnalysisServiceImpl extends BaseServiceImpl implements DataAnal 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 class DataAnalysisServiceImpl extends BaseServiceImpl implements DataAnal * @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 class DataAnalysisServiceImpl extends BaseServiceImpl implements DataAnal 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 c095c2bd62..0ac7763ba7 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.enums.Status; 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 class DataSourceServiceImpl extends BaseServiceImpl implements DataSource 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 class DataSourceServiceImpl extends BaseServiceImpl implements DataSource 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 class DataSourceServiceImpl extends BaseServiceImpl implements DataSource * @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 class DataSourceServiceImpl extends BaseServiceImpl implements DataSource * @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 f61a82f76d..ec1c7d47d1 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 class DqExecuteResultServiceImpl extends BaseServiceImpl implements DqExe 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 class DqExecuteResultServiceImpl extends BaseServiceImpl implements DqExe 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 6aca9b88f8..1be8599887 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 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.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 class DqRuleServiceImpl extends BaseServiceImpl implements DqRuleService 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 class DqRuleServiceImpl extends BaseServiceImpl implements DqRuleService 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 18de9c5412..ade9aea483 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 static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationCon 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 class EnvironmentServiceImpl extends BaseServiceImpl implements Environme 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 class EnvironmentServiceImpl extends BaseServiceImpl implements Environme }); } } - 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 class EnvironmentServiceImpl extends BaseServiceImpl implements Environme */ @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 class EnvironmentServiceImpl extends BaseServiceImpl implements Environme 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 class EnvironmentServiceImpl extends BaseServiceImpl implements Environme 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 class EnvironmentServiceImpl extends BaseServiceImpl implements Environme 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 @@ public class EnvironmentServiceImpl extends BaseServiceImpl implements Environme 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 dc5e761190..53530d6419 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 class ExecutorServiceImpl extends BaseServiceImpl implements ExecutorServ 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 class ExecutorServiceImpl extends BaseServiceImpl implements ExecutorServ 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 class ExecutorServiceImpl extends BaseServiceImpl implements ExecutorServ } @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 class ExecutorServiceImpl extends BaseServiceImpl implements ExecutorServ 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 4d25725761..f05e69b9db 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 class K8SNamespaceServiceImpl extends BaseServiceImpl implements K8sNames @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 class K8SNamespaceServiceImpl extends BaseServiceImpl implements K8sNames @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 @@ public class K8SNamespaceServiceImpl extends BaseServiceImpl implements K8sNames @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 class K8SNamespaceServiceImpl extends BaseServiceImpl implements K8sNames 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 ab4929648a..24dc41ce3a 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.File; 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 class LoggerServiceImpl extends BaseServiceImpl implements LoggerService */ @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 class LoggerServiceImpl extends BaseServiceImpl implements LoggerService */ @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 5ab42d3089..907ab2329c 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 class MonitorServiceImpl extends BaseServiceImpl implements MonitorServic * @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 class MonitorServiceImpl extends BaseServiceImpl implements MonitorServic * @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 class MonitorServiceImpl extends BaseServiceImpl implements MonitorServic }) .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 4884646173..0f57e55722 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.dolphinscheduler.dao.mapper.UserMapper; 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 class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic 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 class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic */ 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 class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic return checkResult; } + @Override + public void checkHasProjectWritePermissionThrowException(User loginUser, long projectCode) { + Project project = projectMapper.queryByCode(projectCode); + checkHasProjectWritePermissionThrowException(loginUser, project); + } + + @Override + public void checkHasProjectWritePermissionThrowException(User loginUser, Project project) { + if (project == null) { + throw new ServiceException(Status.PROJECT_NOT_FOUND, null); + } + // case 1: user is admin + if (loginUser.getUserType() == UserType.ADMIN_USER) { + return; + } + // case 2: user is project owner + if (project.getUserId().equals(loginUser.getId())) { + return; + } + // case 3: check user permission level + ProjectUser projectUser = projectUserMapper.queryProjectRelation(project.getId(), loginUser.getId()); + if (projectUser == null || projectUser.getPerm() != Constants.DEFAULT_ADMIN_PERMISSION) { + throw new ServiceException(Status.USER_NO_WRITE_PROJECT_PERM, loginUser.getUserName(), project.getCode()); + } + } + @Override public boolean hasProjectAndPerm(User loginUser, Project project, Result result, String permission) { boolean checkResult = false; diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/QueueServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/QueueServiceImpl.java index 723c3010c5..430f3a6969 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/QueueServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/QueueServiceImpl.java @@ -25,7 +25,6 @@ import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.exceptions.ServiceException; 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.common.enums.AuthorizationType; import org.apache.dolphinscheduler.common.enums.UserType; @@ -40,11 +39,8 @@ import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Objects; import java.util.Set; @@ -52,7 +48,6 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; 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; @@ -78,7 +73,7 @@ public class QueueServiceImpl extends BaseServiceImpl implements QueueService { * * @param queue The queue object want to create */ - private void createQueueValid(Queue queue) throws ServiceException { + private void validQueue(Queue queue) throws ServiceException { if (StringUtils.isEmpty(queue.getQueue())) { throw new ServiceException(Status.REQUEST_PARAMS_NOT_VALID_ERROR, Constants.QUEUE); } else if (StringUtils.isEmpty(queue.getQueueName())) { @@ -125,91 +120,73 @@ public class QueueServiceImpl extends BaseServiceImpl implements QueueService { * @return queue list */ @Override - public Result queryList(User loginUser) { - Result result = new Result(); + public List 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 class QueueServiceImpl extends BaseServiceImpl implements QueueService { } 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 class QueueServiceImpl extends BaseServiceImpl implements QueueService { } 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 class QueueServiceImpl extends BaseServiceImpl implements QueueService { * @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 class QueueServiceImpl extends BaseServiceImpl implements QueueService { 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 d06829bc7e..0a8e21071f 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 class ResourcesServiceImpl extends BaseServiceImpl implements ResourcesSe 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 e654332010..3122d090e2 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.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; @@ -129,8 +128,6 @@ public class TaskGroupServiceImpl extends BaseServiceImpl implements TaskGroupSe .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 fc5fc73159..e77baed1d3 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.QueueService; 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 @@ public class TenantServiceImpl extends BaseServiceImpl implements TenantService */ @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 class TenantServiceImpl extends BaseServiceImpl implements TenantService * @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 class TenantServiceImpl extends BaseServiceImpl implements TenantService * @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 class TenantServiceImpl extends BaseServiceImpl implements TenantService 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 @@ public class TenantServiceImpl extends BaseServiceImpl implements TenantService * @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 class TenantServiceImpl extends BaseServiceImpl implements TenantService * @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 2bd9f3845b..05f8f6a20e 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 class UdfFuncServiceImpl extends BaseServiceImpl implements UdfFuncServic 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 7966b7f391..15f39d696f 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 @@ package org.apache.dolphinscheduler.api.service.impl; 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 e097e2ff58..88cd3247ae 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 @@ public class WorkerGroupServiceImpl extends BaseServiceImpl implements WorkerGro 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 ba5735f402..d783fcf342 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 @@ package org.apache.dolphinscheduler.api.utils; 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 class PageInfo { 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 d7fa93c40c..d2da5bc638 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 86e382d238..5d8308760b 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 class AccessTokenControllerTest extends AbstractControllerTest { 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 7ade90985a..905767c395 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 class AccessTokenV2ControllerTest extends AbstractControllerTest { .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 b2ffbe2b2f..5ad7b2da52 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 @@ package org.apache.dolphinscheduler.api.controller; 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.request.MockMvcRequestBuilder 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.test.web.servlet.MvcResult; 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 class AlertPluginInstanceControllerTest extends AbstractControllerTest { 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,22 +84,18 @@ public class AlertPluginInstanceControllerTest extends AbstractControllerTest { // 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 testSendAlertPluginInstance() 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("pluginInstanceParams", pluginInstanceParams); - when(alertPluginInstanceService.testSend(eq(pluginDefineId), eq(pluginInstanceParams))) - .thenReturn(result); + doNothing().when(alertPluginInstanceService).testSend(eq(pluginDefineId), eq(pluginInstanceParams)); // When final MvcResult mvcResult = mockMvc.perform(post("/alert-plugin-instances/test-send") @@ -117,7 +108,8 @@ public class AlertPluginInstanceControllerTest extends AbstractControllerTest { // 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 @@ -129,9 +121,8 @@ public class AlertPluginInstanceControllerTest extends AbstractControllerTest { 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) @@ -144,7 +135,8 @@ public class AlertPluginInstanceControllerTest extends AbstractControllerTest { // 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 @@ -155,8 +147,7 @@ public class AlertPluginInstanceControllerTest extends AbstractControllerTest { 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) @@ -169,7 +160,8 @@ public class AlertPluginInstanceControllerTest extends AbstractControllerTest { // 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 @@ -178,8 +170,7 @@ public class AlertPluginInstanceControllerTest extends AbstractControllerTest { 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) @@ -192,14 +183,14 @@ public class AlertPluginInstanceControllerTest extends AbstractControllerTest { // 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") @@ -211,7 +202,8 @@ public class AlertPluginInstanceControllerTest extends AbstractControllerTest { // 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,7 +230,8 @@ public class AlertPluginInstanceControllerTest extends AbstractControllerTest { // 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 @@ -266,16 +259,13 @@ public class AlertPluginInstanceControllerTest extends AbstractControllerTest { // 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"); @@ -283,7 +273,7 @@ public class AlertPluginInstanceControllerTest extends AbstractControllerTest { 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") @@ -296,7 +286,8 @@ public class AlertPluginInstanceControllerTest extends AbstractControllerTest { // 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 @@ -323,6 +314,7 @@ public class AlertPluginInstanceControllerTest extends AbstractControllerTest { // 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 dbbda17334..04464550e9 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 @@ package org.apache.dolphinscheduler.api.controller; 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.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.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 org.apache.dolphinscheduler.plugin.task.api.enums.dp.RuleType; 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 class DataQualityControllerTest { } @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 class DataQualityControllerTest { 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 ead9554f41..b0f7bbc362 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 @@ public class WorkFlowLineageControllerTest { 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 d7c4c88389..f5c488581f 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 class StatisticsV2ControllerTest extends AbstractControllerTest { 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 28eb2bea08..8db009bb71 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.permission.ResourcePermissionCheckService 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.ArrayList; 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 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; 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 class AccessTokenServiceTest { 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 class AccessTokenServiceTest { 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 class AccessTokenServiceTest { 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 class AccessTokenServiceTest { 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 class AccessTokenServiceTest { 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 695ee3b5a4..85cbcbc833 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 org.apache.commons.collections4.CollectionUtils; 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 class AlertGroupServiceTest { 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 class AlertGroupServiceTest { 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 class AlertGroupServiceTest { 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 class AlertGroupServiceTest { 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 class AlertGroupServiceTest { 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 class AlertGroupServiceTest { // 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 8072c72e3b..52910858d5 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,16 +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.api.utils.Result; -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; @@ -40,7 +41,6 @@ import org.apache.dolphinscheduler.dao.mapper.AlertGroupMapper; import org.apache.dolphinscheduler.dao.mapper.AlertPluginInstanceMapper; import org.apache.dolphinscheduler.dao.mapper.PluginDefineMapper; import org.apache.dolphinscheduler.extract.alert.request.AlertSendResponse; -import org.apache.dolphinscheduler.extract.alert.request.AlertTestSendRequest; import org.apache.dolphinscheduler.registry.api.RegistryClient; import org.apache.dolphinscheduler.registry.api.enums.RegistryNodeType; @@ -48,7 +48,6 @@ import java.util.ArrayList; 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; @@ -185,83 +184,79 @@ public class AlertPluginInstanceServiceTest { @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 testSendAlert() { - Result result; Mockito.when(registryClient.getServerList(RegistryNodeType.ALERT_SERVER)).thenReturn(new ArrayList<>()); - result = alertPluginInstanceService.testSend(1, uiParams); - Assertions.assertEquals(Status.ALERT_SERVER_NOT_EXIST.getCode(), result.getCode()); + assertThrowsServiceException(Status.ALERT_SERVER_NOT_EXIST, + () -> alertPluginInstanceService.testSend(1, uiParams)); AlertSendResponse.AlertSendResponseResult alertResult = new AlertSendResponse.AlertSendResponseResult(); alertResult.setSuccess(true); - AlertTestSendRequest alertTestSendRequest = new AlertTestSendRequest( - 1, - uiParams); Server server = new Server(); server.setPort(50052); server.setHost("127.0.0.1"); Mockito.when(registryClient.getServerList(RegistryNodeType.ALERT_SERVER)) .thenReturn(Collections.singletonList(server)); - result = alertPluginInstanceService.testSend(1, uiParams); - Assertions.assertEquals(Status.ALERT_TEST_SENDING_FAILED.getCode(), result.getCode()); + assertThrowsServiceException(Status.ALERT_TEST_SENDING_FAILED, + () -> alertPluginInstanceService.testSend(1, uiParams)); } @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 @@ -271,10 +266,9 @@ public class AlertPluginInstanceServiceTest { 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 64ca0e72c2..3bde4a1b8d 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 @@ package org.apache.dolphinscheduler.api.service; 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 class AuditServiceTest { 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 deb04f4199..0000000000 --- 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 de25d0154f..1d15ce6107 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.Map; 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 class DataAnalysisServiceTest { 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 class DataAnalysisServiceTest { 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 class DataAnalysisServiceTest { 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 class DataAnalysisServiceTest { 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 class DataAnalysisServiceTest { 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 class DataAnalysisServiceTest { 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 411979542b..68d4db0ff2 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 class DataSourceServiceTest { 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 class DataSourceServiceTest { 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 class DataSourceServiceTest { 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 class DataSourceServiceTest { 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 class DataSourceServiceTest { @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 class DataSourceServiceTest { 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 class DataSourceServiceTest { // 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 class DataSourceServiceTest { 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 class DataSourceServiceTest { 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 class DataSourceServiceTest { 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 class DataSourceServiceTest { } 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 class DataSourceServiceTest { 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 class DataSourceServiceTest { 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 class DataSourceServiceTest { 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 6bfff0f6a1..4e43b5886c 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.ArrayList; 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 class DqExecuteResultServiceTest { 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 c87b74f05b..8c3af3e220 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 org.apache.dolphinscheduler.spi.params.base.FormType; 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 class DqRuleServiceTest { + "\"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 class DqRuleServiceTest { 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 06bd0aef98..b4b95ea497 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 class K8SNamespaceServiceTest { // 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 class K8SNamespaceServiceTest { // 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 0cabfdebd1..4f93043bb0 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 class LoggerServiceTest { 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 class LoggerServiceTest { // 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 class LoggerServiceTest { } // 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 class LoggerServiceTest { 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 class LoggerServiceTest { // 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 class LoggerServiceTest { } // 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 class LoggerServiceTest { @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 class LoggerServiceTest { 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 class LoggerServiceTest { 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 54808a0290..6bfbffd093 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 org.apache.commons.collections4.CollectionUtils; 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 class MonitorServiceTest { 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 ea70cf7f73..fbe019098a 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 class QueueServiceTest { 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 class QueueServiceTest { 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 class QueueServiceTest { 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 class QueueServiceTest { 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 class QueueServiceTest { 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 class QueueServiceTest { 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 class QueueServiceTest { 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 832b1f1c2c..5746840e90 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.dolphinscheduler.plugin.storage.api.StorageOperate; 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 org.slf4j.LoggerFactory; 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 class TenantServiceTest { 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 class TenantServiceTest { 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 1a5e97931b..316ed5d18e 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.ProjectMapper; 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 class WorkFlowLineageServiceTest { 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 0000000000..5733c2d480 --- /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 b6b1eb5b07..14e1ed956b 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 class EnvironmentServiceTest { 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 class EnvironmentServiceTest { @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 class EnvironmentServiceTest { @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 class EnvironmentServiceTest { @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 class EnvironmentServiceTest { 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 16675df65c..4775fccd46 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 class Tenant { 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 7eb4cb3820..0f2e325da5 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 @@ package org.apache.dolphinscheduler.dao.entity; 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 bc2d3a405f..c58b7951f7 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 d820a8d013..aea53c51c3 100644 --- a/dolphinscheduler-tools/src/test/resources/logback.xml +++ b/dolphinscheduler-tools/src/test/resources/logback.xml @@ -29,7 +29,7 @@ - +