mirror of
https://github.com/didi/KnowStreaming.git
synced 2025-12-24 11:52:08 +08:00
AuthorityServiceTest && SecurityServiceTest && TopicReportServiceTest && ClusterServiceTest && ZookeeperServiceTest单元测试
This commit is contained in:
@@ -0,0 +1,493 @@
|
||||
package com.xiaojukeji.kafka.manager.service.service;
|
||||
|
||||
import com.xiaojukeji.kafka.manager.common.bizenum.DBStatusEnum;
|
||||
import com.xiaojukeji.kafka.manager.common.entity.Result;
|
||||
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
|
||||
import com.xiaojukeji.kafka.manager.common.entity.ao.ClusterDetailDTO;
|
||||
import com.xiaojukeji.kafka.manager.common.entity.ao.cluster.ControllerPreferredCandidate;
|
||||
import com.xiaojukeji.kafka.manager.common.entity.pojo.*;
|
||||
import com.xiaojukeji.kafka.manager.common.entity.vo.normal.cluster.ClusterNameDTO;
|
||||
import com.xiaojukeji.kafka.manager.dao.ClusterMetricsDao;
|
||||
import com.xiaojukeji.kafka.manager.dao.ControllerDao;
|
||||
import com.xiaojukeji.kafka.manager.service.cache.LogicalClusterMetadataManager;
|
||||
import com.xiaojukeji.kafka.manager.service.cache.PhysicalClusterMetadataManager;
|
||||
import com.xiaojukeji.kafka.manager.service.config.BaseTest;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author xuguang
|
||||
* @Date 2021/12/8
|
||||
*/
|
||||
public class ClusterServiceTest extends BaseTest {
|
||||
|
||||
@Autowired
|
||||
@InjectMocks
|
||||
private ClusterService clusterService;
|
||||
|
||||
@Autowired
|
||||
private ClusterMetricsDao clusterMetricsDao;
|
||||
|
||||
@Autowired
|
||||
private ControllerDao controllerDao;
|
||||
|
||||
@Mock
|
||||
private RegionService regionService;
|
||||
|
||||
@Mock
|
||||
private LogicalClusterMetadataManager logicalClusterMetadataManager;
|
||||
|
||||
@Mock
|
||||
private PhysicalClusterMetadataManager physicalClusterMetadataManager;
|
||||
|
||||
@Mock
|
||||
private ZookeeperService zookeeperService;
|
||||
|
||||
@BeforeMethod
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
}
|
||||
|
||||
@DataProvider(name = "provideClusterDO")
|
||||
public static Object[][] provideClusterDO() {
|
||||
ClusterDO clusterDO = new ClusterDO();
|
||||
clusterDO.setId(3L);
|
||||
clusterDO.setClusterName("LogiKM_moduleTest");
|
||||
clusterDO.setZookeeper("10.190.46.198:2181,10.190.14.237:2181,10.190.50.65:2181/xg");
|
||||
clusterDO.setBootstrapServers("10.190.46.198:9093,10.190.14.237:9093,10.190.50.65:9093");
|
||||
clusterDO.setSecurityProperties("{ \t\"security.protocol\": \"SASL_PLAINTEXT\", \t\"sasl.mechanism\": \"PLAIN\", \t\"sasl.jaas.config\": \"org.apache.kafka.common.security.plain.PlainLoginModule required username=\\\"dkm_admin\\\" password=\\\"km_kMl4N8as1Kp0CCY\\\";\" }");
|
||||
clusterDO.setStatus(1);
|
||||
clusterDO.setGmtCreate(new Date());
|
||||
clusterDO.setGmtModify(new Date());
|
||||
return new Object[][] {{clusterDO}};
|
||||
}
|
||||
|
||||
@DataProvider(name = "provideClusterMetricsDO")
|
||||
public static Object[][] provideClusterMetricsDO() {
|
||||
ClusterMetricsDO clusterMetricsDO = new ClusterMetricsDO();
|
||||
clusterMetricsDO.setId(10L);
|
||||
clusterMetricsDO.setClusterId(1L);
|
||||
clusterMetricsDO.setMetrics("{\"PartitionNum\":52,\"BrokerNum\":0,\"CreateTime\":1638235221102,\"TopicNum\":2}");
|
||||
clusterMetricsDO.setGmtCreate(new Date());
|
||||
return new Object[][] {{clusterMetricsDO}};
|
||||
}
|
||||
|
||||
@DataProvider(name = "provideControllerDO")
|
||||
public static Object[][] provideControllerDO() {
|
||||
ControllerDO controllerDO = new ControllerDO();
|
||||
controllerDO.setClusterId(1L);
|
||||
controllerDO.setBrokerId(1);
|
||||
controllerDO.setHost("127.0.0.1");
|
||||
controllerDO.setTimestamp(0L);
|
||||
controllerDO.setVersion(1);
|
||||
return new Object[][] {{controllerDO}};
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideClusterDO", description = "测试新增物理集群")
|
||||
public void addNewTest(ClusterDO clusterDO) {
|
||||
// 测试新增物理集群成功
|
||||
addaddNew2SuccessTest(clusterDO);
|
||||
// 测试新增物理集群时键重复
|
||||
addaddNew2DuplicateKeyTest(clusterDO);
|
||||
// 测试新增物理集群时数据库插入失败
|
||||
addaddNew2MysqlErrorTest(clusterDO);
|
||||
// 测试新增物理集群时参数有误
|
||||
addNew2ParamIllegalTest(clusterDO);
|
||||
// 测试新增物理集群时zk无法连接
|
||||
addNew2ZookeeperConnectFailedTest(clusterDO);
|
||||
}
|
||||
|
||||
private void addNew2ParamIllegalTest(ClusterDO clusterDO) {
|
||||
ResultStatus result1 = clusterService.addNew(clusterDO, null);
|
||||
Assert.assertEquals(result1.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
|
||||
|
||||
ResultStatus result2 = clusterService.addNew(null, "admin");
|
||||
Assert.assertEquals(result2.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
|
||||
}
|
||||
|
||||
private void addNew2ZookeeperConnectFailedTest(ClusterDO clusterDO) {
|
||||
clusterDO.setZookeeper("xxx");
|
||||
ResultStatus result = clusterService.addNew(clusterDO, "admin");
|
||||
Assert.assertEquals(result.getCode(), ResultStatus.ZOOKEEPER_CONNECT_FAILED.getCode());
|
||||
}
|
||||
|
||||
private void addaddNew2SuccessTest(ClusterDO clusterDO) {
|
||||
ResultStatus result = clusterService.addNew(clusterDO, "admin");
|
||||
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
|
||||
}
|
||||
|
||||
public void addaddNew2DuplicateKeyTest(ClusterDO clusterDO) {
|
||||
|
||||
ResultStatus result = clusterService.addNew(clusterDO, "admin");
|
||||
Assert.assertEquals(result.getCode(), ResultStatus.RESOURCE_ALREADY_EXISTED.getCode());
|
||||
}
|
||||
|
||||
public void addaddNew2MysqlErrorTest(ClusterDO clusterDO) {
|
||||
// operateRecord数据库插入失败
|
||||
clusterDO.setClusterName(null);
|
||||
ResultStatus result = clusterService.addNew(clusterDO, "admin");
|
||||
Assert.assertEquals(result.getCode(), ResultStatus.MYSQL_ERROR.getCode());
|
||||
|
||||
// cluster数据库插入失败
|
||||
clusterDO.setClusterName("clusterTest");
|
||||
clusterDO.setBootstrapServers(null);
|
||||
ResultStatus result2 = clusterService.addNew(clusterDO, "admin");
|
||||
Assert.assertEquals(result2.getCode(), ResultStatus.MYSQL_ERROR.getCode());
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideClusterDO", description = "测试由id获取ClusterDO")
|
||||
public void getById(ClusterDO clusterDO) {
|
||||
// 测试由id获取ClusterDO时,返回null
|
||||
getById2NullTest();
|
||||
// 测试由id获取ClusterDO时,返回成功
|
||||
getById2SuccessTest(clusterDO);
|
||||
}
|
||||
|
||||
private void getById2NullTest() {
|
||||
ClusterDO clusterDO = clusterService.getById(null);
|
||||
Assert.assertNull(clusterDO);
|
||||
}
|
||||
|
||||
private void getById2SuccessTest(ClusterDO clusterDO) {
|
||||
clusterService.addNew(clusterDO, "admin");
|
||||
|
||||
ClusterDO result = clusterService.getById(clusterDO.getId());
|
||||
Assert.assertNotNull(result);
|
||||
Assert.assertEquals(result, clusterDO);
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideClusterDO", description = "测试修改物理集群")
|
||||
public void updateById(ClusterDO clusterDO) {
|
||||
// 测试修改物理集群时参数有误
|
||||
updateById2ParamIllegalTest(clusterDO);
|
||||
// 测试修改物理集群时,集群不存在
|
||||
updateById2ClusterNotExistTest(clusterDO);
|
||||
// 测试修改物理集群时,zk配置不能修改
|
||||
updateById2ChangeZookeeperForbiddenTest(clusterDO);
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideClusterDO", description = "测试修改物理集群时,mysqlError")
|
||||
public void updateById2mysqlErrorTest(ClusterDO clusterDO) {
|
||||
clusterService.addNew(clusterDO, "admin");
|
||||
|
||||
clusterDO.setBootstrapServers(null);
|
||||
ResultStatus result1 = clusterService.updateById(clusterDO, "admin");
|
||||
Assert.assertEquals(result1.getCode(), ResultStatus.MYSQL_ERROR.getCode());
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideClusterDO", description = "测试修改物理集群成功")
|
||||
public void updateById2SuccessTest(ClusterDO clusterDO) {
|
||||
clusterService.addNew(clusterDO, "admin");
|
||||
|
||||
clusterDO.setJmxProperties("jmx");
|
||||
ResultStatus result1 = clusterService.updateById(clusterDO, "admin");
|
||||
Assert.assertEquals(result1.getCode(), ResultStatus.SUCCESS.getCode());
|
||||
}
|
||||
|
||||
private void updateById2ParamIllegalTest(ClusterDO clusterDO) {
|
||||
ResultStatus result1 = clusterService.updateById(clusterDO, null);
|
||||
Assert.assertEquals(result1.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
|
||||
|
||||
ResultStatus result2 = clusterService.updateById(null, "admin");
|
||||
Assert.assertEquals(result2.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
|
||||
}
|
||||
|
||||
private void updateById2ClusterNotExistTest(ClusterDO clusterDO) {
|
||||
clusterDO.setId(100L);
|
||||
ResultStatus result1 = clusterService.updateById(clusterDO, "admin");
|
||||
Assert.assertEquals(result1.getCode(), ResultStatus.CLUSTER_NOT_EXIST.getCode());
|
||||
}
|
||||
|
||||
private void updateById2ChangeZookeeperForbiddenTest(ClusterDO clusterDO) {
|
||||
clusterDO.setZookeeper("zzz");
|
||||
clusterDO.setId(1L);
|
||||
ResultStatus result1 = clusterService.updateById(clusterDO, "admin");
|
||||
Assert.assertEquals(result1.getCode(), ResultStatus.CHANGE_ZOOKEEPER_FORBIDDEN.getCode());
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideClusterDO", description = "测试修改物理集群状态")
|
||||
public void modifyStatusTest(ClusterDO clusterDO) {
|
||||
// 测试修改物理集群状态时参数有误
|
||||
modifyStatus2ParamIllegalTest();
|
||||
// 测试修改物理集群状态时,集群不存在
|
||||
modifyStatus2ClusterNotExistTest();
|
||||
// 测试修改物理集群状态成功
|
||||
modifyStatus2SuccessTest(clusterDO);
|
||||
}
|
||||
|
||||
public void modifyStatus2ParamIllegalTest() {
|
||||
ResultStatus result1 = clusterService.modifyStatus(null, 0, "admin");
|
||||
Assert.assertEquals(result1.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
|
||||
|
||||
ResultStatus result2 = clusterService.modifyStatus(1L, null,"admin");
|
||||
Assert.assertEquals(result2.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
|
||||
}
|
||||
|
||||
public void modifyStatus2ClusterNotExistTest() {
|
||||
ResultStatus result1 = clusterService.modifyStatus(100L, 0, "admin");
|
||||
Assert.assertEquals(result1.getCode(), ResultStatus.CLUSTER_NOT_EXIST.getCode());
|
||||
}
|
||||
|
||||
public void modifyStatus2SuccessTest(ClusterDO clusterDO) {
|
||||
clusterService.addNew(clusterDO, "admin");
|
||||
|
||||
ResultStatus result1 = clusterService.modifyStatus(clusterDO.getId(), clusterDO.getStatus(), "admin");
|
||||
Assert.assertEquals(result1.getCode(), ResultStatus.SUCCESS.getCode());
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideClusterDO")
|
||||
public void listTest(ClusterDO clusterDO) {
|
||||
clusterService.addNew(clusterDO, "admin");
|
||||
|
||||
List<ClusterDO> list = clusterService.list();
|
||||
Assert.assertEquals(list.size(), 1);
|
||||
Assert.assertEquals(list.get(0), clusterDO);
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideClusterDO")
|
||||
public void listMapTest(ClusterDO clusterDO) {
|
||||
clusterService.addNew(clusterDO, "admin");
|
||||
|
||||
Map<Long, ClusterDO> longClusterDOMap = clusterService.listMap();
|
||||
Assert.assertEquals(longClusterDOMap.size(), 1);
|
||||
Assert.assertEquals(longClusterDOMap.get(clusterDO.getId()), clusterDO);
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideClusterDO")
|
||||
public void listAllTest(ClusterDO clusterDO) {
|
||||
clusterService.addNew(clusterDO, "admin");
|
||||
|
||||
List<ClusterDO> list = clusterService.listAll();
|
||||
list.forEach(System.out::println);
|
||||
|
||||
Assert.assertEquals(list.size(), 1);
|
||||
Assert.assertEquals(list.get(0), clusterDO);
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideClusterMetricsDO")
|
||||
public void getClusterMetricsFromDBTest(ClusterMetricsDO clusterMetricsDO) {
|
||||
clusterMetricsDao.batchAdd(Arrays.asList(clusterMetricsDO));
|
||||
|
||||
List<ClusterMetricsDO> clusterMetricsDOList = clusterService.getClusterMetricsFromDB(
|
||||
clusterMetricsDO.getClusterId(),
|
||||
new Date(0L), new Date()
|
||||
);
|
||||
|
||||
Assert.assertNotNull(clusterMetricsDOList);
|
||||
Assert.assertEquals(clusterMetricsDOList.size(), 1);
|
||||
Assert.assertTrue(clusterMetricsDOList.stream().allMatch(clusterMetricsDO1 ->
|
||||
clusterMetricsDO1.getMetrics().equals(clusterMetricsDO.getMetrics()) &&
|
||||
clusterMetricsDO1.getClusterId().equals(clusterMetricsDO.getClusterId())));
|
||||
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideControllerDO")
|
||||
public void getKafkaControllerHistoryTest(ControllerDO controllerDO) {
|
||||
controllerDao.insert(controllerDO);
|
||||
|
||||
List<ControllerDO> kafkaControllerHistory = clusterService.getKafkaControllerHistory(controllerDO.getClusterId());
|
||||
Assert.assertNotNull(kafkaControllerHistory);
|
||||
Assert.assertTrue(kafkaControllerHistory.stream()
|
||||
.filter(controllerDO1 -> controllerDO1.getTimestamp().equals(0L))
|
||||
.allMatch(controllerDO1 ->
|
||||
controllerDO1.getClusterId().equals(controllerDO.getClusterId()) &&
|
||||
controllerDO1.getBrokerId().equals(controllerDO.getBrokerId()) &&
|
||||
controllerDO1.getTimestamp().equals(controllerDO.getTimestamp()))
|
||||
);
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideClusterDO", description = "参数needDetail为false")
|
||||
public void getClusterDetailDTOListWithFalseNeedDetailTest(ClusterDO clusterDO) {
|
||||
clusterService.addNew(clusterDO, "admin");
|
||||
|
||||
String kafkaVersion = "2.7";
|
||||
when(physicalClusterMetadataManager.getKafkaVersionFromCache(Mockito.anyLong())).thenReturn(kafkaVersion);
|
||||
|
||||
List<ClusterDetailDTO> clusterDetailDTOList = clusterService.getClusterDetailDTOList(false);
|
||||
Assert.assertNotNull(clusterDetailDTOList);
|
||||
Assert.assertTrue(clusterDetailDTOList.stream().allMatch(clusterDetailDTO ->
|
||||
clusterDetailDTO.getBootstrapServers().equals(clusterDO.getBootstrapServers()) &&
|
||||
clusterDetailDTO.getZookeeper().equals(clusterDO.getZookeeper()) &&
|
||||
clusterDetailDTO.getKafkaVersion().equals(kafkaVersion)));
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideClusterDO", description = "参数needDetail为true")
|
||||
public void getClusterDetailDTOListWithTrueNeedDetailTest(ClusterDO clusterDO) {
|
||||
List<ClusterDetailDTO> clusterDetailDTOList = clusterService.getClusterDetailDTOList(true);
|
||||
Assert.assertNotNull(clusterDetailDTOList);
|
||||
Assert.assertTrue(clusterDetailDTOList.stream().allMatch(clusterDetailDTO ->
|
||||
clusterDetailDTO.getBootstrapServers().equals(clusterDO.getBootstrapServers()) &&
|
||||
clusterDetailDTO.getZookeeper().equals(clusterDO.getZookeeper()) &&
|
||||
clusterDetailDTO.getClusterName().equals("LogiKM_xg") &&
|
||||
clusterDetailDTO.getBrokerNum().equals(1)));
|
||||
}
|
||||
|
||||
@Test(description = "测试获取ClusterNameDTO时,无对应的逻辑集群")
|
||||
public void getClusterName2EmptyTest() {
|
||||
when(logicalClusterMetadataManager.getLogicalCluster(Mockito.anyLong())).thenReturn(null);
|
||||
ClusterNameDTO clusterName = clusterService.getClusterName(10L);
|
||||
Assert.assertEquals(clusterName.toString(), new ClusterNameDTO().toString());
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideClusterDO", description = "测试获取ClusterNameDTO成功")
|
||||
public void getClusterName2SuccessTest(ClusterDO clusterDO) {
|
||||
clusterService.addNew(clusterDO, "admin");
|
||||
|
||||
LogicalClusterDO logicalClusterDO = new LogicalClusterDO();
|
||||
logicalClusterDO.setIdentification("logical");
|
||||
logicalClusterDO.setClusterId(clusterDO.getId());
|
||||
logicalClusterDO.setId(1L);
|
||||
when(logicalClusterMetadataManager.getLogicalCluster(Mockito.anyLong())).thenReturn(logicalClusterDO);
|
||||
ClusterNameDTO clusterName = clusterService.getClusterName(logicalClusterDO.getId());
|
||||
Assert.assertEquals(clusterName.getLogicalClusterName(), logicalClusterDO.getName());
|
||||
Assert.assertEquals(clusterName.getLogicalClusterId(), logicalClusterDO.getId());
|
||||
Assert.assertEquals(clusterName.getPhysicalClusterId(), logicalClusterDO.getClusterId());
|
||||
Assert.assertEquals(clusterName.getPhysicalClusterName(), clusterDO.getClusterName());
|
||||
}
|
||||
|
||||
@Test(description = "测试删除集群时,该集群下还有region,禁止删除")
|
||||
public void deleteById2OperationForbiddenTest() {
|
||||
when(regionService.getByClusterId(Mockito.anyLong())).thenReturn(Arrays.asList(new RegionDO()));
|
||||
ResultStatus resultStatus = clusterService.deleteById(1L, "admin");
|
||||
Assert.assertEquals(resultStatus.getCode(), ResultStatus.OPERATION_FORBIDDEN.getCode());
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideClusterDO", description = "测试删除集群成功")
|
||||
public void deleteById2SuccessTest(ClusterDO clusterDO) {
|
||||
clusterService.addNew(clusterDO, "admin");
|
||||
|
||||
when(regionService.getByClusterId(Mockito.anyLong())).thenReturn(Collections.emptyList());
|
||||
ResultStatus resultStatus = clusterService.deleteById(clusterDO.getId(), "admin");
|
||||
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
|
||||
|
||||
}
|
||||
|
||||
@Test(description = "测试删除集群成功")
|
||||
public void deleteById2MysqlErrorTest() {
|
||||
when(regionService.getByClusterId(Mockito.anyLong())).thenReturn(Collections.emptyList());
|
||||
ResultStatus resultStatus = clusterService.deleteById(100L, "admin");
|
||||
Assert.assertEquals(resultStatus.getCode(), ResultStatus.MYSQL_ERROR.getCode());
|
||||
}
|
||||
|
||||
@Test(description = "测试从zk中获取被选举的broker")
|
||||
public void getControllerPreferredCandidatesTest() {
|
||||
// "测试从zk中获取被选举的broker失败"
|
||||
getControllerPreferredCandidates2FailedTest();
|
||||
// 测试从zk中获取被选举的broker为空
|
||||
getControllerPreferredCandidates2BrokersEmptyTest();
|
||||
// 测试从zk中获取被选举的broker的brokerMetadata为null
|
||||
getControllerPreferredCandidates2BrokerMetadataNullTest();
|
||||
// 测试从zk中获取被选举的broker成功
|
||||
getControllerPreferredCandidates2SuccessTest();
|
||||
}
|
||||
|
||||
private void getControllerPreferredCandidates2FailedTest() {
|
||||
when(zookeeperService.getControllerPreferredCandidates(Mockito.anyLong())).thenReturn(new Result<>(-1, "fail"));
|
||||
|
||||
Result<List<ControllerPreferredCandidate>> result = clusterService.getControllerPreferredCandidates(1L);
|
||||
Assert.assertTrue(result.getCode() != ResultStatus.SUCCESS.getCode());
|
||||
}
|
||||
|
||||
private void getControllerPreferredCandidates2BrokersEmptyTest() {
|
||||
when(zookeeperService.getControllerPreferredCandidates(Mockito.anyLong())).thenReturn(new Result<>(0, new ArrayList<>(), "fail"));
|
||||
|
||||
Result<List<ControllerPreferredCandidate>> result = clusterService.getControllerPreferredCandidates(1L);
|
||||
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
|
||||
Assert.assertTrue(result.getData().isEmpty());
|
||||
}
|
||||
|
||||
private void getControllerPreferredCandidates2BrokerMetadataNullTest() {
|
||||
when(zookeeperService.getControllerPreferredCandidates(Mockito.anyLong())).thenReturn(new Result<>(0, Arrays.asList(100), "fail"));
|
||||
|
||||
Result<List<ControllerPreferredCandidate>> result = clusterService.getControllerPreferredCandidates(1L);
|
||||
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
|
||||
Assert.assertEquals((int) result.getData().get(0).getStatus(), DBStatusEnum.DEAD.getStatus());
|
||||
}
|
||||
|
||||
private void getControllerPreferredCandidates2SuccessTest() {
|
||||
when(zookeeperService.getControllerPreferredCandidates(Mockito.anyLong())).thenReturn(new Result<>(0, Arrays.asList(2), "fail"));
|
||||
|
||||
Result<List<ControllerPreferredCandidate>> result = clusterService.getControllerPreferredCandidates(1L);
|
||||
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
|
||||
Assert.assertEquals((int) result.getData().get(0).getStatus(), DBStatusEnum.ALIVE.getStatus());
|
||||
}
|
||||
|
||||
@Test(description = "增加优先被选举为controller的broker")
|
||||
public void addControllerPreferredCandidatesTest() {
|
||||
// 增加优先被选举为controller的broker时参数错误
|
||||
addControllerPreferredCandidates2ParamIllegalTest();
|
||||
// 增加优先被选举为controller的broker时broker不存活
|
||||
addControllerPreferredCandidates2BrokerNotExistTest();
|
||||
// 增加优先被选举为controller的broker失败
|
||||
addControllerPreferredCandidates2FailedTest();
|
||||
// 增加优先被选举为controller的broker成功
|
||||
addControllerPreferredCandidates2SuccessTest();
|
||||
}
|
||||
|
||||
private void addControllerPreferredCandidates2ParamIllegalTest() {
|
||||
Result result1 = clusterService.addControllerPreferredCandidates(null, Arrays.asList(1));
|
||||
Assert.assertEquals(result1.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
|
||||
|
||||
Result result2 = clusterService.addControllerPreferredCandidates(1L, Collections.emptyList());
|
||||
Assert.assertEquals(result2.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
|
||||
}
|
||||
|
||||
private void addControllerPreferredCandidates2BrokerNotExistTest() {
|
||||
Result result1 = clusterService.addControllerPreferredCandidates(1L, Arrays.asList(100));
|
||||
Assert.assertEquals(result1.getCode(), ResultStatus.BROKER_NOT_EXIST.getCode());
|
||||
}
|
||||
|
||||
private void addControllerPreferredCandidates2FailedTest() {
|
||||
when(zookeeperService.addControllerPreferredCandidate(Mockito.anyLong(), Mockito.anyInt())).thenReturn(new Result(-1, "fail"));
|
||||
Result result1 = clusterService.addControllerPreferredCandidates(1L, Arrays.asList(2));
|
||||
Assert.assertNotEquals(result1.getCode(), ResultStatus.SUCCESS.getCode());
|
||||
}
|
||||
|
||||
private void addControllerPreferredCandidates2SuccessTest() {
|
||||
when(zookeeperService.addControllerPreferredCandidate(Mockito.anyLong(), Mockito.anyInt())).thenReturn(new Result(0, "fail"));
|
||||
Result result1 = clusterService.addControllerPreferredCandidates(1L, Arrays.asList(2));
|
||||
Assert.assertEquals(result1.getCode(), ResultStatus.SUCCESS.getCode());
|
||||
}
|
||||
|
||||
@Test(description = "删除优先被选举为controller的broker")
|
||||
public void deleteControllerPreferredCandidates() {
|
||||
// 删除优先被选举为controller的broker时参数错误
|
||||
deleteControllerPreferredCandidates2ParamIllegal();
|
||||
// 删除优先被选举为controller的broker失败
|
||||
deleteControllerPreferredCandidates2FailedTest();
|
||||
// 删除优先被选举为controller的broker成功
|
||||
deleteControllerPreferredCandidates2SuccessTest();
|
||||
}
|
||||
|
||||
private void deleteControllerPreferredCandidates2ParamIllegal() {
|
||||
Result result1 = clusterService.deleteControllerPreferredCandidates(null, Arrays.asList(1));
|
||||
Assert.assertEquals(result1.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
|
||||
|
||||
Result result2 = clusterService.deleteControllerPreferredCandidates(1L, Collections.emptyList());
|
||||
Assert.assertEquals(result2.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
|
||||
}
|
||||
|
||||
private void deleteControllerPreferredCandidates2FailedTest() {
|
||||
when(zookeeperService.deleteControllerPreferredCandidate(Mockito.anyLong(), Mockito.anyInt())).thenReturn(new Result(-1, "fail"));
|
||||
Result result1 = clusterService.deleteControllerPreferredCandidates(1L, Arrays.asList(2));
|
||||
Assert.assertNotEquals(result1.getCode(), ResultStatus.SUCCESS.getCode());
|
||||
}
|
||||
|
||||
private void deleteControllerPreferredCandidates2SuccessTest() {
|
||||
when(zookeeperService.deleteControllerPreferredCandidate(Mockito.anyLong(), Mockito.anyInt())).thenReturn(new Result(0, "fail"));
|
||||
Result result1 = clusterService.deleteControllerPreferredCandidates(1L, Arrays.asList(2));
|
||||
Assert.assertEquals(result1.getCode(), ResultStatus.SUCCESS.getCode());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,428 @@
|
||||
package com.xiaojukeji.kafka.manager.service.service;
|
||||
|
||||
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
|
||||
import com.xiaojukeji.kafka.manager.common.entity.ao.cluster.LogicalCluster;
|
||||
import com.xiaojukeji.kafka.manager.common.entity.ao.cluster.LogicalClusterMetrics;
|
||||
import com.xiaojukeji.kafka.manager.common.entity.pojo.LogicalClusterDO;
|
||||
import com.xiaojukeji.kafka.manager.common.entity.pojo.gateway.AppDO;
|
||||
import com.xiaojukeji.kafka.manager.common.zookeeper.znode.brokers.BrokerMetadata;
|
||||
import com.xiaojukeji.kafka.manager.common.zookeeper.znode.brokers.TopicMetadata;
|
||||
import com.xiaojukeji.kafka.manager.dao.LogicalClusterDao;
|
||||
import com.xiaojukeji.kafka.manager.service.cache.LogicalClusterMetadataManager;
|
||||
import com.xiaojukeji.kafka.manager.service.config.BaseTest;
|
||||
import com.xiaojukeji.kafka.manager.service.service.gateway.AppService;
|
||||
import org.apache.kafka.clients.Metadata;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author xuguang
|
||||
* @Date 2021/12/10
|
||||
*/
|
||||
public class LogicalClusterServiceTest extends BaseTest {
|
||||
|
||||
@Autowired
|
||||
@InjectMocks
|
||||
private LogicalClusterService logicalClusterService;
|
||||
|
||||
@Mock
|
||||
private LogicalClusterDao logicalClusterDao;
|
||||
|
||||
@Mock
|
||||
private LogicalClusterMetadataManager logicalClusterMetadataManager;
|
||||
|
||||
@Mock
|
||||
private AppService appService;
|
||||
|
||||
@BeforeMethod
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
}
|
||||
|
||||
@DataProvider(name = "provideLogicalClusterDO")
|
||||
public Object[][] provideLogicalClusterDO() {
|
||||
LogicalClusterDO logicalClusterDO = new LogicalClusterDO();
|
||||
logicalClusterDO.setId(100L);
|
||||
logicalClusterDO.setClusterId(1L);
|
||||
logicalClusterDO.setIdentification("moduleTestLogicalCluster");
|
||||
logicalClusterDO.setName("moduleTestLogicalCluster");
|
||||
logicalClusterDO.setMode(1);
|
||||
logicalClusterDO.setRegionList("2,3");
|
||||
logicalClusterDO.setAppId("moduleTest");
|
||||
logicalClusterDO.setGmtCreate(new Date());
|
||||
logicalClusterDO.setGmtModify(new Date());
|
||||
return new Object[][] {{logicalClusterDO}};
|
||||
}
|
||||
|
||||
private LogicalClusterDO getLogicalClusterDO() {
|
||||
LogicalClusterDO logicalClusterDO = new LogicalClusterDO();
|
||||
logicalClusterDO.setId(100L);
|
||||
logicalClusterDO.setClusterId(1L);
|
||||
logicalClusterDO.setIdentification("moduleTestLogicalCluster");
|
||||
logicalClusterDO.setName("moduleTestLogicalCluster");
|
||||
logicalClusterDO.setMode(0);
|
||||
logicalClusterDO.setRegionList("2,3");
|
||||
logicalClusterDO.setAppId("");
|
||||
logicalClusterDO.setGmtCreate(new Date());
|
||||
logicalClusterDO.setGmtModify(new Date());
|
||||
return logicalClusterDO;
|
||||
}
|
||||
|
||||
public AppDO getAppDO() {
|
||||
AppDO appDO = new AppDO();
|
||||
appDO.setId(4L);
|
||||
appDO.setAppId("moduleTest");
|
||||
appDO.setName("moduleTestApp");
|
||||
appDO.setPassword("moduleTestApp");
|
||||
appDO.setType(1);
|
||||
appDO.setApplicant("admin");
|
||||
appDO.setPrincipals("module");
|
||||
appDO.setDescription("moduleTestApp");
|
||||
appDO.setCreateTime(new Date(1638786493173L));
|
||||
appDO.setModifyTime(new Date(1638786493173L));
|
||||
return appDO;
|
||||
}
|
||||
|
||||
@Test(description = "创建逻辑集群时参数错误")
|
||||
public void createLogicalCluster2paramIllegalTest() {
|
||||
ResultStatus result = logicalClusterService.createLogicalCluster(null);
|
||||
Assert.assertEquals(result.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideLogicalClusterDO", description = "创建逻辑集群时,region已使用")
|
||||
public void createLogicalCluster2existRegionAlreadyInUseTest(LogicalClusterDO logicalClusterDO) {
|
||||
// 物理集群Id为null
|
||||
logicalClusterDO.setClusterId(null);
|
||||
ResultStatus result1 = logicalClusterService.createLogicalCluster(logicalClusterDO);
|
||||
Assert.assertEquals(result1.getCode(), ResultStatus.RESOURCE_ALREADY_USED.getCode());
|
||||
|
||||
// regionList为空情况
|
||||
logicalClusterDO.setClusterId(1L);
|
||||
logicalClusterDO.setRegionList("");
|
||||
ResultStatus result2 = logicalClusterService.createLogicalCluster(logicalClusterDO);
|
||||
Assert.assertEquals(result2.getCode(), ResultStatus.RESOURCE_ALREADY_USED.getCode());
|
||||
|
||||
// region已存在使用
|
||||
logicalClusterDao.insert(logicalClusterDO);
|
||||
ResultStatus result3 = logicalClusterService.createLogicalCluster(logicalClusterDO);
|
||||
Assert.assertEquals(result3.getCode(), ResultStatus.RESOURCE_ALREADY_USED.getCode());
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideLogicalClusterDO", description = "创建逻辑集群时,物理集群不存在")
|
||||
public void createLogicalCluster2PhysicalClusterNotExistTest(LogicalClusterDO logicalClusterDO) {
|
||||
Mockito.when(logicalClusterDao.insert(Mockito.any())).thenReturn(1);
|
||||
// 不存在该物理集群情况
|
||||
logicalClusterDO.setClusterId(100L);
|
||||
ResultStatus result1 = logicalClusterService.createLogicalCluster(logicalClusterDO);
|
||||
Assert.assertNotEquals(result1.getCode(), ResultStatus.RESOURCE_ALREADY_USED.getCode());
|
||||
Assert.assertEquals(result1.getCode(), ResultStatus.SUCCESS.getCode());
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideLogicalClusterDO", description = "创建逻辑集群时,不存在region已使用")
|
||||
public void createLogicalCluster2NotexistRegionAlreadyInUseTest(LogicalClusterDO logicalClusterDO) {
|
||||
Mockito.when(logicalClusterDao.insert(Mockito.any())).thenReturn(1);
|
||||
// region没有存在使用
|
||||
ResultStatus result2 = logicalClusterService.createLogicalCluster(logicalClusterDO);
|
||||
Assert.assertNotEquals(result2.getCode(), ResultStatus.RESOURCE_ALREADY_USED.getCode());
|
||||
Assert.assertEquals(result2.getCode(), ResultStatus.SUCCESS.getCode());
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideLogicalClusterDO", description = "创建逻辑集群时,不存在region已使用")
|
||||
public void createLogicalCluster2DuplicateKeyTest(LogicalClusterDO logicalClusterDO) {
|
||||
Mockito.when(logicalClusterDao.insert(Mockito.any())).thenThrow(DuplicateKeyException.class);
|
||||
logicalClusterDO.setRegionList("100");
|
||||
ResultStatus result3 = logicalClusterService.createLogicalCluster(logicalClusterDO);
|
||||
Assert.assertEquals(result3.getCode(), ResultStatus.RESOURCE_ALREADY_EXISTED.getCode());
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideLogicalClusterDO", description = "创建逻辑集群成功")
|
||||
public void createLogicalCluster2SuccessTest(LogicalClusterDO logicalClusterDO) {
|
||||
Mockito.when(logicalClusterDao.insert(Mockito.any())).thenReturn(1);
|
||||
|
||||
ResultStatus result3 = logicalClusterService.createLogicalCluster(logicalClusterDO);
|
||||
Assert.assertEquals(result3.getCode(), ResultStatus.SUCCESS.getCode());
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideLogicalClusterDO", description = "通过物理集群ID查找")
|
||||
public void getByPhysicalClusterIdTest(LogicalClusterDO logicalClusterDO) {
|
||||
logicalClusterDO.setClusterId(2L);
|
||||
logicalClusterDao.insert(logicalClusterDO);
|
||||
List<LogicalClusterDO> result = logicalClusterService.getByPhysicalClusterId(logicalClusterDO.getClusterId());
|
||||
Assert.assertFalse(result.isEmpty());
|
||||
Assert.assertTrue(result.stream().allMatch(logicalClusterDO1 ->
|
||||
logicalClusterDO1.getClusterId().equals(logicalClusterDO.getClusterId()) &&
|
||||
logicalClusterDO1.getIdentification().equals(logicalClusterDO.getIdentification())));
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideLogicalClusterDO", description = "通过逻辑集群ID查找")
|
||||
public void getByIdTest(LogicalClusterDO logicalClusterDO) {
|
||||
LogicalClusterDO result = logicalClusterService.getById(7L);
|
||||
Assert.assertNotNull(result);
|
||||
Assert.assertEquals(result.getIdentification(), logicalClusterDO.getIdentification());
|
||||
}
|
||||
|
||||
@Test(description = "测试删除集群")
|
||||
public void deleteByIdTest() {
|
||||
// 删除集群成功
|
||||
deleteById2SuccessTest();
|
||||
// 删除集群时参数错误
|
||||
deleteById2paramIllegalTest();
|
||||
// 删除集群时无该集群
|
||||
deleteById2ResourceNotExistTest();
|
||||
// 删除集群时,mysqlError
|
||||
deleteById2MysqlErrorTest();
|
||||
}
|
||||
|
||||
private void deleteById2paramIllegalTest() {
|
||||
ResultStatus resultStatus = logicalClusterService.deleteById(null);
|
||||
Assert.assertEquals(resultStatus.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
|
||||
}
|
||||
|
||||
private void deleteById2ResourceNotExistTest() {
|
||||
Mockito.when(logicalClusterDao.deleteById(Mockito.anyLong())).thenReturn(-1);
|
||||
|
||||
ResultStatus resultStatus = logicalClusterService.deleteById(100L);
|
||||
Assert.assertEquals(resultStatus.getCode(), ResultStatus.RESOURCE_NOT_EXIST.getCode());
|
||||
}
|
||||
|
||||
private void deleteById2MysqlErrorTest() {
|
||||
Mockito.when(logicalClusterDao.deleteById(Mockito.anyLong())).thenThrow(RuntimeException.class);
|
||||
|
||||
ResultStatus resultStatus = logicalClusterService.deleteById(7L);
|
||||
Assert.assertEquals(resultStatus.getCode(), ResultStatus.MYSQL_ERROR.getCode());
|
||||
}
|
||||
|
||||
private void deleteById2SuccessTest() {
|
||||
Mockito.when(logicalClusterDao.deleteById(Mockito.anyLong())).thenReturn(1);
|
||||
ResultStatus resultStatus = logicalClusterService.deleteById(7L);
|
||||
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideLogicalClusterDO", description = "修改集群时参数错误")
|
||||
public void updateById2paramIllegalTest(LogicalClusterDO logicalClusterDO) {
|
||||
logicalClusterDO.setId(null);
|
||||
ResultStatus resultStatus = logicalClusterService.updateById(logicalClusterDO);
|
||||
Assert.assertEquals(resultStatus.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
|
||||
|
||||
logicalClusterDO = null;
|
||||
ResultStatus resultStatus2 = logicalClusterService.updateById(logicalClusterDO);
|
||||
Assert.assertEquals(resultStatus2.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideLogicalClusterDO", description = "修改集群时无对应逻辑集群")
|
||||
public void updateById2ResourceNotExistTest(LogicalClusterDO logicalClusterDO) {
|
||||
logicalClusterDO.setId(100L);
|
||||
ResultStatus resultStatus2 = logicalClusterService.updateById(logicalClusterDO);
|
||||
Assert.assertEquals(resultStatus2.getCode(), ResultStatus.RESOURCE_NOT_EXIST.getCode());
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideLogicalClusterDO", description = "修改集群时,region已在使用")
|
||||
public void updateById2existRegionAlreadyInUseTest(LogicalClusterDO logicalClusterDO) {
|
||||
Mockito.when(logicalClusterDao.getById(Mockito.anyLong())).thenReturn(logicalClusterDO);
|
||||
|
||||
// 物理集群Id为null
|
||||
logicalClusterDO.setClusterId(null);
|
||||
ResultStatus result1 = logicalClusterService.updateById(logicalClusterDO);
|
||||
Assert.assertEquals(result1.getCode(), ResultStatus.RESOURCE_ALREADY_USED.getCode());
|
||||
|
||||
// regionList为空情况
|
||||
logicalClusterDO.setClusterId(1L);
|
||||
logicalClusterDO.setRegionList("");
|
||||
ResultStatus result2 = logicalClusterService.updateById(logicalClusterDO);
|
||||
Assert.assertEquals(result2.getCode(), ResultStatus.RESOURCE_ALREADY_USED.getCode());
|
||||
|
||||
// region已存在使用
|
||||
logicalClusterDao.insert(logicalClusterDO);
|
||||
ResultStatus result3 = logicalClusterService.updateById(logicalClusterDO);
|
||||
Assert.assertEquals(result3.getCode(), ResultStatus.RESOURCE_ALREADY_USED.getCode());
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideLogicalClusterDO", description = "修改集群成功")
|
||||
public void updateById2SuccessTest(LogicalClusterDO logicalClusterDO) {
|
||||
Mockito.when(logicalClusterDao.updateById(Mockito.any())).thenReturn(1);
|
||||
Mockito.when(logicalClusterDao.getById(Mockito.anyLong())).thenReturn(logicalClusterDO);
|
||||
|
||||
ResultStatus result3 = logicalClusterService.updateById(logicalClusterDO);
|
||||
Assert.assertEquals(result3.getCode(), ResultStatus.SUCCESS.getCode());
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideLogicalClusterDO", description = "测试获取所有逻辑集群")
|
||||
public void listAllTest(LogicalClusterDO logicalClusterDO) {
|
||||
Mockito.when(logicalClusterDao.listAll()).thenReturn(Arrays.asList(logicalClusterDO));
|
||||
|
||||
List<LogicalClusterDO> logicalClusterDOS = logicalClusterService.listAll();
|
||||
Assert.assertFalse(logicalClusterDOS.isEmpty());
|
||||
Assert.assertTrue(logicalClusterDOS.stream().allMatch(logicalClusterDO1 ->
|
||||
logicalClusterDO1.getIdentification().equals(logicalClusterDO.getIdentification())));
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideLogicalClusterDO", description = "从缓存中获取所有的逻辑集群")
|
||||
public void getAllLogicalClusterTest(LogicalClusterDO logicalClusterDO) {
|
||||
// 从缓存中获取所有的逻辑集群为空
|
||||
getAllLogicalCluster2NullTest(logicalClusterDO);
|
||||
// 从缓存中获取所有的逻辑集群不为空
|
||||
getAllLogicalCluster2NotNullTest(logicalClusterDO);
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideLogicalClusterDO", description = "从缓存中获取所有的逻辑集群为空")
|
||||
private void getAllLogicalCluster2NullTest(LogicalClusterDO logicalClusterDO) {
|
||||
Mockito.when(logicalClusterMetadataManager.getLogicalClusterList()).thenReturn(Collections.emptyList());
|
||||
|
||||
List<LogicalCluster> allLogicalCluster = logicalClusterService.getAllLogicalCluster();
|
||||
Assert.assertNotNull(allLogicalCluster);
|
||||
Assert.assertTrue(allLogicalCluster.isEmpty());
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideLogicalClusterDO", description = "从缓存中获取所有的逻辑集群不为空")
|
||||
private void getAllLogicalCluster2NotNullTest(LogicalClusterDO logicalClusterDO) {
|
||||
Mockito.when(logicalClusterMetadataManager.getLogicalClusterList()).thenReturn(Arrays.asList(logicalClusterDO));
|
||||
|
||||
List<LogicalCluster> allLogicalCluster = logicalClusterService.getAllLogicalCluster();
|
||||
Assert.assertNotNull(allLogicalCluster);
|
||||
Assert.assertEquals(allLogicalCluster.get(0).getLogicalClusterIdentification(), logicalClusterDO.getIdentification());
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideLogicalClusterDO", description = "获取逻辑集群信息测试")
|
||||
public void getLogicalClusterTest(LogicalClusterDO logicalClusterDO) {
|
||||
// 获取逻辑集群信息失败
|
||||
getLogicalCluster2NullTest();
|
||||
// 测试获取逻辑集群成功
|
||||
getLogicalCluster2SuccessTest(logicalClusterDO);
|
||||
}
|
||||
|
||||
private void getLogicalCluster2NullTest() {
|
||||
LogicalCluster logicalCluster = logicalClusterService.getLogicalCluster(100L);
|
||||
Assert.assertNull(logicalCluster);
|
||||
}
|
||||
|
||||
private void getLogicalCluster2SuccessTest(LogicalClusterDO logicalClusterDO) {
|
||||
Mockito.when(logicalClusterMetadataManager.getLogicalCluster(Mockito.anyLong())).thenReturn(logicalClusterDO);
|
||||
|
||||
LogicalCluster logicalCluster = logicalClusterService.getLogicalCluster(logicalClusterDO.getId());
|
||||
Assert.assertNotNull(logicalCluster);
|
||||
Assert.assertEquals(logicalCluster.getLogicalClusterIdentification(), logicalClusterDO.getIdentification());
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideLogicalClusterDO", description = "获取逻辑集群信息测试")
|
||||
public void getLogicalClusterListByPrincipal(LogicalClusterDO logicalClusterDO) {
|
||||
// 责任人为空
|
||||
getLogicalClusterListByPrincipal2PrincipalIsBlankTest();
|
||||
// 获取的appDOList为空
|
||||
getLogicalClusterListByPrincipal2AppIsEmptyTest();
|
||||
// 完整流程
|
||||
getLogicalClusterListByPrincipal2Test(logicalClusterDO);
|
||||
}
|
||||
|
||||
private void getLogicalClusterListByPrincipal2PrincipalIsBlankTest() {
|
||||
Mockito.when(logicalClusterMetadataManager.getLogicalClusterList()).thenReturn(Collections.emptyList());
|
||||
|
||||
List<LogicalCluster> list = logicalClusterService.getLogicalClusterListByPrincipal("");
|
||||
Assert.assertNotNull(list);
|
||||
Assert.assertTrue(list.isEmpty());
|
||||
}
|
||||
|
||||
private void getLogicalClusterListByPrincipal2AppIsEmptyTest() {
|
||||
Mockito.when(logicalClusterMetadataManager.getLogicalClusterList()).thenReturn(Collections.emptyList());
|
||||
Mockito.when(appService.getByPrincipal(Mockito.anyString())).thenReturn(Collections.emptyList());
|
||||
|
||||
List<LogicalCluster> list = logicalClusterService.getLogicalClusterListByPrincipal("admin");
|
||||
Assert.assertNotNull(list);
|
||||
Assert.assertTrue(list.isEmpty());
|
||||
}
|
||||
|
||||
private void getLogicalClusterListByPrincipal2Test(LogicalClusterDO logicalClusterDO) {
|
||||
List<LogicalClusterDO> LogicalClusterDOList = new ArrayList<>();
|
||||
LogicalClusterDOList.add(logicalClusterDO);
|
||||
LogicalClusterDOList.add(getLogicalClusterDO());
|
||||
Mockito.when(logicalClusterMetadataManager.getLogicalClusterList()).thenReturn(LogicalClusterDOList);
|
||||
Mockito.when(appService.getByPrincipal(Mockito.anyString())).thenReturn(Arrays.asList(getAppDO()));
|
||||
|
||||
List<LogicalCluster> list = logicalClusterService.getLogicalClusterListByPrincipal("module");
|
||||
Assert.assertNotNull(list);
|
||||
Assert.assertEquals(list.size(), 2);
|
||||
Assert.assertTrue(list.stream().allMatch(logicalCluster ->
|
||||
logicalCluster.getLogicalClusterName().equals(logicalClusterDO.getName())));
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideLogicalClusterDO", description = "逻辑集群下Topic元信息测试")
|
||||
public void getTopicMetadatasTest(LogicalClusterDO logicalClusterDO) {
|
||||
// 传入的logicalClusterDO为空
|
||||
getTopicMetadatas2ParamisNullTest();
|
||||
// 获取逻辑集群下Topic元信息成功
|
||||
getTopicMetadatas2SuccessTest(logicalClusterDO);
|
||||
}
|
||||
|
||||
private void getTopicMetadatas2ParamisNullTest() {
|
||||
List<TopicMetadata> topicMetadatas = logicalClusterService.getTopicMetadatas(null);
|
||||
Assert.assertTrue(topicMetadatas.isEmpty());
|
||||
}
|
||||
|
||||
private void getTopicMetadatas2SuccessTest(LogicalClusterDO logicalClusterDO) {
|
||||
Set<String> set = new HashSet<>();
|
||||
set.add("xgTest");
|
||||
set.add("topicTest");
|
||||
Mockito.when(logicalClusterMetadataManager.getTopicNameSet(Mockito.anyLong()))
|
||||
.thenReturn(set);
|
||||
|
||||
List<TopicMetadata> topicMetadatas = logicalClusterService.getTopicMetadatas(logicalClusterDO);
|
||||
Assert.assertFalse(topicMetadatas.isEmpty());
|
||||
Assert.assertTrue(topicMetadatas.stream().allMatch(topicMetadata ->
|
||||
topicMetadata.getTopic().equals("xgTest")));
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideLogicalClusterDO", description = "逻辑集群下broker元信息测试")
|
||||
public void getBrokerMetadatasTest(LogicalClusterDO logicalClusterDO) {
|
||||
// 传入的logicalClusterDO为空
|
||||
getBrokerMetadatas2ParamisNullTest();
|
||||
// 获取逻辑集群下broker元信息成功
|
||||
getTopicBroker2SuccessTest(logicalClusterDO);
|
||||
}
|
||||
|
||||
private void getBrokerMetadatas2ParamisNullTest() {
|
||||
List<BrokerMetadata> brokerMetadatas = logicalClusterService.getBrokerMetadatas(null);
|
||||
Assert.assertTrue(brokerMetadatas.isEmpty());
|
||||
}
|
||||
|
||||
private void getTopicBroker2SuccessTest(LogicalClusterDO logicalClusterDO) {
|
||||
Set<Integer> set = new HashSet<>();
|
||||
set.add(1);
|
||||
set.add(111);
|
||||
Mockito.when(logicalClusterMetadataManager.getBrokerIdSet(Mockito.anyLong()))
|
||||
.thenReturn(set);
|
||||
|
||||
List<BrokerMetadata> brokerMetadatas = logicalClusterService.getBrokerMetadatas(logicalClusterDO);
|
||||
Assert.assertFalse(brokerMetadatas.isEmpty());
|
||||
Assert.assertTrue(brokerMetadatas.stream().allMatch(brokerMetadata ->
|
||||
brokerMetadata.getBrokerId() == 1));
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideLogicalClusterDO", description = "获取逻辑集群流量测试")
|
||||
public void getLogicalClusterMetricsFromDBTest(LogicalClusterDO logicalClusterDO) {
|
||||
Set<Integer> set = new HashSet<>();
|
||||
set.add(1);
|
||||
set.add(111);
|
||||
Mockito.when(logicalClusterMetadataManager.getBrokerIdSet(Mockito.anyLong()))
|
||||
.thenReturn(set);
|
||||
|
||||
long startTime = 1639360565000L;
|
||||
long endTime = 1639407365000L;
|
||||
List<LogicalClusterMetrics> list = logicalClusterService.getLogicalClusterMetricsFromDB(
|
||||
logicalClusterDO, new Date(startTime), new Date(endTime));
|
||||
Assert.assertFalse(list.isEmpty());
|
||||
Assert.assertTrue(list.stream().allMatch(logicalClusterMetrics ->
|
||||
logicalClusterMetrics.getGmtCreate().compareTo(startTime) > 0 &&
|
||||
logicalClusterMetrics.getGmtCreate().compareTo(endTime) < 0));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,235 @@
|
||||
package com.xiaojukeji.kafka.manager.service.service;
|
||||
|
||||
import com.xiaojukeji.kafka.manager.common.entity.Result;
|
||||
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
|
||||
import com.xiaojukeji.kafka.manager.common.exception.ConfigException;
|
||||
import com.xiaojukeji.kafka.manager.common.zookeeper.ZkConfigImpl;
|
||||
import com.xiaojukeji.kafka.manager.common.zookeeper.ZkPathUtil;
|
||||
import com.xiaojukeji.kafka.manager.common.zookeeper.znode.didi.TopicJmxSwitch;
|
||||
import com.xiaojukeji.kafka.manager.service.config.BaseTest;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author xuguang
|
||||
* @Date 2021/12/9
|
||||
*/
|
||||
public class ZookeeperServiceTest extends BaseTest {
|
||||
|
||||
@Autowired
|
||||
private ZookeeperService zookeeperService;
|
||||
|
||||
private final static String ZOOKEEPER_ADDRESS = "10.190.46.198:2181,10.190.14.237:2181,10.190.50.65:2181/xg";
|
||||
|
||||
@DataProvider(name = "extendsAndCandidatesZnodeExist")
|
||||
public static Object[][] extendsAndCandidatesZnodeExist() {
|
||||
// zk中 config下extends节点是否存在,extends节点下candidates节点是否存在
|
||||
return new Object[][] {{false, false}, {false, true}, {true, false}, {true, true}};
|
||||
}
|
||||
|
||||
@BeforeMethod
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
}
|
||||
|
||||
@Test(description = "开启JMX参数测试")
|
||||
public void openTopicJmxTest() {
|
||||
// 开启JMX参数有误
|
||||
openTopicJmx2ParamIllegalTest();
|
||||
// 开启JMX, 无topic
|
||||
openTopicJmx2TopicNotExistTest();
|
||||
// 开启JMX成功
|
||||
openTopicJmx2SuccessTest();
|
||||
}
|
||||
|
||||
private void openTopicJmx2ParamIllegalTest() {
|
||||
Result result1 = zookeeperService.openTopicJmx(null, "xgTest", null);
|
||||
Assert.assertEquals(result1.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
|
||||
|
||||
Result result2 = zookeeperService.openTopicJmx(1L, null, null);
|
||||
Assert.assertEquals(result2.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
|
||||
}
|
||||
|
||||
private void openTopicJmx2TopicNotExistTest() {
|
||||
Result result1 = zookeeperService.openTopicJmx(1L, "xgTestxxx",
|
||||
new TopicJmxSwitch(true, true, true));
|
||||
Assert.assertEquals(result1.getCode(), ResultStatus.TOPIC_NOT_EXIST.getCode());
|
||||
}
|
||||
|
||||
private void openTopicJmx2SuccessTest() {
|
||||
Result result1 = zookeeperService.openTopicJmx(1L, "xgTest",
|
||||
new TopicJmxSwitch(true, true, true));
|
||||
Assert.assertEquals(result1.getCode(), ResultStatus.SUCCESS.getCode());
|
||||
}
|
||||
|
||||
@Test(description = "获取优先被选举为controller的broker")
|
||||
public void getControllerPreferredCandidatesTest() throws ConfigException {
|
||||
// 获取优先被选举为controller的broker时参数错误
|
||||
getControllerPreferredCandidates2ParamIllegalTest();
|
||||
// 获取优先被选举为controller的broker时参数错误
|
||||
getControllerPreferredCandidates2ZookeeperConnectFailedTest();
|
||||
// 获取优先被选举为controller的broker时, zk路径不存在
|
||||
getControllerPreferredCandidates2NoZkRootTest();
|
||||
// 获取优先被选举为controller的broker时,broker为空
|
||||
getControllerPreferredCandidates2BrokerEmptyTest();
|
||||
// 获取优先被选举为controller的broker成功
|
||||
getControllerPreferredCandidates2SuccessTest();
|
||||
}
|
||||
|
||||
private void getControllerPreferredCandidates2ParamIllegalTest() {
|
||||
Result<List<Integer>> brokerIds = zookeeperService.getControllerPreferredCandidates(null);
|
||||
Assert.assertEquals(brokerIds.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
|
||||
}
|
||||
|
||||
private void getControllerPreferredCandidates2ZookeeperConnectFailedTest() {
|
||||
Result<List<Integer>> brokerIds = zookeeperService.getControllerPreferredCandidates(100L);
|
||||
Assert.assertEquals(brokerIds.getCode(), ResultStatus.ZOOKEEPER_CONNECT_FAILED.getCode());
|
||||
}
|
||||
|
||||
private void getControllerPreferredCandidates2NoZkRootTest() {
|
||||
Result<List<Integer>> brokerIds = zookeeperService.getControllerPreferredCandidates(1L);
|
||||
Assert.assertEquals(brokerIds.getCode(), ResultStatus.SUCCESS.getCode());
|
||||
Assert.assertTrue(brokerIds.getData().isEmpty());
|
||||
}
|
||||
|
||||
private void getControllerPreferredCandidates2BrokerEmptyTest() throws ConfigException {
|
||||
ZkConfigImpl zkConfig = new ZkConfigImpl(ZOOKEEPER_ADDRESS);
|
||||
zkConfig.setOrCreatePersistentNodeStat(ZkPathUtil.D_CONFIG_EXTENSION_ROOT_NODE, "");
|
||||
zkConfig.setOrCreatePersistentNodeStat(ZkPathUtil.D_CONTROLLER_CANDIDATES, "");
|
||||
|
||||
Result<List<Integer>> brokerIds = zookeeperService.getControllerPreferredCandidates(1L);
|
||||
Assert.assertEquals(brokerIds.getCode(), ResultStatus.SUCCESS.getCode());
|
||||
Assert.assertTrue(brokerIds.getData().isEmpty());
|
||||
|
||||
zkConfig.delete(ZkPathUtil.D_CONTROLLER_CANDIDATES);
|
||||
zkConfig.delete(ZkPathUtil.D_CONFIG_EXTENSION_ROOT_NODE);
|
||||
zkConfig.close();
|
||||
}
|
||||
|
||||
private void getControllerPreferredCandidates2SuccessTest() throws ConfigException {
|
||||
ZkConfigImpl zkConfig = new ZkConfigImpl(ZOOKEEPER_ADDRESS);
|
||||
zkConfig.setOrCreatePersistentNodeStat(ZkPathUtil.D_CONFIG_EXTENSION_ROOT_NODE, "");
|
||||
zkConfig.setOrCreatePersistentNodeStat(ZkPathUtil.D_CONTROLLER_CANDIDATES, "");
|
||||
zkConfig.setOrCreatePersistentNodeStat(ZkPathUtil.D_CONTROLLER_CANDIDATES + "/1", "");
|
||||
|
||||
Result<List<Integer>> brokerIds = zookeeperService.getControllerPreferredCandidates(1L);
|
||||
Assert.assertEquals(brokerIds.getCode(), ResultStatus.SUCCESS.getCode());
|
||||
Assert.assertFalse(brokerIds.getData().isEmpty());
|
||||
Assert.assertEquals(brokerIds.getData().get(0), Integer.valueOf(1));
|
||||
|
||||
zkConfig.delete(ZkPathUtil.D_CONTROLLER_CANDIDATES + "/1");
|
||||
zkConfig.delete(ZkPathUtil.D_CONTROLLER_CANDIDATES);
|
||||
zkConfig.delete(ZkPathUtil.D_CONFIG_EXTENSION_ROOT_NODE);
|
||||
zkConfig.close();
|
||||
}
|
||||
|
||||
@Test(dataProvider = "extendsAndCandidatesZnodeExist", description = "增加优先被选举为controller的broker")
|
||||
public void addControllerPreferredCandidateTest(boolean extendsExist, boolean candidatesExist) throws ConfigException {
|
||||
// 增加优先被选举为controller的broker时参数错误
|
||||
addControllerPreferredCandidate2ParamIllegalTest();
|
||||
// 增加优先被选举为controller的broker时,zk无法连接
|
||||
addControllerPreferredCandidate2zkConnectFailedTest();
|
||||
// 增加优先被选举为controller的broker时,节点已经存在
|
||||
addControllerPreferredCandidate2zkExistTest();
|
||||
// 增加优先被选举为controller的broker成功,四种情况
|
||||
addControllerPreferredCandidate2SuccessTest(extendsExist, candidatesExist);
|
||||
}
|
||||
|
||||
private void addControllerPreferredCandidate2ParamIllegalTest() {
|
||||
Result result = zookeeperService.addControllerPreferredCandidate(null, 100);
|
||||
Assert.assertEquals(result.getCode(),ResultStatus.PARAM_ILLEGAL.getCode());
|
||||
}
|
||||
|
||||
private void addControllerPreferredCandidate2zkConnectFailedTest() {
|
||||
Result result = zookeeperService.addControllerPreferredCandidate(100L, 100);
|
||||
Assert.assertEquals(result.getCode(),ResultStatus.ZOOKEEPER_CONNECT_FAILED.getCode());
|
||||
}
|
||||
|
||||
private void addControllerPreferredCandidate2zkExistTest() throws ConfigException {
|
||||
ZkConfigImpl zkConfig = new ZkConfigImpl(ZOOKEEPER_ADDRESS);
|
||||
zkConfig.setOrCreatePersistentNodeStat(ZkPathUtil.D_CONFIG_EXTENSION_ROOT_NODE, "");
|
||||
zkConfig.setOrCreatePersistentNodeStat(ZkPathUtil.D_CONTROLLER_CANDIDATES, "");
|
||||
zkConfig.setOrCreatePersistentNodeStat(ZkPathUtil.D_CONTROLLER_CANDIDATES + "/1", "");
|
||||
|
||||
Result result = zookeeperService.addControllerPreferredCandidate(1L, 1);
|
||||
Assert.assertEquals(result.getCode(),ResultStatus.SUCCESS.getCode());
|
||||
|
||||
zkConfig.delete(ZkPathUtil.D_CONTROLLER_CANDIDATES + "/1");
|
||||
zkConfig.delete(ZkPathUtil.D_CONTROLLER_CANDIDATES);
|
||||
zkConfig.delete(ZkPathUtil.D_CONFIG_EXTENSION_ROOT_NODE);
|
||||
zkConfig.close();
|
||||
}
|
||||
|
||||
private void addControllerPreferredCandidate2SuccessTest(boolean extendsExist, boolean candidatesExist) throws ConfigException {
|
||||
ZkConfigImpl zkConfig = new ZkConfigImpl(ZOOKEEPER_ADDRESS);
|
||||
if (extendsExist) {
|
||||
zkConfig.setOrCreatePersistentNodeStat(ZkPathUtil.D_CONFIG_EXTENSION_ROOT_NODE, "");
|
||||
}
|
||||
if (extendsExist && candidatesExist) {
|
||||
zkConfig.setOrCreatePersistentNodeStat(ZkPathUtil.D_CONTROLLER_CANDIDATES, "");
|
||||
}
|
||||
|
||||
Result result = zookeeperService.addControllerPreferredCandidate(1L, 1);
|
||||
Assert.assertEquals(result.getCode(),ResultStatus.SUCCESS.getCode());
|
||||
|
||||
zkConfig.delete(ZkPathUtil.D_CONTROLLER_CANDIDATES + "/1");
|
||||
zkConfig.delete(ZkPathUtil.D_CONTROLLER_CANDIDATES);
|
||||
zkConfig.delete(ZkPathUtil.D_CONFIG_EXTENSION_ROOT_NODE);
|
||||
zkConfig.close();
|
||||
}
|
||||
|
||||
@Test(description = "减少优先被选举为controller的broker")
|
||||
public void deleteControllerPreferredCandidate() throws ConfigException {
|
||||
// 减少优先被选举为controller的broker时参数错误
|
||||
deleteControllerPreferredCandidate2ParamIllegalTest();
|
||||
// 减少优先被选举为controller的broker时,zk无法连接
|
||||
deleteControllerPreferredCandidate2zkConnectFailedTest();
|
||||
// 减少优先被选举为controller的broker时,节点已经存在
|
||||
addControllerPreferredCandidate2zkNodeNotExistTest();
|
||||
// 减少优先被选举为controller的broker成功
|
||||
addControllerPreferredCandidate2SuccessTest();
|
||||
}
|
||||
|
||||
private void deleteControllerPreferredCandidate2ParamIllegalTest() {
|
||||
Result result = zookeeperService.deleteControllerPreferredCandidate(null, 100);
|
||||
Assert.assertEquals(result.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
|
||||
}
|
||||
|
||||
private void deleteControllerPreferredCandidate2zkConnectFailedTest() {
|
||||
Result result = zookeeperService.addControllerPreferredCandidate(100L, 100);
|
||||
Assert.assertEquals(result.getCode(), ResultStatus.ZOOKEEPER_CONNECT_FAILED.getCode());
|
||||
}
|
||||
|
||||
private void addControllerPreferredCandidate2zkNodeNotExistTest() throws ConfigException {
|
||||
ZkConfigImpl zkConfig = new ZkConfigImpl(ZOOKEEPER_ADDRESS);
|
||||
zkConfig.setOrCreatePersistentNodeStat(ZkPathUtil.D_CONFIG_EXTENSION_ROOT_NODE, "");
|
||||
zkConfig.setOrCreatePersistentNodeStat(ZkPathUtil.D_CONTROLLER_CANDIDATES, "");
|
||||
|
||||
Result result = zookeeperService.deleteControllerPreferredCandidate(1L, 1);
|
||||
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
|
||||
|
||||
zkConfig.delete(ZkPathUtil.D_CONTROLLER_CANDIDATES);
|
||||
zkConfig.delete(ZkPathUtil.D_CONFIG_EXTENSION_ROOT_NODE);
|
||||
zkConfig.close();
|
||||
}
|
||||
|
||||
private void addControllerPreferredCandidate2SuccessTest() throws ConfigException {
|
||||
ZkConfigImpl zkConfig = new ZkConfigImpl(ZOOKEEPER_ADDRESS);
|
||||
zkConfig.setOrCreatePersistentNodeStat(ZkPathUtil.D_CONFIG_EXTENSION_ROOT_NODE, "");
|
||||
zkConfig.setOrCreatePersistentNodeStat(ZkPathUtil.D_CONTROLLER_CANDIDATES, "");
|
||||
zkConfig.setOrCreatePersistentNodeStat(ZkPathUtil.D_CONTROLLER_CANDIDATES + "/1", "");
|
||||
|
||||
Result result = zookeeperService.deleteControllerPreferredCandidate(1L, 1);
|
||||
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
|
||||
|
||||
zkConfig.delete(ZkPathUtil.D_CONTROLLER_CANDIDATES);
|
||||
zkConfig.delete(ZkPathUtil.D_CONFIG_EXTENSION_ROOT_NODE);
|
||||
zkConfig.close();
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,22 @@
|
||||
package com.xiaojukeji.kafka.manager.service.service.gateway;
|
||||
|
||||
import com.xiaojukeji.kafka.manager.common.bizenum.TopicAuthorityEnum;
|
||||
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
|
||||
import com.xiaojukeji.kafka.manager.common.entity.ao.gateway.TopicQuota;
|
||||
import com.xiaojukeji.kafka.manager.common.entity.pojo.gateway.AuthorityDO;
|
||||
import com.xiaojukeji.kafka.manager.service.config.BaseTest;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.testng.Assert;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.annotation.Rollback;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -26,17 +34,31 @@ public class AuthorityServiceTest extends BaseTest {
|
||||
public Object[][] provideAuthorityDO() {
|
||||
AuthorityDO authorityDO = new AuthorityDO();
|
||||
authorityDO.setId(4L);
|
||||
authorityDO.setAppId("testAppId");
|
||||
authorityDO.setAppId("appIdModuleTest");
|
||||
authorityDO.setClusterId(1L);
|
||||
authorityDO.setTopicName("moduleTest");
|
||||
authorityDO.setTopicName("topicModuleTest");
|
||||
authorityDO.setAccess(2);
|
||||
authorityDO.setCreateTime(new Date(1638786493173L));
|
||||
authorityDO.setModifyTime(new Date(1638786493173L));
|
||||
return new Object[][] {{authorityDO}};
|
||||
}
|
||||
|
||||
public TopicQuota getTopicQuota() {
|
||||
TopicQuota topicQuotaDO = new TopicQuota();
|
||||
topicQuotaDO.setAppId("testAppId");
|
||||
topicQuotaDO.setClusterId(1L);
|
||||
topicQuotaDO.setTopicName("moduleTest");
|
||||
topicQuotaDO.setProduceQuota(100000L);
|
||||
topicQuotaDO.setConsumeQuota(100000L);
|
||||
return topicQuotaDO;
|
||||
}
|
||||
|
||||
@BeforeMethod
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideAuthorityDO")
|
||||
@Rollback(value = false)
|
||||
public void addAuthorityTest(AuthorityDO authorityDO) {
|
||||
// 测试新增权限对象
|
||||
addNewAuthority(authorityDO);
|
||||
@@ -46,40 +68,37 @@ public class AuthorityServiceTest extends BaseTest {
|
||||
addNewAuthorityAccessTest(authorityDO);
|
||||
// 测试新增权限失败
|
||||
addNewAuthority2Failure();
|
||||
|
||||
}
|
||||
|
||||
private void addNewAuthority(AuthorityDO authorityDO) {
|
||||
int result = authorityService.addAuthority(authorityDO);
|
||||
Assert.assertEquals( 1, result);
|
||||
Assert.assertEquals(result, 1);
|
||||
}
|
||||
|
||||
private void newAccessEqualOldAccessTest(AuthorityDO authorityDO) {
|
||||
int result = authorityService.addAuthority(authorityDO);
|
||||
Assert.assertEquals( 0, result);
|
||||
Assert.assertEquals(result, 0);
|
||||
}
|
||||
|
||||
private void addNewAuthorityAccessTest(AuthorityDO authorityDO) {
|
||||
authorityDO.setAccess(3);
|
||||
int result = authorityService.addAuthority(authorityDO);
|
||||
Assert.assertEquals( 1, result);
|
||||
Assert.assertEquals(result, 1);
|
||||
}
|
||||
|
||||
private void addNewAuthority2Failure() {
|
||||
int result = authorityService.addAuthority(new AuthorityDO());
|
||||
Assert.assertEquals( 0, result);
|
||||
Assert.assertEquals(result, 0);
|
||||
}
|
||||
|
||||
public void deleteSpecifiedAccess() {
|
||||
@Test(dataProvider = "provideAuthorityDO", description = "测试删除权限对象")
|
||||
public void deleteSpecifiedAccess(AuthorityDO authorityDO) {
|
||||
// 测试删除权限对象时无该对象
|
||||
deleteSpecifiedAccess2AuthorityNotExist();
|
||||
|
||||
// 测试删除权限对象时参数错误, 传入的access为3,数据库中为2
|
||||
deleteSpecifiedAccess2ParamIllegal();
|
||||
|
||||
// 测试删除权限对象成功,传入的access为3,数据库中为3
|
||||
deleteSpecifiedAccess2Success();
|
||||
|
||||
// 测试删除权限对象时参数错误
|
||||
deleteSpecifiedAccess2ParamIllegal(authorityDO);
|
||||
// 测试删除权限对象成功
|
||||
deleteSpecifiedAccess2Success(authorityDO);
|
||||
}
|
||||
|
||||
private void deleteSpecifiedAccess2AuthorityNotExist() {
|
||||
@@ -87,37 +106,57 @@ public class AuthorityServiceTest extends BaseTest {
|
||||
Assert.assertEquals(ResultStatus.AUTHORITY_NOT_EXIST.getCode(), result.getCode());
|
||||
}
|
||||
|
||||
private void deleteSpecifiedAccess2ParamIllegal() {
|
||||
ResultStatus result = authorityService.deleteSpecifiedAccess("dkm_admin", 1L, "xgTest", 3, "admin");
|
||||
Assert.assertEquals(ResultStatus.PARAM_ILLEGAL.getCode(), result.getCode());
|
||||
private void deleteSpecifiedAccess2ParamIllegal(AuthorityDO authorityDO) {
|
||||
authorityService.addAuthority(authorityDO);
|
||||
|
||||
ResultStatus result = authorityService.deleteSpecifiedAccess(
|
||||
authorityDO.getAppId(),
|
||||
authorityDO.getClusterId(),
|
||||
authorityDO.getTopicName(),
|
||||
3, "admin"
|
||||
);
|
||||
Assert.assertEquals(result.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
|
||||
}
|
||||
|
||||
private void deleteSpecifiedAccess2Success() {
|
||||
ResultStatus result = authorityService.deleteSpecifiedAccess("dkm_admin", 1L, "xgTest", 3, "admin");
|
||||
private void deleteSpecifiedAccess2Success(AuthorityDO authorityDO) {
|
||||
authorityDO.setAccess(3);
|
||||
authorityDO.setAppId("sss");
|
||||
authorityService.addAuthority(authorityDO);
|
||||
|
||||
ResultStatus result = authorityService.deleteSpecifiedAccess(
|
||||
authorityDO.getAppId(),
|
||||
authorityDO.getClusterId(),
|
||||
authorityDO.getTopicName(),
|
||||
3, "admin"
|
||||
);
|
||||
Assert.assertEquals(ResultStatus.SUCCESS.getCode(), result.getCode());
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideAuthorityDO")
|
||||
@Test(dataProvider = "provideAuthorityDO", description = "测试查询")
|
||||
public void getAuthorityTest(AuthorityDO authorityDO) {
|
||||
// 测试查询成功
|
||||
getAuthority2SuccessTest(authorityDO);
|
||||
// 测试查询为null
|
||||
getAuthority2NullTest(authorityDO);
|
||||
|
||||
getAuthority2NullTest();
|
||||
}
|
||||
|
||||
private void getAuthority2SuccessTest(AuthorityDO authorityDO) {
|
||||
AuthorityDO result = authorityService.getAuthority(1L, "moduleTest", "testAppId");
|
||||
Assert.assertEquals(result.toString(), authorityDO.toString());
|
||||
authorityService.addAuthority(authorityDO);
|
||||
|
||||
AuthorityDO result = authorityService.getAuthority(authorityDO.getClusterId(), authorityDO.getTopicName(), authorityDO.getAppId());
|
||||
Assert.assertEquals(result.getClusterId(), authorityDO.getClusterId());
|
||||
Assert.assertEquals(result.getAppId(), authorityDO.getAppId());
|
||||
Assert.assertEquals(result.getTopicName(), authorityDO.getTopicName());
|
||||
Assert.assertEquals(result.getAccess(), authorityDO.getAccess());
|
||||
}
|
||||
|
||||
private void getAuthority2NullTest(AuthorityDO authorityDO) {
|
||||
private void getAuthority2NullTest() {
|
||||
AuthorityDO result = authorityService.getAuthority(10L, "moduleTest", "testAppId");
|
||||
Assert.assertNull(result);
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideAuthorityDO")
|
||||
public void getAuthorityByTopic(AuthorityDO authorityDO) {
|
||||
@Test(dataProvider = "provideAuthorityDO", description = "测试查询")
|
||||
public void getAuthorityByTopicTest(AuthorityDO authorityDO) {
|
||||
// 测试查询成功
|
||||
getAuthorityByTopic2SuccessTest(authorityDO);
|
||||
// 测试查询为null
|
||||
@@ -125,29 +164,36 @@ public class AuthorityServiceTest extends BaseTest {
|
||||
}
|
||||
|
||||
private void getAuthorityByTopic2SuccessTest(AuthorityDO authorityDO) {
|
||||
List<AuthorityDO> result = authorityService.getAuthorityByTopic(1L, "moduleTest");
|
||||
Assert.assertEquals(result.size(), 1);
|
||||
Assert.assertEquals(result.get(0).toString(), authorityDO.toString());
|
||||
authorityService.addAuthority(authorityDO);
|
||||
|
||||
List<AuthorityDO> result = authorityService.getAuthorityByTopic(authorityDO.getClusterId(), authorityDO.getTopicName());
|
||||
Assert.assertNotNull(result);
|
||||
Assert.assertTrue(result.stream()
|
||||
.allMatch(authorityDO1 -> authorityDO1.getTopicName().equals(authorityDO.getTopicName()) &&
|
||||
authorityDO1.getClusterId().equals(authorityDO.getClusterId())));
|
||||
}
|
||||
|
||||
private void getAuthorityByTopic2NullTest() {
|
||||
List<AuthorityDO> result = authorityService.getAuthorityByTopic(10L, "moduleTest");
|
||||
List<AuthorityDO> result = authorityService.getAuthorityByTopic(100L, "moduleTestxxx");
|
||||
Assert.assertTrue(result.isEmpty());
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideAuthorityDO")
|
||||
@Test(dataProvider = "provideAuthorityDO", description = "测试查询")
|
||||
public void getAuthorityByAppIdTest(AuthorityDO authorityDO) {
|
||||
// 测试查询成功
|
||||
getAuthorityByAppId2SuccessTest(authorityDO);
|
||||
|
||||
// 测试查询为null
|
||||
getAuthorityByAppId2NullTest();
|
||||
}
|
||||
|
||||
private void getAuthorityByAppId2SuccessTest(AuthorityDO authorityDO) {
|
||||
List<AuthorityDO> result = authorityService.getAuthority("testAppId");
|
||||
Assert.assertEquals(result.size(), 1);
|
||||
Assert.assertEquals(result.get(0).toString(), authorityDO.toString());
|
||||
authorityService.addAuthority(authorityDO);
|
||||
|
||||
List<AuthorityDO> result = authorityService.getAuthority(authorityDO.getAppId());
|
||||
Assert.assertNotNull(result);
|
||||
Assert.assertTrue(result.stream().
|
||||
allMatch(authorityDO1 -> authorityDO1.getAppId().equals(authorityDO.getAppId()) &&
|
||||
!authorityDO1.getAccess().equals(TopicAuthorityEnum.DENY.getCode())));
|
||||
}
|
||||
|
||||
private void getAuthorityByAppId2NullTest() {
|
||||
@@ -155,42 +201,58 @@ public class AuthorityServiceTest extends BaseTest {
|
||||
Assert.assertTrue(result.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void listAllTest() {
|
||||
@Test(dataProvider = "provideAuthorityDO")
|
||||
public void listAllTest(AuthorityDO authorityDO) {
|
||||
authorityService.addAuthority(authorityDO);
|
||||
|
||||
List<AuthorityDO> result = authorityService.listAll();
|
||||
Assert.assertEquals(result.size(), 2);
|
||||
Assert.assertEquals(result.size(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(dataProvider = "provideAuthorityDO", description = "添加权限和quota")
|
||||
public void addAuthorityAndQuotaTest(AuthorityDO authorityDO) {
|
||||
|
||||
// 添加权限和quota成功
|
||||
addAuthorityAndQuota2SuccessTest(authorityDO);
|
||||
// 添加权限和quota失败
|
||||
addAuthorityAndQuota2FaliureTest(authorityDO);
|
||||
}
|
||||
|
||||
private void addAuthorityAndQuota2SuccessTest(AuthorityDO authorityDO) {
|
||||
|
||||
int result = authorityService.addAuthorityAndQuota(authorityDO, getTopicQuota());
|
||||
Assert.assertEquals(result, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAllAuthorityTest() {
|
||||
private void addAuthorityAndQuota2FaliureTest(AuthorityDO authorityDO) {
|
||||
authorityService.addAuthority(authorityDO);
|
||||
// 重复插入
|
||||
int result2 = authorityService.addAuthorityAndQuota(authorityDO, getTopicQuota());
|
||||
Assert.assertEquals(result2, 0);
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideAuthorityDO")
|
||||
public void getAllAuthorityTest(AuthorityDO authorityDO) {
|
||||
authorityService.addAuthority(authorityDO);
|
||||
|
||||
Map<String, Map<Long, Map<String, AuthorityDO>>> allAuthority = authorityService.getAllAuthority();
|
||||
Assert.assertEquals(allAuthority.size(), 2);
|
||||
Assert.assertEquals(allAuthority.size(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteAuthorityByTopicTest() {
|
||||
// 测试查询成功
|
||||
deleteAuthorityByTopic2SuccessTest();
|
||||
// 测试查询为null
|
||||
@Test(dataProvider = "provideAuthorityDO", description = "测试删除")
|
||||
public void deleteAuthorityByTopicTest(AuthorityDO authorityDO) {
|
||||
// 测试删除成功
|
||||
deleteAuthorityByTopic2SuccessTest(authorityDO);
|
||||
// 测试删除失败
|
||||
deleteAuthorityByTopic2FailureTest();
|
||||
}
|
||||
|
||||
private void deleteAuthorityByTopic2SuccessTest() {
|
||||
int result = authorityService.deleteAuthorityByTopic(1L, "moduleTest");
|
||||
private void deleteAuthorityByTopic2SuccessTest(AuthorityDO authorityDO) {
|
||||
authorityService.addAuthority(authorityDO);
|
||||
int result = authorityService.deleteAuthorityByTopic(authorityDO.getClusterId(), authorityDO.getTopicName());
|
||||
Assert.assertEquals(result, 1);
|
||||
}
|
||||
|
||||
private void deleteAuthorityByTopic2FailureTest() {
|
||||
int result = authorityService.deleteAuthorityByTopic(10L, "moduleTest");
|
||||
int result = authorityService.deleteAuthorityByTopic(100L, "moduleTest");
|
||||
Assert.assertEquals(result, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.xiaojukeji.kafka.manager.service.service.gateway;
|
||||
|
||||
import com.xiaojukeji.kafka.manager.common.entity.pojo.gateway.KafkaAclDO;
|
||||
import com.xiaojukeji.kafka.manager.common.entity.pojo.gateway.KafkaUserDO;
|
||||
import com.xiaojukeji.kafka.manager.dao.gateway.KafkaAclDao;
|
||||
import com.xiaojukeji.kafka.manager.dao.gateway.KafkaUserDao;
|
||||
import com.xiaojukeji.kafka.manager.service.config.BaseTest;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author xuguang
|
||||
* @Date 2021/12/7
|
||||
*/
|
||||
public class SecurityServiceTest extends BaseTest {
|
||||
|
||||
@Autowired
|
||||
private SecurityService securityService;
|
||||
|
||||
@Autowired
|
||||
private KafkaUserDao kafkaUserDao;
|
||||
|
||||
@Autowired
|
||||
private KafkaAclDao kafkaAclDao;
|
||||
|
||||
@DataProvider(name = "provideKafkaUserDO")
|
||||
public static Object[][] provideKafkaUserDO() {
|
||||
KafkaUserDO kafkaUserDO = new KafkaUserDO();
|
||||
kafkaUserDO.setAppId("AppIdModuleTest");
|
||||
kafkaUserDO.setPassword("AppIdTest");
|
||||
kafkaUserDO.setUserType(1);
|
||||
kafkaUserDO.setOperation(0);
|
||||
return new Object[][] {{kafkaUserDO}};
|
||||
}
|
||||
|
||||
@DataProvider(name = "provideKafkaAclDO")
|
||||
public static Object[][] provideKafkaAclDO() {
|
||||
KafkaAclDO kafkaAclDO = new KafkaAclDO();
|
||||
kafkaAclDO.setAppId("AppIdModuleTest");
|
||||
kafkaAclDO.setClusterId(1L);
|
||||
kafkaAclDO.setTopicName("topicModuleTest");
|
||||
kafkaAclDO.setAccess(3);
|
||||
kafkaAclDO.setOperation(0);
|
||||
return new Object[][] {{kafkaAclDO}};
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideKafkaUserDO")
|
||||
public void getKafkaUsersTest(KafkaUserDO kafkaUserDO) {
|
||||
kafkaUserDao.insert(kafkaUserDO);
|
||||
|
||||
long now = System.currentTimeMillis();
|
||||
List<KafkaUserDO> kafkaUsers = securityService.getKafkaUsers(0L, now);
|
||||
Assert.assertFalse(kafkaUsers.isEmpty());
|
||||
Assert.assertTrue(kafkaUsers.stream()
|
||||
.allMatch( kafkaUser -> kafkaUser.getCreateTime().after(new Date(0L)) &&
|
||||
kafkaUser.getCreateTime().before( new Date(now))));
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideKafkaAclDO")
|
||||
public void getKafkaAclsTest(KafkaAclDO kafkaAclDO) {
|
||||
kafkaAclDao.insert(kafkaAclDO);
|
||||
|
||||
long now = System.currentTimeMillis();
|
||||
List<KafkaAclDO> kafkaAcls = securityService.getKafkaAcls(kafkaAclDO.getClusterId(), 0L, now);
|
||||
Assert.assertFalse(kafkaAcls.isEmpty());
|
||||
Assert.assertTrue(kafkaAcls.stream()
|
||||
.allMatch(kafkaUser -> kafkaUser.getCreateTime().after(new Date(0L)) &&
|
||||
kafkaUser.getCreateTime().before( new Date(now)) &&
|
||||
kafkaUser.getClusterId().equals(kafkaAclDO.getClusterId())));
|
||||
}
|
||||
}
|
||||
@@ -40,8 +40,15 @@ public class TopicReportServiceTest extends BaseTest {
|
||||
public void getNeedReportTopicTest(TopicReportDO topicReportDO) {
|
||||
// 数据库中插入数据
|
||||
int replace = topicReportDao.replace(topicReportDO);
|
||||
|
||||
List<TopicReportDO> result = topicReportService.getNeedReportTopic(1L);
|
||||
Assert.assertEquals(result.size(), 1);
|
||||
Assert.assertEquals(result.get(0).toString(), topicReportDO.toString());
|
||||
}
|
||||
|
||||
@Test(dataProvider = "provideTopicReportDO")
|
||||
public void replaceTest(TopicReportDO topicReportDO) {
|
||||
int replace = topicReportDao.replace(topicReportDO);
|
||||
Assert.assertEquals(replace, 2);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user