Merge pull request #472 from didi/dev_v2.5.0_addtest

LogiKM增加单元测试和集成测试
This commit is contained in:
EricZeng
2022-03-07 17:29:20 +08:00
committed by GitHub
171 changed files with 21563 additions and 124 deletions

1
.gitignore vendored
View File

@@ -111,3 +111,4 @@ dist/
dist/*
kafka-manager-web/src/main/resources/templates/
.DS_Store
kafka-manager-console/package-lock.json

View File

@@ -0,0 +1,47 @@
---
![kafka-manager-logo](../assets/images/common/logo_name.png)
**一站式`Apache Kafka`集群指标监控与运维管控平台**
---
# LogiKM单元测试和集成测试
## 1、单元测试
### 1.1 单元测试介绍
单元测试又称模块测试,是针对软件设计的最小单位——程序模块进行正确性检验的测试工作。
其目的在于检查每个程序单元能否正确实现详细设计说明中的模块功能、性能、接口和设计约束等要求,
发现各模块内部可能存在的各种错误。单元测试需要从程序的内部结构出发设计测试用例。
多个模块可以平行地独立进行单元测试。
### 1.2 LogiKM单元测试思路
LogiKM单元测试思路主要是测试Service层的方法通过罗列方法的各种参数
判断方法返回的结果是否符合预期。单元测试的基类加了@SpringBootTest注解,即每次运行单测用例都启动容器
### 1.3 LogiKM单元测试注意事项
1. 单元测试用例在kafka-manager-core以及kafka-manager-extends下的test包中
2. 配置在resources/application.yml包括运行单元测试用例启用的数据库配置等等
3. 编译打包项目时,加上参数-DskipTests可不执行测试用例例如使用命令行mvn -DskipTests进行打包
## 2、集成测试
### 2.1 集成测试介绍
集成测试又称组装测试,是一种黑盒测试。通常在单元测试的基础上,将所有的程序模块进行有序的、递增的测试。
集成测试是检验程序单元或部件的接口关系,逐步集成为符合概要设计要求的程序部件或整个系统。
### 2.2 LogiKM集成测试思路
LogiKM集成测试主要思路是对Controller层的接口发送Http请求。
通过罗列测试用例模拟用户的操作对接口发送Http请求判断结果是否达到预期。
本地运行集成测试用例时,无需加@SpringBootTest注解(即无需每次运行测试用例都启动容器)
### 2.3 LogiKM集成测试注意事项
1. 集成测试用例在kafka-manager-web的test包下
2. 因为对某些接口发送Http请求需要先登陆比较麻烦可以绕过登陆方法可见教程见docs -> user_guide -> call_api_bypass_login
3. 集成测试的配置在resources/integrationTest-settings.properties文件下包括集群地址zk地址的配置等等
4. 如果需要运行集成测试用例需要本地先启动LogiKM项目
5. 编译打包项目时,加上参数-DskipTests可不执行测试用例例如使用命令行mvn -DskipTests进行打包

View File

@@ -82,15 +82,15 @@ public class Result<T> implements Serializable {
return JSON.toJSONString(this);
}
public static Result buildSuc() {
Result result = new Result();
public static <T> Result<T> buildSuc() {
Result<T> result = new Result<>();
result.setCode(ResultStatus.SUCCESS.getCode());
result.setMessage(ResultStatus.SUCCESS.getMessage());
return result;
}
public static <T> Result<T> buildSuc(T data) {
Result<T> result = new Result<T>();
Result<T> result = new Result<>();
result.setCode(ResultStatus.SUCCESS.getCode());
result.setMessage(ResultStatus.SUCCESS.getMessage());
result.setData(data);
@@ -98,7 +98,7 @@ public class Result<T> implements Serializable {
}
public static <T> Result<T> buildGatewayFailure(String message) {
Result<T> result = new Result<T>();
Result<T> result = new Result<>();
result.setCode(ResultStatus.GATEWAY_INVALID_REQUEST.getCode());
result.setMessage(message);
result.setData(null);
@@ -106,22 +106,22 @@ public class Result<T> implements Serializable {
}
public static <T> Result<T> buildFailure(String message) {
Result<T> result = new Result<T>();
Result<T> result = new Result<>();
result.setCode(ResultStatus.FAIL.getCode());
result.setMessage(message);
result.setData(null);
return result;
}
public static Result buildFrom(ResultStatus resultStatus) {
Result result = new Result();
public static <T> Result<T> buildFrom(ResultStatus resultStatus) {
Result<T> result = new Result<>();
result.setCode(resultStatus.getCode());
result.setMessage(resultStatus.getMessage());
return result;
}
public static Result buildFrom(ResultStatus resultStatus, Object data) {
Result result = new Result();
public static <T> Result<T> buildFrom(ResultStatus resultStatus, T data) {
Result<T> result = new Result<>();
result.setCode(resultStatus.getCode());
result.setMessage(resultStatus.getMessage());
result.setData(data);

View File

@@ -118,10 +118,7 @@ public class LogicalClusterDTO {
}
public boolean legal() {
if (ValidateUtils.isNull(clusterId)
|| ValidateUtils.isNull(clusterId)
|| ValidateUtils.isEmptyList(regionIdList)
|| ValidateUtils.isNull(mode)) {
if (ValidateUtils.isNull(clusterId) || ValidateUtils.isEmptyList(regionIdList) || ValidateUtils.isNull(mode)) {
return false;
}
if (!ClusterModeEnum.SHARED_MODE.getCode().equals(mode) && ValidateUtils.isNull(appId)) {

View File

@@ -94,10 +94,7 @@ public class RegionDTO {
}
public boolean legal() {
if (ValidateUtils.isNull(clusterId)
|| ValidateUtils.isNull(clusterId)
|| ValidateUtils.isEmptyList(brokerIdList)
|| ValidateUtils.isNull(status)) {
if (ValidateUtils.isNull(clusterId) || ValidateUtils.isEmptyList(brokerIdList) || ValidateUtils.isNull(status)) {
return false;
}
description = ValidateUtils.isNull(description)? "": description;

View File

@@ -13,6 +13,7 @@ import org.springframework.context.ApplicationEvent;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@@ -81,16 +82,19 @@ public class SpringTool implements ApplicationContextAware, DisposableBean {
}
public static String getUserName(){
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String username = null;
if (TrickLoginConstant.TRICK_LOGIN_SWITCH_ON.equals(request.getHeader(TrickLoginConstant.TRICK_LOGIN_SWITCH))) {
// trick登录方式的获取用户
username = request.getHeader(TrickLoginConstant.TRICK_LOGIN_USER);
} else {
// 走页面登录方式登录的获取用户
HttpSession session = request.getSession();
username = (String) session.getAttribute(LoginConstant.SESSION_USERNAME_KEY);
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if (!ValidateUtils.isNull(requestAttributes)) {
HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
if (TrickLoginConstant.TRICK_LOGIN_SWITCH_ON.equals(request.getHeader(TrickLoginConstant.TRICK_LOGIN_SWITCH))) {
// trick登录方式的获取用户
username = request.getHeader(TrickLoginConstant.TRICK_LOGIN_USER);
} else {
// 走页面登录方式登录的获取用户
HttpSession session = request.getSession();
username = (String) session.getAttribute(LoginConstant.SESSION_USERNAME_KEY);
}
}
if (ValidateUtils.isNull(username)) {

View File

@@ -29,10 +29,10 @@ public class TopicQuotaData {
public static TopicQuotaData getClientData(Long producerByteRate, Long consumerByteRate) {
TopicQuotaData clientData = new TopicQuotaData();
if (!ValidateUtils.isNull(producerByteRate) && consumerByteRate != -1) {
if (!ValidateUtils.isNull(consumerByteRate) && consumerByteRate != -1) {
clientData.setConsumer_byte_rate(consumerByteRate.toString());
}
if (!ValidateUtils.isNull(consumerByteRate) && producerByteRate != -1) {
if (!ValidateUtils.isNull(producerByteRate) && producerByteRate != -1) {
clientData.setProducer_byte_rate(producerByteRate.toString());
}
return clientData;

View File

@@ -95,5 +95,23 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<!-- testng -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.10</version>
</dependency>
</dependencies>
</project>

View File

@@ -42,6 +42,13 @@ public interface ConsumerService {
*/
List<String> getConsumerGroupConsumedTopicList(Long clusterId, String consumerGroup, String location);
/**
* 获取消费者offset
* @param clusterDO 集群
* @param topicName topic
* @param consumerGroup 消费组
* @return Map<partitionId, offset>
*/
Map<Integer, Long> getConsumerOffset(ClusterDO clusterDO, String topicName, ConsumerGroup consumerGroup);
/**
@@ -52,7 +59,20 @@ public interface ConsumerService {
ConsumerGroup consumerGroup,
List<PartitionOffsetDTO> partitionOffsetDTOList);
/**
* 获取每个集群消费组的个数
* @param clusterDOList 物理集群列表
* @return Map<clusterId, consumerGroupNums>
*/
Map<Long, Integer> getConsumerGroupNumMap(List<ClusterDO> clusterDOList);
/**
* 验证消费组是否存在
* @param offsetLocation offset存放位置
* @param id 集群id
* @param topicName topic
* @param consumerGroup 消费组
* @return true:存在false:不存在
*/
boolean checkConsumerGroupExist(OffsetLocationEnum offsetLocation, Long id, String topicName, String consumerGroup);
}

View File

@@ -54,12 +54,12 @@ public interface RegionService {
Map<Integer, RegionDO> convert2BrokerIdRegionMap(List<RegionDO> regionDOList);
/**
* 更新逻辑集群容量
* @param clusterId 集群id
* 根据RegionId更新Region
* @param regionId region的id
* @param newBrokerList 新的broker列表
* @return ResultStatus
*/
ResultStatus updateRegion(Long clusterId, String newBrokerList);
ResultStatus updateRegion(Long regionId, String newBrokerList);
/**
* 获取空闲的region的broker列表

View File

@@ -104,6 +104,13 @@ public interface TopicService {
*/
List<TopicBrokerDTO> getTopicBrokerList(Long clusterId, String topicName);
/**
* 判断topic是否有数据写入即分区topic的offset变化
* @param physicalClusterId 物理集群Id
* @param topicName topic名称
* @param latestTime 离当前多久开始计算
* @return
*/
Result<TopicOffsetChangedEnum> checkTopicOffsetChanged(Long physicalClusterId, String topicName, Long latestTime);
}

View File

@@ -159,7 +159,7 @@ public class ConsumerServiceImpl implements ConsumerService {
if (topicMetadata == null) {
logger.warn("class=ConsumerServiceImpl||method=getConsumeDetail||clusterId={}||topicName={}||msg=topicMetadata is null!",
clusterDO.getId(), topicName);
return null;
return Collections.emptyList();
}
List<ConsumeDetailDTO> consumerGroupDetailDTOList = null;
@@ -170,7 +170,7 @@ public class ConsumerServiceImpl implements ConsumerService {
}
if (consumerGroupDetailDTOList == null) {
logger.info("class=ConsumerServiceImpl||method=getConsumeDetail||msg=consumerGroupDetailDTOList is null!");
return null;
return Collections.emptyList();
}
Map<TopicPartition, Long> topicPartitionLongMap = topicService.getPartitionOffset(clusterDO, topicName, OffsetPosEnum.END);
@@ -317,9 +317,6 @@ public class ConsumerServiceImpl implements ConsumerService {
String consumerGroup) {
Map<Integer, String> stringOffsetMap =
getOffsetByGroupAndTopicFromBroker(clusterDO, consumerGroup, topicName);
if (ValidateUtils.isNull(stringOffsetMap)) {
return new HashMap<>(0);
}
Map<Integer, Long> offsetMap = new HashMap<>(stringOffsetMap.size());
for (Map.Entry<Integer, String> entry: stringOffsetMap.entrySet()) {

View File

@@ -167,9 +167,11 @@ public class JmxServiceImpl implements JmxService {
if (ValidateUtils.isNull(jmxConnectorWrap)|| !jmxConnectorWrap.checkJmxConnectionAndInitIfNeed()) {
return null;
}
KafkaVersion kafkaVersion = physicalClusterMetadataManager.getKafkaVersion(clusterId, brokerId);
TopicMetrics metrics = new TopicMetrics(clusterId, topicName);
for (MbeanV2 mbeanV2: mbeanV2List) {
KafkaVersion kafkaVersion = physicalClusterMetadataManager.getKafkaVersion(clusterId, brokerId);
try {
getAndSupplyAttributes2BaseMetrics(
metrics,

View File

@@ -138,11 +138,11 @@ public class RegionServiceImpl implements RegionService {
@Override
public ResultStatus updateRegion(Long clusterId, String newBrokerList) {
if (ValidateUtils.isNull(clusterId) || ValidateUtils.isExistBlank(newBrokerList)) {
public ResultStatus updateRegion(Long regionId, String newBrokerList) {
if (ValidateUtils.isNull(regionId) || ValidateUtils.isExistBlank(newBrokerList)) {
return ResultStatus.PARAM_ILLEGAL;
}
RegionDO regionDO = getById(clusterId);
RegionDO regionDO = getById(regionId);
if (ValidateUtils.isNull(regionDO)) {
return ResultStatus.CLUSTER_NOT_EXIST;
}

View File

@@ -419,6 +419,7 @@ public class TopicManagerServiceImpl implements TopicManagerService {
authorityDO.setTopicName(topicName);
authorityDO.setAccess(TopicAuthorityEnum.READ_WRITE.getCode());
authorityService.addAuthority(authorityDO);
return ResultStatus.SUCCESS;
} catch (Exception e) {
LOGGER.error("modify topic failed, clusterId:{} topicName:{} description:{} operator:{} ",
clusterId, topicName, description, operator, e);
@@ -631,7 +632,7 @@ public class TopicManagerServiceImpl implements TopicManagerService {
// 该用户无应用,需要先申请应用
return ResultStatus.APP_NOT_EXIST;
}
List<Long> appIds = appDOs.stream().map(AppDO::getId).collect(Collectors.toList());
List<String> appIds = appDOs.stream().map(AppDO::getAppId).collect(Collectors.toList());
if (!appIds.contains(authorityDO.getAppId())) {
// 入参中的appId该用户未拥有
return ResultStatus.APP_NOT_EXIST;

View File

@@ -250,11 +250,11 @@ public class TopicServiceImpl implements TopicService {
@Override
public List<TopicPartitionDTO> getTopicPartitionDTO(ClusterDO clusterDO, String topicName, Boolean needDetail) {
if (ValidateUtils.isNull(clusterDO) || ValidateUtils.isNull(topicName)) {
return null;
return new ArrayList<>();
}
TopicMetadata topicMetadata = PhysicalClusterMetadataManager.getTopicMetadata(clusterDO.getId(), topicName);
if (ValidateUtils.isNull(topicMetadata)) {
return null;
return new ArrayList<>();
}
List<PartitionState> partitionStateList = KafkaZookeeperUtils.getTopicPartitionState(
@@ -419,9 +419,6 @@ public class TopicServiceImpl implements TopicService {
topicDO,
appDO
);
if (ValidateUtils.isNull(overview)) {
continue;
}
dtoList.add(overview);
}
@@ -531,7 +528,7 @@ public class TopicServiceImpl implements TopicService {
public List<PartitionOffsetDTO> getPartitionOffsetList(ClusterDO clusterDO, String topicName, Long timestamp) {
TopicMetadata topicMetadata = PhysicalClusterMetadataManager.getTopicMetadata(clusterDO.getId(), topicName);
if (topicMetadata == null) {
return null;
return new ArrayList<>();
}
Map<TopicPartition, Long> timestampsToSearch = new HashMap<>();
for (Integer partitionId : topicMetadata.getPartitionMap().getPartitions().keySet()) {
@@ -575,7 +572,7 @@ public class TopicServiceImpl implements TopicService {
kafkaConsumer.close();
}
}
return null;
return new ArrayList<>();
}
private List<String> fetchTopicData(KafkaConsumer kafkaConsumer, ClusterDO clusterDO, String topicName, TopicDataSampleDTO reqObj) {
@@ -588,7 +585,7 @@ public class TopicServiceImpl implements TopicService {
tpList.add(new TopicPartition(topicName, partitionId));
}
if (ValidateUtils.isEmptyList(tpList)) {
return null;
return new ArrayList<>();
}
kafkaConsumer.assign(tpList);

View File

@@ -92,15 +92,15 @@ public class DidiHealthScoreStrategy extends AbstractHealthScoreStrategy {
return HEALTH_SCORE_BAD;
}
Object RequestHandlerAvgIdlePercentOneMinuteRate = metrics.getMetricsMap().get("RequestHandlerAvgIdlePercentOneMinuteRate");
Object NetworkProcessorAvgIdlePercentValue = metrics.getMetricsMap().get("NetworkProcessorAvgIdlePercentValue");
if (ValidateUtils.isNull(RequestHandlerAvgIdlePercentOneMinuteRate)
|| ValidateUtils.isNull(NetworkProcessorAvgIdlePercentValue)) {
Object requestHandlerAvgIdlePercentOneMinuteRate = metrics.getMetricsMap().get("RequestHandlerAvgIdlePercentOneMinuteRate");
Object networkProcessorAvgIdlePercentValue = metrics.getMetricsMap().get("NetworkProcessorAvgIdlePercentValue");
if (ValidateUtils.isNull(requestHandlerAvgIdlePercentOneMinuteRate)
|| ValidateUtils.isNull(networkProcessorAvgIdlePercentValue)) {
// 数据获取失败
return Constant.INVALID_CODE;
}
if (((Double) RequestHandlerAvgIdlePercentOneMinuteRate) < MIN_IDLE * KAFKA_REQUEST_HANDLER_POOL_SIZE
|| ((Double) NetworkProcessorAvgIdlePercentValue) < MIN_IDLE) {
if (((Double) requestHandlerAvgIdlePercentOneMinuteRate) < MIN_IDLE * KAFKA_REQUEST_HANDLER_POOL_SIZE
|| ((Double) networkProcessorAvgIdlePercentValue) < MIN_IDLE) {
return HEALTH_SCORE_NORMAL;
}
return HEALTH_SCORE_HEALTHY;
@@ -117,7 +117,7 @@ public class DidiHealthScoreStrategy extends AbstractHealthScoreStrategy {
return Constant.INVALID_CODE;
}
List<Integer> brokerIdList = new ArrayList<>(metadata.getBrokerIdSet().size());
List<Integer> brokerIdList = new ArrayList<>(metadata.getBrokerIdSet());
FutureTask<Integer>[] taskList = new FutureTask[brokerIdList.size()];
for (int i = 0; i < brokerIdList.size(); ++i) {

View File

@@ -0,0 +1,43 @@
package com.xiaojukeji.kafka.manager.service;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.pojo.ClusterDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.gateway.AppDO;
import com.xiaojukeji.kafka.manager.service.config.BaseTest;
import com.xiaojukeji.kafka.manager.service.service.ClusterService;
import com.xiaojukeji.kafka.manager.service.service.gateway.AppService;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import static org.mockito.Mockito.when;
public class DemoTest extends BaseTest {
@Autowired
private ClusterService clusterService;
@Mock
private AppService appServiceMock;
@BeforeMethod
public void setup() {
MockitoAnnotations.initMocks(this);
}
@Test
public void test() {
Assert.assertNull(clusterService.getById(100L));
}
@Test
public void testMock() {
when(appServiceMock.getByAppId("100")).thenReturn(new AppDO());
Assert.assertNotNull(appServiceMock.getByAppId("100"));
}
}

View File

@@ -1,47 +0,0 @@
package com.xiaojukeji.kafka.manager.service;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import org.junit.Test;
public class FutureTest {
@Test
public void test() throws InterruptedException, ExecutionException {
FutureTask<Integer> f1 = new FutureTask<Integer>(new Callable<Integer>() {
@Override
public Integer call() throws InterruptedException {
Thread.sleep(1000L);
return 1;
}
});
FutureTask<Integer> f2 = new FutureTask<Integer>(new Callable<Integer>() {
@Override
public Integer call() throws InterruptedException {
Thread.sleep(1000L);
return 2;
}
});
ExecutorService threadPool = Executors.newCachedThreadPool();
long ct = System.currentTimeMillis();
threadPool.submit(f1);
threadPool.submit(f2);
threadPool.shutdown();
System.out.println(f1.get() + " : " + f2.get() + " use:"
+ (System.currentTimeMillis() - ct));
}
}

View File

@@ -0,0 +1,11 @@
package com.xiaojukeji.kafka.manager.service.config;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
@SpringBootTest(classes = CoreSpringBootStartUp.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(classes = CoreSpringBootStartUp.class)
public class BaseTest extends AbstractTransactionalTestNGSpringContextTests {
}

View File

@@ -0,0 +1,21 @@
package com.xiaojukeji.kafka.manager.service.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableAsync
@EnableScheduling
@ServletComponentScan
@EnableAutoConfiguration
@SpringBootApplication(scanBasePackages = {"com.xiaojukeji.kafka.manager"})
public class CoreSpringBootStartUp {
public static void main(String[] args) {
SpringApplication sa = new SpringApplication(CoreSpringBootStartUp.class);
sa.run(args);
}
}

View File

@@ -0,0 +1,51 @@
package com.xiaojukeji.kafka.manager.service.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* @author zengqiao
* @date 20/3/17
*/
@Configuration
public class DataSourceConfig {
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource.kafka-manager")
@Primary
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "sqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
bean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:mybatis-config.xml"));
return bean.getObject();
}
@Bean(name = "transactionManager")
@Primary
public DataSourceTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "sqlSession")
@Primary
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}

View File

@@ -0,0 +1,455 @@
package com.xiaojukeji.kafka.manager.service.service;
import com.xiaojukeji.kafka.manager.common.bizenum.TaskStatusEnum;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.pojo.ClusterDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.TopicDO;
import com.xiaojukeji.kafka.manager.common.exception.ConfigException;
import com.xiaojukeji.kafka.manager.common.zookeeper.ZkConfigImpl;
import com.xiaojukeji.kafka.manager.service.config.BaseTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.Arrays;
import java.util.Date;
import java.util.Properties;
/**
* @author xuguang
* @Date 2021/12/24
*/
public class AdminServiceTest extends BaseTest {
/**
* 集群共包括三个broker:1,2,3, 该topic 1分区 1副本因子在broker1上
*/
@Value("${test.topic.name1}")
private String REAL_TOPIC1_IN_ZK;
@Value("${test.topic.name3}")
private String REAL_TOPIC3_IN_ZK;
/**
* 集群共包括三个broker:1,2,3, 该topic 2分区 3副本因子在broker1,2,3上
*/
@Value("${test.topic.name2}")
private String REAL_TOPIC2_IN_ZK;
private final static String INVALID_TOPIC = "xxxxx";
private final static String ZK_DEFAULT_TOPIC = "_consumer_offsets";
private final static String CREATE_TOPIC_TEST = "createTopicTest";
@Value("${test.phyCluster.id}")
private Long REAL_CLUSTER_ID_IN_MYSQL;
@Value("${test.broker.id1}")
private Integer REAL_BROKER_ID_IN_ZK;
private final static Long INVALID_CLUSTER_ID = -1L;
private final static Integer INVALID_PARTITION_ID = -1;
private final static Integer REAL_PARTITION_ID = 0;
private final static Integer INVALID_BROKER_ID = -1;
@Value("${test.app.id}")
private String APP_ID;
private final static Long INVALID_REGION_ID = -1L;
private final static Long REAL_REGION_ID_IN_MYSQL = 1L;
@Value("${test.admin}")
private String ADMIN;
@Value("${test.phyCluster.name}")
private String REAL_PHYSICAL_CLUSTER_NAME;
@Value("${test.ZK.address}")
private String ZOOKEEPER_ADDRESS;
@Value("${test.ZK.bootstrap-servers}")
private String BOOTSTRAP_SERVERS;
private final static String SECURITY_PROTOCOL = "{ \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\\\";\" }";
// 优先副本节点在zk上的路径
private final static String ZK_NODE_PATH_PREFERRED = "/admin/preferred_replica_election";
// 创建的topic节点在zk上的路径;brokers节点下的
private final static String ZK_NODE_PATH_BROKERS_TOPIC = "/brokers/topics/createTopicTest";
// config节点下的
private final static String ZK_NODE_PATH_CONFIG_TOPIC = "/config/topics/createTopicTest";
@Autowired
private AdminService adminService;
@Autowired
private TopicManagerService topicManagerService;
private TopicDO getTopicDO() {
TopicDO topicDO = new TopicDO();
topicDO.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
topicDO.setTopicName(CREATE_TOPIC_TEST);
topicDO.setAppId(APP_ID);
topicDO.setDescription(CREATE_TOPIC_TEST);
topicDO.setPeakBytesIn(100000L);
return topicDO;
}
public ClusterDO getClusterDO() {
ClusterDO clusterDO = new ClusterDO();
clusterDO.setId(REAL_CLUSTER_ID_IN_MYSQL);
clusterDO.setClusterName(REAL_PHYSICAL_CLUSTER_NAME);
clusterDO.setZookeeper(ZOOKEEPER_ADDRESS);
clusterDO.setBootstrapServers(BOOTSTRAP_SERVERS);
clusterDO.setSecurityProperties(SECURITY_PROTOCOL);
clusterDO.setStatus(1);
clusterDO.setGmtCreate(new Date());
clusterDO.setGmtModify(new Date());
return clusterDO;
}
@Test(description = "测试创建topic")
public void createTopicTest() throws ConfigException {
// broker not exist
createTopic2BrokerNotExistTest();
// success to create topic
createTopic2SuccessTest();
// failure to create topic, topic already exists
createTopic2FailureTest();
// 创建成功后数据库和zk中会存在该Topic需要删除防止影响后面测试
// 写入数据库的整个Test结束后回滚因此只用删除zk上的topic节点
ZkConfigImpl zkConfig = new ZkConfigImpl(ZOOKEEPER_ADDRESS);
zkConfig.delete(ZK_NODE_PATH_BROKERS_TOPIC);
zkConfig.delete(ZK_NODE_PATH_CONFIG_TOPIC);
zkConfig.close();
}
private void createTopic2BrokerNotExistTest() {
TopicDO topicDO = getTopicDO();
ClusterDO clusterDO = getClusterDO();
ResultStatus result = adminService.createTopic(
clusterDO,
topicDO,
1,
1,
INVALID_REGION_ID,
Arrays.asList(INVALID_BROKER_ID),
new Properties(),
ADMIN,
ADMIN);
Assert.assertEquals(result.getCode(), ResultStatus.BROKER_NOT_EXIST.getCode());
}
private void createTopic2FailureTest() {
TopicDO topicDO = getTopicDO();
ClusterDO clusterDO = getClusterDO();
ResultStatus result = adminService.createTopic(
clusterDO,
topicDO,
1,
1,
INVALID_REGION_ID,
Arrays.asList(REAL_BROKER_ID_IN_ZK),
new Properties(),
ADMIN,
ADMIN);
Assert.assertNotEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
}
private void createTopic2SuccessTest() {
TopicDO topicDO = getTopicDO();
ClusterDO clusterDO = getClusterDO();
ResultStatus result = adminService.createTopic(
clusterDO,
topicDO,
1,
1,
INVALID_REGION_ID,
Arrays.asList(REAL_BROKER_ID_IN_ZK),
new Properties(),
ADMIN,
ADMIN);
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test(description = "测试删除topic")
public void deleteTopicTest() {
// topic does not exist
deleteTopic2FailureTest();
// success to delete
deleteTopic2SuccessTest();
}
private void deleteTopic2FailureTest() {
ClusterDO clusterDO = getClusterDO();
ResultStatus resultStatus = adminService.deleteTopic(
clusterDO,
INVALID_TOPIC,
ADMIN
);
Assert.assertNotEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
}
private void deleteTopic2SuccessTest() {
TopicDO topicDO = getTopicDO();
ClusterDO clusterDO = getClusterDO();
ResultStatus result = adminService.createTopic(
clusterDO,
topicDO,
1,
1,
INVALID_REGION_ID,
Arrays.asList(REAL_BROKER_ID_IN_ZK),
new Properties(),
ADMIN,
ADMIN);
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
ResultStatus resultStatus = adminService.deleteTopic(
clusterDO,
CREATE_TOPIC_TEST,
ADMIN
);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test(description = "测试优先副本选举状态")
public void preferredReplicaElectionStatusTest() throws ConfigException {
// running
// preferredReplicaElectionStatus2RunningTest();
// not running
preferredReplicaElectionStatus2NotRunningTest();
}
private void preferredReplicaElectionStatus2RunningTest() throws ConfigException{
// zk上需要创建/admin/preferred_replica_election节点
ZkConfigImpl zkConfig = new ZkConfigImpl(ZOOKEEPER_ADDRESS);
zkConfig.setOrCreatePersistentNodeStat(ZK_NODE_PATH_PREFERRED, "");
ClusterDO clusterDO = getClusterDO();
TaskStatusEnum taskStatusEnum = adminService.preferredReplicaElectionStatus(clusterDO);
Assert.assertEquals(taskStatusEnum.getCode(), TaskStatusEnum.RUNNING.getCode());
// 删除之前创建的节点,防止影响后续测试
zkConfig.delete(ZK_NODE_PATH_PREFERRED);
zkConfig.close();
}
private void preferredReplicaElectionStatus2NotRunningTest() throws ConfigException {
ClusterDO clusterDO = getClusterDO();
// zk上无/admin/preferred_replica_election节点
TaskStatusEnum taskStatusEnum = adminService.preferredReplicaElectionStatus(clusterDO);
Assert.assertEquals(taskStatusEnum.getCode(), TaskStatusEnum.SUCCEED.getCode());
// 删除创建的节点,防止影响后续测试
ZkConfigImpl zkConfig = new ZkConfigImpl(ZOOKEEPER_ADDRESS);
zkConfig.delete(ZK_NODE_PATH_PREFERRED);
zkConfig.close();
}
@Test(description = "测试集群纬度优先副本选举")
public void preferredReplicaElectionOfCluster2Test() throws ConfigException {
ClusterDO clusterDO = getClusterDO();
ResultStatus resultStatus = adminService.preferredReplicaElection(clusterDO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
// 删除创建的节点,防止影响后续测试
ZkConfigImpl zkConfig = new ZkConfigImpl(ZOOKEEPER_ADDRESS);
zkConfig.delete(ZK_NODE_PATH_PREFERRED);
zkConfig.close();
}
@Test(description = "Broker纬度优先副本选举")
public void preferredReplicaElectionOfBrokerTest() throws ConfigException {
// 参数异常
preferredReplicaElectionOfBroker2ParamIllegalTest();
// success
preferredReplicaElectionOfBroker2SuccessTest();
}
private void preferredReplicaElectionOfBroker2ParamIllegalTest() {
ClusterDO clusterDO = getClusterDO();
ResultStatus resultStatus = adminService.preferredReplicaElection(
clusterDO,
INVALID_BROKER_ID,
ADMIN
);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
}
private void preferredReplicaElectionOfBroker2SuccessTest() throws ConfigException {
ClusterDO clusterDO = getClusterDO();
ResultStatus resultStatus = adminService.preferredReplicaElection(
clusterDO,
REAL_BROKER_ID_IN_ZK,
ADMIN
);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
// 删除创建的节点,防止影响后续测试
ZkConfigImpl zkConfig = new ZkConfigImpl(ZOOKEEPER_ADDRESS);
zkConfig.delete(ZK_NODE_PATH_PREFERRED);
zkConfig.close();
}
@Test(description = "Topic纬度优先副本选举")
public void preferredReplicaElectionOfTopicTest() throws ConfigException {
// topic not exist
preferredReplicaElectionOfTopic2TopicNotExistTest();
// success
preferredReplicaElectionOfTopic2SuccessTest();
}
private void preferredReplicaElectionOfTopic2TopicNotExistTest() {
ClusterDO clusterDO = getClusterDO();
ResultStatus resultStatus = adminService.preferredReplicaElection(
clusterDO,
INVALID_TOPIC,
ADMIN
);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.TOPIC_NOT_EXIST.getCode());
}
private void preferredReplicaElectionOfTopic2SuccessTest() throws ConfigException {
ClusterDO clusterDO = getClusterDO();
ResultStatus resultStatus = adminService.preferredReplicaElection(
clusterDO,
REAL_TOPIC1_IN_ZK,
ADMIN
);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
// 删除创建的节点,防止影响后续测试
ZkConfigImpl zkConfig = new ZkConfigImpl(ZOOKEEPER_ADDRESS);
zkConfig.delete(ZK_NODE_PATH_PREFERRED);
zkConfig.close();
}
@Test(description = "分区纬度优先副本选举")
public void preferredReplicaElectionOfPartitionTest() throws ConfigException {
// topic not exist
preferredReplicaElectionOfPartition2TopicNotExistTest();
// partition Not Exist
preferredReplicaElectionOfPartition2PartitionNotExistTest();
// success
preferredReplicaElectionOfPartition2SuccessTest();
}
private void preferredReplicaElectionOfPartition2TopicNotExistTest() {
ClusterDO clusterDO = getClusterDO();
ResultStatus resultStatus = adminService.preferredReplicaElection(
clusterDO,
INVALID_TOPIC,
INVALID_PARTITION_ID,
ADMIN
);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.TOPIC_NOT_EXIST.getCode());
}
private void preferredReplicaElectionOfPartition2PartitionNotExistTest() {
ClusterDO clusterDO = getClusterDO();
ResultStatus resultStatus = adminService.preferredReplicaElection(
clusterDO,
REAL_TOPIC2_IN_ZK,
INVALID_PARTITION_ID,
ADMIN
);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.PARTITION_NOT_EXIST.getCode());
}
private void preferredReplicaElectionOfPartition2SuccessTest() throws ConfigException {
ClusterDO clusterDO = getClusterDO();
ResultStatus resultStatus = adminService.preferredReplicaElection(
clusterDO,
REAL_TOPIC2_IN_ZK,
REAL_PARTITION_ID,
ADMIN
);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
// 删除创建的节点,防止影响后续测试
ZkConfigImpl zkConfig = new ZkConfigImpl(ZOOKEEPER_ADDRESS);
zkConfig.delete(ZK_NODE_PATH_PREFERRED);
zkConfig.close();
}
@Test(description = "测试获取Topic配置")
public void getTopicConfigTest() {
// result is null
getTopicConfig2NullTest();
// result not null
getTopicConfig2NotNullTest();
}
private void getTopicConfig2NullTest() {
ClusterDO clusterDO = getClusterDO();
clusterDO.setId(INVALID_CLUSTER_ID);
Properties topicConfig = adminService.getTopicConfig(clusterDO, REAL_TOPIC1_IN_ZK);
Assert.assertNull(topicConfig);
}
private void getTopicConfig2NotNullTest() {
ClusterDO clusterDO = getClusterDO();
Properties topicConfig = adminService.getTopicConfig(clusterDO, REAL_TOPIC1_IN_ZK);
Assert.assertNotNull(topicConfig);
}
@Test(description = "测试修改Topic配置")
public void modifyTopicConfigTest() {
ClusterDO clusterDO = getClusterDO();
Properties properties = new Properties();
properties.put("retention.ms", "21600000");
ResultStatus resultStatus = adminService.modifyTopicConfig(
clusterDO,
REAL_TOPIC1_IN_ZK,
properties,
ADMIN
);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test(description = "测试扩分区")
// 该测试会导致真实topic分区发生变化
public void expandPartitionsTest() {
// broker not exist
// expandPartitions2BrokerNotExistTest();
// success
// expandPartitions2SuccessTest();
}
private void expandPartitions2BrokerNotExistTest() {
// 存在两个下线broker, region中包含一个
ClusterDO clusterDO = getClusterDO();
ResultStatus resultStatus = adminService.expandPartitions(
clusterDO,
REAL_TOPIC1_IN_ZK,
2,
REAL_REGION_ID_IN_MYSQL,
Arrays.asList(INVALID_BROKER_ID),
ADMIN
);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.BROKER_NOT_EXIST.getCode());
}
private void expandPartitions2SuccessTest() {
ClusterDO clusterDO = getClusterDO();
ResultStatus resultStatus = adminService.expandPartitions(
clusterDO,
REAL_TOPIC3_IN_ZK,
2,
INVALID_REGION_ID,
Arrays.asList(REAL_BROKER_ID_IN_ZK),
ADMIN
);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
}
}

View File

@@ -0,0 +1,57 @@
package com.xiaojukeji.kafka.manager.service.service;
import com.xiaojukeji.kafka.manager.common.entity.ao.analysis.AnalysisBrokerDTO;
import com.xiaojukeji.kafka.manager.service.config.BaseTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.testng.Assert;
import org.testng.annotations.Test;
/**
* @author xuguang
* @Date 2021/12/23
*/
public class AnalysisServiceTest extends BaseTest {
@Value("${test.phyCluster.id}")
private Long REAL_CLUSTER_ID_IN_MYSQL;
@Value("${test.broker.id1}")
private final static Integer REAL_BROKER_ID_IN_ZK = 1;
private final static Long INVALID_CLUSTER_ID = -1L;
@Autowired
private AnalysisService analysisService;
@Test
public void doAnalysisBrokerTest() {
// brokerMetrics is null
doAnalysisBroker2brokerMetricsIsNullTest();
// brokerMetrics is not null
doAnalysisBroker2brokerMetricsIsNotNullTest();
}
private void doAnalysisBroker2brokerMetricsIsNullTest() {
AnalysisBrokerDTO analysisBrokerDTO = analysisService.doAnalysisBroker(
INVALID_CLUSTER_ID,
REAL_BROKER_ID_IN_ZK
);
Assert.assertNotNull(analysisBrokerDTO);
Assert.assertEquals(analysisBrokerDTO.getBrokerId(), REAL_BROKER_ID_IN_ZK);
Assert.assertEquals(analysisBrokerDTO.getClusterId(), INVALID_CLUSTER_ID);
Assert.assertNull(analysisBrokerDTO.getBytesIn());
}
private void doAnalysisBroker2brokerMetricsIsNotNullTest() {
AnalysisBrokerDTO analysisBrokerDTO = analysisService.doAnalysisBroker(
REAL_CLUSTER_ID_IN_MYSQL,
REAL_BROKER_ID_IN_ZK
);
Assert.assertNotNull(analysisBrokerDTO);
Assert.assertEquals(analysisBrokerDTO.getBrokerId(), REAL_BROKER_ID_IN_ZK);
Assert.assertEquals(analysisBrokerDTO.getClusterId(), REAL_CLUSTER_ID_IN_MYSQL);
Assert.assertNotNull(analysisBrokerDTO.getBytesIn());
}
}

View File

@@ -0,0 +1,301 @@
package com.xiaojukeji.kafka.manager.service.service;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.ao.BrokerBasicDTO;
import com.xiaojukeji.kafka.manager.common.entity.ao.BrokerOverviewDTO;
import com.xiaojukeji.kafka.manager.common.entity.ao.TopicDiskLocation;
import com.xiaojukeji.kafka.manager.common.entity.metrics.BrokerMetrics;
import com.xiaojukeji.kafka.manager.common.entity.pojo.BrokerDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.BrokerMetricsDO;
import com.xiaojukeji.kafka.manager.common.zookeeper.znode.brokers.BrokerMetadata;
import com.xiaojukeji.kafka.manager.common.zookeeper.znode.brokers.PartitionState;
import com.xiaojukeji.kafka.manager.service.config.BaseTest;
import org.apache.kafka.common.TopicPartition;
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.beans.factory.annotation.Value;
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 BrokerServiceTest extends BaseTest {
@Value("${test.phyCluster.id}")
private Long REAL_CLUSTER_ID_IN_MYSQL;
@Value("${test.broker.id1}")
private Integer REAL_BROKER_ID_IN_ZK;
@Value("${test.sasl-plaintext}")
private String END_POINTS_IN_BROKER;
@Autowired
@InjectMocks
private BrokerService brokerService;
@Mock
private JmxService jmxService;
@Mock
private TopicService topicService;
@BeforeMethod
public void setup() {
MockitoAnnotations.initMocks(this);
}
private BrokerDO getBrokerDO() {
BrokerDO brokerDO = new BrokerDO();
brokerDO.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
brokerDO.setBrokerId(100);
brokerDO.setHost("127.0.0.1");
brokerDO.setPort(9093);
brokerDO.setTimestamp(1638605696062L);
brokerDO.setMaxAvgBytesIn(0d);
brokerDO.setStatus(0);
brokerDO.setGmtCreate(new Date(1638605696062L));
brokerDO.setGmtModify(new Date(1638605696062L));
return brokerDO;
}
private BrokerMetadata getBrokerMetadata() {
BrokerMetadata brokerMetadata = new BrokerMetadata();
brokerMetadata.setBrokerId(REAL_BROKER_ID_IN_ZK);
brokerMetadata.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
brokerMetadata.setHost("127.0.0.1");
brokerMetadata.setPort(9092);
brokerMetadata.setEndpoints(Arrays.asList(END_POINTS_IN_BROKER));
brokerMetadata.setTimestamp(1638605696062L);
brokerMetadata.setJmxPort(9999);
brokerMetadata.setRack("CY");
brokerMetadata.setVersion("2");
return brokerMetadata;
}
private TopicDiskLocation getTopicDiskLocation() {
TopicDiskLocation topicDiskLocation = new TopicDiskLocation();
topicDiskLocation.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
topicDiskLocation.setBrokerId(1);
topicDiskLocation.setTopicName("testTopic");
topicDiskLocation.setDiskName("disk");
topicDiskLocation.setLeaderPartitions(new ArrayList<>());
topicDiskLocation.setFollowerPartitions(Arrays.asList(0));
topicDiskLocation.setUnderReplicatedPartitions(new ArrayList<>());
topicDiskLocation.setUnderReplicated(false);
return topicDiskLocation;
}
private TopicPartition getTopicPartition() {
TopicPartition topicPartition = new TopicPartition("testTopic", 0);
return topicPartition;
}
private Map<TopicPartition, String> getDiskNameMap() {
Map<TopicPartition, String> diskNameMap = new HashMap<>();
TopicPartition topicPartition = getTopicPartition();
diskNameMap.put(topicPartition, "disk");
return diskNameMap;
}
private PartitionState getPartitionState() {
PartitionState partitionState = new PartitionState();
return partitionState;
}
private Map<String, List<PartitionState>> getStateMap() {
PartitionState partitionState = getPartitionState();
Map<String, List<PartitionState>> stateMap = new HashMap<>();
stateMap.put("string", Arrays.asList(partitionState));
return stateMap;
}
public BrokerMetrics getBrokerMetrics() {
BrokerMetrics brokerMetrics = new BrokerMetrics(1L, 1);
Map<String, Object> metricsMap = new HashMap<>();
metricsMap.put("PartitionCountValue", 100);
metricsMap.put("LeaderCountValue", 100);
brokerMetrics.setMetricsMap(metricsMap);
return brokerMetrics;
}
@Test
public void getBrokerVersionTest() {
String version = "1.4";
Mockito.when(jmxService.getBrokerVersion(Mockito.anyLong(), Mockito.anyInt())).thenReturn(version);
String brokerVersion = brokerService.getBrokerVersion(1L, 1);
Assert.assertNotNull(brokerVersion);
Assert.assertEquals(brokerVersion, version);
}
@Test(description = "根据Cluster和brokerId获取broker的具体信息测试")
public void getBrokerBasicDTO() {
// 测试结果为null
getBrokerBasicDTO2nullTest();
// 获取的brokerMetrics为空
getBrokerBasicDTO2brokerMetricsNullTest();
// 获取的brokerMetrics不为空
getBrokerBasicDTO2brokerMetricsNotNullTest();
}
private void getBrokerBasicDTO2nullTest() {
BrokerBasicDTO result1 = brokerService.getBrokerBasicDTO(null, 1);
Assert.assertNull(result1);
BrokerBasicDTO result2 = brokerService.getBrokerBasicDTO(1L, null);
Assert.assertNull(result2);
BrokerBasicDTO result3 = brokerService.getBrokerBasicDTO(100L, 100);
Assert.assertNull(result3);
}
private void getBrokerBasicDTO2brokerMetricsNullTest() {
BrokerBasicDTO result1 = brokerService.getBrokerBasicDTO(1L, 1);
Assert.assertNotNull(result1);
Assert.assertNull(result1.getPartitionCount());
Assert.assertNull(result1.getLeaderCount());
}
private void getBrokerBasicDTO2brokerMetricsNotNullTest() {
Mockito.when(jmxService.getBrokerMetrics(
Mockito.anyLong(), Mockito.anyInt(), Mockito.anyInt())).thenReturn(getBrokerMetrics());
BrokerBasicDTO result1 = brokerService.getBrokerBasicDTO(1L, 1);
Assert.assertNotNull(result1);
Assert.assertNotNull(result1.getPartitionCount());
Assert.assertNotNull(result1.getLeaderCount());
}
@Test
public void getBrokerTopicLocationTest() {
Map<TopicPartition, String> diskNameMap = getDiskNameMap();
Mockito.when(jmxService.getBrokerTopicLocation(Mockito.any(), Mockito.any())).thenReturn(diskNameMap);
Map<String, List<PartitionState>> stateMap = getStateMap();
Mockito.when(topicService.getTopicPartitionState(Mockito.any(), Mockito.any())).thenReturn(stateMap);
TopicDiskLocation topicDiskLocation = getTopicDiskLocation();
List<TopicDiskLocation> expectedResult = Arrays.asList(topicDiskLocation);
List<TopicDiskLocation> actualResult = brokerService.getBrokerTopicLocation(1L, 1);
Assert.assertEquals(expectedResult.toString(), actualResult.toString());
}
@Test(description = "计算Broker的峰值均值流量测试")
public void calBrokerMaxAvgBytesInTest() {
// 参数异常
calBrokerMaxAvgBytesIn2ParamIllegalTest();
// 获取的指标为空
calBrokerMaxAvgBytesIn2ZeroTest();
// 整个流程
calBrokerMaxAvgBytesIn2Success();
}
private void calBrokerMaxAvgBytesIn2ParamIllegalTest() {
Double result1 = brokerService.calBrokerMaxAvgBytesIn(null, 1, 1, new Date(), new Date());
Assert.assertEquals(result1, -1.0);
Double result2 = brokerService.calBrokerMaxAvgBytesIn(1L, null, 1, new Date(), new Date());
Assert.assertEquals(result2, -1.0);
Double result3 = brokerService.calBrokerMaxAvgBytesIn(1L, 1, null, new Date(), new Date());
Assert.assertEquals(result3, -1.0);
Double result4 = brokerService.calBrokerMaxAvgBytesIn(1L, 1, 1, null, new Date());
Assert.assertEquals(result4, -1.0);
Double result5 = brokerService.calBrokerMaxAvgBytesIn(1L, 1, 1, new Date(), null);
Assert.assertEquals(result5, -1.0);
}
private void calBrokerMaxAvgBytesIn2ZeroTest() {
Double result = brokerService.calBrokerMaxAvgBytesIn(1L, 100, 100, new Date(), new Date());
Assert.assertEquals(result, 0.0);
}
private void calBrokerMaxAvgBytesIn2Success() {
// 此测试需要brokerId=1的broker上有真实的流量
long startTime = 0L;
long endTime = new Date().getTime();
Double result = brokerService.calBrokerMaxAvgBytesIn(
1L, 1, 2, new Date(startTime), new Date(endTime));
Assert.assertTrue(result > 0.0);
}
@Test(description = "获取BrokerMetrics信息测试单个broker")
public void getBrokerMetricsFromJmxTest() {
// 参数错误
getBrokerMetricsFromJmx2ParamIllegalTest();
// 返回为null
getBrokerMetricsFromJmx2nullTest();
// 获取成功
getBrokerMetricsFromJmx2SuccessTest();
}
private void getBrokerMetricsFromJmx2ParamIllegalTest() {
BrokerMetrics result1 = brokerService.getBrokerMetricsFromJmx(null, 1, 200);
Assert.assertNull(result1);
BrokerMetrics result3 = brokerService.getBrokerMetricsFromJmx(1L, 1, null);
Assert.assertNull(result3);
}
private void getBrokerMetricsFromJmx2nullTest() {
BrokerMetrics result1 = brokerService.getBrokerMetricsFromJmx(1L, 1, 200);
Assert.assertNull(result1);
}
private void getBrokerMetricsFromJmx2SuccessTest() {
Mockito.when(jmxService.getBrokerMetrics(
Mockito.anyLong(), Mockito.anyInt(), Mockito.anyInt())).thenReturn(new BrokerMetrics(1L, 1));
BrokerMetrics result1 = brokerService.getBrokerMetricsFromJmx(1L, 1, 200);
Assert.assertNotNull(result1);
Assert.assertEquals(Optional.ofNullable(result1.getClusterId()), Optional.ofNullable(1L));
Assert.assertEquals(Optional.ofNullable(result1.getBrokerId()), Optional.ofNullable(1));
}
@Test(description = "获取BrokerMetrics信息测试多个broker")
public void getBrokerMetricsFromJmxWithMoreBrokersTest() {
Mockito.when(jmxService.getBrokerMetrics(
Mockito.anyLong(), Mockito.anyInt(), Mockito.anyInt())).thenReturn(new BrokerMetrics(1L, 1));
Set<Integer> set = new HashSet<>();
set.add(1);
set.add(2);
set.add(3);
List<BrokerMetrics> result = brokerService.getBrokerMetricsFromJmx(1L, set, 200);
Assert.assertNotNull(result);
Assert.assertTrue(result.stream().allMatch(brokerMetric ->
brokerMetric.getClusterId().equals(1L)));
}
@Test(description = "获取Broker列表信息")
public void getBrokerOverviewListTest() {
// brokerIdSet为空时
getBrokerOverviewList2BrokerIdSetIsNullTest();
// brokerIdSet不为空时
getBrokerOverviewList2BrokerIdSetNotNullTest();
}
private void getBrokerOverviewList2BrokerIdSetIsNullTest() {
List<BrokerOverviewDTO> brokerOverviewList = brokerService.getBrokerOverviewList(1L, null);
Assert.assertFalse(brokerOverviewList.isEmpty());
Assert.assertTrue(brokerOverviewList.stream().allMatch(brokerOverviewDTO ->
brokerOverviewDTO.getPort().equals(9093)));
}
private void getBrokerOverviewList2BrokerIdSetNotNullTest() {
Set<Integer> set = new HashSet<>();
set.add(1);
set.add(2);
List<BrokerOverviewDTO> brokerOverviewList = brokerService.getBrokerOverviewList(1L, set);
Assert.assertFalse(brokerOverviewList.isEmpty());
Assert.assertTrue(brokerOverviewList.stream().allMatch(brokerOverviewDTO ->
brokerOverviewDTO.getPort().equals(9093)));
}
}

View File

@@ -0,0 +1,464 @@
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.ClusterDao;
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.springframework.beans.factory.annotation.Value;
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.*;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.when;
/**
* @author xuguang
* @Date 2021/12/8
*/
public class ClusterServiceTest extends BaseTest {
@Value("${test.phyCluster.id}")
private Long REAL_CLUSTER_ID_IN_MYSQL;
@Value("${test.broker.id1}")
private Integer REAL_BROKER_ID_IN_ZK;
@Value("${test.phyCluster.name}")
private String REAL_PHYSICAL_CLUSTER_NAME;
@Value("${test.ZK.address}")
private String ZOOKEEPER_ADDRESS;
@Value("${test.ZK.bootstrap-servers}")
private String BOOTSTRAP_SERVERS;
private final static String SECURITY_PROTOCOL = "{ \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\\\";\" }";
@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;
@Mock
private OperateRecordService operateRecordService;
@Mock
private ClusterDao clusterDao;
@Mock
private ConsumerService consumerService;
@BeforeMethod
public void setup() {
MockitoAnnotations.initMocks(this);
}
private ClusterDO getClusterDO1() {
ClusterDO clusterDO = new ClusterDO();
clusterDO.setId(3L);
clusterDO.setClusterName(REAL_PHYSICAL_CLUSTER_NAME);
clusterDO.setZookeeper(ZOOKEEPER_ADDRESS);
clusterDO.setBootstrapServers(BOOTSTRAP_SERVERS);
clusterDO.setSecurityProperties(SECURITY_PROTOCOL);
clusterDO.setStatus(1);
clusterDO.setGmtCreate(new Date());
clusterDO.setGmtModify(new Date());
return clusterDO;
}
private ClusterMetricsDO getClusterMetricsDO() {
ClusterMetricsDO clusterMetricsDO = new ClusterMetricsDO();
clusterMetricsDO.setId(10L);
clusterMetricsDO.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
clusterMetricsDO.setMetrics("{\"PartitionNum\":52,\"BrokerNum\":0,\"CreateTime\":1638235221102,\"TopicNum\":2}");
clusterMetricsDO.setGmtCreate(new Date());
return clusterMetricsDO;
}
private ControllerDO getControllerDO() {
ControllerDO controllerDO = new ControllerDO();
controllerDO.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
controllerDO.setBrokerId(REAL_BROKER_ID_IN_ZK);
controllerDO.setHost("127.0.0.1");
controllerDO.setTimestamp(0L);
controllerDO.setVersion(1);
return controllerDO;
}
private Map<Long, Integer> getRegionNum() {
Map<Long, Integer> map = new HashMap<>();
map.put(REAL_CLUSTER_ID_IN_MYSQL, 1);
return map;
}
private Map<Long, Integer> getConsumerGroupNumMap() {
Map<Long, Integer> map = new HashMap<>();
map.put(REAL_CLUSTER_ID_IN_MYSQL, 1);
return map;
}
private ClusterDO getClusterDO() {
ClusterDO clusterDO = new ClusterDO();
clusterDO.setId(3L);
clusterDO.setClusterName(REAL_PHYSICAL_CLUSTER_NAME);
clusterDO.setZookeeper("zzz");
clusterDO.setBootstrapServers(BOOTSTRAP_SERVERS);
clusterDO.setSecurityProperties(SECURITY_PROTOCOL);
clusterDO.setStatus(1);
clusterDO.setGmtCreate(new Date());
clusterDO.setGmtModify(new Date());
return clusterDO;
}
@Test(description = "测试新增物理集群")
public void addNewTest() {
ClusterDO clusterDO = getClusterDO1();
// 测试新增物理集群成功
addNew2SuccessTest(clusterDO);
// 测试新增物理集群时键重复
addNew2DuplicateKeyTest(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 addNew2SuccessTest(ClusterDO clusterDO) {
Mockito.when(operateRecordService.insert(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(1);
Mockito.when(clusterDao.insert(Mockito.any())).thenReturn(1);
ResultStatus result = clusterService.addNew(clusterDO, "admin");
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
}
public void addNew2DuplicateKeyTest(ClusterDO clusterDO) {
Mockito.when(operateRecordService.insert(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenThrow(DuplicateKeyException.class);
ResultStatus result = clusterService.addNew(clusterDO, "admin");
Assert.assertEquals(result.getCode(), ResultStatus.RESOURCE_ALREADY_EXISTED.getCode());
}
@Test(description = "测试修改物理集群")
public void updateById() {
ClusterDO clusterDO = getClusterDO1();
// 测试修改物理集群时参数有误
updateById2ParamIllegalTest(clusterDO);
// 测试修改物理集群时,集群不存在
updateById2ClusterNotExistTest(clusterDO);
}
@Test(description = "测试修改物理集群时,mysqlError")
public void updateById2mysqlErrorTest() {
ClusterDO clusterDO = getClusterDO1();
Mockito.when(clusterDao.getById(Mockito.any())).thenReturn(clusterDO);
Mockito.when(operateRecordService.insert(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(1);
Mockito.when(clusterDao.updateById(Mockito.any())).thenReturn(0);
ResultStatus result1 = clusterService.updateById(clusterDO, "admin");
Assert.assertEquals(result1.getCode(), ResultStatus.MYSQL_ERROR.getCode());
}
@Test(description = "测试修改物理集群成功")
public void updateById2SuccessTest() {
ClusterDO clusterDO = getClusterDO1();
Mockito.when(clusterDao.getById(Mockito.any())).thenReturn(clusterDO);
Mockito.when(clusterDao.updateById(Mockito.any())).thenReturn(1);
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) {
Mockito.when(clusterDao.getById(Mockito.any())).thenReturn(null);
ResultStatus result1 = clusterService.updateById(clusterDO, "admin");
Assert.assertEquals(result1.getCode(), ResultStatus.CLUSTER_NOT_EXIST.getCode());
}
@Test()
public void updateById2ChangeZookeeperForbiddenTest() {
ClusterDO clusterDO = getClusterDO1();
ClusterDO clusterDO1 = getClusterDO();
Mockito.when(clusterDao.getById(Mockito.any())).thenReturn(clusterDO);
ResultStatus result1 = clusterService.updateById(clusterDO1, "admin");
Assert.assertEquals(result1.getCode(), ResultStatus.CHANGE_ZOOKEEPER_FORBIDDEN.getCode());
}
@Test( description = "测试修改物理集群状态")
public void modifyStatusTest() {
ClusterDO clusterDO = getClusterDO1();
// 测试修改物理集群状态时参数有误
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");
Mockito.when(clusterDao.getById(Mockito.any())).thenReturn(null);
Assert.assertEquals(result1.getCode(), ResultStatus.CLUSTER_NOT_EXIST.getCode());
}
public void modifyStatus2SuccessTest(ClusterDO clusterDO) {
Mockito.when(clusterDao.getById(Mockito.any())).thenReturn(clusterDO);
Mockito.when(clusterDao.updateById(Mockito.any())).thenReturn(1);
ResultStatus result1 = clusterService.modifyStatus(clusterDO.getId(), clusterDO.getStatus(), "admin");
Assert.assertEquals(result1.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test(description = "参数needDetail为false")
public void getClusterDetailDTOListWithFalseNeedDetailTest() {
ClusterDO clusterDO = getClusterDO1();
Mockito.when(clusterDao.listAll()).thenReturn(Arrays.asList(clusterDO));
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(description = "参数needDetail为true")
public void getClusterDetailDTOListWithTrueNeedDetailTest() {
ClusterDO clusterDO = getClusterDO1();
Mockito.when(clusterDao.listAll()).thenReturn(Arrays.asList(clusterDO));
Mockito.when(regionService.getRegionNum()).thenReturn(getRegionNum());
Mockito.when(consumerService.getConsumerGroupNumMap(Mockito.any())).thenReturn(getConsumerGroupNumMap());
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(REAL_PHYSICAL_CLUSTER_NAME)));
}
@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(description = "测试获取ClusterNameDTO成功")
public void getClusterName2SuccessTest() {
ClusterDO clusterDO = getClusterDO1();
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);
Mockito.when(clusterDao.getById(Mockito.any())).thenReturn(clusterDO);
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(description = "测试删除集群成功")
public void deleteById2SuccessTest() {
ClusterDO clusterDO = getClusterDO1();
when(regionService.getByClusterId(Mockito.anyLong())).thenReturn(Collections.emptyList());
Mockito.when(operateRecordService.insert(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(1);
Mockito.when(clusterDao.deleteById(Mockito.any())).thenReturn(1);
ResultStatus resultStatus = clusterService.deleteById(clusterDO.getId(), "admin");
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test(description = "测试MYSQL_ERROR")
public void deleteById2MysqlErrorTest() {
when(regionService.getByClusterId(Mockito.anyLong())).thenReturn(Collections.emptyList());
ResultStatus resultStatus = clusterService.deleteById(100L, "admin");
Mockito.when(operateRecordService.insert(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(1);
Mockito.when(clusterDao.deleteById(Mockito.any())).thenReturn(-1);
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());
}
}

View File

@@ -0,0 +1,453 @@
package com.xiaojukeji.kafka.manager.service.service;
import com.alibaba.fastjson.JSON;
import com.xiaojukeji.kafka.manager.common.constant.ConfigConstant;
import com.xiaojukeji.kafka.manager.common.constant.TopicCreationConstant;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.ao.config.CreateTopicConfig;
import com.xiaojukeji.kafka.manager.common.entity.ao.config.CreateTopicElemConfig;
import com.xiaojukeji.kafka.manager.common.entity.ao.config.expert.TopicExpiredConfig;
import com.xiaojukeji.kafka.manager.common.entity.dto.config.ConfigDTO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.ClusterDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.ConfigDO;
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.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author wyc
* @date 2021/12/9
*/
public class ConfigServiceTest extends BaseTest {
@Autowired
private ConfigService configService;
@DataProvider(name = "configDTO")
public Object[][] provideConfigDO() {
ConfigDTO dto = new ConfigDTO();
dto.setConfigKey("key1");
dto.setConfigValue("value1");
dto.setConfigDescription("test");
return new Object[][] {{dto}};
}
public ConfigDTO getConfigDTO() {
ConfigDTO dto = new ConfigDTO();
dto.setConfigKey("key1");
dto.setConfigValue("value1");
dto.setConfigDescription("test");
return dto;
}
@Test(dataProvider = "configDTO")
public void insertTest(ConfigDTO dto) {
// 插入时MySQL错误
insert2MySQLErrorTest(dto);
// 插入成功测试
insert2SuccessTest(dto);
// 插入时,资源已存在测试
insert2ResourceExistedTest(dto);
}
private void insert2SuccessTest(ConfigDTO dto) {
dto.setConfigKey("key1");
ResultStatus result = configService.insert(dto);
Assert.assertEquals(result, ResultStatus.SUCCESS);
}
private void insert2ResourceExistedTest(ConfigDTO dto2) {
ResultStatus result2 = configService.insert(dto2);
Assert.assertEquals(result2, ResultStatus.RESOURCE_ALREADY_EXISTED);
}
private void insert2MySQLErrorTest(ConfigDTO dto) {
dto.setConfigKey(null);
ResultStatus result = configService.insert(dto);
Assert.assertEquals(result, ResultStatus.MYSQL_ERROR);
}
@Test
public void deleteByKeyTest() {
// deleteByKey, key时null
deleteByKey2NullTest();
// deleteByKey, 配置不存在测试
deleteByKey2ConfigNotExistTest();
// deleteByKey, 成功测试
deleteByKey2SuccessTest();
}
private void deleteByKey2NullTest() {
ResultStatus result = configService.deleteByKey(null);
Assert.assertEquals(result, ResultStatus.PARAM_ILLEGAL);
}
private void deleteByKey2SuccessTest() {
ConfigDTO dto = getConfigDTO();
ResultStatus insertResult = configService.insert(dto);
Assert.assertEquals(insertResult, ResultStatus.SUCCESS);
ResultStatus deleteResult = configService.deleteByKey(dto.getConfigKey());
Assert.assertEquals(deleteResult, ResultStatus.SUCCESS);
}
private void deleteByKey2ConfigNotExistTest() {
ResultStatus result = configService.deleteByKey("key");
Assert.assertEquals(result, ResultStatus.CONFIG_NOT_EXIST);
}
@Test(dataProvider = "configDTO")
public void updateByKeyTest(ConfigDTO dto) {
configService.insert(dto);
// updateByKey, 成功测试
updateByKey2SuccessTest(dto);
// updateByKey, 配置不存在测试
updateByKey2ConfigNotExistTest(dto);
}
private void updateByKey2SuccessTest(ConfigDTO dto) {
dto.setConfigValue("newValue");
ResultStatus updateResult = configService.updateByKey(dto);
Assert.assertEquals(updateResult, ResultStatus.SUCCESS);
}
@Test(dataProvider = "configDTO", description = "updateByKey, 配置不存在测试")
private void updateByKey2ConfigNotExistTest(ConfigDTO dto) {
dto.setConfigKey("newKey");
ResultStatus updateResult = configService.updateByKey(dto);
Assert.assertEquals(updateResult, ResultStatus.CONFIG_NOT_EXIST);
}
@Test(dataProvider = "configDTO")
public void updateByKeyTest2(ConfigDTO dto) {
configService.insert(dto);
// updateByKey重载方法成功测试
updateByKey2SuccessTest1(dto);
// updateByKey重载方法资源不存在测试
updateByKey2ConfigNotExistTest1(dto);
}
private void updateByKey2SuccessTest1(ConfigDTO dto) {
String key = dto.getConfigKey();
String value = "newValue";
Assert.assertEquals(configService.updateByKey(key, value), ResultStatus.SUCCESS);
}
private void updateByKey2ConfigNotExistTest1(ConfigDTO dto) {
Assert.assertEquals(configService.updateByKey("key2", "newValue"), ResultStatus.CONFIG_NOT_EXIST);
}
@Test(dataProvider = "configDTO")
public void getByKeyTest(ConfigDTO dto) {
configService.insert(dto);
// getByKey, 成功测试
getByKey2SuccessTest(dto);
// getByKey, 获取失败测试
getByKey2NullTest();
}
private void getByKey2SuccessTest(ConfigDTO dto) {
ConfigDO result = configService.getByKey(dto.getConfigKey());
Assert.assertNotNull(result);
Assert.assertTrue(result.getConfigKey().equals(dto.getConfigKey()) &&
result.getConfigValue().equals(dto.getConfigValue()) &&
result.getConfigDescription().equals(dto.getConfigDescription()));
}
private void getByKey2NullTest() {
Assert.assertNull(configService.getByKey("key2"));
}
@Test(dataProvider = "configDTO")
public void getByKeyTest2(ConfigDTO dto) {
// 需要用到TopicExpiredConfig类
TopicExpiredConfig config = getTopicExpiredConfig();
dto.setConfigValue(JSON.toJSONString(config));
Assert.assertEquals(configService.insert(dto), ResultStatus.SUCCESS);
// getByKey, 成功测试
getByKey2SuccessTest1(dto);
// getByKey, 返回null测试
getByKey2NullTest1(dto);
}
private TopicExpiredConfig getTopicExpiredConfig() {
TopicExpiredConfig config = new TopicExpiredConfig();
List<Long> list = new ArrayList<>();
list.add(1L);
list.add(2L);
config.setIgnoreClusterIdList(list);
return config;
}
private void getByKey2SuccessTest1(ConfigDTO dto) {
TopicExpiredConfig result = configService.getByKey(dto.getConfigKey(), TopicExpiredConfig.class);
Assert.assertEquals(result.toString(), getTopicExpiredConfig().toString());
}
private void getByKey2NullTest1(ConfigDTO dto) {
Assert.assertNull(configService.getByKey("key", TopicExpiredConfig.class));
}
@Test(dataProvider = "configDTO")
public void getArrayByKeyTest(ConfigDTO dto) {
dto.setConfigValue(JSON.toJSONString(getStringArray()));
Assert.assertEquals(configService.insert(dto), ResultStatus.SUCCESS);
// getArrayByKey 成功测试
getArrayByKey2SuccessTest(dto);
// getArrayByKey 返回null测试
getArrayByKey2NullTest();
}
private List<String> getStringArray() {
List<String> list = new ArrayList<>();
list.add("value1");
list.add("value2");
list.add("value3");
return list;
}
private void getArrayByKey2SuccessTest(ConfigDTO dto) {
List<String> result = configService.getArrayByKey(dto.getConfigKey(), String.class);
Assert.assertEquals(result, getStringArray());
}
private void getArrayByKey2NullTest() {
Assert.assertNull(configService.getArrayByKey(null, String.class));
}
@Test(dataProvider = "configDTO", description = "getLongValue, 成功测试")
public void getLongValue2SuccessTest(ConfigDTO dto) {
dto.setConfigValue("100");
Assert.assertEquals(configService.insert(dto), ResultStatus.SUCCESS);
Assert.assertEquals(configService.getLongValue(dto.getConfigKey(), 0L), Long.valueOf(dto.getConfigValue()));
}
@Test(description = "getLongValue, 不存在key返回默认值测试")
public void getLongValue2NotExistTest() {
Assert.assertEquals(configService.getLongValue("key", 100L), Long.valueOf(100L));
}
@Test(dataProvider = "configDTO", description = "getLongValue, 存在key但是value是null")
public void getLongValue2ValueIsNull(ConfigDTO dto) {
dto.setConfigValue(null);
Assert.assertEquals(configService.insert(dto), ResultStatus.SUCCESS);
Assert.assertEquals(configService.getLongValue(dto.getConfigKey(), 100L), Long.valueOf(100L));
}
@Test(dataProvider = "configDTO", description = "listAll, 成功测试")
public void listAll2SuccessTest(ConfigDTO dto) {
Assert.assertEquals(configService.insert(dto), ResultStatus.SUCCESS);
List<ConfigDO> result = configService.listAll();
Assert.assertNotNull(result);
List<ConfigDTO> list = new ArrayList<>();
list.add(dto);
// 判断key字段是否相同
Assert.assertEquals(result.stream().map(ConfigDO::getConfigKey).collect(Collectors.toList()),
list.stream().map(ConfigDTO::getConfigKey).collect(Collectors.toList()));
Assert.assertEquals(result.stream().map(ConfigDO::getConfigValue).collect(Collectors.toList()),
list.stream().map(ConfigDTO::getConfigValue).collect(Collectors.toList()));
}
public CreateTopicConfig getCreateTopicConfig() {
return new CreateTopicConfig();
}
public ConfigDTO getConfigDTO1() {
ConfigDTO dto = new ConfigDTO();
dto.setConfigKey(TopicCreationConstant.INNER_CREATE_TOPIC_CONFIG_KEY);
dto.setConfigValue(JSON.toJSONString(getCreateTopicConfig()));
dto.setConfigDescription("test");
return dto;
}
@Test(description = "getAutoPassedTopicApplyOrderNumPerTask, config表中不存在INNER_CREATE_TOPIC_CONFIG_KEY" +
"对应的记录,返回默认值测试")
public void getAutoPassedTopicApplyOrderNumPerTask2NotExistTest() {
Assert.assertEquals(configService.getAutoPassedTopicApplyOrderNumPerTask(), TopicCreationConstant.DEFAULT_MAX_PASSED_ORDER_NUM_PER_TASK);
}
@Test(description = "getAutoPassedTopicApplyOrderNumPerTask, 查到的记录中记录的maxPassedOrderNumPerTask属性为null测试")
public void getAutoPassedTopicApplyOrderNumPerTask2NullTest() {
configService.insert(getConfigDTO1());
Assert.assertEquals(configService.getAutoPassedTopicApplyOrderNumPerTask(), TopicCreationConstant.DEFAULT_MAX_PASSED_ORDER_NUM_PER_TASK);
}
@Test(description = "getAutoPassedTopicApplyOrderNumPerTask, 查到的记录中记录的maxPassedOrderNumPerTask" +
"比TopicCreationConstant.MAX_PASSED_ORDER_NUM_PER_TASK大时测试")
public void getAutoPassedTopicApplyOrderNumPerTask2BiggerMaxTest() {
ConfigDTO configDTO = getConfigDTO1();
CreateTopicConfig createTopicConfig = getCreateTopicConfig();
createTopicConfig.setMaxPassedOrderNumPerTask(TopicCreationConstant.MAX_PASSED_ORDER_NUM_PER_TASK + 10);
configDTO.setConfigValue(JSON.toJSONString(createTopicConfig));
configService.insert(configDTO);
Assert.assertEquals(configService.getAutoPassedTopicApplyOrderNumPerTask(), TopicCreationConstant.MAX_PASSED_ORDER_NUM_PER_TASK);
}
@Test(description = "getAutoPassedTopicApplyOrderNumPerTask, 查到的记录中记录的maxPassedOrderNumPerTask" +
"比TopicCreationConstant.MAX_PASSED_ORDER_NUM_PER_TASK小时测试")
public void getAutoPassedTopicApplyOrderNumPerTask2SmallerMaxTest() {
ConfigDTO configDTO = getConfigDTO1();
CreateTopicConfig createTopicConfig = getCreateTopicConfig();
int val = TopicCreationConstant.MAX_PASSED_ORDER_NUM_PER_TASK - 10;
createTopicConfig.setMaxPassedOrderNumPerTask(val);
configDTO.setConfigValue(JSON.toJSONString(createTopicConfig));
configService.insert(configDTO);
Assert.assertEquals(configService.getAutoPassedTopicApplyOrderNumPerTask(), Integer.valueOf(val));
}
public CreateTopicElemConfig getCreateTopicElemConfig(Long clusterId) {
CreateTopicElemConfig config = new CreateTopicElemConfig();
config.setClusterId(clusterId);
config.setBrokerIdList(new ArrayList<>());
config.setRegionIdList(new ArrayList<>());
config.setPartitionNum(TopicCreationConstant.DEFAULT_PARTITION_NUM);
config.setReplicaNum(TopicCreationConstant.DEFAULT_REPLICA);
config.setRetentionTimeUnitHour(TopicCreationConstant.DEFAULT_RETENTION_TIME_UNIT_HOUR);
config.setAutoExecMaxPeakBytesInUnitB(TopicCreationConstant.AUTO_EXEC_MAX_BYTES_IN_UNIT_B);
return config;
}
@Test(description = "getCreateTopicConfig, config表中不存在key时测试")
public void getCreateTopicConfig2NotExistKeyTest() {
CreateTopicElemConfig createTopicElemConfig = getCreateTopicElemConfig(10L);
Assert.assertEquals(configService.getCreateTopicConfig(10L, "systemCode").toString(), createTopicElemConfig.toString());
}
@Test(description = "getCreateTopicConfig, value中存在和clusterId一致的记录")
public void getCreateTopicConfig2ExistTest() {
CreateTopicElemConfig createTopicElemConfig = getCreateTopicElemConfig(10L);
createTopicElemConfig.setReplicaNum(4);
List<CreateTopicElemConfig> list = new ArrayList<>();
list.add(createTopicElemConfig);
CreateTopicConfig createTopicConfig = getCreateTopicConfig();
createTopicConfig.setConfigList(list);
ConfigDTO configDTO = getConfigDTO1();
configDTO.setConfigValue(JSON.toJSONString(createTopicConfig));
configService.insert(configDTO);
Assert.assertEquals(configService.getCreateTopicConfig(10L, "systemCode").toString(), createTopicElemConfig.toString());
}
@Test(description = "getCreateTopicConfig, value中不存在和clusterId一致的记录")
public void getCreateTopicConfig2NotExitConfigEleTest() {
CreateTopicElemConfig createTopicElemConfig = getCreateTopicElemConfig(11L);
createTopicElemConfig.setReplicaNum(4);
List<CreateTopicElemConfig> list = new ArrayList<>();
list.add(createTopicElemConfig);
CreateTopicConfig createTopicConfig = getCreateTopicConfig();
createTopicConfig.setConfigList(list);
ConfigDTO configDTO = getConfigDTO1();
configDTO.setConfigValue(JSON.toJSONString(createTopicConfig));
configService.insert(configDTO);
Assert.assertEquals(configService.getCreateTopicConfig(10L, "systemCode").toString(), getCreateTopicElemConfig(10L).toString());
}
public ConfigDTO getConfigDTO2() {
ConfigDTO dto = new ConfigDTO();
dto.setConfigKey(ConfigConstant.KAFKA_CLUSTER_DO_CONFIG_KEY);
dto.setConfigDescription("test");
return dto;
}
public ConfigDO getConfigDO() {
return new ConfigDO();
}
@Test(description = "getClusterDO, config表中不存在ConfigConstant.KAFKA_CLUSTER_DO_CONFIG_KEY这个key")
public void getClusterDO2NotExistKeyTest() {
Assert.assertNull(configService.getClusterDO(10L));
}
@Test(description = "getClusterDO, config表中key对应的value没法解析成ConfigDO测试")
public void getClusterDO2ParseFailTest() {
ConfigDTO configDTO2 = getConfigDTO2();
configDTO2.setConfigValue("value");
configService.insert(configDTO2);
Assert.assertNull(configService.getClusterDO(10L));
}
public List<ClusterDO> getClusterDOList() {
ClusterDO clusterDO1 = new ClusterDO();
clusterDO1.setId(10L);
clusterDO1.setClusterName("test1");
ClusterDO clusterDO2 = new ClusterDO();
clusterDO2.setId(20L);
clusterDO2.setClusterName("test2");
List<ClusterDO> list = new ArrayList<>();
list.add(clusterDO1);
list.add(clusterDO2);
return list;
}
@Test(description = "getClusterDO, 成功查到测试")
public void getClusterDO2SuccessTest() {
ConfigDTO configDTO2 = getConfigDTO2();
List<ClusterDO> clusterDOList = getClusterDOList();
configDTO2.setConfigValue(JSON.toJSONString(clusterDOList));
configService.insert(configDTO2);
ClusterDO clusterDO = new ClusterDO();
clusterDO.setId(20L);
clusterDO.setClusterName("test2");
Assert.assertEquals(configService.getClusterDO(20L), clusterDO);
}
}

View File

@@ -0,0 +1,279 @@
package com.xiaojukeji.kafka.manager.service.service;
import com.xiaojukeji.kafka.manager.common.bizenum.OffsetLocationEnum;
import com.xiaojukeji.kafka.manager.common.entity.Result;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.ao.PartitionOffsetDTO;
import com.xiaojukeji.kafka.manager.common.entity.ao.consumer.ConsumeDetailDTO;
import com.xiaojukeji.kafka.manager.common.entity.ao.consumer.ConsumerGroup;
import com.xiaojukeji.kafka.manager.common.entity.ao.consumer.ConsumerGroupSummary;
import com.xiaojukeji.kafka.manager.common.entity.pojo.ClusterDO;
import com.xiaojukeji.kafka.manager.service.config.BaseTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 测试消费组消费情况需要保证集群中存在消费组
* @author xuguang
* @Date 2021/12/23
*/
public class ConsumerServiceTest extends BaseTest {
@Value("${test.phyCluster.id}")
private Long REAL_CLUSTER_ID_IN_MYSQL;
/**
* 集群共包括三个broker:1,2,3, 该topic 1分区 1副本因子在broker1上
*/
@Value("${test.topic.name1}")
private String REAL_TOPIC1_IN_ZK;
/**
* 集群共包括三个broker:1,2,3, 该topic 2分区 3副本因子在broker1,2,3上
*/
private final static String REAL_TOPIC2_IN_ZK = "xgTest";
private final static String INVALID_TOPIC = "xxxxxx";
@Value("${test.consumer-group}")
private String REAL_CONSUMER_GROUP_NAME;
private final static String INVALID_CONSUMER_GROUP_NAME = "xxxxxxxx";
@Value("${test.phyCluster.name}")
private String REAL_PHYSICAL_CLUSTER_NAME;
@Value("${test.ZK.address}")
private String ZOOKEEPER_ADDRESS;
@Value("${test.ZK.bootstrap-servers}")
private String BOOTSTRAP_SERVERS;
private String SECURITY_PROTOCOL = "{ \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\\\";\" }";
@Autowired
private ConsumerService consumerService;
private ClusterDO getClusterDO() {
ClusterDO clusterDO = new ClusterDO();
clusterDO.setId(REAL_CLUSTER_ID_IN_MYSQL);
clusterDO.setClusterName(REAL_PHYSICAL_CLUSTER_NAME);
clusterDO.setZookeeper(ZOOKEEPER_ADDRESS);
clusterDO.setBootstrapServers(BOOTSTRAP_SERVERS);
clusterDO.setSecurityProperties(SECURITY_PROTOCOL);
clusterDO.setStatus(1);
clusterDO.setGmtCreate(new Date());
clusterDO.setGmtModify(new Date());
return clusterDO;
}
private ConsumerGroup getConsumerGroup() {
return new ConsumerGroup(
REAL_CLUSTER_ID_IN_MYSQL,
REAL_CONSUMER_GROUP_NAME,
OffsetLocationEnum.BROKER);
}
private PartitionOffsetDTO getPartitionOffsetDTO() {
PartitionOffsetDTO partitionOffsetDTO = new PartitionOffsetDTO();
partitionOffsetDTO.setOffset(0L);
partitionOffsetDTO.setPartitionId(0);
return partitionOffsetDTO;
}
// @Test(description = "测试获取消费组列表")
// 因定时任务暂时无法跑通
public void getConsumerGroupListTest() {
List<ConsumerGroup> consumerGroupList = consumerService.getConsumerGroupList(REAL_CLUSTER_ID_IN_MYSQL);
Assert.assertFalse(consumerGroupList.isEmpty());
Assert.assertTrue(consumerGroupList.stream().allMatch(consumerGroup ->
consumerGroup.getClusterId().equals(REAL_CLUSTER_ID_IN_MYSQL)));
}
// @Test(description = "测试查询消费Topic的消费组")
// 因定时任务暂时无法跑通
public void getConsumerGroupListWithTopicTest() {
List<ConsumerGroup> consumerGroupList = consumerService.getConsumerGroupList(
REAL_CLUSTER_ID_IN_MYSQL,
REAL_TOPIC1_IN_ZK
);
Assert.assertFalse(consumerGroupList.isEmpty());
Assert.assertTrue(consumerGroupList.stream().allMatch(consumerGroup ->
consumerGroup.getClusterId().equals(REAL_CLUSTER_ID_IN_MYSQL)));
}
// @Test(description = "测试获取消费Topic的消费组概要信息")
// 因定时任务暂时无法跑通
public void getConsumerGroupSummariesTest() {
// result is empty
getConsumerGroupSummaries2EmptyTest();
// result is not empty
getConsumerGroupSummaries2NotEmptyTest();
}
private void getConsumerGroupSummaries2EmptyTest() {
List<ConsumerGroupSummary> consumerGroupSummaries = consumerService.getConsumerGroupSummaries(
REAL_CLUSTER_ID_IN_MYSQL,
INVALID_TOPIC
);
Assert.assertTrue(consumerGroupSummaries.isEmpty());
}
private void getConsumerGroupSummaries2NotEmptyTest() {
List<ConsumerGroupSummary> consumerGroupSummaries = consumerService.getConsumerGroupSummaries(
REAL_CLUSTER_ID_IN_MYSQL,
REAL_TOPIC1_IN_ZK
);
Assert.assertFalse(consumerGroupSummaries.isEmpty());
}
@Test(description = "测试查询消费详情")
public void getConsumeDetail() {
// result is empty
getConsumeDetail2Empty();
// result is not empty
getConsumeDetail2NotEmpty();
}
private void getConsumeDetail2Empty() {
ClusterDO clusterDO = getClusterDO();
List<ConsumeDetailDTO> consumeDetail1 =
consumerService.getConsumeDetail(clusterDO, INVALID_TOPIC, null);
Assert.assertTrue(consumeDetail1.isEmpty());
ConsumerGroup consumerGroup = getConsumerGroup();
consumerGroup.setOffsetStoreLocation(null);
List<ConsumeDetailDTO> consumeDetail2 =
consumerService.getConsumeDetail(clusterDO, REAL_TOPIC1_IN_ZK, consumerGroup);
Assert.assertTrue(consumeDetail2.isEmpty());
}
private void getConsumeDetail2NotEmpty() {
ClusterDO clusterDO = getClusterDO();
ConsumerGroup consumerGroup = getConsumerGroup();
List<ConsumeDetailDTO> consumeDetail1 =
consumerService.getConsumeDetail(clusterDO, REAL_TOPIC1_IN_ZK, consumerGroup);
Assert.assertFalse(consumeDetail1.isEmpty());
}
@Test(description = "测试获取消费组消费的Topic列表")
public void getConsumerGroupConsumedTopicListTest() {
// result is empty
getConsumerGroupConsumedTopicList2Empty();
// result is not empty
// 因定时任务暂时无法跑通
// getConsumerGroupConsumedTopicList2NotEmpty();
}
private void getConsumerGroupConsumedTopicList2Empty() {
List<String> list = consumerService.getConsumerGroupConsumedTopicList(
null,
null,
null);
Assert.assertTrue(list.isEmpty());
}
private void getConsumerGroupConsumedTopicList2NotEmpty() {
List<String> list = consumerService.getConsumerGroupConsumedTopicList(
REAL_CLUSTER_ID_IN_MYSQL,
REAL_CONSUMER_GROUP_NAME,
"broker");
Assert.assertFalse(list.isEmpty());
}
@Test(description = "测试获取消费者offset")
public void getConsumerOffsetTest() {
// result is null
getConsumerOffset2NullTest();
// result is not null
getConsumerOffset2NotNullTest();
}
private void getConsumerOffset2NullTest() {
Map<Integer, Long> consumerOffset1 = consumerService.getConsumerOffset(null, null, null);
Assert.assertNull(consumerOffset1);
ClusterDO clusterDO = getClusterDO();
ConsumerGroup consumerGroup = getConsumerGroup();
consumerGroup.setOffsetStoreLocation(null);
Map<Integer, Long> consumerOffset2 = consumerService.getConsumerOffset(
clusterDO,
REAL_TOPIC1_IN_ZK,
consumerGroup
);
Assert.assertNull(consumerOffset2);
}
private void getConsumerOffset2NotNullTest() {
ClusterDO clusterDO = getClusterDO();
ConsumerGroup consumerGroup = getConsumerGroup();
Map<Integer, Long> consumerOffset = consumerService.getConsumerOffset(
clusterDO,
REAL_TOPIC1_IN_ZK,
consumerGroup
);
Assert.assertNotNull(consumerOffset);
Assert.assertFalse(consumerOffset.isEmpty());
}
@Test(description = "测试获取每个集群消费组的个数")
public void getConsumerGroupNumMapTest() {
ClusterDO clusterDO = getClusterDO();
Map<Long, Integer> map = consumerService.getConsumerGroupNumMap(Arrays.asList(clusterDO));
Assert.assertFalse(map.isEmpty());
Assert.assertTrue(clusterDO.getId() >= 0);
}
@Test(description = "验证消费组是否存在")
public void checkConsumerGroupExistTest() {
// 不存在
checkConsumerGroupExist2FalseTest();
// 存在
// 因定时任务暂时无法跑通
// checkConsumerGroupExist2TrueTest();
}
private void checkConsumerGroupExist2FalseTest() {
boolean result = consumerService.checkConsumerGroupExist(
OffsetLocationEnum.BROKER,
REAL_CLUSTER_ID_IN_MYSQL,
REAL_TOPIC1_IN_ZK,
INVALID_CONSUMER_GROUP_NAME
);
Assert.assertFalse(result);
}
private void checkConsumerGroupExist2TrueTest() {
boolean result = consumerService.checkConsumerGroupExist(
OffsetLocationEnum.BROKER,
REAL_CLUSTER_ID_IN_MYSQL,
REAL_TOPIC1_IN_ZK,
REAL_CONSUMER_GROUP_NAME
);
Assert.assertTrue(result);
}
@Test(description = "测试重置offset")
public void resetConsumerOffsetTest() {
ClusterDO clusterDO = getClusterDO();
ConsumerGroup consumerGroup = getConsumerGroup();
PartitionOffsetDTO partitionOffsetDTO1 = getPartitionOffsetDTO();
List<Result> results = consumerService.resetConsumerOffset(
clusterDO,
REAL_TOPIC1_IN_ZK,
consumerGroup,
Arrays.asList(partitionOffsetDTO1)
);
Assert.assertFalse(results.isEmpty());
Assert.assertTrue(results.stream().allMatch(result ->
result.getCode() == ResultStatus.SUCCESS.getCode()));
}
}

View File

@@ -0,0 +1,246 @@
package com.xiaojukeji.kafka.manager.service.service;
import com.xiaojukeji.kafka.manager.common.entity.ao.config.expert.RegionTopicHotConfig;
import com.xiaojukeji.kafka.manager.common.entity.ao.config.expert.TopicExpiredConfig;
import com.xiaojukeji.kafka.manager.common.entity.ao.config.expert.TopicInsufficientPartitionConfig;
import com.xiaojukeji.kafka.manager.common.entity.ao.expert.TopicInsufficientPartition;
import com.xiaojukeji.kafka.manager.common.entity.ao.expert.TopicRegionHot;
import com.xiaojukeji.kafka.manager.common.entity.pojo.ClusterDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.TopicExpiredDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.TopicMetricsDO;
import com.xiaojukeji.kafka.manager.dao.TopicMetricsDao;
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.springframework.beans.factory.annotation.Value;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import javax.validation.constraints.AssertTrue;
import java.util.*;
/**
* @author wyc
* @date 2021/12/27
*/
public class ExpertServiceTest extends BaseTest {
@Value("${test.phyCluster.id}")
private Long REAL_CLUSTER_ID_IN_MYSQL;
@Value("${test.topic.name4}")
private String REAL_TOPIC_IN_ZK;
private final static Set<Integer> REAL_BROKER_ID_SET = new HashSet<>();
private String metrics = "{\"TotalFetchRequestsPerSecFiveMinuteRate\":4.132236103122026,\"BytesRejectedPerSecFiveMinuteRate\":0.0,\"TotalFetchRequestsPerSecFifteenMinuteRate\":1.5799208507558833,\"ProduceTotalTimeMs98thPercentile\":0.0,\"MessagesInPerSecMeanRate\":0.0,\"ProduceTotalTimeMs75thPercentile\":0.0,\"ProduceTotalTimeMs99thPercentile\":0.0,\"TotalProduceRequestsPerSecOneMinuteRate\":0.0,\"FailedProduceRequestsPerSecFifteenMinuteRate\":0.0,\"BytesInPerSecMeanRate\":0.0,\"TotalProduceRequestsPerSecFiveMinuteRate\":0.0,\"FetchConsumerTotalTimeMs999thPercentile\":0.0,\"FetchConsumerTotalTimeMs98thPercentile\":0.0,\"FetchConsumerTotalTimeMsMean\":0.0,\"FetchConsumerTotalTimeMs99thPercentile\":0.0,\"FailedFetchRequestsPerSecFifteenMinuteRate\":0.0,\"MessagesInPerSecFiveMinuteRate\":0.0,\"RequestHandlerAvgIdlePercentOneMinuteRate\":0.999221766772746,\"ProduceTotalTimeMsMean\":0.0,\"BytesInPerSecFiveMinuteRate\":0.0,\"FailedProduceRequestsPerSecMeanRate\":0.0,\"FailedFetchRequestsPerSecMeanRate\":0.0,\"FailedProduceRequestsPerSecFiveMinuteRate\":0.0,\"BytesOutPerSecFifteenMinuteRate\":0.0,\"BytesInPerSecOneMinuteRate\":100.0,\"BytesOutPerSecFiveMinuteRate\":0.0,\"HealthScore\":90,\"FailedFetchRequestsPerSecOneMinuteRate\":0.0,\"MessagesInPerSecOneMinuteRate\":0.0,\"BytesRejectedPerSecFifteenMinuteRate\":0.0,\"FailedFetchRequestsPerSecFiveMinuteRate\":0.0,\"RequestHandlerAvgIdlePercentFiveMinuteRate\":0.999803118809842,\"BytesOutPerSecOneMinuteRate\":0.0,\"ResponseQueueSizeValue\":0,\"MessagesInPerSecFifteenMinuteRate\":0.0,\"TotalProduceRequestsPerSecMeanRate\":0.0,\"BytesRejectedPerSecMeanRate\":0.0,\"TotalFetchRequestsPerSecMeanRate\":1.2674449706628523,\"NetworkProcessorAvgIdlePercentValue\":1.0,\"TotalFetchRequestsPerSecOneMinuteRate\":10.457259856316893,\"BytesInPerSecFifteenMinuteRate\":0.0,\"BytesOutPerSecMeanRate\":0.0,\"TotalProduceRequestsPerSecFifteenMinuteRate\":0.0,\"FetchConsumerTotalTimeMs50thPercentile\":0.0,\"RequestHandlerAvgIdlePercentFifteenMinuteRate\":0.9999287809186348,\"FetchConsumerTotalTimeMs95thPercentile\":0.0,\"FailedProduceRequestsPerSecOneMinuteRate\":0.0,\"CreateTime\":1638792321071,\"FetchConsumerTotalTimeMs75thPercentile\":0.0,\"ProduceTotalTimeMs999thPercentile\":0.0,\"RequestQueueSizeValue\":0,\"ProduceTotalTimeMs50thPercentile\":0.0,\"BytesRejectedPerSecOneMinuteRate\":0.0,\"RequestHandlerAvgIdlePercentMeanRate\":0.9999649184090593,\"ProduceTotalTimeMs95thPercentile\":0.0}";
static {
REAL_BROKER_ID_SET.add(1);
REAL_BROKER_ID_SET.add(2);
}
@Autowired
@InjectMocks
private ExpertService expertService;
@Mock
private ConfigService configService;
@Mock
private RegionService regionService;
@Mock
private ClusterService clusterService;
@Mock
private TopicManagerService topicManagerService;
@Autowired
private TopicMetricsDao topicMetricsDao;
@BeforeMethod
public void init() {
MockitoAnnotations.initMocks(this);
}
private ClusterDO getClusterDO() {
ClusterDO clusterDO = new ClusterDO();
clusterDO.setId(REAL_CLUSTER_ID_IN_MYSQL);
return clusterDO;
}
private RegionTopicHotConfig getRegionTopicHotConfig() {
RegionTopicHotConfig config = new RegionTopicHotConfig();
config.setMaxDisPartitionNum(-1);// 为了通过检测
// ignoreClusterIdList字段不用设置
config.setMinTopicBytesInUnitB(-1L);// 为了通过检测
return config;
}
private TopicRegionHot getTopicRegionHot() {
ClusterDO clusterDO = getClusterDO();
TopicRegionHot hotTopic = new TopicRegionHot(clusterDO, REAL_TOPIC_IN_ZK, null, new HashMap<>());
return hotTopic;
}
private Map<String, Set<Integer>> getTopicNameRegionBrokerIdMap() {
Map<String, Set<Integer>> map = new HashMap<>();
map.put(REAL_TOPIC_IN_ZK, REAL_BROKER_ID_SET);
return map;
}
private Map<String, List<Double>> getMaxAvgBytesInMap() {
Map<String, List<Double>> map = new HashMap<>();
map.put(REAL_TOPIC_IN_ZK, new ArrayList<>());
return map;
}
private TopicInsufficientPartitionConfig getTopicInsufficientPartitionConfig() {
TopicInsufficientPartitionConfig config = new TopicInsufficientPartitionConfig();
config.setMinTopicBytesInUnitB(-1L);// 为了通过测试
config.setMaxBytesInPerPartitionUnitB(10L);// 为了通过测试
return config;
}
private TopicExpiredDO getTopicExpiredDO() {
TopicExpiredDO topicExpiredDO = new TopicExpiredDO();
topicExpiredDO.setTopicName(REAL_TOPIC_IN_ZK);
topicExpiredDO.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
return topicExpiredDO;
}
private TopicExpiredConfig getTopicExpiredConfig() {
TopicExpiredConfig config = new TopicExpiredConfig();
config.setIgnoreClusterIdList(new ArrayList<>());
return config;
}
private TopicMetricsDO getTopicMetricsDO() {
TopicMetricsDO topicMetricsDO = new TopicMetricsDO();
topicMetricsDO.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
topicMetricsDO.setTopicName(REAL_TOPIC_IN_ZK);
topicMetricsDO.setMetrics(metrics);
return topicMetricsDO;
}
private TopicInsufficientPartition getTopicInsufficientPartition() {
ClusterDO clusterDO = getClusterDO();
return new TopicInsufficientPartition(
clusterDO,
REAL_TOPIC_IN_ZK,
null,
null,
null,
null,
new ArrayList<>(REAL_BROKER_ID_SET)
);
}
@Test
public void getRegionHotTopicsTest() {
// 返回空集合测试
getRegionHotTopics2EmptyTest();
getRegionHotTopics2SuccessTest();
}
private void getRegionHotTopics2EmptyTest() {
Mockito.when(configService.getByKey(Mockito.anyString(), Mockito.any())).thenReturn(null);
Assert.assertTrue(expertService.getRegionHotTopics().isEmpty());
}
private void getRegionHotTopics2SuccessTest() {
RegionTopicHotConfig config = getRegionTopicHotConfig();
Mockito.when(configService.getByKey(Mockito.anyString(), Mockito.any())).thenReturn(config);
ClusterDO clusterDO = getClusterDO();
List<ClusterDO> clusterDOList = new ArrayList<>();
clusterDOList.add(clusterDO);
Mockito.when(clusterService.list()).thenReturn(clusterDOList);
Map<String, Set<Integer>> map = getTopicNameRegionBrokerIdMap();
Mockito.when(regionService.getTopicNameRegionBrokerIdMap(Mockito.anyLong())).thenReturn(map);
Assert.assertTrue(expertService.getRegionHotTopics().stream().allMatch(hotTopic -> hotTopic.getClusterDO().getId().equals(clusterDO.getId())));
}
@Test
public void getPartitionInsufficientTopicsTest() {
// 返回空集合测试
getPartitionInsufficientTopic2EmptyTest();
// 成功测试
getPartitionInsufficientTopicsSuccessTest();
}
private void getPartitionInsufficientTopic2EmptyTest() {
TopicInsufficientPartitionConfig config = getTopicInsufficientPartitionConfig();
Mockito.when(configService.getByKey(Mockito.anyString(), Mockito.any())).thenReturn(config);
Mockito.when(clusterService.list()).thenReturn(new ArrayList<>());
Assert.assertTrue(expertService.getPartitionInsufficientTopics().isEmpty());
}
private void getPartitionInsufficientTopicsSuccessTest() {
// 先向数据库中插入
List<TopicMetricsDO> topicMetricsDOList = new ArrayList<>();
topicMetricsDOList.add(getTopicMetricsDO());
topicMetricsDao.batchAdd(topicMetricsDOList);
TopicInsufficientPartitionConfig config = getTopicInsufficientPartitionConfig();
Mockito.when(configService.getByKey(Mockito.anyString(), Mockito.any())).thenReturn(config);
ClusterDO clusterDO = getClusterDO();
List<ClusterDO> clusterDOList = new ArrayList<>();
clusterDOList.add(clusterDO);
Mockito.when(clusterService.list()).thenReturn(clusterDOList);
Map<String, Set<Integer>> map = getTopicNameRegionBrokerIdMap();
Mockito.when(regionService.getTopicNameRegionBrokerIdMap(Mockito.anyLong())).thenReturn(map);
Map<String, List<Double>> maxAvgBytesInMap = getMaxAvgBytesInMap();
Mockito.when(topicManagerService.getTopicMaxAvgBytesIn(Mockito.anyLong(), Mockito.anyInt(), Mockito.anyDouble())).thenReturn(maxAvgBytesInMap);
TopicInsufficientPartition expectResult = getTopicInsufficientPartition();
Assert.assertTrue(expertService.getPartitionInsufficientTopics().stream().allMatch(topic -> topic.getClusterDO().getId().equals(expectResult.getClusterDO().getId()) &&
topic.getTopicName().equals(expectResult.getTopicName())));
}
@Test
public void getExpiredTopicsTest() {
// 返回空集合测试
getExpiredTopics2EmptyTest();
// 成功测试
getExpiredTopics2SuccessTest();
}
private void getExpiredTopics2EmptyTest() {
TopicExpiredConfig topicExpiredConfig = getTopicExpiredConfig();
Mockito.when(configService.getByKey(Mockito.anyString(), Mockito.any())).thenReturn(topicExpiredConfig);
Mockito.when(topicManagerService.getExpiredTopics(Mockito.anyInt())).thenReturn(new ArrayList<>());
Assert.assertTrue(expertService.getExpiredTopics().isEmpty());
}
public void getExpiredTopics2SuccessTest() {
TopicExpiredConfig topicExpiredConfig = getTopicExpiredConfig();
Mockito.when(configService.getByKey(Mockito.anyString(), Mockito.any())).thenReturn(topicExpiredConfig);
TopicExpiredDO topicExpiredDO = getTopicExpiredDO();
List<TopicExpiredDO> topicExpiredDOList = new ArrayList<>();
topicExpiredDOList.add(topicExpiredDO);
Mockito.when(topicManagerService.getExpiredTopics(Mockito.anyInt())).thenReturn(topicExpiredDOList);
Assert.assertTrue(expertService.getExpiredTopics().stream().allMatch(expiredDO -> expiredDO.getClusterId().equals(topicExpiredDO.getClusterId()) &&
expiredDO.getTopicName().equals(topicExpiredDO.getTopicName())));
}
}

View File

@@ -0,0 +1,393 @@
package com.xiaojukeji.kafka.manager.service.service;
import com.xiaojukeji.kafka.manager.common.bizenum.KafkaClientEnum;
import com.xiaojukeji.kafka.manager.common.constant.KafkaMetricsCollections;
import com.xiaojukeji.kafka.manager.common.entity.ao.PartitionAttributeDTO;
import com.xiaojukeji.kafka.manager.common.entity.metrics.BrokerMetrics;
import com.xiaojukeji.kafka.manager.common.entity.metrics.TopicMetrics;
import com.xiaojukeji.kafka.manager.common.zookeeper.znode.brokers.PartitionState;
import com.xiaojukeji.kafka.manager.service.config.BaseTest;
import org.apache.kafka.common.TopicPartition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.*;
/**
* @author xuguang
* @Date 2021/12/14
*/
public class JmxServiceTest extends BaseTest {
/**
* 集群共包括三个broker:1,2,3, 该topic 1分区 1副本因子在broker1上
*/
@Value("${test.topic.name1}")
private String REAL_TOPIC1_IN_ZK;
/**
* 集群共包括三个broker:1,2,3, 该topic 2分区 3副本因子在broker1,2,3上
*/
@Value("${test.topic.name2}")
private String REAL_TOPIC2_IN_ZK;
private final static String INVALID_TOPIC = "xxxxx";
private final static String ZK_DEFAULT_TOPIC = "_consumer_offsets";
private final static String NO_OFFSET_CHANGE_TOPIC_IN_ZK = "NoOffsetChangeTopic";
@Value("${test.phyCluster.id}")
private Long REAL_CLUSTER_ID_IN_MYSQL;
@Value("${test.broker.id1}")
private Integer REAL_BROKER_ID_IN_ZK;
private final static Integer INVALID_BROKER_ID = -1;
private final static Long INVALID_CLUSTER_ID = -1L;
private final static Integer INVALID_PARTITION_ID = -1;
@Value("${test.client-id}")
private String CLIENT_ID;
private final static Integer INVALID_METRICS_CODE = -1;
@Autowired
private JmxService jmxService;
private PartitionState getPartitionState() {
PartitionState partitionState = new PartitionState();
partitionState.setPartitionId(0);
partitionState.setLeader(2);
return partitionState;
}
@Test
public void getBrokerMetricsTest() {
// 结果为空
getBrokerMetrics2NullTest();
// mbeanV2ListEmpty
getBrokerMetrics2mbeanV2ListEmptyTest();
// 获取成功
getBrokerMetrics2SuccessTest();
}
private void getBrokerMetrics2NullTest() {
BrokerMetrics brokerMetrics1 = jmxService.getBrokerMetrics(null, null, null);
Assert.assertNull(brokerMetrics1);
BrokerMetrics brokerMetrics2 = jmxService.getBrokerMetrics(
REAL_CLUSTER_ID_IN_MYSQL,
INVALID_BROKER_ID,
KafkaMetricsCollections.BROKER_ANALYSIS_METRICS);
Assert.assertNull(brokerMetrics2);
}
private void getBrokerMetrics2mbeanV2ListEmptyTest() {
BrokerMetrics brokerMetrics2 = jmxService.getBrokerMetrics(
REAL_CLUSTER_ID_IN_MYSQL,
REAL_BROKER_ID_IN_ZK,
-1);
Assert.assertNotNull(brokerMetrics2);
Assert.assertEquals(brokerMetrics2.getClusterId(), REAL_CLUSTER_ID_IN_MYSQL);
Assert.assertEquals(brokerMetrics2.getBrokerId(), REAL_BROKER_ID_IN_ZK);
Assert.assertTrue(brokerMetrics2.getMetricsMap().isEmpty());
}
private void getBrokerMetrics2SuccessTest() {
BrokerMetrics brokerMetrics2 = jmxService.getBrokerMetrics(
REAL_CLUSTER_ID_IN_MYSQL,
REAL_BROKER_ID_IN_ZK,
KafkaMetricsCollections.BROKER_ANALYSIS_METRICS);
Assert.assertNotNull(brokerMetrics2);
Assert.assertEquals(brokerMetrics2.getClusterId(), REAL_CLUSTER_ID_IN_MYSQL);
Assert.assertEquals(brokerMetrics2.getBrokerId(), REAL_BROKER_ID_IN_ZK);
Assert.assertFalse(brokerMetrics2.getMetricsMap().isEmpty());
}
@Test
public void getTopicMetricsWithBrokerIdTest() {
// 结果为空
getTopicMetricsWithBrokerId2nullTest();
// 获取的metrics为空
getTopicMetricsWithBrokerId2MetricsIsNullTest();
// 获取指标成功
getTopicMetricsWithBrokerId2SuccessTest();
}
private void getTopicMetricsWithBrokerId2nullTest() {
TopicMetrics topicMetrics1 = jmxService.getTopicMetrics(
REAL_CLUSTER_ID_IN_MYSQL,
REAL_BROKER_ID_IN_ZK,
REAL_TOPIC1_IN_ZK,
-1, true);
Assert.assertNull(topicMetrics1);
TopicMetrics topicMetrics2 = jmxService.getTopicMetrics(
REAL_CLUSTER_ID_IN_MYSQL,
INVALID_BROKER_ID,
REAL_TOPIC1_IN_ZK,
KafkaMetricsCollections.BROKER_ANALYSIS_METRICS
, true);
Assert.assertNull(topicMetrics2);
}
private void getTopicMetricsWithBrokerId2MetricsIsNullTest() {
// brokerId为3不在该topic下
TopicMetrics topicMetrics2 = jmxService.getTopicMetrics(
REAL_CLUSTER_ID_IN_MYSQL,
3,
REAL_TOPIC1_IN_ZK,
KafkaMetricsCollections.BROKER_ANALYSIS_METRICS
, true);
Assert.assertNotNull(topicMetrics2);
Assert.assertEquals(topicMetrics2.getClusterId(), REAL_CLUSTER_ID_IN_MYSQL);
Assert.assertEquals(topicMetrics2.getTopicName(), REAL_TOPIC1_IN_ZK);
Assert.assertTrue(topicMetrics2.getMetricsMap().isEmpty());
}
private void getTopicMetricsWithBrokerId2SuccessTest() {
TopicMetrics topicMetrics2 = jmxService.getTopicMetrics(
REAL_CLUSTER_ID_IN_MYSQL,
REAL_BROKER_ID_IN_ZK,
REAL_TOPIC1_IN_ZK,
KafkaMetricsCollections.TOPIC_REQUEST_TIME_METRICS_TO_DB
, true);
Assert.assertNotNull(topicMetrics2);
Assert.assertEquals(topicMetrics2.getClusterId(), REAL_CLUSTER_ID_IN_MYSQL);
Assert.assertEquals(topicMetrics2.getTopicName(), REAL_TOPIC1_IN_ZK);
Assert.assertFalse(topicMetrics2.getMetricsMap().isEmpty());
}
@Test
public void getTopicMetricsWithoutBrokerId() {
// 返回为空
getTopicMetricsWithoutBrokerId2Null();
// add
getTopicMetricsWithoutBrokerId2Add();
// max
getTopicMetricsWithoutBrokerId2Max();
}
private void getTopicMetricsWithoutBrokerId2Null() {
TopicMetrics topicMetrics = jmxService.getTopicMetrics(
REAL_CLUSTER_ID_IN_MYSQL,
INVALID_TOPIC,
KafkaMetricsCollections.TOPIC_METRICS_TO_DB
, true);
Assert.assertNull(topicMetrics);
}
private void getTopicMetricsWithoutBrokerId2Add() {
TopicMetrics topicMetrics = jmxService.getTopicMetrics(
REAL_CLUSTER_ID_IN_MYSQL,
REAL_TOPIC1_IN_ZK,
KafkaMetricsCollections.TOPIC_REQUEST_TIME_METRICS_TO_DB
, true);
Assert.assertNotNull(topicMetrics);
Assert.assertNotNull(topicMetrics.getBrokerMetricsList());
Assert.assertNotNull(topicMetrics.getMetricsMap());
}
private void getTopicMetricsWithoutBrokerId2Max() {
TopicMetrics topicMetrics = jmxService.getTopicMetrics(
REAL_CLUSTER_ID_IN_MYSQL,
REAL_TOPIC2_IN_ZK,
KafkaMetricsCollections.TOPIC_REQUEST_TIME_METRICS_TO_DB
, false);
Assert.assertNotNull(topicMetrics);
Assert.assertNotNull(topicMetrics.getBrokerMetricsList());
Assert.assertNotNull(topicMetrics.getMetricsMap());
}
@Test(description = "测试获取集群下所有topic指标")
public void getTopicMetricsList() {
List<TopicMetrics> topicMetrics = jmxService.getTopicMetrics(
REAL_CLUSTER_ID_IN_MYSQL,
KafkaMetricsCollections.TOPIC_REQUEST_TIME_METRICS_TO_DB
, false);
Assert.assertFalse(topicMetrics.isEmpty());
Assert.assertTrue(topicMetrics.stream().allMatch(topicMetric ->
topicMetric.getClusterId().equals(REAL_CLUSTER_ID_IN_MYSQL)));
}
@Test(description = "测试获取broker版本")
public void getBrokerVersion() {
// 结果为空
getBrokerVersion2Empty();
// 结果不为空
getBrokerVersion2NotEmpty();
}
private void getBrokerVersion2Empty() {
String brokerVersion = jmxService.getBrokerVersion(
REAL_CLUSTER_ID_IN_MYSQL,
INVALID_BROKER_ID);
Assert.assertEquals(brokerVersion, "");
}
private void getBrokerVersion2NotEmpty() {
String brokerVersion = jmxService.getBrokerVersion(
REAL_CLUSTER_ID_IN_MYSQL,
REAL_BROKER_ID_IN_ZK);
Assert.assertNotEquals(brokerVersion, "");
}
@Test(description = "获取客户端限流信息")
public void getTopicAppThrottleTest() {
// 结果为0
getTopicAppThrottle2ZeroTest();
// 结果不为0
// getTopicAppThrottle2NotZeroTest();
}
private void getTopicAppThrottle2ZeroTest() {
double topicAppThrottle = jmxService.getTopicAppThrottle(
REAL_CLUSTER_ID_IN_MYSQL,
INVALID_BROKER_ID,
"1",
KafkaClientEnum.FETCH_CLIENT);
Assert.assertEquals(topicAppThrottle, 0.0d);
}
private void getTopicAppThrottle2NotZeroTest() {
double topicAppThrottle = jmxService.getTopicAppThrottle(
REAL_CLUSTER_ID_IN_MYSQL,
REAL_BROKER_ID_IN_ZK,
CLIENT_ID,
KafkaClientEnum.FETCH_CLIENT);
// 未设置限流所以还是为0
Assert.assertEquals(topicAppThrottle, 0.0d);
}
@Test(description = "获取被限流信息")
public void getBrokerThrottleClientsTest() {
// 结果为空
getBrokerThrottleClients2EmptyTest();
// 构造限流client返回结果不为空
// 需要流量达到限制值,比较难构造
// getBrokerThrottleClients2NotEmptyTest();
}
private void getBrokerThrottleClients2EmptyTest() {
Set<String> brokerThrottleClients = jmxService.getBrokerThrottleClients(
REAL_CLUSTER_ID_IN_MYSQL,
INVALID_BROKER_ID,
KafkaClientEnum.FETCH_CLIENT);
Assert.assertTrue(brokerThrottleClients.isEmpty());
}
private void getBrokerThrottleClients2NotEmptyTest() {
Set<String> brokerThrottleClients = jmxService.getBrokerThrottleClients(
REAL_CLUSTER_ID_IN_MYSQL,
REAL_BROKER_ID_IN_ZK,
KafkaClientEnum.FETCH_CLIENT);
Assert.assertFalse(brokerThrottleClients.isEmpty());
}
@Test(description = "测试获取topic消息压缩指标")
public void getTopicCodeCValueTest() {
// 结果为null
getTopicCodeCValue2NullTest();
// 结果不为null
getTopicCodeCValue2SuccessTest();
}
private void getTopicCodeCValue2NullTest() {
String result = jmxService.getTopicCodeCValue(REAL_CLUSTER_ID_IN_MYSQL, INVALID_TOPIC);
Assert.assertNull(result);
}
private void getTopicCodeCValue2SuccessTest() {
String result = jmxService.getTopicCodeCValue(
REAL_CLUSTER_ID_IN_MYSQL,
REAL_TOPIC2_IN_ZK);
Assert.assertNotNull(result);
}
@Test(description = "测试从JMX中获取appId维度的的流量信息")
public void getTopicAppMetricsTest() {
// result is empty
getTopicAppMetrics2Empty();
// result is not empty
getTopicAppMetrics2NotEmpty();
}
private void getTopicAppMetrics2Empty() {
List<TopicMetrics> topicAppMetrics = jmxService.getTopicAppMetrics(
REAL_CLUSTER_ID_IN_MYSQL,
INVALID_METRICS_CODE);
Assert.assertTrue(topicAppMetrics.isEmpty());
List<TopicMetrics> topicAppMetrics2 = jmxService.getTopicAppMetrics(
INVALID_CLUSTER_ID,
KafkaMetricsCollections.APP_TOPIC_METRICS_TO_DB);
Assert.assertTrue(topicAppMetrics2.isEmpty());
}
private void getTopicAppMetrics2NotEmpty() {
List<TopicMetrics> topicAppMetrics = jmxService.getTopicAppMetrics(
REAL_CLUSTER_ID_IN_MYSQL,
KafkaMetricsCollections.APP_TOPIC_METRICS_TO_DB
);
Assert.assertFalse(topicAppMetrics.isEmpty());
}
// @Test
public void getBrokerTopicLocationTest() {
// result is empty
getBrokerTopicLocation2EmptyTest();
// result is not empty
getBrokerTopicLocation2NotEmptyTest();
}
private void getBrokerTopicLocation2EmptyTest() {
Map<TopicPartition, String> brokerTopicLocation = jmxService.getBrokerTopicLocation(
REAL_CLUSTER_ID_IN_MYSQL,
INVALID_BROKER_ID
);
Assert.assertTrue(brokerTopicLocation.isEmpty());
}
private void getBrokerTopicLocation2NotEmptyTest() {
Map<TopicPartition, String> brokerTopicLocation = jmxService.getBrokerTopicLocation(
REAL_CLUSTER_ID_IN_MYSQL,
2
);
Assert.assertFalse(brokerTopicLocation.isEmpty());
}
@Test
public void getPartitionAttributeTest() {
// result is empty
getPartitionAttribute2EmptyTest();
// result is not empty
getPartitionAttribute2NotEmptyTest();
}
private void getPartitionAttribute2EmptyTest() {
Map<Integer, PartitionAttributeDTO> list = jmxService.getPartitionAttribute(
REAL_CLUSTER_ID_IN_MYSQL,
REAL_TOPIC2_IN_ZK,
Collections.emptyList());
Assert.assertTrue(list.isEmpty());
}
private void getPartitionAttribute2NotEmptyTest() {
// 需要确定leader所在broker
PartitionState partitionState1 = getPartitionState();
PartitionState partitionState2 = getPartitionState();
partitionState2.setLeader(3);
partitionState2.setPartitionId(1);
Map<Integer, PartitionAttributeDTO> list = jmxService.getPartitionAttribute(
REAL_CLUSTER_ID_IN_MYSQL,
REAL_TOPIC2_IN_ZK,
Arrays.asList(partitionState1, partitionState1, partitionState2)
);
Assert.assertFalse(list.isEmpty());
}
}

View File

@@ -0,0 +1,191 @@
package com.xiaojukeji.kafka.manager.service.service;
import com.xiaojukeji.kafka.manager.common.entity.ao.cluster.LogicalClusterMetrics;
import com.xiaojukeji.kafka.manager.common.entity.pojo.BrokerMetricsDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.KafkaBillDO;
import com.xiaojukeji.kafka.manager.dao.KafkaBillDao;
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.springframework.beans.factory.annotation.Value;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
* @author xuguang
* @Date 2021/12/14
*/
public class KafkaBillServiceTest extends BaseTest {
@Autowired
@InjectMocks
private KafkaBillService kafkaBillService;
@Mock
private KafkaBillDao kafkaBillDao;
@BeforeMethod
public void setup() {
MockitoAnnotations.initMocks(this);
}
@Value("${test.phyCluster.id}")
private Long REAL_CLUSTER_ID_IN_MYSQL;
@Value("${test.admin}")
private String ADMIN;
private KafkaBillDO getKafkaBillDO() {
KafkaBillDO kafkaBillDO = new KafkaBillDO();
kafkaBillDO.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
kafkaBillDO.setCost(100.0d);
kafkaBillDO.setGmtCreate(new Date(1638605696062L));
kafkaBillDO.setGmtDay("10");
kafkaBillDO.setPrincipal(ADMIN);
kafkaBillDO.setQuota(1000.0d);
kafkaBillDO.setTopicName("moduleTest");
return kafkaBillDO;
}
private BrokerMetricsDO getBrokerMetricsDO() {
BrokerMetricsDO metricsDO = new BrokerMetricsDO();
metricsDO.setMetrics("");
return metricsDO;
}
@Test()
public void replaceTest() {
KafkaBillDO kafkaBillDO = getKafkaBillDO();
// 插入成功
replace2SuccessTest(kafkaBillDO);
// 插入失败
replace2ExceptionTest(kafkaBillDO);
}
private void replace2SuccessTest(KafkaBillDO kafkaBillDO) {
Mockito.when(kafkaBillDao.replace(Mockito.any())).thenReturn(1);
int result = kafkaBillService.replace(kafkaBillDO);
Assert.assertEquals(result, 1);
}
private void replace2ExceptionTest(KafkaBillDO kafkaBillDO) {
Mockito.when(kafkaBillDao.replace(Mockito.any())).thenThrow(RuntimeException.class);
int result = kafkaBillService.replace(kafkaBillDO);
Assert.assertEquals(result, 0);
}
@Test()
public void getByTopicNameTest() {
KafkaBillDO kafkaBillDO = getKafkaBillDO();
// 查询成功
getByTopicName2SuccessTest(kafkaBillDO);
// 查询异常
getByTopicName2ExceptionTest();
}
private void getByTopicName2SuccessTest(KafkaBillDO kafkaBillDO) {
Mockito.when(kafkaBillDao.getByTopicName(
Mockito.anyLong(), Mockito.anyString(), Mockito.any(), Mockito.any())).thenReturn(Arrays.asList(kafkaBillDO));
List<KafkaBillDO> result = kafkaBillService.getByTopicName(1L, "moudleTest", new Date(0L), new Date());
Assert.assertFalse(result.isEmpty());
Assert.assertTrue(result.stream().allMatch(kafkaBillDO1 ->
kafkaBillDO1.getTopicName().equals(kafkaBillDO.getTopicName()) &&
kafkaBillDO1.getClusterId().equals(kafkaBillDO.getClusterId())));
}
private void getByTopicName2ExceptionTest() {
Mockito.when(kafkaBillDao.getByTopicName(
Mockito.anyLong(), Mockito.anyString(), Mockito.any(), Mockito.any())).thenThrow(RuntimeException.class);
List<KafkaBillDO> result = kafkaBillService.getByTopicName(1L, "moudleTest", new Date(0L), new Date());
Assert.assertTrue(result.isEmpty());
}
@Test()
public void getByPrincipalTest() {
KafkaBillDO kafkaBillDO = getKafkaBillDO();
// 查询成功
getByPrincipal2SuccessTest(kafkaBillDO);
// 查询失败
getByPrincipal2ExceptionTest();
}
private void getByPrincipal2SuccessTest(KafkaBillDO kafkaBillDO) {
Mockito.when(kafkaBillDao.getByPrincipal(
Mockito.anyString(), Mockito.any(), Mockito.any())).thenReturn(Arrays.asList(kafkaBillDO));
List<KafkaBillDO> result = kafkaBillService.getByPrincipal("admin", new Date(0L), new Date());
Assert.assertFalse(result.isEmpty());
Assert.assertTrue(result.stream().allMatch(kafkaBillDO1 ->
kafkaBillDO1.getTopicName().equals(kafkaBillDO.getTopicName()) &&
kafkaBillDO1.getClusterId().equals(kafkaBillDO.getClusterId())));
}
private void getByPrincipal2ExceptionTest() {
Mockito.when(kafkaBillDao.getByPrincipal(
Mockito.anyString(), Mockito.any(), Mockito.any())).thenThrow(RuntimeException.class);
List<KafkaBillDO> result = kafkaBillService.getByPrincipal("admin", new Date(0L), new Date());
Assert.assertTrue(result.isEmpty());
}
@Test()
public void getByTimeBetweenTest() {
KafkaBillDO kafkaBillDO = getKafkaBillDO();
// 查询成功
getByTimeBetween2SuccessTest(kafkaBillDO);
// 查询失败
getByTimeBetween2ExceptionTest();
}
private void getByTimeBetween2SuccessTest(KafkaBillDO kafkaBillDO) {
Mockito.when(kafkaBillDao.getByTimeBetween(
Mockito.any(), Mockito.any())).thenReturn(Arrays.asList(kafkaBillDO));
List<KafkaBillDO> result = kafkaBillService.getByTimeBetween(new Date(0L), new Date());
Assert.assertFalse(result.isEmpty());
Assert.assertTrue(result.stream().allMatch(kafkaBillDO1 ->
kafkaBillDO1.getTopicName().equals(kafkaBillDO.getTopicName()) &&
kafkaBillDO1.getClusterId().equals(kafkaBillDO.getClusterId())));
}
private void getByTimeBetween2ExceptionTest() {
Mockito.when(kafkaBillDao.getByTimeBetween(
Mockito.any(), Mockito.any())).thenThrow(RuntimeException.class);
List<KafkaBillDO> result = kafkaBillService.getByTimeBetween(new Date(0L), new Date());
Assert.assertTrue(result.isEmpty());
}
@Test()
public void getByGmtDayTest() {
KafkaBillDO kafkaBillDO = getKafkaBillDO();
// 查询成功
getByGmtDay2SuccessTest(kafkaBillDO);
// 查询失败
getByGmtDay2ExceptionTest();
}
private void getByGmtDay2SuccessTest(KafkaBillDO kafkaBillDO) {
Mockito.when(kafkaBillDao.getByGmtDay(
Mockito.anyString())).thenReturn(Arrays.asList(kafkaBillDO));
List<KafkaBillDO> result = kafkaBillService.getByGmtDay("10");
Assert.assertFalse(result.isEmpty());
Assert.assertTrue(result.stream().allMatch(kafkaBillDO1 ->
kafkaBillDO1.getTopicName().equals(kafkaBillDO.getTopicName()) &&
kafkaBillDO1.getClusterId().equals(kafkaBillDO.getClusterId())));
}
private void getByGmtDay2ExceptionTest() {
Mockito.when(kafkaBillDao.getByGmtDay(
Mockito.anyString())).thenThrow(RuntimeException.class);
List<KafkaBillDO> result = kafkaBillService.getByGmtDay("10");
Assert.assertTrue(result.isEmpty());
}
}

View File

@@ -0,0 +1,430 @@
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.beans.factory.annotation.Value;
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 {
private final static Long INVALID_CLUSTER_ID = -1L;
@Value("${test.phyCluster.id}")
private Long REAL_CLUSTER_ID_IN_MYSQL;
@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(INVALID_CLUSTER_ID);
logicalClusterDO.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
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(INVALID_CLUSTER_ID);
logicalClusterDO.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
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 createLogicalCluster() {
// 创建逻辑集群时参数错误
createLogicalCluster2paramIllegalTest();
// 创建逻辑集群时,region已使用
createLogicalCluster2existRegionAlreadyInUseTest();
// 创建逻辑集群时,物理集群不存在
createLogicalCluster2PhysicalClusterNotExistTest();
// 创建逻辑集群时,不存在region已使用
createLogicalCluster2NotexistRegionAlreadyInUseTest();
// 创建逻辑集群成功
createLogicalCluster2SuccessTest();
}
private void createLogicalCluster2paramIllegalTest() {
ResultStatus result = logicalClusterService.createLogicalCluster(null);
Assert.assertEquals(result.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
}
private void createLogicalCluster2existRegionAlreadyInUseTest() {
LogicalClusterDO logicalClusterDO = getLogicalClusterDO();
// 物理集群Id为null
logicalClusterDO.setClusterId(null);
ResultStatus result1 = logicalClusterService.createLogicalCluster(logicalClusterDO);
Assert.assertEquals(result1.getCode(), ResultStatus.RESOURCE_ALREADY_USED.getCode());
// regionList为空情况
logicalClusterDO.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
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());
}
private void createLogicalCluster2PhysicalClusterNotExistTest() {
LogicalClusterDO logicalClusterDO = getLogicalClusterDO();
Mockito.when(logicalClusterDao.insert(Mockito.any())).thenReturn(1);
// 不存在该物理集群情况
logicalClusterDO.setClusterId(INVALID_CLUSTER_ID);
ResultStatus result1 = logicalClusterService.createLogicalCluster(logicalClusterDO);
Assert.assertNotEquals(result1.getCode(), ResultStatus.RESOURCE_ALREADY_USED.getCode());
Assert.assertEquals(result1.getCode(), ResultStatus.SUCCESS.getCode());
}
private void createLogicalCluster2NotexistRegionAlreadyInUseTest() {
LogicalClusterDO logicalClusterDO = getLogicalClusterDO();
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(description = "创建逻辑集群时,不存在region已使用(键重复)")
private void createLogicalCluster2DuplicateKeyTest() {
LogicalClusterDO logicalClusterDO = getLogicalClusterDO();
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());
}
private void createLogicalCluster2SuccessTest() {
LogicalClusterDO logicalClusterDO = getLogicalClusterDO();
Mockito.when(logicalClusterDao.insert(Mockito.any())).thenReturn(1);
ResultStatus result3 = logicalClusterService.createLogicalCluster(logicalClusterDO);
Assert.assertEquals(result3.getCode(), ResultStatus.SUCCESS.getCode());
}
@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(INVALID_CLUSTER_ID);
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(INVALID_CLUSTER_ID);
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(REAL_CLUSTER_ID_IN_MYSQL);
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(INVALID_CLUSTER_ID);
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 = new Date().getTime();
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));
}
}

View File

@@ -0,0 +1,136 @@
package com.xiaojukeji.kafka.manager.service.service;
import com.xiaojukeji.kafka.manager.common.bizenum.ModuleEnum;
import com.xiaojukeji.kafka.manager.common.bizenum.OperateEnum;
import com.xiaojukeji.kafka.manager.common.entity.dto.rd.OperateRecordDTO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.OperateRecordDO;
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.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author wyc
* @date 2021/12/8
*/
public class OperateRecordServiceTest extends BaseTest {
@Autowired
private OperateRecordService operateRecordService;
@DataProvider(name = "operateRecordDO")
public Object[][] provideOperateRecordDO() {
OperateRecordDO operateRecordDO = new OperateRecordDO();
operateRecordDO.setId(3L);
// 0:topic, 1:应用, 2:配额, 3:权限, 4:集群, 5:分区, 6:Gateway配置, -1:未知
operateRecordDO.setModuleId(ModuleEnum.CLUSTER.getCode());
// 0:新增, 1:删除, 2:修改
operateRecordDO.setOperateId(OperateEnum.ADD.getCode());
// topic名称、app名称
operateRecordDO.setResource("testOpRecord");
operateRecordDO.setContent("testContent");
operateRecordDO.setOperator("admin");
return new Object[][] {{operateRecordDO}};
}
private OperateRecordDTO getOperateRecordDTO() {
OperateRecordDTO dto = new OperateRecordDTO();
dto.setModuleId(ModuleEnum.CLUSTER.getCode());
dto.setOperateId(OperateEnum.ADD.getCode());
dto.setOperator("admin");
return dto;
}
@Test(dataProvider = "operateRecordDO", description = "插入操作记录成功测试")
public void insert2SuccessTest(OperateRecordDO operateRecordDO) {
int result = operateRecordService.insert(operateRecordDO);
Assert.assertEquals(result, 1);
}
@Test(description = "插入的重载方法操作成功测试")
public void insert2SuccessTest1() {
Map<String, String> content = new HashMap<>();
content.put("key", "value");
int result = operateRecordService.insert("admin", ModuleEnum.CLUSTER, "testOpRecord", OperateEnum.ADD, content);
Assert.assertEquals(result, 1);
}
@Test(dataProvider = "operateRecordDO")
public void queryByConditionTest(OperateRecordDO operateRecordDO) {
operateRecordService.insert(operateRecordDO);
// endTime和startTime都是null
queryByConditionTest3(operateRecordDO);
// startTime是null
queryByConditionTest1(operateRecordDO);
// endTime是null
queryByConditionTest2(operateRecordDO);
// endTime和startTime都不是null
queryByConditionTest4(operateRecordDO);
}
private void queryByConditionTest1(OperateRecordDO operateRecordDO) {
OperateRecordDTO dto = getOperateRecordDTO();
dto.setEndTime(new Date().getTime());
List<OperateRecordDO> queryResult = operateRecordService.queryByCondition(dto);
Assert.assertFalse(queryResult.isEmpty());
// 判断查询得到的OperateRecordDO中日期是否符合要求
Assert.assertTrue(queryResult.stream().allMatch(operateRecordDO1 ->
operateRecordDO1.getCreateTime().after(new Date(0L)) &&
operateRecordDO1.getCreateTime().before(new Date()) &&
operateRecordDO1.getModuleId().equals(dto.getModuleId()) &&
operateRecordDO1.getOperateId().equals(dto.getOperateId()) &&
operateRecordDO1.getOperator().equals(dto.getOperator())));
}
private void queryByConditionTest2(OperateRecordDO operateRecordDO) {
OperateRecordDTO dto = getOperateRecordDTO();
dto.setStartTime(new Date().getTime());
// 查询的是create_time >= startTime, 因为创建时间在当前时间之前,因此查到的数据是空的
List<OperateRecordDO> queryResult = operateRecordService.queryByCondition(dto);
Assert.assertTrue(queryResult.isEmpty());
}
private void queryByConditionTest3(OperateRecordDO operateRecordDO) {
OperateRecordDTO dto = getOperateRecordDTO();
List<OperateRecordDO> queryResult = operateRecordService.queryByCondition(dto);
Assert.assertFalse(queryResult.isEmpty());
Assert.assertTrue(queryResult.stream().allMatch(operateRecordDO1 ->
operateRecordDO1.getCreateTime().after(new Date(0L)) &&
operateRecordDO1.getCreateTime().before(new Date()) &&
operateRecordDO1.getModuleId().equals(dto.getModuleId()) &&
operateRecordDO1.getOperateId().equals(dto.getOperateId()) &&
operateRecordDO1.getOperator().equals(dto.getOperator())));
}
private void queryByConditionTest4(OperateRecordDO operateRecordDO) {
OperateRecordDTO dto = getOperateRecordDTO();
dto.setStartTime(0L);
dto.setEndTime(1649036393371L);
List<OperateRecordDO> queryResult = operateRecordService.queryByCondition(dto);
Assert.assertFalse(queryResult.isEmpty());
Assert.assertTrue(queryResult.stream().allMatch(operateRecordDO1 ->
operateRecordDO1.getCreateTime().after(new Date(0L)) &&
operateRecordDO1.getCreateTime().before(new Date()) &&
operateRecordDO1.getModuleId().equals(dto.getModuleId()) &&
operateRecordDO1.getOperateId().equals(dto.getOperateId()) &&
operateRecordDO1.getOperator().equals(dto.getOperator())));
}
}

View File

@@ -0,0 +1,483 @@
package com.xiaojukeji.kafka.manager.service.service;
import com.xiaojukeji.kafka.manager.common.bizenum.TaskStatusReassignEnum;
import com.xiaojukeji.kafka.manager.common.bizenum.TopicReassignActionEnum;
import com.xiaojukeji.kafka.manager.common.entity.Result;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.ao.reassign.ReassignStatus;
import com.xiaojukeji.kafka.manager.common.entity.dto.op.reassign.ReassignExecDTO;
import com.xiaojukeji.kafka.manager.common.entity.dto.op.reassign.ReassignExecSubDTO;
import com.xiaojukeji.kafka.manager.common.entity.dto.op.reassign.ReassignTopicDTO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.ClusterDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.ReassignTaskDO;
import com.xiaojukeji.kafka.manager.dao.ReassignTaskDao;
import com.xiaojukeji.kafka.manager.service.config.BaseTest;
import kafka.common.TopicAndPartition;
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.beans.factory.annotation.Value;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.*;
/**
* @author xuguang
* @Date 2021/12/14
*/
public class ReassignServiceTest extends BaseTest {
/**
* 集群共包括三个broker:1,2,3, 该topic 2分区 3副本因子在broker1,2,3上
*/
@Value("${test.topic.name2}")
private String REAL_TOPIC2_IN_ZK;
@Value("${test.admin}")
private String ADMIN_OPERATOR;
@Autowired
@InjectMocks
private ReassignService reassignService;
@Mock
private RegionService regionService;
@Mock
private ClusterService clusterService;
@Mock
private ReassignTaskDao reassignTaskDao;
@BeforeMethod
public void setup() {
MockitoAnnotations.initMocks(this);
}
@Value("${test.ZK.address}")
private String ZOOKEEPER_ADDRESS;
@Value("${test.ZK.bootstrap-servers}")
private String BOOTSTRAP_SERVERS;
private final static String SECURITY_PROTOCOL = "{ \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\\\";\" }";
private final static String REASSIGNMENTJSON =
"{ \"version\": 1, \"partitions\": [ { \"topic\": \"reassignTest\", \"partition\": 1, \"replicas\": [ 1,2,3 ], \"log_dirs\": [ \"any\",\"any\",\"any\" ] }, { \"topic\": \"reassignTest\", \"partition\": 0, \"replicas\": [ 1,2,3 ], \"log_dirs\": [ \"any\",\"any\",\"any\" ] } ] }";
@Value("${test.phyCluster.id}")
private Long REAL_CLUSTER_ID_IN_MYSQL;
@Value("${test.phyCluster.name}")
private String REAL_PHYSICAL_CLUSTER_NAME;
private ReassignTopicDTO getReassignTopicDTO() {
// 让分区从原本的broker123变成只落到broker2,3
ReassignTopicDTO reassignTopicDTO = new ReassignTopicDTO();
reassignTopicDTO.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
reassignTopicDTO.setTopicName(REAL_TOPIC2_IN_ZK);
reassignTopicDTO.setBrokerIdList(Arrays.asList(2,3));
reassignTopicDTO.setRegionId(1000000L);
// 原本Topic只有两个分区
reassignTopicDTO.setPartitionIdList(Arrays.asList(0, 1));
reassignTopicDTO.setThrottle(100000L);
reassignTopicDTO.setMaxThrottle(100000L);
reassignTopicDTO.setMinThrottle(100000L);
reassignTopicDTO.setOriginalRetentionTime(10000L);
reassignTopicDTO.setReassignRetentionTime(10000L);
reassignTopicDTO.setBeginTime(100000L);
reassignTopicDTO.setDescription("");
return reassignTopicDTO;
}
private ReassignExecDTO getReassignExecDTO() {
ReassignExecDTO reassignExecDTO = new ReassignExecDTO();
reassignExecDTO.setTaskId(1L);
reassignExecDTO.setAction("modify");
reassignExecDTO.setBeginTime(0L);
return reassignExecDTO;
}
private ReassignTaskDO getReassignTaskDO() {
ReassignTaskDO reassignTaskDO = new ReassignTaskDO();
reassignTaskDO.setId(1L);
reassignTaskDO.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
reassignTaskDO.setStatus(0);
reassignTaskDO.setTaskId(1L);
reassignTaskDO.setTopicName(REAL_TOPIC2_IN_ZK);
reassignTaskDO.setPartitions("0,1,2");
reassignTaskDO.setReassignmentJson("");
reassignTaskDO.setRealThrottle(1000L);
reassignTaskDO.setMaxThrottle(1000L);
reassignTaskDO.setMinThrottle(1000L);
reassignTaskDO.setBeginTime(new Date());
reassignTaskDO.setSrcBrokers("0");
reassignTaskDO.setDestBrokers("1");
reassignTaskDO.setReassignRetentionTime(1000L);
reassignTaskDO.setOriginalRetentionTime(1000L);
reassignTaskDO.setDescription("测试迁移任务");
reassignTaskDO.setOperator(ADMIN_OPERATOR);
return reassignTaskDO;
}
private ReassignExecSubDTO getReassignExecSubDTO() {
ReassignExecSubDTO reassignExecSubDTO = new ReassignExecSubDTO();
reassignExecSubDTO.setSubTaskId(1L);
reassignExecSubDTO.setAction("modify");
reassignExecSubDTO.setThrottle(100000L);
reassignExecSubDTO.setMaxThrottle(100000L);
reassignExecSubDTO.setMinThrottle(100000L);
return reassignExecSubDTO;
}
private ClusterDO getClusterDO() {
ClusterDO clusterDO = new ClusterDO();
clusterDO.setId(REAL_CLUSTER_ID_IN_MYSQL);
clusterDO.setClusterName(REAL_PHYSICAL_CLUSTER_NAME);
clusterDO.setZookeeper(ZOOKEEPER_ADDRESS);
clusterDO.setBootstrapServers(BOOTSTRAP_SERVERS);
clusterDO.setSecurityProperties(SECURITY_PROTOCOL);
clusterDO.setStatus(1);
clusterDO.setGmtCreate(new Date());
clusterDO.setGmtModify(new Date());
return clusterDO;
}
private Map<Long, ClusterDO> getMap() {
ClusterDO clusterDO = getClusterDO();
HashMap<Long, ClusterDO> map = new HashMap<>();
map.put(REAL_CLUSTER_ID_IN_MYSQL, clusterDO);
return map;
}
@Test(description = "创建迁移任务")
public void createTaskTest() {
// 参数错误
createTask2paramIllegalTest();
// 物理集群不存在
createTask2ClusterNotExistTest();
// topic不存在
createTask2TopicNotExistTest();
// broker数量不足
createTask2BrokerNumNotEnoughTest();
// broker不存在
createTask2BrokerNotExistTest();
// broker数量不足, checkParamLegal()方法中
createTask2BrokerNumNotEnough2Test();
// 参数错误, checkParamLegal()方法中
createTask2ParamIllegal2Test();
// 分区为空
createTask2PartitionIdListEmptyTest();
// 分区不存在
// 因定时任务暂时无法跑通
// createTask2PartitionNotExistTest();
// 创建任务成功
// 因定时任务暂时无法跑通
// createTask2SuccessTest();
}
private void createTask2paramIllegalTest() {
ResultStatus result = reassignService.createTask(Collections.emptyList(), ADMIN_OPERATOR);
Assert.assertEquals(result.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
}
private void createTask2ClusterNotExistTest() {
ReassignTopicDTO reassignTopicDTO = getReassignTopicDTO();
Mockito.when(clusterService.listMap()).thenReturn(new HashMap<>());
ResultStatus result = reassignService.createTask(Arrays.asList(reassignTopicDTO), ADMIN_OPERATOR);
Assert.assertEquals(result.getCode(), ResultStatus.CLUSTER_NOT_EXIST.getCode());
}
private void createTask2TopicNotExistTest() {
ReassignTopicDTO reassignTopicDTO = getReassignTopicDTO();
reassignTopicDTO.setTopicName("xxx");
Mockito.when(clusterService.listMap()).thenReturn(getMap());
ResultStatus result = reassignService.createTask(Arrays.asList(reassignTopicDTO), ADMIN_OPERATOR);
Assert.assertEquals(result.getCode(), ResultStatus.TOPIC_NOT_EXIST.getCode());
}
private void createTask2BrokerNumNotEnoughTest() {
Mockito.when(clusterService.listMap()).thenReturn(getMap());
Mockito.when(regionService.getFullBrokerIdList(
Mockito.anyLong(), Mockito.anyLong(), Mockito.anyList())).thenReturn(null);
ReassignTopicDTO reassignTopicDTO = getReassignTopicDTO();
ResultStatus result = reassignService.createTask(Arrays.asList(reassignTopicDTO), ADMIN_OPERATOR);
Assert.assertEquals(result.getCode(), ResultStatus.BROKER_NUM_NOT_ENOUGH.getCode());
}
private void createTask2BrokerNotExistTest() {
Mockito.when(clusterService.listMap()).thenReturn(getMap());
Mockito.when(regionService.getFullBrokerIdList(
Mockito.anyLong(), Mockito.anyLong(), Mockito.anyList())).thenReturn(Arrays.asList(100, 2, 3));
ReassignTopicDTO reassignTopicDTO = getReassignTopicDTO();
ResultStatus result = reassignService.createTask(Arrays.asList(reassignTopicDTO), ADMIN_OPERATOR);
Assert.assertEquals(result.getCode(), ResultStatus.BROKER_NOT_EXIST.getCode());
}
private void createTask2BrokerNumNotEnough2Test() {
Mockito.when(clusterService.listMap()).thenReturn(getMap());
Mockito.when(regionService.getFullBrokerIdList(
Mockito.anyLong(), Mockito.anyLong(), Mockito.anyList())).thenReturn(Arrays.asList(2, 3));
ReassignTopicDTO reassignTopicDTO = getReassignTopicDTO();
ResultStatus result = reassignService.createTask(Arrays.asList(reassignTopicDTO), ADMIN_OPERATOR);
Assert.assertEquals(result.getCode(), ResultStatus.BROKER_NUM_NOT_ENOUGH.getCode());
}
private void createTask2ParamIllegal2Test() {
Mockito.when(clusterService.listMap()).thenReturn(getMap());
Mockito.when(regionService.getFullBrokerIdList(
Mockito.anyLong(), Mockito.anyLong(), Mockito.anyList())).thenReturn(Arrays.asList(1, 2, 3));
ReassignTopicDTO reassignTopicDTO = getReassignTopicDTO();
ResultStatus result = reassignService.createTask(Arrays.asList(reassignTopicDTO), ADMIN_OPERATOR);
Assert.assertEquals(result.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
}
private void createTask2PartitionIdListEmptyTest() {
Mockito.when(clusterService.listMap()).thenReturn(getMap());
Mockito.when(regionService.getFullBrokerIdList(
Mockito.anyLong(), Mockito.anyLong(), Mockito.anyList())).thenReturn(Arrays.asList(1, 2, 3));
ReassignTopicDTO reassignTopicDTO = getReassignTopicDTO();
reassignTopicDTO.setOriginalRetentionTime(168 * 3600000L);
reassignTopicDTO.setPartitionIdList(Collections.emptyList());
ResultStatus result = reassignService.createTask(Arrays.asList(reassignTopicDTO), ADMIN_OPERATOR);
Assert.assertEquals(result.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
}
private void createTask2PartitionNotExistTest() {
Mockito.when(clusterService.listMap()).thenReturn(getMap());
Mockito.when(regionService.getFullBrokerIdList(
Mockito.anyLong(), Mockito.anyLong(), Mockito.anyList())).thenReturn(Arrays.asList(1, 2, 3));
ReassignTopicDTO reassignTopicDTO = getReassignTopicDTO();
reassignTopicDTO.setClusterId(1L);
reassignTopicDTO.setTopicName(REAL_TOPIC2_IN_ZK);
// 注意要求topic中数据保存时间为168小时
reassignTopicDTO.setOriginalRetentionTime(168 * 3600000L);
reassignTopicDTO.setPartitionIdList(Arrays.asList(100, 0));
ResultStatus result = reassignService.createTask(Arrays.asList(reassignTopicDTO), ADMIN_OPERATOR);
Assert.assertEquals(result.getCode(), ResultStatus.PARTITION_NOT_EXIST.getCode());
}
private void createTask2SuccessTest() {
Mockito.when(clusterService.listMap()).thenReturn(getMap());
Mockito.when(regionService.getFullBrokerIdList(
Mockito.anyLong(), Mockito.anyLong(), Mockito.anyList())).thenReturn(Arrays.asList(1, 2, 3));
ReassignTopicDTO reassignTopicDTO = getReassignTopicDTO();
reassignTopicDTO.setTopicName(REAL_TOPIC2_IN_ZK);
reassignTopicDTO.setOriginalRetentionTime(168 * 3600000L);
ResultStatus result = reassignService.createTask(Arrays.asList(reassignTopicDTO), ADMIN_OPERATOR);
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test(description = "测试获取迁移任务")
public void getTaskTest() {
// 测试获取成功
getTaskTest2Success();
// 测试获取失败
getTaskTest2Exception();
}
private void getTaskTest2Success() {
ReassignTaskDO reassignTask = getReassignTaskDO();
Mockito.when(reassignTaskDao.getByTaskId(Mockito.anyLong())).thenReturn(Arrays.asList(reassignTask));
List<ReassignTaskDO> task = reassignService.getTask(reassignTask.getTaskId());
Assert.assertFalse(task.isEmpty());
Assert.assertTrue(task.stream().allMatch(reassignTaskDO ->
reassignTaskDO.getTaskId().equals(reassignTask.getTaskId()) &&
reassignTaskDO.getClusterId().equals(reassignTask.getClusterId()) &&
reassignTaskDO.getStatus().equals(reassignTask.getStatus())));
}
private void getTaskTest2Exception() {
ReassignTaskDO reassignTask = getReassignTaskDO();
Mockito.when(reassignTaskDao.getByTaskId(Mockito.anyLong())).thenThrow(RuntimeException.class);
List<ReassignTaskDO> task = reassignService.getTask(reassignTask.getTaskId());
Assert.assertNull(task);
}
@Test(description = "修改迁移任务")
public void modifyTask() {
// operation forbidden
modifyTask2OperationForbiddenTest();
// 修改成功
modifyTask2Success();
// mysqlError
modifyTask2MysqlError();
// 任务不存在
modifyTask2TaskNotExistTest();
}
private void modifyTask2TaskNotExistTest() {
Mockito.when(reassignTaskDao.getByTaskId(Mockito.anyLong())).thenThrow(RuntimeException.class);
ReassignExecDTO reassignExecDTO = getReassignExecDTO();
reassignExecDTO.setTaskId(100L);
ResultStatus resultStatus = reassignService.modifyTask(reassignExecDTO, TopicReassignActionEnum.START);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.TASK_NOT_EXIST.getCode());
}
private void modifyTask2OperationForbiddenTest() {
ReassignTaskDO reassignTask = getReassignTaskDO();
reassignTask.setStatus(1);
Mockito.when(reassignTaskDao.getByTaskId(Mockito.anyLong())).thenReturn(Arrays.asList(reassignTask));
ReassignExecDTO reassignExecDTO = getReassignExecDTO();
ResultStatus resultStatus1 = reassignService.modifyTask(reassignExecDTO, TopicReassignActionEnum.START);
Assert.assertEquals(resultStatus1.getCode(), ResultStatus.OPERATION_FORBIDDEN.getCode());
}
private void modifyTask2Success() {
ReassignTaskDO reassignTask = getReassignTaskDO();
reassignTask.setStatus(0);
Mockito.when(reassignTaskDao.getByTaskId(Mockito.anyLong())).thenReturn(Arrays.asList(reassignTask));
ReassignExecDTO reassignExecDTO = getReassignExecDTO();
// cancel action
ResultStatus resultStatus1 = reassignService.modifyTask(reassignExecDTO, TopicReassignActionEnum.CANCEL);
Assert.assertEquals(resultStatus1.getCode(), ResultStatus.SUCCESS.getCode());
// start action
reassignTask.setStatus(0);
Mockito.when(reassignTaskDao.getByTaskId(Mockito.anyLong())).thenReturn(Arrays.asList(reassignTask));
ResultStatus resultStatus2 = reassignService.modifyTask(reassignExecDTO, TopicReassignActionEnum.START);
Assert.assertEquals(resultStatus2.getCode(), ResultStatus.SUCCESS.getCode());
// modify action
reassignTask.setStatus(0);
Mockito.when(reassignTaskDao.getByTaskId(Mockito.anyLong())).thenReturn(Arrays.asList(reassignTask));
ResultStatus resultStatus3 = reassignService.modifyTask(reassignExecDTO, TopicReassignActionEnum.MODIFY);
Assert.assertEquals(resultStatus3.getCode(), ResultStatus.SUCCESS.getCode());
}
private void modifyTask2MysqlError() {
ReassignTaskDO reassignTask = getReassignTaskDO();
reassignTask.setStatus(0);
Mockito.when(reassignTaskDao.getByTaskId(Mockito.anyLong())).thenReturn(Arrays.asList(reassignTask));
ReassignExecDTO reassignExecDTO = getReassignExecDTO();
// cancel action
Mockito.doThrow(RuntimeException.class).when(reassignTaskDao).batchUpdate(Mockito.anyList());
ResultStatus resultStatus1 = reassignService.modifyTask(reassignExecDTO, TopicReassignActionEnum.CANCEL);
Assert.assertEquals(resultStatus1.getCode(), ResultStatus.MYSQL_ERROR.getCode());
// start action
reassignTask.setStatus(0);
Mockito.when(reassignTaskDao.getByTaskId(Mockito.anyLong())).thenReturn(Arrays.asList(reassignTask));
Mockito.doThrow(RuntimeException.class).when(reassignTaskDao).batchUpdate(Mockito.anyList());
ResultStatus resultStatus2 = reassignService.modifyTask(reassignExecDTO, TopicReassignActionEnum.START);
Assert.assertEquals(resultStatus2.getCode(), ResultStatus.MYSQL_ERROR.getCode());
// modify action
reassignTask.setStatus(0);
Mockito.when(reassignTaskDao.getByTaskId(Mockito.anyLong())).thenReturn(Arrays.asList(reassignTask));
Mockito.doThrow(RuntimeException.class).when(reassignTaskDao).batchUpdate(Mockito.anyList());
ResultStatus resultStatus3 = reassignService.modifyTask(reassignExecDTO, TopicReassignActionEnum.MODIFY);
Assert.assertEquals(resultStatus3.getCode(), ResultStatus.MYSQL_ERROR.getCode());
}
@Test(description = "修改子任务测试")
public void modifySubTaskTest() {
// 任务不存在
modifySubTask2TaskNotExist();
// 修改任务成功
modifySubTask2Success();
// 修改任务失败
modifySubTask2MysqlError();
}
private void modifySubTask2TaskNotExist() {
Mockito.when(reassignTaskDao.getSubTask(Mockito.anyLong())).thenReturn(null);
ResultStatus resultStatus = reassignService.modifySubTask(new ReassignExecSubDTO());
Assert.assertEquals(resultStatus.getCode(), ResultStatus.TASK_NOT_EXIST.getCode());
}
private void modifySubTask2Success() {
ReassignTaskDO reassignTask = getReassignTaskDO();
Mockito.when(reassignTaskDao.getSubTask(Mockito.anyLong())).thenReturn(reassignTask);
ReassignExecSubDTO reassignExecSubDTO = getReassignExecSubDTO();
ResultStatus resultStatus = reassignService.modifySubTask(reassignExecSubDTO);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
}
private void modifySubTask2MysqlError() {
ReassignTaskDO reassignTask = getReassignTaskDO();
Mockito.when(reassignTaskDao.getSubTask(Mockito.anyLong())).thenReturn(reassignTask);
Mockito.when(reassignTaskDao.updateById(Mockito.any())).thenThrow(RuntimeException.class);
ReassignExecSubDTO reassignExecSubDTO = getReassignExecSubDTO();
ResultStatus resultStatus = reassignService.modifySubTask(reassignExecSubDTO);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.MYSQL_ERROR.getCode());
}
@Test(description = "获取任务列表测试")
public void getReassignTaskListTest() {
// 获取成功
getReassignTaskList2Success();
// 获取失败
getReassignTaskList2Empty();
}
private void getReassignTaskList2Success() {
Mockito.when(reassignTaskDao.listAll()).thenReturn(Arrays.asList(new ReassignTaskDO()));
List<ReassignTaskDO> reassignTaskList = reassignService.getReassignTaskList();
Assert.assertFalse(reassignTaskList.isEmpty());
}
private void getReassignTaskList2Empty() {
Mockito.when(reassignTaskDao.listAll()).thenThrow(RuntimeException.class);
List<ReassignTaskDO> reassignTaskList = reassignService.getReassignTaskList();
Assert.assertTrue(reassignTaskList.isEmpty());
}
@Test(description = "获取任务状态测试")
public void getReassignStatusTest() {
// 获取成功
getReassignStatus2Success();
// task不存在
getReassignStatus2TaskNotExistTest();
}
private void getReassignStatus2TaskNotExistTest() {
Mockito.when(reassignTaskDao.getByTaskId(Mockito.anyLong())).thenThrow(RuntimeException.class);
Result<List<ReassignStatus>> reassignStatus = reassignService.getReassignStatus(1L);
Assert.assertEquals(reassignStatus.getCode(), ResultStatus.TASK_NOT_EXIST.getCode());
}
private void getReassignStatus2Success() {
ClusterDO clusterDO1 = getClusterDO();
ClusterDO clusterDO2 = getClusterDO();
clusterDO2.setId(100L);
Map<Long, ClusterDO> map = new HashMap<>();
map.put(clusterDO1.getId(), clusterDO1);
map.put(clusterDO2.getId(), clusterDO2);
Mockito.when(clusterService.listMap()).thenReturn(map);
ReassignTaskDO reassignTaskDO1 = getReassignTaskDO();
ReassignTaskDO reassignTaskDO2 = getReassignTaskDO();
reassignTaskDO2.setStatus(TaskStatusReassignEnum.RUNNING.getCode());
Mockito.when(reassignTaskDao.getByTaskId(Mockito.anyLong())).thenReturn(Arrays.asList(reassignTaskDO1, reassignTaskDO2));
Result<List<ReassignStatus>> reassignStatus = reassignService.getReassignStatus(1L);
Assert.assertFalse(reassignStatus.getData().isEmpty());
Assert.assertEquals(reassignStatus.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test
public void verifyAssignmenTest() {
Map<TopicAndPartition, TaskStatusReassignEnum> map = reassignService.verifyAssignment(ZOOKEEPER_ADDRESS, REASSIGNMENTJSON);
Assert.assertFalse(map.isEmpty());
}
}

View File

@@ -0,0 +1,439 @@
package com.xiaojukeji.kafka.manager.service.service;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.pojo.LogicalClusterDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.RegionDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.TopicDO;
import com.xiaojukeji.kafka.manager.common.utils.ListUtils;
import com.xiaojukeji.kafka.manager.service.config.BaseTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author wyc
* @date 2021/12/8
*/
public class RegionServiceTest extends BaseTest{
@Value("${test.phyCluster.id}")
private Long REAL_CLUSTER_ID_IN_MYSQL;
@Value("${test.region-name}")
private String REAL_REGION_NAME_IN_CLUSTER;
@Value("${test.topic.name1}")
private String REAL_TOPIC1_IN_ZK;
@Autowired
private RegionService regionService;
@DataProvider(name = "regionDO")
public Object[][] provideRegionDO() {
RegionDO regionDO = new RegionDO();
regionDO.setStatus(0);
regionDO.setName("region1");
// 物理集群id
regionDO.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
regionDO.setDescription("test");
List<Integer> brokerIdList = new ArrayList<>();
brokerIdList.add(3);
regionDO.setBrokerList(ListUtils.intList2String(brokerIdList));
return new Object[][] {{regionDO}};
}
private RegionDO getRegionDO() {
RegionDO regionDO = new RegionDO();
regionDO.setStatus(0);
regionDO.setName("region1");
// 物理集群id
regionDO.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
regionDO.setDescription("test");
List<Integer> brokerIdList = new ArrayList<>();
brokerIdList.add(3);
regionDO.setBrokerList(ListUtils.intList2String(brokerIdList));
return regionDO;
}
@Test(description = "creatRegion, 参数为null测试")
public void createRegion2ParamIllegalTest() {
Assert.assertEquals(regionService.createRegion(null), ResultStatus.PARAM_ILLEGAL);
}
@Test(description = "createRegion, 成功测试")
public void createRegion2SuccessTest() {
RegionDO regionDO = getRegionDO();
Assert.assertEquals(regionService.createRegion(regionDO), ResultStatus.SUCCESS);
}
@Test(description = "createRegion, clusterId为空测试")
public void createRegion2ExistBrokerIdAlreadyInRegionTest1() {
RegionDO regionDO = getRegionDO();
regionDO.setClusterId(null);
Assert.assertEquals(regionService.createRegion(regionDO), ResultStatus.RESOURCE_ALREADY_USED);
}
@Test(description = "createRegion, 创建时传入的brokerList中有被使用过的")
public void createRegion2ExistBrokerIdAlreadyInRegionTest2() {
RegionDO regionDO = getRegionDO();
// 真实物理集群和数据库中region使用1,2broker
// 再创建一个Region, 使用1,3broker
List<Integer> newBrokerIdList = new ArrayList<>();
newBrokerIdList.add(1);
newBrokerIdList.add(3);
regionDO.setBrokerList(ListUtils.intList2String(newBrokerIdList));
Assert.assertEquals(regionService.createRegion(regionDO), ResultStatus.RESOURCE_ALREADY_USED);
}
@Test(description = "createRegion, 创建时region使用到的broker挂掉了")
public void createRegion2BrokerNotExistTest() {
RegionDO regionDO = getRegionDO();
// 传入一个不存在的物理集群检测时会认为该集群存活的broker个数为0
regionDO.setClusterId(-1L);
Assert.assertEquals(regionService.createRegion(regionDO), ResultStatus.BROKER_NOT_EXIST);
}
@Test(description = "createRegion, 创建时regionName重复")
public void createRegion2ResourceAlreadyExistTest() {
RegionDO regionDO = getRegionDO();
// 插入同名Region注意brokerList需要保持不一样不然会返回RESOURCE_ALREADY_USED
regionDO.setName(REAL_REGION_NAME_IN_CLUSTER);
Assert.assertEquals(regionService.createRegion(regionDO), ResultStatus.RESOURCE_ALREADY_EXISTED);
}
@Test
public void deleteByIdTest() {
RegionDO regionDO = getRegionDO();
// 参数非法测试
deleteById2ParamIllegalTest(regionDO);
// 资源不存在测试
deleteById2ResourceNotExistTest(regionDO);
// 删除成功测试
deleteById2SuccessTest(regionDO);
}
private void deleteById2ParamIllegalTest(RegionDO regionDO) {
Assert.assertEquals(regionService.deleteById(null), ResultStatus.PARAM_ILLEGAL);
}
private void deleteById2ResourceNotExistTest(RegionDO regionDO) {
Assert.assertEquals(regionService.deleteById(10L), ResultStatus.RESOURCE_NOT_EXIST);
}
private void deleteById2SuccessTest(RegionDO regionDO) {
regionDO.setId(1L);
Assert.assertEquals(regionService.createRegion(regionDO), ResultStatus.SUCCESS);
// 插入时xml文件中没用到idid交给数据库自增因此需要先查出Region的id再根据id删除
List<RegionDO> regionDOList = regionService.getByClusterId(1L);
RegionDO region = regionDOList.get(0);
Assert.assertEquals(regionService.deleteById(region.getId()), ResultStatus.SUCCESS);
}
@Test(description = "updateRegion, 参数非法测试")
public void updateRegion2ParamIllegalTest1() {
RegionDO regionDO = getRegionDO();
Assert.assertEquals(regionService.updateRegion(null), ResultStatus.PARAM_ILLEGAL);
Assert.assertEquals(regionService.updateRegion(regionDO), ResultStatus.PARAM_ILLEGAL);
}
@Test(description = "updateRegion, 资源不存在测试")
public void updateRegion2ResourceNotExistTest1() {
RegionDO regionDO = getRegionDO();
// 不插入Region直接更新
regionDO.setId(-1L);
Assert.assertEquals(regionService.updateRegion(regionDO), ResultStatus.RESOURCE_NOT_EXIST);
}
@Test(description = "updateRegion, brokerList未改变成功测试")
public void updateRegion2SuccessWithBrokerListNotChangeTest1() {
RegionDO regionDO = getRegionDO();
// 先在数据库中创建一个Region
Assert.assertEquals(regionService.createRegion(regionDO), ResultStatus.SUCCESS);
// 查询出创建的Region,并修改一些参数后作为新的Region
List<RegionDO> regionDOList = regionService.getByClusterId(1L);
RegionDO newRegionDO = regionDOList.get(0);
newRegionDO.setStatus(1);
Assert.assertEquals(regionService.updateRegion(newRegionDO), ResultStatus.SUCCESS);
}
@Test(description = "updateRegion, 传入的broker已经被使用测试")
public void updateRegion2ResourceAlreadyUsedTest1() {
RegionDO regionDO = getRegionDO();
// 先在数据库中创建一个Region
Assert.assertEquals(regionService.createRegion(regionDO), ResultStatus.SUCCESS);
// 查询出创建的Region,并修改brokerList后作为新的Region
List<RegionDO> regionDOList = regionService.getByClusterId(1L);
RegionDO newRegionDO = regionDOList.get(0);
List<Integer> newBrokerIdList = new ArrayList<>();
newBrokerIdList.add(1);
newBrokerIdList.add(3);
// 更新Region的brokerList
newRegionDO.setBrokerList(ListUtils.intList2String(newBrokerIdList));
// 构造情况
newRegionDO.setClusterId(null);
Assert.assertEquals(regionService.updateRegion(newRegionDO), ResultStatus.RESOURCE_ALREADY_USED);
}
@Test(description = "updateRegion, 更新的broker不存在")
public void updateRegion2BrokerNotExistTest1() {
RegionDO regionDO = getRegionDO();
// 先在数据库中创建一个Region
Assert.assertEquals(regionService.createRegion(regionDO), ResultStatus.SUCCESS);
// 查询出创建的Region,并修改brokerList后作为新的Region
List<RegionDO> regionDOList = regionService.getByClusterId(1L);
RegionDO newRegionDO = regionDOList.get(0);
// 构造情况
List<Integer> newBrokerIdList = new ArrayList<>();
newBrokerIdList.add(4);
newBrokerIdList.add(5);
newRegionDO.setBrokerList(ListUtils.intList2String(newBrokerIdList));
Assert.assertEquals(regionService.updateRegion(newRegionDO), ResultStatus.BROKER_NOT_EXIST);
}
@Test(description = "updateRegion, brokeList发生了改变成功测试")
public void updateRegion2SuccessWithBrokerListChangeTest1() {
RegionDO regionDO = getRegionDO();
// 查询出创建的Region,并修改brokerList后作为新的Region
List<RegionDO> regionDOList = regionService.getByClusterId(1L);
RegionDO newRegionDO = regionDOList.get(0);
// 构造情况
List<Integer> newBrokerIdList = new ArrayList<>();
newBrokerIdList.add(1);
newBrokerIdList.add(3);
newRegionDO.setBrokerList(ListUtils.intList2String(newBrokerIdList));
Assert.assertEquals(regionService.updateRegion(newRegionDO), ResultStatus.SUCCESS);
}
@Test(description = "updateRegion重载方法参数非法测试")
public void updateRegion2ParamIllegalTest2() {
RegionDO regionDO = getRegionDO();
Assert.assertEquals(regionService.updateRegion(null, "1,3"), ResultStatus.PARAM_ILLEGAL);
Assert.assertEquals(regionService.updateRegion(1L, "1, 3"), ResultStatus.PARAM_ILLEGAL);
}
@Test(description = "updateRegion重载方法成功测试")
public void updateRegion2SuccessTest2() {
RegionDO regionDO = getRegionDO();
Assert.assertEquals(regionService.createRegion(regionDO), ResultStatus.SUCCESS);
List<RegionDO> regionDOList = regionService.getByClusterId(1L);
RegionDO region = regionDOList.get(0);
Assert.assertEquals(regionService.updateRegion(region.getId(), "1,3"), ResultStatus.SUCCESS);
}
@Test
public void updateCapacityByIdTest() {
RegionDO regionDO = getRegionDO();
Assert.assertEquals(regionService.createRegion(regionDO), ResultStatus.SUCCESS);
RegionDO region = regionService.getByClusterId(1L).get(0);
region.setCapacity(1000L);
// 成功测试
updateCapacityById2SuccessTest(region);
// 失败测试
// 集群中不存在regionId是100的
region.setId(100L);
updateCapacityByIdFailureTest(region);
}
private void updateCapacityById2SuccessTest(RegionDO regionDO) {
Assert.assertEquals(regionService.updateCapacityById(regionDO), 1);
}
private void updateCapacityByIdFailureTest(RegionDO regionDO) {
Assert.assertEquals(regionService.updateCapacityById(regionDO), 0);
}
@Test
public void getByIdTest() {
RegionDO regionDO = getRegionDO();
Assert.assertEquals(regionService.createRegion(regionDO), ResultStatus.SUCCESS);
// 获取成功测试
RegionDO region = regionService.getByClusterId(1L).get(0);
getById2SuccessTest(region);
// 获取失败测试
region.setId(-1L);
getById2FailureTest(region);
}
private void getById2SuccessTest(RegionDO regionDO) {
Assert.assertEquals(regionService.getById(regionDO.getId()).toString(), regionDO.toString());
}
private void getById2FailureTest(RegionDO regionDO) {
Assert.assertNull(regionService.getById(regionDO.getId()));
}
private void getByClusterId2SuccessTest(RegionDO regionDO) {
Assert.assertNotNull(regionService.getByClusterId(regionDO.getClusterId()));
Assert.assertTrue(regionService.getByClusterId(regionDO.getClusterId()).stream().allMatch(regionDO1 ->
regionDO1.getName().equals(regionDO.getName())));
}
private void getByClusterId2FailureTest(RegionDO regionDO) {
Assert.assertTrue(regionService.getByClusterId(-1L).isEmpty());
}
@Test(dataProvider = "regionDO")
public void getRegionNumTest(RegionDO regionDO) {
// 插入一条数据
Map<Long, Integer> regionNum = regionService.getRegionNum();
for(Map.Entry<Long, Integer> entry : regionNum.entrySet()) {
Assert.assertEquals(entry.getKey(), Long.valueOf(1));
Assert.assertEquals(entry.getValue(), Integer.valueOf(1));
}
}
@Test(dataProvider = "regionDO")
public void getFullBrokerIdListTest(RegionDO regionDO) {
List<Integer> brokerIdList = new ArrayList<>();
brokerIdList.add(3);
// regionId是null测试
getFullBrokerIdList2RegionIdIsNullTest(regionDO, brokerIdList);
// 数据库中不存在对应的regionId数据
getFullBrokerIdList2RegionNotExistTest(regionDO, brokerIdList);
Assert.assertEquals(regionService.createRegion(regionDO), ResultStatus.SUCCESS);
RegionDO region = regionService.getByClusterId(1L).get(0);
// 传进来的brokerList是空的
getFullBrokerIdList2BrokerIdListIsEmpty(regionDO, region, new ArrayList<>());
// 传进来的brokerList不是空的
getFullBrokerIdList2Success(regionDO, region, brokerIdList);
}
private void getFullBrokerIdList2RegionIdIsNullTest(RegionDO regionDO, List<Integer> brokerIdList) {
List<Integer> fullBrokerIdList = regionService.getFullBrokerIdList(1L, null, brokerIdList);
Assert.assertEquals(fullBrokerIdList, brokerIdList);
}
private void getFullBrokerIdList2RegionNotExistTest(RegionDO regionDO, List<Integer> brokerIdList) {
Assert.assertEquals(regionService.getFullBrokerIdList(1L, -1L, brokerIdList), brokerIdList);
}
private void getFullBrokerIdList2BrokerIdListIsEmpty(RegionDO regionDO, RegionDO regionInDataBase, List<Integer> brokerIdList) {
List<Integer> fullBrokerIdList = regionService.getFullBrokerIdList(1L, regionInDataBase.getId(), brokerIdList);
Assert.assertEquals(fullBrokerIdList, ListUtils.string2IntList(regionInDataBase.getBrokerList()));
}
private void getFullBrokerIdList2Success(RegionDO regionDO, RegionDO regionInDataBase, List<Integer> brokerIdList) {
List<Integer> fullBrokerIdList = regionService.getFullBrokerIdList(1L, regionInDataBase.getId(), brokerIdList);
List<Integer> allBrokerIdList = ListUtils.string2IntList(regionInDataBase.getBrokerList());
allBrokerIdList.addAll(brokerIdList);
Assert.assertEquals(allBrokerIdList, fullBrokerIdList);
}
private void convert2BrokerIdRegionMap2RegionListDOIsNull() {
Assert.assertTrue(regionService.convert2BrokerIdRegionMap(null).isEmpty());
}
private void convert2BrokerIdRegionMap2Success(RegionDO regionDO) {
// 预期结果, key是brokerId, value是Region
List<RegionDO> regionDOList = regionService.getByClusterId(1L);
RegionDO region = regionDOList.get(0);
Map<Integer, RegionDO> brokerIdRegionDOMap = ListUtils.string2IntList(regionDO.getBrokerList()).stream().collect(Collectors.toMap(brokerId -> brokerId, regionDO1 -> region));
// 实际结果
Map<Integer, RegionDO> result = regionService.convert2BrokerIdRegionMap(regionDOList);
Assert.assertEquals(brokerIdRegionDOMap, result);
}
@Test(dataProvider = "regionDO")
public void getIdleRegionBrokerListTest(RegionDO regionDO) {
// 物理集群id和regionIdList是null测试
getIdleRegionBrokerList2PhysicalClusterIdIsNullTest();
// 参数物理集群下的regionDOList为空测试
getIdleRegionBrokerList2RegionDOListIsEmptyTest();
// 成功测试
getIdleRegionBrokerList2SuccessTest(regionDO);
}
private void getIdleRegionBrokerList2PhysicalClusterIdIsNullTest() {
Assert.assertNull(regionService.getIdleRegionBrokerList(null, new ArrayList<>()));
}
private void getIdleRegionBrokerList2RegionDOListIsEmptyTest() {
List<Long> regionIdList = new ArrayList<>();
regionIdList.add(-1L);
Assert.assertNull(regionService.getIdleRegionBrokerList(1L, regionIdList));
}
private void getIdleRegionBrokerList2SuccessTest(RegionDO regionDO) {
// 从数据库中查找
List<Long> regionIdList = regionService.getByClusterId(1L).stream().map(RegionDO::getId).collect(Collectors.toList());
List<Integer> brokerIdList = regionService.getByClusterId(1L)
.stream().flatMap(regionDO1 -> ListUtils.string2IntList(regionDO1.getBrokerList()).stream())
.collect(Collectors.toList());
Assert.assertEquals(regionService.getIdleRegionBrokerList(1L, regionIdList), brokerIdList);
}
@Test
public void getTopicNameRegionBrokerIdMap2SuccessTest() {
// 创建逻辑集群创建Topic均已在数据库写入
// 逻辑集群基于物理集群1建立region的brokerList是12
// Topic基于region建立也就是使用到broker1和2
// 这个方法是返回topicName -> topic所使用broker以及这些broker所在region中所有的broker
Map<String, Set<Integer>> topicNameRegionBrokerIdMap = regionService.getTopicNameRegionBrokerIdMap(1L);
Set<Integer> set = new HashSet<>();
set.add(1);
set.add(2);
Assert.assertEquals(topicNameRegionBrokerIdMap.get(REAL_TOPIC1_IN_ZK), set);
}
@Test
public void getRegionListByTopicNameTest() {
// 数据库中依然建立了Region, LogicalCluster, Topic
getRegionListByTopicName2EmptyTest();
// 返回集合不为空测试
getRegionListByTopicName2Success();
}
private void getRegionListByTopicName2EmptyTest() {
// 传入一个不存在的topic
Assert.assertEquals(regionService.getRegionListByTopicName(1L, "notExistTopic"), new ArrayList<>());
}
private void getRegionListByTopicName2Success() {
List<RegionDO> expectedResult = regionService.getByClusterId(1L);
Assert.assertEquals(regionService.getRegionListByTopicName(1L, REAL_TOPIC1_IN_ZK), expectedResult);
}
}

View File

@@ -0,0 +1,138 @@
package com.xiaojukeji.kafka.manager.service.service;
import com.xiaojukeji.kafka.manager.common.bizenum.KafkaClientEnum;
import com.xiaojukeji.kafka.manager.common.entity.metrics.TopicThrottledMetrics;
import com.xiaojukeji.kafka.manager.common.entity.pojo.TopicThrottledMetricsDO;
import com.xiaojukeji.kafka.manager.dao.TopicThrottledMetricsDao;
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.springframework.beans.factory.annotation.Value;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.*;
/**
* @author wyc
* @date 2021/12/24
*/
public class ThrottleServiceTest extends BaseTest {
@Value("${test.phyCluster.id}")
private Long REAL_CLUSTER_ID_IN_MYSQL;
@Value("${test.topic.name1}")
private String REAL_TOPIC_IN_ZK;
@Value("${test.app.id}")
private String KAFKA_MANAGER_APP_ID;
private final static Set<Integer> REAL_BROKER_ID_SET = new HashSet<>();
static {
REAL_BROKER_ID_SET.add(1);
REAL_BROKER_ID_SET.add(2);
}
@Autowired
@InjectMocks
private ThrottleService throttleService;
@Mock
private TopicThrottledMetricsDao topicThrottleDao;
@Mock
private JmxService jmxService;
@BeforeMethod
public void init() {
MockitoAnnotations.initMocks(this);
}
private TopicThrottledMetricsDO getTopicThrottledMetricsDO() {
TopicThrottledMetricsDO metricsDO = new TopicThrottledMetricsDO();
metricsDO.setAppId(KAFKA_MANAGER_APP_ID);
metricsDO.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
metricsDO.setTopicName(REAL_TOPIC_IN_ZK);
return metricsDO;
}
private TopicThrottledMetrics getTopicThrottledMetrics() {
TopicThrottledMetrics metrics = new TopicThrottledMetrics();
metrics.setClientType(KafkaClientEnum.PRODUCE_CLIENT);
metrics.setTopicName(REAL_TOPIC_IN_ZK);
metrics.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
return metrics;
}
@Test
public void insertBatchTest() {
// dataList为空测试
Assert.assertEquals(throttleService.insertBatch(new ArrayList<>()), 0);
// 成功测试
insertBatch2SuccessTest();
}
private void insertBatch2SuccessTest() {
TopicThrottledMetricsDO metricsDO = getTopicThrottledMetricsDO();
List<TopicThrottledMetricsDO> doList = new ArrayList<>();
doList.add(metricsDO);
Mockito.when(topicThrottleDao.insertBatch(Mockito.anyList())).thenReturn(3);
Assert.assertTrue(throttleService.insertBatch(doList) > 0);
}
@Test
public void getTopicThrottleFromDBTest() {
// 返回空集合测试
getTopicThrottleFromDB2TopicThrottleDOListIsEmptyTest();
// 成功测试
getTopicThrottleFromDB2SuccessTest();
}
private void getTopicThrottleFromDB2SuccessTest() {
TopicThrottledMetricsDO metricsDO = getTopicThrottledMetricsDO();
List<TopicThrottledMetricsDO> metricsDOList = new ArrayList<>();
metricsDOList.add(metricsDO);
Mockito.when(topicThrottleDao.getTopicThrottle(Mockito.anyLong(), Mockito.anyString(), Mockito.anyString(), Mockito.any(), Mockito.any())).thenReturn(metricsDOList);
Assert.assertTrue(throttleService.getTopicThrottleFromDB(REAL_CLUSTER_ID_IN_MYSQL, REAL_TOPIC_IN_ZK, KAFKA_MANAGER_APP_ID, new Date(), new Date()).stream().allMatch(metricsDO1 ->
metricsDO1.getAppId().equals(metricsDO.getAppId()) &&
metricsDO1.getClusterId().equals(metricsDO.getClusterId()) &&
metricsDO1.getTopicName().equals(metricsDO.getTopicName())));
}
private void getTopicThrottleFromDB2TopicThrottleDOListIsEmptyTest() {
Mockito.when(topicThrottleDao.getAppIdThrottle(Mockito.anyLong(), Mockito.anyString(), Mockito.any(), Mockito.any())).thenReturn(new ArrayList<>());
Assert.assertTrue(throttleService.getTopicThrottleFromDB(REAL_CLUSTER_ID_IN_MYSQL, REAL_TOPIC_IN_ZK, KAFKA_MANAGER_APP_ID, new Date(), new Date()).isEmpty());
}
@Test
public void getThrottledTopicsFromJmxTest() {
// 返回空集合测试
getThrottledTopicsFromJmx2ReturnEmptyTest();
// 返回成功测试
getThrottledTopicsFromJmx2SuccessTest();
}
private void getThrottledTopicsFromJmx2ReturnEmptyTest() {
Assert.assertTrue(throttleService.getThrottledTopicsFromJmx(REAL_CLUSTER_ID_IN_MYSQL, REAL_BROKER_ID_SET, new ArrayList<>()).isEmpty());
}
private void getThrottledTopicsFromJmx2SuccessTest() {
List<KafkaClientEnum> clientList = new ArrayList<>();
clientList.add(KafkaClientEnum.PRODUCE_CLIENT);
Assert.assertTrue(throttleService.getThrottledTopicsFromJmx(REAL_CLUSTER_ID_IN_MYSQL, REAL_BROKER_ID_SET, clientList).isEmpty());
}
}

View File

@@ -0,0 +1,76 @@
package com.xiaojukeji.kafka.manager.service.service;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.pojo.TopicExpiredDO;
import com.xiaojukeji.kafka.manager.dao.TopicExpiredDao;
import com.xiaojukeji.kafka.manager.service.config.BaseTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.List;
/**
* @author wyc
* @date 2021/12/20
*/
public class TopicExpiredServiceTest extends BaseTest {
/*
该topic在region_1上region_1使用了1,2broker该topic3个分区2个副本
*/
@Value("${test.topic.name4}")
private String REAL_TOPIC1_IN_ZK;
@Value("${test.phyCluster.id}")
private Long REAL_CLUSTER_ID_IN_MYSQL;
@Autowired
private TopicExpiredDao topicExpiredDao;
@Autowired
private TopicExpiredService topicExpiredService;
private TopicExpiredDO getTopicExpiredDO() {
TopicExpiredDO topicExpiredDO = new TopicExpiredDO();
topicExpiredDO.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
topicExpiredDO.setExpiredDay(30);
topicExpiredDO.setTopicName(REAL_TOPIC1_IN_ZK);
topicExpiredDO.setStatus(0);
return topicExpiredDO;
}
@Test
public void retainExpiredTopicTest() {
// 参数非法测试
Assert.assertEquals(topicExpiredService.retainExpiredTopic(1L, "topic_a", -1), ResultStatus.PARAM_ILLEGAL);
// Topic不存在测试
Assert.assertEquals(topicExpiredService.retainExpiredTopic(1L, "topicNotExist", 40), ResultStatus.TOPIC_NOT_EXIST);
// 成功测试
// 过期Topic插入到topic_expired表中时会先检查这个Topic是否在这个物理集群中所以测试基于集群中建立了"topic_a"的topic
topicExpiredDao.replace(getTopicExpiredDO());
Assert.assertEquals(topicExpiredService.retainExpiredTopic(1L, getTopicExpiredDO().getTopicName(), 40), ResultStatus.SUCCESS);
}
@Test
public void deleteByNameTest() {
// 删除失败
Assert.assertEquals(topicExpiredService.deleteByTopicName(1L, "notExistTopic"), 0);
// 删除成功
// 先在topic_expired表中插入数据,可以插入不存在的topic因为这个删除只是从数据库中删除删除的时候并没有检验topic是否存在于集群
// 根据返回值判断是否删除成功
TopicExpiredDO topicExpiredDO = getTopicExpiredDO();
topicExpiredDO.setTopicName("test-topic");
topicExpiredDao.replace(topicExpiredDO);
Assert.assertEquals(topicExpiredService.deleteByTopicName(getTopicExpiredDO().getClusterId(), "test-topic"), 1);
}
}

View File

@@ -0,0 +1,751 @@
package com.xiaojukeji.kafka.manager.service.service;
import com.xiaojukeji.kafka.manager.common.bizenum.KafkaClientEnum;
import com.xiaojukeji.kafka.manager.common.bizenum.TopicAuthorityEnum;
import com.xiaojukeji.kafka.manager.common.entity.Result;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.ao.RdTopicBasic;
import com.xiaojukeji.kafka.manager.common.entity.ao.topic.MineTopicSummary;
import com.xiaojukeji.kafka.manager.common.entity.ao.topic.TopicAppData;
import com.xiaojukeji.kafka.manager.common.entity.ao.topic.TopicBusinessInfo;
import com.xiaojukeji.kafka.manager.common.entity.ao.topic.TopicDTO;
import com.xiaojukeji.kafka.manager.common.entity.metrics.TopicThrottledMetrics;
import com.xiaojukeji.kafka.manager.common.entity.pojo.*;
import com.xiaojukeji.kafka.manager.common.entity.pojo.gateway.AppDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.gateway.AuthorityDO;
import com.xiaojukeji.kafka.manager.common.utils.ListUtils;
import com.xiaojukeji.kafka.manager.dao.TopicDao;
import com.xiaojukeji.kafka.manager.dao.TopicExpiredDao;
import com.xiaojukeji.kafka.manager.dao.TopicStatisticsDao;
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 com.xiaojukeji.kafka.manager.service.service.gateway.AuthorityService;
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.beans.factory.annotation.Value;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.*;
/**
* @author wyc
* @date 2021/12/21
*/
public class TopicManagerServiceTest extends BaseTest {
@Value("${test.phyCluster.id}")
private Long REAL_CLUSTER_ID_IN_MYSQL;
@Value("${test.topic.name4}")
private String REAL_TOPIC_IN_ZK;
@Value("${test.admin}")
private String ADMIN_NAME_IN_MYSQL;
private final static String KAFKA_MANAGER_APP_NAME = "KM管理员";
@Value("${test.app.id}")
private String KAFKA_MANAGER_APP_ID;
private final static Set<Integer> REAL_BROKER_ID_SET = new HashSet<>();
@Value("${test.region-name}")
private String REAL_REGION_IN_CLUSTER;
@Value("${test.logicalCluster.name}")
private String REAL_LOGICAL_CLUSTER_NAME;
// 共享集群
private final static Integer REAL_LOGICAL_CLUSTER_MODE = 0;
static {
REAL_BROKER_ID_SET.add(1);
REAL_BROKER_ID_SET.add(2);
}
@Autowired
@InjectMocks
private TopicManagerService topicManagerService;
@Mock
private TopicDao topicDao;
@Mock
private TopicStatisticsDao topicStatisticsDao;
@Mock
private TopicExpiredDao topicExpiredDao;
@Mock
private AppService appService;
@Mock
private ClusterService clusterService;
@Mock
private AuthorityService authorityService;
@Mock
private ThrottleService throttleService;
@Mock
private RegionService regionService;
@Mock
private LogicalClusterMetadataManager logicalClusterMetadataManager;
@BeforeMethod
public void init() {
MockitoAnnotations.initMocks(this);
}
private TopicDO getTopicDO() {
TopicDO topicDO = new TopicDO();
topicDO.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
topicDO.setTopicName("test_topic");
topicDO.setAppId("appId");
topicDO.setPeakBytesIn(100L);
return topicDO;
}
private TopicDO getTopicDOInCluster() {
// 这个Topic是通过工单手动在物理集群中插入的
TopicDO topicDO = new TopicDO();
topicDO.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
topicDO.setTopicName(REAL_TOPIC_IN_ZK);
topicDO.setAppId(KAFKA_MANAGER_APP_ID);
topicDO.setPeakBytesIn(100L);
return topicDO;
}
private TopicStatisticsDO getTopicStatisticsDO() {
// cluster_id, topic_name, offset_sum, max_avg_bytes_in, gmt_day
TopicStatisticsDO topicStatisticsDO = new TopicStatisticsDO();
topicStatisticsDO.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
topicStatisticsDO.setTopicName(REAL_TOPIC_IN_ZK);
topicStatisticsDO.setOffsetSum(1L);
topicStatisticsDO.setMaxAvgBytesIn(1.0);
topicStatisticsDO.setGmtDay("2020-03-30");
return topicStatisticsDO;
}
private TopicExpiredDO getTopicExpiredDO() {
TopicExpiredDO topicExpiredDO = new TopicExpiredDO();
return topicExpiredDO;
}
private AuthorityDO getAuthorityDO() {
AuthorityDO authorityDO = new AuthorityDO();
authorityDO.setAccess(3);
authorityDO.setAppId(KAFKA_MANAGER_APP_ID);
authorityDO.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
authorityDO.setTopicName(REAL_TOPIC_IN_ZK);
return authorityDO;
}
private TopicThrottledMetrics getTopicThrottledMetrics() {
TopicThrottledMetrics metrics = new TopicThrottledMetrics();
metrics.setAppId(KAFKA_MANAGER_APP_ID);
metrics.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
metrics.setTopicName(REAL_TOPIC_IN_ZK);
metrics.setClientType(KafkaClientEnum.PRODUCE_CLIENT);
metrics.setBrokerIdSet(REAL_BROKER_ID_SET);
return metrics;
}
private TopicAppData getTopicAppData() {
TopicAppData data = new TopicAppData();
data.setAccess(3);
data.setAppId(KAFKA_MANAGER_APP_ID);
data.setAppName(KAFKA_MANAGER_APP_NAME);
data.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
data.setTopicName(REAL_TOPIC_IN_ZK);
data.setAppPrincipals(ADMIN_NAME_IN_MYSQL);
data.setConsumerQuota(15728640L);
data.setProduceQuota(15728640L);
return data;
}
private ClusterDO getClusterDO() {
ClusterDO clusterDO = new ClusterDO();
clusterDO.setId(REAL_CLUSTER_ID_IN_MYSQL);
return clusterDO;
}
private RegionDO getRegionDO() {
RegionDO regionDO = new RegionDO();
regionDO.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
regionDO.setBrokerList(ListUtils.intList2String(new ArrayList<>(REAL_BROKER_ID_SET)));
regionDO.setName(REAL_REGION_IN_CLUSTER);
return regionDO;
}
private RdTopicBasic getRdTopicBasic() {
RdTopicBasic rdTopicBasic = new RdTopicBasic();
rdTopicBasic.setAppId(KAFKA_MANAGER_APP_ID);
rdTopicBasic.setAppName(KAFKA_MANAGER_APP_NAME);
List<String> regionNameList = new ArrayList<>();
regionNameList.add(REAL_REGION_IN_CLUSTER);
rdTopicBasic.setRegionNameList(regionNameList);
return rdTopicBasic;
}
private TopicBusinessInfo getTopicBusinessInfo() {
TopicBusinessInfo info = new TopicBusinessInfo();
info.setAppId(KAFKA_MANAGER_APP_ID);
info.setAppName(KAFKA_MANAGER_APP_NAME);
info.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
info.setPrincipals(ADMIN_NAME_IN_MYSQL);
info.setTopicName(REAL_TOPIC_IN_ZK);
return info;
}
private LogicalClusterDO getLogicalClusterDO() {
LogicalClusterDO logicalClusterDO = new LogicalClusterDO();
logicalClusterDO.setName(REAL_LOGICAL_CLUSTER_NAME);
logicalClusterDO.setMode(REAL_LOGICAL_CLUSTER_MODE);
logicalClusterDO.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
logicalClusterDO.setAppId(KAFKA_MANAGER_APP_ID);
return logicalClusterDO;
}
@Test
public void addTopicTest() {
// addTopic只是向数据库topic表中写数据
// 成功测试
Mockito.when(topicDao.insert(Mockito.any())).thenReturn(1);
Assert.assertEquals(topicManagerService.addTopic(getTopicDO()), 1);
// 失败测试,再次插入相同的Topic
Mockito.when(topicDao.insert(Mockito.any())).thenReturn(0);
Assert.assertEquals(topicManagerService.addTopic(getTopicDO()), 0);
}
@Test
public void deleteByTopicNameTest() {
// 删除也只是删除topic表中的数据
// 删除失败测试,删除一个不存在的
Mockito.when(topicDao.deleteByName(Mockito.anyLong(), Mockito.anyString())).thenReturn(0);
Assert.assertEquals(topicManagerService.deleteByTopicName(getTopicDO().getClusterId(), "notExistTopic"), 0);
// 删除成功测试
Mockito.when(topicDao.deleteByName(Mockito.anyLong(), Mockito.anyString())).thenReturn(1);
Assert.assertEquals(topicManagerService.deleteByTopicName(getTopicDO().getClusterId(), getTopicDO().getTopicName()), 1);
}
@Test(description = "物理集群中不存在该topic时的删除操作")
public void modifyTopic2TopicNotExist() {
Mockito.when(topicDao.getByTopicName(Mockito.anyLong(), Mockito.anyString())).thenReturn(null);
Assert.assertEquals(topicManagerService.modifyTopic(getTopicDO().getClusterId(), "notExistTopic", null, null), ResultStatus.TOPIC_NOT_EXIST);
}
@Test(description = "modifyTopic, 成功")
public void modifyTopic2Success() {
TopicDO topicDO = getTopicDOInCluster();
// 因为会检查集群中是否存在这个Topic因此直接用KM创建topic用这个Topic测试
Mockito.when(topicDao.getByTopicName(Mockito.anyLong(), Mockito.anyString())).thenReturn(topicDO);
Mockito.when(topicDao.updateByName(Mockito.any())).thenReturn(1);
Assert.assertEquals(topicManagerService.modifyTopic(REAL_CLUSTER_ID_IN_MYSQL, REAL_TOPIC_IN_ZK, "更改过", "operator"), ResultStatus.SUCCESS);
}
@Test(description = "modifyTopicByOp, 物理集群中不存在该topic")
public void modifyTopicByOp2TopicNotExistTest() {
Assert.assertEquals(topicManagerService.modifyTopicByOp(getTopicDOInCluster().getClusterId(), "notExistTopic", "dkm_admin", null, "admin"), ResultStatus.TOPIC_NOT_EXIST);
}
@Test(description = "modifyTopicByOp, 物理集群中不存在传入的appId")
public void modifyTopicByOp2AppNotExistTest() {
Mockito.when(appService.getByAppId(Mockito.anyString())).thenReturn(null);
Assert.assertEquals(topicManagerService.modifyTopicByOp(getTopicDOInCluster().getClusterId(), getTopicDOInCluster().getTopicName(), "notExistAppId", null, "admin"), ResultStatus.APP_NOT_EXIST);
}
@Test(description = "modifyTopicByOp, 成功测试")
public void modifyTopicByOp2SuccessTest() {
AppDO appDO = getAppDO();
Mockito.when(appService.getByAppId(Mockito.anyString())).thenReturn(appDO);
Assert.assertEquals(topicManagerService.modifyTopicByOp(getTopicDOInCluster().getClusterId(), getTopicDOInCluster().getTopicName(), getTopicDOInCluster().getAppId(), "", "admin"), ResultStatus.SUCCESS);
}
private void addAuthority2ClusterNotExistTest() {
AuthorityDO authorityDO = getAuthorityDO();
AppDO appDO = getAppDO();
Mockito.when(appService.getByAppId(Mockito.anyString())).thenReturn(appDO);
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(Mockito.anyLong())).thenReturn(null);
Assert.assertEquals(topicManagerService.addAuthority(authorityDO), ResultStatus.CLUSTER_NOT_EXIST);
}
@Test
public void listAllTest() {
// 准备mock返回数据
List<TopicDO> doList = new ArrayList<>();
doList.add(getTopicDO());
topicDao.insert(getTopicDO());
Mockito.when(topicDao.listAll()).thenReturn(doList);
List<TopicDO> topicDOS = topicManagerService.listAll();
Assert.assertFalse(topicDOS.isEmpty());
Assert.assertTrue(topicDOS.stream().allMatch(topicDO ->
topicDO.getClusterId().equals(getTopicDO().getClusterId()) &&
topicDO.getAppId().equals(getTopicDO().getAppId()) &&
topicDO.getTopicName().equals(getTopicDO().getTopicName())));
}
@Test
public void getByClusterIdFromCacheTest() {
// 返回空集合测试
getByClusterIdFromCache2ReturnEmptyListTest();
// 返回成功测试
getByClusterIdFromCache2SuccessTest();
}
private void getByClusterIdFromCache2ReturnEmptyListTest() {
Assert.assertTrue(topicManagerService.getByClusterIdFromCache(null).isEmpty());
}
private void getByClusterIdFromCache2SuccessTest() {
List<TopicDO> doList = new ArrayList<>();
doList.add(getTopicDO());
Mockito.when(topicDao.getByClusterIdFromCache(Mockito.anyLong())).thenReturn(doList);
Assert.assertEquals(topicManagerService.getByClusterIdFromCache(REAL_CLUSTER_ID_IN_MYSQL), doList);
}
@Test
public void getByClusterIdTest() {
// 返回空集合测试
getByClusterId2ReturnEmptyListTest();
// 返回成功测试
getByClusterId2SuccessTest();
}
private void getByClusterId2ReturnEmptyListTest() {
Assert.assertTrue(topicManagerService.getByClusterId(null).isEmpty());
}
private void getByClusterId2SuccessTest() {
List<TopicDO> doList = new ArrayList<>();
doList.add(getTopicDO());
Mockito.when(topicDao.getByClusterId(Mockito.anyLong())).thenReturn(doList);
Assert.assertEquals(topicManagerService.getByClusterId(REAL_CLUSTER_ID_IN_MYSQL), doList);
}
@Test
public void getByTopicNameTest() {
// 返回null测试
getByTopicName2ReturnNullTest();
// 成功测试
getByTopicName2SuccessTest();
}
private void getByTopicName2ReturnNullTest() {
Assert.assertNull(topicManagerService.getByTopicName(null, REAL_TOPIC_IN_ZK));
Assert.assertNull(topicManagerService.getByTopicName(REAL_CLUSTER_ID_IN_MYSQL, null));
}
private void getByTopicName2SuccessTest() {
TopicDO topicDOInCluster = getTopicDOInCluster();
Mockito.when(topicDao.getByTopicName(Mockito.anyLong(), Mockito.anyString())).thenReturn(topicDOInCluster);
Assert.assertEquals(topicManagerService.getByTopicName(REAL_CLUSTER_ID_IN_MYSQL, REAL_TOPIC_IN_ZK), topicDOInCluster);
}
@Test
public void replaceTopicStatisticsTest() {
// 失败测试
Mockito.when(topicStatisticsDao.replace(Mockito.any())).thenReturn(0);
Assert.assertEquals(topicManagerService.replaceTopicStatistics(new TopicStatisticsDO()), 0);
// 成功测试
Mockito.when(topicStatisticsDao.replace(Mockito.any())).thenReturn(1);
Assert.assertEquals(topicManagerService.replaceTopicStatistics(new TopicStatisticsDO()), 1);
}
@Test
public void getTopicMaxAvgBytesInTest() {
// 返回空Map测试
getTopicMaxAvgBytesIn2ReturnEmptyMapTest();
// 返回成功测试
getTopicMaxAvgBytesIn2SuccessTest();
}
private void getTopicMaxAvgBytesIn2ReturnEmptyMapTest() {
List<TopicStatisticsDO> doList = new ArrayList<>();
Mockito.when(topicStatisticsDao.getTopicStatisticData(Mockito.anyLong(), Mockito.any(), Mockito.anyDouble())).thenReturn(doList);
Assert.assertTrue(topicManagerService.getTopicMaxAvgBytesIn(REAL_CLUSTER_ID_IN_MYSQL,1, 1.0).size() == 0);
}
private void getTopicMaxAvgBytesIn2SuccessTest() {
List<TopicStatisticsDO> doList = new ArrayList<>();
TopicStatisticsDO topicStatisticsDO = getTopicStatisticsDO();
doList.add(topicStatisticsDO);
Mockito.when(topicStatisticsDao.getTopicStatisticData(Mockito.anyLong(), Mockito.any(), Mockito.anyDouble())).thenReturn(doList);
Map<String, List<Double>> expectMap = new HashMap<>();
List<Double> list = new ArrayList<>();
list.add(topicStatisticsDO.getMaxAvgBytesIn());
expectMap.put(topicStatisticsDO.getTopicName(), list);
Map<String, List<Double>> actualMap = topicManagerService.getTopicMaxAvgBytesIn(REAL_CLUSTER_ID_IN_MYSQL, 1, 1.0);
Assert.assertTrue(actualMap.keySet().stream().allMatch(key -> actualMap.get(key).equals(expectMap.get(key))));
}
@Test
public void getTopicMaxAvgBytesInTest2() {
// 返回空测试
getTopicMaxAvgBytesInTest2NullTest();
// 成功测试
getTopicMaxAvgBytesInTest2SuccessTest();
}
private void getTopicMaxAvgBytesInTest2NullTest() {
Mockito.when(topicStatisticsDao.getTopicMaxAvgBytesIn(Mockito.anyLong(), Mockito.anyString(), Mockito.any(), Mockito.any(), Mockito.anyInt())).thenReturn(null);
Assert.assertEquals(topicManagerService.getTopicMaxAvgBytesIn(REAL_CLUSTER_ID_IN_MYSQL, REAL_TOPIC_IN_ZK, new Date(), new Date(), 1), null);
}
private void getTopicMaxAvgBytesInTest2SuccessTest() {
Mockito.when(topicStatisticsDao.getTopicMaxAvgBytesIn(Mockito.anyLong(), Mockito.anyString(), Mockito.any(), Mockito.any(), Mockito.anyInt())).thenReturn(1.0);
Assert.assertEquals(topicManagerService.getTopicMaxAvgBytesIn(REAL_CLUSTER_ID_IN_MYSQL, REAL_TOPIC_IN_ZK, new Date(), new Date(), 1), 1.0);
}
@Test
public void getByTopicAndDayTest() {
TopicStatisticsDO topicStatisticsDO = getTopicStatisticsDO();
Mockito.when(topicStatisticsDao.getByTopicAndDay(Mockito.anyLong(), Mockito.anyString(), Mockito.anyString())).thenReturn(topicStatisticsDO);
Assert.assertEquals(topicManagerService.getByTopicAndDay(REAL_CLUSTER_ID_IN_MYSQL, REAL_TOPIC_IN_ZK, "2020-03-30"), topicStatisticsDO);
}
@Test
public void getExpiredTopicsTest() {
Mockito.when(topicExpiredDao.getExpiredTopics(Mockito.anyInt())).thenReturn(new ArrayList<>());
Assert.assertTrue(topicManagerService.getExpiredTopics(30).isEmpty());
}
private MineTopicSummary getMineTopicSummary() {
MineTopicSummary summary = new MineTopicSummary();
summary.setAppName(KAFKA_MANAGER_APP_NAME);
summary.setAccess(TopicAuthorityEnum.OWNER.getCode());
summary.setPhysicalClusterId(1L);
summary.setTopicName(REAL_TOPIC_IN_ZK);
return summary;
}
private TopicDO getRealTopicDO() {
TopicDO topicDO = new TopicDO();
topicDO.setAppId(KAFKA_MANAGER_APP_ID);
topicDO.setTopicName(REAL_TOPIC_IN_ZK);
topicDO.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
topicDO.setDescription("topic介绍");
topicDO.setAppId(KAFKA_MANAGER_APP_ID);
return topicDO;
}
private AppDO getAppDO() {
AppDO appDO = new AppDO();
appDO.setAppId(KAFKA_MANAGER_APP_ID);
appDO.setName(KAFKA_MANAGER_APP_NAME);
appDO.setPrincipals(ADMIN_NAME_IN_MYSQL);
appDO.setType(1);
return appDO;
}
@Test
public void getMyTopicsTest() {
MineTopicSummary mineTopicSummary = getMineTopicSummary();
List<TopicDO> topicDOList = new ArrayList<>();
TopicDO realTopicDO = getRealTopicDO();
topicDOList.add(realTopicDO);
List<AppDO> appDOList = new ArrayList<>();
AppDO appDO = getAppDO();
appDOList.add(appDO);
Mockito.when(topicDao.listAll()).thenReturn(topicDOList);
Mockito.when(topicDao.getByTopicName(Mockito.anyLong(), Mockito.anyString())).thenReturn(realTopicDO);
Mockito.when(appService.getByPrincipal(Mockito.anyString())).thenReturn(appDOList);
Assert.assertTrue(topicManagerService.getMyTopics(ADMIN_NAME_IN_MYSQL).stream().allMatch(summary ->
summary.getAccess().equals(mineTopicSummary.getAccess()) &&
summary.getAppName().equals(mineTopicSummary.getAppName()) &&
summary.getTopicName().equals(mineTopicSummary.getTopicName())));
}
@Test
public void getTopicsTest() {
// ClusterDO为空测试
getTopic2clusterDOListIsEmptyTest();
// appList为空测试
getTopic2AppListIsEmptyTest();
// 成功测试
getTopic2SuccessTest();
}
private TopicDTO getTopicDTO() {
TopicDTO topicDTO = new TopicDTO();
topicDTO.setAppId(KAFKA_MANAGER_APP_ID);
topicDTO.setAppName(KAFKA_MANAGER_APP_NAME);
topicDTO.setAppPrincipals(ADMIN_NAME_IN_MYSQL);
topicDTO.setTopicName(REAL_TOPIC_IN_ZK);
return topicDTO;
}
private void getTopic2clusterDOListIsEmptyTest() {
Mockito.when(clusterService.listAll()).thenReturn(new ArrayList<>());
Assert.assertTrue(topicManagerService.getTopics(ADMIN_NAME_IN_MYSQL).isEmpty());
}
private void getTopic2AppListIsEmptyTest() {
List<ClusterDO> clusterDOList = new ArrayList<>();
clusterDOList.add(new ClusterDO());
Mockito.when(clusterService.listAll()).thenReturn(clusterDOList);
Mockito.when(appService.listAll()).thenReturn(new ArrayList<>());
Assert.assertTrue(topicManagerService.getTopics(ADMIN_NAME_IN_MYSQL).isEmpty());
}
private void getTopic2SuccessTest() {
List<ClusterDO> clusterDOList = new ArrayList<>();
ClusterDO clusterDO = getClusterDO();
clusterDOList.add(clusterDO);
List<AppDO> appDOList = new ArrayList<>();
AppDO appDO = getAppDO();
appDOList.add(appDO);
TopicDTO topicDTO = getTopicDTO();
LogicalClusterDO logicalClusterDO = getLogicalClusterDO();
Mockito.when(clusterService.listAll()).thenReturn(clusterDOList);
Mockito.when(appService.listAll()).thenReturn(appDOList);
Mockito.when(logicalClusterMetadataManager.getTopicLogicalCluster(Mockito.anyLong(), Mockito.anyString())).thenReturn(logicalClusterDO);
Assert.assertTrue(topicManagerService.getTopics(ADMIN_NAME_IN_MYSQL).stream().allMatch(dto ->
dto.getAppId().equals(topicDTO.getAppId()) &&
dto.getAppName().equals(topicDTO.getAppName()) &&
dto.getTopicName().equals(topicDTO.getTopicName())));
}
@Test
public void getTopicAuthorizedAppsTest() {
// Topic不存在测试
getTopicAuthorizedApps2TopicNotExistTest();
// 没有权限测试
getTopicAuthorizedApps2NoAuthorityTest();
// 成功测试
getTopicAuthorizedApps2successTest();
}
private void getTopicAuthorizedApps2TopicNotExistTest() {
Assert.assertTrue(topicManagerService.getTopicAuthorizedApps(-1L, "notExistTopic").isEmpty());
}
private void getTopicAuthorizedApps2NoAuthorityTest() {
Mockito.when(authorityService.getAuthorityByTopic(Mockito.anyLong(), Mockito.anyString())).thenReturn(new ArrayList<>());
Assert.assertTrue(topicManagerService.getTopicAuthorizedApps(REAL_CLUSTER_ID_IN_MYSQL, REAL_TOPIC_IN_ZK).isEmpty());
}
private void getTopicAuthorizedApps2successTest() {
AuthorityDO authorityDO = getAuthorityDO();
List<AuthorityDO> authorityDOList = new ArrayList<>();
authorityDOList.add(authorityDO);
Mockito.when(authorityService.getAuthorityByTopic(Mockito.anyLong(), Mockito.anyString())).thenReturn(authorityDOList);
List<TopicThrottledMetrics> topicThrottledMetricsList = new ArrayList<>();
TopicThrottledMetrics topicThrottledMetrics = getTopicThrottledMetrics();
topicThrottledMetricsList.add(topicThrottledMetrics);
Mockito.when(throttleService.getThrottledTopicsFromJmx(Mockito.anyLong(), Mockito.anySet(), Mockito.anyList())).thenReturn(topicThrottledMetricsList);
AppDO appDO = getAppDO();
Mockito.when(appService.getByAppId(Mockito.anyString())).thenReturn(appDO);
TopicAppData topicAppData = getTopicAppData();
Assert.assertTrue(topicManagerService.getTopicAuthorizedApps(REAL_CLUSTER_ID_IN_MYSQL, REAL_TOPIC_IN_ZK).stream().allMatch(data ->
data.getAccess().equals(topicAppData.getAccess()) &&
data.getAppId().equals(topicAppData.getAppId()) &&
data.getTopicName().equals(topicAppData.getTopicName()) &&
data.getAppName().equals(topicAppData.getAppName()) &&
data.getAppPrincipals().equals(topicAppData.getAppPrincipals())));
}
@Test
public void getTopicMineAppsTest() {
// topic不存在测试
getTopicMineApps2TopicNotExistTest();
// appDOList为空测试
getTopicMineApps2AppDOListIsEmptyTest();
// 成功测试
getTopicMineApps2Success();
}
private void getTopicMineApps2TopicNotExistTest() {
Assert.assertTrue(topicManagerService.getTopicMineApps(-1L, "notExistTopic", ADMIN_NAME_IN_MYSQL).isEmpty());
}
private void getTopicMineApps2AppDOListIsEmptyTest() {
Mockito.when(appService.getByPrincipal(Mockito.anyString())).thenReturn(new ArrayList<>());
Assert.assertTrue(topicManagerService.getTopicMineApps(REAL_CLUSTER_ID_IN_MYSQL, REAL_TOPIC_IN_ZK, ADMIN_NAME_IN_MYSQL).isEmpty());
}
private void getTopicMineApps2Success() {
AppDO appDO = getAppDO();
List<AppDO> appDOList = new ArrayList<>();
appDOList.add(appDO);
Mockito.when(appService.getByPrincipal(Mockito.anyString())).thenReturn(appDOList);
AuthorityDO authorityDO = getAuthorityDO();
List<AuthorityDO> authorityDOList = new ArrayList<>();
authorityDOList.add(authorityDO);
Mockito.when(authorityService.getAuthorityByTopic(Mockito.anyLong(), Mockito.anyString())).thenReturn(authorityDOList);
List<TopicThrottledMetrics> topicThrottledMetricsList = new ArrayList<>();
TopicThrottledMetrics topicThrottledMetrics = getTopicThrottledMetrics();
topicThrottledMetricsList.add(topicThrottledMetrics);
Mockito.when(throttleService.getThrottledTopicsFromJmx(Mockito.anyLong(), Mockito.anySet(), Mockito.anyList())).thenReturn(topicThrottledMetricsList);
System.out.println(topicManagerService.getTopicMineApps(REAL_CLUSTER_ID_IN_MYSQL, REAL_TOPIC_IN_ZK, ADMIN_NAME_IN_MYSQL));
TopicAppData topicAppData = getTopicAppData();
Assert.assertFalse(topicManagerService.getTopicMineApps(REAL_CLUSTER_ID_IN_MYSQL, REAL_TOPIC_IN_ZK, ADMIN_NAME_IN_MYSQL).isEmpty());
}
@Test
public void getRdTopicBasicTest() {
// 集群不存在测试
getRdTopicBasic2ClusterNotExistTest();
// 成功测试
getRdTopicBasic2SuccessTest();
}
private void getRdTopicBasic2ClusterNotExistTest() {
Mockito.when(clusterService.getById(Mockito.anyLong())).thenReturn(null);
Assert.assertEquals(topicManagerService.getRdTopicBasic(REAL_CLUSTER_ID_IN_MYSQL, REAL_TOPIC_IN_ZK).toString(), Result.buildFrom(ResultStatus.CLUSTER_NOT_EXIST).toString());
}
private void getRdTopicBasic2SuccessTest() {
ClusterDO clusterDO = getClusterDO();
Mockito.when(clusterService.getById(REAL_CLUSTER_ID_IN_MYSQL)).thenReturn(clusterDO);
RegionDO regionDO = getRegionDO();
List<RegionDO> regionDOList = new ArrayList<>();
regionDOList.add(regionDO);
Mockito.when(regionService.getRegionListByTopicName(Mockito.anyLong(), Mockito.anyString())).thenReturn(regionDOList);
AppDO appDO = getAppDO();
Mockito.when(appService.getByAppId(Mockito.anyString())).thenReturn(appDO);
TopicDO topicDO = getTopicDOInCluster();
Mockito.when(topicDao.getByTopicName(Mockito.anyLong(), Mockito.anyString())).thenReturn(topicDO);
RdTopicBasic actualResult = topicManagerService.getRdTopicBasic(REAL_CLUSTER_ID_IN_MYSQL, REAL_TOPIC_IN_ZK).getData();
RdTopicBasic expectedResult = getRdTopicBasic();
Assert.assertNotNull(actualResult);
Assert.assertTrue(actualResult.getAppId().equals(expectedResult.getAppId()) &&
actualResult.getAppName().equals(expectedResult.getAppName()) &&
actualResult.getRegionNameList().equals(expectedResult.getRegionNameList()));
}
@Test
public void getTopicBusinessInfoTest() {
// TopicDO为null测试
getRdTopicBasic2SuccessTest();
// AppDO为null测试
getTopicBusinessInfo2AppDOIsNullTest();
// 成功测试
getTopicBusinessInfo2SuccessTest();
}
private void getTopicBusinessInfo2ReturnNullTest() {
Mockito.when(topicDao.getByTopicName(Mockito.anyLong(), Mockito.anyString())).thenReturn(null);
Assert.assertNull(topicManagerService.getTopicBusinessInfo(REAL_CLUSTER_ID_IN_MYSQL, REAL_TOPIC_IN_ZK));
}
private void getTopicBusinessInfo2AppDOIsNullTest() {
TopicDO topicDO = getTopicDOInCluster();
Mockito.when(topicDao.getByTopicName(Mockito.anyLong(), Mockito.anyString())).thenReturn(topicDO);
Mockito.when(appService.getByAppId(Mockito.anyString())).thenReturn(null);
TopicBusinessInfo expected = getTopicBusinessInfo();
expected.setAppId(null);
expected.setAppName(null);
expected.setPrincipals(null);
Assert.assertEquals(topicManagerService.getTopicBusinessInfo(REAL_CLUSTER_ID_IN_MYSQL, REAL_TOPIC_IN_ZK).toString(), expected.toString());
}
private void getTopicBusinessInfo2SuccessTest() {
TopicDO topicDO = getTopicDOInCluster();
Mockito.when(topicDao.getByTopicName(Mockito.anyLong(), Mockito.anyString())).thenReturn(topicDO);
AppDO appDO = getAppDO();
Mockito.when(appService.getByAppId(Mockito.anyString())).thenReturn(appDO);
TopicBusinessInfo expected = getTopicBusinessInfo();
Assert.assertEquals(topicManagerService.getTopicBusinessInfo(REAL_CLUSTER_ID_IN_MYSQL, REAL_TOPIC_IN_ZK).toString(), expected.toString());
}
@Test
public void getTopicStatisticTest() {
TopicStatisticsDO topicStatisticsDO = getTopicStatisticsDO();
List<TopicStatisticsDO> topicStatisticsDOList = new ArrayList<>();
topicStatisticsDOList.add(topicStatisticsDO);
Mockito.when(topicStatisticsDao.getTopicStatistic(Mockito.anyLong(), Mockito.anyString(), Mockito.any(), Mockito.any())).thenReturn(topicStatisticsDOList);
Assert.assertTrue(topicManagerService.getTopicStatistic(REAL_CLUSTER_ID_IN_MYSQL, REAL_TOPIC_IN_ZK, new Date(), new Date()).stream().allMatch(statisticsDO ->
statisticsDO.getClusterId().equals(topicStatisticsDO.getClusterId()) &&
statisticsDO.getMaxAvgBytesIn().equals(topicStatisticsDO.getMaxAvgBytesIn()) &&
statisticsDO.getOffsetSum().equals(topicStatisticsDO.getOffsetSum())));
}
@Test
public void addAuthorityTest() {
// app不存在测试
addAuthority2AppNotExistTest();
}
private void addAuthority2AppNotExistTest() {
AuthorityDO authorityDO = getAuthorityDO();
Assert.assertEquals(topicManagerService.addAuthority(authorityDO), ResultStatus.APP_NOT_EXIST);
}
}

View File

@@ -0,0 +1,776 @@
package com.xiaojukeji.kafka.manager.service.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xiaojukeji.kafka.manager.common.bizenum.OffsetPosEnum;
import com.xiaojukeji.kafka.manager.common.bizenum.TopicOffsetChangedEnum;
import com.xiaojukeji.kafka.manager.common.constant.TopicSampleConstant;
import com.xiaojukeji.kafka.manager.common.entity.Result;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.ao.PartitionAttributeDTO;
import com.xiaojukeji.kafka.manager.common.entity.ao.PartitionOffsetDTO;
import com.xiaojukeji.kafka.manager.common.entity.ao.topic.*;
import com.xiaojukeji.kafka.manager.common.entity.dto.normal.TopicDataSampleDTO;
import com.xiaojukeji.kafka.manager.common.entity.metrics.BaseMetrics;
import com.xiaojukeji.kafka.manager.common.entity.metrics.TopicMetrics;
import com.xiaojukeji.kafka.manager.common.entity.pojo.ClusterDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.TopicDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.TopicMetricsDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.TopicThrottledMetricsDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.gateway.AppDO;
import com.xiaojukeji.kafka.manager.common.zookeeper.znode.brokers.PartitionState;
import com.xiaojukeji.kafka.manager.dao.TopicAppMetricsDao;
import com.xiaojukeji.kafka.manager.dao.TopicMetricsDao;
import com.xiaojukeji.kafka.manager.dao.TopicRequestMetricsDao;
import com.xiaojukeji.kafka.manager.service.config.BaseTest;
import com.xiaojukeji.kafka.manager.service.service.gateway.AppService;
import org.apache.kafka.common.TopicPartition;
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.beans.factory.annotation.Value;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.*;
/**
* @author xuguang
* @Date 2021/12/20
*/
public class TopicServiceTest extends BaseTest {
/**
* 集群共包括三个broker:1,2,3, 该topic 1分区 1副本因子在broker1上
* 要求测试之前moduleTest这个topic需要有过生产者生产和消费者消费moduleTest
*/
@Value("${test.topic.name1}")
private String REAL_TOPIC1_IN_ZK;
/**
* 集群共包括三个broker:1,2,3, 该topic 2分区 3副本因子在broker1,2,3上
*/
@Value("${test.topic.name2}")
private String REAL_TOPIC2_IN_ZK;
private final static String INVALID_TOPIC = "xxxxx";
@Value("${test.topic.name6}")
private String ZK_DEFAULT_TOPIC;
/**
* 该topic同样需要被创建但是不能有流量
*/
@Value("${test.topic.name5}")
private String NO_OFFSET_CHANGE_TOPIC_IN_ZK;
@Value("${test.phyCluster.id}")
private Long REAL_CLUSTER_ID_IN_MYSQL;
@Value("${test.broker.id3}")
private Integer REAL_BROKER_ID_IN_ZK;
private final static Long INVALID_CLUSTER_ID = -1L;
private final static Integer INVALID_PARTITION_ID = -1;
@Value("${test.phyCluster.name}")
private String REAL_PHYSICAL_CLUSTER_NAME;
@Value("${test.ZK.address}")
private String ZOOKEEPER_ADDRESS;
@Value("${test.ZK.bootstrap-servers}")
private String BOOTSTRAP_SERVERS;
private final static String SECURITY_PROTOCOL = "{ \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\\\";\" }";
@Autowired
@InjectMocks
private TopicService topicService;
@Mock
private TopicManagerService topicManagerService;
@Mock
private AppService appService;
@Mock
private JmxService jmxService;
@Mock
private TopicMetricsDao topicMetricsDao;
@Mock
private ThrottleService topicThrottleService;
@Mock
private TopicAppMetricsDao topicAppMetricsDao;
@Mock
private TopicRequestMetricsDao topicRequestMetricsDao;
@BeforeMethod
public void setup() {
MockitoAnnotations.initMocks(this);
}
private TopicMetricsDO getTopicMetricsDO() {
TopicMetricsDO topicMetricsDO = new TopicMetricsDO();
topicMetricsDO.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
topicMetricsDO.setAppId("moduleTestAppId");
topicMetricsDO.setTopicName(REAL_TOPIC1_IN_ZK);
topicMetricsDO.setMetrics("");
topicMetricsDO.setGmtCreate(new Date());
return topicMetricsDO;
}
private TopicMetricsDO getTopicMetricsDO1() {
TopicMetricsDO topicMetricsDO = new TopicMetricsDO();
topicMetricsDO.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
topicMetricsDO.setAppId("moduleTestAppId");
topicMetricsDO.setTopicName(REAL_TOPIC1_IN_ZK);
String metrics = "{\"TotalFetchRequestsPerSecFiveMinuteRate\":4.132236103122026,\"BytesRejectedPerSecFiveMinuteRate\":0.0,\"TotalFetchRequestsPerSecFifteenMinuteRate\":1.5799208507558833,\"ProduceTotalTimeMs98thPercentile\":0.0,\"MessagesInPerSecMeanRate\":0.0,\"ProduceTotalTimeMs75thPercentile\":0.0,\"ProduceTotalTimeMs99thPercentile\":0.0,\"TotalProduceRequestsPerSecOneMinuteRate\":0.0,\"FailedProduceRequestsPerSecFifteenMinuteRate\":0.0,\"BytesInPerSecMeanRate\":0.0,\"TotalProduceRequestsPerSecFiveMinuteRate\":0.0,\"FetchConsumerTotalTimeMs999thPercentile\":0.0,\"FetchConsumerTotalTimeMs98thPercentile\":0.0,\"FetchConsumerTotalTimeMsMean\":0.0,\"FetchConsumerTotalTimeMs99thPercentile\":0.0,\"FailedFetchRequestsPerSecFifteenMinuteRate\":0.0,\"MessagesInPerSecFiveMinuteRate\":0.0,\"RequestHandlerAvgIdlePercentOneMinuteRate\":0.999221766772746,\"ProduceTotalTimeMsMean\":0.0,\"BytesInPerSecFiveMinuteRate\":0.0,\"FailedProduceRequestsPerSecMeanRate\":0.0,\"FailedFetchRequestsPerSecMeanRate\":0.0,\"FailedProduceRequestsPerSecFiveMinuteRate\":0.0,\"BytesOutPerSecFifteenMinuteRate\":0.0,\"BytesInPerSecOneMinuteRate\":0.0,\"BytesOutPerSecFiveMinuteRate\":0.0,\"HealthScore\":90,\"FailedFetchRequestsPerSecOneMinuteRate\":0.0,\"MessagesInPerSecOneMinuteRate\":0.0,\"BytesRejectedPerSecFifteenMinuteRate\":0.0,\"FailedFetchRequestsPerSecFiveMinuteRate\":0.0,\"RequestHandlerAvgIdlePercentFiveMinuteRate\":0.999803118809842,\"BytesOutPerSecOneMinuteRate\":0.0,\"ResponseQueueSizeValue\":0,\"MessagesInPerSecFifteenMinuteRate\":0.0,\"TotalProduceRequestsPerSecMeanRate\":0.0,\"BytesRejectedPerSecMeanRate\":0.0,\"TotalFetchRequestsPerSecMeanRate\":1.2674449706628523,\"NetworkProcessorAvgIdlePercentValue\":1.0,\"TotalFetchRequestsPerSecOneMinuteRate\":10.457259856316893,\"BytesInPerSecFifteenMinuteRate\":0.0,\"BytesOutPerSecMeanRate\":0.0,\"TotalProduceRequestsPerSecFifteenMinuteRate\":0.0,\"FetchConsumerTotalTimeMs50thPercentile\":0.0,\"RequestHandlerAvgIdlePercentFifteenMinuteRate\":0.9999287809186348,\"FetchConsumerTotalTimeMs95thPercentile\":0.0,\"FailedProduceRequestsPerSecOneMinuteRate\":0.0,\"CreateTime\":1638792321071,\"FetchConsumerTotalTimeMs75thPercentile\":0.0,\"ProduceTotalTimeMs999thPercentile\":0.0,\"RequestQueueSizeValue\":0,\"ProduceTotalTimeMs50thPercentile\":0.0,\"BytesRejectedPerSecOneMinuteRate\":0.0,\"RequestHandlerAvgIdlePercentMeanRate\":0.9999649184090593,\"ProduceTotalTimeMs95thPercentile\":0.0}";
topicMetricsDO.setMetrics(metrics);
topicMetricsDO.setGmtCreate(new Date(0L));
return topicMetricsDO;
}
private TopicDO getTopicDO() {
TopicDO topicDO = new TopicDO();
topicDO.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
topicDO.setTopicName(REAL_TOPIC1_IN_ZK);
topicDO.setAppId("moduleTestAppId");
topicDO.setDescription(INVALID_TOPIC);
topicDO.setPeakBytesIn(100000L);
return topicDO;
}
private AppDO getAppDO() {
AppDO appDO = new AppDO();
appDO.setId(4L);
appDO.setAppId("moduleTestAppId");
appDO.setName("moduleTestApp");
appDO.setPassword("moduleTestApp");
appDO.setType(1);
appDO.setApplicant("admin");
appDO.setPrincipals("admin");
appDO.setDescription("moduleTestApp");
appDO.setCreateTime(new Date(1638786493173L));
appDO.setModifyTime(new Date(1638786493173L));
return appDO;
}
public ClusterDO getClusterDO() {
ClusterDO clusterDO = new ClusterDO();
clusterDO.setId(REAL_CLUSTER_ID_IN_MYSQL);
clusterDO.setClusterName(REAL_PHYSICAL_CLUSTER_NAME);
clusterDO.setZookeeper(ZOOKEEPER_ADDRESS);
clusterDO.setBootstrapServers(BOOTSTRAP_SERVERS);
clusterDO.setSecurityProperties(SECURITY_PROTOCOL);
clusterDO.setStatus(1);
clusterDO.setGmtCreate(new Date());
clusterDO.setGmtModify(new Date());
return clusterDO;
}
private TopicDataSampleDTO getTopicDataSampleDTO() {
TopicDataSampleDTO topicDataSampleDTO = new TopicDataSampleDTO();
topicDataSampleDTO.setPartitionId(0);
topicDataSampleDTO.setOffset(0L);
topicDataSampleDTO.setTimeout(5000);
topicDataSampleDTO.setTruncate(true);
topicDataSampleDTO.setMaxMsgNum(90);
return topicDataSampleDTO;
}
private TopicMetrics getTopicMetrics() {
TopicMetrics topicMetrics = new TopicMetrics(REAL_CLUSTER_ID_IN_MYSQL, REAL_TOPIC1_IN_ZK);
String metrics = "{\"TotalFetchRequestsPerSecFiveMinuteRate\":4.132236103122026,\"BytesRejectedPerSecFiveMinuteRate\":0.0,\"TotalFetchRequestsPerSecFifteenMinuteRate\":1.5799208507558833,\"ProduceTotalTimeMs98thPercentile\":0.0,\"MessagesInPerSecMeanRate\":0.0,\"ProduceTotalTimeMs75thPercentile\":0.0,\"ProduceTotalTimeMs99thPercentile\":0.0,\"TotalProduceRequestsPerSecOneMinuteRate\":0.0,\"FailedProduceRequestsPerSecFifteenMinuteRate\":0.0,\"BytesInPerSecMeanRate\":0.0,\"TotalProduceRequestsPerSecFiveMinuteRate\":0.0,\"FetchConsumerTotalTimeMs999thPercentile\":0.0,\"FetchConsumerTotalTimeMs98thPercentile\":0.0,\"FetchConsumerTotalTimeMsMean\":0.0,\"FetchConsumerTotalTimeMs99thPercentile\":0.0,\"FailedFetchRequestsPerSecFifteenMinuteRate\":0.0,\"MessagesInPerSecFiveMinuteRate\":0.0,\"RequestHandlerAvgIdlePercentOneMinuteRate\":0.999221766772746,\"ProduceTotalTimeMsMean\":0.0,\"BytesInPerSecFiveMinuteRate\":0.0,\"FailedProduceRequestsPerSecMeanRate\":0.0,\"FailedFetchRequestsPerSecMeanRate\":0.0,\"FailedProduceRequestsPerSecFiveMinuteRate\":0.0,\"BytesOutPerSecFifteenMinuteRate\":0.0,\"BytesInPerSecOneMinuteRate\":0.0,\"BytesOutPerSecFiveMinuteRate\":0.0,\"HealthScore\":90,\"FailedFetchRequestsPerSecOneMinuteRate\":0.0,\"MessagesInPerSecOneMinuteRate\":0.0,\"BytesRejectedPerSecFifteenMinuteRate\":0.0,\"FailedFetchRequestsPerSecFiveMinuteRate\":0.0,\"RequestHandlerAvgIdlePercentFiveMinuteRate\":0.999803118809842,\"BytesOutPerSecOneMinuteRate\":0.0,\"ResponseQueueSizeValue\":0,\"MessagesInPerSecFifteenMinuteRate\":0.0,\"TotalProduceRequestsPerSecMeanRate\":0.0,\"BytesRejectedPerSecMeanRate\":0.0,\"TotalFetchRequestsPerSecMeanRate\":1.2674449706628523,\"NetworkProcessorAvgIdlePercentValue\":1.0,\"TotalFetchRequestsPerSecOneMinuteRate\":10.457259856316893,\"BytesInPerSecFifteenMinuteRate\":0.0,\"BytesOutPerSecMeanRate\":0.0,\"TotalProduceRequestsPerSecFifteenMinuteRate\":0.0,\"FetchConsumerTotalTimeMs50thPercentile\":0.0,\"RequestHandlerAvgIdlePercentFifteenMinuteRate\":0.9999287809186348,\"FetchConsumerTotalTimeMs95thPercentile\":0.0,\"FailedProduceRequestsPerSecOneMinuteRate\":0.0,\"CreateTime\":1638792321071,\"FetchConsumerTotalTimeMs75thPercentile\":0.0,\"ProduceTotalTimeMs999thPercentile\":0.0,\"RequestQueueSizeValue\":0,\"ProduceTotalTimeMs50thPercentile\":0.0,\"BytesRejectedPerSecOneMinuteRate\":0.0,\"RequestHandlerAvgIdlePercentMeanRate\":0.9999649184090593,\"ProduceTotalTimeMs95thPercentile\":0.0}";
JSONObject jsonObject = JSON.parseObject(metrics);
Map<String, Object> metricsMap = new HashMap<>();
for (Map.Entry<String, Object> stringObjectEntry : jsonObject.entrySet()) {
metricsMap.put(stringObjectEntry.getKey(), stringObjectEntry.getValue());
}
topicMetrics.setMetricsMap(metricsMap);
return topicMetrics;
}
private TopicThrottledMetricsDO getTopicThrottledMetricsDO() {
TopicThrottledMetricsDO throttledMetricsDO = new TopicThrottledMetricsDO();
throttledMetricsDO.setGmtCreate(new Date(1638792321071L));
throttledMetricsDO.setFetchThrottled(100);
throttledMetricsDO.setProduceThrottled(100);
return throttledMetricsDO;
}
@Test
public void getTopicMetricsFromDBWithAppIdTest() {
Mockito.when(topicMetricsDao.getTopicMetrics(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(Arrays.asList(getTopicMetricsDO1()));
Mockito.when(topicThrottleService.getTopicThrottleFromDB(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(Arrays.asList(getTopicThrottledMetricsDO()));
Mockito.when(topicAppMetricsDao.getTopicAppMetrics(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(Arrays.asList(getTopicMetricsDO1()));
List<TopicMetricsDTO> list = topicService.getTopicMetricsFromDB("moduleTestAppId", REAL_CLUSTER_ID_IN_MYSQL, REAL_TOPIC1_IN_ZK, new Date(0L), new Date());
Assert.assertFalse(list.isEmpty());
Assert.assertTrue(list.stream().allMatch(topicMetricsDTO -> topicMetricsDTO.getConsumeThrottled() && topicMetricsDTO.getProduceThrottled()));
}
@Test(description = "测试获取指定时间段内的峰值的均值流量")
public void getMaxAvgBytesInFromDBTest() {
// 为空
getMaxAvgBytesInFromDB2NullTest();
// 获取成功
getMaxAvgBytesInFromDB2SuccessTest();
}
private void getMaxAvgBytesInFromDB2NullTest() {
Double result = topicService.getMaxAvgBytesInFromDB(REAL_CLUSTER_ID_IN_MYSQL, INVALID_TOPIC, new Date(0L), new Date());
Assert.assertNull(result);
}
private void getMaxAvgBytesInFromDB2SuccessTest() {
Mockito.when(topicMetricsDao.getTopicMetrics(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(Arrays.asList(getTopicMetricsDO1()));
Double result = topicService.getMaxAvgBytesInFromDB(REAL_CLUSTER_ID_IN_MYSQL, REAL_TOPIC1_IN_ZK, new Date(0L), new Date());
Assert.assertNotNull(result);
}
@Test(description = "获取brokerId下所有的Topic及其对应的PartitionId")
public void getTopicPartitionIdMapTest() {
Map<String, List<Integer>> topicPartitionIdMap = topicService.getTopicPartitionIdMap(REAL_CLUSTER_ID_IN_MYSQL, 1);
Assert.assertFalse(topicPartitionIdMap.isEmpty());
Assert.assertTrue(topicPartitionIdMap.containsKey(REAL_TOPIC1_IN_ZK));
}
@Test(description = "测试获取 Topic 的 basic-info 信息")
public void getTopicBasicDTOTest() {
// TopicMetadata is Null
getTopicBasicDTO2TopicMetadataIsNull();
// TopicDO is Null
getTopicBasicDTO2TopicMetadata2TopicDOIsNull();
// TopicDO is not Null
getTopicBasicDTO2TopicMetadata2TopicDOIsNotNull();
}
private void getTopicBasicDTO2TopicMetadataIsNull() {
TopicBasicDTO result = topicService.getTopicBasicDTO(REAL_CLUSTER_ID_IN_MYSQL, INVALID_TOPIC);
Assert.assertEquals(result.getClusterId(), Long.valueOf(REAL_CLUSTER_ID_IN_MYSQL));
Assert.assertEquals(result.getTopicName(), INVALID_TOPIC);
Assert.assertNull(result.getAppId());
}
private void getTopicBasicDTO2TopicMetadata2TopicDOIsNull() {
Mockito.when(topicManagerService.getByTopicName(Mockito.anyLong(), Mockito.anyString())).thenReturn(null);
TopicBasicDTO result = topicService.getTopicBasicDTO(REAL_CLUSTER_ID_IN_MYSQL, REAL_TOPIC1_IN_ZK);
Assert.assertNotNull(result);
Assert.assertEquals(result.getClusterId(), Long.valueOf(REAL_CLUSTER_ID_IN_MYSQL));
Assert.assertEquals(result.getTopicName(), REAL_TOPIC1_IN_ZK);
Assert.assertNull(result.getDescription());
Assert.assertNull(result.getAppId());
}
private void getTopicBasicDTO2TopicMetadata2TopicDOIsNotNull() {
TopicDO topicDO = getTopicDO();
Mockito.when(topicManagerService.getByTopicName(Mockito.anyLong(), Mockito.anyString())).thenReturn(topicDO);
Mockito.when(appService.getByAppId(Mockito.anyString())).thenReturn(null);
TopicBasicDTO result = topicService.getTopicBasicDTO(REAL_CLUSTER_ID_IN_MYSQL, REAL_TOPIC1_IN_ZK);
Assert.assertNotNull(result);
Assert.assertEquals(result.getClusterId(), Long.valueOf(REAL_CLUSTER_ID_IN_MYSQL));
Assert.assertEquals(result.getTopicName(), REAL_TOPIC1_IN_ZK);
Assert.assertEquals(result.getDescription(), topicDO.getDescription());
// appId不存在
Assert.assertNull(result.getAppId());
// appId存在
topicDO.setAppId("moduleTestAppId");
Mockito.when(topicManagerService.getByTopicName(Mockito.anyLong(), Mockito.anyString())).thenReturn(topicDO);
Mockito.when(appService.getByAppId(Mockito.anyString())).thenReturn(getAppDO());
TopicBasicDTO result2 = topicService.getTopicBasicDTO(REAL_CLUSTER_ID_IN_MYSQL, REAL_TOPIC1_IN_ZK);
Assert.assertNotNull(result2);
Assert.assertEquals(result2.getClusterId(), Long.valueOf(REAL_CLUSTER_ID_IN_MYSQL));
Assert.assertEquals(result2.getTopicName(), REAL_TOPIC1_IN_ZK);
Assert.assertEquals(result2.getDescription(), topicDO.getDescription());
Assert.assertEquals(result2.getAppId(), topicDO.getAppId());
}
@Test(description = "获取Topic的PartitionState信息")
public void getTopicPartitionDTOTest() {
// result is emptyList
getTopicPartitionDTO2EmptyTest();
// needDetail is false
getTopicPartitionDTO2NeedDetailFalseTest();
// needDetail is true
getTopicPartitionDTO2NeedDetailTrueTest();
}
private void getTopicPartitionDTO2EmptyTest() {
List<TopicPartitionDTO> list = topicService.getTopicPartitionDTO(null, null, true);
Assert.assertTrue(list.isEmpty());
ClusterDO clusterDO = new ClusterDO();
clusterDO.setId(REAL_CLUSTER_ID_IN_MYSQL);
List<TopicPartitionDTO> list2 = topicService.getTopicPartitionDTO(clusterDO, INVALID_TOPIC, true);
Assert.assertTrue(list2.isEmpty());
}
private void getTopicPartitionDTO2NeedDetailFalseTest() {
Map<Integer, PartitionAttributeDTO> map = new HashMap<>();
PartitionAttributeDTO partitionAttributeDTO1 = new PartitionAttributeDTO();
partitionAttributeDTO1.setLogSize(0L);
map.put(0, partitionAttributeDTO1);
map.put(1, null);
Mockito.when(jmxService.getPartitionAttribute(
Mockito.anyLong(), Mockito.anyString(), Mockito.anyList())).thenReturn(map);
ClusterDO clusterDO = getClusterDO();
List<TopicPartitionDTO> list = topicService.getTopicPartitionDTO(clusterDO, REAL_TOPIC1_IN_ZK, false);
Assert.assertFalse(list.isEmpty());
Assert.assertEquals(list.size(), 1);
Assert.assertTrue(list.stream().allMatch(topicPartitionDTO ->
topicPartitionDTO.getBeginningOffset() == null &&
topicPartitionDTO.getEndOffset() == null));
}
private void getTopicPartitionDTO2NeedDetailTrueTest() {
Map<Integer, PartitionAttributeDTO> map = new HashMap<>();
PartitionAttributeDTO partitionAttributeDTO1 = new PartitionAttributeDTO();
partitionAttributeDTO1.setLogSize(0L);
map.put(0, partitionAttributeDTO1);
map.put(1, null);
Mockito.when(jmxService.getPartitionAttribute(
Mockito.anyLong(), Mockito.anyString(), Mockito.anyList())).thenReturn(map);
ClusterDO clusterDO = getClusterDO();
List<TopicPartitionDTO> list = topicService.getTopicPartitionDTO(clusterDO, REAL_TOPIC1_IN_ZK, true);
Assert.assertFalse(list.isEmpty());
Assert.assertEquals(list.size(), 1);
Assert.assertTrue(list.stream().allMatch(topicPartitionDTO ->
topicPartitionDTO.getBeginningOffset() != null &&
topicPartitionDTO.getEndOffset() != null));
}
@Test
public void getTopicMetricsFromJMXTest() {
Mockito.when(jmxService.getTopicMetrics(
Mockito.anyLong(), Mockito.anyString(), Mockito.anyInt(), Mockito.anyBoolean())).thenReturn(new TopicMetrics(REAL_CLUSTER_ID_IN_MYSQL, REAL_TOPIC1_IN_ZK));
BaseMetrics result = topicService.getTopicMetricsFromJMX(REAL_CLUSTER_ID_IN_MYSQL, REAL_TOPIC1_IN_ZK, 200, true);
Assert.assertNotNull(result);
}
@Test(description = "测试获取Topic的分区的offset")
public void getPartitionOffsetTest() {
// 结果为空
getPartitionOffset2EmptyTest();
// 获取成功
getPartitionOffset2SuccessTest();
}
private void getPartitionOffset2EmptyTest() {
ClusterDO clusterDO = getClusterDO();
Map<TopicPartition, Long> partitionOffset = topicService.getPartitionOffset(
null, null, OffsetPosEnum.BEGINNING);
Assert.assertTrue(partitionOffset.isEmpty());
Map<TopicPartition, Long> partitionOffset2 = topicService.getPartitionOffset(
clusterDO, INVALID_TOPIC, OffsetPosEnum.BEGINNING);
Assert.assertTrue(partitionOffset2.isEmpty());
}
private void getPartitionOffset2SuccessTest() {
ClusterDO clusterDO = getClusterDO();
// 获取beginning offset
Map<TopicPartition, Long> partitionOffset1 = topicService.getPartitionOffset(
clusterDO, REAL_TOPIC1_IN_ZK, OffsetPosEnum.BEGINNING);
Assert.assertFalse(partitionOffset1.isEmpty());
// 获取end offset
Map<TopicPartition, Long> partitionOffset2 = topicService.getPartitionOffset(
clusterDO, REAL_TOPIC1_IN_ZK, OffsetPosEnum.END);
Assert.assertFalse(partitionOffset2.isEmpty());
}
@Test(description = "测试获取Topic概览信息参数clusterId, brokerId")
public void getTopicOverviewListTest() {
// 结果为空
getTopicOverviewList2EmptyTest();
// 获取成功
getTopicOverviewList2SuccessTest();
}
private void getTopicOverviewList2EmptyTest() {
List<TopicOverview> topicOverviewList = topicService.getTopicOverviewList(null, 1);
Assert.assertTrue(topicOverviewList.isEmpty());
}
private void getTopicOverviewList2SuccessTest() {
List<TopicOverview> topicOverviewList = topicService.getTopicOverviewList(REAL_CLUSTER_ID_IN_MYSQL, 1);
Assert.assertFalse(topicOverviewList.isEmpty());
}
@Test(description = "测试获取Topic概览信息参数clusterId, topicNameList")
public void getTopicOverviewListWithTopicList() {
// 结果为空
getTopicOverviewListWithTopicList2EmptyTest();
// topicDOList is nullappDOList is null, metrics is null
getTopicOverviewListWithTopicList2TopicAndApp1Test();
// topicDOList is nullappDOList is null, metrics is not null
getTopicOverviewListWithTopicList2TopicAndApp2Test();
// topicDOList is nullappDOList is not null, metrics is null
getTopicOverviewListWithTopicList2TopicAndApp3Test();
// topicDOList is nullappDOList is not null, metrics is not null
getTopicOverviewListWithTopicList2TopicAndApp4Test();
// topicDOList is not nullappDOList is null, metrics is null
getTopicOverviewListWithTopicList2TopicAndApp5Test();
// topicDOList is not nullappDOList is null, metrics is not null
getTopicOverviewListWithTopicList2TopicAndApp6Test();
// topicDOList is not nullappDOList is not null, metrics is null
getTopicOverviewListWithTopicList2TopicAndApp7Test();
// topicDOList is not nullappDOList is not null, metrics is not null
getTopicOverviewListWithTopicList2TopicAndApp8Test();
}
private void getTopicOverviewListWithTopicList2EmptyTest() {
List<TopicOverview> topicOverviewList = topicService.getTopicOverviewList(null, Arrays.asList(REAL_TOPIC1_IN_ZK, ZK_DEFAULT_TOPIC, INVALID_TOPIC));
Assert.assertTrue(topicOverviewList.isEmpty());
}
private void getTopicOverviewListWithTopicList2TopicAndApp1Test() {
Mockito.when(topicManagerService.getByClusterIdFromCache(Mockito.anyLong())).thenReturn(null);
Mockito.when(appService.listAll()).thenReturn(null);
Mockito.when(jmxService.getTopicMetrics(
Mockito.anyLong(), Mockito.anyString(), Mockito.anyInt(), Mockito.anyBoolean())).thenReturn(null);
List<String> topics = Arrays.asList(REAL_TOPIC1_IN_ZK, ZK_DEFAULT_TOPIC, INVALID_TOPIC);
List<TopicOverview> topicOverviewList = topicService.getTopicOverviewList(REAL_CLUSTER_ID_IN_MYSQL, topics);
Assert.assertFalse(topicOverviewList.isEmpty());
Assert.assertTrue(topicOverviewList.stream().allMatch(topicOverview ->
topicOverview.getClusterId().equals(REAL_CLUSTER_ID_IN_MYSQL) &&
topics.contains(topicOverview.getTopicName()) &&
topicOverview.getAppId() == null &&
topicOverview.getAppName() == null &&
topicOverview.getByteIn() == null));
}
private void getTopicOverviewListWithTopicList2TopicAndApp2Test() {
Mockito.when(topicManagerService.getByClusterIdFromCache(Mockito.anyLong())).thenReturn(null);
Mockito.when(appService.listAll()).thenReturn(null);
TopicMetrics topicMetrics = getTopicMetrics();
Mockito.when(jmxService.getTopicMetrics(
Mockito.anyLong(), Mockito.anyString(), Mockito.anyInt(), Mockito.anyBoolean())).
thenReturn(topicMetrics);
List<String> topics = Arrays.asList(REAL_TOPIC1_IN_ZK, ZK_DEFAULT_TOPIC, INVALID_TOPIC);
List<TopicOverview> topicOverviewList = topicService.getTopicOverviewList(REAL_CLUSTER_ID_IN_MYSQL, topics);
Assert.assertFalse(topicOverviewList.isEmpty());
Assert.assertTrue(topicOverviewList.stream().allMatch(topicOverview ->
topicOverview.getClusterId().equals(REAL_CLUSTER_ID_IN_MYSQL) &&
topics.contains(topicOverview.getTopicName()) &&
topicOverview.getAppId() == null &&
topicOverview.getAppName() == null &&
topicOverview.getByteIn() != null));
}
private void getTopicOverviewListWithTopicList2TopicAndApp3Test() {
Mockito.when(topicManagerService.getByClusterIdFromCache(Mockito.anyLong())).thenReturn(null);
AppDO appDO = getAppDO();
Mockito.when(appService.listAll()).thenReturn(Arrays.asList(appDO));
Mockito.when(jmxService.getTopicMetrics(
Mockito.anyLong(), Mockito.anyString(), Mockito.anyInt(), Mockito.anyBoolean())).
thenReturn(null);
List<String> topics = Arrays.asList(REAL_TOPIC1_IN_ZK, ZK_DEFAULT_TOPIC, INVALID_TOPIC);
List<TopicOverview> topicOverviewList = topicService.getTopicOverviewList(REAL_CLUSTER_ID_IN_MYSQL, topics);
Assert.assertFalse(topicOverviewList.isEmpty());
Assert.assertTrue(topicOverviewList.stream().allMatch(topicOverview ->
topicOverview.getClusterId().equals(REAL_CLUSTER_ID_IN_MYSQL) &&
topics.contains(topicOverview.getTopicName()) &&
topicOverview.getAppId() == null &&
topicOverview.getAppName() == null &&
topicOverview.getByteIn() == null));
}
private void getTopicOverviewListWithTopicList2TopicAndApp4Test() {
Mockito.when(topicManagerService.getByClusterIdFromCache(Mockito.anyLong())).thenReturn(null);
AppDO appDO = getAppDO();
Mockito.when(appService.listAll()).thenReturn(Arrays.asList(appDO));
TopicMetrics topicMetrics = getTopicMetrics();
Mockito.when(jmxService.getTopicMetrics(
Mockito.anyLong(), Mockito.anyString(), Mockito.anyInt(), Mockito.anyBoolean())).
thenReturn(topicMetrics);
List<String> topics = Arrays.asList(REAL_TOPIC1_IN_ZK, ZK_DEFAULT_TOPIC, INVALID_TOPIC);
List<TopicOverview> topicOverviewList = topicService.getTopicOverviewList(REAL_CLUSTER_ID_IN_MYSQL, topics);
Assert.assertFalse(topicOverviewList.isEmpty());
Assert.assertTrue(topicOverviewList.stream().allMatch(topicOverview ->
topicOverview.getClusterId().equals(REAL_CLUSTER_ID_IN_MYSQL) &&
topics.contains(topicOverview.getTopicName()) &&
topicOverview.getAppId() == null &&
topicOverview.getAppName() == null &&
topicOverview.getByteIn() != null));
}
private void getTopicOverviewListWithTopicList2TopicAndApp5Test() {
TopicDO topicDO = getTopicDO();
Mockito.when(topicManagerService.getByClusterIdFromCache(Mockito.anyLong())).thenReturn(Arrays.asList(topicDO));
Mockito.when(appService.listAll()).thenReturn(null);
Mockito.when(jmxService.getTopicMetrics(
Mockito.anyLong(), Mockito.anyString(), Mockito.anyInt(), Mockito.anyBoolean())).thenReturn(null);
List<String> topics = Arrays.asList(REAL_TOPIC1_IN_ZK, ZK_DEFAULT_TOPIC, INVALID_TOPIC);
List<TopicOverview> topicOverviewList = topicService.getTopicOverviewList(REAL_CLUSTER_ID_IN_MYSQL, topics);
Assert.assertFalse(topicOverviewList.isEmpty());
Assert.assertTrue(topicOverviewList.stream().allMatch(topicOverview ->
topicOverview.getClusterId().equals(REAL_CLUSTER_ID_IN_MYSQL) &&
topics.contains(topicOverview.getTopicName()) &&
topicOverview.getAppId().equals(topicDO.getAppId()) &&
topicOverview.getAppName() == null &&
topicOverview.getByteIn() == null));
}
private void getTopicOverviewListWithTopicList2TopicAndApp6Test() {
TopicDO topicDO = getTopicDO();
Mockito.when(topicManagerService.getByClusterIdFromCache(Mockito.anyLong())).thenReturn(Arrays.asList(topicDO));
Mockito.when(appService.listAll()).thenReturn(null);
TopicMetrics topicMetrics = getTopicMetrics();
Mockito.when(jmxService.getTopicMetrics(
Mockito.anyLong(), Mockito.anyString(), Mockito.anyInt(), Mockito.anyBoolean())).
thenReturn(topicMetrics);
List<String> topics = Arrays.asList(REAL_TOPIC1_IN_ZK, ZK_DEFAULT_TOPIC, INVALID_TOPIC);
List<TopicOverview> topicOverviewList = topicService.getTopicOverviewList(REAL_CLUSTER_ID_IN_MYSQL, topics);
Assert.assertFalse(topicOverviewList.isEmpty());
Assert.assertTrue(topicOverviewList.stream().allMatch(topicOverview ->
topicOverview.getClusterId().equals(REAL_CLUSTER_ID_IN_MYSQL) &&
topics.contains(topicOverview.getTopicName()) &&
topicOverview.getAppId().equals(topicDO.getAppId()) &&
topicOverview.getAppName() == null &&
topicOverview.getByteIn() != null));
}
private void getTopicOverviewListWithTopicList2TopicAndApp7Test() {
TopicDO topicDO = getTopicDO();
Mockito.when(topicManagerService.getByClusterIdFromCache(Mockito.anyLong())).thenReturn(Arrays.asList(topicDO));
AppDO appDO = getAppDO();
Mockito.when(appService.listAll()).thenReturn(Arrays.asList(appDO));
Mockito.when(jmxService.getTopicMetrics(
Mockito.anyLong(), Mockito.anyString(), Mockito.anyInt(), Mockito.anyBoolean())).
thenReturn(null);
List<String> topics = Arrays.asList(REAL_TOPIC1_IN_ZK, ZK_DEFAULT_TOPIC, INVALID_TOPIC);
List<TopicOverview> topicOverviewList = topicService.getTopicOverviewList(REAL_CLUSTER_ID_IN_MYSQL, topics);
Assert.assertFalse(topicOverviewList.isEmpty());
Assert.assertTrue(topicOverviewList.stream().allMatch(topicOverview ->
topicOverview.getClusterId().equals(REAL_CLUSTER_ID_IN_MYSQL) &&
topics.contains(topicOverview.getTopicName()) &&
topicOverview.getAppId().equals(topicDO.getAppId()) &&
topicOverview.getAppName().equals(appDO.getName()) &&
topicOverview.getByteIn() == null));
}
private void getTopicOverviewListWithTopicList2TopicAndApp8Test() {
TopicDO topicDO = getTopicDO();
Mockito.when(topicManagerService.getByClusterIdFromCache(Mockito.anyLong())).thenReturn(Arrays.asList(topicDO));
AppDO appDO = getAppDO();
Mockito.when(appService.listAll()).thenReturn(Arrays.asList(appDO));
TopicMetrics topicMetrics = getTopicMetrics();
Mockito.when(jmxService.getTopicMetrics(
Mockito.anyLong(), Mockito.anyString(), Mockito.anyInt(), Mockito.anyBoolean())).
thenReturn(topicMetrics);
List<String> topics = Arrays.asList(REAL_TOPIC1_IN_ZK, ZK_DEFAULT_TOPIC, INVALID_TOPIC);
List<TopicOverview> topicOverviewList = topicService.getTopicOverviewList(REAL_CLUSTER_ID_IN_MYSQL, topics);
Assert.assertFalse(topicOverviewList.isEmpty());
Assert.assertTrue(topicOverviewList.stream().allMatch(topicOverview ->
topicOverview.getClusterId().equals(REAL_CLUSTER_ID_IN_MYSQL) &&
topics.contains(topicOverview.getTopicName()) &&
topicOverview.getAppId().equals(topicDO.getAppId()) &&
topicOverview.getAppName().equals(appDO.getName()) &&
topicOverview.getByteIn() != null));
}
@Test(description = "测试获取指定时间的offset信息")
public void getPartitionOffsetListTest() {
// 结果为空
getPartitionOffsetList2EmptyTest();
// 获取成功
getPartitionOffsetList2SuccessTest();
}
private void getPartitionOffsetList2EmptyTest() {
ClusterDO clusterDO = getClusterDO();
List<PartitionOffsetDTO> list = topicService.getPartitionOffsetList(clusterDO, INVALID_TOPIC, 0L);
Assert.assertTrue(list.isEmpty());
}
private void getPartitionOffsetList2SuccessTest() {
ClusterDO clusterDO = getClusterDO();
List<PartitionOffsetDTO> list = topicService.getPartitionOffsetList(clusterDO, REAL_TOPIC1_IN_ZK, 0L);
Assert.assertFalse(list.isEmpty());
}
@Test()
public void getTopicPartitionStateTest() {
// 结果为空
getTopicPartitionState2EmptyTest();
// 获取结果成功
getTopicPartitionState2SuccessTest();
}
private void getTopicPartitionState2EmptyTest() {
Map<String, List<PartitionState>> map1 =
topicService.getTopicPartitionState(null, REAL_BROKER_ID_IN_ZK);
Assert.assertTrue(map1.isEmpty());
Map<String, List<PartitionState>> map2 =
topicService.getTopicPartitionState(INVALID_CLUSTER_ID, REAL_BROKER_ID_IN_ZK);
Assert.assertTrue(map2.isEmpty());
}
/**
* 共有三个topic, REAL_TOPIC1_IN_ZK, REAL_TOPIC2_IN_ZK, ZK_DEFAULT_TOPIC
*/
private void getTopicPartitionState2SuccessTest() {
Map<String, List<PartitionState>> map1 =
topicService.getTopicPartitionState(REAL_CLUSTER_ID_IN_MYSQL, REAL_BROKER_ID_IN_ZK);
Assert.assertFalse(map1.isEmpty());
}
@Test(description = "测试数据采样")
public void fetchTopicDataTest() {
// invalid partitionId
fetchTopicData2InvalidPartitionId();
// 指定了offset,截断
fetchTopicData2OffsetAndTruncate();
// 指定了offset,未截断
fetchTopicData2OffsetAndNoTruncate();
// 未指定offset, 返回空
fetchTopicData2NoOffset2Empty();
// 未指定offset,截断
fetchTopicData2NoOffsetAndTruncate();
// 未指定offset,未截断
fetchTopicData2NoOffsetAndNoTruncate();
}
private void fetchTopicData2InvalidPartitionId() {
ClusterDO clusterDO = getClusterDO();
TopicDataSampleDTO topicDataSampleDTO = getTopicDataSampleDTO();
topicDataSampleDTO.setPartitionId(INVALID_PARTITION_ID);
List<String> result = topicService.fetchTopicData(clusterDO, REAL_TOPIC1_IN_ZK, topicDataSampleDTO);
Assert.assertTrue(result.isEmpty());
}
private void fetchTopicData2NoOffsetAndTruncate() {
ClusterDO clusterDO = getClusterDO();
TopicDataSampleDTO topicDataSampleDTO = getTopicDataSampleDTO();
topicDataSampleDTO.setOffset(null);
List<String> result = topicService.fetchTopicData(clusterDO, REAL_TOPIC1_IN_ZK, topicDataSampleDTO);
Assert.assertFalse(result.isEmpty());
Assert.assertTrue(result.stream().allMatch(
value -> value.length() <= TopicSampleConstant.MAX_DATA_LENGTH_UNIT_BYTE));
}
private void fetchTopicData2NoOffsetAndNoTruncate() {
ClusterDO clusterDO = getClusterDO();
TopicDataSampleDTO topicDataSampleDTO = getTopicDataSampleDTO();
topicDataSampleDTO.setOffset(null);
topicDataSampleDTO.setTruncate(false);
List<String> result = topicService.fetchTopicData(clusterDO, REAL_TOPIC1_IN_ZK, topicDataSampleDTO);
Assert.assertFalse(result.isEmpty());
Assert.assertTrue(result.stream().allMatch(
value -> value.length() != TopicSampleConstant.MAX_DATA_LENGTH_UNIT_BYTE));
}
private void fetchTopicData2OffsetAndTruncate() {
ClusterDO clusterDO = getClusterDO();
TopicDataSampleDTO topicDataSampleDTO = getTopicDataSampleDTO();
List<String> result = topicService.fetchTopicData(clusterDO, REAL_TOPIC1_IN_ZK, topicDataSampleDTO);
Assert.assertFalse(result.isEmpty());
Assert.assertTrue(result.stream().allMatch(
value -> value.length() <= TopicSampleConstant.MAX_DATA_LENGTH_UNIT_BYTE));
}
private void fetchTopicData2OffsetAndNoTruncate() {
ClusterDO clusterDO = getClusterDO();
TopicDataSampleDTO topicDataSampleDTO = getTopicDataSampleDTO();
topicDataSampleDTO.setTruncate(false);
List<String> result = topicService.fetchTopicData(clusterDO, REAL_TOPIC1_IN_ZK, topicDataSampleDTO);
Assert.assertFalse(result.isEmpty());
Assert.assertTrue(result.stream().allMatch(
value -> value.length() != TopicSampleConstant.MAX_DATA_LENGTH_UNIT_BYTE));
}
private void fetchTopicData2NoOffset2Empty() {
ClusterDO clusterDO = getClusterDO();
TopicDataSampleDTO topicDataSampleDTO = getTopicDataSampleDTO();
topicDataSampleDTO.setOffset(null);
topicDataSampleDTO.setTimeout(-1);
List<String> result = topicService.fetchTopicData(clusterDO, REAL_TOPIC1_IN_ZK, topicDataSampleDTO);
Assert.assertTrue(result.isEmpty());
}
@Test(description = "测试获取topic的broker列表")
public void getTopicBrokerListTest() {
List<TopicBrokerDTO> topicBrokerList = topicService.getTopicBrokerList(
REAL_CLUSTER_ID_IN_MYSQL, REAL_TOPIC2_IN_ZK);
Assert.assertFalse(topicBrokerList.isEmpty());
}
@Test(description = "测试topic是否有数据写入")
public void checkTopicOffsetChangedTest() {
// physicalCluster does not exist
checkTopicOffsetChanged2ClusterNotExistTest();
// endOffsetMap is empty
checkTopicOffsetChanged2UnknownTest();
// dtoList is not empty and result is Yes
checkTopicOffsetChanged2dtoListNotNullAndYesTest();
// dtoList is empty and result is No
checkTopicOffsetChanged2NoTest();
}
private void checkTopicOffsetChanged2ClusterNotExistTest() {
Result<TopicOffsetChangedEnum> result =
topicService.checkTopicOffsetChanged(INVALID_CLUSTER_ID, REAL_TOPIC1_IN_ZK, 0L);
Assert.assertEquals(result.getCode(), ResultStatus.CLUSTER_NOT_EXIST.getCode());
}
private void checkTopicOffsetChanged2UnknownTest() {
Result<TopicOffsetChangedEnum> result =
topicService.checkTopicOffsetChanged(REAL_CLUSTER_ID_IN_MYSQL, INVALID_TOPIC, 0L);
Assert.assertEquals(result.getData().getCode(), TopicOffsetChangedEnum.UNKNOWN.getCode());
}
private void checkTopicOffsetChanged2dtoListNotNullAndYesTest() {
Result<TopicOffsetChangedEnum> result = topicService.checkTopicOffsetChanged(
REAL_CLUSTER_ID_IN_MYSQL,
REAL_TOPIC1_IN_ZK,
System.currentTimeMillis());
Assert.assertNotNull(result);
Assert.assertEquals(result.getData().getCode(), TopicOffsetChangedEnum.YES.getCode());
}
private void checkTopicOffsetChanged2NoTest() {
Result<TopicOffsetChangedEnum> result = topicService.checkTopicOffsetChanged(
REAL_CLUSTER_ID_IN_MYSQL,
NO_OFFSET_CHANGE_TOPIC_IN_ZK,
System.currentTimeMillis());
Assert.assertNotNull(result);
Assert.assertEquals(result.getData().getCode(), TopicOffsetChangedEnum.NO.getCode());
}
}

View File

@@ -0,0 +1,238 @@
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.springframework.beans.factory.annotation.Value;
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;
@Value("${test.ZK.address}")
private String ZOOKEEPER_ADDRESS;
@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();
}
}

View File

@@ -0,0 +1,214 @@
package com.xiaojukeji.kafka.manager.service.service.gateway;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.ao.AppTopicDTO;
import com.xiaojukeji.kafka.manager.common.entity.dto.normal.AppDTO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.gateway.AppDO;
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/6
*/
public class AppServiceTest extends BaseTest {
@Autowired
private AppService appService;
@DataProvider(name = "provideAppDO")
public static Object[][] provideAppDO() {
AppDO appDO = new AppDO();
appDO.setId(4L);
appDO.setAppId("moduleTestAppId");
appDO.setName("moduleTestApp");
appDO.setPassword("moduleTestApp");
appDO.setType(1);
appDO.setApplicant("admin");
appDO.setPrincipals("admin");
appDO.setDescription("moduleTestApp");
appDO.setCreateTime(new Date(1638786493173L));
appDO.setModifyTime(new Date(1638786493173L));
return new Object[][] {{appDO}};
}
@DataProvider(name = "provideAppDTO")
public Object[][] provideAppDTO() {
AppDTO appDTO = new AppDTO();
appDTO.setAppId("testAppId");
appDTO.setName("testApp");
appDTO.setPrincipals("admin");
appDTO.setDescription("testApp");
return new Object[][] {{appDTO}};
}
private AppDO getAppDO() {
AppDO appDO = new AppDO();
appDO.setId(4L);
appDO.setAppId("testAppId");
appDO.setName("testApp");
appDO.setPassword("password");
appDO.setType(1);
appDO.setApplicant("admin");
appDO.setPrincipals("admin");
return appDO;
}
@Test(dataProvider = "provideAppDO")
public void addAppTest(AppDO appDO) {
// 测试app添加成功
addApp2SuccessTest(appDO);
// 测试app添加失败键重复
addApp2DuplicateKeyTest(appDO);
// 测试app添加失败
addApp2MysqlErrorTest();
}
private void addApp2SuccessTest(AppDO appDO) {
ResultStatus addAppResult = appService.addApp(appDO, "admin");
Assert.assertEquals(addAppResult.getCode(), ResultStatus.SUCCESS.getCode());
}
private void addApp2DuplicateKeyTest(AppDO appDO) {
ResultStatus addAppResult = appService.addApp(appDO, "admin");
Assert.assertEquals(addAppResult.getCode(), ResultStatus.RESOURCE_ALREADY_EXISTED.getCode());
}
private void addApp2MysqlErrorTest() {
ResultStatus addAppResult = appService.addApp(new AppDO(), "admin");
Assert.assertEquals(addAppResult.getCode(), ResultStatus.MYSQL_ERROR.getCode());
}
@Test(dataProvider = "provideAppDO")
public void deleteAppTest(AppDO appDO) {
appService.addApp(appDO, "admin");
// 测试删除app成功
deleteApp2SuccessTest(appDO);
// 测试删除app失败
deleteApp2FailureTest(appDO);
}
private void deleteApp2SuccessTest(AppDO appDO) {
appService.addApp(appDO, "admin");
int result = appService.deleteApp(appDO, "admin");
Assert.assertEquals(result, 1);
}
private void deleteApp2FailureTest(AppDO appDO) {
appService.addApp(appDO, "admin");
int result = appService.deleteApp(new AppDO(), "admin");
Assert.assertEquals(result, 0);
}
@Test(dataProvider = "provideAppDTO")
public void updateByAppIdTest(AppDTO appDTO) {
// 测试更新app时app不存在
updateByAppId2AppNotExistTest();
// 测试更新app时用户无权限
AppDO appDO = getAppDO();
appService.addApp(appDO, "admin");
updateByAppId2UserWithoutAuthorityTest(appDTO);
// 测试更新app成功
updateByAppId2SuccessTest(appDTO);
}
private void updateByAppId2AppNotExistTest() {
ResultStatus result = appService.updateByAppId(new AppDTO(), "admin", true);
Assert.assertEquals(result.getCode(), ResultStatus.APP_NOT_EXIST.getCode());
}
private void updateByAppId2UserWithoutAuthorityTest(AppDTO appDTO) {
ResultStatus result = appService.updateByAppId(appDTO, "xxx", false);
Assert.assertEquals(result.getCode(), ResultStatus.USER_WITHOUT_AUTHORITY.getCode());
}
private void updateByAppId2SuccessTest(AppDTO appDTO) {
ResultStatus result1 = appService.updateByAppId(appDTO, "admin", false);
Assert.assertEquals(result1.getCode(), ResultStatus.SUCCESS.getCode());
ResultStatus result2 = appService.updateByAppId(appDTO, "admin", true);
Assert.assertEquals(result2.getCode(), ResultStatus.SUCCESS.getCode());
ResultStatus result3 = appService.updateByAppId(appDTO, "xxx", true);
Assert.assertEquals(result3.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test
public void getAppByUserAndIdTest() {
// 测试查询app为空
getAppByUserAndId2NullTest();
// 测试查询app成功
getAppByUserAndId2SuccessTest();
}
private void getAppByUserAndId2NullTest() {
AppDO result1 = appService.getAppByUserAndId("xxx", "admin");
Assert.assertNull(result1);
AppDO result2 = appService.getAppByUserAndId("dkm_admin", "xxx");
Assert.assertNull(result2);
}
private void getAppByUserAndId2SuccessTest() {
AppDO result1 = appService.getAppByUserAndId("dkm_admin", "admin");
Assert.assertNotNull(result1);
}
@Test
public void verifyAppIdByPassword2DSucessTest() {
// 测试验证app成功
boolean result = appService.verifyAppIdByPassword("dkm_admin", "km_kMl4N8as1Kp0CCY");
Assert.assertTrue(result);
}
@Test(dataProvider = "provideAppIdAndPassword")
public void verifyAppIdByPassword2False(String appId, String password) {
// 测试验证app失败情况
boolean result = appService.verifyAppIdByPassword(appId, password);
Assert.assertFalse(result);
}
@DataProvider(name = "provideAppIdAndPassword")
public Object[][] provideAppIdAndPassword() {
return new Object[][] {{"", ""}, {"dkm_admin", ""}, {"xxx", "km_kMl4N8as1Kp0CCY"}, {"dkm_admin", "xxx"}};
}
@Test
public void getAppTopicDTOList2Test() {
// 测试获取的集合为空
getAppTopicDTOList2EmptyList();
// 测试查询成功,且集合不为空
getAppTopicDTOList2Success();
// TODO 查询的其他分支
}
private void getAppTopicDTOList2EmptyList() {
List<AppTopicDTO> result1 = appService.getAppTopicDTOList("xxx", true);
Assert.assertTrue(result1.isEmpty());
List<AppTopicDTO> result2 = appService.getAppTopicDTOList("dkm_admin", false);
Assert.assertTrue(result2.isEmpty());
List<AppTopicDTO> result3 = appService.getAppTopicDTOList("testAppId", true);
Assert.assertTrue(result3.isEmpty());
List<AppTopicDTO> result4 = appService.getAppTopicDTOList("testAppId", false);
Assert.assertTrue(result4.isEmpty());
}
private void getAppTopicDTOList2Success() {
List<AppTopicDTO> result = appService.getAppTopicDTOList("dkm_admin", true);
Assert.assertFalse(result.isEmpty());
}
}

View File

@@ -0,0 +1,242 @@
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;
/**
* @author xuguang
* @Date 2021/12/6
*/
public class AuthorityServiceTest extends BaseTest {
@Autowired
private AuthorityService authorityService;
@DataProvider(name = "provideAuthorityDO")
public Object[][] provideAuthorityDO() {
AuthorityDO authorityDO = new AuthorityDO();
authorityDO.setId(4L);
authorityDO.setAppId("appIdModuleTest");
authorityDO.setClusterId(1L);
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")
public void addAuthorityTest(AuthorityDO authorityDO) {
// 测试新增权限对象
addNewAuthority(authorityDO);
// 测试新旧对象权限一致
newAccessEqualOldAccessTest(authorityDO);
// 测试在原有对象上新增权限
addNewAuthorityAccessTest(authorityDO);
// 测试新增权限失败
addNewAuthority2Failure();
}
private void addNewAuthority(AuthorityDO authorityDO) {
int result = authorityService.addAuthority(authorityDO);
Assert.assertEquals(result, 1);
}
private void newAccessEqualOldAccessTest(AuthorityDO authorityDO) {
int result = authorityService.addAuthority(authorityDO);
Assert.assertEquals(result, 0);
}
private void addNewAuthorityAccessTest(AuthorityDO authorityDO) {
authorityDO.setAccess(3);
int result = authorityService.addAuthority(authorityDO);
Assert.assertEquals(result, 1);
}
private void addNewAuthority2Failure() {
int result = authorityService.addAuthority(new AuthorityDO());
Assert.assertEquals(result, 0);
}
@Test(dataProvider = "provideAuthorityDO", description = "测试删除权限对象")
public void deleteSpecifiedAccess(AuthorityDO authorityDO) {
// 测试删除权限对象时无该对象
deleteSpecifiedAccess2AuthorityNotExist();
// 测试删除权限对象时参数错误
deleteSpecifiedAccess2ParamIllegal(authorityDO);
// 测试删除权限对象成功
deleteSpecifiedAccess2Success(authorityDO);
}
private void deleteSpecifiedAccess2AuthorityNotExist() {
ResultStatus result = authorityService.deleteSpecifiedAccess("xxx", 1L, "moduleTest", 2, "admin");
Assert.assertEquals(ResultStatus.AUTHORITY_NOT_EXIST.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(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", description = "测试查询")
public void getAuthorityTest(AuthorityDO authorityDO) {
// 测试查询成功
getAuthority2SuccessTest(authorityDO);
// 测试查询为null
getAuthority2NullTest();
}
private void getAuthority2SuccessTest(AuthorityDO authorityDO) {
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 result = authorityService.getAuthority(10L, "moduleTest", "testAppId");
Assert.assertNull(result);
}
@Test(dataProvider = "provideAuthorityDO", description = "测试查询")
public void getAuthorityByTopicTest(AuthorityDO authorityDO) {
// 测试查询成功
getAuthorityByTopic2SuccessTest(authorityDO);
// 测试查询为null
getAuthorityByTopic2NullTest();
}
private void getAuthorityByTopic2SuccessTest(AuthorityDO authorityDO) {
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(100L, "moduleTestxxx");
Assert.assertTrue(result.isEmpty());
}
@Test(dataProvider = "provideAuthorityDO", description = "测试查询")
public void getAuthorityByAppIdTest(AuthorityDO authorityDO) {
// 测试查询成功
getAuthorityByAppId2SuccessTest(authorityDO);
// 测试查询为null
getAuthorityByAppId2NullTest();
}
private void getAuthorityByAppId2SuccessTest(AuthorityDO authorityDO) {
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() {
List<AuthorityDO> result = authorityService.getAuthority("xxx");
Assert.assertTrue(result.isEmpty());
}
@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);
}
private void addAuthorityAndQuota2FaliureTest(AuthorityDO authorityDO) {
authorityService.addAuthority(authorityDO);
// 重复插入
int result2 = authorityService.addAuthorityAndQuota(authorityDO, getTopicQuota());
Assert.assertEquals(result2, 0);
}
@Test(dataProvider = "provideAuthorityDO", description = "测试删除")
public void deleteAuthorityByTopicTest(AuthorityDO authorityDO) {
// 测试删除成功
deleteAuthorityByTopic2SuccessTest(authorityDO);
// 测试删除失败
deleteAuthorityByTopic2FailureTest();
}
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(100L, "moduleTest");
Assert.assertEquals(result, 0);
}
}

View File

@@ -0,0 +1,173 @@
package com.xiaojukeji.kafka.manager.service.service.gateway;
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.cache.LogicalClusterMetadataManager;
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;
/**
* @author xuguang
* @Date 2021/12/6
*/
public class QuotaServiceTest extends BaseTest {
@Autowired
@InjectMocks
private QuotaService quotaService;
@Mock
private LogicalClusterMetadataManager logicalClusterMetadataManager;
@Mock
private AuthorityService authorityService;
@BeforeMethod
public void init() {
MockitoAnnotations.initMocks(this);
}
@DataProvider(name = "provideTopicQuota")
public static Object[][] provideTopicQuota() {
TopicQuota topicQuotaDO = new TopicQuota();
topicQuotaDO.setAppId("testAppId");
topicQuotaDO.setClusterId(1L);
topicQuotaDO.setTopicName("moduleTest");
topicQuotaDO.setProduceQuota(100000L);
topicQuotaDO.setConsumeQuota(100000L);
return new Object[][] {{topicQuotaDO}};
}
private AuthorityDO getAuthority() {
AuthorityDO authorityDO = new AuthorityDO();
authorityDO.setAccess(0);
return authorityDO;
}
@Test(dataProvider = "provideTopicQuota")
public void addTopicQuotaTest(TopicQuota topicQuotaDO) {
// 测试新增成功
addTopicQuota2SuccessTest(topicQuotaDO);
// 测试新增失败
addTopicQuota2FailureTest(topicQuotaDO);
}
private void addTopicQuota2SuccessTest(TopicQuota topicQuotaDO) {
int result = quotaService.addTopicQuota(topicQuotaDO);
Assert.assertEquals(result, 1);
}
private void addTopicQuota2FailureTest(TopicQuota topicQuotaDO) {
topicQuotaDO.setClusterId(10L);
int result = quotaService.addTopicQuota(topicQuotaDO);
Assert.assertEquals(result, 0);
}
@Test(dataProvider = "provideTopicQuota")
public void addTopicQuotaWithAccessTest(TopicQuota topicQuotaDO) {
// 测试新增成功
addTopicQuotaWithAccess2SuccessTest(topicQuotaDO);
// 测试新增失败
addTopicQuotaWithAccess2FailureTest(topicQuotaDO);
}
private void addTopicQuotaWithAccess2SuccessTest(TopicQuota topicQuotaDO) {
int result = quotaService.addTopicQuota(topicQuotaDO, 2);
Assert.assertEquals(result, 1);
}
private void addTopicQuotaWithAccess2FailureTest(TopicQuota topicQuotaDO) {
topicQuotaDO.setClusterId(10L);
int result = quotaService.addTopicQuota(topicQuotaDO, 2);
Assert.assertEquals(result, 0);
}
@Test(dataProvider = "provideTopicQuota")
public void getQuotaFromZkTest(TopicQuota topicQuotaDO) {
// 测试查询成功
getQuotaFromZk2SuccessTest(topicQuotaDO);
// 测试查询失败
getQuotaFromZk2FailureTest();
}
private void getQuotaFromZk2SuccessTest(TopicQuota topicQuotaDO) {
TopicQuota result = quotaService.getQuotaFromZk(1L, "moduleTest", "testAppId");
Assert.assertNotNull(result);
Assert.assertEquals(result.toString(), topicQuotaDO.toString());
}
private void getQuotaFromZk2FailureTest() {
TopicQuota result = quotaService.getQuotaFromZk(10L, "moduleTest", "testAppId");
Assert.assertNull(result);
}
@Test
public void modifyProduceQuotaTest() {
// 测试修改成功
modifyProduceQuota2SuccessTest();
// 测试修改失败
modifyProduceQuota2FailureTest();
}
private void modifyProduceQuota2SuccessTest() {
Boolean result = quotaService.modifyProduceQuota(1L, "moduleTest", "testAppId", 100L);
Assert.assertTrue(result);
}
private void modifyProduceQuota2FailureTest() {
Boolean result1 = quotaService.modifyProduceQuota(10L, "moduleTest", "testAppId", 100L);
Assert.assertFalse(result1);
}
@Test(dataProvider = "provideTopicQuota")
public void addTopicQuotaByAuthorityTest(TopicQuota topicQuotaDO) {
// 测试新增时,无相应集群异常
addTopicQuotaByAuthority2ClusterNotExistTest(topicQuotaDO);
// 测试新增时,无权限异常
addTopicQuotaByAuthority2UserWithoutAuthority1Test(topicQuotaDO);
// 测试新增成功包含三个流程access为123时通过数据库修改
addTopicQuotaByAuthority2SuccessTest(topicQuotaDO);
// 测试新增时无法写入zk异常(关闭zk)包含三个流程access为123时通过数据库修改
// addTopicQuotaByAuthority2ZookeeperWriteFailedTest(topicQuotaDO);
}
private void addTopicQuotaByAuthority2SuccessTest(TopicQuota topicQuotaDO) {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(Mockito.any())).thenReturn(1L);
AuthorityDO authority = getAuthority();
authority.setAccess(2);
Mockito.when(authorityService.getAuthority(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(authority);
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(Mockito.any())).thenReturn(1L);
ResultStatus resultStatus = quotaService.addTopicQuotaByAuthority(topicQuotaDO);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
}
private void addTopicQuotaByAuthority2ClusterNotExistTest(TopicQuota topicQuotaDO) {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(Mockito.any())).thenReturn(null);
ResultStatus resultStatus = quotaService.addTopicQuotaByAuthority(topicQuotaDO);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.CLUSTER_NOT_EXIST.getCode());
}
private void addTopicQuotaByAuthority2UserWithoutAuthority1Test(TopicQuota topicQuotaDO) {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(Mockito.any())).thenReturn(1L);
Mockito.when(authorityService.getAuthority(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(null);
topicQuotaDO.setTopicName("xxx");
ResultStatus resultStatus1 = quotaService.addTopicQuotaByAuthority(topicQuotaDO);
Assert.assertEquals(resultStatus1.getCode(), ResultStatus.USER_WITHOUT_AUTHORITY.getCode());
}
private void addTopicQuotaByAuthority2ZookeeperWriteFailedTest(TopicQuota topicQuotaDO) {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(Mockito.any())).thenReturn(1L);
ResultStatus resultStatus = quotaService.addTopicQuotaByAuthority(topicQuotaDO);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.ZOOKEEPER_WRITE_FAILED.getCode());
}
}

View File

@@ -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())));
}
}

View File

@@ -0,0 +1,94 @@
package com.xiaojukeji.kafka.manager.service.service.gateway;
import com.xiaojukeji.kafka.manager.common.entity.ao.topic.TopicConnection;
import com.xiaojukeji.kafka.manager.common.entity.pojo.gateway.TopicConnectionDO;
import com.xiaojukeji.kafka.manager.service.config.BaseTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @author xuguang
* @Date 2021/12/7
*/
public class TopicConnectionServiceTest extends BaseTest {
@Autowired
private TopicConnectionService topicConnectionService;
@Value("${test.topic.name1}")
private String TOPIC_NAME;
@Value("${test.phyCluster.id}")
private Long CLUSTER_ID;
@Value("${test.app.id}")
private String APP_ID;
@Value("${test.gateway}")
private String GATEWAY;
public TopicConnectionDO getTopicConnectionDO() {
TopicConnectionDO topicConnectionDO = new TopicConnectionDO();
topicConnectionDO.setId(13L);
topicConnectionDO.setAppId(APP_ID);
topicConnectionDO.setClusterId(CLUSTER_ID);
topicConnectionDO.setTopicName(TOPIC_NAME);
topicConnectionDO.setType("fetch");
// topicConnectionDO.setIp("172.23.142.253");
topicConnectionDO.setIp(GATEWAY);
topicConnectionDO.setClientVersion("2.4");
topicConnectionDO.setCreateTime(new Date(1638786493173L));
return topicConnectionDO;
}
// 测试批量插入为空的情况
@Test
private void batchAdd2EmptyTest() {
topicConnectionService.batchAdd(new ArrayList<>());
}
// 测试批量插入成功的情况通过调整list的数量和TopicConnectionServiceImpl中splitInterval的数量使每个流程都测试一遍
@Test()
private void batchAdd2SuccessTest() {
TopicConnectionDO topicConnectionDO = getTopicConnectionDO();
List<TopicConnectionDO> list = new ArrayList<>();
list.add(topicConnectionDO);
list.add(topicConnectionDO);
list.add(topicConnectionDO);
topicConnectionService.batchAdd(list);
}
@Test()
public void getByTopicName2Test() {
TopicConnectionDO topicConnectionDO = getTopicConnectionDO();
List<TopicConnection> result = topicConnectionService.getByTopicName(CLUSTER_ID, TOPIC_NAME, new Date(0L), new Date());
Assert.assertFalse(result.isEmpty());
}
// 测试获取数据时为空
@Test
public void getByTopicName2EmptyTest() {
List<TopicConnection> result = topicConnectionService.getByTopicName(100L, "xgTestxxx", new Date(0L), new Date());
Assert.assertTrue(result.isEmpty());
}
// 测试获取数据,clusterId不为nullTODO
@Test()
public void getByTopicName2SuccessTest() {
TopicConnectionDO topicConnectionDO = getTopicConnectionDO();
List<TopicConnectionDO> list = new ArrayList<>();
list.add(topicConnectionDO);
topicConnectionService.batchAdd(list);
List<TopicConnection> result = topicConnectionService.getByTopicName(CLUSTER_ID, TOPIC_NAME, new Date(0L), new Date());
Assert.assertTrue(result.stream().anyMatch(topicConnection -> topicConnection.getTopicName().equals(TOPIC_NAME)
&& topicConnection.getClusterId().equals(CLUSTER_ID)));
}
}

View File

@@ -0,0 +1,35 @@
package com.xiaojukeji.kafka.manager.service.strategy;
import com.xiaojukeji.kafka.manager.common.entity.ao.gateway.TopicQuota;
import com.xiaojukeji.kafka.manager.service.config.BaseTest;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.annotations.Test;
/**
* @author xuguang
* @Date 2021/12/15
*/
public class AbstractAllocateQuotaStrategyTest extends BaseTest {
@Autowired
private AbstractAllocateQuotaStrategy abstractAllocateQuotaStrategy;
private TopicQuota getTopicQuota() {
TopicQuota topicQuota = new TopicQuota();
topicQuota.setTopicName("xxx");
topicQuota.setConsumeQuota(1000L);
topicQuota.setProduceQuota(1000L);
topicQuota.setAppId("xxxAppId");
topicQuota.setClusterId(1L);
return topicQuota;
}
@Test
public void getNewTopicQuotaTest() {
TopicQuota topicQuota = getTopicQuota();
TopicQuota newTopicQuota = abstractAllocateQuotaStrategy.getNewTopicQuota(topicQuota, 3, 1000L);
Assert.assertNotNull(newTopicQuota);
Assert.assertEquals(newTopicQuota.toString(), topicQuota.toString());
}
}

View File

@@ -0,0 +1,231 @@
package com.xiaojukeji.kafka.manager.service.strategy;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xiaojukeji.kafka.manager.common.constant.Constant;
import com.xiaojukeji.kafka.manager.common.entity.metrics.BrokerMetrics;
import com.xiaojukeji.kafka.manager.service.config.BaseTest;
import com.xiaojukeji.kafka.manager.service.service.JmxService;
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.Test;
import java.util.HashMap;
import java.util.Map;
/**
* @author xuguang
* @Date 2021/12/15
*/
public class AbstractHealthScoreStrategyTest extends BaseTest {
private BrokerMetrics getBrokerMetrics() {
BrokerMetrics brokerMetrics = new BrokerMetrics(1L, 1);
String metrics = "{\"TotalFetchRequestsPerSecFiveMinuteRate\":4.132236103122026,\"BytesRejectedPerSecFiveMinuteRate\":0.0,\"TotalFetchRequestsPerSecFifteenMinuteRate\":1.5799208507558833,\"ProduceTotalTimeMs98thPercentile\":0.0,\"MessagesInPerSecMeanRate\":0.0,\"ProduceTotalTimeMs75thPercentile\":0.0,\"ProduceTotalTimeMs99thPercentile\":0.0,\"TotalProduceRequestsPerSecOneMinuteRate\":0.0,\"FailedProduceRequestsPerSecFifteenMinuteRate\":0.0,\"BytesInPerSecMeanRate\":0.0,\"TotalProduceRequestsPerSecFiveMinuteRate\":0.0,\"FetchConsumerTotalTimeMs999thPercentile\":0.0,\"FetchConsumerTotalTimeMs98thPercentile\":0.0,\"FetchConsumerTotalTimeMsMean\":0.0,\"FetchConsumerTotalTimeMs99thPercentile\":0.0,\"FailedFetchRequestsPerSecFifteenMinuteRate\":0.0,\"MessagesInPerSecFiveMinuteRate\":0.0,\"RequestHandlerAvgIdlePercentOneMinuteRate\":0.999221766772746,\"ProduceTotalTimeMsMean\":0.0,\"BytesInPerSecFiveMinuteRate\":0.0,\"FailedProduceRequestsPerSecMeanRate\":0.0,\"FailedFetchRequestsPerSecMeanRate\":0.0,\"FailedProduceRequestsPerSecFiveMinuteRate\":0.0,\"BytesOutPerSecFifteenMinuteRate\":0.0,\"BytesInPerSecOneMinuteRate\":0.0,\"BytesOutPerSecFiveMinuteRate\":0.0,\"HealthScore\":90,\"FailedFetchRequestsPerSecOneMinuteRate\":0.0,\"MessagesInPerSecOneMinuteRate\":0.0,\"BytesRejectedPerSecFifteenMinuteRate\":0.0,\"FailedFetchRequestsPerSecFiveMinuteRate\":0.0,\"RequestHandlerAvgIdlePercentFiveMinuteRate\":0.999803118809842,\"BytesOutPerSecOneMinuteRate\":0.0,\"ResponseQueueSizeValue\":0,\"MessagesInPerSecFifteenMinuteRate\":0.0,\"TotalProduceRequestsPerSecMeanRate\":0.0,\"BytesRejectedPerSecMeanRate\":0.0,\"TotalFetchRequestsPerSecMeanRate\":1.2674449706628523,\"NetworkProcessorAvgIdlePercentValue\":1.0,\"TotalFetchRequestsPerSecOneMinuteRate\":10.457259856316893,\"BytesInPerSecFifteenMinuteRate\":0.0,\"BytesOutPerSecMeanRate\":0.0,\"TotalProduceRequestsPerSecFifteenMinuteRate\":0.0,\"FetchConsumerTotalTimeMs50thPercentile\":0.0,\"RequestHandlerAvgIdlePercentFifteenMinuteRate\":0.9999287809186348,\"FetchConsumerTotalTimeMs95thPercentile\":0.0,\"FailedProduceRequestsPerSecOneMinuteRate\":0.0,\"CreateTime\":1638792321071,\"FetchConsumerTotalTimeMs75thPercentile\":0.0,\"ProduceTotalTimeMs999thPercentile\":0.0,\"RequestQueueSizeValue\":0,\"ProduceTotalTimeMs50thPercentile\":0.0,\"BytesRejectedPerSecOneMinuteRate\":0.0,\"RequestHandlerAvgIdlePercentMeanRate\":0.9999649184090593,\"ProduceTotalTimeMs95thPercentile\":0.0}";
JSONObject jsonObject = JSON.parseObject(metrics);
Map<String, Object> metricsMap = new HashMap<>();
for (Map.Entry<String, Object> stringObjectEntry : jsonObject.entrySet()) {
metricsMap.put(stringObjectEntry.getKey(), stringObjectEntry.getValue());
}
brokerMetrics.setMetricsMap(metricsMap);
return brokerMetrics;
}
@Autowired
@InjectMocks
private AbstractHealthScoreStrategy didiHealthScoreStrategy;
@Mock
private JmxService jmxService;
@BeforeMethod
public void setup() {
MockitoAnnotations.initMocks(this);
}
@Test(description = "测试计算broker健康分")
public void calBrokerHealthScoreWithBrokerMetricsTest() {
// brokerMetrics为空时
calBrokerHealthScoreWithBrokerMetrics2InvideCodeTest();
// HEALTH_SCORE_VERY_BAD
calBrokerHealthScoreWithBrokerMetrics2HealthScoreVeryBadTest();
// requestQueueSizeValue is Null or responseQueueSizeValue is Null
calBrokerHealthScoreWithBrokerMetrics2requestQueueSizeValueNullTest();
// HEALTH_SCORE_BAD
calBrokerHealthScoreWithBrokerMetrics2HealthScoreBadTest();
// requestHandlerAvgIdlePercentOneMinuteRate is null
calBrokerHealthScoreWithBrokerMetrics2InvideCode3Test();
// HEALTH_SCORE_NORMAL
calBrokerHealthScoreWithBrokerMetrics2HealthScoreNormalTest();
// HEALTH_SCORE_Healthy
calBrokerHealthScoreWithBrokerMetrics2HealthScoreHealthyTest();
// exception
calBrokerHealthScoreWithBrokerMetrics2ExceptionTest();
}
private void calBrokerHealthScoreWithBrokerMetrics2InvideCodeTest() {
Integer result1 = didiHealthScoreStrategy.calBrokerHealthScore(1L, 1, null);
Assert.assertEquals(result1, Constant.INVALID_CODE);
Integer result2 = didiHealthScoreStrategy.calBrokerHealthScore(1L, 1, new BrokerMetrics(1L, 1));
Assert.assertEquals(result2, Constant.INVALID_CODE);
}
private void calBrokerHealthScoreWithBrokerMetrics2HealthScoreVeryBadTest() {
BrokerMetrics brokerMetrics = getBrokerMetrics();
Map<String, Object> metricsMap = brokerMetrics.getMetricsMap();
metricsMap.put("FailedFetchRequestsPerSecOneMinuteRate", 0.02);
metricsMap.put("FailedProduceRequestsPerSecOneMinuteRate", 0.02);
Integer result = didiHealthScoreStrategy.calBrokerHealthScore(1L, 1, brokerMetrics);
Assert.assertEquals(result, Integer.valueOf(30));
}
private void calBrokerHealthScoreWithBrokerMetrics2requestQueueSizeValueNullTest() {
BrokerMetrics brokerMetrics = getBrokerMetrics();
Map<String, Object> metricsMap = brokerMetrics.getMetricsMap();
metricsMap.put("FailedFetchRequestsPerSecOneMinuteRate", 0.0);
metricsMap.put("FailedProduceRequestsPerSecOneMinuteRate", 0.0);
metricsMap.put("RequestQueueSizeValue", null);
metricsMap.put("ResponseQueueSizeValue", null);
Integer result = didiHealthScoreStrategy.calBrokerHealthScore(1L, 1, brokerMetrics);
Assert.assertEquals(result, Constant.INVALID_CODE);
}
private void calBrokerHealthScoreWithBrokerMetrics2HealthScoreBadTest() {
BrokerMetrics brokerMetrics = getBrokerMetrics();
Map<String, Object> metricsMap = brokerMetrics.getMetricsMap();
metricsMap.put("FailedFetchRequestsPerSecOneMinuteRate", 0.0);
metricsMap.put("FailedProduceRequestsPerSecOneMinuteRate", 0.0);
metricsMap.put("RequestQueueSizeValue", 500);
metricsMap.put("ResponseQueueSizeValue", 500);
Integer result = didiHealthScoreStrategy.calBrokerHealthScore(1L, 1, brokerMetrics);
Assert.assertEquals(result, Integer.valueOf(60));
}
private void calBrokerHealthScoreWithBrokerMetrics2InvideCode3Test() {
BrokerMetrics brokerMetrics = getBrokerMetrics();
Map<String, Object> metricsMap = brokerMetrics.getMetricsMap();
metricsMap.put("FailedFetchRequestsPerSecOneMinuteRate", 0.0);
metricsMap.put("FailedProduceRequestsPerSecOneMinuteRate", 0.0);
metricsMap.put("RequestQueueSizeValue", 300);
metricsMap.put("ResponseQueueSizeValue", 300);
metricsMap.put("RequestHandlerAvgIdlePercentOneMinuteRate", null);
metricsMap.put("NetworkProcessorAvgIdlePercentValue", null);
Integer result = didiHealthScoreStrategy.calBrokerHealthScore(1L, 1, brokerMetrics);
Assert.assertEquals(result, Constant.INVALID_CODE);
}
private void calBrokerHealthScoreWithBrokerMetrics2HealthScoreNormalTest() {
BrokerMetrics brokerMetrics = getBrokerMetrics();
Map<String, Object> metricsMap = brokerMetrics.getMetricsMap();
metricsMap.put("FailedFetchRequestsPerSecOneMinuteRate", 0.0);
metricsMap.put("FailedProduceRequestsPerSecOneMinuteRate", 0.0);
metricsMap.put("RequestQueueSizeValue", 300);
metricsMap.put("ResponseQueueSizeValue", 300);
metricsMap.put("RequestHandlerAvgIdlePercentOneMinuteRate", 0.0);
metricsMap.put("NetworkProcessorAvgIdlePercentValue", 0.0);
Integer result = didiHealthScoreStrategy.calBrokerHealthScore(1L, 1, brokerMetrics);
Assert.assertEquals(result, Integer.valueOf(90));
}
private void calBrokerHealthScoreWithBrokerMetrics2HealthScoreHealthyTest() {
BrokerMetrics brokerMetrics = getBrokerMetrics();
Map<String, Object> metricsMap = brokerMetrics.getMetricsMap();
metricsMap.put("FailedFetchRequestsPerSecOneMinuteRate", 0.0);
metricsMap.put("FailedProduceRequestsPerSecOneMinuteRate", 0.0);
metricsMap.put("RequestQueueSizeValue", 300);
metricsMap.put("ResponseQueueSizeValue", 300);
metricsMap.put("RequestHandlerAvgIdlePercentOneMinuteRate", 100.0);
metricsMap.put("NetworkProcessorAvgIdlePercentValue", 100.0);
Integer result = didiHealthScoreStrategy.calBrokerHealthScore(1L, 1, brokerMetrics);
Assert.assertEquals(result, Integer.valueOf(100));
}
private void calBrokerHealthScoreWithBrokerMetrics2ExceptionTest() {
BrokerMetrics brokerMetrics = getBrokerMetrics();
Map<String, Object> metricsMap = brokerMetrics.getMetricsMap();
metricsMap.put("FailedFetchRequestsPerSecOneMinuteRate", 0.0);
metricsMap.put("FailedProduceRequestsPerSecOneMinuteRate", 0.0);
metricsMap.put("RequestQueueSizeValue", 300);
metricsMap.put("ResponseQueueSizeValue", 300);
// Integer转Double出现异常
metricsMap.put("RequestHandlerAvgIdlePercentOneMinuteRate", 100);
metricsMap.put("NetworkProcessorAvgIdlePercentValue", 100);
Integer result = didiHealthScoreStrategy.calBrokerHealthScore(1L, 1, brokerMetrics);
Assert.assertEquals(result, Constant.INVALID_CODE);
}
@Test(description = "测试计算broker健康分")
public void calBrokerHealthScoreTest() {
// BrokerMetadata is Null
calBrokerHealthScore2BrokerMetadataIsNullTest();
// INVALID_CODE
calBrokerHealthScore2InvideCodeTest();
// success
calBrokerHealthScore2SuccessTest();
}
private void calBrokerHealthScore2BrokerMetadataIsNullTest() {
Integer result = didiHealthScoreStrategy.calBrokerHealthScore(1L, 100);
Assert.assertEquals(result, Integer.valueOf(100));
}
private void calBrokerHealthScore2InvideCodeTest() {
Mockito.when(jmxService.getBrokerMetrics(Mockito.anyLong(), Mockito.anyInt(), Mockito.anyInt())).thenReturn(null);
Integer result = didiHealthScoreStrategy.calBrokerHealthScore(1L, 1);
Assert.assertEquals(result, Constant.INVALID_CODE);
}
private void calBrokerHealthScore2SuccessTest() {
BrokerMetrics brokerMetrics = getBrokerMetrics();
Map<String, Object> metricsMap = brokerMetrics.getMetricsMap();
metricsMap.put("FailedFetchRequestsPerSecOneMinuteRate", 0.0);
metricsMap.put("FailedProduceRequestsPerSecOneMinuteRate", 0.0);
metricsMap.put("RequestQueueSizeValue", 500);
metricsMap.put("ResponseQueueSizeValue", 500);
Mockito.when(jmxService.getBrokerMetrics(Mockito.anyLong(), Mockito.anyInt(), Mockito.anyInt())).thenReturn(brokerMetrics);
Integer result = didiHealthScoreStrategy.calBrokerHealthScore(1L, 1);
Assert.assertEquals(result, Integer.valueOf(60));
}
@Test(description = "测试计算健康分")
public void calTopicHealthScore() {
// TopicMetadata为空
calTopicHealthScore2InvadeCodeTest();
// 测试计算topic健康分成功
calTopicHealthScore2SuccessTest();
}
private void calTopicHealthScore2InvadeCodeTest() {
Integer result = didiHealthScoreStrategy.calTopicHealthScore(1L, "xxx");
Assert.assertEquals(result, Constant.INVALID_CODE);
}
private void calTopicHealthScore2SuccessTest() {
BrokerMetrics brokerMetrics = getBrokerMetrics();
Map<String, Object> metricsMap = brokerMetrics.getMetricsMap();
metricsMap.put("FailedFetchRequestsPerSecOneMinuteRate", 0.0);
metricsMap.put("FailedProduceRequestsPerSecOneMinuteRate", 0.0);
metricsMap.put("RequestQueueSizeValue", 500);
metricsMap.put("ResponseQueueSizeValue", 500);
Mockito.when(jmxService.getBrokerMetrics(Mockito.anyLong(), Mockito.anyInt(), Mockito.anyInt())).thenReturn(brokerMetrics);
Integer result = didiHealthScoreStrategy.calTopicHealthScore(1L, "xgTest");
Assert.assertNotEquals(result, Constant.INVALID_CODE);
}
}

View File

@@ -1,13 +0,0 @@
package com.xiaojukeji.kafka.manager.service.utils;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Created by arthur on 2017/5/31.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:biz-test.xml" })
public class SpringTestBase {
}

View File

@@ -0,0 +1,260 @@
package com.xiaojukeji.kafka.manager.service.utils;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.pojo.ClusterDO;
import com.xiaojukeji.kafka.manager.service.config.BaseTest;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.Properties;
/**
* @author xuguang
* @Date 2022/1/6
*/
public class TopicCommandsTest extends BaseTest {
private final static Long REAL_CLUSTER_ID_IN_MYSQL = 1L;
private final static String TEST_CREATE_TOPIC = "createTopicTest";
private final static String REAL_TOPIC1_IN_ZK2 = "expandPartitionTopic";
private final static String REAL_TOPIC_IN_ZK = "moduleTest";
private final static String INVALID_TOPIC = ".,&";
private final static Integer PARTITION_NUM = 1;
private final static Integer REPLICA_NUM = 1;
private final static Integer BROKER_ID = 1;
private final static String REAL_PHYSICAL_CLUSTER_NAME = "LogiKM_moduleTest";
private final static String ZOOKEEPER_ADDRESS = "10.190.12.242:2181,10.190.25.160:2181,10.190.25.41:2181/wyc";
private final static String BOOTSTRAP_SERVERS = "10.190.12.242:9093,10.190.25.160:9093,10.190.25.41:9093";
private final static String SECURITY_PROTOCOL = "{ \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\\\";\" }";
public ClusterDO getClusterDO() {
ClusterDO clusterDO = new ClusterDO();
clusterDO.setId(REAL_CLUSTER_ID_IN_MYSQL);
clusterDO.setClusterName(REAL_PHYSICAL_CLUSTER_NAME);
clusterDO.setZookeeper(ZOOKEEPER_ADDRESS);
clusterDO.setBootstrapServers(BOOTSTRAP_SERVERS);
clusterDO.setSecurityProperties(SECURITY_PROTOCOL);
clusterDO.setStatus(1);
clusterDO.setGmtCreate(new Date());
clusterDO.setGmtModify(new Date());
return clusterDO;
}
@Test(description = "测试创建topic")
public void createTopicTest() {
// NullPointerException
createTopic2NullPointerExceptionTest();
// InvalidPartitions
createTopic2InvalidPartitionsTest();
// InvalidReplicationFactor
createTopic2InvalidReplicationFactorTest();
// topic exists
createTopic2TopicExistsTest();
// invalid topic
createTopic2InvalidTopicTest();
// success
createTopic2SuccessTest();
}
private void createTopic2NullPointerExceptionTest() {
ClusterDO clusterDO = getClusterDO();
clusterDO.setZookeeper(null);
clusterDO.setBootstrapServers(null);
ResultStatus result = TopicCommands.createTopic(
clusterDO,
TEST_CREATE_TOPIC,
PARTITION_NUM,
REPLICA_NUM,
Arrays.asList(BROKER_ID),
new Properties()
);
Assert.assertEquals(result.getCode(), ResultStatus.TOPIC_OPERATION_PARAM_NULL_POINTER.getCode());
}
private void createTopic2InvalidPartitionsTest() {
ClusterDO clusterDO = getClusterDO();
ResultStatus result = TopicCommands.createTopic(
clusterDO,
TEST_CREATE_TOPIC,
-1,
REPLICA_NUM,
Arrays.asList(BROKER_ID),
new Properties()
);
Assert.assertEquals(result.getCode(), ResultStatus.TOPIC_OPERATION_PARTITION_NUM_ILLEGAL.getCode());
}
private void createTopic2InvalidReplicationFactorTest() {
ClusterDO clusterDO = getClusterDO();
ResultStatus result = TopicCommands.createTopic(
clusterDO,
TEST_CREATE_TOPIC,
PARTITION_NUM,
REPLICA_NUM,
Collections.emptyList(),
new Properties()
);
Assert.assertEquals(result.getCode(), ResultStatus.BROKER_NUM_NOT_ENOUGH.getCode());
}
private void createTopic2TopicExistsTest() {
ClusterDO clusterDO = getClusterDO();
ResultStatus result = TopicCommands.createTopic(
clusterDO,
REAL_TOPIC_IN_ZK,
PARTITION_NUM,
REPLICA_NUM,
Arrays.asList(BROKER_ID),
new Properties()
);
Assert.assertEquals(result.getCode(), ResultStatus.TOPIC_OPERATION_TOPIC_EXISTED.getCode());
}
private void createTopic2InvalidTopicTest() {
ClusterDO clusterDO = getClusterDO();
ResultStatus result = TopicCommands.createTopic(
clusterDO,
INVALID_TOPIC,
PARTITION_NUM,
REPLICA_NUM,
Arrays.asList(BROKER_ID),
new Properties()
);
Assert.assertEquals(result.getCode(), ResultStatus.TOPIC_OPERATION_TOPIC_NAME_ILLEGAL.getCode());
}
private void createTopic2SuccessTest() {
ClusterDO clusterDO = getClusterDO();
ResultStatus result = TopicCommands.createTopic(
clusterDO,
TEST_CREATE_TOPIC,
PARTITION_NUM,
REPLICA_NUM,
Arrays.asList(BROKER_ID),
new Properties()
);
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
// 删除这个Topic
ResultStatus result1 = TopicCommands.deleteTopic(clusterDO, TEST_CREATE_TOPIC);
Assert.assertEquals(result1.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test(description = "测试修改topic配置")
public void modifyTopicConfigTest() {
// AdminOperationException
modifyTopicConfig2AdminOperationExceptionTest();
// InvalidConfigurationException
modifyTopicConfig2InvalidConfigurationExceptionTest();
// success
modifyTopicConfig2SuccessTest();
}
private void modifyTopicConfig2AdminOperationExceptionTest() {
ClusterDO clusterDO = getClusterDO();
ResultStatus result = TopicCommands.modifyTopicConfig(clusterDO, INVALID_TOPIC, new Properties());
Assert.assertEquals(result.getCode(), ResultStatus.TOPIC_OPERATION_UNKNOWN_TOPIC_PARTITION.getCode());
}
private void modifyTopicConfig2InvalidConfigurationExceptionTest() {
ClusterDO clusterDO = getClusterDO();
Properties properties = new Properties();
properties.setProperty("xxx", "xxx");
ResultStatus result = TopicCommands.modifyTopicConfig(clusterDO, REAL_TOPIC_IN_ZK, properties);
Assert.assertEquals(result.getCode(), ResultStatus.TOPIC_OPERATION_TOPIC_CONFIG_ILLEGAL.getCode());
}
private void modifyTopicConfig2SuccessTest() {
ClusterDO clusterDO = getClusterDO();
Properties properties = new Properties();
ResultStatus result = TopicCommands.modifyTopicConfig(clusterDO, REAL_TOPIC_IN_ZK, properties);
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test(description = "测试扩分区")
public void expandTopicTest() {
// failed
expandTopic2FailureTest();
// success
expandTopic2SuccessTest();
}
private void expandTopic2FailureTest() {
ClusterDO clusterDO = getClusterDO();
ResultStatus result = TopicCommands.expandTopic(
clusterDO,
INVALID_TOPIC,
PARTITION_NUM + 1,
Arrays.asList(BROKER_ID, 2)
);
Assert.assertEquals(result.getCode(), ResultStatus.TOPIC_OPERATION_UNKNOWN_ERROR.getCode());
}
private void expandTopic2SuccessTest() {
ClusterDO clusterDO = getClusterDO();
ResultStatus result = TopicCommands.expandTopic(
clusterDO,
REAL_TOPIC1_IN_ZK2,
PARTITION_NUM + 1,
Arrays.asList(BROKER_ID, 2)
);
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test(description = "测试删除Topic")
public void deleteTopicTest() {
// UnknownTopicOrPartitionException
deleteTopic2UnknownTopicOrPartitionExceptionTest();
// NullPointerException
deleteTopic2NullPointerExceptionTest();
// success
deleteTopic2SuccessTest();
}
private void deleteTopic2UnknownTopicOrPartitionExceptionTest() {
ClusterDO clusterDO = getClusterDO();
ResultStatus result = TopicCommands.deleteTopic(clusterDO, INVALID_TOPIC);
Assert.assertEquals(result.getCode(), ResultStatus.TOPIC_OPERATION_UNKNOWN_TOPIC_PARTITION.getCode());
}
private void deleteTopic2NullPointerExceptionTest() {
ClusterDO clusterDO = getClusterDO();
clusterDO.setBootstrapServers(null);
clusterDO.setZookeeper(null);
ResultStatus result = TopicCommands.deleteTopic(clusterDO, INVALID_TOPIC);
Assert.assertEquals(result.getCode(), ResultStatus.TOPIC_OPERATION_UNKNOWN_ERROR.getCode());
}
private void deleteTopic2SuccessTest() {
// 需要先创建这个Topic
ClusterDO clusterDO = getClusterDO();
ResultStatus result = TopicCommands.createTopic(
clusterDO,
TEST_CREATE_TOPIC,
PARTITION_NUM,
REPLICA_NUM,
Arrays.asList(BROKER_ID),
new Properties()
);
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
ResultStatus result1 = TopicCommands.deleteTopic(clusterDO, TEST_CREATE_TOPIC);
Assert.assertEquals(result1.getCode(), ResultStatus.SUCCESS.getCode());
}
}

View File

@@ -0,0 +1,87 @@
package com.xiaojukeji.kafka.manager.service.utils;
import com.xiaojukeji.kafka.manager.common.entity.pojo.ClusterDO;
import com.xiaojukeji.kafka.manager.service.config.BaseTest;
import org.junit.Assert;
import org.testng.annotations.Test;
import java.util.Arrays;
import java.util.Date;
/**
* @author xuguang
* @Date 2022/1/6
*/
public class TopicReassignUtilsTest extends BaseTest {
private final static Long REAL_CLUSTER_ID_IN_MYSQL = 1L;
private final static String TEST_CREATE_TOPIC = "createTopicTest";
private final static String REAL_TOPIC_IN_ZK = "moduleTest";
private final static String INVALID_TOPIC = ".,&";
private final static Integer PARTITION_NUM = 1;
private final static Integer REPLICA_NUM = 1;
private final static Integer BROKER_ID = 1;
private final static Integer PARTITION_ID = 1;
private final static String REAL_PHYSICAL_CLUSTER_NAME = "LogiKM_moduleTest";
private final static String ZOOKEEPER_ADDRESS = "10.190.12.242:2181,10.190.25.160:2181,10.190.25.41:2181/wyc";
private final static String BOOTSTRAP_SERVERS = "10.190.12.242:9093,10.190.25.160:9093,10.190.25.41:9093";
private final static String SECURITY_PROTOCOL = "{ \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\\\";\" }";
public ClusterDO getClusterDO() {
ClusterDO clusterDO = new ClusterDO();
clusterDO.setId(REAL_CLUSTER_ID_IN_MYSQL);
clusterDO.setClusterName(REAL_PHYSICAL_CLUSTER_NAME);
clusterDO.setZookeeper(ZOOKEEPER_ADDRESS);
clusterDO.setBootstrapServers(BOOTSTRAP_SERVERS);
clusterDO.setSecurityProperties(SECURITY_PROTOCOL);
clusterDO.setStatus(1);
clusterDO.setGmtCreate(new Date());
clusterDO.setGmtModify(new Date());
return clusterDO;
}
@Test
public void generateReassignmentJsonTest() {
// null
generateReassignmentJson2NullPointerExceptionTest();
// not null
generateReassignmentJson2SuccessTest();
}
private void generateReassignmentJson2NullPointerExceptionTest() {
ClusterDO clusterDO = getClusterDO();
clusterDO.setZookeeper(null);
String result = TopicReassignUtils.generateReassignmentJson(
clusterDO,
REAL_TOPIC_IN_ZK,
Arrays.asList(PARTITION_ID),
Arrays.asList(BROKER_ID)
);
Assert.assertNull(result);
}
private void generateReassignmentJson2SuccessTest() {
ClusterDO clusterDO = getClusterDO();
String result = TopicReassignUtils.generateReassignmentJson(
clusterDO,
REAL_TOPIC_IN_ZK,
Arrays.asList(PARTITION_ID),
Arrays.asList(BROKER_ID)
);
Assert.assertNotNull(result);
}
}

View File

@@ -0,0 +1,127 @@
server:
port: 8080
tomcat:
accept-count: 1000
max-connections: 10000
max-threads: 800
min-spare-threads: 100
spring:
application:
name: kafkamanager
profiles:
active: dev
datasource:
kafka-manager:
jdbc-url: jdbc:mysql://localhost:3306/user_test?characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
main:
allow-bean-definition-overriding: true
servlet:
multipart:
max-file-size: 100MB
max-request-size: 100MB
logging:
config: classpath:logback-spring.xml
custom:
idc: cn
jmx:
max-conn: 10 # 2.3版本配置不在这个地方生效
store-metrics-task:
community:
broker-metrics-enabled: true
topic-metrics-enabled: true
didi:
app-topic-metrics-enabled: false
topic-request-time-metrics-enabled: false
topic-throttled-metrics: false
save-days: 7
# 任务相关的开关
task:
op:
sync-topic-enabled: false # 未落盘的Topic定期同步到DB中
order-auto-exec: # 工单自动化审批线程的开关
topic-enabled: false # Topic工单自动化审批开关, false:关闭自动化审批, true:开启
app-enabled: false # App工单自动化审批开关, false:关闭自动化审批, true:开启
account:
ldap:
enabled: false
url: ldap://127.0.0.1:389/
basedn: dc=tsign,dc=cn
factory: com.sun.jndi.ldap.LdapCtxFactory
filter: sAMAccountName
security:
authentication: simple
principal: cn=admin,dc=tsign,dc=cn
credentials: admin
auth-user-registration: true
auth-user-registration-role: normal
kcm:
enabled: false
s3:
endpoint: s3.didiyunapi.com
access-key: 1234567890
secret-key: 0987654321
bucket: logi-kafka
n9e:
base-url: http://127.0.0.1:8004
user-token: 12345678
timeout: 300
account: root
script-file: kcm_script.sh
monitor:
enabled: false
n9e:
nid: 2
user-token: 1234567890
mon:
base-url: http://127.0.0.1:8000 # 夜莺v4版本默认端口统一调整为了8000
sink:
base-url: http://127.0.0.1:8000 # 夜莺v4版本默认端口统一调整为了8000
rdb:
base-url: http://127.0.0.1:8000 # 夜莺v4版本默认端口统一调整为了8000
notify:
kafka:
cluster-id: 95
topic-name: didi-kafka-notify
order:
detail-url: http://127.0.0.1
test:
topic:
name1: moduleTest
name2: xgTest
name3: expandPartitionTopic
name4: topic_a
name5: NoOffsetChangeTopic
name6: _consumer_offsets
phyCluster:
id: 1
name: LogiKM_moduleTest
logicalCluster:
name: logical_cluster_1
broker:
id1: 1
id2: 2
id3: 3
app:
id: dkm_admin
ZK:
address: 127.0.0.1:2181
bootstrap-servers: 127.0.0.1:9093
gateway: 127.0.0.1
sasl-plaintext: SASL_PLAINTEXT://127.0.0.1:9093
admin: admin
consumer-group: moduleTestGroup
client-id: dkm_admin.moduleTest
region-name: region_1

View File

@@ -0,0 +1,63 @@
<assembly>
<id>assembly</id>
<formats>
<format>tar</format>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>src/main/resources/bin</directory>
<outputDirectory>bin</outputDirectory>
<includes>
<include>control.sh</include>
<include>start.bat</include>
</includes>
<fileMode>0755</fileMode>
</fileSet>
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>config</outputDirectory>
<includes>
<include>*.properties</include>
<include>*.xml</include>
<include>*.yml</include>
<include>env/dev/*</include>
<include>env/qa/*</include>
<include>env/uat/*</include>
<include>env/prod/*</include>
</includes>
</fileSet>
<fileSet>
<directory>target</directory>
<outputDirectory>lib</outputDirectory>
<includes>
<!--
<include>*release*.jar</include>
-->
<include>kafka-manager-web*.jar</include>
</includes>
<excludes>
<exclude>*sources.jar</exclude>
</excludes>
</fileSet>
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>logs</outputDirectory>
<fileMode>0755</fileMode>
<excludes>
<exclude>**/*</exclude>
</excludes>
</fileSet>
<!-- <fileSet>
<directory>${project.build.directory}/asciidoc</directory>
<outputDirectory>docs</outputDirectory>
<includes>
<include>md/*</include>
<include>html/*</include>
<include>pdf/*</include>
</includes>
<fileMode>0755</fileMode>
</fileSet>-->
</fileSets>
</assembly>

View File

@@ -0,0 +1,215 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="10 seconds">
<contextName>logback</contextName>
<property name="log.path" value="./logs" />
<!-- 彩色日志 -->
<!-- 彩色日志依赖的渲染类 -->
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
<!-- 彩色日志格式 -->
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<!--输出到控制台-->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>info</level>
</filter>
<encoder>
<Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<!--输出到文件-->
<!-- 时间滚动输出 level为 DEBUG 日志 -->
<appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/log_debug.log</file>
<!--日志文件输出格式-->
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset> <!-- 设置字符集 -->
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志归档 -->
<fileNamePattern>${log.path}/log_debug_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!--日志文件保留天数-->
<maxHistory>7</maxHistory>
</rollingPolicy>
<!-- 此日志文件只记录debug级别的 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>debug</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 时间滚动输出 level为 INFO 日志 -->
<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 正在记录的日志文件的路径及文件名 -->
<file>${log.path}/log_info.log</file>
<!--日志文件输出格式-->
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 每天日志归档路径以及格式 -->
<fileNamePattern>${log.path}/log_info_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!--日志文件保留天数-->
<maxHistory>7</maxHistory>
</rollingPolicy>
<!-- 此日志文件只记录info级别的 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>info</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 时间滚动输出 level为 WARN 日志 -->
<appender name="WARN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 正在记录的日志文件的路径及文件名 -->
<file>${log.path}/log_warn.log</file>
<!--日志文件输出格式-->
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset> <!-- 此处设置字符集 -->
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/log_warn_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!--日志文件保留天数-->
<maxHistory>7</maxHistory>
</rollingPolicy>
<!-- 此日志文件只记录warn级别的 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>warn</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 时间滚动输出 level为 ERROR 日志 -->
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 正在记录的日志文件的路径及文件名 -->
<file>${log.path}/log_error.log</file>
<!--日志文件输出格式-->
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset> <!-- 此处设置字符集 -->
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/log_error_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!--日志文件保留天数-->
<maxHistory>7</maxHistory>
</rollingPolicy>
<!-- 此日志文件只记录ERROR级别的 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- Metrics信息收集日志 -->
<appender name="COLLECTOR_METRICS_LOGGER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/metrics/collector_metrics.log</file>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/metrics/collector_metrics_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>3</maxHistory>
</rollingPolicy>
</appender>
<!-- Metrics信息收集日志 -->
<appender name="API_METRICS_LOGGER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/metrics/api_metrics.log</file>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/metrics/api_metrics_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>3</maxHistory>
</rollingPolicy>
</appender>
<!-- Metrics信息收集日志 -->
<appender name="SCHEDULED_TASK_LOGGER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/metrics/scheduled_tasks.log</file>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/metrics/scheduled_tasks_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>5</maxHistory>
</rollingPolicy>
</appender>
<logger name="COLLECTOR_METRICS_LOGGER" level="DEBUG" additivity="false">
<appender-ref ref="COLLECTOR_METRICS_LOGGER"/>
</logger>
<logger name="API_METRICS_LOGGER" level="DEBUG" additivity="false">
<appender-ref ref="API_METRICS_LOGGER"/>
</logger>
<logger name="SCHEDULED_TASK_LOGGER" level="DEBUG" additivity="false">
<appender-ref ref="SCHEDULED_TASK_LOGGER"/>
</logger>
<logger name="org.apache.ibatis" level="INFO" additivity="false" />
<logger name="org.mybatis.spring" level="INFO" additivity="false" />
<logger name="com.github.miemiedev.mybatis.paginator" level="INFO" additivity="false" />
<root level="info">
<appender-ref ref="CONSOLE" />
<appender-ref ref="DEBUG_FILE" />
<appender-ref ref="INFO_FILE" />
<appender-ref ref="WARN_FILE" />
<appender-ref ref="ERROR_FILE" />
<!--<appender-ref ref="METRICS_LOG" />-->
</root>
<!--生产环境:输出到文件-->
<!--<springProfile name="pro">-->
<!--<root level="info">-->
<!--<appender-ref ref="CONSOLE" />-->
<!--<appender-ref ref="DEBUG_FILE" />-->
<!--<appender-ref ref="INFO_FILE" />-->
<!--<appender-ref ref="ERROR_FILE" />-->
<!--<appender-ref ref="WARN_FILE" />-->
<!--</root>-->
<!--</springProfile>-->
</configuration>

View File

@@ -27,13 +27,13 @@
]]>
</delete>
<select id="getNeedReportTopics" parameterType="java.util.Map" resultMap="TopicReportMap">
<select id="getNeedReportTopic" parameterType="java.util.Map" resultMap="TopicReportMap">
<![CDATA[
SELECT *
FROM topic_report
WHERE (cluster_id = #{clusterId}
AND start_time <= #{now}
And end_time >= #{now})
AND end_time >= #{now})
]]>
</select>

View File

@@ -39,5 +39,22 @@
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
<!-- testng -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.10</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Class-Path:

View File

@@ -0,0 +1,99 @@
package com.xiaojukeji.kafka.manager.account;
import com.xiaojukeji.kafka.manager.account.common.EnterpriseStaff;
import com.xiaojukeji.kafka.manager.account.component.AbstractEnterpriseStaffService;
import com.xiaojukeji.kafka.manager.account.config.BaseTest;
import com.xiaojukeji.kafka.manager.common.entity.pojo.AccountDO;
import com.xiaojukeji.kafka.manager.dao.AccountDao;
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.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author wyc
* @date 2021/12/30
*/
public class AbstractEnterpriseStaffServiceTest extends BaseTest {
@Autowired
@InjectMocks
private AbstractEnterpriseStaffService abstractEnterpriseStaffService;
@Mock
private AccountDao accountDao;
@BeforeMethod
public void init() {
MockitoAnnotations.initMocks(this);
}
private EnterpriseStaff getEnterpriseStaff() {
EnterpriseStaff staff = new EnterpriseStaff("username", "username", "department");
return staff;
}
private AccountDO getAccountDO() {
AccountDO accountDO = new AccountDO();
accountDO.setUsername("username");
return accountDO;
}
@Test
public void getEnterpriseStaffByNameTest() {
// 返回null测试
getEnterpriseStaffByName2NullTest();
// 成功测试
getEnterpriseStaffByName2SuccessTest();
}
private void getEnterpriseStaffByName2NullTest() {
Mockito.when(accountDao.getByName(Mockito.anyString())).thenReturn(null);
Assert.assertNull(abstractEnterpriseStaffService.getEnterpriseStaffByName("username"));
}
private void getEnterpriseStaffByName2SuccessTest() {
AccountDO accountDO = getAccountDO();
EnterpriseStaff staff = getEnterpriseStaff();
Mockito.when(accountDao.getByName(Mockito.anyString())).thenReturn(accountDO);
EnterpriseStaff result = abstractEnterpriseStaffService.getEnterpriseStaffByName("username");
Assert.assertTrue(result.getUsername().equals(staff.getUsername()) && result.getChineseName().equals(staff.getChineseName()));
}
@Test
public void searchEnterpriseStaffByKeyWordTest() {
// 返回空集合测试
searchEnterpriseStaffByKeyWord2EmptyTest();
// 返回非空集合测试
searchEnterpriseStaffByKeyWord2AllTest();
}
private void searchEnterpriseStaffByKeyWord2EmptyTest() {
Mockito.when(accountDao.searchByNamePrefix(Mockito.anyString())).thenReturn(new ArrayList<>());
Assert.assertTrue(abstractEnterpriseStaffService.searchEnterpriseStaffByKeyWord("username").isEmpty());
}
private void searchEnterpriseStaffByKeyWord2AllTest() {
AccountDO accountDO = getAccountDO();
Mockito.when(accountDao.searchByNamePrefix(Mockito.anyString())).thenReturn(Arrays.asList(accountDO));
EnterpriseStaff staff = getEnterpriseStaff();
List<EnterpriseStaff> result = abstractEnterpriseStaffService.searchEnterpriseStaffByKeyWord("username");
Assert.assertTrue(!result.isEmpty() && result.stream().allMatch(enterpriseStaff -> enterpriseStaff.getChineseName().equals(staff.getChineseName()) &&
enterpriseStaff.getUsername().equals(staff.getUsername())));
}
}

View File

@@ -0,0 +1,245 @@
package com.xiaojukeji.kafka.manager.account;
import com.xiaojukeji.kafka.manager.account.component.AbstractSingleSignOn;
import com.xiaojukeji.kafka.manager.account.component.ldap.LdapAuthentication;
import com.xiaojukeji.kafka.manager.account.config.BaseTest;
import com.xiaojukeji.kafka.manager.common.bizenum.AccountRoleEnum;
import com.xiaojukeji.kafka.manager.common.constant.LoginConstant;
import com.xiaojukeji.kafka.manager.common.entity.Result;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.dto.normal.LoginDTO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.AccountDO;
import com.xiaojukeji.kafka.manager.common.utils.EncryptUtil;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.internal.util.reflection.FieldSetter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.util.HashMap;
/**
* @author wyc
* @date 2021/12/30
*/
public class AbstractSingleSignOnTest extends BaseTest {
@Autowired
@InjectMocks
private AbstractSingleSignOn abstractSingleSignOn;
@Mock
private AccountService accountService;
@Mock
private LdapAuthentication ldapAuthentication;
@BeforeMethod
public void init() {
MockitoAnnotations.initMocks(this);
}
private HttpServletRequest getHttpServletRequest() {
HttpServletRequest request = new MockHttpServletRequest();
return request;
}
private HttpServletResponse getHttpServletResponse() {
HttpServletResponse response = new MockHttpServletResponse();
return response;
}
private LoginDTO getLoginDTO() {
LoginDTO dto = new LoginDTO();
dto.setUsername("username");
dto.setPassword("password");
return dto;
}
private AccountDO getAccountDO() {
AccountDO accountDO = new AccountDO();
accountDO.setUsername("username");
accountDO.setPassword("password");
return accountDO;
}
@Test
public void loginAndGetLdapTest() throws NoSuchFieldException {
// username为null测试
loginAndGetLdap2ParamNullTest();
// LDAP未激活测试从最后的return返回
loginAndGetLdap2LdapDisabledTest();
// LDAP激活返回false测试
loginAndGetLdap2LdapEnabledReturnFalseTest();
// LDAP激活返回true测试
loginAndGetLdap2LdapEnabledReturnTrueTest();
loginAndGetLdap2FailureTest();
// name illegal 测试
loginAndGetLdap2NameIllegalTest();
// password illegal 测试
loginAndGetLdap2PasswordIllegalTest();
}
private void resetAbstractSingleSignOn() throws NoSuchFieldException {
// 通过反射将abstractSingleSignOn内属性的值置于初始状态,因为每个private测试中会通过反射改变abstractSingleSignOn成员变量的值
Field accountLdapEnabled = abstractSingleSignOn.getClass().getDeclaredField("accountLdapEnabled");
FieldSetter.setField(abstractSingleSignOn, accountLdapEnabled, null);
Field authUserRegistrationRole = abstractSingleSignOn.getClass().getDeclaredField("authUserRegistrationRole");
FieldSetter.setField(abstractSingleSignOn, authUserRegistrationRole, null);
Field authUserRegistration = abstractSingleSignOn.getClass().getDeclaredField("authUserRegistration");
FieldSetter.setField(abstractSingleSignOn, authUserRegistration, false);
}
private void loginAndGetLdap2ParamNullTest() throws NoSuchFieldException {
resetAbstractSingleSignOn();
HttpServletRequest request = getHttpServletRequest();
HttpServletResponse response = getHttpServletResponse();
LoginDTO loginDTO = getLoginDTO();
loginDTO.setUsername(null);
Assert.assertEquals(abstractSingleSignOn.loginAndGetLdap(request, response, loginDTO).toString(), Result.buildFailure("Missing parameters").toString());
}
private void loginAndGetLdap2LdapDisabledTest() throws NoSuchFieldException {
resetAbstractSingleSignOn();
HttpServletRequest request = getHttpServletRequest();
HttpServletResponse response = getHttpServletResponse();
LoginDTO loginDTO = getLoginDTO();
String pass = EncryptUtil.md5(loginDTO.getPassword());
AccountDO accountDO1 = getAccountDO();
accountDO1.setPassword(pass);
Result<AccountDO> result = Result.buildSuc(accountDO1);
Mockito.when(accountService.getAccountDO(Mockito.any())).thenReturn(result);
Assert.assertEquals(abstractSingleSignOn.loginAndGetLdap(request, response, loginDTO).toString(), Result.buildSuc(result.getData().getUsername()).toString());
}
private void loginAndGetLdap2LdapEnabledReturnFalseTest() throws NoSuchFieldException {
resetAbstractSingleSignOn();
HttpServletRequest request = getHttpServletRequest();
HttpServletResponse response = getHttpServletResponse();
LoginDTO dto = getLoginDTO();
// 通过反射将abstractSingleSignOn对象中accountLdapEnabled属性设置为true
Field accountLdapEnabled = abstractSingleSignOn.getClass().getDeclaredField("accountLdapEnabled");
FieldSetter.setField(abstractSingleSignOn, accountLdapEnabled, true);
Mockito.when(ldapAuthentication.authenticate(Mockito.anyString(), Mockito.anyString())).thenReturn(null);
Assert.assertEquals(abstractSingleSignOn.loginAndGetLdap(request, response, dto).toString(), Result.buildFrom(ResultStatus.LDAP_AUTHENTICATION_FAILED).toString());
}
private void loginAndGetLdap2LdapEnabledReturnTrueTest() throws NoSuchFieldException {
resetAbstractSingleSignOn();
HttpServletRequest request = getHttpServletRequest();
HttpServletResponse response = getHttpServletResponse();
LoginDTO dto = getLoginDTO();
Mockito.when(accountService.getAccountDO(Mockito.any())).thenReturn(null);
// 通过反射将abstractSingleSignOn对象中accountLdapEnabled属性设置为true
Field accountLdapEnabled = abstractSingleSignOn.getClass().getDeclaredField("accountLdapEnabled");
FieldSetter.setField(abstractSingleSignOn, accountLdapEnabled, true);
Mockito.when(ldapAuthentication.authenticate(Mockito.anyString(), Mockito.anyString())).thenReturn(new HashMap<>());
// 通过反射初始化成员变量,防止出现空指针异常
Field authUserRegistrationRole = abstractSingleSignOn.getClass().getDeclaredField("authUserRegistrationRole");
FieldSetter.setField(abstractSingleSignOn, authUserRegistrationRole, AccountRoleEnum.NORMAL.getMessage());
Field authUserRegistration = abstractSingleSignOn.getClass().getDeclaredField("authUserRegistration");
FieldSetter.setField(abstractSingleSignOn, authUserRegistration, true);
Assert.assertEquals(abstractSingleSignOn.loginAndGetLdap(request, response, dto).toString(), Result.buildSuc(dto.getUsername()).toString());
}
private void loginAndGetLdap2FailureTest() throws NoSuchFieldException {
resetAbstractSingleSignOn();
HttpServletRequest request = getHttpServletRequest();
HttpServletResponse response = getHttpServletResponse();
LoginDTO dto = getLoginDTO();
Result result = Result.buildFailure("fail");
Mockito.when(accountService.getAccountDO(Mockito.any())).thenReturn(result);
Assert.assertEquals(abstractSingleSignOn.loginAndGetLdap(request, response, dto).toString(), result.toString());
}
private void loginAndGetLdap2NameIllegalTest() throws NoSuchFieldException {
resetAbstractSingleSignOn();
HttpServletRequest request = getHttpServletRequest();
HttpServletResponse response = getHttpServletResponse();
LoginDTO dto = getLoginDTO();
Result result = Result.buildSuc();
Mockito.when(accountService.getAccountDO(Mockito.any())).thenReturn(result);
Assert.assertEquals(abstractSingleSignOn.loginAndGetLdap(request, response, dto).toString(), Result.buildFailure("username illegal").toString());
}
private void loginAndGetLdap2PasswordIllegalTest() throws NoSuchFieldException {
resetAbstractSingleSignOn();
HttpServletRequest request = getHttpServletRequest();
HttpServletResponse response = getHttpServletResponse();
LoginDTO dto = getLoginDTO();
AccountDO accountDO = getAccountDO();
Result<AccountDO> result = Result.buildSuc(accountDO);
Mockito.when(accountService.getAccountDO(Mockito.any())).thenReturn(result);
Assert.assertEquals(abstractSingleSignOn.loginAndGetLdap(request, response, dto).toString(), Result.buildFailure("password illegal").toString());
}
@Test
public void logoutTest() {
HttpServletRequest request = getHttpServletRequest();
HttpServletResponse response = getHttpServletResponse();
abstractSingleSignOn.logout(request, response, true);
Assert.assertEquals(response.getStatus(), 401);
Assert.assertEquals(response.getHeader("location"), "");
}
@Test
public void checkLoginAndGetLdapTest() {
// 返回null测试
checkLoginAndGetLdap2NullTest();
// 成功测试
checkLoginAndGetLdap2SuccessTest();
}
private void checkLoginAndGetLdap2NullTest() {
HttpServletRequest request = getHttpServletRequest();
Assert.assertNull(abstractSingleSignOn.checkLoginAndGetLdap(request));
}
private void checkLoginAndGetLdap2SuccessTest() {
HttpServletRequest request = getHttpServletRequest();
request.getSession().setAttribute(LoginConstant.SESSION_USERNAME_KEY, "username");
Assert.assertEquals(abstractSingleSignOn.checkLoginAndGetLdap(request), "username");
}
@Test
public void setRedirectToLoginPageTest() {
HttpServletResponse response = getHttpServletResponse();
response.setStatus(401);
response.setHeader("location", "");
Assert.assertEquals(response.getStatus(), 401);
Assert.assertEquals(response.getHeader("location"), "");
}
}

View File

@@ -0,0 +1,291 @@
package com.xiaojukeji.kafka.manager.account;
import com.xiaojukeji.kafka.manager.account.common.EnterpriseStaff;
import com.xiaojukeji.kafka.manager.account.component.AbstractEnterpriseStaffService;
import com.xiaojukeji.kafka.manager.account.config.BaseTest;
import com.xiaojukeji.kafka.manager.common.bizenum.AccountRoleEnum;
import com.xiaojukeji.kafka.manager.common.constant.Constant;
import com.xiaojukeji.kafka.manager.common.entity.Result;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.ao.account.Account;
import com.xiaojukeji.kafka.manager.common.entity.pojo.AccountDO;
import com.xiaojukeji.kafka.manager.dao.AccountDao;
import com.xiaojukeji.kafka.manager.service.service.ConfigService;
import com.xiaojukeji.kafka.manager.service.service.OperateRecordService;
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.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author wyc
* @Date 2021/12/29
*/
public class AccountServiceTest extends BaseTest {
/*
此测试不能一起运行因为一些test中会执行一次flush()执行完毕后缓存就不为null
后面的测试中本来应该再次刷新缓存但由于缓存不为null就不会再执行flush
*/
@Autowired
@InjectMocks
private AccountService accountService;
@Mock
private AccountDao accountDao;
@Mock
private OperateRecordService operateRecordService;
@Mock
private AbstractEnterpriseStaffService enterpriseStaffService;
@Mock
private ConfigService configService;
@BeforeMethod
public void init() {
MockitoAnnotations.initMocks(this);
}
private AccountDO getAccountDO() {
AccountDO accountDO = new AccountDO();
accountDO.setUsername("test_username");
accountDO.setPassword("test_password");
accountDO.setRole(0);
return accountDO;
}
private EnterpriseStaff getEnterpriseStaff() {
EnterpriseStaff staff = new EnterpriseStaff("username", "ChineseName", "department");
return staff;
}
private Account getAccount() {
Account account = new Account();
return account;
}
@Test
public void createAccountTest() {
AccountDO accountDO = getAccountDO();
// 创建成功测试
createAccount2SuccessTest(accountDO);
// 主键重复测试
createAccount2DuplicateKeyExceptionTest(accountDO);
}
private void createAccount2SuccessTest(AccountDO accountDO) {
Mockito.when(accountDao.addNewAccount(Mockito.any())).thenReturn(1);
Assert.assertEquals(accountService.createAccount(accountDO), ResultStatus.SUCCESS);
}
private void createAccount2DuplicateKeyExceptionTest(AccountDO accountDO) {
Mockito.when(accountDao.addNewAccount(Mockito.any())).thenThrow(DuplicateKeyException.class);
Assert.assertEquals(accountService.createAccount(accountDO), ResultStatus.RESOURCE_ALREADY_EXISTED);
}
private void createAccount2MySQLErrorTest(AccountDO accountDO) {
Mockito.when(accountDao.addNewAccount(Mockito.any())).thenReturn(-1);
Assert.assertEquals(accountService.createAccount(accountDO), ResultStatus.MYSQL_ERROR);
}
@Test
public void deleteByNameTest() {
// 删除成功测试
deleteName2SuccessTest();
// MySQL_ERROR错误测试
deleteName2MySQLErrorTest();
}
private void deleteName2SuccessTest() {
Mockito.when(accountDao.deleteByName(Mockito.anyString())).thenReturn(1);
Assert.assertEquals(accountService.deleteByName("username", "admin"), ResultStatus.SUCCESS);
}
private void deleteName2MySQLErrorTest() {
Mockito.when(accountDao.deleteByName(Mockito.anyString())).thenReturn(-1);
Assert.assertEquals(accountService.deleteByName("username", "admin"), ResultStatus.MYSQL_ERROR);
}
@Test
public void updateAccountTest() {
// 账号不存在测试
updateAccount2AccountNotExistTest();
// 更新成功测试
updateAccount2SuccessTest();
// MySQL_ERROR测试
updateAccount2MySQLErrorTest();
}
private void updateAccount2AccountNotExistTest() {
AccountDO accountDO = getAccountDO();
Mockito.when(accountDao.getByName(Mockito.anyString())).thenReturn(null);
Assert.assertEquals(accountService.updateAccount(accountDO), ResultStatus.ACCOUNT_NOT_EXIST);
}
private void updateAccount2SuccessTest() {
AccountDO accountDO = getAccountDO();
Mockito.when(accountDao.getByName(Mockito.anyString())).thenReturn(accountDO);
Mockito.when(accountDao.updateByName(Mockito.any())).thenReturn(1);
Assert.assertEquals(accountService.updateAccount(accountDO), ResultStatus.SUCCESS);
}
private void updateAccount2MySQLErrorTest() {
AccountDO accountDO = getAccountDO();
Mockito.when(accountDao.getByName(Mockito.anyString())).thenReturn(accountDO);
Mockito.when(accountDao.updateByName(Mockito.any())).thenReturn(-1);
Assert.assertEquals(accountService.updateAccount(accountDO), ResultStatus.MYSQL_ERROR);
}
@Test
public void getAccountDOTest() {
AccountDO accountDO = getAccountDO();
Result<AccountDO> expectedResult = Result.buildSuc(accountDO);
Mockito.when(accountDao.getByName(Mockito.anyString())).thenReturn(accountDO);
Assert.assertEquals(accountService.getAccountDO(accountDO.getUsername()).toString(), expectedResult.toString());
}
@Test
public void listTest() {
AccountDO accountDO = getAccountDO();
List<AccountDO> list = new ArrayList<>();
list.add(accountDO);
Mockito.when(accountDao.list()).thenReturn(list);
List<AccountDO> actualResult = accountService.list();
Assert.assertTrue(!actualResult.isEmpty() && actualResult.stream().allMatch(account -> account.getUsername().equals(accountDO.getUsername()) &&
account.getPassword().equals(accountDO.getPassword())));
}
@Test
public void searchAccountByPrefixTest() {
EnterpriseStaff staff = getEnterpriseStaff();
List<EnterpriseStaff> expectedResult = new ArrayList<>();
expectedResult.add(staff);
Mockito.when(enterpriseStaffService.searchEnterpriseStaffByKeyWord(Mockito.anyString())).thenReturn(expectedResult);
List<EnterpriseStaff> actualResult = accountService.searchAccountByPrefix("prefix");
Assert.assertTrue(!actualResult.isEmpty() && actualResult.stream().allMatch(enterpriseStaff -> enterpriseStaff.getUsername().equals(staff.getUsername())));
}
// 因为flush只会执行一次因此需要分开测试
@Test(description = "普通角色测试")
public void getAccountRoleFromCache2NormalTest() {
Assert.assertEquals(accountService.getAccountRoleFromCache("username"), AccountRoleEnum.NORMAL);
}
@Test(description = "op角色测试")
public void getAccountRoleFromCache2OpTest() {
AccountDO accountDO = getAccountDO();
accountDO.setUsername("admin");
accountDO.setRole(2);
Mockito.when(accountDao.list()).thenReturn(Arrays.asList(accountDO));
Assert.assertEquals(accountService.getAccountRoleFromCache("admin"), AccountRoleEnum.OP);
}
@Test(description = "自动审批测试")
public void getAccountFromCache2AutoHandleTest() {
Account account = getAccount();
account.setUsername(Constant.AUTO_HANDLE_USER_NAME);
account.setChineseName(Constant.AUTO_HANDLE_CHINESE_NAME);
account.setAccountRoleEnum(AccountRoleEnum.OP);
Assert.assertEquals(accountService.getAccountFromCache(Constant.AUTO_HANDLE_USER_NAME).toString(), account.toString());
}
@Test(description = "staff为null测试")
public void getAccountFromCache2EnterpriseStaffIsNullTest() {
Account account = getAccount();
account.setUsername("username1");
account.setChineseName("username1");
account.setAccountRoleEnum(AccountRoleEnum.NORMAL);
Mockito.when(enterpriseStaffService.getEnterpriseStaffByName(Mockito.anyString())).thenReturn(null);
Assert.assertEquals(accountService.getAccountFromCache("username1").toString(), account.toString());
}
@Test(description = "staff不为null测试")
public void getAccountFromCache2EnterpriseStaffIsNotNullTest() {
Account account = getAccount();
account.setUsername("username");
account.setChineseName("ChineseName");
account.setAccountRoleEnum(AccountRoleEnum.NORMAL);
account.setDepartment("department");
EnterpriseStaff enterpriseStaff = getEnterpriseStaff();
Mockito.when(enterpriseStaffService.getEnterpriseStaffByName(Mockito.any())).thenReturn(enterpriseStaff);
Assert.assertEquals(accountService.getAccountFromCache("username").toString(), account.toString());
}
@Test(description = "op角色测试")
public void isAdminOrderHandler2OpTest() {
AccountDO accountDO = getAccountDO();
accountDO.setUsername("admin");
accountDO.setRole(2);
Mockito.when(accountDao.list()).thenReturn(Arrays.asList(accountDO));
Assert.assertTrue(accountService.isAdminOrderHandler("admin"));
}
@Test(description = "ADMIN_ORDER_HANDLER_CACHE包含用户名测试")
public void isAdminOrderHandler2CacheContainsTest() {
Mockito.when(configService.getArrayByKey(Mockito.anyString(), Mockito.any())).thenReturn(Arrays.asList("username"));
Mockito.when(accountDao.list()).thenReturn(new ArrayList<>());
Assert.assertTrue(accountService.isAdminOrderHandler("username"));
}
@Test(description = "普通角色测试")
public void isAdminOrderHandler2NormalTest() {
Mockito.when(accountDao.list()).thenReturn(new ArrayList<>());
Assert.assertFalse(accountService.isAdminOrderHandler("username"));
}
@Test(description = "op角色测试")
public void isOpOrRd2OpTest() {
AccountDO accountDO = getAccountDO();
accountDO.setUsername("admin");
accountDO.setRole(2);
Mockito.when(accountDao.list()).thenReturn(Arrays.asList(accountDO));
Assert.assertTrue(accountService.isOpOrRd("admin"));
}
@Test
public void isOpOrRdTest2RdTest() {
AccountDO accountDO = getAccountDO();
accountDO.setUsername("admin");
accountDO.setRole(1);
Mockito.when(accountDao.list()).thenReturn(Arrays.asList(accountDO));
Assert.assertTrue(accountService.isOpOrRd("admin"));
}
@Test
public void getAdminOrderHandlerFromCacheTest() {
AccountDO accountDO = getAccountDO();
accountDO.setUsername("username");
accountDO.setRole(2);
Mockito.when(accountDao.list()).thenReturn(Arrays.asList(accountDO));
List<Account> actualList = accountService.getAdminOrderHandlerFromCache();
Assert.assertTrue(!actualList.isEmpty() && actualList.stream().allMatch(account -> account.getUsername().equals(accountDO.getUsername())));
}
}

View File

@@ -0,0 +1,198 @@
package com.xiaojukeji.kafka.manager.account;
import com.xiaojukeji.kafka.manager.account.component.AbstractSingleSignOn;
import com.xiaojukeji.kafka.manager.account.component.login.trick.TrickLoginService;
import com.xiaojukeji.kafka.manager.account.config.BaseTest;
import com.xiaojukeji.kafka.manager.common.bizenum.AccountRoleEnum;
import com.xiaojukeji.kafka.manager.common.constant.ApiPrefix;
import com.xiaojukeji.kafka.manager.common.constant.LoginConstant;
import com.xiaojukeji.kafka.manager.common.entity.Result;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.ao.account.Account;
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.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import javax.servlet.http.*;
/**
* @author wyc
* @Date 2021/12/29
*/
public class LoginServiceTest extends BaseTest {
@Autowired
@InjectMocks
private LoginService loginService;
@Mock
private AbstractSingleSignOn singleSignOn;
@Mock
private AccountService accountService;
@Mock
private TrickLoginService trickLoginService;
@BeforeMethod
public void init() {
MockitoAnnotations.initMocks(this);
}
private Account getAccount() {
Account account = new Account();
account.setUsername("username");
return account;
}
private HttpServletRequest getHttpServletRequest() {
HttpServletRequest request = new MockHttpServletRequest();
return request;
}
private HttpServletResponse getHttpServletResponse() {
HttpServletResponse response = new MockHttpServletResponse();
return response;
}
@Test
public void loginTest() {
// 失败测试
login2FailureTest();
// 成功测试
HttpServletRequest request = getHttpServletRequest();
HttpServletResponse response = getHttpServletResponse();
login2SuccessTest(request, response);
}
private void login2SuccessTest(HttpServletRequest request, HttpServletResponse response) {
Account account = getAccount();
Result<Account> expectResult = Result.buildSuc(account);
Result<String> midResult = Result.buildSuc();
Mockito.when(singleSignOn.loginAndGetLdap(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(midResult);
Mockito.when(accountService.getAccountFromCache(Mockito.any())).thenReturn(account);
Assert.assertEquals(loginService.login(request, response, null).toString(), expectResult.toString());
}
private void login2FailureTest() {
Result<String> result = new Result<>();
result.setCode(ResultStatus.FAIL.getCode());
result.setMessage(ResultStatus.FAIL.getMessage());
Mockito.when(singleSignOn.loginAndGetLdap(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(result);
Result<Account> actualResult = loginService.login(null, null, null);
Assert.assertEquals(actualResult.toString(), result.toString());
}
@Test
public void checkLoginTest() {
// classRequestMappingValue为null测试
checkLogin2ClassValueIsNullTest();
// 白名单接口测试
checkLogin2SSOTest();
// trick登陆返回用户名为空
checkLogin2TrickFalseTest();
// 权限检查normal接口测试
checkLogin2NormalTest();
// 权限检查RD接口, 成功测试
checkLogin2RDSuccessTest();
// 权限检查RD接口, 失败测试
checkLogin2RDFailureTest();
// 权限检查OP接口, 成功测试
checkLogin2OPSuccessTest();
// 权限检查OP接口失败测试
checkLogin2OPFailureTest();
}
private void checkLogin2ClassValueIsNullTest() {
HttpServletResponse response = getHttpServletResponse();
HttpServletRequest request = getHttpServletRequest();
Mockito.doNothing().when(singleSignOn).setRedirectToLoginPage(Mockito.any());
Assert.assertFalse(loginService.checkLogin(request, response, null));
Assert.assertEquals(response.getStatus(), HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
private void checkLogin2SSOTest() {
HttpServletResponse response = getHttpServletResponse();
HttpServletRequest request = getHttpServletRequest();
Assert.assertTrue(loginService.checkLogin(request, response, ApiPrefix.API_V1_SSO_PREFIX));
}
private void checkLogin2TrickFalseTest() {
HttpServletResponse response = getHttpServletResponse();
HttpServletRequest request = getHttpServletRequest();
Mockito.when(trickLoginService.isTrickLoginOn(Mockito.any())).thenReturn(true);
Mockito.when(trickLoginService.checkTrickLogin(Mockito.any())).thenReturn("");
Mockito.doNothing().when(singleSignOn).setRedirectToLoginPage(response);
Assert.assertFalse(loginService.checkLogin(request, response, "string"));
Assert.assertEquals(response.getStatus(), HttpServletResponse.SC_UNAUTHORIZED);
}
private void checkLogin2NormalTest() {
HttpServletResponse response = getHttpServletResponse();
HttpServletRequest request = getHttpServletRequest();
Mockito.when(trickLoginService.isTrickLoginOn(Mockito.any())).thenReturn(true);
Mockito.when(trickLoginService.checkTrickLogin(Mockito.any())).thenReturn("username");
Assert.assertTrue(loginService.checkLogin(request, response, ApiPrefix.API_V1_NORMAL_PREFIX));
Assert.assertEquals(request.getSession().getAttribute(LoginConstant.SESSION_USERNAME_KEY), "username");
}
private void checkLogin2RDSuccessTest() {
HttpServletResponse response = getHttpServletResponse();
HttpServletRequest request = getHttpServletRequest();
Mockito.when(trickLoginService.isTrickLoginOn(Mockito.any())).thenReturn(true);
Mockito.when(trickLoginService.checkTrickLogin(Mockito.any())).thenReturn("username");
Mockito.when(accountService.getAccountRoleFromCache(Mockito.any())).thenReturn(AccountRoleEnum.OP);
Assert.assertTrue(loginService.checkLogin(request, response, ApiPrefix.API_V1_RD_PREFIX));
Assert.assertEquals(request.getSession().getAttribute(LoginConstant.SESSION_USERNAME_KEY), "username");
}
private void checkLogin2RDFailureTest() {
HttpServletResponse response = getHttpServletResponse();
HttpServletRequest request = getHttpServletRequest();
Mockito.when(trickLoginService.isTrickLoginOn(Mockito.any())).thenReturn(true);
Mockito.when(trickLoginService.checkTrickLogin(Mockito.any())).thenReturn("username");
Mockito.when(accountService.getAccountRoleFromCache(Mockito.any())).thenReturn(AccountRoleEnum.NORMAL);
Assert.assertFalse(loginService.checkLogin(request, response, ApiPrefix.API_V1_RD_PREFIX));
Assert.assertEquals(response.getStatus(), HttpServletResponse.SC_FORBIDDEN);
}
private void checkLogin2OPSuccessTest() {
HttpServletResponse response = getHttpServletResponse();
HttpServletRequest request = getHttpServletRequest();
Mockito.when(trickLoginService.isTrickLoginOn(Mockito.any())).thenReturn(true);
Mockito.when(trickLoginService.checkTrickLogin(Mockito.any())).thenReturn("username");
Mockito.when(accountService.getAccountRoleFromCache(Mockito.any())).thenReturn(AccountRoleEnum.OP);
Assert.assertTrue(loginService.checkLogin(request, response, ApiPrefix.API_V1_OP_PREFIX));
Assert.assertEquals(request.getSession().getAttribute(LoginConstant.SESSION_USERNAME_KEY), "username");
}
private void checkLogin2OPFailureTest() {
HttpServletResponse response = getHttpServletResponse();
HttpServletRequest request = getHttpServletRequest();
Mockito.when(trickLoginService.isTrickLoginOn(Mockito.any())).thenReturn(true);
Mockito.when(trickLoginService.checkTrickLogin(Mockito.any())).thenReturn("username");
Mockito.when(accountService.getAccountRoleFromCache(Mockito.any())).thenReturn(AccountRoleEnum.NORMAL);
Assert.assertFalse(loginService.checkLogin(request, response, ApiPrefix.API_V1_OP_PREFIX));
Assert.assertEquals(response.getStatus(), HttpServletResponse.SC_FORBIDDEN);
}
}

View File

@@ -0,0 +1,11 @@
package com.xiaojukeji.kafka.manager.account.config;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
@SpringBootTest(classes = CoreSpringBootStartUp.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(classes = CoreSpringBootStartUp.class)
public class BaseTest extends AbstractTransactionalTestNGSpringContextTests {
}

View File

@@ -0,0 +1,21 @@
package com.xiaojukeji.kafka.manager.account.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableAsync
@EnableScheduling
@ServletComponentScan
@EnableAutoConfiguration
@SpringBootApplication(scanBasePackages = {"com.xiaojukeji.kafka.manager"})
public class CoreSpringBootStartUp {
public static void main(String[] args) {
SpringApplication sa = new SpringApplication(CoreSpringBootStartUp.class);
sa.run(args);
}
}

View File

@@ -0,0 +1,51 @@
package com.xiaojukeji.kafka.manager.account.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* @author zengqiao
* @date 20/3/17
*/
@Configuration
public class DataSourceConfig {
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource.kafka-manager")
@Primary
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "sqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
bean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:mybatis-config.xml"));
return bean.getObject();
}
@Bean(name = "transactionManager")
@Primary
public DataSourceTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "sqlSession")
@Primary
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}

View File

@@ -0,0 +1,98 @@
server:
port: 8080
tomcat:
accept-count: 1000
max-connections: 10000
max-threads: 800
min-spare-threads: 100
spring:
application:
name: kafkamanager
profiles:
active: dev
datasource:
kafka-manager:
jdbc-url: jdbc:mysql://localhost:3306/user_test?characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
main:
allow-bean-definition-overriding: true
servlet:
multipart:
max-file-size: 100MB
max-request-size: 100MB
logging:
config: classpath:logback-spring.xml
custom:
idc: cn
jmx:
max-conn: 10 # 2.3版本配置不在这个地方生效
store-metrics-task:
community:
broker-metrics-enabled: true
topic-metrics-enabled: true
didi:
app-topic-metrics-enabled: false
topic-request-time-metrics-enabled: false
topic-throttled-metrics: false
save-days: 7
# 任务相关的开关
task:
op:
sync-topic-enabled: false # 未落盘的Topic定期同步到DB中
order-auto-exec: # 工单自动化审批线程的开关
topic-enabled: false # Topic工单自动化审批开关, false:关闭自动化审批, true:开启
app-enabled: false # App工单自动化审批开关, false:关闭自动化审批, true:开启
account:
ldap:
enabled: false
url: ldap://127.0.0.1:389/
basedn: dc=tsign,dc=cn
factory: com.sun.jndi.ldap.LdapCtxFactory
filter: sAMAccountName
security:
authentication: simple
principal: cn=admin,dc=tsign,dc=cn
credentials: admin
auth-user-registration: true
auth-user-registration-role: normal
kcm:
enabled: false
s3:
endpoint: s3.didiyunapi.com
access-key: 1234567890
secret-key: 0987654321
bucket: logi-kafka
n9e:
base-url: http://127.0.0.1:8004
user-token: 12345678
timeout: 300
account: root
script-file: kcm_script.sh
monitor:
enabled: false
n9e:
nid: 2
user-token: 1234567890
mon:
base-url: http://127.0.0.1:8000 # 夜莺v4版本默认端口统一调整为了8000
sink:
base-url: http://127.0.0.1:8000 # 夜莺v4版本默认端口统一调整为了8000
rdb:
base-url: http://127.0.0.1:8000 # 夜莺v4版本默认端口统一调整为了8000
notify:
kafka:
cluster-id: 95
topic-name: didi-kafka-notify
order:
detail-url: http://127.0.0.1

View File

@@ -0,0 +1,63 @@
<assembly>
<id>assembly</id>
<formats>
<format>tar</format>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>src/main/resources/bin</directory>
<outputDirectory>bin</outputDirectory>
<includes>
<include>control.sh</include>
<include>start.bat</include>
</includes>
<fileMode>0755</fileMode>
</fileSet>
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>config</outputDirectory>
<includes>
<include>*.properties</include>
<include>*.xml</include>
<include>*.yml</include>
<include>env/dev/*</include>
<include>env/qa/*</include>
<include>env/uat/*</include>
<include>env/prod/*</include>
</includes>
</fileSet>
<fileSet>
<directory>target</directory>
<outputDirectory>lib</outputDirectory>
<includes>
<!--
<include>*release*.jar</include>
-->
<include>kafka-manager-web*.jar</include>
</includes>
<excludes>
<exclude>*sources.jar</exclude>
</excludes>
</fileSet>
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>logs</outputDirectory>
<fileMode>0755</fileMode>
<excludes>
<exclude>**/*</exclude>
</excludes>
</fileSet>
<!-- <fileSet>
<directory>${project.build.directory}/asciidoc</directory>
<outputDirectory>docs</outputDirectory>
<includes>
<include>md/*</include>
<include>html/*</include>
<include>pdf/*</include>
</includes>
<fileMode>0755</fileMode>
</fileSet>-->
</fileSets>
</assembly>

View File

@@ -0,0 +1,215 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="10 seconds">
<contextName>logback</contextName>
<property name="log.path" value="./logs" />
<!-- 彩色日志 -->
<!-- 彩色日志依赖的渲染类 -->
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
<!-- 彩色日志格式 -->
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<!--输出到控制台-->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>info</level>
</filter>
<encoder>
<Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<!--输出到文件-->
<!-- 时间滚动输出 level为 DEBUG 日志 -->
<appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/log_debug.log</file>
<!--日志文件输出格式-->
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset> <!-- 设置字符集 -->
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志归档 -->
<fileNamePattern>${log.path}/log_debug_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!--日志文件保留天数-->
<maxHistory>7</maxHistory>
</rollingPolicy>
<!-- 此日志文件只记录debug级别的 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>debug</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 时间滚动输出 level为 INFO 日志 -->
<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 正在记录的日志文件的路径及文件名 -->
<file>${log.path}/log_info.log</file>
<!--日志文件输出格式-->
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 每天日志归档路径以及格式 -->
<fileNamePattern>${log.path}/log_info_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!--日志文件保留天数-->
<maxHistory>7</maxHistory>
</rollingPolicy>
<!-- 此日志文件只记录info级别的 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>info</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 时间滚动输出 level为 WARN 日志 -->
<appender name="WARN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 正在记录的日志文件的路径及文件名 -->
<file>${log.path}/log_warn.log</file>
<!--日志文件输出格式-->
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset> <!-- 此处设置字符集 -->
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/log_warn_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!--日志文件保留天数-->
<maxHistory>7</maxHistory>
</rollingPolicy>
<!-- 此日志文件只记录warn级别的 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>warn</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 时间滚动输出 level为 ERROR 日志 -->
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 正在记录的日志文件的路径及文件名 -->
<file>${log.path}/log_error.log</file>
<!--日志文件输出格式-->
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset> <!-- 此处设置字符集 -->
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/log_error_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!--日志文件保留天数-->
<maxHistory>7</maxHistory>
</rollingPolicy>
<!-- 此日志文件只记录ERROR级别的 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- Metrics信息收集日志 -->
<appender name="COLLECTOR_METRICS_LOGGER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/metrics/collector_metrics.log</file>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/metrics/collector_metrics_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>3</maxHistory>
</rollingPolicy>
</appender>
<!-- Metrics信息收集日志 -->
<appender name="API_METRICS_LOGGER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/metrics/api_metrics.log</file>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/metrics/api_metrics_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>3</maxHistory>
</rollingPolicy>
</appender>
<!-- Metrics信息收集日志 -->
<appender name="SCHEDULED_TASK_LOGGER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/metrics/scheduled_tasks.log</file>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/metrics/scheduled_tasks_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>5</maxHistory>
</rollingPolicy>
</appender>
<logger name="COLLECTOR_METRICS_LOGGER" level="DEBUG" additivity="false">
<appender-ref ref="COLLECTOR_METRICS_LOGGER"/>
</logger>
<logger name="API_METRICS_LOGGER" level="DEBUG" additivity="false">
<appender-ref ref="API_METRICS_LOGGER"/>
</logger>
<logger name="SCHEDULED_TASK_LOGGER" level="DEBUG" additivity="false">
<appender-ref ref="SCHEDULED_TASK_LOGGER"/>
</logger>
<logger name="org.apache.ibatis" level="INFO" additivity="false" />
<logger name="org.mybatis.spring" level="INFO" additivity="false" />
<logger name="com.github.miemiedev.mybatis.paginator" level="INFO" additivity="false" />
<root level="info">
<appender-ref ref="CONSOLE" />
<appender-ref ref="DEBUG_FILE" />
<appender-ref ref="INFO_FILE" />
<appender-ref ref="WARN_FILE" />
<appender-ref ref="ERROR_FILE" />
<!--<appender-ref ref="METRICS_LOG" />-->
</root>
<!--生产环境:输出到文件-->
<!--<springProfile name="pro">-->
<!--<root level="info">-->
<!--<appender-ref ref="CONSOLE" />-->
<!--<appender-ref ref="DEBUG_FILE" />-->
<!--<appender-ref ref="INFO_FILE" />-->
<!--<appender-ref ref="ERROR_FILE" />-->
<!--<appender-ref ref="WARN_FILE" />-->
<!--</root>-->
<!--</springProfile>-->
</configuration>

View File

@@ -35,5 +35,22 @@
<artifactId>kafka-manager-account</artifactId>
<version>${project.parent.version}</version>
</dependency>
<!-- testng -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.10</version>
</dependency>
</dependencies>
</project>

View File

@@ -215,7 +215,7 @@ public class OrderServiceImpl implements OrderService {
} catch (Exception e) {
LOGGER.error("get wait deal order failed.", e);
}
return null;
return Collections.emptyList();
}
@Override
@@ -225,7 +225,7 @@ public class OrderServiceImpl implements OrderService {
} catch (Exception e) {
LOGGER.error("get passed order failed, startTime:{}.", startTime, e);
}
return null;
return Collections.emptyList();
}
private TopicDO getTopicDOFromCacheOrDB(Long physicalClusterId,

View File

@@ -39,6 +39,9 @@ public class ApplyAppOrder extends AbstractAppOrder {
}
OrderDetailApplyAppDTO orderDetailDTO = new OrderDetailApplyAppDTO();
if (ValidateUtils.isNull(orderExtensionDTO)) {
return orderDetailDTO;
}
orderDetailDTO.setName(orderExtensionDTO.getName());
orderDetailDTO.setPrincipals(orderExtensionDTO.getPrincipals());
AppDO appDO = appService.getByName(orderExtensionDTO.getName());

View File

@@ -62,6 +62,9 @@ public class DeleteTopicOrder extends AbstractTopicOrder {
OrderExtensionDeleteTopicDTO.class);
orderDetailDTO.setTopicName(orderExtensionDTO.getTopicName());
ClusterNameDTO clusterNameDTO = clusterService.getClusterName(orderExtensionDTO.getClusterId());
if(ValidateUtils.isNull(clusterNameDTO)) {
return orderDetailDTO;
}
orderDetailDTO.setLogicalClusterId(clusterNameDTO.getLogicalClusterId());
orderDetailDTO.setLogicalClusterName(clusterNameDTO.getLogicalClusterName());
orderDetailDTO.setPhysicalClusterId(clusterNameDTO.getPhysicalClusterId());
@@ -129,6 +132,8 @@ public class DeleteTopicOrder extends AbstractTopicOrder {
if (!PhysicalClusterMetadataManager.isTopicExistStrictly(physicalClusterId, extensionDTO.getTopicName())) {
return ResultStatus.TOPIC_NOT_EXIST;
}
// 最近topic是否还有生产或者消费操作
if (connectionService.isExistConnection(
physicalClusterId,
extensionDTO.getTopicName(),

View File

@@ -0,0 +1,81 @@
package com.xiaojukeji.kafka.manager.bpm;
import com.xiaojukeji.kafka.manager.bpm.component.AbstractOrderStorageService;
import com.xiaojukeji.kafka.manager.bpm.config.BaseTest;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.pojo.OrderDO;
import com.xiaojukeji.kafka.manager.dao.OrderDao;
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.Test;
/**
* @author wyc
* @date 2022/1/5
*/
public class AbstractOrderStorageServiceTest extends BaseTest {
@Autowired
@InjectMocks
private AbstractOrderStorageService abstractOrderStorageService;
@Mock
private OrderDao orderDao;
@BeforeMethod
public void init() {
MockitoAnnotations.initMocks(this);
}
private OrderDO getOrderDO() {
OrderDO orderDO = new OrderDO();
orderDO.setApplicant("applicant");
return orderDO;
}
@Test
public void cancelTest() {
// 返回null测试
cancel2ReturnNullTest();
// 无权限测试
cancel2WithoutAuthority();
// 成功测试
cancel2SuccessTest();
// 数据库错误测试
cancel2MySQLErrorTest();
}
private void cancel2ReturnNullTest() {
Mockito.when(orderDao.getById(Mockito.anyLong())).thenReturn(null);
Assert.assertEquals(abstractOrderStorageService.cancel(1L, "applicant"), ResultStatus.ORDER_NOT_EXIST);
}
private void cancel2WithoutAuthority() {
OrderDO orderDO = getOrderDO();
Mockito.when(orderDao.getById(Mockito.any())).thenReturn(orderDO);
Assert.assertEquals(abstractOrderStorageService.cancel(1L, "username"), ResultStatus.USER_WITHOUT_AUTHORITY);
}
private void cancel2SuccessTest() {
OrderDO orderDO = getOrderDO();
Mockito.when(orderDao.getById(Mockito.anyLong())).thenReturn(orderDO);
Mockito.when(orderDao.updateOrderStatusById(Mockito.anyLong(), Mockito.anyInt())).thenReturn(1);
Assert.assertEquals(abstractOrderStorageService.cancel(1L, "applicant"), ResultStatus.SUCCESS);
}
private void cancel2MySQLErrorTest() {
OrderDO orderDO = getOrderDO();
Mockito.when(orderDao.getById(Mockito.anyLong())).thenReturn(orderDO);
Mockito.when(orderDao.updateOrderStatusById(Mockito.anyLong(), Mockito.anyInt())).thenReturn(0);
Assert.assertEquals(abstractOrderStorageService.cancel(1L, "applicant"), ResultStatus.MYSQL_ERROR);
}
}

View File

@@ -0,0 +1,268 @@
package com.xiaojukeji.kafka.manager.bpm;
import com.xiaojukeji.kafka.manager.account.AccountService;
import com.xiaojukeji.kafka.manager.bpm.common.OrderResult;
import com.xiaojukeji.kafka.manager.bpm.common.OrderStatusEnum;
import com.xiaojukeji.kafka.manager.bpm.common.OrderTypeEnum;
import com.xiaojukeji.kafka.manager.bpm.common.entry.apply.OrderDTO;
import com.xiaojukeji.kafka.manager.bpm.common.handle.OrderHandleBaseDTO;
import com.xiaojukeji.kafka.manager.bpm.common.handle.OrderHandleBatchDTO;
import com.xiaojukeji.kafka.manager.bpm.component.AbstractOrderStorageService;
import com.xiaojukeji.kafka.manager.bpm.config.BaseTest;
import com.xiaojukeji.kafka.manager.common.entity.Result;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.pojo.OrderDO;
import com.xiaojukeji.kafka.manager.service.cache.LogicalClusterMetadataManager;
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.beans.factory.annotation.Value;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
* @author xuguang
* @Date 2021/12/27
*/
public class OrderServiceTest extends BaseTest {
@Value("${test.admin}")
private String ADMIN;
private static final Integer INVALID_ORDER_TYPE = -1;
private static final Integer APPLY_TOPIC_TYPE = 0;
private static final Long ORDER_ID = 1L;
private static final Long INVALID_ORDER_ID = -1L;
// EXTENSIONS中的clusterId需要是自己数据库中真实的逻辑集群id这样createOrder才能跑通
private static final String EXTENSIONS = "{\"clusterId\":15,\"topicName\":\"moduleTest2\",\"appId\":\"dkm_admin\",\"peakBytesIn\":104857600000}";
/**
* 工单状态, 0:待审批, 1:通过, 2:拒绝, 3:取消
*/
private static final Integer ORDER_PASSED_STATUS = 1;
private static final Integer ORDER_REFUSED_STATUS = 2;
@Autowired
@InjectMocks
private OrderService orderService;
@Mock
private AbstractOrderStorageService orderStorageService;
@Mock
private AccountService accountService;
@BeforeMethod
public void setup() {
MockitoAnnotations.initMocks(this);
}
private OrderDTO getOrderDTO() {
OrderDTO orderDTO = new OrderDTO();
orderDTO.setApplicant(ADMIN);
orderDTO.setType(APPLY_TOPIC_TYPE);
orderDTO.setExtensions(EXTENSIONS);
orderDTO.setDescription("测试用测试用");
return orderDTO;
}
private OrderDO getOrderDO() {
OrderDO orderDO = new OrderDO();
orderDO.setId(ORDER_ID);
orderDO.setType(APPLY_TOPIC_TYPE);
orderDO.setTitle("apply topic");
orderDO.setApplicant(ADMIN);
orderDO.setDescription("测试的OrderDO");
orderDO.setApprover(ADMIN);
orderDO.setGmtHandle(new Date());
orderDO.setGmtCreate(new Date());
orderDO.setExtensions(EXTENSIONS);
orderDO.setStatus(ORDER_PASSED_STATUS);
return orderDO;
}
private OrderHandleBaseDTO getOrderHandleBaseDTO() {
OrderHandleBaseDTO orderHandleBaseDTO = new OrderHandleBaseDTO();
orderHandleBaseDTO.setId(ORDER_ID);
orderHandleBaseDTO.setDetail("");
orderHandleBaseDTO.setStatus(ORDER_PASSED_STATUS);
orderHandleBaseDTO.setOpinion("");
return orderHandleBaseDTO;
}
@Test(description = "测试创建工单")
public void createOrder() {
// paramIllegal
createOrder2ParamIllegal();
// checkExtensionFieldsAndGenerateTitle false
createOrder2Success();
// mysql error
createOrder2MysqlError();
}
private void createOrder2ParamIllegal() {
Result order1 = orderService.createOrder(null);
Assert.assertEquals(order1.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
OrderDTO orderDTO = getOrderDTO();
orderDTO.setType(INVALID_ORDER_TYPE);
Result order2 = orderService.createOrder(orderDTO);
Assert.assertEquals(order2.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
}
private void createOrder2Success() {
Mockito.when(orderStorageService.save(Mockito.any())).thenReturn(true);
OrderDTO orderDTO = getOrderDTO();
Result order2 = orderService.createOrder(orderDTO);
Assert.assertEquals(order2.getCode(), ResultStatus.SUCCESS.getCode());
}
private void createOrder2MysqlError() {
Mockito.when(orderStorageService.save(Mockito.any())).thenReturn(false);
OrderDTO orderDTO = getOrderDTO();
Result order2 = orderService.createOrder(orderDTO);
Assert.assertEquals(order2.getCode(), ResultStatus.MYSQL_ERROR.getCode());
}
@Test(description = "测试工单详情")
public void getOrderDetailDataTest() {
// order not exist
getOrderDetailData2OrderNotExistTest();
// param illegal
getOrderDetailData2ParamIllegalTest();
// success
getOrderDetailData2SuccessTest();
}
private void getOrderDetailData2OrderNotExistTest() {
Mockito.when(orderStorageService.getById(Mockito.anyLong())).thenReturn(null);
Result orderDetailData = orderService.getOrderDetailData(ORDER_ID);
Assert.assertEquals(orderDetailData.getCode(), ResultStatus.ORDER_NOT_EXIST.getCode());
}
private void getOrderDetailData2ParamIllegalTest() {
Mockito.when(orderStorageService.getById(Mockito.anyLong())).thenReturn(new OrderDO());
Result orderDetailData = orderService.getOrderDetailData(ORDER_ID);
Assert.assertEquals(orderDetailData.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
}
private void getOrderDetailData2SuccessTest() {
OrderDO orderDO = getOrderDO();
Mockito.when(orderStorageService.getById(Mockito.anyLong())).thenReturn(orderDO);
Result orderDetailData = orderService.getOrderDetailData(ORDER_ID);
Assert.assertEquals(orderDetailData.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test(description = "测试处理工单")
public void handleOrderTest() {
// paramIllegal
handleOrder2ParamIllegalTest();
// orderNotExist
handleOrder2OrderNotExistTest();
// orderAlreadyHandled,
// SpringTool.getUserName() 需要构建上下文对象
handleOrder2OrderAlreadyHandledTest();
}
private void handleOrder2ParamIllegalTest() {
ResultStatus result1 = orderService.handleOrder(null);
Assert.assertEquals(result1.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
OrderDO orderDO = getOrderDO();
orderDO.setType(INVALID_ORDER_TYPE);
Mockito.when(orderStorageService.getById(Mockito.anyLong())).thenReturn(orderDO);
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
ResultStatus result2 = orderService.handleOrder(orderHandleBaseDTO);
Assert.assertEquals(result2.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
}
private void handleOrder2OrderNotExistTest() {
Mockito.when(orderStorageService.getById(Mockito.anyLong())).thenReturn(null);
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
ResultStatus result2 = orderService.handleOrder(orderHandleBaseDTO);
Assert.assertEquals(result2.getCode(), ResultStatus.ORDER_NOT_EXIST.getCode());
}
private void handleOrder2OrderAlreadyHandledTest() {
OrderDO orderDO = getOrderDO();
Mockito.when(orderStorageService.getById(Mockito.anyLong())).thenReturn(orderDO);
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
ResultStatus result2 = orderService.handleOrder(orderHandleBaseDTO);
Assert.assertEquals(result2.getCode(), ResultStatus.ORDER_ALREADY_HANDLED.getCode());
}
@Test(description = "测试获取全部的工单审核列表")
public void getApprovalListTest() {
Mockito.when(accountService.isAdminOrderHandler(Mockito.any())).thenReturn(false);
OrderDO orderDO = getOrderDO();
Mockito.when(orderStorageService.getByApproverAndStatus(Mockito.anyString(), Mockito.any()))
.thenReturn(Arrays.asList(orderDO));
}
@Test
public void handleOrderBatchTest() {
// 要通过所有审批
handleOrderBatchWithPassedTest();
// 要拒绝所有审批
handleOrderBatchWithRefusedTest();
}
private void handleOrderBatchWithPassedTest() {
/*
构造数据尽量走handleOrderBatch的每个流程
*/
OrderDO orderDO1 = getOrderDO();
Mockito.when(orderStorageService.getById(ORDER_ID)).thenReturn(orderDO1);
Mockito.when(orderStorageService.getById(INVALID_ORDER_ID)).thenReturn(null);
OrderDO orderDO2 = getOrderDO();
orderDO2.setId(2L);
orderDO2.setType(OrderTypeEnum.APPLY_APP.getCode());
Mockito.when(orderStorageService.getById(2L)).thenReturn(orderDO2);
OrderHandleBatchDTO orderHandleBatchDTO = new OrderHandleBatchDTO();
orderHandleBatchDTO.setStatus(OrderStatusEnum.PASSED.getCode());
orderHandleBatchDTO.setOrderIdList(Arrays.asList(ORDER_ID, INVALID_ORDER_ID, 2L));
List<OrderResult> orderResults = orderService.handleOrderBatch(orderHandleBatchDTO, ADMIN);
Assert.assertFalse(orderResults.isEmpty());
}
private void handleOrderBatchWithRefusedTest() {
/*
构造数据尽量走handleOrderBatch的每个流程
*/
OrderDO orderDO1 = getOrderDO();
Mockito.when(orderStorageService.getById(ORDER_ID)).thenReturn(orderDO1);
Mockito.when(orderStorageService.getById(INVALID_ORDER_ID)).thenReturn(null);
OrderDO orderDO2 = getOrderDO();
orderDO2.setId(2L);
orderDO2.setType(OrderTypeEnum.APPLY_APP.getCode());
Mockito.when(orderStorageService.getById(2L)).thenReturn(orderDO2);
OrderHandleBatchDTO orderHandleBatchDTO = new OrderHandleBatchDTO();
orderHandleBatchDTO.setStatus(OrderStatusEnum.REFUSED.getCode());
orderHandleBatchDTO.setOrderIdList(Arrays.asList(ORDER_ID, INVALID_ORDER_ID, 2L));
List<OrderResult> orderResults = orderService.handleOrderBatch(orderHandleBatchDTO, ADMIN);
Assert.assertFalse(orderResults.isEmpty());
}
}

View File

@@ -0,0 +1,11 @@
package com.xiaojukeji.kafka.manager.bpm.config;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
@SpringBootTest(classes = CoreSpringBootStartUp.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(classes = CoreSpringBootStartUp.class)
public class BaseTest extends AbstractTransactionalTestNGSpringContextTests {
}

View File

@@ -0,0 +1,21 @@
package com.xiaojukeji.kafka.manager.bpm.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableAsync
@EnableScheduling
@ServletComponentScan
@EnableAutoConfiguration
@SpringBootApplication(scanBasePackages = {"com.xiaojukeji.kafka.manager"})
public class CoreSpringBootStartUp {
public static void main(String[] args) {
SpringApplication sa = new SpringApplication(CoreSpringBootStartUp.class);
sa.run(args);
}
}

View File

@@ -0,0 +1,51 @@
package com.xiaojukeji.kafka.manager.bpm.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* @author zengqiao
* @date 20/3/17
*/
@Configuration
public class DataSourceConfig {
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource.kafka-manager")
@Primary
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "sqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
bean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:mybatis-config.xml"));
return bean.getObject();
}
@Bean(name = "transactionManager")
@Primary
public DataSourceTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "sqlSession")
@Primary
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}

View File

@@ -0,0 +1,139 @@
package com.xiaojukeji.kafka.manager.bpm.order;
import com.xiaojukeji.kafka.manager.bpm.common.entry.detail.AbstractOrderDetailData;
import com.xiaojukeji.kafka.manager.bpm.common.entry.detail.OrderDetailApplyAppDTO;
import com.xiaojukeji.kafka.manager.bpm.common.handle.OrderHandleBaseDTO;
import com.xiaojukeji.kafka.manager.bpm.config.BaseTest;
import com.xiaojukeji.kafka.manager.common.entity.Result;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.pojo.OrderDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.gateway.AppDO;
import com.xiaojukeji.kafka.manager.service.service.gateway.AppService;
import com.xiaojukeji.kafka.manager.service.utils.ConfigUtils;
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.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.Date;
/**
* @author xuguang
* @Date 2021/12/29
*/
public class ApplyAppOrderTest extends BaseTest {
private static final String EXTENSIONS = "{\"name\":\"dkm_admin\",\"idc\":\"country\",\"principals\":\"admin\"}";
private static final Long ORDER_ID = 1L;
@Value("${test.admin}")
private String ADMIN;
private static final Integer APPLY_TOPIC_TYPE = 0;
/**
* 工单状态, 0:待审批, 1:通过, 2:拒绝, 3:取消
*/
private static final Integer ORDER_PASSED_STATUS = 1;
@Value("${test.app.id}")
private String APP_ID;
@Autowired
@Qualifier("applyAppOrder")
@InjectMocks
private AbstractOrder applyAppOrder;
@Mock
private AppService appService;
@Mock
private ConfigUtils configUtils;
@BeforeMethod
public void setup() {
MockitoAnnotations.initMocks(this);
}
private OrderDO getOrderDO() {
OrderDO orderDO = new OrderDO();
orderDO.setId(ORDER_ID);
orderDO.setType(APPLY_TOPIC_TYPE);
orderDO.setTitle("apply topic");
orderDO.setApplicant(ADMIN);
orderDO.setDescription("测试的OrderDO");
orderDO.setApprover(ADMIN);
orderDO.setGmtHandle(new Date());
orderDO.setGmtCreate(new Date());
orderDO.setExtensions(EXTENSIONS);
orderDO.setStatus(ORDER_PASSED_STATUS);
return orderDO;
}
@Test
public void checkExtensionFieldsAndGenerateTitleTest() {
// paramIllegal
checkExtensionFieldsAndGenerateTitle2ParamIllegalTest();
// resourceAlreadyExist
checkExtensionFieldsAndGenerateTitle2ResourceAlreadyExistTest();
// idc not exist
checkExtensionFieldsAndGenerateTitle2IdcNotExistTest();
// success
checkExtensionFieldsAndGenerateTitle2SuccessTest();
}
private void checkExtensionFieldsAndGenerateTitle2ParamIllegalTest() {
Result<String> result = applyAppOrder.checkExtensionFieldsAndGenerateTitle("{}");
Assert.assertEquals(result.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2ResourceAlreadyExistTest() {
Mockito.when(appService.getByName(Mockito.any())).thenReturn(new AppDO());
Result<String> result = applyAppOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.RESOURCE_ALREADY_EXISTED.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2IdcNotExistTest() {
Mockito.when(appService.getByName(Mockito.any())).thenReturn(null);
Mockito.when(configUtils.getIdc()).thenReturn("");
Result<String> result = applyAppOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.IDC_NOT_EXIST.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2SuccessTest() {
Mockito.when(appService.getByName(Mockito.any())).thenReturn(null);
Mockito.when(configUtils.getIdc()).thenReturn("country");
Result<String> result = applyAppOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test(description = "")
public void handleOrderDetailTest() {
Mockito.when(appService.addApp(Mockito.any(), Mockito.any())).thenReturn(ResultStatus.SUCCESS);
OrderDO orderDO = getOrderDO();
ResultStatus resultStatus = applyAppOrder.handleOrderDetail(orderDO, new OrderHandleBaseDTO(), ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test(description = "")
public void getOrderExtensionDetailDataTest() {
AppDO appDO = new AppDO();
appDO.setAppId(APP_ID);
appDO.setPassword("password");
Mockito.when(appService.getByName(Mockito.any())).thenReturn(appDO);
OrderDetailApplyAppDTO data = (OrderDetailApplyAppDTO) applyAppOrder.getOrderExtensionDetailData(EXTENSIONS);
Assert.assertNotNull(data);
Assert.assertEquals(data.getAppId(), APP_ID);
}
}

View File

@@ -0,0 +1,347 @@
package com.xiaojukeji.kafka.manager.bpm.order;
import com.xiaojukeji.kafka.manager.account.AccountService;
import com.xiaojukeji.kafka.manager.bpm.common.entry.detail.OrderDetailApplyAuthorityDTO;
import com.xiaojukeji.kafka.manager.bpm.common.handle.OrderHandleBaseDTO;
import com.xiaojukeji.kafka.manager.bpm.config.BaseTest;
import com.xiaojukeji.kafka.manager.common.entity.Result;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.ao.account.Account;
import com.xiaojukeji.kafka.manager.common.entity.pojo.LogicalClusterDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.OrderDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.TopicDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.gateway.AppDO;
import com.xiaojukeji.kafka.manager.service.cache.LogicalClusterMetadataManager;
import com.xiaojukeji.kafka.manager.service.service.TopicManagerService;
import com.xiaojukeji.kafka.manager.service.service.gateway.AppService;
import com.xiaojukeji.kafka.manager.service.service.gateway.AuthorityService;
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.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.Date;
import java.util.List;
/**
* @author xuguang
* @Date 2021/12/29
*/
public class ApplyAuthorityOrderTest extends BaseTest {
private final static String EXTENSIONS = "{\"clusterId\":7,\"topicName\":\"moduleTest\",\"appId\":\"dkm_admin\",\"access\":\"3\"}";
private final static String TOPIC_NOT_EXIST_EXTENSIONS = "{\"clusterId\":7,\"topicName\":\"xxxx\",\"appId\":\"dkm_admin\",\"access\":\"3\"}";
@Value("${test.phyCluster.id}")
private Long REAL_CLUSTER_ID_IN_MYSQL;
private static final Long ORDER_ID = 1L;
@Value("${test.admin}")
private String ADMIN;
private static final Integer APPLY_TOPIC_TYPE = 0;
@Value("${test.app.id}")
private String APP_ID;
private static final String INVALIDE_USER = "xxxx";
/**
* 工单状态, 0:待审批, 1:通过, 2:拒绝, 3:取消
*/
private static final Integer ORDER_PASSED_STATUS = 1;
@Autowired
@Qualifier("applyAuthorityOrder")
@InjectMocks
private AbstractOrder applyAuthorityOrder;
@Mock
private LogicalClusterMetadataManager logicalClusterMetadataManager;
@Mock
private AppService appService;
@Mock
private TopicManagerService topicManagerService;
@Mock
private AccountService accountService;
@Mock
private AuthorityService authorityService;
@BeforeMethod
public void setup() {
MockitoAnnotations.initMocks(this);
}
private OrderDO getOrderDO() {
OrderDO orderDO = new OrderDO();
orderDO.setId(ORDER_ID);
orderDO.setType(APPLY_TOPIC_TYPE);
orderDO.setTitle("apply topic");
orderDO.setApplicant(ADMIN);
orderDO.setDescription("测试的OrderDO");
orderDO.setApprover(ADMIN);
orderDO.setGmtHandle(new Date());
orderDO.setGmtCreate(new Date());
orderDO.setExtensions(EXTENSIONS);
orderDO.setStatus(ORDER_PASSED_STATUS);
return orderDO;
}
private AppDO getAppDO() {
AppDO appDO = new AppDO();
appDO.setId(4L);
appDO.setAppId(APP_ID);
appDO.setName("moduleTestApp");
appDO.setPassword("moduleTestApp");
appDO.setType(1);
appDO.setApplicant(ADMIN);
appDO.setPrincipals(ADMIN);
appDO.setDescription("moduleTestApp");
appDO.setCreateTime(new Date());
appDO.setModifyTime(new Date());
return appDO;
}
@Test
public void checkExtensionFieldsAndGenerateTitleTest() {
// paramIllegal
checkExtensionFieldsAndGenerateTitle2ParamIllegalTest();
// cluster not exist
checkExtensionFieldsAndGenerateTitle2ClusterNotExistTest();
// topic not exist
checkExtensionFieldsAndGenerateTitle2TopicNotExistTest();
// app not exist
checkExtensionFieldsAndGenerateTitle2AppNotExistTest();
// success
checkExtensionFieldsAndGenerateTitle2SuccessTest();
}
private void checkExtensionFieldsAndGenerateTitle2ParamIllegalTest() {
Result<String> result = applyAuthorityOrder.checkExtensionFieldsAndGenerateTitle("{}");
Assert.assertEquals(result.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2ClusterNotExistTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(null);
Result<String> result = applyAuthorityOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.CLUSTER_NOT_EXIST.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2TopicNotExistTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Result<String> result = applyAuthorityOrder.checkExtensionFieldsAndGenerateTitle(TOPIC_NOT_EXIST_EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.TOPIC_NOT_EXIST.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2AppNotExistTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(null);
Result<String> result = applyAuthorityOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.APP_NOT_EXIST.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2SuccessTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(new AppDO());
Result<String> result = applyAuthorityOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test
public void checkAuthorityTest() {
// JsonParseError
checkAuthority2JsonParseErrorTest();
// cluster not exist
checkAuthority2ClusterNotExistTest();
// topic not exist
checkAuthority2TopicNotExistTest();
// user without authority
checkAuthority2UserWithoutAuthorityTest();
// success
checkAuthority2SuccessTest();
}
private void checkAuthority2JsonParseErrorTest() {
OrderDO orderDO = getOrderDO();
orderDO.setExtensions("{");
ResultStatus resultStatus = applyAuthorityOrder.checkAuthority(orderDO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.JSON_PARSER_ERROR.getCode());
}
private void checkAuthority2ClusterNotExistTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(null);
OrderDO orderDO = getOrderDO();
ResultStatus resultStatus = applyAuthorityOrder.checkAuthority(orderDO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.CLUSTER_NOT_EXIST.getCode());
}
private void checkAuthority2TopicNotExistTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Mockito.when(topicManagerService.getByTopicName(Mockito.any(), Mockito.any())).thenReturn(null);
OrderDO orderDO = getOrderDO();
ResultStatus resultStatus = applyAuthorityOrder.checkAuthority(orderDO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.TOPIC_BIZ_DATA_NOT_EXIST.getCode());
}
private void checkAuthority2UserWithoutAuthorityTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
TopicDO topicDO = new TopicDO();
topicDO.setAppId(APP_ID);
Mockito.when(topicManagerService.getByTopicName(Mockito.any(), Mockito.any())).thenReturn(topicDO);
AppDO appDO = new AppDO();
appDO.setPrincipals("");
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(appDO);
OrderDO orderDO = getOrderDO();
ResultStatus resultStatus = applyAuthorityOrder.checkAuthority(orderDO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.USER_WITHOUT_AUTHORITY.getCode());
}
private void checkAuthority2SuccessTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
TopicDO topicDO = new TopicDO();
topicDO.setAppId(APP_ID);
Mockito.when(topicManagerService.getByTopicName(Mockito.any(), Mockito.any())).thenReturn(topicDO);
AppDO appDO = new AppDO();
appDO.setPrincipals(ADMIN);
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(appDO);
OrderDO orderDO = getOrderDO();
ResultStatus resultStatus = applyAuthorityOrder.checkAuthority(orderDO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test
public void getApproverListTest() {
// emptyList
getApproverList2EmptyListTest();
// not empty list
getApproverList2NotEmptyListTest();
}
private void getApproverList2EmptyListTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Mockito.when(topicManagerService.getByTopicName(Mockito.any(), Mockito.any())).thenReturn(null);
List<Account> approverList = applyAuthorityOrder.getApproverList(EXTENSIONS);
Assert.assertTrue(approverList.isEmpty());
TopicDO topicDO = new TopicDO();
topicDO.setAppId(APP_ID);
Mockito.when(topicManagerService.getByTopicName(Mockito.any(), Mockito.any())).thenReturn(topicDO);
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(null);
List<Account> approverList2 = applyAuthorityOrder.getApproverList(EXTENSIONS);
Assert.assertTrue(approverList2.isEmpty());
}
private void getApproverList2NotEmptyListTest() {
TopicDO topicDO = new TopicDO();
topicDO.setAppId(APP_ID);
Mockito.when(topicManagerService.getByTopicName(Mockito.any(), Mockito.any())).thenReturn(topicDO);
AppDO appDO = getAppDO();
appDO.setPrincipals(ADMIN + "," + INVALIDE_USER);
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(appDO);
Mockito.when(accountService.getAccountFromCache(ADMIN)).thenReturn(new Account());
Mockito.when(accountService.getAccountFromCache(INVALIDE_USER)).thenReturn(null);
List<Account> approverList = applyAuthorityOrder.getApproverList(EXTENSIONS);
Assert.assertFalse(approverList.isEmpty());
}
@Test
public void handleOrderDetailTest() {
// cluster not exist
handleOrderDetail2ClusterNotExistTest();
// operationFailed
handleOrderDetail2OperationFailedTest();
// success
handleOrderDetail2SuccessTest();
}
private void handleOrderDetail2ClusterNotExistTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(null);
OrderDO orderDO = getOrderDO();
ResultStatus resultStatus = applyAuthorityOrder.handleOrderDetail(orderDO, new OrderHandleBaseDTO(), ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.CLUSTER_NOT_EXIST.getCode());
}
private void handleOrderDetail2OperationFailedTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Mockito.when(authorityService.addAuthorityAndQuota(Mockito.any(), Mockito.any())).thenReturn(-1);
OrderDO orderDO = getOrderDO();
ResultStatus resultStatus = applyAuthorityOrder.handleOrderDetail(orderDO, new OrderHandleBaseDTO(), ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.OPERATION_FAILED.getCode());
}
private void handleOrderDetail2SuccessTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Mockito.when(authorityService.addAuthorityAndQuota(Mockito.any(), Mockito.any())).thenReturn(1);
OrderDO orderDO = getOrderDO();
ResultStatus resultStatus = applyAuthorityOrder.handleOrderDetail(orderDO, new OrderHandleBaseDTO(), ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test
public void getOrderExtensionDetailDataTest() {
// app is null
getOrderExtensionDetailData2AppIsNullTest();
// app is not null
getOrderExtensionDetailData2AppNotNullTest();
}
private void getOrderExtensionDetailData2AppIsNullTest() {
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(null);
LogicalClusterDO logicalClusterDO = new LogicalClusterDO();
logicalClusterDO.setId(REAL_CLUSTER_ID_IN_MYSQL);
logicalClusterDO.setName("");
Mockito.when(logicalClusterMetadataManager.getLogicalCluster(Mockito.anyLong())).thenReturn(logicalClusterDO);
OrderDetailApplyAuthorityDTO data = (OrderDetailApplyAuthorityDTO) applyAuthorityOrder.getOrderExtensionDetailData(EXTENSIONS);
Assert.assertNotNull(data);
Assert.assertNull(data.getAppName());
}
private void getOrderExtensionDetailData2AppNotNullTest() {
AppDO appDO = getAppDO();
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(appDO);
LogicalClusterDO logicalClusterDO = new LogicalClusterDO();
logicalClusterDO.setId(REAL_CLUSTER_ID_IN_MYSQL);
logicalClusterDO.setName("");
Mockito.when(logicalClusterMetadataManager.getLogicalCluster(Mockito.anyLong())).thenReturn(logicalClusterDO);
OrderDetailApplyAuthorityDTO data = (OrderDetailApplyAuthorityDTO) applyAuthorityOrder.getOrderExtensionDetailData(EXTENSIONS);
Assert.assertNotNull(data);
Assert.assertNotNull(data.getAppName());
Assert.assertEquals(data.getAppName(), appDO.getName());
}
}

View File

@@ -0,0 +1,110 @@
package com.xiaojukeji.kafka.manager.bpm.order;
import com.xiaojukeji.kafka.manager.bpm.common.entry.detail.OrderDetailApplyClusterDTO;
import com.xiaojukeji.kafka.manager.bpm.common.handle.OrderHandleBaseDTO;
import com.xiaojukeji.kafka.manager.bpm.config.BaseTest;
import com.xiaojukeji.kafka.manager.common.entity.Result;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.pojo.OrderDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.gateway.AppDO;
import com.xiaojukeji.kafka.manager.service.service.gateway.AppService;
import com.xiaojukeji.kafka.manager.service.utils.ConfigUtils;
import org.junit.Assert;
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.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
/**
* @author xuguang
* @Date 2021/12/30
*/
public class ApplyClusterOrderTest extends BaseTest {
@Value("${test.app.id}")
private String APP_ID;
private final static String IDC = "国内";
private final static String EXTENSIONS = "{\"idc\":\"国内\",\"bytesIn\":2000,\"mode\":200,\"appId\":\"dkm_admin\"}";
private final static String INVALID_IDC = "xxx";
@Value("${test.admin}")
private String ADMIN;
@Autowired
@Qualifier("applyClusterOrder")
@InjectMocks
private AbstractOrder applyClusterOrder;
@Mock
private AppService appService;
@Mock
private ConfigUtils configUtils;
@BeforeMethod
public void setup() {
MockitoAnnotations.initMocks(this);
}
@Test
public void checkExtensionFieldsAndGenerateTitleTest() {
// paramIllegal
checkExtensionFieldsAndGenerateTitle2ParamIllegalTest();
// app not exist
checkExtensionFieldsAndGenerateTitle2AppNotExistTest();
// idc not exist
checkExtensionFieldsAndGenerateTitle2IdcNotExistTest();
// success
checkExtensionFieldsAndGenerateTitle2SuccessTest();
}
private void checkExtensionFieldsAndGenerateTitle2ParamIllegalTest() {
Result<String> result = applyClusterOrder.checkExtensionFieldsAndGenerateTitle("{}");
Assert.assertEquals(result.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2AppNotExistTest() {
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(null);
Result<String> result = applyClusterOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.APP_NOT_EXIST.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2IdcNotExistTest() {
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(new AppDO());
Mockito.when(configUtils.getIdc()).thenReturn(INVALID_IDC);
Result<String> result = applyClusterOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.IDC_NOT_EXIST.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2SuccessTest() {
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(new AppDO());
Mockito.when(configUtils.getIdc()).thenReturn(IDC);
Result<String> result = applyClusterOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test
public void getOrderExtensionDetailDataTest() {
OrderDetailApplyClusterDTO data = (OrderDetailApplyClusterDTO) applyClusterOrder.getOrderExtensionDetailData(EXTENSIONS);
Assert.assertEquals(data.getAppId(), APP_ID);
Assert.assertEquals(data.getIdc(), IDC);
}
@Test
public void handleOrderDetail() {
ResultStatus resultStatus = applyClusterOrder.handleOrderDetail(new OrderDO(), new OrderHandleBaseDTO(), ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
}
}

View File

@@ -0,0 +1,218 @@
package com.xiaojukeji.kafka.manager.bpm.order;
import com.xiaojukeji.kafka.manager.bpm.common.entry.detail.AbstractOrderDetailData;
import com.xiaojukeji.kafka.manager.bpm.common.entry.detail.PartitionOrderDetailData;
import com.xiaojukeji.kafka.manager.bpm.common.handle.OrderHandleBaseDTO;
import com.xiaojukeji.kafka.manager.bpm.config.BaseTest;
import com.xiaojukeji.kafka.manager.common.entity.Result;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.pojo.ClusterDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.LogicalClusterDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.OrderDO;
import com.xiaojukeji.kafka.manager.service.cache.LogicalClusterMetadataManager;
import com.xiaojukeji.kafka.manager.service.service.AdminService;
import com.xiaojukeji.kafka.manager.service.service.ClusterService;
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.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.Date;
/**
* @author xuguang
* @Date 2021/12/30
*/
public class ApplyPartitionOrderTest extends BaseTest {
private final static String EXTENSIONS = "{\"clusterId\":1,\"topicName\":\"moduleTest\",\"needIncrPartitionNum\":20}";
private final static String INVALIDE_TOPIC_EXTENSIONS = "{\"clusterId\":1,\"topicName\":\"xxxx\",\"needIncrPartitionNum\":20}";
private static final String APPROVE_ORDER_APPLY_DETAIL = "{\"brokerIdList\":[3],\"partitionNum\":1,\"regionId\":1}";
private static final String INVALIDE_APPROVE_ORDER_APPLY_DETAIL = "{\"brokerIdList\":[3],\"partitionNum\":0,\"regionId\":1}";
@Value("${test.phyCluster.id}")
private Long REAL_CLUSTER_ID_IN_MYSQL;
/**
* 工单状态, 0:待审批, 1:通过, 2:拒绝, 3:取消
*/
private static final Integer ORDER_PASSED_STATUS = 1;
@Value("${test.admin}")
private String ADMIN;
private static final String INVALID_USER_NAME = "xxxxx";
private static final Integer INVALID_ORDER_TYPE = -1;
private static final Integer APPLY_TOPIC_TYPE = 0;
private static final Long ORDER_ID = 1L;
private static final Long INVALID_ORDER_ID = -1L;
@Autowired
@Qualifier("applyPartitionOrder")
@InjectMocks
private AbstractOrder applyPartitionOrder;
@Mock
private LogicalClusterMetadataManager logicalClusterMetadataManager;
@Mock
private AdminService adminService;
@Mock
private ClusterService clusterService;
private OrderDO getOrderDO() {
OrderDO orderDO = new OrderDO();
orderDO.setId(ORDER_ID);
orderDO.setType(APPLY_TOPIC_TYPE);
orderDO.setTitle("apply topic");
orderDO.setApplicant(ADMIN);
orderDO.setDescription("测试的OrderDO");
orderDO.setApprover(ADMIN);
orderDO.setGmtHandle(new Date());
orderDO.setGmtCreate(new Date());
orderDO.setExtensions(EXTENSIONS);
orderDO.setStatus(ORDER_PASSED_STATUS);
return orderDO;
}
private OrderHandleBaseDTO getOrderHandleBaseDTO() {
OrderHandleBaseDTO orderHandleBaseDTO = new OrderHandleBaseDTO();
orderHandleBaseDTO.setId(ORDER_ID);
orderHandleBaseDTO.setStatus(ORDER_PASSED_STATUS);
orderHandleBaseDTO.setDetail(APPROVE_ORDER_APPLY_DETAIL);
return orderHandleBaseDTO;
}
@BeforeMethod
public void setup() {
MockitoAnnotations.initMocks(this);
}
@Test
public void checkExtensionFieldsAndGenerateTitleTest() {
// paramIllegal
checkExtensionFieldsAndGenerateTitle2ParamIllegalTest();
// cluster not exist
checkExtensionFieldsAndGenerateTitle2ClusterNotExistTest();
// topic not exist
checkExtensionFieldsAndGenerateTitle2TopicNotExistTest();
// success
checkExtensionFieldsAndGenerateTitle2SuccessTest();
}
private void checkExtensionFieldsAndGenerateTitle2ParamIllegalTest() {
Result<String> stringResult = applyPartitionOrder.checkExtensionFieldsAndGenerateTitle("{}");
Assert.assertEquals(stringResult.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2ClusterNotExistTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(Mockito.anyLong(), Mockito.any())).thenReturn(null);
Result<String> stringResult = applyPartitionOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
Assert.assertEquals(stringResult.getCode(), ResultStatus.CLUSTER_NOT_EXIST.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2TopicNotExistTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Result<String> stringResult = applyPartitionOrder.checkExtensionFieldsAndGenerateTitle(INVALIDE_TOPIC_EXTENSIONS);
Assert.assertEquals(stringResult.getCode(), ResultStatus.TOPIC_NOT_EXIST.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2SuccessTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Result<String> stringResult = applyPartitionOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
Assert.assertEquals(stringResult.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test
public void handleOrderDetailTest() {
// cluster not exist
handleOrderDetail2ClusterNotExistTest();
// topic not exist
handleOrderDetail2TopicNotExistTest();
// operation failed
handleOrderDetail2OperationFailedTest();
// success
handleOrderDetail2SuccessTest();
}
private void handleOrderDetail2ClusterNotExistTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(Mockito.anyLong(), Mockito.any())).thenReturn(null);
OrderDO orderDO = getOrderDO();
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
ResultStatus resultStatus = applyPartitionOrder.handleOrderDetail(orderDO, orderHandleBaseDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.CLUSTER_NOT_EXIST.getCode());
}
private void handleOrderDetail2TopicNotExistTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
OrderDO orderDO = getOrderDO();
orderDO.setExtensions(INVALIDE_TOPIC_EXTENSIONS);
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
ResultStatus resultStatus = applyPartitionOrder.handleOrderDetail(orderDO, orderHandleBaseDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.TOPIC_NOT_EXIST.getCode());
}
private void handleOrderDetail2OperationFailedTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
OrderDO orderDO = getOrderDO();
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
orderHandleBaseDTO.setDetail(INVALIDE_APPROVE_ORDER_APPLY_DETAIL);
ResultStatus resultStatus = applyPartitionOrder.handleOrderDetail(orderDO, orderHandleBaseDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.OPERATION_FAILED.getCode());
}
private void handleOrderDetail2SuccessTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Mockito.when(adminService.expandPartitions(
Mockito.any(), Mockito.any(), Mockito.any(),
Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(ResultStatus.SUCCESS);
OrderDO orderDO = getOrderDO();
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
ResultStatus resultStatus = applyPartitionOrder.handleOrderDetail(orderDO, orderHandleBaseDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test
public void getOrderExtensionDetailDataTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
LogicalClusterDO logicalCluster = new LogicalClusterDO();
logicalCluster.setId(REAL_CLUSTER_ID_IN_MYSQL);
logicalCluster.setName("");
Mockito.when(logicalClusterMetadataManager.getLogicalCluster(
Mockito.anyLong(), Mockito.any())).thenReturn(logicalCluster);
ClusterDO clusterDO = new ClusterDO();
clusterDO.setId(REAL_CLUSTER_ID_IN_MYSQL);
clusterDO.setClusterName("");
Mockito.when(clusterService.getById(Mockito.any())).thenReturn(clusterDO);
PartitionOrderDetailData data2 = (PartitionOrderDetailData) applyPartitionOrder.getOrderExtensionDetailData(EXTENSIONS);
Assert.assertNotNull(data2);
Assert.assertNotNull(data2.getPhysicalClusterId());
Assert.assertNotNull(data2.getLogicalClusterId());
}
}

View File

@@ -0,0 +1,319 @@
package com.xiaojukeji.kafka.manager.bpm.order;
import com.xiaojukeji.kafka.manager.bpm.OrderService;
import com.xiaojukeji.kafka.manager.bpm.common.entry.detail.QuotaOrderDetailData;
import com.xiaojukeji.kafka.manager.bpm.common.handle.OrderHandleBaseDTO;
import com.xiaojukeji.kafka.manager.bpm.config.BaseTest;
import com.xiaojukeji.kafka.manager.common.entity.Result;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.pojo.ClusterDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.LogicalClusterDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.OrderDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.gateway.AppDO;
import com.xiaojukeji.kafka.manager.service.cache.LogicalClusterMetadataManager;
import com.xiaojukeji.kafka.manager.service.service.AdminService;
import com.xiaojukeji.kafka.manager.service.service.ClusterService;
import com.xiaojukeji.kafka.manager.service.service.RegionService;
import com.xiaojukeji.kafka.manager.service.service.TopicManagerService;
import com.xiaojukeji.kafka.manager.service.service.gateway.AppService;
import com.xiaojukeji.kafka.manager.service.service.gateway.QuotaService;
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.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
/**
* @author xuguang
* @Date 2021/12/30
*/
public class ApplyQuotaOrderTest extends BaseTest {
@Value("${test.admin}")
private String ADMIN;
private static final String INVALID_USER_NAME = "xxxxx";
private static final Integer INVALID_ORDER_TYPE = -1;
private static final Integer APPLY_TOPIC_TYPE = 0;
private static final Long ORDER_ID = 1L;
private static final Long INVALID_ORDER_ID = -1L;
private static final String EXTENSIONS = "{\"appId\":\"dkm_admin\",\"clusterId\":7,\"partitionNum\":2,\"produceQuota\":104857600,\"consumeQuota\":104857600,\"regionId\":1,\"topicName\":\"moduleTest\",\"brokerIdList\":[3]}";
private static final String TOPIC_NOT_EXIST_EXTENSIONS = "{\"appId\":\"dkm_admin\",\"clusterId\":7,\"partitionNum\":2,\"produceQuota\":104857600,\"consumeQuota\":104857600,\"regionId\":1,\"retentionTime\":12,\"topicName\":\"xxx\",\"brokerIdList\":[3]}";
private static final String APPROVE_ORDER_APPLY_DETAIL = "{\"brokerIdList\":[3],\"partitionNum\":1,\"regionId\":1}";
@Value("${test.phyCluster.id}")
private Long REAL_CLUSTER_ID_IN_MYSQL;
/**
* 工单状态, 0:待审批, 1:通过, 2:拒绝, 3:取消
*/
private static final Integer ORDER_PASSED_STATUS = 1;
@Autowired
@Qualifier("applyQuotaOrder")
@InjectMocks
private AbstractOrder applyQuotaOrder;
@Mock
private LogicalClusterMetadataManager logicalClusterMetadataManager;
@Mock
private AdminService adminService;
@Mock
private ClusterService clusterService;
@Mock
private AppService appService;
@Mock
private OrderService orderService;
@Mock
private QuotaService quotaService;
@Mock
private TopicManagerService topicManagerService;
@Mock
private RegionService regionService;
private OrderDO getOrderDO() {
OrderDO orderDO = new OrderDO();
orderDO.setId(ORDER_ID);
orderDO.setType(APPLY_TOPIC_TYPE);
orderDO.setTitle("apply topic");
orderDO.setApplicant(ADMIN);
orderDO.setDescription("测试的OrderDO");
orderDO.setApprover(ADMIN);
orderDO.setGmtHandle(new Date());
orderDO.setGmtCreate(new Date());
orderDO.setExtensions(EXTENSIONS);
orderDO.setStatus(ORDER_PASSED_STATUS);
return orderDO;
}
private OrderHandleBaseDTO getOrderHandleBaseDTO() {
OrderHandleBaseDTO orderHandleBaseDTO = new OrderHandleBaseDTO();
orderHandleBaseDTO.setId(ORDER_ID);
orderHandleBaseDTO.setStatus(ORDER_PASSED_STATUS);
orderHandleBaseDTO.setDetail(APPROVE_ORDER_APPLY_DETAIL);
return orderHandleBaseDTO;
}
private LogicalClusterDO getLogicalClusterDO() {
LogicalClusterDO logicalClusterDO = new LogicalClusterDO();
logicalClusterDO.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
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;
}
private AppDO getAppDO() {
AppDO appDO = new AppDO();
appDO.setId(4L);
appDO.setAppId("moduleTestAppId");
appDO.setName("moduleTestApp");
appDO.setPassword("moduleTestApp");
appDO.setType(1);
appDO.setApplicant("admin");
appDO.setPrincipals("admin");
appDO.setDescription("moduleTestApp");
appDO.setCreateTime(new Date(1638786493173L));
appDO.setModifyTime(new Date(1638786493173L));
return appDO;
}
@BeforeMethod
public void setup() {
MockitoAnnotations.initMocks(this);
}
@Test(description = "测试检查扩展字段并生成工单的Title")
public void checkExtensionFieldsAndGenerateTitle() {
// paramIllegal
checkExtensionFieldsAndGenerateTitle2ParamIllegal();
// cluster not exist
checkExtensionFieldsAndGenerateTitle2ClusterNotExist();
// topic not exist
checkExtensionFieldsAndGenerateTitle2TopicNotExist();
// app not exist
checkExtensionFieldsAndGenerateTitle2AppNotExist();
// success
checkExtensionFieldsAndGenerateTitle2Success();
}
private void checkExtensionFieldsAndGenerateTitle2ParamIllegal() {
Result<String> result = applyQuotaOrder.checkExtensionFieldsAndGenerateTitle("{}");
Assert.assertEquals(result.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2ClusterNotExist() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(null);
Result<String> result = applyQuotaOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.CLUSTER_NOT_EXIST.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2TopicNotExist() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Result<String> result = applyQuotaOrder.checkExtensionFieldsAndGenerateTitle(TOPIC_NOT_EXIST_EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.TOPIC_NOT_EXIST.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2AppNotExist() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(null);
Result<String> result = applyQuotaOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.APP_NOT_EXIST.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2Success() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(new AppDO());
Result<String> result = applyQuotaOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test
public void handleOrderDetail() {
// app not exist
handleOrderDetail2AppNotExist();
// cluster not exist
handleOrderDetail2ClusterNotExist();
// topic not exist
handleOrderDetail2TopicNotExist();
// operation fobidden
handleOrderDetail2OperationFobiddenTest();
// success
handleOrderDetail2SuccessTest();
//
handleOrderDetail2OperationFailedTest();
}
private void handleOrderDetail2AppNotExist() {
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(null);
OrderDO orderDO = getOrderDO();
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
ResultStatus resultStatus = applyQuotaOrder.handleOrderDetail(orderDO, orderHandleBaseDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.APP_NOT_EXIST.getCode());
}
private void handleOrderDetail2ClusterNotExist() {
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(new AppDO());
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(Mockito.anyLong(), Mockito.any())).thenReturn(null);
OrderDO orderDO = getOrderDO();
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
ResultStatus resultStatus = applyQuotaOrder.handleOrderDetail(orderDO, orderHandleBaseDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.CLUSTER_NOT_EXIST.getCode());
}
private void handleOrderDetail2TopicNotExist() {
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(new AppDO());
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
OrderDO orderDO = getOrderDO();
orderDO.setExtensions(TOPIC_NOT_EXIST_EXTENSIONS);
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
ResultStatus resultStatus = applyQuotaOrder.handleOrderDetail(orderDO, orderHandleBaseDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.TOPIC_NOT_EXIST.getCode());
}
private void handleOrderDetail2OperationFobiddenTest() {
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(new AppDO());
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Mockito.when(adminService.expandPartitions(
Mockito.any(), Mockito.any(), Mockito.any(),
Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(ResultStatus.OPERATION_FORBIDDEN);
OrderDO orderDO = getOrderDO();
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
ResultStatus resultStatus = applyQuotaOrder.handleOrderDetail(orderDO, orderHandleBaseDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.OPERATION_FORBIDDEN.getCode());
}
private void handleOrderDetail2SuccessTest() {
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(new AppDO());
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Mockito.when(adminService.expandPartitions(
Mockito.any(), Mockito.any(), Mockito.any(),
Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(ResultStatus.SUCCESS);
Mockito.when(quotaService.addTopicQuota(Mockito.any())).thenReturn(1);
OrderDO orderDO = getOrderDO();
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
ResultStatus resultStatus = applyQuotaOrder.handleOrderDetail(orderDO, orderHandleBaseDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
}
private void handleOrderDetail2OperationFailedTest() {
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(new AppDO());
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Mockito.when(adminService.expandPartitions(
Mockito.any(), Mockito.any(), Mockito.any(),
Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(ResultStatus.SUCCESS);
Mockito.when(quotaService.addTopicQuota(Mockito.any())).thenReturn(0);
OrderDO orderDO = getOrderDO();
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
ResultStatus resultStatus = applyQuotaOrder.handleOrderDetail(orderDO, orderHandleBaseDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.OPERATION_FAILED.getCode());
}
@Test
public void getOrderExtensionDetailData() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
AppDO appDO = getAppDO();
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(appDO);
Mockito.when(topicManagerService.getTopicStatistic(
Mockito.any(), Mockito.any(),
Mockito.any(), Mockito.any())).thenReturn(Collections.emptyList());
Mockito.when(regionService.getRegionListByTopicName(Mockito.any(), Mockito.any())).thenReturn(Collections.emptyList());
ClusterDO clusterDO = new ClusterDO();
clusterDO.setId(REAL_CLUSTER_ID_IN_MYSQL);
clusterDO.setClusterName("");
Mockito.when(clusterService.getById(Mockito.any())).thenReturn(clusterDO);
LogicalClusterDO logicalClusterDO = new LogicalClusterDO();
logicalClusterDO.setId(REAL_CLUSTER_ID_IN_MYSQL);
logicalClusterDO.setName("");
Mockito.when(logicalClusterMetadataManager.getLogicalCluster(
Mockito.anyLong(), Mockito.any())).thenReturn(logicalClusterDO);
QuotaOrderDetailData data = (QuotaOrderDetailData) applyQuotaOrder.getOrderExtensionDetailData(EXTENSIONS);
Assert.assertEquals(data.getLogicalClusterId(), REAL_CLUSTER_ID_IN_MYSQL);
}
}

View File

@@ -0,0 +1,341 @@
package com.xiaojukeji.kafka.manager.bpm.order;
import com.xiaojukeji.kafka.manager.bpm.OrderService;
import com.xiaojukeji.kafka.manager.bpm.common.OrderStatusEnum;
import com.xiaojukeji.kafka.manager.bpm.common.entry.BaseOrderDetailData;
import com.xiaojukeji.kafka.manager.bpm.common.entry.detail.AbstractOrderDetailData;
import com.xiaojukeji.kafka.manager.bpm.common.entry.detail.OrderDetailApplyTopicDTO;
import com.xiaojukeji.kafka.manager.bpm.common.handle.OrderHandleBaseDTO;
import com.xiaojukeji.kafka.manager.bpm.config.BaseTest;
import com.xiaojukeji.kafka.manager.common.entity.Result;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.pojo.ClusterDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.LogicalClusterDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.OrderDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.gateway.AppDO;
import com.xiaojukeji.kafka.manager.service.cache.LogicalClusterMetadataManager;
import com.xiaojukeji.kafka.manager.service.service.AdminService;
import com.xiaojukeji.kafka.manager.service.service.ClusterService;
import com.xiaojukeji.kafka.manager.service.service.gateway.AppService;
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.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.Date;
/**
* @author xuguang
* @Date 2021/12/27
*/
public class ApplyTopicOrderTest extends BaseTest {
@Value("${test.admin}")
private String ADMIN;
private static final String INVALID_USER_NAME = "xxxxx";
private static final Integer INVALID_ORDER_TYPE = -1;
private static final Integer APPLY_TOPIC_TYPE = 0;
private static final Long ORDER_ID = 1L;
private static final Long INVALID_ORDER_ID = -1L;
private static final String EXTENSIONS = "{\"clusterId\":7,\"topicName\":\"moduleTest2\",\"appId\":\"dkm_admin\",\"peakBytesIn\":104857600000}";
private static final String TOPIC_EXIST_EXTENSIONS = "{\"clusterId\":7,\"topicName\":\"moduleTest\",\"appId\":\"dkm_admin\",\"peakBytesIn\":104857600000}";
private static final String APPROVE_ORDER_APPLY_DETAIL = "{\"brokerIdList\":[3],\"partitionNum\":1,\"replicaNum\":1,\"retentionTime\":12}";
@Value("${test.phyCluster.id}")
private Long REAL_CLUSTER_ID_IN_MYSQL;
/**
* 工单状态, 0:待审批, 1:通过, 2:拒绝, 3:取消
*/
private static final Integer ORDER_PASSED_STATUS = 1;
@Autowired
@Qualifier("applyTopicOrder")
@InjectMocks
private AbstractTopicOrder applyTopicOrder;
@Mock
private LogicalClusterMetadataManager logicalClusterMetadataManager;
@Mock
private AdminService adminService;
@Mock
private ClusterService clusterService;
@Mock
private AppService appService;
@Mock
private OrderService orderService;
private OrderDO getOrderDO() {
OrderDO orderDO = new OrderDO();
orderDO.setId(ORDER_ID);
orderDO.setType(APPLY_TOPIC_TYPE);
orderDO.setTitle("apply topic");
orderDO.setApplicant(ADMIN);
orderDO.setDescription("测试的OrderDO");
orderDO.setApprover(ADMIN);
orderDO.setGmtHandle(new Date());
orderDO.setGmtCreate(new Date());
orderDO.setExtensions(EXTENSIONS);
orderDO.setStatus(ORDER_PASSED_STATUS);
return orderDO;
}
private OrderHandleBaseDTO getOrderHandleBaseDTO() {
OrderHandleBaseDTO orderHandleBaseDTO = new OrderHandleBaseDTO();
orderHandleBaseDTO.setId(ORDER_ID);
orderHandleBaseDTO.setStatus(ORDER_PASSED_STATUS);
orderHandleBaseDTO.setDetail(APPROVE_ORDER_APPLY_DETAIL);
return orderHandleBaseDTO;
}
private LogicalClusterDO getLogicalClusterDO() {
LogicalClusterDO logicalClusterDO = new LogicalClusterDO();
logicalClusterDO.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
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;
}
private AppDO getAppDO() {
AppDO appDO = new AppDO();
appDO.setId(4L);
appDO.setAppId("moduleTestAppId");
appDO.setName("moduleTestApp");
appDO.setPassword("moduleTestApp");
appDO.setType(1);
appDO.setApplicant("admin");
appDO.setPrincipals("admin");
appDO.setDescription("moduleTestApp");
appDO.setCreateTime(new Date(1638786493173L));
appDO.setModifyTime(new Date(1638786493173L));
return appDO;
}
@BeforeMethod
public void setup() {
MockitoAnnotations.initMocks(this);
}
@Test(description = "测试获取工单详情")
public void getOrderDetailTest() {
// null
getOrderDetail2NullTest();
// success
getOrderDetail2SuccessTest();
}
private void getOrderDetail2NullTest() {
BaseOrderDetailData orderDetail = applyTopicOrder.getOrderDetail(null);
Assert.assertNull(orderDetail);
}
private void getOrderDetail2SuccessTest() {
BaseOrderDetailData orderDetail = applyTopicOrder.getOrderDetail(getOrderDO());
Assert.assertNotNull(orderDetail);
}
@Test
public void getOrderExtensionDetailDataTest() {
// app is null
getOrderExtensionDetailData2AppIsNullTest();
// app is not null
getOrderExtensionDetailData2AppIsNotNullTest();
}
private void getOrderExtensionDetailData2AppIsNullTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
LogicalClusterDO logicalClusterDO = getLogicalClusterDO();
Mockito.when(logicalClusterMetadataManager.getLogicalCluster(
Mockito.anyLong(), Mockito.any())).thenReturn(logicalClusterDO);
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(null);
OrderDetailApplyTopicDTO data = (OrderDetailApplyTopicDTO) applyTopicOrder.getOrderExtensionDetailData(EXTENSIONS);
Assert.assertNotNull(data);
Assert.assertNull(data.getAppName());
}
private void getOrderExtensionDetailData2AppIsNotNullTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
LogicalClusterDO logicalClusterDO = getLogicalClusterDO();
Mockito.when(logicalClusterMetadataManager.getLogicalCluster(
Mockito.anyLong(), Mockito.any())).thenReturn(logicalClusterDO);
AppDO appDO = getAppDO();
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(appDO);
OrderDetailApplyTopicDTO data = (OrderDetailApplyTopicDTO) applyTopicOrder.getOrderExtensionDetailData(EXTENSIONS);
Assert.assertNotNull(data);
Assert.assertNotNull(data.getAppId());
Assert.assertEquals(data.getAppName(), appDO.getName());
}
@Test(description = "测试检查扩展字段并生成工单的Title")
public void checkExtensionFieldsAndGenerateTitle() {
// paramIllegal
checkExtensionFieldsAndGenerateTitle2ParamIllegal();
// cluster not exist
checkExtensionFieldsAndGenerateTitle2ClusterNotExist();
// topic already exist
checkExtensionFieldsAndGenerateTitle2TopicAlreadyExist();
// success
checkExtensionFieldsAndGenerateTitle2Success();
}
private void checkExtensionFieldsAndGenerateTitle2ParamIllegal() {
Result<String> result = applyTopicOrder.checkExtensionFieldsAndGenerateTitle("{}");
Assert.assertEquals(result.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2ClusterNotExist() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(null);
Result<String> result = applyTopicOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.CLUSTER_NOT_EXIST.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2TopicAlreadyExist() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Result<String> result = applyTopicOrder.checkExtensionFieldsAndGenerateTitle(TOPIC_EXIST_EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.TOPIC_ALREADY_EXIST.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2Success() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Result<String> result = applyTopicOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test(description = "测试处理具体的订单信息")
public void handleOrderDetailTest() {
// paramIllegal
handleOrderDetail2ParamIllegalTest();
// app not exist
handleOrderDetail2AppNotExistTest();
// cluster not exist
handleOrderDetail2ClusterNotExistTest();
// success
handleOrderDetail2SuccessTest();
}
private void handleOrderDetail2ParamIllegalTest() {
OrderDO orderDO = getOrderDO();
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
orderHandleBaseDTO.setDetail("{}");
ResultStatus resultStatus = applyTopicOrder.handleOrderDetail(orderDO, orderHandleBaseDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
}
private void handleOrderDetail2AppNotExistTest() {
OrderDO orderDO = getOrderDO();
orderDO.setExtensions("{}");
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
ResultStatus resultStatus = applyTopicOrder.handleOrderDetail(orderDO, orderHandleBaseDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.APP_NOT_EXIST.getCode());
}
private void handleOrderDetail2ClusterNotExistTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(null);
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(new AppDO());
OrderDO orderDO = getOrderDO();
orderDO.setExtensions("{\"appId\":\"dkm_admin\"}");
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
ResultStatus resultStatus = applyTopicOrder.handleOrderDetail(orderDO, orderHandleBaseDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.CLUSTER_NOT_EXIST.getCode());
orderDO.setExtensions("{\"appId\":\"dkm_admin\", \"clusterId\":\"-1\"}");
ResultStatus resultStatus2 = applyTopicOrder.handleOrderDetail(orderDO, orderHandleBaseDTO, ADMIN);
Assert.assertEquals(resultStatus2.getCode(), ResultStatus.CLUSTER_NOT_EXIST.getCode());
}
private void handleOrderDetail2SuccessTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Mockito.when(clusterService.getById(Mockito.anyLong())).thenReturn(new ClusterDO());
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(new AppDO());
Mockito.when(adminService.createTopic(
Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(),
Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())
).thenReturn(ResultStatus.SUCCESS);
OrderDO orderDO = getOrderDO();
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
ResultStatus resultStatus = applyTopicOrder.handleOrderDetail(orderDO, orderHandleBaseDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test(description = "测试处理工单")
public void handleOrderTest() {
// not wait deal
handleOrder2OrderAlreadyHandledTest();
// check authority and no authority
handleOrder2WithoutAuthorityTest();
// refuse
handleOrder2RefuseTest();
}
private void handleOrder2OrderAlreadyHandledTest() {
OrderDO orderDO = getOrderDO();
orderDO.setStatus(OrderStatusEnum.PASSED.getCode());
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
ResultStatus resultStatus = applyTopicOrder.handleOrder(orderDO, orderHandleBaseDTO, ADMIN, true);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.ORDER_ALREADY_HANDLED.getCode());
}
private void handleOrder2WithoutAuthorityTest() {
OrderDO orderDO = getOrderDO();
orderDO.setStatus(OrderStatusEnum.WAIT_DEAL.getCode());
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
ResultStatus resultStatus = applyTopicOrder.handleOrder(
orderDO, orderHandleBaseDTO,
INVALID_USER_NAME, true
);
Assert.assertNotEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
}
private void handleOrder2RefuseTest() {
Mockito.when(orderService.updateOrderById(Mockito.any())).thenReturn(1);
OrderDO orderDO = getOrderDO();
orderDO.setStatus(OrderStatusEnum.WAIT_DEAL.getCode());
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
orderHandleBaseDTO.setStatus(OrderStatusEnum.REFUSED.getCode());
ResultStatus resultStatus = applyTopicOrder.handleOrder(
orderDO, orderHandleBaseDTO,
ADMIN, true
);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
}
}

View File

@@ -0,0 +1,208 @@
package com.xiaojukeji.kafka.manager.bpm.order;
import com.xiaojukeji.kafka.manager.bpm.common.entry.detail.OrderDetailDeleteAppDTO;
import com.xiaojukeji.kafka.manager.bpm.common.handle.OrderHandleBaseDTO;
import com.xiaojukeji.kafka.manager.bpm.config.BaseTest;
import com.xiaojukeji.kafka.manager.common.entity.Result;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.pojo.OrderDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.gateway.AppDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.gateway.AuthorityDO;
import com.xiaojukeji.kafka.manager.service.service.gateway.AppService;
import com.xiaojukeji.kafka.manager.service.service.gateway.AuthorityService;
import com.xiaojukeji.kafka.manager.service.service.gateway.TopicConnectionService;
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.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
/**
* @author xuguang
* @Date 2021/12/29
*/
public class DeleteAppOrderTest extends BaseTest {
private static final String EXTENSIONS = "{\"appId\":\"dkm_admin\"}";
private static final Long ORDER_ID = 1L;
@Value("${test.admin}")
private String ADMIN;
private static final Integer APPLY_TOPIC_TYPE = 0;
/**
* 工单状态, 0:待审批, 1:通过, 2:拒绝, 3:取消
*/
private static final Integer ORDER_PASSED_STATUS = 1;
@Autowired
@Qualifier("deleteAppOrder")
@InjectMocks
private AbstractOrder deleteAppOrder;
@Mock
private AppService appService;
@Mock
private AuthorityService authorityService;
@Mock
private TopicConnectionService connectionService;
@BeforeMethod
public void setup() {
MockitoAnnotations.initMocks(this);
}
private OrderDO getOrderDO() {
OrderDO orderDO = new OrderDO();
orderDO.setId(ORDER_ID);
orderDO.setType(APPLY_TOPIC_TYPE);
orderDO.setTitle("apply topic");
orderDO.setApplicant(ADMIN);
orderDO.setDescription("测试的OrderDO");
orderDO.setApprover(ADMIN);
orderDO.setGmtHandle(new Date());
orderDO.setGmtCreate(new Date());
orderDO.setExtensions(EXTENSIONS);
orderDO.setStatus(ORDER_PASSED_STATUS);
return orderDO;
}
private AppDO getAppDO() {
AppDO appDO = new AppDO();
appDO.setId(4L);
appDO.setAppId("moduleTestAppId");
appDO.setName("moduleTestApp");
appDO.setPassword("moduleTestApp");
appDO.setType(1);
appDO.setApplicant("admin");
appDO.setPrincipals("admin");
appDO.setDescription("moduleTestApp");
appDO.setCreateTime(new Date(1638786493173L));
appDO.setModifyTime(new Date(1638786493173L));
return appDO;
}
@Test
public void checkExtensionFieldsAndGenerateTitleTest() {
// paramIllegal
checkExtensionFieldsAndGenerateTitle2ParamIllegalTest();
// app not exist
checkExtensionFieldsAndGenerateTitle2AppNotExistTest();
// success
checkExtensionFieldsAndGenerateTitle2SuccessTest();
}
private void checkExtensionFieldsAndGenerateTitle2ParamIllegalTest() {
Result<String> result = deleteAppOrder.checkExtensionFieldsAndGenerateTitle("{}");
Assert.assertEquals(result.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2AppNotExistTest() {
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(null);
Result<String> result = deleteAppOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.APP_NOT_EXIST.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2SuccessTest() {
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(new AppDO());
Result<String> result = deleteAppOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test
public void handleOrderDetailTest() {
// appNotExist
handleOrderDetail2AppNotExistTest();
// app offline forbidden
handleOrderDetail2AppOfflineForbiddenTest();
// success
handleOrderDetail2SuccesssTest();
// failed
handleOrderDetail2FailedTest();
}
private void handleOrderDetail2AppNotExistTest() {
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(null);
OrderDO orderDO = getOrderDO();
ResultStatus resultStatus = deleteAppOrder.handleOrderDetail(
orderDO, new OrderHandleBaseDTO(), ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.APP_NOT_EXIST.getCode());
}
private void handleOrderDetail2AppOfflineForbiddenTest() {
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(new AppDO());
Mockito.when(authorityService.getAuthority(Mockito.any())).thenReturn(Arrays.asList(new AuthorityDO()));
OrderDO orderDO = getOrderDO();
ResultStatus resultStatus = deleteAppOrder.handleOrderDetail(
orderDO, new OrderHandleBaseDTO(), ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.APP_OFFLINE_FORBIDDEN.getCode());
}
private void handleOrderDetail2SuccesssTest() {
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(new AppDO());
Mockito.when(authorityService.getAuthority(Mockito.any())).thenReturn(Collections.emptyList());
Mockito.when(appService.deleteApp(Mockito.any(), Mockito.any())).thenReturn(1);
OrderDO orderDO = getOrderDO();
ResultStatus resultStatus = deleteAppOrder.handleOrderDetail(
orderDO, new OrderHandleBaseDTO(), ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
}
private void handleOrderDetail2FailedTest() {
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(new AppDO());
Mockito.when(authorityService.getAuthority(Mockito.any())).thenReturn(Collections.emptyList());
Mockito.when(appService.deleteApp(Mockito.any(), Mockito.any())).thenReturn(-1);
OrderDO orderDO = getOrderDO();
ResultStatus resultStatus = deleteAppOrder.handleOrderDetail(
orderDO, new OrderHandleBaseDTO(), ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.OPERATION_FAILED.getCode());
}
@Test
public void getOrderExtensionDetailDataTest() {
// app is null
getOrderExtensionDetailData2AppDOIsNullTest();
// success
getOrderExtensionDetailData2SuccessTest();
}
private void getOrderExtensionDetailData2AppDOIsNullTest() {
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(null);
OrderDetailDeleteAppDTO data = (OrderDetailDeleteAppDTO) deleteAppOrder.getOrderExtensionDetailData(EXTENSIONS);
Assert.assertNotNull(data);
Assert.assertNull(data.getName());
}
private void getOrderExtensionDetailData2SuccessTest() {
AppDO appDO = getAppDO();
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(appDO);
Mockito.when(connectionService.getByAppId(
Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(Collections.emptyList());
OrderDetailDeleteAppDTO data = (OrderDetailDeleteAppDTO) deleteAppOrder.getOrderExtensionDetailData(EXTENSIONS);
Assert.assertNotNull(data);
Assert.assertNotNull(data.getName());
Assert.assertTrue(data.getConnectionList().isEmpty());
}
}

View File

@@ -0,0 +1,190 @@
package com.xiaojukeji.kafka.manager.bpm.order;
import com.xiaojukeji.kafka.manager.bpm.common.entry.detail.OrderDetailDeleteAuthorityDTO;
import com.xiaojukeji.kafka.manager.bpm.common.handle.OrderHandleBaseDTO;
import com.xiaojukeji.kafka.manager.bpm.config.BaseTest;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.pojo.OrderDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.gateway.AppDO;
import com.xiaojukeji.kafka.manager.common.entity.vo.normal.cluster.ClusterNameDTO;
import com.xiaojukeji.kafka.manager.service.cache.LogicalClusterMetadataManager;
import com.xiaojukeji.kafka.manager.service.service.ClusterService;
import com.xiaojukeji.kafka.manager.service.service.gateway.AppService;
import com.xiaojukeji.kafka.manager.service.service.gateway.AuthorityService;
import com.xiaojukeji.kafka.manager.service.service.gateway.TopicConnectionService;
import org.junit.Assert;
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.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.Date;
/**
* @author xuguang
* @Date 2021/12/29
*/
public class DeleteAuthorityOrderTest extends BaseTest {
private final static String EXTENSIONS = "{\"clusterId\":7,\"topicName\":\"moduleTest\",\"appId\":\"dkm_admin\",\"access\":\"3\"}";
@Value("${test.phyCluster.id}")
private final static Long REAL_CLUSTER_ID_IN_MYSQL = 1L;
private static final Long ORDER_ID = 1L;
@Value("${test.admin}")
private String ADMIN;
private static final Integer APPLY_TOPIC_TYPE = 0;
@Value("${test.app.id}")
private String APP_ID;
private static final String INVALIDE_USER = "xxxx";
/**
* 工单状态, 0:待审批, 1:通过, 2:拒绝, 3:取消
*/
private static final Integer ORDER_PASSED_STATUS = 1;
@Autowired
@Qualifier("deleteAuthorityOrder")
@InjectMocks
private AbstractOrder deleteAuthorityOrder;
@Mock
private LogicalClusterMetadataManager logicalClusterMetadataManager;
@Mock
private TopicConnectionService connectionService;
@Mock
private AuthorityService authorityService;
@Mock
private AppService appService;
@Mock
private ClusterService clusterService;
private OrderDO getOrderDO() {
OrderDO orderDO = new OrderDO();
orderDO.setId(ORDER_ID);
orderDO.setType(APPLY_TOPIC_TYPE);
orderDO.setTitle("apply topic");
orderDO.setApplicant(ADMIN);
orderDO.setDescription("测试的OrderDO");
orderDO.setApprover(ADMIN);
orderDO.setGmtHandle(new Date());
orderDO.setGmtCreate(new Date());
orderDO.setExtensions(EXTENSIONS);
orderDO.setStatus(ORDER_PASSED_STATUS);
return orderDO;
}
private AppDO getAppDO() {
AppDO appDO = new AppDO();
appDO.setId(4L);
appDO.setAppId(APP_ID);
appDO.setName("moduleTestApp");
appDO.setPassword("moduleTestApp");
appDO.setType(1);
appDO.setApplicant(ADMIN);
appDO.setPrincipals(ADMIN);
appDO.setDescription("moduleTestApp");
appDO.setCreateTime(new Date());
appDO.setModifyTime(new Date());
return appDO;
}
private ClusterNameDTO getClusterNameDTO() {
ClusterNameDTO clusterNameDTO = new ClusterNameDTO();
clusterNameDTO.setLogicalClusterId(REAL_CLUSTER_ID_IN_MYSQL);
clusterNameDTO.setPhysicalClusterName("");
clusterNameDTO.setLogicalClusterId(REAL_CLUSTER_ID_IN_MYSQL);
clusterNameDTO.setLogicalClusterName("");
return clusterNameDTO;
}
@BeforeMethod
public void setup() {
MockitoAnnotations.initMocks(this);
}
@Test
public void handleOrderDetailTest() {
// cluster not exist
handleOrderDetail2ClusterNotExistTest();
// OperationForbidden
handleOrderDetail2OperationForbiddenTest();
// success
handleOrderDetail2SuccessTest();
}
private void handleOrderDetail2ClusterNotExistTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(Mockito.any())).thenReturn(null);
OrderDO orderDO = getOrderDO();
ResultStatus resultStatus = deleteAuthorityOrder.handleOrderDetail(orderDO, new OrderHandleBaseDTO(), ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.CLUSTER_NOT_EXIST.getCode());
}
private void handleOrderDetail2OperationForbiddenTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(Mockito.any()))
.thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Mockito.when(connectionService.isExistConnection(
Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(true);
OrderDO orderDO = getOrderDO();
ResultStatus resultStatus = deleteAuthorityOrder.handleOrderDetail(orderDO, new OrderHandleBaseDTO(), ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.OPERATION_FORBIDDEN.getCode());
}
private void handleOrderDetail2SuccessTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(Mockito.any()))
.thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Mockito.when(connectionService.isExistConnection(
Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(false);
Mockito.when(authorityService.deleteSpecifiedAccess(
Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any()))
.thenReturn(ResultStatus.SUCCESS);
OrderDO orderDO = getOrderDO();
ResultStatus resultStatus = deleteAuthorityOrder.handleOrderDetail(orderDO, new OrderHandleBaseDTO(), ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test
public void getOrderExtensionDetailDataTest() {
// app is null
getOrderExtensionDetailData2AppIsNullTest();
// app is not null
getOrderExtensionDetailData2AppNotNullTest();
}
private void getOrderExtensionDetailData2AppIsNullTest() {
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(null);
ClusterNameDTO clusterNameDTO = getClusterNameDTO();
Mockito.when(clusterService.getClusterName(Mockito.any())).thenReturn(clusterNameDTO);
OrderDetailDeleteAuthorityDTO data = (OrderDetailDeleteAuthorityDTO) deleteAuthorityOrder.getOrderExtensionDetailData(EXTENSIONS);
org.testng.Assert.assertNotNull(data);
org.testng.Assert.assertNull(data.getAppName());
}
private void getOrderExtensionDetailData2AppNotNullTest() {
AppDO appDO = getAppDO();
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(appDO);
ClusterNameDTO clusterNameDTO = getClusterNameDTO();
Mockito.when(clusterService.getClusterName(Mockito.any())).thenReturn(clusterNameDTO);
OrderDetailDeleteAuthorityDTO data = (OrderDetailDeleteAuthorityDTO) deleteAuthorityOrder.getOrderExtensionDetailData(EXTENSIONS);
org.testng.Assert.assertNotNull(data);
org.testng.Assert.assertEquals(data.getAppName(), appDO.getName());
}
}

View File

@@ -0,0 +1,135 @@
package com.xiaojukeji.kafka.manager.bpm.order;
import com.xiaojukeji.kafka.manager.bpm.common.entry.detail.OrderDetailDeleteClusterDTO;
import com.xiaojukeji.kafka.manager.bpm.common.handle.OrderHandleBaseDTO;
import com.xiaojukeji.kafka.manager.bpm.config.BaseTest;
import com.xiaojukeji.kafka.manager.common.entity.Result;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.pojo.OrderDO;
import com.xiaojukeji.kafka.manager.common.entity.vo.normal.cluster.ClusterNameDTO;
import com.xiaojukeji.kafka.manager.service.cache.LogicalClusterMetadataManager;
import com.xiaojukeji.kafka.manager.service.service.ClusterService;
import com.xiaojukeji.kafka.manager.service.service.gateway.AppService;
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.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
/**
* @author xuguang
* @Date 2021/12/30
*/
public class DeleteClusterOrderTest extends BaseTest {
@Value("${test.app.id}")
private String APP_ID;
private final static String IDC = "国内";
private final static String EXTENSIONS = "{\"clusterId\":\"7\"}";
private final static String INVALID_IDC = "xxx";
@Value("${test.admin}")
private String ADMIN;
private final static Long REAL_CLUSTER_ID_IN_MYSQL = 1L;
@Autowired
@Qualifier("deleteClusterOrder")
@InjectMocks
private AbstractOrder deleteClusterOrder;
@Mock
private AppService appService;
@Mock
private LogicalClusterMetadataManager logicalClusterMetadataManager;
@Mock
private ClusterService clusterService;
@BeforeMethod
public void setup() {
MockitoAnnotations.initMocks(this);
}
@Test
public void checkExtensionFieldsAndGenerateTitleTest() {
// paramIllegal
checkExtensionFieldsAndGenerateTitle2ParamIllegalTest();
// cluster not exist
checkExtensionFieldsAndGenerateTitle2ClusterNotExistTest();
// success
checkExtensionFieldsAndGenerateTitle2SuccessTest();
}
private void checkExtensionFieldsAndGenerateTitle2ParamIllegalTest() {
Result<String> result = deleteClusterOrder.checkExtensionFieldsAndGenerateTitle("{}");
Assert.assertEquals(result.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2ClusterNotExistTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(Mockito.any())).thenReturn(null);
Result<String> result = deleteClusterOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.CLUSTER_NOT_EXIST.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2SuccessTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Result<String> result = deleteClusterOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test
public void handleOrderDetailTest() {
// operation forbidden
handleOrderDetail2OperationForbiddenTest();
// success
handleOrderDetail2SuccessTest();
}
private void handleOrderDetail2OperationForbiddenTest() {
Set<String> topics = new HashSet<>();
topics.add("");
Mockito.when(logicalClusterMetadataManager.getTopicNameSet(Mockito.any())).thenReturn(topics);
OrderDO orderDO = new OrderDO();
orderDO.setExtensions(EXTENSIONS);
ResultStatus resultStatus = deleteClusterOrder.handleOrderDetail(orderDO, new OrderHandleBaseDTO(), ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.OPERATION_FORBIDDEN.getCode());
}
private void handleOrderDetail2SuccessTest() {
Mockito.when(logicalClusterMetadataManager.getTopicNameSet(Mockito.any())).thenReturn(Collections.emptySet());
OrderDO orderDO = new OrderDO();
orderDO.setExtensions(EXTENSIONS);
ResultStatus resultStatus = deleteClusterOrder.handleOrderDetail(orderDO, new OrderHandleBaseDTO(), ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test
public void getOrderExtensionDetailDataTest() {
ClusterNameDTO clusterNameDTO = new ClusterNameDTO();
clusterNameDTO.setPhysicalClusterId(REAL_CLUSTER_ID_IN_MYSQL);
clusterNameDTO.setLogicalClusterId(REAL_CLUSTER_ID_IN_MYSQL);
clusterNameDTO.setLogicalClusterName("");
clusterNameDTO.setPhysicalClusterName("");
Mockito.when(clusterService.getClusterName(Mockito.any())).thenReturn(clusterNameDTO);
Mockito.when(logicalClusterMetadataManager.getTopicNameSet(Mockito.any())).thenReturn(Collections.emptySet());
OrderDetailDeleteClusterDTO data = (OrderDetailDeleteClusterDTO) deleteClusterOrder.getOrderExtensionDetailData(EXTENSIONS);
Assert.assertEquals(data.getLogicalClusterId(), REAL_CLUSTER_ID_IN_MYSQL);
Assert.assertEquals(data.getPhysicalClusterId(), REAL_CLUSTER_ID_IN_MYSQL);
}
}

View File

@@ -0,0 +1,273 @@
package com.xiaojukeji.kafka.manager.bpm.order;
import com.xiaojukeji.kafka.manager.bpm.common.entry.detail.AbstractOrderDetailData;
import com.xiaojukeji.kafka.manager.bpm.common.entry.detail.OrderDetailDeleteTopicDTO;
import com.xiaojukeji.kafka.manager.bpm.common.handle.OrderHandleBaseDTO;
import com.xiaojukeji.kafka.manager.bpm.config.BaseTest;
import com.xiaojukeji.kafka.manager.common.entity.Result;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.pojo.OrderDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.TopicDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.gateway.AppDO;
import com.xiaojukeji.kafka.manager.common.entity.vo.normal.cluster.ClusterNameDTO;
import com.xiaojukeji.kafka.manager.service.cache.LogicalClusterMetadataManager;
import com.xiaojukeji.kafka.manager.service.service.AdminService;
import com.xiaojukeji.kafka.manager.service.service.ClusterService;
import com.xiaojukeji.kafka.manager.service.service.TopicManagerService;
import com.xiaojukeji.kafka.manager.service.service.gateway.AppService;
import com.xiaojukeji.kafka.manager.service.service.gateway.TopicConnectionService;
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.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.Collections;
import java.util.Date;
/**
* @author xuguang
* @Date 2021/12/28
*/
public class DeleteTopicOrderTest extends BaseTest {
@Value("${test.admin}")
private String ADMIN;
private static final String INVALID_USER_NAME = "xxxxx";
private static final Integer INVALID_ORDER_TYPE = -1;
private static final Integer APPLY_TOPIC_TYPE = 0;
private static final Long ORDER_ID = 1L;
private static final Long INVALID_ORDER_ID = -1L;
private static final String EXTENSIONS = "{\"clusterId\":7,\"topicName\":\"moduleTest\",\"appId\":\"dkm_admin\",\"peakBytesIn\":104857600000}";
private static final String TOPIC_NOT_EXIST_EXTENSIONS = "{\"clusterId\":7,\"topicName\":\"xxx\",\"appId\":\"dkm_admin\",\"peakBytesIn\":104857600000}";
private static final String APPROVE_ORDER_APPLY_DETAIL = "{\"brokerIdList\":[3],\"partitionNum\":1,\"replicaNum\":1,\"retentionTime\":12}";
@Value("${test.phyCluster.id}")
private Long REAL_CLUSTER_ID_IN_MYSQL;
private static final Long INVALID_CLUSTER_ID = -1L;
@Value("${test.app.id}")
private String APP_ID;
/**
* 工单状态, 0:待审批, 1:通过, 2:拒绝, 3:取消
*/
private static final Integer ORDER_PASSED_STATUS = 1;
@Autowired
@Qualifier("deleteTopicOrder")
@InjectMocks
private AbstractTopicOrder deleteTopicOrder;
@Mock
private LogicalClusterMetadataManager logicalClusterMetadataManager;
@Mock
private TopicConnectionService connectionService;
@Mock
private AdminService adminService;
@Mock
private ClusterService clusterService;
@Mock
private TopicManagerService topicManagerService;
@Mock
private AppService appService;
@BeforeMethod
public void setup() {
MockitoAnnotations.initMocks(this);
}
private OrderDO getOrderDO() {
OrderDO orderDO = new OrderDO();
orderDO.setId(ORDER_ID);
orderDO.setType(APPLY_TOPIC_TYPE);
orderDO.setTitle("apply topic");
orderDO.setApplicant(ADMIN);
orderDO.setDescription("测试的OrderDO");
orderDO.setApprover(ADMIN);
orderDO.setGmtHandle(new Date());
orderDO.setGmtCreate(new Date());
orderDO.setExtensions(EXTENSIONS);
orderDO.setStatus(ORDER_PASSED_STATUS);
return orderDO;
}
private OrderHandleBaseDTO getOrderHandleBaseDTO() {
OrderHandleBaseDTO orderHandleBaseDTO = new OrderHandleBaseDTO();
orderHandleBaseDTO.setId(ORDER_ID);
orderHandleBaseDTO.setStatus(ORDER_PASSED_STATUS);
orderHandleBaseDTO.setDetail(APPROVE_ORDER_APPLY_DETAIL);
return orderHandleBaseDTO;
}
private ClusterNameDTO getClusterNameDTO() {
ClusterNameDTO clusterNameDTO = new ClusterNameDTO();
clusterNameDTO.setPhysicalClusterId(REAL_CLUSTER_ID_IN_MYSQL);
clusterNameDTO.setPhysicalClusterName("physicalClusterName");
clusterNameDTO.setLogicalClusterId(REAL_CLUSTER_ID_IN_MYSQL);
clusterNameDTO.setLogicalClusterName("logicalClusterName");
return clusterNameDTO;
}
@Test(description = "测试检查扩展字段并生成工单的Title")
public void checkExtensionFieldsAndGenerateTitle() {
// paramIllegal
checkExtensionFieldsAndGenerateTitle2ParamIllegal();
// cluster not exist
checkExtensionFieldsAndGenerateTitle2ClusterNotExist();
// topic already exist
checkExtensionFieldsAndGenerateTitle2TopicNotExist();
// success
checkExtensionFieldsAndGenerateTitle2Success();
}
private void checkExtensionFieldsAndGenerateTitle2ParamIllegal() {
Result<String> result = deleteTopicOrder.checkExtensionFieldsAndGenerateTitle("{}");
Assert.assertEquals(result.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2ClusterNotExist() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong())).thenReturn(null);
Result<String> result = deleteTopicOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.CLUSTER_NOT_EXIST.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2TopicNotExist() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Result<String> result = deleteTopicOrder.checkExtensionFieldsAndGenerateTitle(TOPIC_NOT_EXIST_EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.TOPIC_NOT_EXIST.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2Success() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Result<String> result = deleteTopicOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test(description = "测试处理删除topic工单")
public void handleOrderDetail() {
// cluster not exist
handleOrderDetail2ClusterNotExistTest();
// topic not exist
handleOrderDetail2TopicNotExistTest();
// operation forbidden, 因为最近topic还有生产和消费操作
handleOrderDetail2OperationForbiddenTest();
// delete success
handleOrderDetail2DeleteSuccessTest();
// delete not success
handleOrderDetail2DeleteNotSuccessTest();
}
private void handleOrderDetail2ClusterNotExistTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(Mockito.anyLong())).thenReturn(null);
OrderDO orderDO = getOrderDO();
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
ResultStatus resultStatus = deleteTopicOrder.handleOrderDetail(orderDO, orderHandleBaseDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.CLUSTER_NOT_EXIST.getCode());
}
private void handleOrderDetail2TopicNotExistTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(Mockito.anyLong())).thenReturn(INVALID_CLUSTER_ID);
OrderDO orderDO = getOrderDO();
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
ResultStatus resultStatus = deleteTopicOrder.handleOrderDetail(orderDO, orderHandleBaseDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.TOPIC_NOT_EXIST.getCode());
}
private void handleOrderDetail2OperationForbiddenTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(Mockito.anyLong())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Mockito.when(connectionService.isExistConnection(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(true);
OrderDO orderDO = getOrderDO();
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
ResultStatus resultStatus = deleteTopicOrder.handleOrderDetail(orderDO, orderHandleBaseDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.OPERATION_FORBIDDEN.getCode());
}
private void handleOrderDetail2DeleteSuccessTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(Mockito.anyLong())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Mockito.when(connectionService.isExistConnection(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(false);
Mockito.when(adminService.deleteTopic(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(ResultStatus.SUCCESS);
OrderDO orderDO = getOrderDO();
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
ResultStatus resultStatus = deleteTopicOrder.handleOrderDetail(orderDO, orderHandleBaseDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
}
private void handleOrderDetail2DeleteNotSuccessTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(Mockito.anyLong())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Mockito.when(connectionService.isExistConnection(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(false);
Mockito.when(adminService.deleteTopic(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(ResultStatus.FAIL);
OrderDO orderDO = getOrderDO();
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
ResultStatus resultStatus = deleteTopicOrder.handleOrderDetail(orderDO, orderHandleBaseDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.FAIL.getCode());
}
@Test(description = "")
public void getOrderExtensionDetailDataTest() {
// topicDO不存在
getOrderExtensionDetailData2TopicNotExistTest();
// 获取成功
getOrderExtensionDetailData2SuccessTest();
}
private void getOrderExtensionDetailData2TopicNotExistTest() {
ClusterNameDTO clusterNameDTO = getClusterNameDTO();
Mockito.when(clusterService.getClusterName(Mockito.any())).thenReturn(clusterNameDTO);
Mockito.when(connectionService.getByTopicName(
Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(Collections.emptyList());
Mockito.when(topicManagerService.getByTopicName(Mockito.any(), Mockito.any())).thenReturn(null);
OrderDetailDeleteTopicDTO data = (OrderDetailDeleteTopicDTO)deleteTopicOrder.getOrderExtensionDetailData(EXTENSIONS);
Assert.assertNotNull(data);
Assert.assertNull(data.getAppId());
}
private void getOrderExtensionDetailData2SuccessTest() {
ClusterNameDTO clusterNameDTO = getClusterNameDTO();
Mockito.when(clusterService.getClusterName(Mockito.any())).thenReturn(clusterNameDTO);
Mockito.when(connectionService.getByTopicName(
Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(Collections.emptyList());
TopicDO topicDO = new TopicDO();
topicDO.setAppId(APP_ID);
Mockito.when(topicManagerService.getByTopicName(Mockito.any(), Mockito.any())).thenReturn(topicDO);
AppDO appDO = new AppDO();
appDO.setAppId(APP_ID);
appDO.setName("");
appDO.setPrincipals("");
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(appDO);
OrderDetailDeleteTopicDTO data = (OrderDetailDeleteTopicDTO)deleteTopicOrder.getOrderExtensionDetailData(EXTENSIONS);
Assert.assertNotNull(data);
Assert.assertNotNull(data.getAppId());
Assert.assertNotNull(data.getAppName());
}
}

View File

@@ -0,0 +1,112 @@
package com.xiaojukeji.kafka.manager.bpm.order;
import com.xiaojukeji.kafka.manager.bpm.common.entry.detail.OrderDetailModifyClusterDTO;
import com.xiaojukeji.kafka.manager.bpm.common.handle.OrderHandleBaseDTO;
import com.xiaojukeji.kafka.manager.bpm.config.BaseTest;
import com.xiaojukeji.kafka.manager.common.entity.Result;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.pojo.OrderDO;
import com.xiaojukeji.kafka.manager.common.entity.vo.normal.cluster.ClusterNameDTO;
import com.xiaojukeji.kafka.manager.service.cache.LogicalClusterMetadataManager;
import com.xiaojukeji.kafka.manager.service.service.ClusterService;
import org.junit.Assert;
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.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
/**
* @author xuguang
* @Date 2021/12/30
*/
public class ModifyClusterOrderTest extends BaseTest {
@Value("${test.admin}")
private String ADMIN;
private static final String INVALID_USER_NAME = "xxxxx";
private static final Integer INVALID_ORDER_TYPE = -1;
private static final Integer APPLY_TOPIC_TYPE = 0;
private static final Long ORDER_ID = 1L;
private static final Long INVALID_ORDER_ID = -1L;
private static final String EXTENSIONS = "{\"clusterId\":7}";
@Value("${test.phyCluster.id}")
private Long REAL_CLUSTER_ID_IN_MYSQL;
@Autowired
@Qualifier("modifyClusterOrder")
@InjectMocks
private AbstractOrder modifyClusterOrder;
@Mock
private LogicalClusterMetadataManager logicalClusterMetadataManager;
@Mock
private ClusterService clusterService;
@BeforeMethod
public void setup() {
MockitoAnnotations.initMocks(this);
}
@Test
public void checkExtensionFieldsAndGenerateTitleTest() {
// paramIllegal
checkExtensionFieldsAndGenerateTitle2ParamIllegalTest();
// cluster not exist
checkExtensionFieldsAndGenerateTitle2ClusterNotExistTest();
// success
checkExtensionFieldsAndGenerateTitle2ZSuccessTest();
}
private void checkExtensionFieldsAndGenerateTitle2ParamIllegalTest() {
Result<String> result = modifyClusterOrder.checkExtensionFieldsAndGenerateTitle("{}");
Assert.assertEquals(result.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2ClusterNotExistTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(Mockito.anyLong())).thenReturn(null);
Result<String> result = modifyClusterOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.CLUSTER_NOT_EXIST.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2ZSuccessTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Result<String> result = modifyClusterOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test
public void handleOrderDetailTest() {
ResultStatus resultStatus = modifyClusterOrder.handleOrderDetail(new OrderDO(), new OrderHandleBaseDTO(), ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test
public void getOrderExtensionDetailDataTest() {
ClusterNameDTO clusterNameDTO = new ClusterNameDTO();
clusterNameDTO.setPhysicalClusterId(REAL_CLUSTER_ID_IN_MYSQL);
clusterNameDTO.setPhysicalClusterName("");
clusterNameDTO.setLogicalClusterId(REAL_CLUSTER_ID_IN_MYSQL);
clusterNameDTO.setLogicalClusterName("");
Mockito.when(clusterService.getClusterName(Mockito.any())).thenReturn(clusterNameDTO);
OrderDetailModifyClusterDTO data = (OrderDetailModifyClusterDTO) modifyClusterOrder.getOrderExtensionDetailData(EXTENSIONS);
Assert.assertEquals(data.getPhysicalClusterId(), REAL_CLUSTER_ID_IN_MYSQL);
Assert.assertEquals(data.getLogicalClusterId(), REAL_CLUSTER_ID_IN_MYSQL);
}
}

View File

@@ -0,0 +1,160 @@
package com.xiaojukeji.kafka.manager.bpm.order;
import com.xiaojukeji.kafka.manager.bpm.common.entry.detail.OrderDetailGatewayConfigModifyData;
import com.xiaojukeji.kafka.manager.bpm.common.handle.OrderHandleBaseDTO;
import com.xiaojukeji.kafka.manager.bpm.config.BaseTest;
import com.xiaojukeji.kafka.manager.common.entity.Result;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.pojo.OrderDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.gateway.GatewayConfigDO;
import com.xiaojukeji.kafka.manager.service.service.gateway.GatewayConfigService;
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.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
/**
* @author xuguang
* @Date 2021/12/31
*/
public class ModifyGatewayConfigOrderTest extends BaseTest {
@Value("${test.admin}")
private String ADMIN;
private static final String INVALID_USER_NAME = "xxxxx";
private static final Integer INVALID_ORDER_TYPE = -1;
private static final Integer APPLY_TOPIC_TYPE = 0;
private static final Long ORDER_ID = 1L;
private static final Long INVALID_ORDER_ID = -1L;
private static final String GATEWAY_TYPE = "SD_CLUSTER_ID";
private static final String EXTENSIONS = "{\"id\":1,\"type\":\"SD_CLUSTER_ID\",\"name\":\"gateway\",\"value\":\"gateway\",\"description\":\"gateway\"}";
private static final String INVALIDE_TYPE_EXTENSIONS = "{\"id\":1,\"type\":\"xxxx\",\"name\":\"gateway\",\"value\":\"gateway\",\"description\":\"gateway\"}";
private static final String APPROVE_ORDER_APPLY_DETAIL = "{\"brokerIdList\":[3],\"partitionNum\":1,\"replicaNum\":1,\"retentionTime\":12}";
private static final Long REAL_CLUSTER_ID_IN_MYSQL = 1L;
private static final Long INVALID_CLUSTER_ID = -1L;
@Value("${test.app.id}")
private String APP_ID;
/**
* 工单状态, 0:待审批, 1:通过, 2:拒绝, 3:取消
*/
private static final Integer ORDER_PASSED_STATUS = 1;
@Autowired
@Qualifier("modifyGatewayConfigOrder")
@InjectMocks
private AbstractOrder modifyGatewayConfigOrder;
@Mock
private GatewayConfigService gatewayConfigService;
@BeforeMethod
public void setup() {
MockitoAnnotations.initMocks(this);
}
@Test
public void checkExtensionFieldsAndGenerateTitleTest() {
// paramIllegal
checkExtensionFieldsAndGenerateTitle2ParamIllegal1Test();
checkExtensionFieldsAndGenerateTitle2ParamIllegal2Test();
checkExtensionFieldsAndGenerateTitle2ParamIllegal3Test();
checkExtensionFieldsAndGenerateTitle2ParamIllegal4Test();
// success
checkExtensionFieldsAndGenerateTitle2SuccessTest();
}
private void checkExtensionFieldsAndGenerateTitle2ParamIllegal1Test() {
Result<String> result = modifyGatewayConfigOrder.checkExtensionFieldsAndGenerateTitle("{");
Assert.assertEquals(result.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2ParamIllegal2Test() {
Result<String> result = modifyGatewayConfigOrder.checkExtensionFieldsAndGenerateTitle("{}");
Assert.assertEquals(result.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2ParamIllegal3Test() {
Mockito.when(gatewayConfigService.getById(Mockito.anyLong())).thenReturn(null);
Result<String> result = modifyGatewayConfigOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2ParamIllegal4Test() {
Mockito.when(gatewayConfigService.getById(Mockito.anyLong())).thenReturn(new GatewayConfigDO());
Result<String> result = modifyGatewayConfigOrder.checkExtensionFieldsAndGenerateTitle(INVALIDE_TYPE_EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2SuccessTest() {
Mockito.when(gatewayConfigService.getById(Mockito.anyLong())).thenReturn(new GatewayConfigDO());
Result<String> result = modifyGatewayConfigOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test
public void handleOrderDetailTest() {
ResultStatus resultStatus = modifyGatewayConfigOrder.handleOrderDetail(new OrderDO(), new OrderHandleBaseDTO(), ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test
public void getOrderExtensionDetailDataTest() {
// result is null
getOrderExtensionDetailData2nullTest();
// old config is null
getOrderExtensionDetailData2OldConfigNullTest();
// old config is not null
getOrderExtensionDetailData2OldConfigNotNullTest();
}
private void getOrderExtensionDetailData2nullTest() {
OrderDetailGatewayConfigModifyData data = (OrderDetailGatewayConfigModifyData)
modifyGatewayConfigOrder.getOrderExtensionDetailData("{");
Assert.assertNull(data);
}
private void getOrderExtensionDetailData2OldConfigNullTest() {
Mockito.when(gatewayConfigService.getById(Mockito.anyLong())).thenReturn(null);
OrderDetailGatewayConfigModifyData data = (OrderDetailGatewayConfigModifyData)
modifyGatewayConfigOrder.getOrderExtensionDetailData(EXTENSIONS);
Assert.assertNotNull(data);
Assert.assertEquals(data.getNewGatewayConfig().getType(), GATEWAY_TYPE);
}
private void getOrderExtensionDetailData2OldConfigNotNullTest() {
GatewayConfigDO gatewayConfigDO = new GatewayConfigDO();
String invalid_value = "xxx";
gatewayConfigDO.setType(invalid_value);
gatewayConfigDO.setId(1L);
gatewayConfigDO.setValue(invalid_value);
gatewayConfigDO.setName(invalid_value);
gatewayConfigDO.setVersion(1L);
Mockito.when(gatewayConfigService.getById(Mockito.anyLong())).thenReturn(gatewayConfigDO);
OrderDetailGatewayConfigModifyData data = (OrderDetailGatewayConfigModifyData)
modifyGatewayConfigOrder.getOrderExtensionDetailData(EXTENSIONS);
Assert.assertNotNull(data);
Assert.assertEquals(data.getNewGatewayConfig().getType(), invalid_value);
}
}

View File

@@ -0,0 +1,325 @@
package com.xiaojukeji.kafka.manager.bpm.order;
import com.xiaojukeji.kafka.manager.bpm.common.entry.detail.OrderDetailDeleteTopicDTO;
import com.xiaojukeji.kafka.manager.bpm.common.handle.OrderHandleBaseDTO;
import com.xiaojukeji.kafka.manager.bpm.config.BaseTest;
import com.xiaojukeji.kafka.manager.common.entity.Result;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.pojo.ClusterDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.OrderDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.TopicDO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.gateway.AppDO;
import com.xiaojukeji.kafka.manager.common.entity.vo.normal.cluster.ClusterNameDTO;
import com.xiaojukeji.kafka.manager.service.cache.LogicalClusterMetadataManager;
import com.xiaojukeji.kafka.manager.service.service.AdminService;
import com.xiaojukeji.kafka.manager.service.service.ClusterService;
import com.xiaojukeji.kafka.manager.service.service.TopicManagerService;
import com.xiaojukeji.kafka.manager.service.service.gateway.AppService;
import com.xiaojukeji.kafka.manager.service.service.gateway.TopicConnectionService;
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.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.Collections;
import java.util.Date;
/**
* @author xuguang
* @Date 2021/12/31
*/
public class ThirdPartDeleteTopicOrderTest extends BaseTest {
@Value("${test.admin}")
private String ADMIN;
private static final String INVALID_USER_NAME = "xxxxx";
private static final Integer INVALID_ORDER_TYPE = -1;
private static final Integer APPLY_TOPIC_TYPE = 0;
private static final Long ORDER_ID = 1L;
private static final Long INVALID_ORDER_ID = -1L;
private static final String EXTENSIONS = "{\"clusterId\":7,\"topicName\":\"moduleTest\",\"appId\":\"dkm_admin\",\"password\":\"123456\"}";
private static final String TOPIC_NOT_EXIST_EXTENSIONS = "{\"clusterId\":7,\"topicName\":\"xxxx\",\"appId\":\"dkm_admin\",\"password\":\"123456\"}";
private static final String APPROVE_ORDER_APPLY_DETAIL = "{\"brokerIdList\":[3],\"partitionNum\":1,\"replicaNum\":1,\"retentionTime\":12}";
@Value("${test.phyCluster.id}")
private Long REAL_CLUSTER_ID_IN_MYSQL;
private static final Long INVALID_CLUSTER_ID = -1L;
@Value("${test.app.id}")
private String APP_ID;
/**
* 工单状态, 0:待审批, 1:通过, 2:拒绝, 3:取消
*/
private static final Integer ORDER_PASSED_STATUS = 1;
@Autowired
@Qualifier("thirdPartDeleteTopicOrder")
@InjectMocks
private AbstractOrder thirdPartDeleteTopicOrder;
@Mock
private LogicalClusterMetadataManager logicalClusterMetadataManager;
@Mock
private TopicConnectionService connectionService;
@Mock
private AdminService adminService;
@Mock
private ClusterService clusterService;
@Mock
private TopicManagerService topicManagerService;
@Mock
private AppService appService;
@BeforeMethod
public void setup() {
MockitoAnnotations.initMocks(this);
}
private OrderDO getOrderDO() {
OrderDO orderDO = new OrderDO();
orderDO.setId(ORDER_ID);
orderDO.setType(APPLY_TOPIC_TYPE);
orderDO.setTitle("apply topic");
orderDO.setApplicant(ADMIN);
orderDO.setDescription("测试的OrderDO");
orderDO.setApprover(ADMIN);
orderDO.setGmtHandle(new Date());
orderDO.setGmtCreate(new Date());
orderDO.setExtensions(EXTENSIONS);
orderDO.setStatus(ORDER_PASSED_STATUS);
return orderDO;
}
private OrderHandleBaseDTO getOrderHandleBaseDTO() {
OrderHandleBaseDTO orderHandleBaseDTO = new OrderHandleBaseDTO();
orderHandleBaseDTO.setId(ORDER_ID);
orderHandleBaseDTO.setStatus(ORDER_PASSED_STATUS);
orderHandleBaseDTO.setDetail(APPROVE_ORDER_APPLY_DETAIL);
return orderHandleBaseDTO;
}
private ClusterNameDTO getClusterNameDTO() {
ClusterNameDTO clusterNameDTO = new ClusterNameDTO();
clusterNameDTO.setPhysicalClusterId(REAL_CLUSTER_ID_IN_MYSQL);
clusterNameDTO.setPhysicalClusterName("physicalClusterName");
clusterNameDTO.setLogicalClusterId(REAL_CLUSTER_ID_IN_MYSQL);
clusterNameDTO.setLogicalClusterName("logicalClusterName");
return clusterNameDTO;
}
@Test(description = "测试检查扩展字段并生成工单的Title")
public void checkExtensionFieldsAndGenerateTitle() {
// paramIllegal
checkExtensionFieldsAndGenerateTitle2ParamIllegal();
// cluster not exist
checkExtensionFieldsAndGenerateTitle2ClusterNotExist();
// topic not exist
checkExtensionFieldsAndGenerateTitle2TopicNotExist();
// app not exist
checkExtensionFieldsAndGenerateTitle2AppNotExist();
// user without authority
checkExtensionFieldsAndGenerateTitle2WithoutAuthority();
// success
checkExtensionFieldsAndGenerateTitle2Success();
}
private void checkExtensionFieldsAndGenerateTitle2ParamIllegal() {
Result<String> result = thirdPartDeleteTopicOrder.checkExtensionFieldsAndGenerateTitle("{}");
org.testng.Assert.assertEquals(result.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2ClusterNotExist() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(null);
Result<String> result = thirdPartDeleteTopicOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
org.testng.Assert.assertEquals(result.getCode(), ResultStatus.CLUSTER_NOT_EXIST.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2TopicNotExist() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Result<String> result = thirdPartDeleteTopicOrder.checkExtensionFieldsAndGenerateTitle(TOPIC_NOT_EXIST_EXTENSIONS);
org.testng.Assert.assertEquals(result.getCode(), ResultStatus.TOPIC_NOT_EXIST.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2AppNotExist() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(null);
Result<String> result = thirdPartDeleteTopicOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
org.testng.Assert.assertEquals(result.getCode(), ResultStatus.APP_NOT_EXIST.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2WithoutAuthority() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
AppDO appDO = new AppDO();
appDO.setPassword("xxx");
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(appDO);
Result<String> result = thirdPartDeleteTopicOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
org.testng.Assert.assertEquals(result.getCode(), ResultStatus.USER_WITHOUT_AUTHORITY.getCode());
}
private void checkExtensionFieldsAndGenerateTitle2Success() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(
Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
AppDO appDO = new AppDO();
appDO.setPassword("123456");
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(appDO);
Result<String> result = thirdPartDeleteTopicOrder.checkExtensionFieldsAndGenerateTitle(EXTENSIONS);
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test(description = "测试处理删除topic工单")
public void handleOrderDetail() {
// cluster not exist
handleOrderDetail2ClusterNotExistTest();
// topic not exist
handleOrderDetail2TopicNotExistTest();
// operation forbidden, 因为最近topic还有生产和消费操作
handleOrderDetail2OperationForbiddenTest();
// app not exist
handleOrderDetail2AppNotExistTest();
// user without authority
handleOrderDetail2UserWithoutAuthorityTest();
// delete success
handleOrderDetail2DeleteSuccessTest();
// delete not success
handleOrderDetail2DeleteNotSuccessTest();
}
private void handleOrderDetail2ClusterNotExistTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(Mockito.anyLong(), Mockito.any())).thenReturn(null);
OrderDO orderDO = getOrderDO();
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
ResultStatus resultStatus = thirdPartDeleteTopicOrder.handleOrderDetail(orderDO, orderHandleBaseDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.CLUSTER_NOT_EXIST.getCode());
}
private void handleOrderDetail2TopicNotExistTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(Mockito.anyLong(), Mockito.any())).thenReturn(INVALID_CLUSTER_ID);
OrderDO orderDO = getOrderDO();
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
ResultStatus resultStatus = thirdPartDeleteTopicOrder.handleOrderDetail(orderDO, orderHandleBaseDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.TOPIC_NOT_EXIST.getCode());
}
private void handleOrderDetail2OperationForbiddenTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Mockito.when(connectionService.isExistConnection(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(true);
OrderDO orderDO = getOrderDO();
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
ResultStatus resultStatus = thirdPartDeleteTopicOrder.handleOrderDetail(orderDO, orderHandleBaseDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.OPERATION_FORBIDDEN.getCode());
}
private void handleOrderDetail2AppNotExistTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Mockito.when(connectionService.isExistConnection(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(false);
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(null);
OrderDO orderDO = getOrderDO();
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
ResultStatus resultStatus = thirdPartDeleteTopicOrder.handleOrderDetail(orderDO, orderHandleBaseDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.APP_NOT_EXIST.getCode());
}
private void handleOrderDetail2UserWithoutAuthorityTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Mockito.when(connectionService.isExistConnection(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(false);
AppDO appDO = new AppDO();
appDO.setPassword("xxx");
appDO.setPrincipals(ADMIN);
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(appDO);
OrderDO orderDO = getOrderDO();
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
ResultStatus resultStatus = thirdPartDeleteTopicOrder.handleOrderDetail(orderDO, orderHandleBaseDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.USER_WITHOUT_AUTHORITY.getCode());
}
private void handleOrderDetail2DeleteSuccessTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Mockito.when(connectionService.isExistConnection(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(false);
Mockito.when(adminService.deleteTopic(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(ResultStatus.SUCCESS);
AppDO appDO = new AppDO();
appDO.setPassword("123456");
appDO.setPrincipals(ADMIN);
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(appDO);
OrderDO orderDO = getOrderDO();
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
ResultStatus resultStatus = thirdPartDeleteTopicOrder.handleOrderDetail(orderDO, orderHandleBaseDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
}
private void handleOrderDetail2DeleteNotSuccessTest() {
Mockito.when(logicalClusterMetadataManager.getPhysicalClusterId(Mockito.anyLong(), Mockito.any())).thenReturn(REAL_CLUSTER_ID_IN_MYSQL);
Mockito.when(connectionService.isExistConnection(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(false);
Mockito.when(adminService.deleteTopic(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(ResultStatus.FAIL);
AppDO appDO = new AppDO();
appDO.setPassword("123456");
appDO.setPrincipals(ADMIN);
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(appDO);
OrderDO orderDO = getOrderDO();
OrderHandleBaseDTO orderHandleBaseDTO = getOrderHandleBaseDTO();
ResultStatus resultStatus = thirdPartDeleteTopicOrder.handleOrderDetail(orderDO, orderHandleBaseDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.FAIL.getCode());
}
@Test(description = "")
public void getOrderExtensionDetailDataTest() {
// 获取成功
getOrderExtensionDetailData2SuccessTest();
}
private void getOrderExtensionDetailData2SuccessTest() {
ClusterDO clusterDO = new ClusterDO();
clusterDO.setId(REAL_CLUSTER_ID_IN_MYSQL);
clusterDO.setClusterName("");
Mockito.when(clusterService.getById(Mockito.any())).thenReturn(clusterDO);
Mockito.when(connectionService.getByTopicName(
Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(Collections.emptyList());
TopicDO topicDO = new TopicDO();
topicDO.setAppId(APP_ID);
Mockito.when(topicManagerService.getByTopicName(Mockito.any(), Mockito.any())).thenReturn(topicDO);
AppDO appDO = new AppDO();
appDO.setAppId(APP_ID);
appDO.setName("");
appDO.setPrincipals("");
Mockito.when(appService.getByAppId(Mockito.any())).thenReturn(appDO);
OrderDetailDeleteTopicDTO data = (OrderDetailDeleteTopicDTO)thirdPartDeleteTopicOrder.getOrderExtensionDetailData(EXTENSIONS);
Assert.assertNotNull(data);
Assert.assertNotNull(data.getAppId());
Assert.assertNotNull(data.getAppName());
}
}

View File

@@ -0,0 +1,127 @@
server:
port: 8080
tomcat:
accept-count: 1000
max-connections: 10000
max-threads: 800
min-spare-threads: 100
spring:
application:
name: kafkamanager
profiles:
active: dev
datasource:
kafka-manager:
jdbc-url: jdbc:mysql://localhost:3306/user_test?characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
main:
allow-bean-definition-overriding: true
servlet:
multipart:
max-file-size: 100MB
max-request-size: 100MB
logging:
config: classpath:logback-spring.xml
custom:
idc: cn
jmx:
max-conn: 10 # 2.3版本配置不在这个地方生效
store-metrics-task:
community:
broker-metrics-enabled: true
topic-metrics-enabled: true
didi:
app-topic-metrics-enabled: false
topic-request-time-metrics-enabled: false
topic-throttled-metrics: false
save-days: 7
# 任务相关的开关
task:
op:
sync-topic-enabled: false # 未落盘的Topic定期同步到DB中
order-auto-exec: # 工单自动化审批线程的开关
topic-enabled: false # Topic工单自动化审批开关, false:关闭自动化审批, true:开启
app-enabled: false # App工单自动化审批开关, false:关闭自动化审批, true:开启
account:
ldap:
enabled: false
url: ldap://127.0.0.1:389/
basedn: dc=tsign,dc=cn
factory: com.sun.jndi.ldap.LdapCtxFactory
filter: sAMAccountName
security:
authentication: simple
principal: cn=admin,dc=tsign,dc=cn
credentials: admin
auth-user-registration: true
auth-user-registration-role: normal
kcm:
enabled: false
s3:
endpoint: s3.didiyunapi.com
access-key: 1234567890
secret-key: 0987654321
bucket: logi-kafka
n9e:
base-url: http://127.0.0.1:8004
user-token: 12345678
timeout: 300
account: root
script-file: kcm_script.sh
monitor:
enabled: false
n9e:
nid: 2
user-token: 1234567890
mon:
base-url: http://127.0.0.1:8000 # 夜莺v4版本默认端口统一调整为了8000
sink:
base-url: http://127.0.0.1:8000 # 夜莺v4版本默认端口统一调整为了8000
rdb:
base-url: http://127.0.0.1:8000 # 夜莺v4版本默认端口统一调整为了8000
notify:
kafka:
cluster-id: 95
topic-name: didi-kafka-notify
order:
detail-url: http://127.0.0.1
test:
topic:
name1: moduleTest
name2: xgTest
name3: expandPartitionTopic
name4: topic_a
name5: NoOffsetChangeTopic
name6: _consumer_offsets
phyCluster:
id: 1
name: LogiKM_moduleTest
logicalCluster:
name: logical_cluster_1
broker:
id1: 1
id2: 2
id3: 3
app:
id: dkm_admin
ZK:
address: 127.0.0.1:2181/wyc
bootstrap-servers: 127.0.0.1:9093
gateway: 127.0.0.1
sasl-plaintext: SASL_PLAINTEXT://127.0.0.1:9093
admin: admin
consumer-group: moduleTestGroup
client-id: dkm_admin.moduleTest
region-name: region_1

View File

@@ -0,0 +1,63 @@
<assembly>
<id>assembly</id>
<formats>
<format>tar</format>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>src/main/resources/bin</directory>
<outputDirectory>bin</outputDirectory>
<includes>
<include>control.sh</include>
<include>start.bat</include>
</includes>
<fileMode>0755</fileMode>
</fileSet>
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>config</outputDirectory>
<includes>
<include>*.properties</include>
<include>*.xml</include>
<include>*.yml</include>
<include>env/dev/*</include>
<include>env/qa/*</include>
<include>env/uat/*</include>
<include>env/prod/*</include>
</includes>
</fileSet>
<fileSet>
<directory>target</directory>
<outputDirectory>lib</outputDirectory>
<includes>
<!--
<include>*release*.jar</include>
-->
<include>kafka-manager-web*.jar</include>
</includes>
<excludes>
<exclude>*sources.jar</exclude>
</excludes>
</fileSet>
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>logs</outputDirectory>
<fileMode>0755</fileMode>
<excludes>
<exclude>**/*</exclude>
</excludes>
</fileSet>
<!-- <fileSet>
<directory>${project.build.directory}/asciidoc</directory>
<outputDirectory>docs</outputDirectory>
<includes>
<include>md/*</include>
<include>html/*</include>
<include>pdf/*</include>
</includes>
<fileMode>0755</fileMode>
</fileSet>-->
</fileSets>
</assembly>

View File

@@ -0,0 +1,215 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="10 seconds">
<contextName>logback</contextName>
<property name="log.path" value="./logs" />
<!-- 彩色日志 -->
<!-- 彩色日志依赖的渲染类 -->
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
<!-- 彩色日志格式 -->
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<!--输出到控制台-->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>info</level>
</filter>
<encoder>
<Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<!--输出到文件-->
<!-- 时间滚动输出 level为 DEBUG 日志 -->
<appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/log_debug.log</file>
<!--日志文件输出格式-->
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset> <!-- 设置字符集 -->
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志归档 -->
<fileNamePattern>${log.path}/log_debug_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!--日志文件保留天数-->
<maxHistory>7</maxHistory>
</rollingPolicy>
<!-- 此日志文件只记录debug级别的 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>debug</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 时间滚动输出 level为 INFO 日志 -->
<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 正在记录的日志文件的路径及文件名 -->
<file>${log.path}/log_info.log</file>
<!--日志文件输出格式-->
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 每天日志归档路径以及格式 -->
<fileNamePattern>${log.path}/log_info_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!--日志文件保留天数-->
<maxHistory>7</maxHistory>
</rollingPolicy>
<!-- 此日志文件只记录info级别的 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>info</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 时间滚动输出 level为 WARN 日志 -->
<appender name="WARN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 正在记录的日志文件的路径及文件名 -->
<file>${log.path}/log_warn.log</file>
<!--日志文件输出格式-->
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset> <!-- 此处设置字符集 -->
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/log_warn_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!--日志文件保留天数-->
<maxHistory>7</maxHistory>
</rollingPolicy>
<!-- 此日志文件只记录warn级别的 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>warn</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 时间滚动输出 level为 ERROR 日志 -->
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 正在记录的日志文件的路径及文件名 -->
<file>${log.path}/log_error.log</file>
<!--日志文件输出格式-->
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset> <!-- 此处设置字符集 -->
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/log_error_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!--日志文件保留天数-->
<maxHistory>7</maxHistory>
</rollingPolicy>
<!-- 此日志文件只记录ERROR级别的 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- Metrics信息收集日志 -->
<appender name="COLLECTOR_METRICS_LOGGER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/metrics/collector_metrics.log</file>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/metrics/collector_metrics_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>3</maxHistory>
</rollingPolicy>
</appender>
<!-- Metrics信息收集日志 -->
<appender name="API_METRICS_LOGGER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/metrics/api_metrics.log</file>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/metrics/api_metrics_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>3</maxHistory>
</rollingPolicy>
</appender>
<!-- Metrics信息收集日志 -->
<appender name="SCHEDULED_TASK_LOGGER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/metrics/scheduled_tasks.log</file>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.path}/metrics/scheduled_tasks_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>5</maxHistory>
</rollingPolicy>
</appender>
<logger name="COLLECTOR_METRICS_LOGGER" level="DEBUG" additivity="false">
<appender-ref ref="COLLECTOR_METRICS_LOGGER"/>
</logger>
<logger name="API_METRICS_LOGGER" level="DEBUG" additivity="false">
<appender-ref ref="API_METRICS_LOGGER"/>
</logger>
<logger name="SCHEDULED_TASK_LOGGER" level="DEBUG" additivity="false">
<appender-ref ref="SCHEDULED_TASK_LOGGER"/>
</logger>
<logger name="org.apache.ibatis" level="INFO" additivity="false" />
<logger name="org.mybatis.spring" level="INFO" additivity="false" />
<logger name="com.github.miemiedev.mybatis.paginator" level="INFO" additivity="false" />
<root level="info">
<appender-ref ref="CONSOLE" />
<appender-ref ref="DEBUG_FILE" />
<appender-ref ref="INFO_FILE" />
<appender-ref ref="WARN_FILE" />
<appender-ref ref="ERROR_FILE" />
<!--<appender-ref ref="METRICS_LOG" />-->
</root>
<!--生产环境:输出到文件-->
<!--<springProfile name="pro">-->
<!--<root level="info">-->
<!--<appender-ref ref="CONSOLE" />-->
<!--<appender-ref ref="DEBUG_FILE" />-->
<!--<appender-ref ref="INFO_FILE" />-->
<!--<appender-ref ref="ERROR_FILE" />-->
<!--<appender-ref ref="WARN_FILE" />-->
<!--</root>-->
<!--</springProfile>-->
</configuration>

View File

@@ -69,5 +69,22 @@
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
</dependency>
<!-- testng -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.10</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,80 @@
package com.xiaojukeji.kafka.manager.kcm;
import com.xiaojukeji.kafka.manager.common.entity.Result;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.kcm.common.entry.ao.CreationTaskData;
import com.xiaojukeji.kafka.manager.kcm.common.entry.dto.ClusterHostTaskDTO;
import com.xiaojukeji.kafka.manager.kcm.config.BaseTest;
import com.xiaojukeji.kafka.manager.kcm.tasks.ClusterHostTaskService;
import org.junit.Assert;
import org.mockito.InjectMocks;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.annotations.Test;
import java.util.Arrays;
/**
* @author xuguang
* @Date 2022/1/5
*/
public class ClusterHostTaskServiceTest extends BaseTest {
@Autowired
@InjectMocks
private ClusterHostTaskService clusterHostTaskService;
private ClusterHostTaskDTO getClusterHostTaskDTO() {
ClusterHostTaskDTO clusterHostTaskDTO = new ClusterHostTaskDTO();
clusterHostTaskDTO.setClusterId(-1L);
clusterHostTaskDTO.setHostList(Arrays.asList("127.0.0.1"));
clusterHostTaskDTO.setTaskType("");
clusterHostTaskDTO.setKafkaFileBaseUrl("");
clusterHostTaskDTO.setKafkaPackageMd5("");
clusterHostTaskDTO.setKafkaPackageName("");
clusterHostTaskDTO.setServerPropertiesMd5("");
clusterHostTaskDTO.setServerPropertiesName("");
return clusterHostTaskDTO;
}
@Test
public void getOperationHostsTest() {
// cluster task host list illegal
getOperationHosts2HostListIllegalTest();
// success
getOperationHosts2SuccessTest();
}
private void getOperationHosts2HostListIllegalTest() {
ClusterHostTaskDTO clusterHostTaskDTO = getClusterHostTaskDTO();
clusterHostTaskDTO.setHostList(Arrays.asList(""));
Result<CreationTaskData> result = clusterHostTaskService.getOperationHosts(clusterHostTaskDTO);
Assert.assertEquals(result.getCode(), ResultStatus.CLUSTER_TASK_HOST_LIST_ILLEGAL.getCode());
}
private void getOperationHosts2SuccessTest() {
ClusterHostTaskDTO clusterHostTaskDTO = getClusterHostTaskDTO();
Result<CreationTaskData> result = clusterHostTaskService.getOperationHosts(clusterHostTaskDTO);
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test
public void getCreateTaskParamDTOTest() {
// not success
getCreateTaskParamDTO2NotSuccessTest();
// success
getCreateTaskParamDTO2SuccessTest();
}
private void getCreateTaskParamDTO2NotSuccessTest() {
ClusterHostTaskDTO clusterHostTaskDTO = getClusterHostTaskDTO();
clusterHostTaskDTO.setHostList(Arrays.asList(""));
Result<CreationTaskData> dto = clusterHostTaskService.getCreateTaskParamDTO(clusterHostTaskDTO);
Assert.assertEquals(dto.getCode(), ResultStatus.CLUSTER_TASK_HOST_LIST_ILLEGAL.getCode());
}
private void getCreateTaskParamDTO2SuccessTest() {
ClusterHostTaskDTO clusterHostTaskDTO = getClusterHostTaskDTO();
Result<CreationTaskData> dto = clusterHostTaskService.getCreateTaskParamDTO(clusterHostTaskDTO);
Assert.assertEquals(dto.getCode(), ResultStatus.SUCCESS.getCode());
}
}

View File

@@ -0,0 +1,74 @@
package com.xiaojukeji.kafka.manager.kcm;
import com.xiaojukeji.kafka.manager.common.bizenum.KafkaBrokerRoleEnum;
import com.xiaojukeji.kafka.manager.common.entity.Result;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.kcm.common.entry.ao.CreationTaskData;
import com.xiaojukeji.kafka.manager.kcm.common.entry.dto.ClusterRoleTaskDTO;
import com.xiaojukeji.kafka.manager.kcm.config.BaseTest;
import com.xiaojukeji.kafka.manager.kcm.tasks.ClusterRoleTaskService;
import org.junit.Assert;
import org.mockito.InjectMocks;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.annotations.Test;
import java.util.*;
/**
* @author xuguang
* @Date 2022/1/5
*/
public class ClusterRoleTaskServiceTest extends BaseTest {
@Autowired
@InjectMocks
private ClusterRoleTaskService clusterRoleTaskService;
private ClusterRoleTaskDTO getClusterRoleTaskDTO() {
ClusterRoleTaskDTO clusterRoleTaskDTO = new ClusterRoleTaskDTO();
clusterRoleTaskDTO.setClusterId(-1L);
clusterRoleTaskDTO.setTaskType("");
return clusterRoleTaskDTO;
}
@Test
public void getOperationHostsTest() {
// controller not alive
getOperationHosts2ControllerNotAliveTest();
// success
getOperationHosts2SuccessTest();
}
private void getOperationHosts2ControllerNotAliveTest() {
ClusterRoleTaskDTO dto = getClusterRoleTaskDTO();
List<String> upgradeSequenceList = new ArrayList<>();
upgradeSequenceList.add(KafkaBrokerRoleEnum.CONTROLLER.getRole());
dto.setUpgradeSequenceList(upgradeSequenceList);
dto.setKafkaRoleBrokerHostMap(new HashMap<>(0));
Result<CreationTaskData> result = clusterRoleTaskService.getOperationHosts(dto);
Assert.assertEquals(result.getCode(), ResultStatus.CONTROLLER_NOT_ALIVE.getCode());
}
private void getOperationHosts2SuccessTest() {
ClusterRoleTaskDTO dto = getClusterRoleTaskDTO();
List<String> upgradeSequenceList = new ArrayList<>();
upgradeSequenceList.add(KafkaBrokerRoleEnum.CONTROLLER.getRole());
upgradeSequenceList.add(KafkaBrokerRoleEnum.NORMAL.getRole());
upgradeSequenceList.add(KafkaBrokerRoleEnum.COORDINATOR.getRole());
dto.setUpgradeSequenceList(upgradeSequenceList);
Map<String, List<String>> map = new HashMap<>();
List<String> controllerList = new ArrayList<>();
controllerList.add("127.0.0.1");
controllerList.add("localhost");
List<String> coordinatorList = new ArrayList<>();
coordinatorList.add("127.0.0.1");
coordinatorList.add("localhost");
map.put(KafkaBrokerRoleEnum.CONTROLLER.getRole(), controllerList);
map.put(KafkaBrokerRoleEnum.COORDINATOR.getRole(), coordinatorList);
dto.setKafkaRoleBrokerHostMap(map);
Result<CreationTaskData> result = clusterRoleTaskService.getOperationHosts(dto);
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
}
}

View File

@@ -0,0 +1,358 @@
package com.xiaojukeji.kafka.manager.kcm;
import com.xiaojukeji.kafka.manager.common.entity.Result;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.pojo.ClusterTaskDO;
import com.xiaojukeji.kafka.manager.dao.ClusterTaskDao;
import com.xiaojukeji.kafka.manager.kcm.common.bizenum.ClusterTaskActionEnum;
import com.xiaojukeji.kafka.manager.kcm.common.bizenum.ClusterTaskStateEnum;
import com.xiaojukeji.kafka.manager.kcm.common.bizenum.ClusterTaskTypeEnum;
import com.xiaojukeji.kafka.manager.kcm.common.entry.ao.ClusterTaskLog;
import com.xiaojukeji.kafka.manager.kcm.common.entry.ao.ClusterTaskStatus;
import com.xiaojukeji.kafka.manager.kcm.common.entry.dto.AbstractClusterTaskDTO;
import com.xiaojukeji.kafka.manager.kcm.common.entry.dto.ClusterHostTaskDTO;
import com.xiaojukeji.kafka.manager.kcm.component.agent.AbstractAgent;
import com.xiaojukeji.kafka.manager.kcm.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.springframework.beans.factory.annotation.Value;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.Arrays;
/**
* @author xuguang
* @Date 2021/12/27
*/
public class ClusterTaskServiceTest extends BaseTest {
@Value("${test.phyCluster.id}")
private Long REAL_CLUSTER_ID_IN_MYSQL;
@Value("${test.admin}")
private String ADMIN;
private static final String BASEURL = "127.0.0.1";
private static final String MD5 = "md5";
private static final Long REAL_TASK_ID_IN_MYSQL = 1L;
private static final Long INVALID_TASK_ID = -1L;
@Autowired
@InjectMocks
private ClusterTaskService clusterTaskService;
@Mock
private AbstractAgent abstractAgent;
@Mock
private ClusterTaskDao clusterTaskDao;
private ClusterHostTaskDTO getClusterHostTaskDTO() {
ClusterHostTaskDTO clusterHostTaskDTO = new ClusterHostTaskDTO();
clusterHostTaskDTO.setClusterId(-1L);
clusterHostTaskDTO.setHostList(Arrays.asList(BASEURL));
clusterHostTaskDTO.setTaskType(ClusterTaskTypeEnum.HOST_UPGRADE.getName());
clusterHostTaskDTO.setKafkaFileBaseUrl(BASEURL);
clusterHostTaskDTO.setKafkaPackageMd5(MD5);
clusterHostTaskDTO.setKafkaPackageName("name");
clusterHostTaskDTO.setServerPropertiesMd5(MD5);
clusterHostTaskDTO.setServerPropertiesName("name");
return clusterHostTaskDTO;
}
private ClusterTaskDO getClusterTaskDO() {
ClusterTaskDO clusterTaskDO = new ClusterTaskDO();
clusterTaskDO.setClusterId(REAL_CLUSTER_ID_IN_MYSQL);
clusterTaskDO.setId(REAL_TASK_ID_IN_MYSQL);
clusterTaskDO.setAgentTaskId(-1L);
clusterTaskDO.setAgentRollbackTaskId(-1L);
clusterTaskDO.setTaskStatus(0);
return clusterTaskDO;
}
@BeforeMethod
public void setup() {
MockitoAnnotations.initMocks(this);
}
@Test(description = "测试创建任务")
public void createTaskTest() {
// paramIllegal
createTask2ParamIllegalTest();
// not success
createTask2NotSuccessTest();
// CallClusterTaskAgentFailed
createTask2CallClusterTaskAgentFailedTest();
// success
createTask2SuccessTest();
// mysqlError
createTask2MysqlErrorTest();
}
private void createTask2ParamIllegalTest() {
AbstractClusterTaskDTO dto = getClusterHostTaskDTO();
dto.setTaskType(null);
Result result1 = clusterTaskService.createTask(dto, ADMIN);
Assert.assertEquals(result1.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
dto.setTaskType("");
Result result2 = clusterTaskService.createTask(dto, ADMIN);
Assert.assertEquals(result2.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
}
private void createTask2NotSuccessTest() {
ClusterHostTaskDTO clusterHostTaskDTO = getClusterHostTaskDTO();
clusterHostTaskDTO.setHostList(Arrays.asList("host"));
Result result = clusterTaskService.createTask(clusterHostTaskDTO, ADMIN);
Assert.assertEquals(result.getCode(), ResultStatus.CLUSTER_TASK_HOST_LIST_ILLEGAL.getCode());
}
private void createTask2CallClusterTaskAgentFailedTest() {
Mockito.when(abstractAgent.createTask(Mockito.any())).thenReturn(null);
ClusterHostTaskDTO clusterHostTaskDTO = getClusterHostTaskDTO();
Result result = clusterTaskService.createTask(clusterHostTaskDTO, ADMIN);
Assert.assertEquals(result.getCode(), ResultStatus.CALL_CLUSTER_TASK_AGENT_FAILED.getCode());
}
private void createTask2SuccessTest() {
Mockito.when(abstractAgent.createTask(Mockito.any())).thenReturn(Result.buildSuc(1L));
Mockito.when(clusterTaskDao.insert(Mockito.any())).thenReturn(1);
ClusterHostTaskDTO clusterHostTaskDTO = getClusterHostTaskDTO();
Result result = clusterTaskService.createTask(clusterHostTaskDTO, ADMIN);
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
}
private void createTask2MysqlErrorTest() {
Mockito.when(abstractAgent.createTask(Mockito.any())).thenReturn(Result.buildSuc(1L));
Mockito.when(clusterTaskDao.insert(Mockito.any())).thenThrow(RuntimeException.class);
ClusterHostTaskDTO clusterHostTaskDTO = getClusterHostTaskDTO();
Result result = clusterTaskService.createTask(clusterHostTaskDTO, ADMIN);
Assert.assertEquals(result.getCode(), ResultStatus.MYSQL_ERROR.getCode());
}
@Test
public void executeTaskTest() {
// task not exist
executeTask2TaskNotExistTest();
// CallClusterTaskAgentFailed
executeTask2CallClusterTaskAgentFailedTest();
// 暂停状态, 可以执行开始
executeTask2StartTest();
// 运行状态, 可以执行暂停
executeTask2PauseTest();
// 忽略
executeTask2IgnoreTest();
// 取消
executeTask2CancelTest();
// operation failed
executeTask2OperationFailedTest();
// 回滚 operation forbidden
executeTask2RollbackForbiddenTest();
}
private void executeTask2TaskNotExistTest() {
ResultStatus resultStatus = clusterTaskService.executeTask(INVALID_TASK_ID, ClusterTaskActionEnum.START.getAction(), ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.RESOURCE_NOT_EXIST.getCode());
}
private void executeTask2CallClusterTaskAgentFailedTest() {
Mockito.when(abstractAgent.getTaskExecuteState(Mockito.anyLong())).thenReturn(null);
ClusterTaskDO clusterTaskDO = getClusterTaskDO();
Mockito.when(clusterTaskDao.getById(Mockito.anyLong())).thenReturn(clusterTaskDO);
ResultStatus resultStatus = clusterTaskService.executeTask(REAL_TASK_ID_IN_MYSQL, ClusterTaskActionEnum.START.getAction(), ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.CALL_CLUSTER_TASK_AGENT_FAILED.getCode());
}
private void executeTask2StartTest() {
Mockito.when(abstractAgent.getTaskExecuteState(Mockito.anyLong())).thenReturn(Result.buildSuc(ClusterTaskStateEnum.BLOCKED));
ClusterTaskDO clusterTaskDO = getClusterTaskDO();
Mockito.when(clusterTaskDao.getById(Mockito.anyLong())).thenReturn(clusterTaskDO);
// success
Mockito.when(abstractAgent.actionTask(Mockito.anyLong(), Mockito.any())).thenReturn(true);
ResultStatus resultStatus = clusterTaskService.executeTask(REAL_TASK_ID_IN_MYSQL, ClusterTaskActionEnum.START.getAction(), ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
// operation failed
Mockito.when(abstractAgent.actionTask(Mockito.anyLong(), Mockito.any())).thenReturn(false);
ResultStatus resultStatus2 = clusterTaskService.executeTask(REAL_TASK_ID_IN_MYSQL, ClusterTaskActionEnum.START.getAction(), ADMIN);
Assert.assertEquals(resultStatus2.getCode(), ResultStatus.OPERATION_FAILED.getCode());
}
private void executeTask2PauseTest() {
Mockito.when(abstractAgent.getTaskExecuteState(Mockito.anyLong())).thenReturn(Result.buildSuc(ClusterTaskStateEnum.RUNNING));
ClusterTaskDO clusterTaskDO = getClusterTaskDO();
Mockito.when(clusterTaskDao.getById(Mockito.anyLong())).thenReturn(clusterTaskDO);
// success
Mockito.when(abstractAgent.actionTask(Mockito.anyLong(), Mockito.any())).thenReturn(true);
ResultStatus resultStatus = clusterTaskService.executeTask(REAL_TASK_ID_IN_MYSQL, ClusterTaskActionEnum.PAUSE.getAction(), ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
// operation failed
Mockito.when(abstractAgent.actionTask(Mockito.anyLong(), Mockito.any())).thenReturn(false);
ResultStatus resultStatus2 = clusterTaskService.executeTask(REAL_TASK_ID_IN_MYSQL, ClusterTaskActionEnum.PAUSE.getAction(), ADMIN);
Assert.assertEquals(resultStatus2.getCode(), ResultStatus.OPERATION_FAILED.getCode());
}
private void executeTask2IgnoreTest() {
Mockito.when(abstractAgent.getTaskExecuteState(Mockito.anyLong())).thenReturn(Result.buildSuc(ClusterTaskStateEnum.RUNNING));
ClusterTaskDO clusterTaskDO = getClusterTaskDO();
Mockito.when(clusterTaskDao.getById(Mockito.anyLong())).thenReturn(clusterTaskDO);
// success
Mockito.when(abstractAgent.actionTask(Mockito.anyLong(), Mockito.any())).thenReturn(true);
ResultStatus resultStatus = clusterTaskService.executeTask(REAL_TASK_ID_IN_MYSQL, ClusterTaskActionEnum.IGNORE.getAction(), "");
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
// operation failed
Mockito.when(abstractAgent.actionTask(Mockito.anyLong(), Mockito.any())).thenReturn(false);
ResultStatus resultStatus2 = clusterTaskService.executeTask(REAL_TASK_ID_IN_MYSQL, ClusterTaskActionEnum.IGNORE.getAction(), "");
Assert.assertEquals(resultStatus2.getCode(), ResultStatus.OPERATION_FAILED.getCode());
}
private void executeTask2CancelTest() {
Mockito.when(abstractAgent.getTaskExecuteState(Mockito.anyLong())).thenReturn(Result.buildSuc(ClusterTaskStateEnum.RUNNING));
ClusterTaskDO clusterTaskDO = getClusterTaskDO();
Mockito.when(clusterTaskDao.getById(Mockito.anyLong())).thenReturn(clusterTaskDO);
// success
Mockito.when(abstractAgent.actionTask(Mockito.anyLong(), Mockito.any())).thenReturn(true);
ResultStatus resultStatus = clusterTaskService.executeTask(REAL_TASK_ID_IN_MYSQL, ClusterTaskActionEnum.CANCEL.getAction(), "");
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
// operation failed
Mockito.when(abstractAgent.actionTask(Mockito.anyLong(), Mockito.any())).thenReturn(false);
ResultStatus resultStatus2 = clusterTaskService.executeTask(REAL_TASK_ID_IN_MYSQL, ClusterTaskActionEnum.CANCEL.getAction(), "");
Assert.assertEquals(resultStatus2.getCode(), ResultStatus.OPERATION_FAILED.getCode());
}
private void executeTask2OperationFailedTest() {
Mockito.when(abstractAgent.getTaskExecuteState(Mockito.anyLong())).thenReturn(Result.buildSuc(ClusterTaskStateEnum.RUNNING));
ClusterTaskDO clusterTaskDO = getClusterTaskDO();
Mockito.when(clusterTaskDao.getById(Mockito.anyLong())).thenReturn(clusterTaskDO);
// operation failed
ResultStatus resultStatus2 = clusterTaskService.executeTask(REAL_TASK_ID_IN_MYSQL, ClusterTaskActionEnum.START.getAction(), ADMIN);
Assert.assertEquals(resultStatus2.getCode(), ResultStatus.OPERATION_FAILED.getCode());
}
private void executeTask2RollbackForbiddenTest() {
Mockito.when(abstractAgent.getTaskExecuteState(Mockito.anyLong())).thenReturn(Result.buildSuc(ClusterTaskStateEnum.RUNNING));
ClusterTaskDO clusterTaskDO = getClusterTaskDO();
clusterTaskDO.setAgentRollbackTaskId(1L);
Mockito.when(clusterTaskDao.getById(Mockito.anyLong())).thenReturn(clusterTaskDO);
// operation failed
ResultStatus resultStatus2 = clusterTaskService.executeTask(REAL_TASK_ID_IN_MYSQL, ClusterTaskActionEnum.ROLLBACK.getAction(), ADMIN);
Assert.assertEquals(resultStatus2.getCode(), ResultStatus.OPERATION_FORBIDDEN.getCode());
}
@Test
public void getTaskLogTest() {
// task not exist
getTaskLog2TaskNotExistTest();
// call cluster task agent failed
getTaskLog2CallClusterTaskAgentFailedTest();
// success
getTaskLog2SuccessTest();
}
private void getTaskLog2TaskNotExistTest() {
Mockito.when(clusterTaskDao.getById(Mockito.anyLong())).thenReturn(null);
Result<String> result = clusterTaskService.getTaskLog(REAL_TASK_ID_IN_MYSQL, ADMIN);
Assert.assertEquals(result.getCode(), ResultStatus.TASK_NOT_EXIST.getCode());
}
private void getTaskLog2CallClusterTaskAgentFailedTest() {
ClusterTaskDO clusterTaskDO = getClusterTaskDO();
Mockito.when(clusterTaskDao.getById(Mockito.anyLong())).thenReturn(clusterTaskDO);
Mockito.when(abstractAgent.getTaskLog(Mockito.anyLong(), Mockito.any())).thenReturn(null);
Result<String> result = clusterTaskService.getTaskLog(REAL_TASK_ID_IN_MYSQL, ADMIN);
Assert.assertEquals(result.getCode(), ResultStatus.CALL_CLUSTER_TASK_AGENT_FAILED.getCode());
}
private void getTaskLog2SuccessTest() {
ClusterTaskDO clusterTaskDO = getClusterTaskDO();
Mockito.when(clusterTaskDao.getById(Mockito.anyLong())).thenReturn(clusterTaskDO);
Mockito.when(abstractAgent.getTaskLog(Mockito.anyLong(), Mockito.any()))
.thenReturn(Result.buildSuc(new ClusterTaskLog("")));
Result<String> result = clusterTaskService.getTaskLog(REAL_TASK_ID_IN_MYSQL, ADMIN);
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test
public void getTaskStateTest() {
// null
getTaskState2NullTest();
//
getTaskState2NotNullTest();
}
private void getTaskState2NullTest() {
Mockito.when(abstractAgent.getTaskExecuteState(Mockito.anyLong())).thenReturn(null);
ClusterTaskStateEnum taskState = clusterTaskService.getTaskState(REAL_TASK_ID_IN_MYSQL);
Assert.assertNull(taskState);
}
private void getTaskState2NotNullTest() {
Mockito.when(abstractAgent.getTaskExecuteState(Mockito.anyLong())).thenReturn(Result.buildSuc(ClusterTaskStateEnum.RUNNING));
ClusterTaskStateEnum taskState = clusterTaskService.getTaskState(REAL_TASK_ID_IN_MYSQL);
Assert.assertNotNull(taskState);
}
@Test
public void getTaskStatusTest() {
// task not exist
getTaskStatus2TaskNotExistTest();
// get failed
getTaskStatus2FailedTest();
// success
getTaskStatus2SuccessTest();
}
private void getTaskStatus2TaskNotExistTest() {
Mockito.when(clusterTaskDao.getById(Mockito.anyLong())).thenReturn(null);
Result<ClusterTaskStatus> result = clusterTaskService.getTaskStatus(REAL_TASK_ID_IN_MYSQL);
Assert.assertEquals(result.getCode(), ResultStatus.TASK_NOT_EXIST.getCode());
}
private void getTaskStatus2FailedTest() {
ClusterTaskDO clusterTaskDO = getClusterTaskDO();
Mockito.when(clusterTaskDao.getById(Mockito.anyLong())).thenReturn(clusterTaskDO);
Mockito.when(abstractAgent.getTaskExecuteState(Mockito.anyLong())).thenReturn(Result.buildFailure(""));
Result<ClusterTaskStatus> result = clusterTaskService.getTaskStatus(REAL_TASK_ID_IN_MYSQL);
Assert.assertEquals(result.getCode(), ResultStatus.FAIL.getCode());
}
private void getTaskStatus2SuccessTest() {
ClusterTaskDO clusterTaskDO = getClusterTaskDO();
Mockito.when(clusterTaskDao.getById(Mockito.anyLong())).thenReturn(clusterTaskDO);
Mockito.when(abstractAgent.getTaskExecuteState(Mockito.anyLong()))
.thenReturn(Result.buildSuc(ClusterTaskStateEnum.RUNNING));
Result<ClusterTaskStatus> result = clusterTaskService.getTaskStatus(REAL_TASK_ID_IN_MYSQL);
Assert.assertEquals(result.getCode(), ResultStatus.SUCCESS.getCode());
}
}

View File

@@ -0,0 +1,226 @@
package com.xiaojukeji.kafka.manager.kcm;
import com.xiaojukeji.kafka.manager.common.bizenum.KafkaFileEnum;
import com.xiaojukeji.kafka.manager.common.entity.Result;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.dto.normal.KafkaFileDTO;
import com.xiaojukeji.kafka.manager.common.entity.pojo.KafkaFileDO;
import com.xiaojukeji.kafka.manager.dao.KafkaFileDao;
import com.xiaojukeji.kafka.manager.kcm.component.storage.AbstractStorageService;
import com.xiaojukeji.kafka.manager.kcm.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.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
/**
* @author xuguang
* @Date 2022/1/4
*/
public class KafkaFileServiceTest extends BaseTest {
private static final Long KAFKA_FILE_ID = 1L;
@Value("${test.admin}")
private static final String ADMIN = "admin";
private KafkaFileDTO getKafkaFileDTO() {
KafkaFileDTO kafkaFileDTO = new KafkaFileDTO();
kafkaFileDTO.setId(KAFKA_FILE_ID);
kafkaFileDTO.setClusterId(-1L);
kafkaFileDTO.setFileMd5("");
kafkaFileDTO.setFileName(".tgz");
kafkaFileDTO.setFileType(KafkaFileEnum.PACKAGE.getCode());
kafkaFileDTO.setUploadFile(new MockMultipartFile("name", new byte[]{1}));
return kafkaFileDTO;
}
private KafkaFileDO getKafkaFileDO() {
KafkaFileDO kafkaFileDO = new KafkaFileDO();
kafkaFileDO.setFileType(KafkaFileEnum.PACKAGE.getCode());
kafkaFileDO.setFileName(".tgz");
return kafkaFileDO;
}
@Autowired
@InjectMocks
private KafkaFileService kafkaFileService;
@Mock
private KafkaFileDao kafkaFileDao;
@Mock
private AbstractStorageService storageService;
@BeforeMethod
public void setup() {
MockitoAnnotations.initMocks(this);
}
@Test
public void uploadKafkaFile() {
// paramIllegal
uploadKafkaFile2ParamIllegalTest();
// mysqlError
uploadKafkaFile2MysqlErrorTest();
// storage upload file failed
uploadKafkaFile2StorageUploadFileFailedTest();
// success
uploadKafkaFile2SuccessTest();
// DuplicateKey
uploadKafkaFile2DuplicateKeyTest();
}
private void uploadKafkaFile2ParamIllegalTest() {
KafkaFileDTO kafkaFileDTO = getKafkaFileDTO();
kafkaFileDTO.setUploadFile(null);
ResultStatus resultStatus = kafkaFileService.uploadKafkaFile(kafkaFileDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
}
private void uploadKafkaFile2MysqlErrorTest() {
Mockito.when(kafkaFileDao.insert(Mockito.any())).thenReturn(-1);
KafkaFileDTO kafkaFileDTO = getKafkaFileDTO();
ResultStatus resultStatus = kafkaFileService.uploadKafkaFile(kafkaFileDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.MYSQL_ERROR.getCode());
}
private void uploadKafkaFile2StorageUploadFileFailedTest() {
Mockito.when(kafkaFileDao.insert(Mockito.any())).thenReturn(1);
Mockito.when(kafkaFileDao.deleteById(Mockito.any())).thenReturn(1);
Mockito.when(storageService.upload(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(false);
KafkaFileDTO kafkaFileDTO = getKafkaFileDTO();
ResultStatus resultStatus = kafkaFileService.uploadKafkaFile(kafkaFileDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.STORAGE_UPLOAD_FILE_FAILED.getCode());
}
private void uploadKafkaFile2SuccessTest() {
Mockito.when(kafkaFileDao.insert(Mockito.any())).thenReturn(1);
Mockito.when(kafkaFileDao.deleteById(Mockito.any())).thenReturn(1);
Mockito.when(storageService.upload(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(true);
KafkaFileDTO kafkaFileDTO = getKafkaFileDTO();
ResultStatus resultStatus = kafkaFileService.uploadKafkaFile(kafkaFileDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
}
private void uploadKafkaFile2DuplicateKeyTest() {
Mockito.when(kafkaFileDao.insert(Mockito.any())).thenThrow(DuplicateKeyException.class);
Mockito.when(kafkaFileDao.deleteById(Mockito.any())).thenReturn(1);
Mockito.when(storageService.upload(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(true);
KafkaFileDTO kafkaFileDTO = getKafkaFileDTO();
ResultStatus resultStatus = kafkaFileService.uploadKafkaFile(kafkaFileDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.RESOURCE_ALREADY_EXISTED.getCode());
}
@Test
public void modifyKafkaFileTest() {
// paramIllegal
modifyKafkaFile2ParamIllegalTest();
// resource not exist
modifyKafkaFile2ResourceNotExistTest();
// operation failed
modifyKafkaFile2OperationFailedTest();
// mysqlError
modifyKafkaFile2MysqlErrorTest();
// success
modifyKafkaFile2SuccessTest();
}
private void modifyKafkaFile2ParamIllegalTest() {
ResultStatus resultStatus = kafkaFileService.modifyKafkaFile(null, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.PARAM_ILLEGAL.getCode());
}
private void modifyKafkaFile2ResourceNotExistTest() {
Mockito.when(kafkaFileDao.getById(Mockito.anyLong())).thenReturn(null);
KafkaFileDTO kafkaFileDTO = getKafkaFileDTO();
ResultStatus resultStatus = kafkaFileService.modifyKafkaFile(kafkaFileDTO, ADMIN);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.RESOURCE_NOT_EXIST.getCode());
}
private void modifyKafkaFile2OperationFailedTest() {
Mockito.when(kafkaFileDao.getById(Mockito.anyLong())).thenReturn(new KafkaFileDO());
Mockito.when(kafkaFileDao.updateById(Mockito.any())).thenReturn(-1);
KafkaFileDTO kafkaFileDTO = getKafkaFileDTO();
kafkaFileDTO.setFileType(-1);
ResultStatus resultStatus1 = kafkaFileService.modifyKafkaFile(kafkaFileDTO, ADMIN);
Assert.assertEquals(resultStatus1.getCode(), ResultStatus.OPERATION_FAILED.getCode());
kafkaFileDTO.setFileType(KafkaFileEnum.PACKAGE.getCode());
kafkaFileDTO.setFileName("xxx");
ResultStatus resultStatus2 = kafkaFileService.modifyKafkaFile(kafkaFileDTO, ADMIN);
Assert.assertEquals(resultStatus2.getCode(), ResultStatus.OPERATION_FAILED.getCode());
}
private void modifyKafkaFile2MysqlErrorTest() {
KafkaFileDO kafkaFileDO = getKafkaFileDO();
Mockito.when(kafkaFileDao.getById(Mockito.anyLong())).thenReturn(kafkaFileDO);
Mockito.when(kafkaFileDao.updateById(Mockito.any())).thenReturn(-1);
KafkaFileDTO kafkaFileDTO = getKafkaFileDTO();
ResultStatus resultStatus1 = kafkaFileService.modifyKafkaFile(kafkaFileDTO, ADMIN);
Assert.assertEquals(resultStatus1.getCode(), ResultStatus.MYSQL_ERROR.getCode());
}
private void modifyKafkaFile2SuccessTest() {
KafkaFileDO kafkaFileDO = getKafkaFileDO();
Mockito.when(kafkaFileDao.getById(Mockito.anyLong())).thenReturn(kafkaFileDO);
Mockito.when(kafkaFileDao.updateById(Mockito.any())).thenReturn(1);
Mockito.when(storageService.upload(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(true);
KafkaFileDTO kafkaFileDTO = getKafkaFileDTO();
ResultStatus resultStatus1 = kafkaFileService.modifyKafkaFile(kafkaFileDTO, ADMIN);
Assert.assertEquals(resultStatus1.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test
public void downloadKafkaFileTest() {
// resource not exist
downloadKafkaFile2ResourceNotExist();
// STORAGE_FILE_TYPE_NOT_SUPPORT
downloadKafkaFile2FileNotSupportExist();
// success
downloadKafkaFile2SuccessExist();
}
private void downloadKafkaFile2ResourceNotExist() {
Mockito.when(kafkaFileDao.getById(Mockito.anyLong())).thenReturn(null);
Result<MultipartFile> resultStatus = kafkaFileService.downloadKafkaFile(KAFKA_FILE_ID);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.RESOURCE_NOT_EXIST.getCode());
}
private void downloadKafkaFile2FileNotSupportExist() {
KafkaFileDO kafkaFileDO = getKafkaFileDO();
Mockito.when(kafkaFileDao.getById(Mockito.anyLong())).thenReturn(kafkaFileDO);
Result<MultipartFile> resultStatus = kafkaFileService.downloadKafkaFile(KAFKA_FILE_ID);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.STORAGE_FILE_TYPE_NOT_SUPPORT.getCode());
}
private void downloadKafkaFile2SuccessExist() {
Mockito.when(storageService.download(Mockito.any(), Mockito.any())).thenReturn(Result.buildFrom(ResultStatus.SUCCESS));
KafkaFileDO kafkaFileDO = getKafkaFileDO();
kafkaFileDO.setFileType(KafkaFileEnum.SERVER_CONFIG.getCode());
Mockito.when(kafkaFileDao.getById(Mockito.anyLong())).thenReturn(kafkaFileDO);
Result<MultipartFile> resultStatus = kafkaFileService.downloadKafkaFile(KAFKA_FILE_ID);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
}
}

View File

@@ -0,0 +1,11 @@
package com.xiaojukeji.kafka.manager.kcm.config;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
@SpringBootTest(classes = CoreSpringBootStartUp.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(classes = CoreSpringBootStartUp.class)
public class BaseTest extends AbstractTransactionalTestNGSpringContextTests {
}

View File

@@ -0,0 +1,21 @@
package com.xiaojukeji.kafka.manager.kcm.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableAsync
@EnableScheduling
@ServletComponentScan
@EnableAutoConfiguration
@SpringBootApplication(scanBasePackages = {"com.xiaojukeji.kafka.manager"})
public class CoreSpringBootStartUp {
public static void main(String[] args) {
SpringApplication sa = new SpringApplication(CoreSpringBootStartUp.class);
sa.run(args);
}
}

View File

@@ -0,0 +1,51 @@
package com.xiaojukeji.kafka.manager.kcm.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* @author zengqiao
* @date 20/3/17
*/
@Configuration
public class DataSourceConfig {
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource.kafka-manager")
@Primary
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "sqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
bean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:mybatis-config.xml"));
return bean.getObject();
}
@Bean(name = "transactionManager")
@Primary
public DataSourceTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "sqlSession")
@Primary
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}

View File

@@ -0,0 +1,127 @@
server:
port: 8080
tomcat:
accept-count: 1000
max-connections: 10000
max-threads: 800
min-spare-threads: 100
spring:
application:
name: kafkamanager
profiles:
active: dev
datasource:
kafka-manager:
jdbc-url: jdbc:mysql://localhost:3306/user_test?characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
main:
allow-bean-definition-overriding: true
servlet:
multipart:
max-file-size: 100MB
max-request-size: 100MB
logging:
config: classpath:logback-spring.xml
custom:
idc: cn
jmx:
max-conn: 10 # 2.3版本配置不在这个地方生效
store-metrics-task:
community:
broker-metrics-enabled: true
topic-metrics-enabled: true
didi:
app-topic-metrics-enabled: false
topic-request-time-metrics-enabled: false
topic-throttled-metrics: false
save-days: 7
# 任务相关的开关
task:
op:
sync-topic-enabled: false # 未落盘的Topic定期同步到DB中
order-auto-exec: # 工单自动化审批线程的开关
topic-enabled: false # Topic工单自动化审批开关, false:关闭自动化审批, true:开启
app-enabled: false # App工单自动化审批开关, false:关闭自动化审批, true:开启
account:
ldap:
enabled: false
url: ldap://127.0.0.1:389/
basedn: dc=tsign,dc=cn
factory: com.sun.jndi.ldap.LdapCtxFactory
filter: sAMAccountName
security:
authentication: simple
principal: cn=admin,dc=tsign,dc=cn
credentials: admin
auth-user-registration: true
auth-user-registration-role: normal
kcm:
enabled: false
s3:
endpoint: s3.didiyunapi.com
access-key: 1234567890
secret-key: 0987654321
bucket: logi-kafka
n9e:
base-url: http://127.0.0.1:8004
user-token: 12345678
timeout: 300
account: root
script-file: kcm_script.sh
monitor:
enabled: false
n9e:
nid: 2
user-token: 1234567890
mon:
base-url: http://127.0.0.1:8000 # 夜莺v4版本默认端口统一调整为了8000
sink:
base-url: http://127.0.0.1:8000 # 夜莺v4版本默认端口统一调整为了8000
rdb:
base-url: http://127.0.0.1:8000 # 夜莺v4版本默认端口统一调整为了8000
notify:
kafka:
cluster-id: 95
topic-name: didi-kafka-notify
order:
detail-url: http://127.0.0.1
test:
topic:
name1: moduleTest
name2: xgTest
name3: expandPartitionTopic
name4: topic_a
name5: NoOffsetChangeTopic
name6: _consumer_offsets
phyCluster:
id: 1
name: LogiKM_moduleTest
logicalCluster:
name: logical_cluster_1
broker:
id1: 1
id2: 2
id3: 3
app:
id: dkm_admin
ZK:
address: 127.0.0.1:2181
bootstrap-servers: 127.0.0.1:9093
gateway: 127.0.0.1
sasl-plaintext: SASL_PLAINTEXT://127.0.0.1:9093
admin: admin
consumer-group: moduleTestGroup
client-id: dkm_admin.moduleTest
region-name: region_1

Some files were not shown because too many files have changed in this diff Show More