Browse Source

[Feature][JsonSplit-api] api of project (#5630)

* processDefinition create/update

* fix codeStyle

* fix codeStyle

* fix ut

* api of project

* fix ut

* project update method

Co-authored-by: JinyLeeChina <297062848@qq.com>
2.0.7-release
JinyLeeChina 4 years ago committed by GitHub
parent
commit
728bd31f73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 50
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProjectController.java
  2. 20
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ProjectService.java
  3. 69
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProjectServiceImpl.java
  4. 20
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/ProjectControllerTest.java
  5. 33
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProjectServiceTest.java
  6. 1
      dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/ProjectMapper.java
  7. 2
      dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/ProjectMapper.xml

50
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProjectController.java

@ -78,7 +78,7 @@ public class ProjectController extends BaseController {
* @param description description
* @return returns an error if it exists
*/
@ApiOperation(value = "createProject", notes = "CREATE_PROJECT_NOTES")
@ApiOperation(value = "create", notes = "CREATE_PROJECT_NOTES")
@ApiImplicitParams({
@ApiImplicitParam(name = "projectName", value = "PROJECT_NAME", dataType = "String"),
@ApiImplicitParam(name = "description", value = "PROJECT_DESC", dataType = "String")
@ -90,23 +90,22 @@ public class ProjectController extends BaseController {
public Result createProject(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestParam("projectName") String projectName,
@RequestParam(value = "description", required = false) String description) {
Map<String, Object> result = projectService.createProject(loginUser, projectName, description);
return returnDataList(result);
}
/**
* updateProcessInstance project
* update project
*
* @param loginUser login user
* @param projectId project id
* @param projectCode project code
* @param projectName project name
* @param description description
* @return update result code
*/
@ApiOperation(value = "updateProject", notes = "UPDATE_PROJECT_NOTES")
@ApiOperation(value = "update", notes = "UPDATE_PROJECT_NOTES")
@ApiImplicitParams({
@ApiImplicitParam(name = "projectId", value = "PROJECT_ID", dataType = "Int", example = "100"),
@ApiImplicitParam(name = "projectCode", value = "PROJECT_CODE", dataType = "Long", example = "123456"),
@ApiImplicitParam(name = "projectName", value = "PROJECT_NAME", dataType = "String"),
@ApiImplicitParam(name = "description", value = "PROJECT_DESC", dataType = "String"),
@ApiImplicitParam(name = "userName", value = "USER_NAME", dataType = "String"),
@ -116,33 +115,32 @@ public class ProjectController extends BaseController {
@ApiException(UPDATE_PROJECT_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result updateProject(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestParam("projectId") Integer projectId,
@RequestParam("projectCode") Long projectCode,
@RequestParam("projectName") String projectName,
@RequestParam(value = "description", required = false) String description,
@RequestParam(value = "userName") String userName) {
Map<String, Object> result = projectService.update(loginUser, projectId, projectName, description, userName);
Map<String, Object> result = projectService.update(loginUser, projectCode, projectName, description, userName);
return returnDataList(result);
}
/**
* query project details by id
* query project details by code
*
* @param loginUser login user
* @param projectId project id
* @param projectCode project code
* @return project detail information
*/
@ApiOperation(value = "queryProjectById", notes = "QUERY_PROJECT_BY_ID_NOTES")
@ApiOperation(value = "queryProjectByCode", notes = "QUERY_PROJECT_BY_ID_NOTES")
@ApiImplicitParams({
@ApiImplicitParam(name = "projectId", value = "PROJECT_ID", dataType = "Int", example = "100")
@ApiImplicitParam(name = "projectCode", value = "PROJECT_CODE", dataType = "Long", example = "123456")
})
@GetMapping(value = "/query-by-id")
@GetMapping(value = "/query-by-code")
@ResponseStatus(HttpStatus.OK)
@ApiException(QUERY_PROJECT_DETAILS_BY_ID_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result queryProjectById(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestParam("projectId") Integer projectId) {
Map<String, Object> result = projectService.queryById(projectId);
@RequestParam("projectCode") Long projectCode) {
Map<String, Object> result = projectService.queryByCode(loginUser, projectCode);
return returnDataList(result);
}
@ -168,9 +166,7 @@ public class ProjectController extends BaseController {
public Result queryProjectListPaging(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestParam(value = "searchVal", required = false) String searchVal,
@RequestParam("pageSize") Integer pageSize,
@RequestParam("pageNo") Integer pageNo
) {
@RequestParam("pageNo") Integer pageNo) {
Map<String, Object> result = checkPageParams(pageNo, pageSize);
if (result.get(Constants.STATUS) != Status.SUCCESS) {
return returnDataListPaging(result);
@ -181,25 +177,23 @@ public class ProjectController extends BaseController {
}
/**
* delete project by id
* delete project by code
*
* @param loginUser login user
* @param projectId project id
* @param projectCode project code
* @return delete result code
*/
@ApiOperation(value = "deleteProjectById", notes = "DELETE_PROJECT_BY_ID_NOTES")
@ApiOperation(value = "delete", notes = "DELETE_PROJECT_BY_ID_NOTES")
@ApiImplicitParams({
@ApiImplicitParam(name = "projectId", value = "PROJECT_ID", dataType = "Int", example = "100")
@ApiImplicitParam(name = "projectCode", value = "PROJECT_CODE", dataType = "Long", example = "123456")
})
@GetMapping(value = "/delete")
@ResponseStatus(HttpStatus.OK)
@ApiException(DELETE_PROJECT_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result deleteProject(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestParam("projectId") Integer projectId
) {
Map<String, Object> result = projectService.deleteProject(loginUser, projectId);
@RequestParam("projectCode") Long projectCode) {
Map<String, Object> result = projectService.deleteProject(loginUser, projectCode);
return returnDataList(result);
}
@ -300,6 +294,4 @@ public class ProjectController extends BaseController {
Map<String, Object> result = projectService.queryAllProjectList();
return returnDataList(result);
}
}

20
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ProjectService.java

@ -38,12 +38,12 @@ public interface ProjectService {
Map<String, Object> createProject(User loginUser, String name, String desc);
/**
* query project details by id
* query project details by code
*
* @param projectId project id
* @param projectCode project code
* @return project detail information
*/
Map<String, Object> queryById(Integer projectId);
Map<String, Object> queryByCode(User loginUser, Long projectCode);
/**
* check project and authorization
@ -69,25 +69,25 @@ public interface ProjectService {
Map<String, Object> queryProjectListPaging(User loginUser, Integer pageSize, Integer pageNo, String searchVal);
/**
* delete project by id
* delete project by code
*
* @param loginUser login user
* @param projectId project id
* @param projectCode project code
* @return delete result code
*/
Map<String, Object> deleteProject(User loginUser, Integer projectId);
Map<String, Object> deleteProject(User loginUser, Long projectCode);
/**
* updateProcessInstance project
*
* @param loginUser login user
* @param projectId project id
* @param projectCode project code
* @param projectName project name
* @param desc description
* @param userName project owner
* @return update result code
*/
Map<String, Object> update(User loginUser, Integer projectId, String projectName, String desc, String userName);
Map<String, Object> update(User loginUser, Long projectCode, String projectName, String desc, String userName);
/**
* query unauthorized project
@ -124,8 +124,8 @@ public interface ProjectService {
/**
* query authorized and user create project list by user id
* @param loginUser
* @return
* @param loginUser login user
* @return project list
*/
Map<String, Object> queryProjectCreatedAndAuthorizedByUser(User loginUser);

69
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProjectServiceImpl.java

@ -43,8 +43,6 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -69,8 +67,6 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
@Autowired
private UserMapper userMapper;
private Logger logger = LoggerFactory.getLogger(ProjectServiceImpl.class);
/**
* create project
*
@ -97,16 +93,16 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
Date now = new Date();
try {
project = Project
.newBuilder()
.name(name)
.code(SnowFlakeUtils.getInstance().nextId())
.description(desc)
.userId(loginUser.getId())
.userName(loginUser.getUserName())
.createTime(now)
.updateTime(now)
.build();
project = Project
.newBuilder()
.name(name)
.code(SnowFlakeUtils.getInstance().nextId())
.description(desc)
.userId(loginUser.getId())
.userName(loginUser.getUserName())
.createTime(now)
.updateTime(now)
.build();
} catch (SnowFlakeException e) {
putMsg(result, Status.CREATE_PROJECT_ERROR);
return result;
@ -122,22 +118,22 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
}
/**
* query project details by id
* query project details by code
*
* @param projectId project id
* @param projectCode project code
* @return project detail information
*/
@Override
public Map<String, Object> queryById(Integer projectId) {
public Map<String, Object> queryByCode(User loginUser, Long projectCode) {
Map<String, Object> result = new HashMap<>();
Project project = projectMapper.selectById(projectId);
Project project = projectMapper.queryByCode(projectCode);
boolean hasProjectAndPerm = hasProjectAndPerm(loginUser, project, result);
if (!hasProjectAndPerm) {
return result;
}
if (project != null) {
result.put(Constants.DATA_LIST, project);
putMsg(result, Status.SUCCESS);
} else {
putMsg(result, Status.PROJECT_NOT_FOUNT, projectId);
}
return result;
}
@ -212,16 +208,16 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
}
/**
* delete project by id
* delete project by code
*
* @param loginUser login user
* @param projectId project id
* @param projectCode project code
* @return delete result code
*/
@Override
public Map<String, Object> deleteProject(User loginUser, Integer projectId) {
public Map<String, Object> deleteProject(User loginUser, Long projectCode) {
Map<String, Object> result = new HashMap<>();
Project project = projectMapper.selectById(projectId);
Project project = projectMapper.queryByCode(projectCode);
Map<String, Object> checkResult = getCheckResult(loginUser, project);
if (checkResult != null) {
return checkResult;
@ -238,7 +234,7 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
putMsg(result, Status.DELETE_PROJECT_ERROR_DEFINES_NOT_NULL);
return result;
}
int delete = projectMapper.deleteById(projectId);
int delete = projectMapper.deleteById(project.getId());
if (delete > 0) {
putMsg(result, Status.SUCCESS);
} else {
@ -268,14 +264,14 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
* updateProcessInstance project
*
* @param loginUser login user
* @param projectId project id
* @param projectCode project code
* @param projectName project name
* @param desc description
* @param userName project owner
* @return update result code
*/
@Override
public Map<String, Object> update(User loginUser, Integer projectId, String projectName, String desc, String userName) {
public Map<String, Object> update(User loginUser, Long projectCode, String projectName, String desc, String userName) {
Map<String, Object> result = new HashMap<>();
Map<String, Object> descCheck = checkDesc(desc);
@ -283,13 +279,13 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
return descCheck;
}
Project project = projectMapper.selectById(projectId);
Project project = projectMapper.queryByCode(projectCode);
boolean hasProjectAndPerm = hasProjectAndPerm(loginUser, project, result);
if (!hasProjectAndPerm) {
return result;
}
Project tempProject = projectMapper.queryByName(projectName);
if (tempProject != null && tempProject.getId() != projectId) {
if (tempProject != null) {
putMsg(result, Status.PROJECT_ALREADY_EXISTS, projectName);
return result;
}
@ -325,12 +321,10 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
if (loginUser.getId() != userId && isNotAdmin(loginUser, result)) {
return result;
}
/**
* query all project list except specified userId
*/
// query all project list except specified userId
List<Project> projectList = projectMapper.queryProjectExceptUserId(userId);
List<Project> resultList = new ArrayList<>();
Set<Project> projectSet = null;
Set<Project> projectSet;
if (projectList != null && !projectList.isEmpty()) {
projectSet = new HashSet<>(projectList);
@ -352,7 +346,7 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
*/
private List<Project> getUnauthorizedProjects(Set<Project> projectSet, List<Project> authedProjectList) {
List<Project> resultList;
Set<Project> authedProjectSet = null;
Set<Project> authedProjectSet;
if (authedProjectList != null && !authedProjectList.isEmpty()) {
authedProjectSet = new HashSet<>(authedProjectList);
projectSet.removeAll(authedProjectSet);
@ -362,7 +356,6 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
return resultList;
}
/**
* query authorized project
*
@ -406,7 +399,7 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
* query authorized and user create project list by user
*
* @param loginUser login user
* @return
* @return project list
*/
@Override
public Map<String, Object> queryProjectCreatedAndAuthorizedByUser(User loginUser) {

20
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/ProjectControllerTest.java

@ -47,16 +47,16 @@ public class ProjectControllerTest extends AbstractControllerTest {
private static Logger logger = LoggerFactory.getLogger(ProjectControllerTest.class);
private String projectId;
private String projectCode;
@Before
public void before() throws Exception {
projectId = testCreateProject("project_test1", "the test project");
projectCode = testCreateProject("project_test11", "the test project");
}
@After
public void after() throws Exception {
testDeleteProject(projectId);
testDeleteProject(projectCode);
}
private String testCreateProject(String projectName, String description) throws Exception {
@ -84,7 +84,7 @@ public class ProjectControllerTest extends AbstractControllerTest {
public void testUpdateProject() throws Exception {
MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
paramsMap.add("projectId", projectId);
paramsMap.add("projectCode", projectCode);
paramsMap.add("projectName","project_test_update");
paramsMap.add("desc","the test project update");
paramsMap.add("userName", "the project owner");
@ -103,12 +103,12 @@ public class ProjectControllerTest extends AbstractControllerTest {
}
@Test
public void testQueryProjectById() throws Exception {
public void testQueryProjectByCode() throws Exception {
MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
paramsMap.add("projectId", projectId);
paramsMap.add("projectCode", projectCode);
MvcResult mvcResult = mockMvc.perform(get("/projects/query-by-id")
MvcResult mvcResult = mockMvc.perform(get("/projects/query-by-code")
.header(SESSION_ID, sessionId)
.params(paramsMap))
.andExpect(status().isOk())
@ -118,7 +118,7 @@ public class ProjectControllerTest extends AbstractControllerTest {
Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
Assert.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue());
logger.info(mvcResult.getResponse().getContentAsString());
logger.info("query project by id :{}, return result:{}", projectId, mvcResult.getResponse().getContentAsString());
logger.info("query project by code :{}, return result:{}", projectCode, mvcResult.getResponse().getContentAsString());
}
@ -216,10 +216,10 @@ public class ProjectControllerTest extends AbstractControllerTest {
logger.info(mvcResult.getResponse().getContentAsString());
}
private void testDeleteProject(String projectId) throws Exception {
private void testDeleteProject(String projectCode) throws Exception {
MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
paramsMap.add("projectId", projectId);
paramsMap.add("projectCode", projectCode);
MvcResult mvcResult = mockMvc.perform(get("/projects/delete")
.header(SESSION_ID, sessionId)

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

@ -103,18 +103,17 @@ public class ProjectServiceTest {
@Test
public void testQueryById() {
User loginUser = getLoginUser();
//not exist
Map<String, Object> result = projectService.queryById(Integer.MAX_VALUE);
Map<String, Object> result = projectService.queryByCode(loginUser, Long.MAX_VALUE);
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);
Mockito.when(projectMapper.queryByCode(1L)).thenReturn(getProject());
result = projectService.queryByCode(loginUser,1L);
logger.info(result.toString());
Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));
}
@Test
@ -206,33 +205,31 @@ public class ProjectServiceTest {
@Test
public void testDeleteProject() {
Mockito.when(projectMapper.selectById(1)).thenReturn(getProject());
User loginUser = getLoginUser();
Mockito.when(projectMapper.queryByCode(1L)).thenReturn(getProject());
//PROJECT_NOT_FOUNT
Map<String, Object> result = projectService.deleteProject(loginUser, 12);
Map<String, Object> result = projectService.deleteProject(loginUser, 11L);
logger.info(result.toString());
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, 1L);
logger.info(result.toString());
Assert.assertEquals(Status.USER_NO_OPERATION_PROJECT_PERM, result.get(Constants.STATUS));
//DELETE_PROJECT_ERROR_DEFINES_NOT_NULL
Mockito.when(processDefinitionMapper.queryAllDefinitionList(1L)).thenReturn(getProcessDefinitions());
loginUser.setUserType(UserType.ADMIN_USER);
result = projectService.deleteProject(loginUser, 1);
result = projectService.deleteProject(loginUser, 1L);
logger.info(result.toString());
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(1L)).thenReturn(new ArrayList<>());
result = projectService.deleteProject(loginUser, 1);
result = projectService.deleteProject(loginUser, 1L);
logger.info(result.toString());
Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));
}
@Test
@ -240,28 +237,28 @@ public class ProjectServiceTest {
User loginUser = getLoginUser();
Project project = getProject();
project.setId(2);
project.setCode(2L);
Mockito.when(projectMapper.queryByName(projectName)).thenReturn(project);
Mockito.when(projectMapper.selectById(1)).thenReturn(getProject());
Mockito.when(projectMapper.queryByCode(2L)).thenReturn(getProject());
// PROJECT_NOT_FOUNT
Map<String, Object> result = projectService.update(loginUser, 12, projectName, "desc", "testUser");
Map<String, Object> result = projectService.update(loginUser, 1L, projectName, "desc", "testUser");
logger.info(result.toString());
Assert.assertEquals(Status.PROJECT_NOT_FOUNT, result.get(Constants.STATUS));
//PROJECT_ALREADY_EXISTS
result = projectService.update(loginUser, 1, projectName, "desc", "testUser");
result = projectService.update(loginUser, 2L, projectName, "desc", userName);
logger.info(result.toString());
Assert.assertEquals(Status.PROJECT_ALREADY_EXISTS, result.get(Constants.STATUS));
Mockito.when(userMapper.queryByUserNameAccurately(Mockito.any())).thenReturn(null);
result = projectService.update(loginUser, 1, "test", "desc", "testuser");
result = projectService.update(loginUser, 2L, "test", "desc", "testuser");
Assert.assertEquals(Status.USER_NOT_EXIST, result.get(Constants.STATUS));
//success
Mockito.when(userMapper.queryByUserNameAccurately(Mockito.any())).thenReturn(new User());
project.setUserId(1);
Mockito.when(projectMapper.updateById(Mockito.any(Project.class))).thenReturn(1);
result = projectService.update(loginUser, 1, "test", "desc", "testUser");
result = projectService.update(loginUser, 2L, "test", "desc", "testUser");
logger.info(result.toString());
Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));

1
dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/ProjectMapper.java

@ -118,5 +118,4 @@ public interface ProjectMapper extends BaseMapper<Project> {
* @return projectList
*/
List<Project> queryAllProject();
}

2
dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/ProjectMapper.xml

@ -29,7 +29,7 @@
select
<include refid="baseSql"/>
from t_ds_project
where code = #{code}
where code = #{projectCode}
</select>
<select id="queryDetailByCode" resultType="org.apache.dolphinscheduler.dao.entity.Project">

Loading…
Cancel
Save