diff --git a/docker/build/Dockerfile b/docker/build/Dockerfile index d0f16d5d0d..ceb94ea8c5 100644 --- a/docker/build/Dockerfile +++ b/docker/build/Dockerfile @@ -27,7 +27,7 @@ ENV DEBIAN_FRONTEND noninteractive #If install slowly, you can replcae alpine's mirror with aliyun's mirror, Example: #RUN sed -i "s/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g" /etc/apk/repositories RUN apk update && \ - apk add dos2unix shadow bash openrc python python3 sudo vim wget iputils net-tools openssh-server py2-pip tini && \ + apk --update add --no-cache dos2unix shadow bash openrc python2 python3 sudo vim wget iputils net-tools openssh-server py-pip tini && \ apk add --update procps && \ openrc boot && \ pip install kazoo 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 36c5c10799..ca0e1fc0ec 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 @@ -16,43 +16,15 @@ */ package org.apache.dolphinscheduler.api.service; -import org.apache.dolphinscheduler.api.enums.Status; -import org.apache.dolphinscheduler.api.utils.PageInfo; -import org.apache.dolphinscheduler.common.Constants; -import org.apache.dolphinscheduler.common.enums.UserType; -import org.apache.dolphinscheduler.dao.entity.ProcessDefinition; import org.apache.dolphinscheduler.dao.entity.Project; -import org.apache.dolphinscheduler.dao.entity.ProjectUser; import org.apache.dolphinscheduler.dao.entity.User; -import org.apache.dolphinscheduler.dao.mapper.ProcessDefinitionMapper; -import org.apache.dolphinscheduler.dao.mapper.ProjectMapper; -import org.apache.dolphinscheduler.dao.mapper.ProjectUserMapper; -import com.baomidou.mybatisplus.core.metadata.IPage; -import com.baomidou.mybatisplus.extension.plugins.pagination.Page; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import java.util.*; -import static org.apache.dolphinscheduler.api.utils.CheckUtils.checkDesc; + +import java.util.Map; /** * project service - *HttpTask./ **/ -@Service -public class ProjectService extends BaseService { - - private static final Logger logger = LoggerFactory.getLogger(ProjectService.class); - - @Autowired - private ProjectMapper projectMapper; - - @Autowired - private ProjectUserMapper projectUserMapper; - - @Autowired - private ProcessDefinitionMapper processDefinitionMapper; +public interface ProjectService { /** * create project @@ -62,38 +34,7 @@ public class ProjectService extends BaseService { * @param desc description * @return returns an error if it exists */ - public Map createProject(User loginUser, String name, String desc) { - - Map result = new HashMap<>(); - Map descCheck = checkDesc(desc); - if (descCheck.get(Constants.STATUS) != Status.SUCCESS) { - return descCheck; - } - - Project project = projectMapper.queryByName(name); - if (project != null) { - putMsg(result, Status.PROJECT_ALREADY_EXISTS, name); - return result; - } - project = new Project(); - Date now = new Date(); - - project.setName(name); - project.setDescription(desc); - project.setUserId(loginUser.getId()); - project.setUserName(loginUser.getUserName()); - project.setCreateTime(now); - project.setUpdateTime(now); - - if (projectMapper.insert(project) > 0) { - Project insertedProject = projectMapper.queryByName(name); - result.put(Constants.DATA_LIST, insertedProject); - putMsg(result, Status.SUCCESS); - } else { - putMsg(result, Status.CREATE_PROJECT_ERROR); - } - return result; - } + Map createProject(User loginUser, String name, String desc); /** * query project details by id @@ -101,19 +42,7 @@ public class ProjectService extends BaseService { * @param projectId project id * @return project detail information */ - public Map queryById(Integer projectId) { - - Map result = new HashMap<>(); - Project project = projectMapper.selectById(projectId); - - if (project != null) { - result.put(Constants.DATA_LIST, project); - putMsg(result, Status.SUCCESS); - } else { - putMsg(result, Status.PROJECT_NOT_FOUNT, projectId); - } - return result; - } + Map queryById(Integer projectId); /** * check project and authorization @@ -123,30 +52,9 @@ public class ProjectService extends BaseService { * @param projectName project name * @return true if the login user have permission to see the project */ - public Map checkProjectAndAuth(User loginUser, Project project, String projectName) { - Map result = new HashMap<>(); - if (project == null) { - putMsg(result, Status.PROJECT_NOT_FOUNT, projectName); - } else if (!checkReadPermission(loginUser, project)) { - // check read permission - putMsg(result, Status.USER_NO_OPERATION_PROJECT_PERM, loginUser.getUserName(), projectName); - }else { - putMsg(result, Status.SUCCESS); - } - return result; - } + Map checkProjectAndAuth(User loginUser, Project project, String projectName); - public boolean hasProjectAndPerm(User loginUser, Project project, Map result) { - boolean checkResult = false; - if (project == null) { - putMsg(result, Status.PROJECT_NOT_FOUNT, ""); - } else if (!checkReadPermission(loginUser, project)) { - putMsg(result, Status.USER_NO_OPERATION_PROJECT_PERM, loginUser.getUserName(), project.getName()); - } else { - checkResult = true; - } - return checkResult; - } + boolean hasProjectAndPerm(User loginUser, Project project, Map result); /** * admin can view all projects @@ -157,29 +65,7 @@ public class ProjectService extends BaseService { * @param pageNo page number * @return project list which the login user have permission to see */ - public Map queryProjectListPaging(User loginUser, Integer pageSize, Integer pageNo, String searchVal) { - Map result = new HashMap<>(); - PageInfo pageInfo = new PageInfo(pageNo, pageSize); - - Page page = new Page(pageNo, pageSize); - - int userId = loginUser.getUserType() == UserType.ADMIN_USER ? 0 : loginUser.getId(); - IPage projectIPage = projectMapper.queryProjectListPaging(page, userId, searchVal); - - List projectList = projectIPage.getRecords(); - if(userId != 0){ - for (Project project : projectList) { - project.setPerm(org.apache.dolphinscheduler.common.Constants.DEFAULT_ADMIN_PERMISSION); - } - } - pageInfo.setTotalCount((int)projectIPage.getTotal()); - pageInfo.setLists(projectList); - result.put(Constants.COUNT, (int)projectIPage.getTotal()); - result.put(Constants.DATA_LIST, pageInfo); - putMsg(result, Status.SUCCESS); - - return result; - } + Map queryProjectListPaging(User loginUser, Integer pageSize, Integer pageNo, String searchVal); /** * delete project by id @@ -188,50 +74,7 @@ public class ProjectService extends BaseService { * @param projectId project id * @return delete result code */ - public Map deleteProject(User loginUser, Integer projectId) { - Map result = new HashMap<>(); - Project project = projectMapper.selectById(projectId); - Map checkResult = getCheckResult(loginUser, project); - if (checkResult != null) { - return checkResult; - } - - if (!hasPerm(loginUser, project.getUserId())) { - putMsg(result, Status.USER_NO_OPERATION_PERM); - return result; - } - - List processDefinitionList = processDefinitionMapper.queryAllDefinitionList(projectId); - - if(processDefinitionList.size() > 0){ - putMsg(result, Status.DELETE_PROJECT_ERROR_DEFINES_NOT_NULL); - return result; - } - int delete = projectMapper.deleteById(projectId); - if (delete > 0) { - putMsg(result, Status.SUCCESS); - } else { - putMsg(result, Status.DELETE_PROJECT_ERROR); - } - return result; - } - - /** - * get check result - * - * @param loginUser login user - * @param project project - * @return check result - */ - private Map getCheckResult(User loginUser, Project project) { - String projectName = project == null ? null:project.getName(); - Map checkResult = checkProjectAndAuth(loginUser, project, projectName); - Status status = (Status) checkResult.get(Constants.STATUS); - if (status != Status.SUCCESS) { - return checkResult; - } - return null; - } + Map deleteProject(User loginUser, Integer projectId); /** * updateProcessInstance project @@ -242,37 +85,7 @@ public class ProjectService extends BaseService { * @param desc description * @return update result code */ - public Map update(User loginUser, Integer projectId, String projectName, String desc) { - Map result = new HashMap<>(); - - Map descCheck = checkDesc(desc); - if (descCheck.get(Constants.STATUS) != Status.SUCCESS) { - return descCheck; - } - - Project project = projectMapper.selectById(projectId); - boolean hasProjectAndPerm = hasProjectAndPerm(loginUser, project, result); - if (!hasProjectAndPerm) { - return result; - } - Project tempProject = projectMapper.queryByName(projectName); - if (tempProject != null && tempProject.getId() != projectId) { - putMsg(result, Status.PROJECT_ALREADY_EXISTS, projectName); - return result; - } - project.setName(projectName); - project.setDescription(desc); - project.setUpdateTime(new Date()); - - int update = projectMapper.updateById(project); - if (update > 0) { - putMsg(result, Status.SUCCESS); - } else { - putMsg(result, Status.UPDATE_PROJECT_ERROR); - } - return result; - } - + Map update(User loginUser, Integer projectId, String projectName, String desc); /** * query unauthorized project @@ -281,48 +94,7 @@ public class ProjectService extends BaseService { * @param userId user id * @return the projects which user have not permission to see */ - public Map queryUnauthorizedProject(User loginUser, Integer userId) { - Map result = new HashMap<>(); - if (checkAdmin(loginUser, result)) { - return result; - } - /** - * query all project list except specified userId - */ - List projectList = projectMapper.queryProjectExceptUserId(userId); - List resultList = new ArrayList<>(); - Set projectSet = null; - if (projectList != null && projectList.size() > 0) { - projectSet = new HashSet<>(projectList); - - List authedProjectList = projectMapper.queryAuthedProjectListByUserId(userId); - - resultList = getUnauthorizedProjects(projectSet, authedProjectList); - } - result.put(Constants.DATA_LIST, resultList); - putMsg(result,Status.SUCCESS); - return result; - } - - /** - * get unauthorized project - * - * @param projectSet project set - * @param authedProjectList authed project list - * @return project list that authorization - */ - private List getUnauthorizedProjects(Set projectSet, List authedProjectList) { - List resultList; - Set authedProjectSet = null; - if (authedProjectList != null && authedProjectList.size() > 0) { - authedProjectSet = new HashSet<>(authedProjectList); - projectSet.removeAll(authedProjectSet); - - } - resultList = new ArrayList<>(projectSet); - return resultList; - } - + Map queryUnauthorizedProject(User loginUser, Integer userId); /** * query authorized project @@ -331,19 +103,7 @@ public class ProjectService extends BaseService { * @param userId user id * @return projects which the user have permission to see, Except for items created by this user */ - public Map queryAuthorizedProject(User loginUser, Integer userId) { - Map result = new HashMap<>(); - - if (checkAdmin(loginUser, result)) { - return result; - } - - List projects = projectMapper.queryAuthedProjectListByUserId(userId); - result.put(Constants.DATA_LIST, projects); - putMsg(result,Status.SUCCESS); - - return result; - } + Map queryAuthorizedProject(User loginUser, Integer userId); /** * query authorized project @@ -351,83 +111,13 @@ public class ProjectService extends BaseService { * @param loginUser login user * @return projects which the user have permission to see, Except for items created by this user */ - public Map queryProjectCreatedByUser(User loginUser) { - Map result = new HashMap<>(); - - if (checkAdmin(loginUser, result)) { - return result; - } - - List projects = projectMapper.queryProjectCreatedByUser(loginUser.getId()); - result.put(Constants.DATA_LIST, projects); - putMsg(result,Status.SUCCESS); - - return result; - } - - - /** - * check whether have read permission - * - * @param user user - * @param project project - * @return true if the user have permission to see the project, otherwise return false - */ - private boolean checkReadPermission(User user, Project project) { - int permissionId = queryPermission(user, project); - return (permissionId & Constants.READ_PERMISSION) != 0; - } - - /** - * query permission id - * - * @param user user - * @param project project - * @return permission - */ - private int queryPermission(User user, Project project) { - if (user.getUserType() == UserType.ADMIN_USER) { - return Constants.READ_PERMISSION; - } - - if (project.getUserId() == user.getId()) { - return Constants.ALL_PERMISSIONS; - } - - ProjectUser projectUser = projectUserMapper.queryProjectRelation(project.getId(), user.getId()); - - if (projectUser == null) { - return 0; - } - - return projectUser.getPerm(); - - } + Map queryProjectCreatedByUser(User loginUser); /** * query all project list that have one or more process definitions. + * * @return project list */ - public Map queryAllProjectList() { - Map result = new HashMap<>(); - List projects = projectMapper.selectList(null); - List processDefinitions = processDefinitionMapper.selectList(null); - if(projects != null){ - Set set = new HashSet<>(); - for (ProcessDefinition processDefinition : processDefinitions){ - set.add(processDefinition.getProjectId()); - } - List tempDeletelist = new ArrayList(); - for (Project project : projects) { - if(!set.contains(project.getId())){ - tempDeletelist.add(project); - } - } - projects.removeAll(tempDeletelist); - } - result.put(Constants.DATA_LIST, projects); - putMsg(result,Status.SUCCESS); - return result; - } + Map queryAllProjectList(); -} +} \ No newline at end of file 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 new file mode 100644 index 0000000000..395da6027f --- /dev/null +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProjectServiceImpl.java @@ -0,0 +1,443 @@ +/* + * 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.utils.CheckUtils.checkDesc; + +import org.apache.dolphinscheduler.api.enums.Status; +import org.apache.dolphinscheduler.api.service.BaseService; +import org.apache.dolphinscheduler.api.service.ProjectService; +import org.apache.dolphinscheduler.api.utils.PageInfo; +import org.apache.dolphinscheduler.common.Constants; +import org.apache.dolphinscheduler.common.enums.UserType; +import org.apache.dolphinscheduler.dao.entity.ProcessDefinition; +import org.apache.dolphinscheduler.dao.entity.Project; +import org.apache.dolphinscheduler.dao.entity.ProjectUser; +import org.apache.dolphinscheduler.dao.entity.User; +import org.apache.dolphinscheduler.dao.mapper.ProcessDefinitionMapper; +import org.apache.dolphinscheduler.dao.mapper.ProjectMapper; +import org.apache.dolphinscheduler.dao.mapper.ProjectUserMapper; + +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; + +/** + * project service implement + **/ +@Service +public class ProjectServiceImpl extends BaseService implements ProjectService { + + @Autowired + private ProjectMapper projectMapper; + + @Autowired + private ProjectUserMapper projectUserMapper; + + @Autowired + private ProcessDefinitionMapper processDefinitionMapper; + + /** + * create project + * + * @param loginUser login user + * @param name project name + * @param desc description + * @return returns an error if it exists + */ + public Map createProject(User loginUser, String name, String desc) { + + Map result = new HashMap<>(); + Map descCheck = checkDesc(desc); + if (descCheck.get(Constants.STATUS) != Status.SUCCESS) { + return descCheck; + } + + Project project = projectMapper.queryByName(name); + if (project != null) { + putMsg(result, Status.PROJECT_ALREADY_EXISTS, name); + return result; + } + + Date now = new Date(); + + project = Project + .newBuilder() + .name(name) + .description(desc) + .userId(loginUser.getId()) + .userName(loginUser.getUserName()) + .createTime(now) + .updateTime(now) + .build(); + + if (projectMapper.insert(project) > 0) { + Project insertedProject = projectMapper.queryByName(name); + result.put(Constants.DATA_LIST, insertedProject); + putMsg(result, Status.SUCCESS); + } else { + putMsg(result, Status.CREATE_PROJECT_ERROR); + } + return result; + } + + /** + * query project details by id + * + * @param projectId project id + * @return project detail information + */ + public Map queryById(Integer projectId) { + + Map result = new HashMap<>(); + Project project = projectMapper.selectById(projectId); + + if (project != null) { + result.put(Constants.DATA_LIST, project); + putMsg(result, Status.SUCCESS); + } else { + putMsg(result, Status.PROJECT_NOT_FOUNT, projectId); + } + return result; + } + + /** + * check project and authorization + * + * @param loginUser login user + * @param project project + * @param projectName project name + * @return true if the login user have permission to see the project + */ + public Map checkProjectAndAuth(User loginUser, Project project, String projectName) { + Map result = new HashMap<>(); + if (project == null) { + putMsg(result, Status.PROJECT_NOT_FOUNT, projectName); + } else if (!checkReadPermission(loginUser, project)) { + // check read permission + putMsg(result, Status.USER_NO_OPERATION_PROJECT_PERM, loginUser.getUserName(), projectName); + } else { + putMsg(result, Status.SUCCESS); + } + return result; + } + + public boolean hasProjectAndPerm(User loginUser, Project project, Map result) { + boolean checkResult = false; + if (project == null) { + putMsg(result, Status.PROJECT_NOT_FOUNT, ""); + } else if (!checkReadPermission(loginUser, project)) { + putMsg(result, Status.USER_NO_OPERATION_PROJECT_PERM, loginUser.getUserName(), project.getName()); + } else { + checkResult = true; + } + return checkResult; + } + + /** + * admin can view all projects + * + * @param loginUser login user + * @param searchVal search value + * @param pageSize page size + * @param pageNo page number + * @return project list which the login user have permission to see + */ + public Map queryProjectListPaging(User loginUser, Integer pageSize, Integer pageNo, String searchVal) { + Map result = new HashMap<>(); + PageInfo pageInfo = new PageInfo<>(pageNo, pageSize); + + Page page = new Page<>(pageNo, pageSize); + + int userId = loginUser.getUserType() == UserType.ADMIN_USER ? 0 : loginUser.getId(); + IPage projectIPage = projectMapper.queryProjectListPaging(page, userId, searchVal); + + List projectList = projectIPage.getRecords(); + if (userId != 0) { + for (Project project : projectList) { + project.setPerm(Constants.DEFAULT_ADMIN_PERMISSION); + } + } + pageInfo.setTotalCount((int) projectIPage.getTotal()); + pageInfo.setLists(projectList); + result.put(Constants.COUNT, (int) projectIPage.getTotal()); + result.put(Constants.DATA_LIST, pageInfo); + putMsg(result, Status.SUCCESS); + + return result; + } + + /** + * delete project by id + * + * @param loginUser login user + * @param projectId project id + * @return delete result code + */ + public Map deleteProject(User loginUser, Integer projectId) { + Map result = new HashMap<>(); + Project project = projectMapper.selectById(projectId); + Map checkResult = getCheckResult(loginUser, project); + if (checkResult != null) { + return checkResult; + } + + if (!hasPerm(loginUser, project.getUserId())) { + putMsg(result, Status.USER_NO_OPERATION_PERM); + return result; + } + + List processDefinitionList = processDefinitionMapper.queryAllDefinitionList(projectId); + + if (!processDefinitionList.isEmpty()) { + putMsg(result, Status.DELETE_PROJECT_ERROR_DEFINES_NOT_NULL); + return result; + } + int delete = projectMapper.deleteById(projectId); + if (delete > 0) { + putMsg(result, Status.SUCCESS); + } else { + putMsg(result, Status.DELETE_PROJECT_ERROR); + } + return result; + } + + /** + * get check result + * + * @param loginUser login user + * @param project project + * @return check result + */ + private Map getCheckResult(User loginUser, Project project) { + String projectName = project == null ? null : project.getName(); + Map checkResult = checkProjectAndAuth(loginUser, project, projectName); + Status status = (Status) checkResult.get(Constants.STATUS); + if (status != Status.SUCCESS) { + return checkResult; + } + return null; + } + + /** + * updateProcessInstance project + * + * @param loginUser login user + * @param projectId project id + * @param projectName project name + * @param desc description + * @return update result code + */ + public Map update(User loginUser, Integer projectId, String projectName, String desc) { + Map result = new HashMap<>(); + + Map descCheck = checkDesc(desc); + if (descCheck.get(Constants.STATUS) != Status.SUCCESS) { + return descCheck; + } + + Project project = projectMapper.selectById(projectId); + boolean hasProjectAndPerm = hasProjectAndPerm(loginUser, project, result); + if (!hasProjectAndPerm) { + return result; + } + Project tempProject = projectMapper.queryByName(projectName); + if (tempProject != null && tempProject.getId() != projectId) { + putMsg(result, Status.PROJECT_ALREADY_EXISTS, projectName); + return result; + } + project.setName(projectName); + project.setDescription(desc); + project.setUpdateTime(new Date()); + + int update = projectMapper.updateById(project); + if (update > 0) { + putMsg(result, Status.SUCCESS); + } else { + putMsg(result, Status.UPDATE_PROJECT_ERROR); + } + return result; + } + + + /** + * query unauthorized project + * + * @param loginUser login user + * @param userId user id + * @return the projects which user have not permission to see + */ + public Map queryUnauthorizedProject(User loginUser, Integer userId) { + Map result = new HashMap<>(); + if (checkAdmin(loginUser, result)) { + return result; + } + /** + * query all project list except specified userId + */ + List projectList = projectMapper.queryProjectExceptUserId(userId); + List resultList = new ArrayList<>(); + Set projectSet = null; + if (projectList != null && !projectList.isEmpty()) { + projectSet = new HashSet<>(projectList); + + List authedProjectList = projectMapper.queryAuthedProjectListByUserId(userId); + + resultList = getUnauthorizedProjects(projectSet, authedProjectList); + } + result.put(Constants.DATA_LIST, resultList); + putMsg(result, Status.SUCCESS); + return result; + } + + /** + * get unauthorized project + * + * @param projectSet project set + * @param authedProjectList authed project list + * @return project list that authorization + */ + private List getUnauthorizedProjects(Set projectSet, List authedProjectList) { + List resultList; + Set authedProjectSet = null; + if (authedProjectList != null && !authedProjectList.isEmpty()) { + authedProjectSet = new HashSet<>(authedProjectList); + projectSet.removeAll(authedProjectSet); + + } + resultList = new ArrayList<>(projectSet); + return resultList; + } + + + /** + * query authorized project + * + * @param loginUser login user + * @param userId user id + * @return projects which the user have permission to see, Except for items created by this user + */ + public Map queryAuthorizedProject(User loginUser, Integer userId) { + Map result = new HashMap<>(); + + if (checkAdmin(loginUser, result)) { + return result; + } + + List projects = projectMapper.queryAuthedProjectListByUserId(userId); + result.put(Constants.DATA_LIST, projects); + putMsg(result, Status.SUCCESS); + + return result; + } + + /** + * query authorized project + * + * @param loginUser login user + * @return projects which the user have permission to see, Except for items created by this user + */ + public Map queryProjectCreatedByUser(User loginUser) { + Map result = new HashMap<>(); + + if (checkAdmin(loginUser, result)) { + return result; + } + + List projects = projectMapper.queryProjectCreatedByUser(loginUser.getId()); + result.put(Constants.DATA_LIST, projects); + putMsg(result, Status.SUCCESS); + + return result; + } + + /** + * check whether have read permission + * + * @param user user + * @param project project + * @return true if the user have permission to see the project, otherwise return false + */ + private boolean checkReadPermission(User user, Project project) { + int permissionId = queryPermission(user, project); + return (permissionId & Constants.READ_PERMISSION) != 0; + } + + /** + * query permission id + * + * @param user user + * @param project project + * @return permission + */ + private int queryPermission(User user, Project project) { + if (user.getUserType() == UserType.ADMIN_USER) { + return Constants.READ_PERMISSION; + } + + if (project.getUserId() == user.getId()) { + return Constants.ALL_PERMISSIONS; + } + + ProjectUser projectUser = projectUserMapper.queryProjectRelation(project.getId(), user.getId()); + + if (projectUser == null) { + return 0; + } + + return projectUser.getPerm(); + + } + + /** + * query all project list that have one or more process definitions. + * + * @return project list + */ + public Map queryAllProjectList() { + Map result = new HashMap<>(); + List projects = projectMapper.selectList(null); + List processDefinitions = processDefinitionMapper.selectList(null); + if (projects != null) { + Set set = new HashSet<>(); + for (ProcessDefinition processDefinition : processDefinitions) { + set.add(processDefinition.getProjectId()); + } + List tempDeletelist = new ArrayList<>(); + for (Project project : projects) { + if (!set.contains(project.getId())) { + tempDeletelist.add(project); + } + } + projects.removeAll(tempDeletelist); + } + result.put(Constants.DATA_LIST, projects); + putMsg(result, Status.SUCCESS); + return result; + } + +} 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 ee127d6f6f..3d8ae91287 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 @@ -18,6 +18,7 @@ package org.apache.dolphinscheduler.api.service; import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.service.impl.DataAnalysisServiceImpl; +import org.apache.dolphinscheduler.api.service.impl.ProjectServiceImpl; import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.enums.CommandType; import org.apache.dolphinscheduler.common.enums.ExecutionStatus; @@ -59,7 +60,7 @@ public class DataAnalysisServiceTest { ProjectMapper projectMapper; @Mock - ProjectService projectService; + ProjectServiceImpl projectService; @Mock ProcessInstanceMapper processInstanceMapper; diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ExecutorService2Test.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ExecutorService2Test.java index 59523bdd11..a4c0c6bfe7 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ExecutorService2Test.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ExecutorService2Test.java @@ -16,17 +16,36 @@ */ package org.apache.dolphinscheduler.api.service; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + import org.apache.dolphinscheduler.api.enums.Status; +import org.apache.dolphinscheduler.api.service.impl.ProjectServiceImpl; import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.enums.CommandType; import org.apache.dolphinscheduler.common.enums.Priority; import org.apache.dolphinscheduler.common.enums.ReleaseState; import org.apache.dolphinscheduler.common.enums.RunMode; import org.apache.dolphinscheduler.common.model.Server; -import org.apache.dolphinscheduler.dao.entity.*; +import org.apache.dolphinscheduler.dao.entity.Command; +import org.apache.dolphinscheduler.dao.entity.ProcessDefinition; +import org.apache.dolphinscheduler.dao.entity.Project; +import org.apache.dolphinscheduler.dao.entity.Schedule; +import org.apache.dolphinscheduler.dao.entity.Tenant; +import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.dao.mapper.ProcessDefinitionMapper; import org.apache.dolphinscheduler.dao.mapper.ProjectMapper; import org.apache.dolphinscheduler.service.process.ProcessService; + +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -36,13 +55,6 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; -import java.text.ParseException; -import java.util.*; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.times; - /** * test for ExecutorService */ @@ -62,7 +74,7 @@ public class ExecutorService2Test { private ProjectMapper projectMapper; @Mock - private ProjectService projectService; + private ProjectServiceImpl projectService; @Mock private MonitorService monitorService; @@ -84,7 +96,7 @@ public class ExecutorService2Test { private String cronTime; @Before - public void init(){ + public void init() { // user loginUser.setId(userId); @@ -111,7 +123,6 @@ public class ExecutorService2Test { /** * not complement - * @throws ParseException */ @Test public void testNoComplement() throws ParseException { @@ -125,13 +136,12 @@ public class ExecutorService2Test { Priority.LOW, Constants.DEFAULT_WORKER_GROUP, 110); Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); verify(processService, times(1)).createCommand(any(Command.class)); - }catch (Exception e){ + } catch (Exception e) { } } /** * date error - * @throws ParseException */ @Test public void testDateError() throws ParseException { @@ -145,13 +155,12 @@ public class ExecutorService2Test { Priority.LOW, Constants.DEFAULT_WORKER_GROUP, 110); Assert.assertEquals(Status.START_PROCESS_INSTANCE_ERROR, result.get(Constants.STATUS)); verify(processService, times(0)).createCommand(any(Command.class)); - }catch (Exception e){ + } catch (Exception e) { } } /** * serial - * @throws ParseException */ @Test public void testSerial() throws ParseException { @@ -165,17 +174,16 @@ public class ExecutorService2Test { Priority.LOW, Constants.DEFAULT_WORKER_GROUP, 110); Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); verify(processService, times(1)).createCommand(any(Command.class)); - }catch (Exception e){ + } catch (Exception e) { } } /** * without schedule - * @throws ParseException */ @Test public void testParallelWithOutSchedule() throws ParseException { - try{ + try { Mockito.when(processService.queryReleaseSchedulerListByProcessDefinitionId(processDefinitionId)).thenReturn(zeroSchedulerList()); Map result = executorService.execProcessInstance(loginUser, projectName, processDefinitionId, cronTime, CommandType.COMPLEMENT_DATA, @@ -185,17 +193,16 @@ public class ExecutorService2Test { Priority.LOW, Constants.DEFAULT_WORKER_GROUP, 110); Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); verify(processService, times(31)).createCommand(any(Command.class)); - }catch (Exception e){ + } catch (Exception e) { } } /** * with schedule - * @throws ParseException */ @Test public void testParallelWithSchedule() throws ParseException { - try{ + try { Mockito.when(processService.queryReleaseSchedulerListByProcessDefinitionId(processDefinitionId)).thenReturn(oneSchedulerList()); Map result = executorService.execProcessInstance(loginUser, projectName, processDefinitionId, cronTime, CommandType.COMPLEMENT_DATA, @@ -205,13 +212,13 @@ public class ExecutorService2Test { Priority.LOW, Constants.DEFAULT_WORKER_GROUP, 110); Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); verify(processService, times(15)).createCommand(any(Command.class)); - }catch (Exception e){ + } catch (Exception e) { } } @Test - public void testNoMsterServers() throws ParseException{ + public void testNoMsterServers() throws ParseException { Mockito.when(monitorService.getServerListFromZK(true)).thenReturn(new ArrayList()); Map result = executorService.execProcessInstance(loginUser, projectName, @@ -220,11 +227,11 @@ public class ExecutorService2Test { null, null, 0, "", "", RunMode.RUN_MODE_PARALLEL, Priority.LOW, Constants.DEFAULT_WORKER_GROUP, 110); - Assert.assertEquals(result.get(Constants.STATUS),Status.MASTER_NOT_EXISTS); + Assert.assertEquals(result.get(Constants.STATUS), Status.MASTER_NOT_EXISTS); } - private List getMasterServersList(){ + private List getMasterServersList() { List masterServerList = new ArrayList<>(); Server masterServer1 = new Server(); masterServer1.setId(1); @@ -242,11 +249,11 @@ public class ExecutorService2Test { } - private List zeroSchedulerList(){ + private List zeroSchedulerList() { return Collections.EMPTY_LIST; } - private List oneSchedulerList(){ + private List oneSchedulerList() { List schedulerList = new LinkedList<>(); Schedule schedule = new Schedule(); schedule.setCrontab("0 0 0 1/2 * ?"); @@ -254,7 +261,7 @@ public class ExecutorService2Test { return schedulerList; } - private Map checkProjectAndAuth(){ + private Map checkProjectAndAuth() { Map result = new HashMap<>(); result.put(Constants.STATUS, Status.SUCCESS); return result; diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProcessDefinitionServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProcessDefinitionServiceTest.java index 67221c5514..38dbdf438d 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProcessDefinitionServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProcessDefinitionServiceTest.java @@ -16,19 +16,10 @@ */ package org.apache.dolphinscheduler.api.service; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - import org.apache.dolphinscheduler.api.dto.ProcessMeta; import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.service.impl.ProcessDefinitionServiceImpl; +import org.apache.dolphinscheduler.api.service.impl.ProjectServiceImpl; import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.enums.ExecutionStatus; import org.apache.dolphinscheduler.common.enums.FailureStrategy; @@ -54,7 +45,19 @@ import org.apache.dolphinscheduler.dao.mapper.ProjectMapper; import org.apache.dolphinscheduler.dao.mapper.ScheduleMapper; import org.apache.dolphinscheduler.dao.mapper.TaskInstanceMapper; import org.apache.dolphinscheduler.service.process.ProcessService; + import org.apache.http.entity.ContentType; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -81,7 +84,7 @@ public class ProcessDefinitionServiceTest { private ProjectMapper projectMapper; @Mock - private ProjectService projectService; + private ProjectServiceImpl projectService; @Mock private ScheduleMapper scheduleMapper; @@ -132,17 +135,17 @@ public class ProcessDefinitionServiceTest { putMsg(result, Status.PROJECT_NOT_FOUNT, projectName); //project not found - Mockito.when(projectService.checkProjectAndAuth(loginUser,project,projectName)).thenReturn(result); - Map map = processDefinitionService.queryProcessDefinitionList(loginUser,"project_test1"); + Mockito.when(projectService.checkProjectAndAuth(loginUser, project, projectName)).thenReturn(result); + Map map = processDefinitionService.queryProcessDefinitionList(loginUser, "project_test1"); Assert.assertEquals(Status.PROJECT_NOT_FOUNT, map.get(Constants.STATUS)); //project check auth success putMsg(result, Status.SUCCESS, projectName); - Mockito.when(projectService.checkProjectAndAuth(loginUser,project,projectName)).thenReturn(result); + Mockito.when(projectService.checkProjectAndAuth(loginUser, project, projectName)).thenReturn(result); List resourceList = new ArrayList<>(); resourceList.add(getProcessDefinition()); Mockito.when(processDefineMapper.queryAllDefinitionList(project.getId())).thenReturn(resourceList); - Map checkSuccessRes = processDefinitionService.queryProcessDefinitionList(loginUser,"project_test1"); + Map checkSuccessRes = processDefinitionService.queryProcessDefinitionList(loginUser, "project_test1"); Assert.assertEquals(Status.SUCCESS, checkSuccessRes.get(Constants.STATUS)); } @@ -161,8 +164,8 @@ public class ProcessDefinitionServiceTest { putMsg(result, Status.PROJECT_NOT_FOUNT, projectName); //project not found - Mockito.when(projectService.checkProjectAndAuth(loginUser,project,projectName)).thenReturn(result); - Map map = processDefinitionService.queryProcessDefinitionListPaging(loginUser, "project_test1", "",1, 5,0); + Mockito.when(projectService.checkProjectAndAuth(loginUser, project, projectName)).thenReturn(result); + Map map = processDefinitionService.queryProcessDefinitionListPaging(loginUser, "project_test1", "", 1, 5, 0); Assert.assertEquals(Status.PROJECT_NOT_FOUNT, map.get(Constants.STATUS)); } @@ -182,14 +185,14 @@ public class ProcessDefinitionServiceTest { putMsg(result, Status.PROJECT_NOT_FOUNT, projectName); //project check auth fail - Mockito.when(projectService.checkProjectAndAuth(loginUser,project,projectName)).thenReturn(result); + Mockito.when(projectService.checkProjectAndAuth(loginUser, project, projectName)).thenReturn(result); Map map = processDefinitionService.queryProcessDefinitionById(loginUser, "project_test1", 1); Assert.assertEquals(Status.PROJECT_NOT_FOUNT, map.get(Constants.STATUS)); //project check auth success, instance not exist putMsg(result, Status.SUCCESS, projectName); - Mockito.when(projectService.checkProjectAndAuth(loginUser,project,projectName)).thenReturn(result); + Mockito.when(projectService.checkProjectAndAuth(loginUser, project, projectName)).thenReturn(result); Mockito.when(processDefineMapper.selectById(1)).thenReturn(null); Map instanceNotexitRes = processDefinitionService.queryProcessDefinitionById(loginUser, "project_test1", 1); @@ -203,7 +206,7 @@ public class ProcessDefinitionServiceTest { } @Test - public void testCopyProcessDefinition() throws Exception{ + public void testCopyProcessDefinition() throws Exception { String projectName = "project_test1"; Mockito.when(projectMapper.queryByName(projectName)).thenReturn(getProject(projectName)); @@ -218,7 +221,7 @@ public class ProcessDefinitionServiceTest { Map result = new HashMap<>(); //project check auth success, instance not exist putMsg(result, Status.SUCCESS, projectName); - Mockito.when(projectService.checkProjectAndAuth(loginUser,project,projectName)).thenReturn(result); + Mockito.when(projectService.checkProjectAndAuth(loginUser, project, projectName)).thenReturn(result); ProcessDefinition definition = getProcessDefinition(); definition.setLocations("{\"tasks-36196\":{\"name\":\"ssh_test1\",\"targetarr\":\"\",\"x\":141,\"y\":70}}"); @@ -239,13 +242,14 @@ public class ProcessDefinitionServiceTest { definition.getLocations(), definition.getConnects())).thenReturn(createProcessResult); - Map successRes = processDefinitionService.batchCopyProcessDefinition(loginUser,"project_test1", - "46",1); + Map successRes = processDefinitionService.batchCopyProcessDefinition(loginUser, "project_test1", + "46", 1); + Assert.assertEquals(Status.SUCCESS, successRes.get(Constants.STATUS)); } @Test - public void testBatchMoveProcessDefinition() throws Exception{ + public void testBatchMoveProcessDefinition() throws Exception { String projectName = "project_test1"; Mockito.when(projectMapper.queryByName(projectName)).thenReturn(getProject(projectName)); @@ -268,8 +272,8 @@ public class ProcessDefinitionServiceTest { Map result2 = new HashMap<>(); putMsg(result2, Status.SUCCESS, targetProject.getName()); - Mockito.when(projectService.checkProjectAndAuth(loginUser,project,projectName)).thenReturn(result); - Mockito.when(projectService.checkProjectAndAuth(loginUser,targetProject,targetProject.getName())).thenReturn(result2); + Mockito.when(projectService.checkProjectAndAuth(loginUser, project, projectName)).thenReturn(result); + Mockito.when(projectService.checkProjectAndAuth(loginUser, targetProject, targetProject.getName())).thenReturn(result2); ProcessDefinition definition = getProcessDefinition(); definition.setLocations("{\"tasks-36196\":{\"name\":\"ssh_test1\",\"targetarr\":\"\",\"x\":141,\"y\":70}}"); @@ -282,8 +286,8 @@ public class ProcessDefinitionServiceTest { putMsg(result, Status.SUCCESS); - Map successRes = processDefinitionService.batchMoveProcessDefinition(loginUser,"project_test1", - "46",2); + Map successRes = processDefinitionService.batchMoveProcessDefinition(loginUser, "project_test1", + "46", 2); Assert.assertEquals(Status.SUCCESS, successRes.get(Constants.STATUS)); } @@ -301,13 +305,13 @@ public class ProcessDefinitionServiceTest { //project check auth fail Map result = new HashMap<>(); putMsg(result, Status.PROJECT_NOT_FOUNT, projectName); - Mockito.when(projectService.checkProjectAndAuth(loginUser,project,projectName)).thenReturn(result); + Mockito.when(projectService.checkProjectAndAuth(loginUser, project, projectName)).thenReturn(result); Map map = processDefinitionService.deleteProcessDefinitionById(loginUser, "project_test1", 6); Assert.assertEquals(Status.PROJECT_NOT_FOUNT, map.get(Constants.STATUS)); //project check auth success, instance not exist putMsg(result, Status.SUCCESS, projectName); - Mockito.when(projectService.checkProjectAndAuth(loginUser,project,projectName)).thenReturn(result); + Mockito.when(projectService.checkProjectAndAuth(loginUser, project, projectName)).thenReturn(result); Mockito.when(processDefineMapper.selectById(1)).thenReturn(null); Map instanceNotexitRes = processDefinitionService.deleteProcessDefinitionById(loginUser, "project_test1", 1); @@ -381,7 +385,7 @@ public class ProcessDefinitionServiceTest { //project check auth fail Map result = new HashMap<>(); putMsg(result, Status.PROJECT_NOT_FOUNT, projectName); - Mockito.when(projectService.checkProjectAndAuth(loginUser,project,projectName)).thenReturn(result); + Mockito.when(projectService.checkProjectAndAuth(loginUser, project, projectName)).thenReturn(result); Map map = processDefinitionService.releaseProcessDefinition(loginUser, "project_test1", 6, ReleaseState.OFFLINE.getCode()); Assert.assertEquals(Status.PROJECT_NOT_FOUNT, map.get(Constants.STATUS)); @@ -424,20 +428,20 @@ public class ProcessDefinitionServiceTest { //project check auth fail Map result = new HashMap<>(); putMsg(result, Status.PROJECT_NOT_FOUNT, projectName); - Mockito.when(projectService.checkProjectAndAuth(loginUser,project,projectName)).thenReturn(result); + Mockito.when(projectService.checkProjectAndAuth(loginUser, project, projectName)).thenReturn(result); Map map = processDefinitionService.verifyProcessDefinitionName(loginUser, "project_test1", "test_pdf"); Assert.assertEquals(Status.PROJECT_NOT_FOUNT, map.get(Constants.STATUS)); //project check auth success, process not exist putMsg(result, Status.SUCCESS, projectName); - Mockito.when(processDefineMapper.queryByDefineName(project.getId(),"test_pdf")).thenReturn(null); + Mockito.when(processDefineMapper.queryByDefineName(project.getId(), "test_pdf")).thenReturn(null); Map processNotExistRes = processDefinitionService.verifyProcessDefinitionName(loginUser, "project_test1", "test_pdf"); Assert.assertEquals(Status.SUCCESS, processNotExistRes.get(Constants.STATUS)); //process exist - Mockito.when(processDefineMapper.queryByDefineName(project.getId(),"test_pdf")).thenReturn(getProcessDefinition()); + Mockito.when(processDefineMapper.queryByDefineName(project.getId(), "test_pdf")).thenReturn(getProcessDefinition()); Map processExistRes = processDefinitionService.verifyProcessDefinitionName(loginUser, "project_test1", "test_pdf"); Assert.assertEquals(Status.PROCESS_INSTANCE_EXIST, processExistRes.get(Constants.STATUS)); @@ -462,7 +466,7 @@ public class ProcessDefinitionServiceTest { Assert.assertEquals(Status.DATA_IS_NULL, taskNotEmptyRes.get(Constants.STATUS)); //json abnormal - String abnormalJson = processDefinitionJson.replaceAll("SHELL",""); + String abnormalJson = processDefinitionJson.replaceAll("SHELL", ""); processData = JSONUtils.parseObject(abnormalJson, ProcessData.class); Map abnormalTaskRes = processDefinitionService.checkProcessNodeList(processData, abnormalJson); Assert.assertEquals(Status.PROCESS_NODE_S_PARAMETER_INVALID, abnormalTaskRes.get(Constants.STATUS)); @@ -587,7 +591,7 @@ public class ProcessDefinitionServiceTest { "\"timeout\":{\"strategy\":\"\",\"interval\":null,\"enable\":false},\"taskInstancePriority\":\"MEDIUM\",\"workerGroupId\":\\\"default\\\\," + "\"preTasks\":[]}],\"tenantId\":1,\"timeout\":0}"; - FileUtils.writeStringToFile(new File("/tmp/task.json"),processJson); + FileUtils.writeStringToFile(new File("/tmp/task.json"), processJson); File file = new File("/tmp/task.json"); @@ -639,7 +643,7 @@ public class ProcessDefinitionServiceTest { } @Test - public void testUpdateProcessDefinition () { + public void testUpdateProcessDefinition() { User loginUser = new User(); loginUser.setId(1); loginUser.setUserType(UserType.ADMIN_USER); @@ -662,20 +666,22 @@ public class ProcessDefinitionServiceTest { /** * get mock datasource + * * @return DataSource */ - private DataSource getDataSource(){ + private DataSource getDataSource() { DataSource dataSource = new DataSource(); dataSource.setId(2); dataSource.setName("test"); - return dataSource; + return dataSource; } /** * get mock processDefinition + * * @return ProcessDefinition */ - private ProcessDefinition getProcessDefinition(){ + private ProcessDefinition getProcessDefinition() { ProcessDefinition processDefinition = new ProcessDefinition(); processDefinition.setId(46); @@ -684,37 +690,40 @@ public class ProcessDefinitionServiceTest { processDefinition.setTenantId(1); processDefinition.setDescription(""); - return processDefinition; + return processDefinition; } /** * get mock Project + * * @param projectName projectName * @return Project */ - private Project getProject(String projectName){ + private Project getProject(String projectName) { Project project = new Project(); project.setId(1); project.setName(projectName); project.setUserId(1); - return project; + return project; } /** * get mock Project + * * @param projectId projectId * @return Project */ - private Project getProjectById(int projectId){ + private Project getProjectById(int projectId) { Project project = new Project(); project.setId(1); project.setName("project_test2"); project.setUserId(1); - return project; + return project; } /** * get mock schedule + * * @return schedule */ private Schedule getSchedule() { @@ -737,6 +746,7 @@ public class ProcessDefinitionServiceTest { /** * get mock processMeta + * * @return processMeta */ private ProcessMeta getProcessMeta() { @@ -768,4 +778,4 @@ public class ProcessDefinitionServiceTest { result.put(Constants.MSG, status.getMsg()); } } -} \ No newline at end of file +} diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProcessInstanceServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProcessInstanceServiceTest.java index aad884d508..82031ca9eb 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProcessInstanceServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProcessInstanceServiceTest.java @@ -19,9 +19,9 @@ package org.apache.dolphinscheduler.api.service; 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.service.impl.LoggerServiceImpl; +import org.apache.dolphinscheduler.api.service.impl.ProjectServiceImpl; import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.enums.CommandType; @@ -60,12 +60,10 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; -import org.springframework.boot.test.context.SpringBootTest; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; @RunWith(MockitoJUnitRunner.Silent.class) -@SpringBootTest(classes = ApiApplicationServer.class) public class ProcessInstanceServiceTest { @InjectMocks @@ -75,7 +73,7 @@ public class ProcessInstanceServiceTest { ProjectMapper projectMapper; @Mock - ProjectService projectService; + ProjectServiceImpl projectService; @Mock ProcessService processService; @@ -185,8 +183,6 @@ public class ProcessInstanceServiceTest { putMsg(result, Status.SUCCESS, projectName); Project project = getProject(projectName); ProcessInstance processInstance = getProcessInstance(); - List processInstanceList = new ArrayList<>(); - processInstanceList.add(processInstance); when(projectMapper.queryByName(projectName)).thenReturn(project); when(projectService.checkProjectAndAuth(loginUser, project, projectName)).thenReturn(result); when(usersService.queryUser(loginUser.getId())).thenReturn(loginUser); diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProjectServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProjectServiceTest.java index 742d18d406..99ec76a745 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProjectServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProjectServiceTest.java @@ -16,9 +16,8 @@ */ package org.apache.dolphinscheduler.api.service; -import com.baomidou.mybatisplus.core.metadata.IPage; -import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.apache.dolphinscheduler.api.enums.Status; +import org.apache.dolphinscheduler.api.service.impl.ProjectServiceImpl; import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.enums.UserType; @@ -30,7 +29,12 @@ import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.dao.mapper.ProcessDefinitionMapper; import org.apache.dolphinscheduler.dao.mapper.ProjectMapper; import org.apache.dolphinscheduler.dao.mapper.ProjectUserMapper; -import org.apache.dolphinscheduler.dao.mapper.UserMapper; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -43,10 +47,8 @@ import org.mockito.junit.MockitoJUnitRunner; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; @RunWith(MockitoJUnitRunner.class) public class ProjectServiceTest { @@ -55,18 +57,18 @@ public class ProjectServiceTest { private static final Logger logger = LoggerFactory.getLogger(ProjectServiceTest.class); @InjectMocks - private ProjectService projectService; + private ProjectServiceImpl projectService; + @Mock private ProjectMapper projectMapper; - @Mock - private UserMapper userMapper; + @Mock private ProjectUserMapper projectUserMapper; + @Mock private ProcessDefinitionMapper processDefinitionMapper; - private String projectName = "ProjectServiceTest"; private String userName = "ProjectServiceTest"; @@ -78,106 +80,109 @@ public class ProjectServiceTest { @After - public void after(){ + public void after() { } @Test - public void testCreateProject(){ + public void testCreateProject() { - User loginUser = getLoginUser(); + User loginUser = getLoginUser(); loginUser.setId(1); Map result = projectService.createProject(loginUser, projectName, getDesc()); logger.info(result.toString()); - Assert.assertEquals(Status.REQUEST_PARAMS_NOT_VALID_ERROR,result.get(Constants.STATUS)); + Assert.assertEquals(Status.REQUEST_PARAMS_NOT_VALID_ERROR, result.get(Constants.STATUS)); //project name exist Mockito.when(projectMapper.queryByName(projectName)).thenReturn(getProject()); result = projectService.createProject(loginUser, projectName, projectName); logger.info(result.toString()); - Assert.assertEquals(Status.PROJECT_ALREADY_EXISTS,result.get(Constants.STATUS)); + Assert.assertEquals(Status.PROJECT_ALREADY_EXISTS, result.get(Constants.STATUS)); //success Mockito.when(projectMapper.insert(Mockito.any(Project.class))).thenReturn(1); result = projectService.createProject(loginUser, "test", "test"); logger.info(result.toString()); - Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); + Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); } + @Test - public void testQueryById(){ + public void testQueryById() { //not exist Map result = projectService.queryById(Integer.MAX_VALUE); - Assert.assertEquals(Status.PROJECT_NOT_FOUNT,result.get(Constants.STATUS)); + Assert.assertEquals(Status.PROJECT_NOT_FOUNT, result.get(Constants.STATUS)); logger.info(result.toString()); //success Mockito.when(projectMapper.selectById(1)).thenReturn(getProject()); result = projectService.queryById(1); logger.info(result.toString()); - Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); + Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); } + @Test - public void testCheckProjectAndAuth(){ + public void testCheckProjectAndAuth() { Mockito.when(projectUserMapper.queryProjectRelation(1, 1)).thenReturn(getProjectUser()); User loginUser = getLoginUser(); - Map result = projectService.checkProjectAndAuth(loginUser,null,projectName); + Map result = projectService.checkProjectAndAuth(loginUser, null, projectName); logger.info(result.toString()); - Status status = (Status)result.get(Constants.STATUS); - Assert.assertEquals(Status.PROJECT_NOT_FOUNT,result.get(Constants.STATUS)); + Status status = (Status) result.get(Constants.STATUS); + Assert.assertEquals(Status.PROJECT_NOT_FOUNT, result.get(Constants.STATUS)); Project project = getProject(); //USER_NO_OPERATION_PROJECT_PERM project.setUserId(2); - result = projectService.checkProjectAndAuth(loginUser,project,projectName); + result = projectService.checkProjectAndAuth(loginUser, project, projectName); logger.info(result.toString()); - Assert.assertEquals(Status.USER_NO_OPERATION_PROJECT_PERM,result.get(Constants.STATUS)); + Assert.assertEquals(Status.USER_NO_OPERATION_PROJECT_PERM, result.get(Constants.STATUS)); //success project.setUserId(1); - result = projectService.checkProjectAndAuth(loginUser,project,projectName); + result = projectService.checkProjectAndAuth(loginUser, project, projectName); logger.info(result.toString()); - Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); + Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); } @Test - public void testHasProjectAndPerm(){ + public void testHasProjectAndPerm() { - // Mockito.when(projectUserMapper.queryProjectRelation(1, 1)).thenReturn(getProjectUser()); + // Mockito.when(projectUserMapper.queryProjectRelation(1, 1)).thenReturn(getProjectUser()); User loginUser = getLoginUser(); Project project = getProject(); Map result = new HashMap<>(); // not exist user User tempUser = new User(); tempUser.setId(Integer.MAX_VALUE); - boolean checkResult = projectService.hasProjectAndPerm(tempUser,project,result); + boolean checkResult = projectService.hasProjectAndPerm(tempUser, project, result); logger.info(result.toString()); Assert.assertFalse(checkResult); //success result = new HashMap<>(); project.setUserId(1); - checkResult = projectService.hasProjectAndPerm(loginUser,project,result); + checkResult = projectService.hasProjectAndPerm(loginUser, project, result); logger.info(result.toString()); Assert.assertTrue(checkResult); } + @Test - public void testQueryProjectListPaging(){ + public void testQueryProjectListPaging() { - IPage page = new Page<>(1,10); + IPage page = new Page<>(1, 10); page.setRecords(getList()); page.setTotal(1L); Mockito.when(projectMapper.queryProjectListPaging(Mockito.any(Page.class), Mockito.eq(1), Mockito.eq(projectName))).thenReturn(page); User loginUser = getLoginUser(); // project owner - Map result = projectService.queryProjectListPaging(loginUser,10,1,projectName); + Map result = projectService.queryProjectListPaging(loginUser, 10, 1, projectName); logger.info(result.toString()); PageInfo pageInfo = (PageInfo) result.get(Constants.DATA_LIST); Assert.assertTrue(CollectionUtils.isNotEmpty(pageInfo.getLists())); @@ -185,83 +190,85 @@ public class ProjectServiceTest { //admin Mockito.when(projectMapper.queryProjectListPaging(Mockito.any(Page.class), Mockito.eq(0), Mockito.eq(projectName))).thenReturn(page); loginUser.setUserType(UserType.ADMIN_USER); - result = projectService.queryProjectListPaging(loginUser,10,1,projectName); + result = projectService.queryProjectListPaging(loginUser, 10, 1, projectName); logger.info(result.toString()); pageInfo = (PageInfo) result.get(Constants.DATA_LIST); Assert.assertTrue(CollectionUtils.isNotEmpty(pageInfo.getLists())); } + @Test - public void testDeleteProject(){ + public void testDeleteProject() { Mockito.when(projectMapper.selectById(1)).thenReturn(getProject()); User loginUser = getLoginUser(); //PROJECT_NOT_FOUNT - Map result= projectService.deleteProject(loginUser,12); + Map result = projectService.deleteProject(loginUser, 12); logger.info(result.toString()); - Assert.assertEquals(Status.PROJECT_NOT_FOUNT,result.get(Constants.STATUS)); + Assert.assertEquals(Status.PROJECT_NOT_FOUNT, result.get(Constants.STATUS)); loginUser.setId(2); //USER_NO_OPERATION_PROJECT_PERM - result= projectService.deleteProject(loginUser,1); + result = projectService.deleteProject(loginUser, 1); logger.info(result.toString()); - Assert.assertEquals(Status.USER_NO_OPERATION_PROJECT_PERM,result.get(Constants.STATUS)); + Assert.assertEquals(Status.USER_NO_OPERATION_PROJECT_PERM, result.get(Constants.STATUS)); //DELETE_PROJECT_ERROR_DEFINES_NOT_NULL Mockito.when(processDefinitionMapper.queryAllDefinitionList(1)).thenReturn(getProcessDefinitions()); loginUser.setUserType(UserType.ADMIN_USER); - result= projectService.deleteProject(loginUser,1); + result = projectService.deleteProject(loginUser, 1); logger.info(result.toString()); - Assert.assertEquals(Status.DELETE_PROJECT_ERROR_DEFINES_NOT_NULL,result.get(Constants.STATUS)); + Assert.assertEquals(Status.DELETE_PROJECT_ERROR_DEFINES_NOT_NULL, result.get(Constants.STATUS)); //success Mockito.when(projectMapper.deleteById(1)).thenReturn(1); Mockito.when(processDefinitionMapper.queryAllDefinitionList(1)).thenReturn(new ArrayList<>()); - result= projectService.deleteProject(loginUser,1); + result = projectService.deleteProject(loginUser, 1); logger.info(result.toString()); - Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); + Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); } @Test - public void testUpdate(){ + public void testUpdate() { User loginUser = getLoginUser(); Project project = getProject(); project.setId(2); Mockito.when(projectMapper.queryByName(projectName)).thenReturn(project); - Mockito.when( projectMapper.selectById(1)).thenReturn(getProject()); + Mockito.when(projectMapper.selectById(1)).thenReturn(getProject()); // PROJECT_NOT_FOUNT - Map result = projectService.update(loginUser,12,projectName,"desc"); + Map result = projectService.update(loginUser, 12, projectName, "desc"); logger.info(result.toString()); - Assert.assertEquals(Status.PROJECT_NOT_FOUNT,result.get(Constants.STATUS)); + Assert.assertEquals(Status.PROJECT_NOT_FOUNT, result.get(Constants.STATUS)); //PROJECT_ALREADY_EXISTS - result = projectService.update(loginUser,1,projectName,"desc"); + result = projectService.update(loginUser, 1, projectName, "desc"); logger.info(result.toString()); - Assert.assertEquals(Status.PROJECT_ALREADY_EXISTS,result.get(Constants.STATUS)); + Assert.assertEquals(Status.PROJECT_ALREADY_EXISTS, result.get(Constants.STATUS)); //success project.setUserId(1); Mockito.when(projectMapper.updateById(Mockito.any(Project.class))).thenReturn(1); - result = projectService.update(loginUser,1,"test","desc"); + result = projectService.update(loginUser, 1, "test", "desc"); logger.info(result.toString()); - Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); + Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); } + @Test - public void testQueryAuthorizedProject(){ + public void testQueryAuthorizedProject() { User loginUser = getLoginUser(); Mockito.when(projectMapper.queryAuthedProjectListByUserId(1)).thenReturn(getList()); //USER_NO_OPERATION_PERM - Map result = projectService.queryAuthorizedProject(loginUser,3); + Map result = projectService.queryAuthorizedProject(loginUser, 3); logger.info(result.toString()); - Assert.assertEquals(Status.USER_NO_OPERATION_PERM,result.get(Constants.STATUS)); + Assert.assertEquals(Status.USER_NO_OPERATION_PERM, result.get(Constants.STATUS)); //success loginUser.setUserType(UserType.ADMIN_USER); - result = projectService.queryAuthorizedProject(loginUser,1); + result = projectService.queryAuthorizedProject(loginUser, 1); logger.info(result.toString()); List projects = (List) result.get(Constants.DATA_LIST); Assert.assertTrue(CollectionUtils.isNotEmpty(projects)); @@ -269,7 +276,7 @@ public class ProjectServiceTest { } @Test - public void testQueryCreatedProject(){ + public void testQueryCreatedProject() { User loginUser = getLoginUser(); @@ -277,7 +284,7 @@ public class ProjectServiceTest { //USER_NO_OPERATION_PERM Map result = projectService.queryProjectCreatedByUser(loginUser); logger.info(result.toString()); - Assert.assertEquals(Status.USER_NO_OPERATION_PERM,result.get(Constants.STATUS)); + Assert.assertEquals(Status.USER_NO_OPERATION_PERM, result.get(Constants.STATUS)); //success loginUser.setUserType(UserType.ADMIN_USER); @@ -287,42 +294,44 @@ public class ProjectServiceTest { Assert.assertTrue(CollectionUtils.isNotEmpty(projects)); } + @Test - public void testQueryAllProjectList(){ + public void testQueryAllProjectList() { Mockito.when(projectMapper.selectList(null)).thenReturn(getList()); Mockito.when(processDefinitionMapper.selectList(null)).thenReturn(getProcessDefinitions()); Map result = projectService.queryAllProjectList(); logger.info(result.toString()); - List projects = (List) result.get(Constants.DATA_LIST); + List projects = (List) result.get(Constants.DATA_LIST); Assert.assertTrue(CollectionUtils.isNotEmpty(projects)); } + @Test - public void testQueryUnauthorizedProject(){ - // Mockito.when(projectMapper.queryAuthedProjectListByUserId(1)).thenReturn(getList()); + public void testQueryUnauthorizedProject() { + // Mockito.when(projectMapper.queryAuthedProjectListByUserId(1)).thenReturn(getList()); Mockito.when(projectMapper.queryProjectExceptUserId(2)).thenReturn(getList()); User loginUser = new User(); loginUser.setUserType(UserType.ADMIN_USER); - Map result = projectService.queryUnauthorizedProject(loginUser,2); + Map result = projectService.queryUnauthorizedProject(loginUser, 2); logger.info(result.toString()); List projects = (List) result.get(Constants.DATA_LIST); Assert.assertTrue(CollectionUtils.isNotEmpty(projects)); } - private Project getProject(){ + private Project getProject() { Project project = new Project(); project.setId(1); project.setName(projectName); project.setUserId(1); - return project; + return project; } - private List getList(){ + private List getList() { List list = new ArrayList<>(); list.add(getProject()); return list; @@ -331,30 +340,28 @@ public class ProjectServiceTest { /** * create admin user - * @return */ - private User getLoginUser(){ + private User getLoginUser() { User loginUser = new User(); loginUser.setUserType(UserType.GENERAL_USER); loginUser.setUserName(userName); loginUser.setId(1); - return loginUser; + return loginUser; } /** * get project user - */ - private ProjectUser getProjectUser(){ + private ProjectUser getProjectUser() { ProjectUser projectUser = new ProjectUser(); projectUser.setProjectId(1); projectUser.setUserId(1); - return projectUser; + return projectUser; } - private List getProcessDefinitions(){ + private List getProcessDefinitions() { List list = new ArrayList<>(); ProcessDefinition processDefinition = new ProcessDefinition(); processDefinition.setProjectId(1); @@ -363,9 +370,7 @@ public class ProjectServiceTest { } - - - private String getDesc(){ + private String getDesc() { return "projectUserMapper.deleteProjectRelation(projectId,userId)projectUserMappe" + ".deleteProjectRelation(projectId,userId)projectUserMappe" + "r.deleteProjectRelation(projectId,userId)projectUserMapper" + diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/SchedulerServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/SchedulerServiceTest.java index f75d808e56..deadc2129c 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/SchedulerServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/SchedulerServiceTest.java @@ -17,6 +17,7 @@ package org.apache.dolphinscheduler.api.service; import org.apache.dolphinscheduler.api.enums.Status; +import org.apache.dolphinscheduler.api.service.impl.ProjectServiceImpl; import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.enums.ReleaseState; import org.apache.dolphinscheduler.common.model.Server; @@ -24,12 +25,16 @@ import org.apache.dolphinscheduler.dao.entity.ProcessDefinition; import org.apache.dolphinscheduler.dao.entity.Project; import org.apache.dolphinscheduler.dao.entity.Schedule; import org.apache.dolphinscheduler.dao.entity.User; -import org.apache.dolphinscheduler.dao.mapper.ProcessDefinitionMapper; import org.apache.dolphinscheduler.dao.mapper.ProjectMapper; -import org.apache.dolphinscheduler.dao.mapper.ProjectUserMapper; import org.apache.dolphinscheduler.dao.mapper.ScheduleMapper; import org.apache.dolphinscheduler.service.process.ProcessService; import org.apache.dolphinscheduler.service.quartz.QuartzExecutors; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -40,13 +45,6 @@ import org.mockito.Mockito; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import org.quartz.Scheduler; -import org.springframework.beans.factory.annotation.Autowired; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; @RunWith(PowerMockRunner.class) @PrepareForTest(QuartzExecutors.class) @@ -57,10 +55,6 @@ public class SchedulerServiceTest { @InjectMocks private SchedulerService schedulerService; - - @Autowired - private ExecutorService executorService; - @Mock private MonitorService monitorService; @@ -72,21 +66,13 @@ public class SchedulerServiceTest { @Mock private ProjectMapper projectMapper; - @Mock - private ProjectUserMapper projectUserMapper; - @Mock - private ProjectService projectService; @Mock - private ProcessDefinitionMapper processDefinitionMapper; + private ProjectServiceImpl projectService; @Mock private QuartzExecutors quartzExecutors; - @Mock - private Scheduler scheduler; - - @Before public void setUp() { @@ -176,10 +162,10 @@ public class SchedulerServiceTest { Mockito.when(quartzExecutors.deleteJob("1", "1")).thenReturn(true); Mockito.when(quartzExecutors.buildJobGroupName(1)).thenReturn("1"); Mockito.when(quartzExecutors.buildJobName(1)).thenReturn("1"); - boolean flag = true; + boolean flag = true; try { schedulerService.deleteSchedule(1, 1); - }catch (Exception e){ + } catch (Exception e) { flag = false; } Assert.assertTrue(flag); diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/TaskInstanceServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/TaskInstanceServiceTest.java index 8e220345bf..f93ed05535 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/TaskInstanceServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/TaskInstanceServiceTest.java @@ -16,9 +16,12 @@ */ package org.apache.dolphinscheduler.api.service; -import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +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.service.impl.ProjectServiceImpl; import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.enums.ExecutionStatus; import org.apache.dolphinscheduler.common.enums.UserType; @@ -30,6 +33,14 @@ import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.dao.mapper.ProjectMapper; import org.apache.dolphinscheduler.dao.mapper.TaskInstanceMapper; import org.apache.dolphinscheduler.service.process.ProcessService; + +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -41,11 +52,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.test.context.SpringBootTest; -import java.text.MessageFormat; -import java.util.*; - -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.when; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; @RunWith(MockitoJUnitRunner.Silent.class) @SpringBootTest(classes = ApiApplicationServer.class) @@ -59,7 +66,7 @@ public class TaskInstanceServiceTest { ProjectMapper projectMapper; @Mock - ProjectService projectService; + ProjectServiceImpl projectService; @Mock ProcessService processService; @@ -74,7 +81,7 @@ public class TaskInstanceServiceTest { UsersService usersService; @Test - public void queryTaskListPaging(){ + public void queryTaskListPaging() { String projectName = "project_test1"; User loginUser = getAdminUser(); @@ -83,7 +90,7 @@ public class TaskInstanceServiceTest { //project auth fail when(projectMapper.queryByName(projectName)).thenReturn(null); - when(projectService.checkProjectAndAuth(loginUser,null,projectName)).thenReturn(result); + when(projectService.checkProjectAndAuth(loginUser, null, projectName)).thenReturn(result); Map proejctAuthFailRes = taskInstanceService.queryTaskListPaging(loginUser, "project_test1", 0, "", "test_user", "2019-02-26 19:48:00", "2019-02-26 19:48:22", "", null, "", 1, 20); Assert.assertEquals(Status.PROJECT_NOT_FOUNT, proejctAuthFailRes.get(Constants.STATUS)); @@ -101,7 +108,7 @@ public class TaskInstanceServiceTest { taskInstanceList.add(taskInstance); pageReturn.setRecords(taskInstanceList); when(projectMapper.queryByName(Mockito.anyString())).thenReturn(project); - when(projectService.checkProjectAndAuth(loginUser,project,projectName)).thenReturn(result); + when(projectService.checkProjectAndAuth(loginUser, project, projectName)).thenReturn(result); when(usersService.queryUser(loginUser.getId())).thenReturn(loginUser); when(usersService.getUserIdByName(loginUser.getUserName())).thenReturn(loginUser.getId()); when(taskInstanceMapper.queryTaskInstanceListPaging(Mockito.any(Page.class), eq(project.getId()), eq(1), eq(""), eq(""), @@ -130,6 +137,7 @@ public class TaskInstanceServiceTest { /** * get Mock Admin User + * * @return admin user */ private User getAdminUser() { @@ -142,19 +150,21 @@ public class TaskInstanceServiceTest { /** * get mock Project + * * @param projectName projectName * @return Project */ - private Project getProject(String projectName){ + private Project getProject(String projectName) { Project project = new Project(); project.setId(1); project.setName(projectName); project.setUserId(1); - return project; + return project; } /** * get Mock process instance + * * @return process instance */ private ProcessInstance getProcessInstance() { @@ -169,6 +179,7 @@ public class TaskInstanceServiceTest { /** * get Mock task instance + * * @return task instance */ private TaskInstance getTaskInstance() { diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/Project.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/Project.java index feddb598f0..6726aa7dad 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/Project.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/Project.java @@ -16,13 +16,13 @@ */ package org.apache.dolphinscheduler.dao.entity; +import java.util.Date; + import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; -import java.util.Date; - /** * project */ @@ -32,7 +32,7 @@ public class Project { /** * id */ - @TableId(value="id", type=IdType.AUTO) + @TableId(value = "id", type = IdType.AUTO) private int id; /** @@ -44,7 +44,7 @@ public class Project { /** * user name */ - @TableField(exist=false) + @TableField(exist = false) private String userName; /** @@ -70,19 +70,19 @@ public class Project { /** * permission */ - @TableField(exist=false) + @TableField(exist = false) private int perm; /** * process define count */ - @TableField(exist=false) + @TableField(exist = false) private int defCount; /** * process instance running count */ - @TableField(exist=false) + @TableField(exist = false) private int instRunningCount; public int getDefCount() { @@ -136,6 +136,7 @@ public class Project { public void setDescription(String description) { this.description = description; } + public String getDescription() { return description; } @@ -163,6 +164,7 @@ public class Project { public void setPerm(int perm) { this.perm = perm; } + @Override public String toString() { return "Project{" + @@ -176,7 +178,6 @@ public class Project { '}'; } - @Override public boolean equals(Object o) { if (this == o) { @@ -202,4 +203,88 @@ public class Project { return result; } + public static Builder newBuilder() { + return new Builder(); + } + + public static final class Builder { + private int id; + private int userId; + private String userName; + private String name; + private String description; + private Date createTime; + private Date updateTime; + private int perm; + private int defCount; + private int instRunningCount; + + private Builder() { + } + + public Builder id(int id) { + this.id = id; + return this; + } + + public Builder userId(int userId) { + this.userId = userId; + return this; + } + + public Builder userName(String userName) { + this.userName = userName; + return this; + } + + public Builder name(String name) { + this.name = name; + return this; + } + + public Builder description(String description) { + this.description = description; + return this; + } + + public Builder createTime(Date createTime) { + this.createTime = createTime; + return this; + } + + public Builder updateTime(Date updateTime) { + this.updateTime = updateTime; + return this; + } + + public Builder perm(int perm) { + this.perm = perm; + return this; + } + + public Builder defCount(int defCount) { + this.defCount = defCount; + return this; + } + + public Builder instRunningCount(int instRunningCount) { + this.instRunningCount = instRunningCount; + return this; + } + + public Project build() { + Project project = new Project(); + project.setId(id); + project.setUserId(userId); + project.setUserName(userName); + project.setName(name); + project.setDescription(description); + project.setCreateTime(createTime); + project.setUpdateTime(updateTime); + project.setPerm(perm); + project.setDefCount(defCount); + project.setInstRunningCount(instRunningCount); + return project; + } + } }