Tboy
5 years ago
committed by
GitHub
16 changed files with 2040 additions and 68 deletions
@ -0,0 +1,181 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.apache.dolphinscheduler.api.service; |
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage; |
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
||||
import org.apache.commons.lang3.time.DateUtils; |
||||
import org.apache.dolphinscheduler.api.enums.Status; |
||||
import org.apache.dolphinscheduler.api.utils.PageInfo; |
||||
import org.apache.dolphinscheduler.common.Constants; |
||||
import org.apache.dolphinscheduler.common.enums.UserType; |
||||
import org.apache.dolphinscheduler.dao.entity.AccessToken; |
||||
import org.apache.dolphinscheduler.dao.entity.User; |
||||
import org.apache.dolphinscheduler.dao.mapper.AccessTokenMapper; |
||||
import org.junit.After; |
||||
import org.junit.Assert; |
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.mockito.InjectMocks; |
||||
import org.mockito.Mock; |
||||
import org.mockito.junit.MockitoJUnitRunner; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Date; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import static org.mockito.ArgumentMatchers.any; |
||||
import static org.mockito.ArgumentMatchers.eq; |
||||
import static org.mockito.Mockito.when; |
||||
|
||||
@RunWith(MockitoJUnitRunner.class) |
||||
public class AccessTokenServiceTest { |
||||
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(AccessTokenServiceTest.class); |
||||
|
||||
|
||||
@InjectMocks |
||||
private AccessTokenService accessTokenService ; |
||||
|
||||
@Mock |
||||
private AccessTokenMapper accessTokenMapper; |
||||
|
||||
@Before |
||||
public void setUp() { |
||||
|
||||
} |
||||
|
||||
|
||||
@After |
||||
public void after(){ |
||||
|
||||
} |
||||
|
||||
|
||||
|
||||
@Test |
||||
public void testQueryAccessTokenList(){ |
||||
|
||||
IPage<AccessToken> tokenPage = new Page<>(); |
||||
tokenPage.setRecords(getList()); |
||||
tokenPage.setTotal(1L); |
||||
when(accessTokenMapper.selectAccessTokenPage(any(Page.class),eq("zhangsan"),eq(0))).thenReturn(tokenPage); |
||||
|
||||
User user =new User(); |
||||
Map<String, Object> result = accessTokenService.queryAccessTokenList(user,"zhangsan",1,10); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||
PageInfo<AccessToken> pageInfo = (PageInfo<AccessToken>) result.get(Constants.DATA_LIST); |
||||
Assert.assertTrue(pageInfo.getTotalCount()>0); |
||||
} |
||||
|
||||
@Test |
||||
public void testCreateToken(){ |
||||
|
||||
|
||||
when(accessTokenMapper.insert(any(AccessToken.class))).thenReturn(2); |
||||
Map<String, Object> result = accessTokenService.createToken(1,getDate(),"AccessTokenServiceTest"); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||
} |
||||
|
||||
@Test |
||||
public void testGenerateToken(){ |
||||
|
||||
Map<String, Object> result = accessTokenService.generateToken(Integer.MAX_VALUE,getDate()); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||
String token = (String) result.get(Constants.DATA_LIST); |
||||
Assert.assertNotNull(token); |
||||
} |
||||
|
||||
@Test |
||||
public void testDelAccessTokenById(){ |
||||
|
||||
when(accessTokenMapper.selectById(1)).thenReturn(getEntity()); |
||||
User userLogin = new User(); |
||||
// not exist
|
||||
Map<String, Object> result = accessTokenService.delAccessTokenById(userLogin,0); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.ACCESS_TOKEN_NOT_EXIST,result.get(Constants.STATUS)); |
||||
// no operate
|
||||
result = accessTokenService.delAccessTokenById(userLogin,1); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.USER_NO_OPERATION_PERM,result.get(Constants.STATUS)); |
||||
//success
|
||||
userLogin.setId(1); |
||||
userLogin.setUserType(UserType.ADMIN_USER); |
||||
result = accessTokenService.delAccessTokenById(userLogin,1); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||
} |
||||
|
||||
@Test |
||||
public void testUpdateToken(){ |
||||
|
||||
when(accessTokenMapper.selectById(1)).thenReturn(getEntity()); |
||||
Map<String, Object> result = accessTokenService.updateToken(1,Integer.MAX_VALUE,getDate(),"token"); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||
// not exist
|
||||
result = accessTokenService.updateToken(2,Integer.MAX_VALUE,getDate(),"token"); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.ACCESS_TOKEN_NOT_EXIST,result.get(Constants.STATUS)); |
||||
|
||||
} |
||||
|
||||
/** |
||||
* create entity |
||||
* @return |
||||
*/ |
||||
private AccessToken getEntity(){ |
||||
AccessToken accessToken = new AccessToken(); |
||||
accessToken.setId(1); |
||||
accessToken.setUserId(1); |
||||
accessToken.setToken("AccessTokenServiceTest"); |
||||
Date date = DateUtils.addDays(new Date(),30); |
||||
accessToken.setExpireTime(date); |
||||
return accessToken; |
||||
} |
||||
|
||||
/** |
||||
* entity list |
||||
* @return |
||||
*/ |
||||
private List<AccessToken> getList(){ |
||||
|
||||
List<AccessToken> list = new ArrayList<>(); |
||||
list.add(getEntity()); |
||||
return list; |
||||
} |
||||
|
||||
|
||||
|
||||
/** |
||||
* get dateStr |
||||
* @return |
||||
*/ |
||||
private String getDate(){ |
||||
Date date = DateUtils.addDays(new Date(),30); |
||||
return org.apache.dolphinscheduler.common.utils.DateUtils.dateToString(date); |
||||
} |
||||
} |
@ -0,0 +1,219 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.apache.dolphinscheduler.api.service; |
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage; |
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
||||
import org.apache.dolphinscheduler.api.enums.Status; |
||||
import org.apache.dolphinscheduler.api.utils.PageInfo; |
||||
import org.apache.dolphinscheduler.api.utils.Result; |
||||
import org.apache.dolphinscheduler.common.Constants; |
||||
import org.apache.dolphinscheduler.common.enums.AlertType; |
||||
import org.apache.dolphinscheduler.common.enums.UserType; |
||||
import org.apache.dolphinscheduler.common.utils.CollectionUtils; |
||||
import org.apache.dolphinscheduler.dao.entity.AlertGroup; |
||||
import org.apache.dolphinscheduler.dao.entity.User; |
||||
import org.apache.dolphinscheduler.dao.mapper.AlertGroupMapper; |
||||
import org.apache.dolphinscheduler.dao.mapper.UserAlertGroupMapper; |
||||
import org.junit.After; |
||||
import org.junit.Assert; |
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.mockito.InjectMocks; |
||||
import org.mockito.Mock; |
||||
import org.mockito.Mockito; |
||||
import org.mockito.junit.MockitoJUnitRunner; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import static org.mockito.ArgumentMatchers.any; |
||||
import static org.mockito.ArgumentMatchers.eq; |
||||
|
||||
@RunWith(MockitoJUnitRunner.class) |
||||
public class AlertGroupServiceTest { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(AlertGroupServiceTest.class); |
||||
|
||||
@InjectMocks |
||||
private AlertGroupService alertGroupService; |
||||
@Mock |
||||
private AlertGroupMapper alertGroupMapper; |
||||
@Mock |
||||
private UserAlertGroupMapper userAlertGroupMapper; |
||||
|
||||
private String groupName = "AlertGroupServiceTest"; |
||||
|
||||
@Before |
||||
public void setUp() { |
||||
} |
||||
|
||||
|
||||
@After |
||||
public void after(){ |
||||
|
||||
} |
||||
|
||||
|
||||
|
||||
@Test |
||||
public void testQueryAlertgroup(){ |
||||
|
||||
Mockito.when(alertGroupMapper.queryAllGroupList()).thenReturn(getList()); |
||||
HashMap<String, Object> result= alertGroupService.queryAlertgroup(); |
||||
logger.info(result.toString()); |
||||
List<AlertGroup> alertGroups = (List<AlertGroup>) result.get(Constants.DATA_LIST); |
||||
Assert.assertTrue(CollectionUtils.isNotEmpty(alertGroups)); |
||||
} |
||||
@Test |
||||
public void testListPaging(){ |
||||
IPage<AlertGroup> page = new Page<>(1,10); |
||||
page.setTotal(1L); |
||||
page.setRecords(getList()); |
||||
Mockito.when(alertGroupMapper.queryAlertGroupPage(any(Page.class),eq(groupName))).thenReturn(page); |
||||
User user = new User(); |
||||
// no operate
|
||||
Map<String, Object> result = alertGroupService.listPaging(user,groupName,1,10); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.USER_NO_OPERATION_PERM,result.get(Constants.STATUS)); |
||||
//success
|
||||
user.setUserType(UserType.ADMIN_USER); |
||||
result = alertGroupService.listPaging(user,groupName,1,10); |
||||
logger.info(result.toString()); |
||||
PageInfo<AlertGroup> pageInfo = (PageInfo<AlertGroup>) result.get(Constants.DATA_LIST); |
||||
Assert.assertTrue(CollectionUtils.isNotEmpty(pageInfo.getLists())); |
||||
|
||||
} |
||||
@Test |
||||
public void testCreateAlertgroup(){ |
||||
|
||||
|
||||
Mockito.when(alertGroupMapper.insert(any(AlertGroup.class))).thenReturn(2); |
||||
User user = new User(); |
||||
//no operate
|
||||
Map<String, Object> result = alertGroupService.createAlertgroup(user,groupName, AlertType.EMAIL,groupName); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.USER_NO_OPERATION_PERM,result.get(Constants.STATUS)); |
||||
user.setUserType(UserType.ADMIN_USER); |
||||
//success
|
||||
result = alertGroupService.createAlertgroup(user,groupName, AlertType.EMAIL,groupName); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||
} |
||||
@Test |
||||
public void testUpdateAlertgroup(){ |
||||
|
||||
User user = new User(); |
||||
// no operate
|
||||
Map<String, Object> result = alertGroupService.updateAlertgroup(user,1,groupName, AlertType.SMS,groupName); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.USER_NO_OPERATION_PERM,result.get(Constants.STATUS)); |
||||
user.setUserType(UserType.ADMIN_USER); |
||||
// not exist
|
||||
result = alertGroupService.updateAlertgroup(user,1,groupName, AlertType.SMS,groupName); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.ALERT_GROUP_NOT_EXIST,result.get(Constants.STATUS)); |
||||
//success
|
||||
Mockito.when(alertGroupMapper.selectById(2)).thenReturn(getEntity()); |
||||
result = alertGroupService.updateAlertgroup(user,2,groupName, AlertType.SMS,groupName); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||
|
||||
} |
||||
@Test |
||||
public void testDelAlertgroupById(){ |
||||
|
||||
User user = new User(); |
||||
// no operate
|
||||
Map<String, Object> result = alertGroupService.delAlertgroupById(user,1); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.USER_NO_OPERATION_PERM,result.get(Constants.STATUS)); |
||||
user.setUserType(UserType.ADMIN_USER); |
||||
// not exist
|
||||
result = alertGroupService.delAlertgroupById(user,2); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.ALERT_GROUP_NOT_EXIST,result.get(Constants.STATUS)); |
||||
//success
|
||||
Mockito.when(alertGroupMapper.selectById(2)).thenReturn(getEntity()); |
||||
result = alertGroupService.delAlertgroupById(user,2); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||
|
||||
|
||||
} |
||||
@Test |
||||
public void testGrantUser(){ |
||||
|
||||
Map<String, Object> result = alertGroupService.grantUser(getLoginUser(),1,"123,321"); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||
} |
||||
@Test |
||||
public void testVerifyGroupName(){ |
||||
//group name not exist
|
||||
Result result = alertGroupService.verifyGroupName(getLoginUser(), groupName); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.SUCCESS.getMsg(),result.getMsg()); |
||||
Mockito.when(alertGroupMapper.queryByGroupName(groupName)).thenReturn(getList()); |
||||
|
||||
//group name exist
|
||||
result = alertGroupService.verifyGroupName(getLoginUser(), groupName); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.ALERT_GROUP_EXIST.getMsg(),result.getMsg()); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* create admin user |
||||
* @return |
||||
*/ |
||||
private User getLoginUser(){ |
||||
|
||||
User loginUser = new User(); |
||||
loginUser.setUserType(UserType.ADMIN_USER); |
||||
loginUser.setId(99999999); |
||||
return loginUser; |
||||
} |
||||
|
||||
/** |
||||
* get list |
||||
* @return |
||||
*/ |
||||
private List<AlertGroup> getList(){ |
||||
List<AlertGroup> alertGroups = new ArrayList<>(); |
||||
alertGroups.add(getEntity()); |
||||
return alertGroups; |
||||
} |
||||
|
||||
/** |
||||
* get entity |
||||
* @return |
||||
*/ |
||||
private AlertGroup getEntity(){ |
||||
AlertGroup alertGroup = new AlertGroup(); |
||||
alertGroup.setId(1); |
||||
alertGroup.setGroupName(groupName); |
||||
alertGroup.setGroupType(AlertType.EMAIL); |
||||
return alertGroup; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,101 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.apache.dolphinscheduler.api.service; |
||||
|
||||
import org.apache.dolphinscheduler.api.enums.Status; |
||||
import org.apache.dolphinscheduler.common.Constants; |
||||
import org.apache.dolphinscheduler.common.enums.DbType; |
||||
import org.apache.dolphinscheduler.common.model.Server; |
||||
import org.apache.dolphinscheduler.common.utils.CollectionUtils; |
||||
import org.apache.dolphinscheduler.dao.MonitorDBDao; |
||||
import org.apache.dolphinscheduler.dao.entity.MonitorRecord; |
||||
import org.junit.Assert; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.mockito.InjectMocks; |
||||
import org.mockito.Mock; |
||||
import org.mockito.Mockito; |
||||
import org.mockito.junit.MockitoJUnitRunner; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
@RunWith(MockitoJUnitRunner.class) |
||||
public class MonitorServiceTest { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(MonitorServiceTest.class); |
||||
|
||||
@InjectMocks |
||||
private MonitorService monitorService; |
||||
@Mock |
||||
private MonitorDBDao monitorDBDao; |
||||
|
||||
|
||||
@Test |
||||
public void testQueryDatabaseState(){ |
||||
|
||||
Mockito.when(monitorDBDao.queryDatabaseState()).thenReturn(getList()); |
||||
Map<String,Object> result = monitorService.queryDatabaseState(null); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||
List<MonitorRecord> monitorRecordList = (List<MonitorRecord>) result.get(Constants.DATA_LIST); |
||||
Assert.assertTrue(CollectionUtils.isNotEmpty(monitorRecordList)); |
||||
} |
||||
@Test |
||||
public void testQueryMaster(){ |
||||
//TODO need zk
|
||||
// Map<String,Object> result = monitorService.queryMaster(null);
|
||||
// logger.info(result.toString());
|
||||
// Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS));
|
||||
} |
||||
@Test |
||||
public void testQueryZookeeperState(){ |
||||
//TODO need zk
|
||||
// Map<String,Object> result = monitorService.queryZookeeperState(null);
|
||||
// logger.info(result.toString());
|
||||
// Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS));
|
||||
} |
||||
|
||||
@Test |
||||
public void testGetServerListFromZK(){ |
||||
//TODO need zk
|
||||
// List<Server> serverList = monitorService.getServerListFromZK(true);
|
||||
// logger.info(serverList.toString());
|
||||
} |
||||
|
||||
private List<MonitorRecord> getList(){ |
||||
List<MonitorRecord> monitorRecordList = new ArrayList<>(); |
||||
monitorRecordList.add(getEntity()); |
||||
return monitorRecordList; |
||||
} |
||||
|
||||
private MonitorRecord getEntity(){ |
||||
MonitorRecord monitorRecord = new MonitorRecord(); |
||||
monitorRecord.setDbType(DbType.MYSQL); |
||||
return monitorRecord; |
||||
} |
||||
|
||||
private List<Server> getServerList(){ |
||||
List<Server> servers = new ArrayList<>(); |
||||
servers.add(new Server()); |
||||
return servers; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,356 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.apache.dolphinscheduler.api.service; |
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage; |
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
||||
import org.apache.dolphinscheduler.api.enums.Status; |
||||
import org.apache.dolphinscheduler.api.utils.PageInfo; |
||||
import org.apache.dolphinscheduler.common.Constants; |
||||
import org.apache.dolphinscheduler.common.enums.UserType; |
||||
import org.apache.dolphinscheduler.common.utils.CollectionUtils; |
||||
import org.apache.dolphinscheduler.dao.entity.ProcessDefinition; |
||||
import org.apache.dolphinscheduler.dao.entity.Project; |
||||
import org.apache.dolphinscheduler.dao.entity.ProjectUser; |
||||
import org.apache.dolphinscheduler.dao.entity.User; |
||||
import org.apache.dolphinscheduler.dao.mapper.ProcessDefinitionMapper; |
||||
import org.apache.dolphinscheduler.dao.mapper.ProjectMapper; |
||||
import org.apache.dolphinscheduler.dao.mapper.ProjectUserMapper; |
||||
import org.apache.dolphinscheduler.dao.mapper.UserMapper; |
||||
import org.junit.After; |
||||
import org.junit.Assert; |
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.mockito.InjectMocks; |
||||
import org.mockito.Mock; |
||||
import org.mockito.Mockito; |
||||
import org.mockito.junit.MockitoJUnitRunner; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
@RunWith(MockitoJUnitRunner.class) |
||||
public class ProjectServiceTest { |
||||
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ProjectServiceTest.class); |
||||
|
||||
@InjectMocks |
||||
private ProjectService projectService; |
||||
@Mock |
||||
private ProjectMapper projectMapper; |
||||
@Mock |
||||
private UserMapper userMapper; |
||||
@Mock |
||||
private ProjectUserMapper projectUserMapper; |
||||
@Mock |
||||
private ProcessDefinitionMapper processDefinitionMapper; |
||||
|
||||
|
||||
|
||||
private String projectName = "ProjectServiceTest"; |
||||
|
||||
private String userName = "ProjectServiceTest"; |
||||
|
||||
@Before |
||||
public void setUp() { |
||||
|
||||
} |
||||
|
||||
|
||||
@After |
||||
public void after(){ |
||||
|
||||
} |
||||
|
||||
@Test |
||||
public void testCreateProject(){ |
||||
|
||||
User loginUser = getLoginUser(); |
||||
loginUser.setId(1); |
||||
Map<String, Object> result = projectService.createProject(loginUser, projectName, getDesc()); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.REQUEST_PARAMS_NOT_VALID_ERROR,result.get(Constants.STATUS)); |
||||
|
||||
//project name exist
|
||||
Mockito.when(projectMapper.queryByName(projectName)).thenReturn(getProject()); |
||||
result = projectService.createProject(loginUser, projectName, projectName); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.PROJECT_ALREADY_EXISTS,result.get(Constants.STATUS)); |
||||
|
||||
//success
|
||||
Mockito.when(projectMapper.insert(Mockito.any(Project.class))).thenReturn(1); |
||||
result = projectService.createProject(loginUser, "test", "test"); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||
|
||||
|
||||
} |
||||
@Test |
||||
public void testQueryById(){ |
||||
|
||||
//not exist
|
||||
Map<String, Object> result = projectService.queryById(Integer.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); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||
|
||||
} |
||||
@Test |
||||
public void testCheckProjectAndAuth(){ |
||||
|
||||
Mockito.when(projectUserMapper.queryProjectRelation(1, 1)).thenReturn(getProjectUser()); |
||||
User loginUser = getLoginUser(); |
||||
|
||||
Map<String, Object> result = projectService.checkProjectAndAuth(loginUser,null,projectName); |
||||
logger.info(result.toString()); |
||||
Status status = (Status)result.get(Constants.STATUS); |
||||
Assert.assertEquals(Status.PROJECT_NOT_FOUNT,result.get(Constants.STATUS)); |
||||
|
||||
Project project = getProject(); |
||||
//USER_NO_OPERATION_PROJECT_PERM
|
||||
project.setUserId(2); |
||||
result = projectService.checkProjectAndAuth(loginUser,project,projectName); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.USER_NO_OPERATION_PROJECT_PERM,result.get(Constants.STATUS)); |
||||
|
||||
//success
|
||||
project.setUserId(1); |
||||
result = projectService.checkProjectAndAuth(loginUser,project,projectName); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
public void testHasProjectAndPerm(){ |
||||
|
||||
// Mockito.when(projectUserMapper.queryProjectRelation(1, 1)).thenReturn(getProjectUser());
|
||||
User loginUser = getLoginUser(); |
||||
Project project = getProject(); |
||||
Map<String, Object> result = new HashMap<>(); |
||||
// not exist user
|
||||
User tempUser = new User(); |
||||
tempUser.setId(Integer.MAX_VALUE); |
||||
boolean checkResult = projectService.hasProjectAndPerm(tempUser,project,result); |
||||
logger.info(result.toString()); |
||||
Assert.assertFalse(checkResult); |
||||
|
||||
//success
|
||||
result = new HashMap<>(); |
||||
project.setUserId(1); |
||||
checkResult = projectService.hasProjectAndPerm(loginUser,project,result); |
||||
logger.info(result.toString()); |
||||
Assert.assertTrue(checkResult); |
||||
} |
||||
@Test |
||||
public void testQueryProjectListPaging(){ |
||||
|
||||
IPage<Project> page = new Page<>(1,10); |
||||
page.setRecords(getList()); |
||||
page.setTotal(1L); |
||||
Mockito.when(projectMapper.queryProjectListPaging(Mockito.any(Page.class), Mockito.eq(1), Mockito.eq(projectName))).thenReturn(page); |
||||
User loginUser = getLoginUser(); |
||||
|
||||
// project owner
|
||||
Map<String, Object> result = projectService.queryProjectListPaging(loginUser,10,1,projectName); |
||||
logger.info(result.toString()); |
||||
PageInfo<Project> pageInfo = (PageInfo<Project>) result.get(Constants.DATA_LIST); |
||||
Assert.assertTrue(CollectionUtils.isNotEmpty(pageInfo.getLists())); |
||||
|
||||
//admin
|
||||
Mockito.when(projectMapper.queryProjectListPaging(Mockito.any(Page.class), Mockito.eq(0), Mockito.eq(projectName))).thenReturn(page); |
||||
loginUser.setUserType(UserType.ADMIN_USER); |
||||
result = projectService.queryProjectListPaging(loginUser,10,1,projectName); |
||||
logger.info(result.toString()); |
||||
pageInfo = (PageInfo<Project>) result.get(Constants.DATA_LIST); |
||||
Assert.assertTrue(CollectionUtils.isNotEmpty(pageInfo.getLists())); |
||||
} |
||||
@Test |
||||
public void testDeleteProject(){ |
||||
|
||||
Mockito.when(projectMapper.selectById(1)).thenReturn(getProject()); |
||||
User loginUser = getLoginUser(); |
||||
//PROJECT_NOT_FOUNT
|
||||
Map<String, Object> result= projectService.deleteProject(loginUser,12); |
||||
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); |
||||
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(1)).thenReturn(getProcessDefinitions()); |
||||
loginUser.setUserType(UserType.ADMIN_USER); |
||||
result= projectService.deleteProject(loginUser,1); |
||||
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(1)).thenReturn(new ArrayList<>()); |
||||
result= projectService.deleteProject(loginUser,1); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||
|
||||
|
||||
} |
||||
|
||||
@Test |
||||
public void testUpdate(){ |
||||
|
||||
User loginUser = getLoginUser(); |
||||
Project project = getProject(); |
||||
project.setId(2); |
||||
Mockito.when(projectMapper.queryByName(projectName)).thenReturn(project); |
||||
Mockito.when( projectMapper.selectById(1)).thenReturn(getProject()); |
||||
// PROJECT_NOT_FOUNT
|
||||
Map<String, Object> result = projectService.update(loginUser,12,projectName,"desc"); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.PROJECT_NOT_FOUNT,result.get(Constants.STATUS)); |
||||
|
||||
//PROJECT_ALREADY_EXISTS
|
||||
result = projectService.update(loginUser,1,projectName,"desc"); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.PROJECT_ALREADY_EXISTS,result.get(Constants.STATUS)); |
||||
|
||||
//success
|
||||
project.setUserId(1); |
||||
Mockito.when(projectMapper.updateById(Mockito.any(Project.class))).thenReturn(1); |
||||
result = projectService.update(loginUser,1,"test","desc"); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||
|
||||
} |
||||
@Test |
||||
public void testQueryAuthorizedProject(){ |
||||
|
||||
User loginUser = getLoginUser(); |
||||
|
||||
Mockito.when(projectMapper.queryAuthedProjectListByUserId(1)).thenReturn(getList()); |
||||
//USER_NO_OPERATION_PERM
|
||||
Map<String, Object> result = projectService.queryAuthorizedProject(loginUser,3); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.USER_NO_OPERATION_PERM,result.get(Constants.STATUS)); |
||||
|
||||
//success
|
||||
loginUser.setUserType(UserType.ADMIN_USER); |
||||
result = projectService.queryAuthorizedProject(loginUser,1); |
||||
logger.info(result.toString()); |
||||
List<Project> projects = (List<Project>) result.get(Constants.DATA_LIST); |
||||
Assert.assertTrue(CollectionUtils.isNotEmpty(projects)); |
||||
|
||||
} |
||||
@Test |
||||
public void testQueryAllProjectList(){ |
||||
|
||||
Mockito.when(projectMapper.selectList(null)).thenReturn(getList()); |
||||
Mockito.when(processDefinitionMapper.selectList(null)).thenReturn(getProcessDefinitions()); |
||||
|
||||
Map<String, Object> result = projectService.queryAllProjectList(); |
||||
logger.info(result.toString()); |
||||
List<Project> projects = (List<Project>) result.get(Constants.DATA_LIST); |
||||
Assert.assertTrue(CollectionUtils.isNotEmpty(projects)); |
||||
|
||||
} |
||||
@Test |
||||
public void testQueryUnauthorizedProject(){ |
||||
// Mockito.when(projectMapper.queryAuthedProjectListByUserId(1)).thenReturn(getList());
|
||||
Mockito.when(projectMapper.queryProjectExceptUserId(2)).thenReturn(getList()); |
||||
|
||||
User loginUser = new User(); |
||||
loginUser.setUserType(UserType.ADMIN_USER); |
||||
|
||||
Map<String, Object> result = projectService.queryUnauthorizedProject(loginUser,2); |
||||
logger.info(result.toString()); |
||||
List<Project> projects = (List<Project>) result.get(Constants.DATA_LIST); |
||||
Assert.assertTrue(CollectionUtils.isNotEmpty(projects)); |
||||
} |
||||
|
||||
|
||||
private Project getProject(){ |
||||
Project project = new Project(); |
||||
project.setId(1); |
||||
project.setName(projectName); |
||||
project.setUserId(1); |
||||
return project; |
||||
} |
||||
|
||||
private List<Project> getList(){ |
||||
List<Project> list = new ArrayList<>(); |
||||
list.add(getProject()); |
||||
return list; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* create admin user |
||||
* @return |
||||
*/ |
||||
private User getLoginUser(){ |
||||
|
||||
User loginUser = new User(); |
||||
loginUser.setUserType(UserType.GENERAL_USER); |
||||
loginUser.setUserName(userName); |
||||
loginUser.setId(1); |
||||
return loginUser; |
||||
|
||||
} |
||||
|
||||
/** |
||||
* get project user |
||||
|
||||
*/ |
||||
private ProjectUser getProjectUser(){ |
||||
ProjectUser projectUser = new ProjectUser(); |
||||
projectUser.setProjectId(1); |
||||
projectUser.setUserId(1); |
||||
return projectUser; |
||||
} |
||||
|
||||
private List<ProcessDefinition> getProcessDefinitions(){ |
||||
List<ProcessDefinition> list = new ArrayList<>(); |
||||
ProcessDefinition processDefinition = new ProcessDefinition(); |
||||
processDefinition.setProjectId(1); |
||||
list.add(processDefinition); |
||||
return list; |
||||
} |
||||
|
||||
|
||||
|
||||
|
||||
private String getDesc(){ |
||||
return "projectUserMapper.deleteProjectRelation(projectId,userId)projectUserMappe" + |
||||
".deleteProjectRelation(projectId,userId)projectUserMappe" + |
||||
"r.deleteProjectRelation(projectId,userId)projectUserMapper" + |
||||
".deleteProjectRelation(projectId,userId)projectUserMapper.deleteProjectRelation(projectId,userId)"; |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,208 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.apache.dolphinscheduler.api.service; |
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage; |
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
||||
import org.apache.dolphinscheduler.api.enums.Status; |
||||
import org.apache.dolphinscheduler.api.utils.PageInfo; |
||||
import org.apache.dolphinscheduler.api.utils.Result; |
||||
import org.apache.dolphinscheduler.common.Constants; |
||||
import org.apache.dolphinscheduler.common.enums.UserType; |
||||
import org.apache.dolphinscheduler.common.utils.CollectionUtils; |
||||
import org.apache.dolphinscheduler.dao.entity.Queue; |
||||
import org.apache.dolphinscheduler.dao.entity.User; |
||||
import org.apache.dolphinscheduler.dao.mapper.QueueMapper; |
||||
import org.apache.dolphinscheduler.dao.mapper.UserMapper; |
||||
import org.junit.After; |
||||
import org.junit.Assert; |
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.mockito.InjectMocks; |
||||
import org.mockito.Mock; |
||||
import org.mockito.Mockito; |
||||
import org.mockito.junit.MockitoJUnitRunner; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
@RunWith(MockitoJUnitRunner.class) |
||||
public class QueueServiceTest { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(QueueServiceTest.class); |
||||
|
||||
@InjectMocks |
||||
private QueueService queueService; |
||||
@Mock |
||||
private QueueMapper queueMapper; |
||||
@Mock |
||||
private UserMapper userMapper; |
||||
private String queueName = "QueueServiceTest"; |
||||
|
||||
@Before |
||||
public void setUp() { |
||||
} |
||||
|
||||
|
||||
@After |
||||
public void after(){ |
||||
} |
||||
|
||||
@Test |
||||
public void testQueryList(){ |
||||
|
||||
Mockito.when(queueMapper.selectList(null)).thenReturn(getQueueList()); |
||||
Map<String, Object> result = queueService.queryList(getLoginUser()); |
||||
logger.info(result.toString()); |
||||
List<Queue> queueList = (List<Queue>) result.get(Constants.DATA_LIST); |
||||
Assert.assertTrue(CollectionUtils.isNotEmpty(queueList)); |
||||
|
||||
} |
||||
@Test |
||||
public void testQueryListPage(){ |
||||
|
||||
IPage<Queue> page = new Page<>(1,10); |
||||
page.setTotal(1L); |
||||
page.setRecords(getQueueList()); |
||||
Mockito.when(queueMapper.queryQueuePaging(Mockito.any(Page.class), Mockito.eq(queueName))).thenReturn(page); |
||||
Map<String, Object> result = queueService.queryList(getLoginUser(),queueName,1,10); |
||||
logger.info(result.toString()); |
||||
PageInfo<Queue> pageInfo = (PageInfo<Queue>) result.get(Constants.DATA_LIST); |
||||
Assert.assertTrue(CollectionUtils.isNotEmpty(pageInfo.getLists())); |
||||
} |
||||
@Test |
||||
public void testCreateQueue(){ |
||||
|
||||
// queue is null
|
||||
Map<String, Object> result = queueService.createQueue(getLoginUser(),null,queueName); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.REQUEST_PARAMS_NOT_VALID_ERROR,result.get(Constants.STATUS)); |
||||
// queueName is null
|
||||
result = queueService.createQueue(getLoginUser(),queueName,null); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.REQUEST_PARAMS_NOT_VALID_ERROR,result.get(Constants.STATUS)); |
||||
// correct
|
||||
result = queueService.createQueue(getLoginUser(),queueName,queueName); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); |
||||
|
||||
} |
||||
@Test |
||||
public void testUpdateQueue(){ |
||||
|
||||
Mockito.when(queueMapper.selectById(1)).thenReturn(getQueue()); |
||||
Mockito.when(queueMapper.queryAllQueueList("test", null)).thenReturn(getQueueList()); |
||||
Mockito.when(queueMapper.queryAllQueueList(null, "test")).thenReturn(getQueueList()); |
||||
Mockito.when(userMapper.queryUserListByQueue(queueName)).thenReturn(getUserList()); |
||||
|
||||
// not exist
|
||||
Map<String, Object> result = queueService.updateQueue(getLoginUser(),0,"queue",queueName); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.QUEUE_NOT_EXIST.getCode(),((Status)result.get(Constants.STATUS)).getCode()); |
||||
//no need update
|
||||
result = queueService.updateQueue(getLoginUser(),1,queueName,queueName); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.NEED_NOT_UPDATE_QUEUE.getCode(),((Status)result.get(Constants.STATUS)).getCode()); |
||||
//queue exist
|
||||
result = queueService.updateQueue(getLoginUser(),1,"test",queueName); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.QUEUE_VALUE_EXIST.getCode(),((Status)result.get(Constants.STATUS)).getCode()); |
||||
// queueName exist
|
||||
result = queueService.updateQueue(getLoginUser(),1,"test1","test"); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.QUEUE_NAME_EXIST.getCode(),((Status)result.get(Constants.STATUS)).getCode()); |
||||
//success
|
||||
result = queueService.updateQueue(getLoginUser(),1,"test1","test1"); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.SUCCESS.getCode(),((Status)result.get(Constants.STATUS)).getCode()); |
||||
|
||||
} |
||||
@Test |
||||
public void testVerifyQueue(){ |
||||
|
||||
Mockito.when(queueMapper.queryAllQueueList(queueName, null)).thenReturn(getQueueList()); |
||||
Mockito.when(queueMapper.queryAllQueueList(null, queueName)).thenReturn(getQueueList()); |
||||
|
||||
//queue null
|
||||
Result result = queueService.verifyQueue(null,queueName); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(result.getCode().intValue(), Status.REQUEST_PARAMS_NOT_VALID_ERROR.getCode()); |
||||
|
||||
//queueName null
|
||||
result = queueService.verifyQueue(queueName,null); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(result.getCode().intValue(), Status.REQUEST_PARAMS_NOT_VALID_ERROR.getCode()); |
||||
|
||||
//exist queueName
|
||||
result = queueService.verifyQueue(queueName,queueName); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(result.getCode().intValue(), Status.QUEUE_NAME_EXIST.getCode()); |
||||
|
||||
//exist queue
|
||||
result = queueService.verifyQueue(queueName,"test"); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(result.getCode().intValue(), Status.QUEUE_VALUE_EXIST.getCode()); |
||||
|
||||
// success
|
||||
result = queueService.verifyQueue("test","test"); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(result.getCode().intValue(), Status.SUCCESS.getCode()); |
||||
|
||||
|
||||
} |
||||
/** |
||||
* create admin user |
||||
* @return |
||||
*/ |
||||
private User getLoginUser(){ |
||||
|
||||
User loginUser = new User(); |
||||
loginUser.setUserType(UserType.ADMIN_USER); |
||||
loginUser.setId(99999999); |
||||
return loginUser; |
||||
} |
||||
|
||||
private List<User> getUserList(){ |
||||
List<User> list = new ArrayList<>(); |
||||
list.add(getLoginUser()); |
||||
return list; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* get queue |
||||
* @return |
||||
*/ |
||||
private Queue getQueue(){ |
||||
Queue queue = new Queue(); |
||||
queue.setId(1); |
||||
queue.setQueue(queueName); |
||||
queue.setQueueName(queueName); |
||||
return queue; |
||||
} |
||||
|
||||
private List<Queue> getQueueList(){ |
||||
List<Queue> queueList = new ArrayList<>(); |
||||
queueList.add(getQueue()); |
||||
return queueList; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,173 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||
* (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.apache.dolphinscheduler.api.service; |
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
||||
import org.apache.dolphinscheduler.api.enums.Status; |
||||
import org.apache.dolphinscheduler.api.utils.PageInfo; |
||||
import org.apache.dolphinscheduler.common.Constants; |
||||
import org.apache.dolphinscheduler.common.enums.UserType; |
||||
import org.apache.dolphinscheduler.common.utils.CollectionUtils; |
||||
import org.apache.dolphinscheduler.dao.entity.ProcessInstance; |
||||
import org.apache.dolphinscheduler.dao.entity.User; |
||||
import org.apache.dolphinscheduler.dao.entity.WorkerGroup; |
||||
import org.apache.dolphinscheduler.dao.mapper.ProcessInstanceMapper; |
||||
import org.apache.dolphinscheduler.dao.mapper.WorkerGroupMapper; |
||||
import org.junit.Assert; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.mockito.InjectMocks; |
||||
import org.mockito.Mock; |
||||
import org.mockito.Mockito; |
||||
import org.mockito.junit.MockitoJUnitRunner; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
@RunWith(MockitoJUnitRunner.class) |
||||
public class WorkerGroupServiceTest { |
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(WorkerGroupServiceTest.class); |
||||
|
||||
@InjectMocks |
||||
private WorkerGroupService workerGroupService; |
||||
@Mock |
||||
private WorkerGroupMapper workerGroupMapper; |
||||
@Mock |
||||
private ProcessInstanceMapper processInstanceMapper; |
||||
|
||||
|
||||
private String groupName="groupName000001"; |
||||
|
||||
|
||||
/** |
||||
* create or update a worker group |
||||
*/ |
||||
@Test |
||||
public void testSaveWorkerGroup(){ |
||||
|
||||
User user = new User(); |
||||
// general user add
|
||||
user.setUserType(UserType.GENERAL_USER); |
||||
Map<String, Object> result = workerGroupService.saveWorkerGroup(user, 0, groupName, "127.0.0.1"); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals( Status.USER_NO_OPERATION_PERM.getMsg(),(String) result.get(Constants.MSG)); |
||||
|
||||
//success
|
||||
user.setUserType(UserType.ADMIN_USER); |
||||
result = workerGroupService.saveWorkerGroup(user, 0, groupName, "127.0.0.1"); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.SUCCESS.getMsg(),(String)result.get(Constants.MSG)); |
||||
// group name exist
|
||||
Mockito.when(workerGroupMapper.selectById(2)).thenReturn(getWorkerGroup(2)); |
||||
Mockito.when(workerGroupMapper.queryWorkerGroupByName(groupName)).thenReturn(getList()); |
||||
result = workerGroupService.saveWorkerGroup(user, 2, groupName, "127.0.0.1"); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.NAME_EXIST,result.get(Constants.STATUS)); |
||||
|
||||
} |
||||
|
||||
/** |
||||
* query worker group paging |
||||
*/ |
||||
@Test |
||||
public void testQueryAllGroupPaging(){ |
||||
|
||||
User user = new User(); |
||||
// general user add
|
||||
user.setUserType(UserType.GENERAL_USER); |
||||
Map<String, Object> result = workerGroupService.queryAllGroupPaging(user, 1, 10, groupName); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals((String) result.get(Constants.MSG), Status.USER_NO_OPERATION_PERM.getMsg()); |
||||
//success
|
||||
user.setUserType(UserType.ADMIN_USER); |
||||
Page<WorkerGroup> page = new Page<>(1,10); |
||||
page.setRecords(getList()); |
||||
page.setSize(1L); |
||||
Mockito.when(workerGroupMapper.queryListPaging(Mockito.any(Page.class), Mockito.eq(groupName))).thenReturn(page); |
||||
result = workerGroupService.queryAllGroupPaging(user, 1, 10, groupName); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.SUCCESS.getMsg(),(String)result.get(Constants.MSG)); |
||||
PageInfo<WorkerGroup> pageInfo = (PageInfo<WorkerGroup>) result.get(Constants.DATA_LIST); |
||||
Assert.assertTrue(CollectionUtils.isNotEmpty(pageInfo.getLists())); |
||||
} |
||||
|
||||
/** |
||||
* delete group by id |
||||
*/ |
||||
@Test |
||||
public void testDeleteWorkerGroupById(){ |
||||
|
||||
//DELETE_WORKER_GROUP_BY_ID_FAIL
|
||||
Mockito.when(processInstanceMapper.queryByWorkerGroupIdAndStatus(1, Constants.NOT_TERMINATED_STATES)).thenReturn(getProcessInstanceList()); |
||||
Map<String, Object> result = workerGroupService.deleteWorkerGroupById(1); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.DELETE_WORKER_GROUP_BY_ID_FAIL.getCode(),((Status) result.get(Constants.STATUS)).getCode()); |
||||
|
||||
//correct
|
||||
result = workerGroupService.deleteWorkerGroupById(2); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.SUCCESS.getMsg(),(String)result.get(Constants.MSG)); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
public void testQueryAllGroup(){ |
||||
Mockito.when(workerGroupMapper.queryAllWorkerGroup()).thenReturn(getList()); |
||||
Map<String, Object> result = workerGroupService.queryAllGroup(); |
||||
logger.info(result.toString()); |
||||
Assert.assertEquals(Status.SUCCESS.getMsg(),(String)result.get(Constants.MSG)); |
||||
List<WorkerGroup> workerGroupList = (List<WorkerGroup>) result.get(Constants.DATA_LIST); |
||||
Assert.assertTrue(workerGroupList.size()>0); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* get processInstances |
||||
* @return |
||||
*/ |
||||
private List<ProcessInstance> getProcessInstanceList(){ |
||||
|
||||
List<ProcessInstance> processInstances = new ArrayList<>(); |
||||
processInstances.add(new ProcessInstance()); |
||||
return processInstances; |
||||
} |
||||
/** |
||||
* get Group |
||||
* @return |
||||
*/ |
||||
private WorkerGroup getWorkerGroup(int id){ |
||||
WorkerGroup workerGroup = new WorkerGroup(); |
||||
workerGroup.setName(groupName); |
||||
workerGroup.setId(id); |
||||
return workerGroup; |
||||
} |
||||
private WorkerGroup getWorkerGroup(){ |
||||
|
||||
return getWorkerGroup(1); |
||||
} |
||||
|
||||
private List<WorkerGroup> getList(){ |
||||
List<WorkerGroup> list = new ArrayList<>(); |
||||
list.add(getWorkerGroup()); |
||||
return list; |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue