Browse Source

[Improvement-12697][UT] Improve the ProjectServiceTest (#12698)

3.2.0-release
Rick Cheng 2 years ago committed by GitHub
parent
commit
7c97febefb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 101
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProjectServiceTest.java

101
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProjectServiceTest.java

@ -36,7 +36,6 @@ import org.apache.dolphinscheduler.common.enums.AuthorizationType;
import org.apache.dolphinscheduler.common.enums.UserType; import org.apache.dolphinscheduler.common.enums.UserType;
import org.apache.dolphinscheduler.dao.entity.ProcessDefinition; import org.apache.dolphinscheduler.dao.entity.ProcessDefinition;
import org.apache.dolphinscheduler.dao.entity.Project; 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.entity.User;
import org.apache.dolphinscheduler.dao.mapper.ProcessDefinitionMapper; import org.apache.dolphinscheduler.dao.mapper.ProcessDefinitionMapper;
import org.apache.dolphinscheduler.dao.mapper.ProjectMapper; import org.apache.dolphinscheduler.dao.mapper.ProjectMapper;
@ -46,7 +45,6 @@ import org.apache.dolphinscheduler.dao.mapper.UserMapper;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -105,18 +103,18 @@ public class ProjectServiceTest {
@Test @Test
public void testCreateProject() { public void testCreateProject() {
User loginUser = getLoginUser(); User loginUser = getLoginUser();
loginUser.setId(1);
Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.PROJECTS, 1, Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.PROJECTS, 1,
PROJECT_CREATE, baseServiceLogger)).thenReturn(true); PROJECT_CREATE, baseServiceLogger)).thenReturn(true);
Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.PROJECTS, null, 1, Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.PROJECTS, null, 1,
baseServiceLogger)).thenReturn(true); baseServiceLogger)).thenReturn(true);
// REQUEST_PARAMS_NOT_VALID_ERROR
Result result = projectService.createProject(loginUser, projectName, getDesc()); Result result = projectService.createProject(loginUser, projectName, getDesc());
logger.info(result.toString()); logger.info(result.toString());
Assertions.assertEquals(Status.REQUEST_PARAMS_NOT_VALID_ERROR.getCode(), 10001); Assertions.assertEquals(Status.REQUEST_PARAMS_NOT_VALID_ERROR.getCode(), 10001);
// project name exist // PROJECT_ALREADY_EXISTS
Mockito.when(projectMapper.queryByName(projectName)).thenReturn(getProject()); Mockito.when(projectMapper.queryByName(projectName)).thenReturn(getProject());
result = projectService.createProject(loginUser, projectName, projectName); result = projectService.createProject(loginUser, projectName, projectName);
logger.info(result.toString()); logger.info(result.toString());
@ -132,17 +130,16 @@ public class ProjectServiceTest {
@Test @Test
public void testCheckProjectAndAuth() { public void testCheckProjectAndAuth() {
long projectCode = 1L; long projectCode = 1L;
User loginUser = getLoginUser(); User loginUser = getLoginUser();
// PROJECT_NOT_EXIST
Map<String, Object> result = projectService.checkProjectAndAuth(loginUser, null, projectCode, PROJECT); Map<String, Object> result = projectService.checkProjectAndAuth(loginUser, null, projectCode, PROJECT);
logger.info(result.toString()); logger.info(result.toString());
Status status = (Status) result.get(Constants.STATUS);
Assertions.assertEquals(Status.PROJECT_NOT_EXIST, result.get(Constants.STATUS)); Assertions.assertEquals(Status.PROJECT_NOT_EXIST, result.get(Constants.STATUS));
Project project = getProject();
// USER_NO_OPERATION_PROJECT_PERM // USER_NO_OPERATION_PROJECT_PERM
Project project = getProject();
project.setUserId(2); project.setUserId(2);
result = projectService.checkProjectAndAuth(loginUser, project, projectCode, PROJECT); result = projectService.checkProjectAndAuth(loginUser, project, projectCode, PROJECT);
logger.info(result.toString()); logger.info(result.toString());
@ -159,31 +156,10 @@ public class ProjectServiceTest {
result = projectService.checkProjectAndAuth(loginUser, project, projectCode, PROJECT); result = projectService.checkProjectAndAuth(loginUser, project, projectCode, PROJECT);
logger.info(result.toString()); logger.info(result.toString());
Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); Assertions.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));
Map<String, Object> result2 = new HashMap<>();
result2 = projectService.checkProjectAndAuth(loginUser, null, projectCode, PROJECT);
Assertions.assertEquals(Status.PROJECT_NOT_EXIST, result2.get(Constants.STATUS));
Project project1 = getProject();
// USER_NO_OPERATION_PROJECT_PERM
project1.setUserId(2);
loginUser.setUserType(UserType.GENERAL_USER);
Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.PROJECTS,
loginUser.getId(), PROJECT, baseServiceLogger)).thenReturn(true);
result2 = projectService.checkProjectAndAuth(loginUser, project1, projectCode, PROJECT);
Assertions.assertEquals(Status.USER_NO_OPERATION_PROJECT_PERM, result2.get(Constants.STATUS));
// success
project1.setUserId(1);
projectService.checkProjectAndAuth(loginUser, project1, projectCode, PROJECT);
} }
@Test @Test
public void testHasProjectAndPerm() { public void testHasProjectAndPerm() {
// Mockito.when(projectUserMapper.queryProjectRelation(1, 1)).thenReturn(getProjectUser());
User loginUser = getLoginUser(); User loginUser = getLoginUser();
Project project = getProject(); Project project = getProject();
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
@ -200,12 +176,11 @@ public class ProjectServiceTest {
// success // success
result = new HashMap<>(); result = new HashMap<>();
project.setUserId(1); project.setUserId(1);
loginUser.setUserType(UserType.ADMIN_USER);
Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.PROJECTS, Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.PROJECTS,
loginUser.getId(), null, baseServiceLogger)).thenReturn(true); loginUser.getId(), null, baseServiceLogger)).thenReturn(true);
Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.PROJECTS, Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.PROJECTS,
new Object[]{project.getId()}, new Object[]{project.getId()},
0, baseServiceLogger)).thenReturn(true); loginUser.getId(), baseServiceLogger)).thenReturn(true);
checkResult = projectService.hasProjectAndPerm(loginUser, project, result, null); checkResult = projectService.hasProjectAndPerm(loginUser, project, result, null);
logger.info(result.toString()); logger.info(result.toString());
Assertions.assertTrue(checkResult); Assertions.assertTrue(checkResult);
@ -240,8 +215,6 @@ public class ProjectServiceTest {
@Test @Test
public void testHasProjectAndWritePerm() { public void testHasProjectAndWritePerm() {
// Mockito.when(projectUserMapper.queryProjectRelation(1, 1)).thenReturn(getProjectUser());
User loginUser = getLoginUser(); User loginUser = getLoginUser();
Project project = getProject(); Project project = getProject();
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
@ -263,7 +236,7 @@ public class ProjectServiceTest {
loginUser.getId(), null, baseServiceLogger)).thenReturn(true); loginUser.getId(), null, baseServiceLogger)).thenReturn(true);
Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.PROJECTS, Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.PROJECTS,
new Object[]{project.getId()}, new Object[]{project.getId()},
0, baseServiceLogger)).thenReturn(true); loginUser.getId(), baseServiceLogger)).thenReturn(true);
checkResult = projectService.hasProjectAndWritePerm(loginUser, project, result); checkResult = projectService.hasProjectAndWritePerm(loginUser, project, result);
logger.info(result.toString()); logger.info(result.toString());
Assertions.assertTrue(checkResult); Assertions.assertTrue(checkResult);
@ -273,19 +246,14 @@ public class ProjectServiceTest {
public void testDeleteProject() { public void testDeleteProject() {
User loginUser = getLoginUser(); User loginUser = getLoginUser();
Mockito.when(projectMapper.queryByCode(1L)).thenReturn(getProject()); Mockito.when(projectMapper.queryByCode(1L)).thenReturn(getProject());
Mockito.when(
resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.PROJECTS, loginUser.getId(),
PROJECT_DELETE, baseServiceLogger))
.thenReturn(true);
// PROJECT_NOT_FOUND // PROJECT_NOT_FOUND
Result result = projectService.deleteProject(loginUser, 11L); Result result = projectService.deleteProject(loginUser, 11L);
logger.info(result.toString()); logger.info(result.toString());
Assertions.assertTrue(Status.PROJECT_NOT_FOUND.getCode() == result.getCode()); Assertions.assertTrue(Status.PROJECT_NOT_FOUND.getCode() == result.getCode());
loginUser.setId(2); loginUser.setId(2);
// USER_NO_OPERATION_PROJECT_PERM // USER_NO_OPERATION_PROJECT_PERM
Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.PROJECTS, new Object[]{1},
loginUser.getId(),
baseServiceLogger)).thenReturn(true);
result = projectService.deleteProject(loginUser, 1L); result = projectService.deleteProject(loginUser, 1L);
logger.info(result.toString()); logger.info(result.toString());
Assertions.assertTrue(Status.USER_NO_WRITE_PROJECT_PERM.getCode() == result.getCode()); Assertions.assertTrue(Status.USER_NO_WRITE_PROJECT_PERM.getCode() == result.getCode());
@ -298,6 +266,10 @@ public class ProjectServiceTest {
resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.PROJECTS, new Object[]{1}, 0, resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.PROJECTS, new Object[]{1}, 0,
baseServiceLogger)) baseServiceLogger))
.thenReturn(true); .thenReturn(true);
Mockito.when(
resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.PROJECTS, loginUser.getId(),
PROJECT_DELETE, baseServiceLogger))
.thenReturn(true);
result = projectService.deleteProject(loginUser, 1L); result = projectService.deleteProject(loginUser, 1L);
logger.info(result.toString()); logger.info(result.toString());
Assertions.assertTrue(Status.DELETE_PROJECT_ERROR_DEFINES_NOT_NULL.getCode() == result.getCode()); Assertions.assertTrue(Status.DELETE_PROJECT_ERROR_DEFINES_NOT_NULL.getCode() == result.getCode());
@ -317,25 +289,26 @@ public class ProjectServiceTest {
loginUser.setId(1); loginUser.setId(1);
Project project = getProject(); Project project = getProject();
project.setCode(2L); project.setCode(2L);
Mockito.when(
resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.PROJECTS, loginUser.getId(),
PROJECT_UPDATE, baseServiceLogger))
.thenReturn(true);
Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.PROJECTS, new Object[]{1},
loginUser.getId(),
baseServiceLogger)).thenReturn(true);
Mockito.when(projectMapper.queryByName(projectName)).thenReturn(project); Mockito.when(projectMapper.queryByName(projectName)).thenReturn(project);
Mockito.when(projectMapper.queryByCode(2L)).thenReturn(getProject()); Mockito.when(projectMapper.queryByCode(2L)).thenReturn(getProject());
// PROJECT_NOT_FOUNT // PROJECT_NOT_FOUND
Result result = projectService.update(loginUser, 1L, projectName, "desc", "testUser"); Result result = projectService.update(loginUser, 1L, projectName, "desc", "testUser");
logger.info(result.toString()); logger.info(result.toString());
Assertions.assertTrue(Status.PROJECT_NOT_FOUND.getCode() == result.getCode()); Assertions.assertTrue(Status.PROJECT_NOT_FOUND.getCode() == result.getCode());
// PROJECT_ALREADY_EXISTS // PROJECT_ALREADY_EXISTS
Mockito.when(
resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.PROJECTS, loginUser.getId(),
PROJECT_UPDATE, baseServiceLogger))
.thenReturn(true);
Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.PROJECTS, new Object[]{1},
loginUser.getId(),
baseServiceLogger)).thenReturn(true);
result = projectService.update(loginUser, 2L, projectName, "desc", userName); result = projectService.update(loginUser, 2L, projectName, "desc", userName);
logger.info(result.toString()); logger.info(result.toString());
Assertions.assertTrue(Status.PROJECT_ALREADY_EXISTS.getCode() == result.getCode()); Assertions.assertTrue(Status.PROJECT_ALREADY_EXISTS.getCode() == result.getCode());
// USER_NOT_EXIST
Mockito.when(userMapper.queryByUserNameAccurately(Mockito.any())).thenReturn(null); Mockito.when(userMapper.queryByUserNameAccurately(Mockito.any())).thenReturn(null);
result = projectService.update(loginUser, 2L, "test", "desc", "testuser"); result = projectService.update(loginUser, 2L, "test", "desc", "testuser");
Assertions.assertTrue(Status.USER_NOT_EXIST.getCode() == result.getCode()); Assertions.assertTrue(Status.USER_NOT_EXIST.getCode() == result.getCode());
@ -381,7 +354,6 @@ public class ProjectServiceTest {
Assertions.assertTrue(Status.PROJECT_NOT_FOUND.getCode() == result.getCode()); Assertions.assertTrue(Status.PROJECT_NOT_FOUND.getCode() == result.getCode());
// Failure 2: USER_NO_OPERATION_PROJECT_PERM // Failure 2: USER_NO_OPERATION_PROJECT_PERM
loginUser.setId(100);
Mockito.when(this.projectMapper.queryByCode(Mockito.anyLong())).thenReturn(this.getProject()); Mockito.when(this.projectMapper.queryByCode(Mockito.anyLong())).thenReturn(this.getProject());
result = this.projectService.queryAuthorizedUser(loginUser, 3682329499136L); result = this.projectService.queryAuthorizedUser(loginUser, 3682329499136L);
logger.info("FAILURE 2: {}", result.toString()); logger.info("FAILURE 2: {}", result.toString());
@ -403,10 +375,6 @@ public class ProjectServiceTest {
loginUser.setId(1); loginUser.setId(1);
loginUser.setUserType(UserType.GENERAL_USER); loginUser.setUserType(UserType.GENERAL_USER);
Mockito.when(
resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.PROJECTS,
loginUser.getId(), PROJECT, baseServiceLogger))
.thenReturn(true);
Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.PROJECTS, new Object[]{1}, Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.PROJECTS, new Object[]{1},
1, baseServiceLogger)).thenReturn(true); 1, baseServiceLogger)).thenReturn(true);
result = this.projectService.queryAuthorizedUser(loginUser, 3682329499136L); result = this.projectService.queryAuthorizedUser(loginUser, 3682329499136L);
@ -433,13 +401,11 @@ public class ProjectServiceTest {
@Test @Test
public void testQueryProjectCreatedAndAuthorizedByUser() { public void testQueryProjectCreatedAndAuthorizedByUser() {
Result result; Result result;
User loginUser = getLoginUser(); User loginUser = getLoginUser();
Set<Integer> set = new HashSet(); Set<Integer> set = new HashSet();
set.add(1); set.add(1);
List<Integer> list = new ArrayList<>(1);
list.add(1);
// not admin user // not admin user
Mockito.when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, Mockito.when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS,
loginUser.getId(), projectLogger)).thenReturn(set); loginUser.getId(), projectLogger)).thenReturn(set);
@ -577,13 +543,6 @@ public class ProjectServiceTest {
return list; return list;
} }
private List<Project> getSingleList() {
return Collections.singletonList(getProject(2));
}
/**
* create admin user
*/
private User getLoginUser() { private User getLoginUser() {
User loginUser = new User(); User loginUser = new User();
loginUser.setUserType(UserType.GENERAL_USER); loginUser.setUserType(UserType.GENERAL_USER);
@ -616,16 +575,6 @@ public class ProjectServiceTest {
return userList; return userList;
} }
/**
* get project user
*/
private ProjectUser getProjectUser() {
ProjectUser projectUser = new ProjectUser();
projectUser.setProjectId(1);
projectUser.setUserId(1);
return projectUser;
}
private List<ProcessDefinition> getProcessDefinitions() { private List<ProcessDefinition> getProcessDefinitions() {
List<ProcessDefinition> list = new ArrayList<>(); List<ProcessDefinition> list = new ArrayList<>();
ProcessDefinition processDefinition = new ProcessDefinition(); ProcessDefinition processDefinition = new ProcessDefinition();
@ -634,10 +583,6 @@ public class ProjectServiceTest {
return list; return list;
} }
private List<Integer> getProjectIds() {
return Collections.singletonList(1);
}
private String getDesc() { private String getDesc() {
return "projectUserMapper.deleteProjectRelation(projectId,userId)projectUserMappe" return "projectUserMapper.deleteProjectRelation(projectId,userId)projectUserMappe"
+ ".deleteProjectRelation(projectId,userId)projectUserMappe" + ".deleteProjectRelation(projectId,userId)projectUserMappe"

Loading…
Cancel
Save