Browse Source

[Improvement][Dao] SQL optimization - check exist #5069 (#5070)

pull/3/MERGE
wenjun 4 years ago committed by GitHub
parent
commit
086e716441
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertGroupServiceImpl.java
  2. 5
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertPluginInstanceServiceImpl.java
  3. 8
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/QueueServiceImpl.java
  4. 5
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java
  5. 7
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/TenantServiceImpl.java
  6. 3
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkFlowLineageServiceImpl.java
  7. 2
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertGroupServiceTest.java
  8. 2
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertPluginInstanceServiceTest.java
  9. 31
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/QueueServiceTest.java
  10. 6
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ResourcesServiceTest.java
  11. 6
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/TenantServiceTest.java
  12. 33
      dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/BooleanUtils.java
  13. 3
      dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/PluginDao.java
  14. 7
      dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/AlertGroupMapper.java
  15. 7
      dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/AlertPluginInstanceMapper.java
  16. 7
      dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/QueueMapper.java
  17. 11
      dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/ResourceMapper.java
  18. 7
      dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/TenantMapper.java
  19. 9
      dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/UserMapper.java
  20. 7
      dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/AlertGroupMapper.xml
  21. 6
      dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/AlertPluginInstanceMapper.xml
  22. 12
      dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/QueueMapper.xml
  23. 8
      dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/ResourceMapper.xml
  24. 5
      dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/TenantMapper.xml
  25. 7
      dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/UserMapper.xml
  26. 9
      dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/AlertGroupMapperTest.java
  27. 8
      dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/AlertPluginInstanceMapperTest.java
  28. 25
      dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/QueueMapperTest.java
  29. 10
      dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ResourceMapperTest.java
  30. 12
      dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/TenantMapperTest.java
  31. 10
      dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/UserMapperTest.java

5
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertGroupServiceImpl.java

@ -21,7 +21,7 @@ import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.service.AlertGroupService; import org.apache.dolphinscheduler.api.service.AlertGroupService;
import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.PageInfo;
import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.utils.CollectionUtils; import org.apache.dolphinscheduler.common.utils.BooleanUtils;
import org.apache.dolphinscheduler.common.utils.StringUtils; import org.apache.dolphinscheduler.common.utils.StringUtils;
import org.apache.dolphinscheduler.dao.entity.AlertGroup; import org.apache.dolphinscheduler.dao.entity.AlertGroup;
import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.dao.entity.User;
@ -207,7 +207,6 @@ public class AlertGroupServiceImpl extends BaseServiceImpl implements AlertGroup
*/ */
@Override @Override
public boolean existGroupName(String groupName) { public boolean existGroupName(String groupName) {
List<AlertGroup> alertGroup = alertGroupMapper.queryByGroupName(groupName); return BooleanUtils.isTrue(alertGroupMapper.existGroupName(groupName));
return CollectionUtils.isNotEmpty(alertGroup);
} }
} }

5
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertPluginInstanceServiceImpl.java

@ -22,6 +22,7 @@ import org.apache.dolphinscheduler.api.service.AlertPluginInstanceService;
import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.PageInfo;
import org.apache.dolphinscheduler.api.vo.AlertPluginInstanceVO; import org.apache.dolphinscheduler.api.vo.AlertPluginInstanceVO;
import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.utils.BooleanUtils;
import org.apache.dolphinscheduler.common.utils.CollectionUtils; import org.apache.dolphinscheduler.common.utils.CollectionUtils;
import org.apache.dolphinscheduler.common.utils.JSONUtils; import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.dolphinscheduler.dao.entity.AlertPluginInstance; import org.apache.dolphinscheduler.dao.entity.AlertPluginInstance;
@ -82,7 +83,7 @@ public class AlertPluginInstanceServiceImpl extends BaseServiceImpl implements A
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
if (CollectionUtils.isNotEmpty(alertPluginInstanceMapper.queryByInstanceName(alertPluginInstance.getInstanceName()))) { if (BooleanUtils.isTrue(alertPluginInstanceMapper.existInstanceName(alertPluginInstance.getInstanceName()))) {
putMsg(result, Status.PLUGIN_INSTANCE_ALREADY_EXIT); putMsg(result, Status.PLUGIN_INSTANCE_ALREADY_EXIT);
return result; return result;
} }
@ -183,7 +184,7 @@ public class AlertPluginInstanceServiceImpl extends BaseServiceImpl implements A
@Override @Override
public boolean checkExistPluginInstanceName(String pluginInstanceName) { public boolean checkExistPluginInstanceName(String pluginInstanceName) {
return CollectionUtils.isNotEmpty(alertPluginInstanceMapper.queryByInstanceName(pluginInstanceName)); return BooleanUtils.isTrue(alertPluginInstanceMapper.existInstanceName(pluginInstanceName));
} }
@Override @Override

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

@ -22,7 +22,7 @@ import org.apache.dolphinscheduler.api.service.QueueService;
import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.PageInfo;
import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.utils.CollectionUtils; import org.apache.dolphinscheduler.common.utils.BooleanUtils;
import org.apache.dolphinscheduler.common.utils.StringUtils; import org.apache.dolphinscheduler.common.utils.StringUtils;
import org.apache.dolphinscheduler.dao.entity.Queue; import org.apache.dolphinscheduler.dao.entity.Queue;
import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.dao.entity.User;
@ -270,7 +270,7 @@ public class QueueServiceImpl extends BaseServiceImpl implements QueueService {
* @return true if the queue not exists, otherwise return false * @return true if the queue not exists, otherwise return false
*/ */
private boolean checkQueueExist(String queue) { private boolean checkQueueExist(String queue) {
return CollectionUtils.isNotEmpty(queueMapper.queryAllQueueList(queue, null)); return BooleanUtils.isTrue(queueMapper.existQueue(queue, null));
} }
/** /**
@ -281,7 +281,7 @@ public class QueueServiceImpl extends BaseServiceImpl implements QueueService {
* @return true if the queue name not exists, otherwise return false * @return true if the queue name not exists, otherwise return false
*/ */
private boolean checkQueueNameExist(String queueName) { private boolean checkQueueNameExist(String queueName) {
return CollectionUtils.isNotEmpty(queueMapper.queryAllQueueList(null, queueName)); return BooleanUtils.isTrue(queueMapper.existQueue(null, queueName));
} }
/** /**
@ -293,7 +293,7 @@ public class QueueServiceImpl extends BaseServiceImpl implements QueueService {
* @return true if need to update user * @return true if need to update user
*/ */
private boolean checkIfQueueIsInUsing (String oldQueue, String newQueue) { private boolean checkIfQueueIsInUsing (String oldQueue, String newQueue) {
return !oldQueue.equals(newQueue) && CollectionUtils.isNotEmpty(userMapper.queryUserListByQueue(oldQueue)); return !oldQueue.equals(newQueue) && BooleanUtils.isTrue(userMapper.existUser(oldQueue));
} }
} }

5
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java

@ -34,6 +34,7 @@ import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.enums.ProgramType; import org.apache.dolphinscheduler.common.enums.ProgramType;
import org.apache.dolphinscheduler.common.enums.ResourceType; import org.apache.dolphinscheduler.common.enums.ResourceType;
import org.apache.dolphinscheduler.common.utils.BooleanUtils;
import org.apache.dolphinscheduler.common.utils.CollectionUtils; import org.apache.dolphinscheduler.common.utils.CollectionUtils;
import org.apache.dolphinscheduler.common.utils.FileUtils; import org.apache.dolphinscheduler.common.utils.FileUtils;
import org.apache.dolphinscheduler.common.utils.HadoopUtils; import org.apache.dolphinscheduler.common.utils.HadoopUtils;
@ -251,8 +252,8 @@ public class ResourcesServiceImpl extends BaseServiceImpl implements ResourcesSe
* @return true if resource exists * @return true if resource exists
*/ */
private boolean checkResourceExists(String fullName, int userId, int type) { private boolean checkResourceExists(String fullName, int userId, int type) {
List<Resource> resources = resourcesMapper.queryResourceList(fullName, userId, type); Boolean existResource = resourcesMapper.existResource(fullName, userId, type);
return resources != null && !resources.isEmpty(); return BooleanUtils.isTrue(existResource);
} }
/** /**

7
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/TenantServiceImpl.java

@ -23,6 +23,7 @@ import org.apache.dolphinscheduler.api.utils.PageInfo;
import org.apache.dolphinscheduler.api.utils.RegexUtils; import org.apache.dolphinscheduler.api.utils.RegexUtils;
import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.utils.BooleanUtils;
import org.apache.dolphinscheduler.common.utils.CollectionUtils; import org.apache.dolphinscheduler.common.utils.CollectionUtils;
import org.apache.dolphinscheduler.common.utils.HadoopUtils; import org.apache.dolphinscheduler.common.utils.HadoopUtils;
import org.apache.dolphinscheduler.common.utils.PropertyUtils; import org.apache.dolphinscheduler.common.utils.PropertyUtils;
@ -95,7 +96,7 @@ public class TenantServiceImpl extends BaseServiceImpl implements TenantService
} }
if (checkTenantExists(tenantCode)) { if (checkTenantExists(tenantCode)) {
putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, tenantCode); putMsg(result, Status.OS_TENANT_CODE_EXIST, tenantCode);
return result; return result;
} }
@ -344,7 +345,7 @@ public class TenantServiceImpl extends BaseServiceImpl implements TenantService
* @return ture if the tenant code exists, otherwise return false * @return ture if the tenant code exists, otherwise return false
*/ */
private boolean checkTenantExists(String tenantCode) { private boolean checkTenantExists(String tenantCode) {
List<Tenant> tenants = tenantMapper.queryByTenantCode(tenantCode); Boolean existTenant = tenantMapper.existTenant(tenantCode);
return CollectionUtils.isNotEmpty(tenants); return BooleanUtils.isTrue(existTenant);
} }
} }

3
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkFlowLineageServiceImpl.java

@ -20,6 +20,7 @@ package org.apache.dolphinscheduler.api.service.impl;
import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.service.WorkFlowLineageService; import org.apache.dolphinscheduler.api.service.WorkFlowLineageService;
import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.utils.CollectionUtils;
import org.apache.dolphinscheduler.dao.entity.WorkFlowLineage; import org.apache.dolphinscheduler.dao.entity.WorkFlowLineage;
import org.apache.dolphinscheduler.dao.entity.WorkFlowRelation; import org.apache.dolphinscheduler.dao.entity.WorkFlowRelation;
import org.apache.dolphinscheduler.dao.mapper.WorkFlowLineageMapper; import org.apache.dolphinscheduler.dao.mapper.WorkFlowLineageMapper;
@ -56,7 +57,7 @@ public class WorkFlowLineageServiceImpl extends BaseServiceImpl implements WorkF
for (int id : ids) { for (int id : ids) {
sourceIds.addAll(ids); sourceIds.addAll(ids);
List<WorkFlowRelation> workFlowRelationsTmp = workFlowLineageMapper.querySourceTarget(id); List<WorkFlowRelation> workFlowRelationsTmp = workFlowLineageMapper.querySourceTarget(id);
if (workFlowRelationsTmp != null && !workFlowRelationsTmp.isEmpty()) { if (CollectionUtils.isNotEmpty(workFlowRelationsTmp)) {
Set<Integer> idsTmp = new HashSet<>(); Set<Integer> idsTmp = new HashSet<>();
for (WorkFlowRelation workFlowRelation:workFlowRelationsTmp) { for (WorkFlowRelation workFlowRelation:workFlowRelationsTmp) {
if (!sourceIds.contains(workFlowRelation.getTargetWorkFlowId())) { if (!sourceIds.contains(workFlowRelation.getTargetWorkFlowId())) {

2
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertGroupServiceTest.java

@ -156,7 +156,7 @@ public class AlertGroupServiceTest {
//group name not exist //group name not exist
boolean result = alertGroupService.existGroupName(groupName); boolean result = alertGroupService.existGroupName(groupName);
Assert.assertFalse(result); Assert.assertFalse(result);
Mockito.when(alertGroupMapper.queryByGroupName(groupName)).thenReturn(getList()); Mockito.when(alertGroupMapper.existGroupName(groupName)).thenReturn(true);
//group name exist //group name exist
result = alertGroupService.existGroupName(groupName); result = alertGroupService.existGroupName(groupName);

2
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertPluginInstanceServiceTest.java

@ -157,7 +157,7 @@ public class AlertPluginInstanceServiceTest {
@Test @Test
public void testCreate() { public void testCreate() {
Mockito.when(alertPluginInstanceMapper.queryByInstanceName("test")).thenReturn(alertPluginInstances); Mockito.when(alertPluginInstanceMapper.existInstanceName("test")).thenReturn(true);
Map<String, Object> result = alertPluginInstanceService.create(user, 1, "test", uiParams); Map<String, Object> result = alertPluginInstanceService.create(user, 1, "test", uiParams);
Assert.assertEquals(Status.PLUGIN_INSTANCE_ALREADY_EXIT, result.get(Constants.STATUS)); Assert.assertEquals(Status.PLUGIN_INSTANCE_ALREADY_EXIT, result.get(Constants.STATUS));
Mockito.when(alertPluginInstanceMapper.insert(Mockito.any())).thenReturn(1); Mockito.when(alertPluginInstanceMapper.insert(Mockito.any())).thenReturn(1);

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

@ -76,7 +76,7 @@ public class QueueServiceTest {
} }
@Test @Test
public void testQueryList(){ public void testQueryList() {
Mockito.when(queueMapper.selectList(null)).thenReturn(getQueueList()); Mockito.when(queueMapper.selectList(null)).thenReturn(getQueueList());
Map<String, Object> result = queueService.queryList(getLoginUser()); Map<String, Object> result = queueService.queryList(getLoginUser());
@ -86,7 +86,7 @@ public class QueueServiceTest {
} }
@Test @Test
public void testQueryListPage(){ public void testQueryListPage() {
IPage<Queue> page = new Page<>(1,10); IPage<Queue> page = new Page<>(1,10);
page.setTotal(1L); page.setTotal(1L);
@ -98,7 +98,7 @@ public class QueueServiceTest {
Assert.assertTrue(CollectionUtils.isNotEmpty(pageInfo.getLists())); Assert.assertTrue(CollectionUtils.isNotEmpty(pageInfo.getLists()));
} }
@Test @Test
public void testCreateQueue(){ public void testCreateQueue() {
// queue is null // queue is null
Map<String, Object> result = queueService.createQueue(getLoginUser(),null,queueName); Map<String, Object> result = queueService.createQueue(getLoginUser(),null,queueName);
@ -114,13 +114,13 @@ public class QueueServiceTest {
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS));
} }
@Test @Test
public void testUpdateQueue(){ public void testUpdateQueue() {
Mockito.when(queueMapper.selectById(1)).thenReturn(getQueue()); Mockito.when(queueMapper.selectById(1)).thenReturn(getQueue());
Mockito.when(queueMapper.queryAllQueueList("test", null)).thenReturn(getQueueList()); Mockito.when(queueMapper.existQueue("test", null)).thenReturn(true);
Mockito.when(queueMapper.queryAllQueueList(null, "test")).thenReturn(getQueueList()); Mockito.when(queueMapper.existQueue(null, "test")).thenReturn(true);
Mockito.when(userMapper.queryUserListByQueue(queueName)).thenReturn(getUserList());
// not exist // not exist
Map<String, Object> result = queueService.updateQueue(getLoginUser(),0,"queue",queueName); Map<String, Object> result = queueService.updateQueue(getLoginUser(),0,"queue",queueName);
@ -144,11 +144,12 @@ public class QueueServiceTest {
Assert.assertEquals(Status.SUCCESS.getCode(),((Status)result.get(Constants.STATUS)).getCode()); Assert.assertEquals(Status.SUCCESS.getCode(),((Status)result.get(Constants.STATUS)).getCode());
} }
@Test @Test
public void testVerifyQueue(){ public void testVerifyQueue() {
Mockito.when(queueMapper.queryAllQueueList(queueName, null)).thenReturn(getQueueList()); Mockito.when(queueMapper.existQueue(queueName, null)).thenReturn(true);
Mockito.when(queueMapper.queryAllQueueList(null, queueName)).thenReturn(getQueueList()); Mockito.when(queueMapper.existQueue(null, queueName)).thenReturn(true);
//queue null //queue null
Result result = queueService.verifyQueue(null,queueName); Result result = queueService.verifyQueue(null,queueName);
@ -175,13 +176,13 @@ public class QueueServiceTest {
logger.info(result.toString()); logger.info(result.toString());
Assert.assertEquals(result.getCode().intValue(), Status.SUCCESS.getCode()); Assert.assertEquals(result.getCode().intValue(), Status.SUCCESS.getCode());
} }
/** /**
* create admin user * create admin user
* @return * @return
*/ */
private User getLoginUser(){ private User getLoginUser() {
User loginUser = new User(); User loginUser = new User();
loginUser.setUserType(UserType.ADMIN_USER); loginUser.setUserType(UserType.ADMIN_USER);
@ -189,7 +190,7 @@ public class QueueServiceTest {
return loginUser; return loginUser;
} }
private List<User> getUserList(){ private List<User> getUserList() {
List<User> list = new ArrayList<>(); List<User> list = new ArrayList<>();
list.add(getLoginUser()); list.add(getLoginUser());
return list; return list;
@ -200,7 +201,7 @@ public class QueueServiceTest {
* get queue * get queue
* @return * @return
*/ */
private Queue getQueue(){ private Queue getQueue() {
Queue queue = new Queue(); Queue queue = new Queue();
queue.setId(1); queue.setId(1);
queue.setQueue(queueName); queue.setQueue(queueName);
@ -208,7 +209,7 @@ public class QueueServiceTest {
return queue; return queue;
} }
private List<Queue> getQueueList(){ private List<Queue> getQueueList() {
List<Queue> queueList = new ArrayList<>(); List<Queue> queueList = new ArrayList<>();
queueList.add(getQueue()); queueList.add(getQueue());
return queueList; return queueList;

6
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ResourcesServiceTest.java

@ -169,7 +169,7 @@ public class ResourcesServiceTest {
Assert.assertEquals(Status.PARENT_RESOURCE_NOT_EXIST.getMsg(), result.getMsg()); Assert.assertEquals(Status.PARENT_RESOURCE_NOT_EXIST.getMsg(), result.getMsg());
//RESOURCE_EXIST //RESOURCE_EXIST
PowerMockito.when(PropertyUtils.getResUploadStartupState()).thenReturn(true); PowerMockito.when(PropertyUtils.getResUploadStartupState()).thenReturn(true);
Mockito.when(resourcesMapper.queryResourceList("/directoryTest", 0, 0)).thenReturn(getResourceList()); Mockito.when(resourcesMapper.existResource("/directoryTest", 0, 0)).thenReturn(true);
result = resourcesService.createDirectory(user, "directoryTest", "directory test", ResourceType.FILE, -1, "/"); result = resourcesService.createDirectory(user, "directoryTest", "directory test", ResourceType.FILE, -1, "/");
logger.info(result.toString()); logger.info(result.toString());
Assert.assertEquals(Status.RESOURCE_EXIST.getMsg(), result.getMsg()); Assert.assertEquals(Status.RESOURCE_EXIST.getMsg(), result.getMsg());
@ -227,7 +227,7 @@ public class ResourcesServiceTest {
Assert.assertEquals(Status.SUCCESS.getMsg(), result.getMsg()); Assert.assertEquals(Status.SUCCESS.getMsg(), result.getMsg());
//RESOURCE_EXIST //RESOURCE_EXIST
Mockito.when(resourcesMapper.queryResourceList("/ResourcesServiceTest1.jar", 0, 0)).thenReturn(getResourceList()); Mockito.when(resourcesMapper.existResource("/ResourcesServiceTest1.jar", 0, 0)).thenReturn(true);
result = resourcesService.updateResource(user, 1, "ResourcesServiceTest1.jar", "ResourcesServiceTest", ResourceType.FILE, null); result = resourcesService.updateResource(user, 1, "ResourcesServiceTest1.jar", "ResourcesServiceTest", ResourceType.FILE, null);
logger.info(result.toString()); logger.info(result.toString());
Assert.assertEquals(Status.RESOURCE_EXIST.getMsg(), result.getMsg()); Assert.assertEquals(Status.RESOURCE_EXIST.getMsg(), result.getMsg());
@ -344,7 +344,7 @@ public class ResourcesServiceTest {
User user = new User(); User user = new User();
user.setId(1); user.setId(1);
Mockito.when(resourcesMapper.queryResourceList("/ResourcesServiceTest.jar", 0, 0)).thenReturn(getResourceList()); Mockito.when(resourcesMapper.existResource("/ResourcesServiceTest.jar", 0, 0)).thenReturn(true);
Result result = resourcesService.verifyResourceName("/ResourcesServiceTest.jar", ResourceType.FILE, user); Result result = resourcesService.verifyResourceName("/ResourcesServiceTest.jar", ResourceType.FILE, user);
logger.info(result.toString()); logger.info(result.toString());
Assert.assertEquals(Status.RESOURCE_EXIST.getMsg(), result.getMsg()); Assert.assertEquals(Status.RESOURCE_EXIST.getMsg(), result.getMsg());

6
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/TenantServiceTest.java

@ -79,7 +79,7 @@ public class TenantServiceTest {
public void testCreateTenant() { public void testCreateTenant() {
User loginUser = getLoginUser(); User loginUser = getLoginUser();
Mockito.when(tenantMapper.queryByTenantCode(tenantCode)).thenReturn(getList()); Mockito.when(tenantMapper.existTenant(tenantCode)).thenReturn(true);
try { try {
//check tenantCode //check tenantCode
Map<String, Object> result = Map<String, Object> result =
@ -90,7 +90,7 @@ public class TenantServiceTest {
//check exist //check exist
result = tenantService.createTenant(loginUser, tenantCode, 1, "TenantServiceTest"); result = tenantService.createTenant(loginUser, tenantCode, 1, "TenantServiceTest");
logger.info(result.toString()); logger.info(result.toString());
Assert.assertEquals(Status.REQUEST_PARAMS_NOT_VALID_ERROR, result.get(Constants.STATUS)); Assert.assertEquals(Status.OS_TENANT_CODE_EXIST, result.get(Constants.STATUS));
// success // success
result = tenantService.createTenant(loginUser, "test", 1, "TenantServiceTest"); result = tenantService.createTenant(loginUser, "test", 1, "TenantServiceTest");
@ -186,7 +186,7 @@ public class TenantServiceTest {
@Test @Test
public void testVerifyTenantCode() { public void testVerifyTenantCode() {
Mockito.when(tenantMapper.queryByTenantCode(tenantCode)).thenReturn(getList()); Mockito.when(tenantMapper.existTenant(tenantCode)).thenReturn(true);
// tenantCode not exist // tenantCode not exist
Result result = tenantService.verifyTenantCode("s00000000000l887888885554444sfjdskfjslakslkdf"); Result result = tenantService.verifyTenantCode("s00000000000l887888885554444sfjdskfjslakslkdf");
logger.info(result.toString()); logger.info(result.toString());

33
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/BooleanUtils.java

@ -0,0 +1,33 @@
/*
* 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.common.utils;
public class BooleanUtils {
public static boolean isTrue(Boolean bool) {
if (bool == null) {
return false;
} else {
return bool;
}
}
public static boolean isNotTrue(Boolean bool) {
return !isTrue(bool);
}
}

3
dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/PluginDao.java

@ -19,6 +19,7 @@ package org.apache.dolphinscheduler.dao;
import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNull;
import org.apache.dolphinscheduler.common.utils.CollectionUtils;
import org.apache.dolphinscheduler.dao.datasource.ConnectionFactory; import org.apache.dolphinscheduler.dao.datasource.ConnectionFactory;
import org.apache.dolphinscheduler.dao.entity.PluginDefine; import org.apache.dolphinscheduler.dao.entity.PluginDefine;
import org.apache.dolphinscheduler.dao.mapper.PluginDefineMapper; import org.apache.dolphinscheduler.dao.mapper.PluginDefineMapper;
@ -64,7 +65,7 @@ public class PluginDao extends AbstractBaseDao {
requireNonNull(pluginDefine.getPluginType(), "pluginType is null"); requireNonNull(pluginDefine.getPluginType(), "pluginType is null");
List<PluginDefine> pluginDefineList = pluginDefineMapper.queryByNameAndType(pluginDefine.getPluginName(), pluginDefine.getPluginType()); List<PluginDefine> pluginDefineList = pluginDefineMapper.queryByNameAndType(pluginDefine.getPluginName(), pluginDefine.getPluginType());
if (pluginDefineList == null || pluginDefineList.size() == 0) { if (CollectionUtils.isEmpty(pluginDefineList)) {
return pluginDefineMapper.insert(pluginDefine); return pluginDefineMapper.insert(pluginDefine);
} }
PluginDefine currPluginDefine = pluginDefineList.get(0); PluginDefine currPluginDefine = pluginDefineList.get(0);

7
dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/AlertGroupMapper.java

@ -48,6 +48,13 @@ public interface AlertGroupMapper extends BaseMapper<AlertGroup> {
*/ */
List<AlertGroup> queryByGroupName(@Param("groupName") String groupName); List<AlertGroup> queryByGroupName(@Param("groupName") String groupName);
/**
* Judge whether the alert group exist
* @param groupName groupName
* @return if exist return true else return null
*/
Boolean existGroupName(@Param("groupName") String groupName);
/** /**
* query by userId * query by userId
* @param userId userId * @param userId userId

7
dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/AlertPluginInstanceMapper.java

@ -44,4 +44,11 @@ public interface AlertPluginInstanceMapper extends BaseMapper<AlertPluginInstanc
List<AlertPluginInstance> queryByInstanceName(@Param("instanceName")String instanceName); List<AlertPluginInstance> queryByInstanceName(@Param("instanceName")String instanceName);
/**
*
* @param instanceName instanceName
* @return if exist return true else return null
*/
Boolean existInstanceName(@Param("instanceName") String instanceName);
} }

7
dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/QueueMapper.java

@ -46,4 +46,11 @@ public interface QueueMapper extends BaseMapper<Queue> {
List<Queue> queryAllQueueList(@Param("queue") String queue, List<Queue> queryAllQueueList(@Param("queue") String queue,
@Param("queueName") String queueName); @Param("queueName") String queueName);
/**
* check the target queue exist
* @param queue queue
* @param queueName queueName
* @return true if exist else return null
*/
Boolean existQueue(@Param("queue") String queue, @Param("queueName") String queueName);
} }

11
dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/ResourceMapper.java

@ -144,4 +144,15 @@ public interface ResourceMapper extends BaseMapper<Resource> {
* @return update num * @return update num
*/ */
int batchUpdateResource(@Param("resourceList") List<Resource> resourceList); int batchUpdateResource(@Param("resourceList") List<Resource> resourceList);
/**
* check resource exist
* @param fullName full name
* @param userId userId
* @param type type
* @return true if exist else return null
*/
Boolean existResource(@Param("fullName") String fullName,
@Param("userId") int userId,
@Param("type") int type);
} }

7
dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/TenantMapper.java

@ -50,4 +50,11 @@ public interface TenantMapper extends BaseMapper<Tenant> {
*/ */
IPage<Tenant> queryTenantPaging(IPage<Tenant> page, IPage<Tenant> queryTenantPaging(IPage<Tenant> page,
@Param("searchVal") String searchVal); @Param("searchVal") String searchVal);
/**
* check tenant exist
* @param tenantCode tenantCode
* @return true if exist else return null
*/
Boolean existTenant(@Param("tenantCode") String tenantCode);
} }

9
dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/UserMapper.java

@ -100,7 +100,14 @@ public interface UserMapper extends BaseMapper<User> {
* @param queueName queue name * @param queueName queue name
* @return user list * @return user list
*/ */
List<User> queryUserListByQueue(@Param("queueName") String queueName); List<User> queryUserListByQueue(@Param("queue") String queueName);
/**
* check the user exist
* @param queueName queue name
* @return true if exist else return null
*/
Boolean existUser(@Param("queue") String queue);
/** /**
* update user with old queue * update user with old queue

7
dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/AlertGroupMapper.xml

@ -38,6 +38,13 @@
from t_ds_alertgroup from t_ds_alertgroup
where group_name=#{groupName} where group_name=#{groupName}
</select> </select>
<select id="existGroupName" resultType="java.lang.Boolean">
select 1
from t_ds_alertgroup
where group_name=#{groupName} limit 1
</select>
<select id="queryByUserId" resultType="org.apache.dolphinscheduler.dao.entity.AlertGroup"> <select id="queryByUserId" resultType="org.apache.dolphinscheduler.dao.entity.AlertGroup">
select select
<include refid="baseSql"/> <include refid="baseSql"/>

6
dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/AlertPluginInstanceMapper.xml

@ -43,4 +43,10 @@
where instance_name = #{instanceName} where instance_name = #{instanceName}
</select> </select>
<select id="existInstanceName" resultType="java.lang.Boolean">
select 1
from t_ds_alert_plugin_instance
where instance_name = #{instanceName} limit 1
</select>
</mapper> </mapper>

12
dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/QueueMapper.xml

@ -43,5 +43,15 @@
and queue_name =#{queueName} and queue_name =#{queueName}
</if> </if>
</select> </select>
<select id="existQueue" resultType="java.lang.Boolean">
select 1 = 1
from t_ds_queue
where 1 = 1
<if test="queue != null and queue != ''">
and queue = #{queue}
</if>
<if test="queueName != null and queueName != ''">
and queue_name =#{queueName}
</if>
</select>
</mapper> </mapper>

8
dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/ResourceMapper.xml

@ -166,4 +166,12 @@
#{i} #{i}
</foreach> </foreach>
</select> </select>
<select id="existResource" resultType="java.lang.Boolean">
select 1
from t_ds_resources
where full_name = #{fullName}
and type = #{type}
and user_id = #{userId} limit 1
</select>
</mapper> </mapper>

5
dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/TenantMapper.xml

@ -54,4 +54,9 @@
</if> </if>
order by t.update_time desc order by t.update_time desc
</select> </select>
<select id="existTenant" resultType="java.lang.Boolean">
select 1
from t_ds_tenant
where tenant_code = #{tenantCode} limit 1
</select>
</mapper> </mapper>

7
dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/UserMapper.xml

@ -103,7 +103,12 @@
select select
<include refid="baseSql"/> <include refid="baseSql"/>
from t_ds_user from t_ds_user
where queue = #{queueName} where queue = #{queue}
</select>
<select id="existUser" resultType="java.lang.Boolean">
select 1
from t_ds_user
where queue = #{queue} limit 1
</select> </select>
<update id="updateUserQueue" parameterType="java.lang.String"> <update id="updateUserQueue" parameterType="java.lang.String">
update t_ds_user update t_ds_user

9
dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/AlertGroupMapperTest.java

@ -31,6 +31,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -161,6 +162,14 @@ public class AlertGroupMapperTest {
compareAlertGroups(alertGroupMap, alertGroupList); compareAlertGroups(alertGroupMap, alertGroupList);
} }
@Test
public void testExistGroupName() {
String groupName = "testGroup";
createAlertGroups(1, groupName);
Assert.assertTrue(alertGroupMapper.existGroupName(groupName));
}
/** /**
* test query all group list * test query all group list
*/ */

8
dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/AlertPluginInstanceMapperTest.java

@ -67,6 +67,14 @@ public class AlertPluginInstanceMapperTest {
AlertGroup alertGroup = testAlertGroupList.get(0); AlertGroup alertGroup = testAlertGroupList.get(0);
} }
@Test
public void testExistInstanceName() {
String instanceName = "test_instance";
Assert.assertNull(alertPluginInstanceMapper.existInstanceName(instanceName));
createAlertPluginInstance();
Assert.assertTrue(alertPluginInstanceMapper.existInstanceName(instanceName));
}
/** /**
* insert * insert
* *

25
dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/QueueMapperTest.java

@ -14,12 +14,18 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.apache.dolphinscheduler.dao.mapper;
package org.apache.dolphinscheduler.dao.mapper;
import org.apache.dolphinscheduler.common.utils.BooleanUtils;
import org.apache.dolphinscheduler.dao.entity.Queue; import org.apache.dolphinscheduler.dao.entity.Queue;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -29,8 +35,6 @@ import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest
@ -47,7 +51,7 @@ public class QueueMapperTest {
* insert * insert
* @return Queue * @return Queue
*/ */
private Queue insertOne(){ private Queue insertOne() {
//insertOne //insertOne
Queue queue = new Queue(); Queue queue = new Queue();
queue.setQueueName("queue"); queue.setQueueName("queue");
@ -62,7 +66,7 @@ public class QueueMapperTest {
* test update * test update
*/ */
@Test @Test
public void testUpdate(){ public void testUpdate() {
//insertOne //insertOne
Queue queue = insertOne(); Queue queue = insertOne();
queue.setCreateTime(new Date()); queue.setCreateTime(new Date());
@ -75,7 +79,7 @@ public class QueueMapperTest {
* test delete * test delete
*/ */
@Test @Test
public void testDelete(){ public void testDelete() {
Queue queue = insertOne(); Queue queue = insertOne();
int delete = queueMapper.deleteById(queue.getId()); int delete = queueMapper.deleteById(queue.getId());
Assert.assertEquals(1, delete); Assert.assertEquals(1, delete);
@ -123,4 +127,13 @@ public class QueueMapperTest {
queues = queueMapper.queryAllQueueList(null, queue.getQueueName()); queues = queueMapper.queryAllQueueList(null, queue.getQueueName());
Assert.assertNotEquals(queues.size(), 0); Assert.assertNotEquals(queues.size(), 0);
} }
@Test
public void existQueue() {
Assert.assertNull(queueMapper.existQueue("queue", null));
Assert.assertNull(queueMapper.existQueue(null, "queue"));
Queue queue = insertOne();
Assert.assertTrue(BooleanUtils.isTrue(queueMapper.existQueue(queue.getQueue(), null)));
Assert.assertTrue(BooleanUtils.isTrue(queueMapper.existQueue(null, queue.getQueueName())));
}
} }

10
dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ResourceMapperTest.java

@ -412,4 +412,14 @@ public class ResourceMapperTest {
Assert.fail("batch update resource data error"); Assert.fail("batch update resource data error");
} }
} }
@Test
public void existResourceTest() {
String fullName = "/ut-resource";
int userId = 111;
int type = ResourceType.FILE.getCode();
Assert.assertNull(resourceMapper.existResource(fullName, userId, type));
insertOne();
Assert.assertTrue(resourceMapper.existResource(fullName, userId, type));
}
} }

12
dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/TenantMapperTest.java

@ -54,6 +54,7 @@ public class TenantMapperTest {
Tenant tenant = new Tenant(); Tenant tenant = new Tenant();
tenant.setCreateTime(new Date()); tenant.setCreateTime(new Date());
tenant.setUpdateTime(new Date()); tenant.setUpdateTime(new Date());
tenant.setTenantCode("test_code");
tenantMapper.insert(tenant); tenantMapper.insert(tenant);
return tenant; return tenant;
} }
@ -122,6 +123,8 @@ public class TenantMapperTest {
Tenant tenant = insertOne(); Tenant tenant = insertOne();
tenant.setTenantCode("ut code"); tenant.setTenantCode("ut code");
tenantMapper.updateById(tenant); tenantMapper.updateById(tenant);
List<Tenant> tenantList = tenantMapper.queryByTenantCode("ut code");
Assert.assertEquals(1, tenantList.size());
} }
/** /**
@ -139,11 +142,18 @@ public class TenantMapperTest {
tenant.setTenantCode("ut code"); tenant.setTenantCode("ut code");
tenant.setQueueId(queue.getId()); tenant.setQueueId(queue.getId());
tenantMapper.updateById(tenant); tenantMapper.updateById(tenant);
Page<Tenant> page = new Page(1,3); Page<Tenant> page = new Page(1, 3);
//tenant.getTenantCode() used instead of tenant.getTenantName() //tenant.getTenantCode() used instead of tenant.getTenantName()
IPage<Tenant> tenantIPage = tenantMapper.queryTenantPaging(page, tenant.getTenantCode()); IPage<Tenant> tenantIPage = tenantMapper.queryTenantPaging(page, tenant.getTenantCode());
Assert.assertNotEquals(tenantIPage.getTotal(), 0); Assert.assertNotEquals(tenantIPage.getTotal(), 0);
} }
public void testExistTenant() {
String tenantCode = "test_code";
Assert.assertNull(tenantMapper.existTenant(tenantCode));
insertOne();
Assert.assertTrue(tenantMapper.existTenant(tenantCode));
}
} }

10
dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/UserMapperTest.java

@ -74,6 +74,8 @@ public class UserMapperTest {
user.setCreateTime(new Date()); user.setCreateTime(new Date());
user.setTenantId(1); user.setTenantId(1);
user.setUpdateTime(new Date()); user.setUpdateTime(new Date());
user.setQueueName("test_queue");
user.setQueue("queue");
userMapper.insert(user); userMapper.insert(user);
return user; return user;
} }
@ -312,4 +314,12 @@ public class UserMapperTest {
Assert.assertEquals(userToken, user); Assert.assertEquals(userToken, user);
} }
@Test
public void testExistUser() {
String queueName = "queue";
Assert.assertNull(userMapper.existUser(queueName));
insertOne();
Assert.assertTrue(userMapper.existUser(queueName));
}
} }

Loading…
Cancel
Save