Browse Source

[Feature][Api] Refactor org.apache.dolphinscheduler.api.controller.QueueController (#11154)

* [Feature][Api] Refactor org.apache.dolphinscheduler.api.controller.QueueController
3.1.0-release
xuhhui 2 years ago committed by GitHub
parent
commit
a50f110d3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 27
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/QueueController.java
  2. 6
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/QueueService.java
  3. 20
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/QueueServiceImpl.java
  4. 17
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/QueueServiceTest.java

27
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/QueueController.java

@ -30,8 +30,6 @@ import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.utils.ParameterUtils;
import org.apache.dolphinscheduler.dao.entity.User;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
@ -74,17 +72,16 @@ public class QueueController extends BaseController {
@ApiException(QUERY_QUEUE_LIST_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result queryList(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser) {
Map<String, Object> result = queueService.queryList(loginUser);
return returnDataList(result);
return queueService.queryList(loginUser);
}
/**
* query queue list paging
*
* @param loginUser login user
* @param pageNo page number
* @param pageNo page number
* @param searchVal search value
* @param pageSize page size
* @param pageSize page size
* @return queue list
*/
@ApiOperation(value = "queryQueueListPaging", notes = "QUERY_QUEUE_LIST_PAGING_NOTES")
@ -115,7 +112,7 @@ public class QueueController extends BaseController {
* create queue
*
* @param loginUser login user
* @param queue queue
* @param queue queue
* @param queueName queue name
* @return create result
*/
@ -131,16 +128,15 @@ public class QueueController extends BaseController {
public Result createQueue(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestParam(value = "queue") String queue,
@RequestParam(value = "queueName") String queueName) {
Map<String, Object> result = queueService.createQueue(loginUser, queue, queueName);
return returnDataList(result);
return queueService.createQueue(loginUser, queue, queueName);
}
/**
* update queue
*
* @param loginUser login user
* @param queue queue
* @param id queue id
* @param queue queue
* @param id queue id
* @param queueName queue name
* @return update result code
*/
@ -158,15 +154,14 @@ public class QueueController extends BaseController {
@PathVariable(value = "id") int id,
@RequestParam(value = "queue") String queue,
@RequestParam(value = "queueName") String queueName) {
Map<String, Object> result = queueService.updateQueue(loginUser, id, queue, queueName);
return returnDataList(result);
return queueService.updateQueue(loginUser, id, queue, queueName);
}
/**
* verify queue and queue name
*
* @param loginUser login user
* @param queue queue
* @param queue queue
* @param queueName queue name
* @return true if the queue name not exists, otherwise return false
*/
@ -181,9 +176,7 @@ public class QueueController extends BaseController {
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result verifyQueue(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestParam(value = "queue") String queue,
@RequestParam(value = "queueName") String queueName
) {
@RequestParam(value = "queueName") String queueName) {
return queueService.verifyQueue(queue, queueName);
}
}

6
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/QueueService.java

@ -34,7 +34,7 @@ public interface QueueService {
* @param loginUser login user
* @return queue list
*/
Map<String, Object> queryList(User loginUser);
Result queryList(User loginUser);
/**
* query queue list paging
@ -55,7 +55,7 @@ public interface QueueService {
* @param queueName queue name
* @return create result
*/
Map<String, Object> createQueue(User loginUser, String queue, String queueName);
Result createQueue(User loginUser, String queue, String queueName);
/**
* update queue
@ -66,7 +66,7 @@ public interface QueueService {
* @param queueName queue name
* @return update result code
*/
Map<String, Object> updateQueue(User loginUser, int id, String queue, String queueName);
Result updateQueue(User loginUser, int id, String queue, String queueName);
/**
* verify queue and queueName

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

@ -116,15 +116,15 @@ public class QueueServiceImpl extends BaseServiceImpl implements QueueService {
* @return queue list
*/
@Override
public Map<String, Object> queryList(User loginUser) {
Map<String, Object> result = new HashMap<>();
public Result queryList(User loginUser) {
Result result = new Result();
Set<Integer> ids = resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.QUEUE, loginUser.getId(), logger);
if (loginUser.getUserType().equals(UserType.GENERAL_USER)) {
ids = ids.isEmpty() ? new HashSet<>() : ids;
ids.add(Constants.DEFAULT_QUEUE_ID);
}
List<Queue> queueList = queueMapper.selectBatchIds(ids);
result.put(Constants.DATA_LIST, queueList);
result.setData(queueList);
putMsg(result, Status.SUCCESS);
return result;
}
@ -169,8 +169,8 @@ public class QueueServiceImpl extends BaseServiceImpl implements QueueService {
*/
@Override
@Transactional
public Map<String, Object> createQueue(User loginUser, String queue, String queueName) {
Map<String, Object> result = new HashMap<>();
public Result createQueue(User loginUser, String queue, String queueName) {
Result result = new Result();
if (!canOperatorPermissions(loginUser,null, AuthorizationType.QUEUE,YARN_QUEUE_CREATE)) {
throw new ServiceException(Status.USER_NO_OPERATION_PERM);
}
@ -179,7 +179,7 @@ public class QueueServiceImpl extends BaseServiceImpl implements QueueService {
createQueueValid(queueObj);
queueMapper.insert(queueObj);
result.put(Constants.DATA_LIST, queueObj);
result.setData(queueObj);
putMsg(result, Status.SUCCESS);
permissionPostHandle(AuthorizationType.QUEUE, loginUser.getId(), Collections.singletonList(queueObj.getId()), logger);
return result;
@ -195,8 +195,8 @@ public class QueueServiceImpl extends BaseServiceImpl implements QueueService {
* @return update result code
*/
@Override
public Map<String, Object> updateQueue(User loginUser, int id, String queue, String queueName) {
Map<String, Object> result = new HashMap<>();
public Result updateQueue(User loginUser, int id, String queue, String queueName) {
Result result = new Result();
if (!canOperatorPermissions(loginUser,new Object[]{id}, AuthorizationType.QUEUE,YARN_QUEUE_UPDATE)) {
throw new ServiceException(Status.USER_NO_OPERATION_PERM);
}
@ -213,7 +213,7 @@ public class QueueServiceImpl extends BaseServiceImpl implements QueueService {
}
queueMapper.updateById(updateQueue);
result.setData(updateQueue);
putMsg(result, Status.SUCCESS);
return result;
}
@ -231,7 +231,7 @@ public class QueueServiceImpl extends BaseServiceImpl implements QueueService {
Queue queueValidator = new Queue(queueName, queue);
createQueueValid(queueValidator);
result.setData(queueValidator);
putMsg(result, Status.SUCCESS);
return result;
}

17
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/QueueServiceTest.java

@ -41,7 +41,6 @@ import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.After;
@ -101,8 +100,8 @@ public class QueueServiceTest {
ids.add(1);
Mockito.when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.QUEUE, getLoginUser().getId(), queueServiceImplLogger)).thenReturn(ids);
Mockito.when(queueMapper.selectBatchIds(Mockito.anySet())).thenReturn(getQueueList());
Map<String, Object> result = queueService.queryList(getLoginUser());
List<Queue> queueList = (List<Queue>) result.get(Constants.DATA_LIST);
Result result = queueService.queryList(getLoginUser());
List<Queue> queueList = (List<Queue>) result.getData();
Assert.assertTrue(CollectionUtils.isNotEmpty(queueList));
}
@ -138,8 +137,8 @@ public class QueueServiceTest {
Assertions.assertEquals(formatter, exception.getMessage());
// correct
Map<String, Object> result = queueService.createQueue(getLoginUser(), QUEUE_NAME, QUEUE_NAME);
Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));
Result result = queueService.createQueue(getLoginUser(), QUEUE_NAME, QUEUE_NAME);
Assert.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue());
}
@Test
@ -172,18 +171,18 @@ public class QueueServiceTest {
//success
Mockito.when(userMapper.existUser(Mockito.anyString())).thenReturn(false);
Map<String, Object> result = queueService.updateQueue(getLoginUser(), 1, NOT_EXISTS, NOT_EXISTS);
Assert.assertEquals(Status.SUCCESS.getCode(), ((Status) result.get(Constants.STATUS)).getCode());
Result result = queueService.updateQueue(getLoginUser(), 1, NOT_EXISTS, NOT_EXISTS);
Assert.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue());
// success update with same queue name
Mockito.when(queueMapper.existQueue(NOT_EXISTS_FINAL, null)).thenReturn(false);
result = queueService.updateQueue(getLoginUser(), 1, NOT_EXISTS_FINAL, NOT_EXISTS);
Assert.assertEquals(Status.SUCCESS.getCode(), ((Status) result.get(Constants.STATUS)).getCode());
Assert.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue());
// success update with same queue value
Mockito.when(queueMapper.existQueue(null, NOT_EXISTS_FINAL)).thenReturn(false);
result = queueService.updateQueue(getLoginUser(), 1, NOT_EXISTS, NOT_EXISTS_FINAL);
Assert.assertEquals(Status.SUCCESS.getCode(), ((Status) result.get(Constants.STATUS)).getCode());
Assert.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue());
}
@Test

Loading…
Cancel
Save