Skip to content

Commit 7c0e9df

Browse files
authored
Merge pull request #479 from didi/dev
集成测试&单元测试补充
2 parents 734a020 + bd62212 commit 7c0e9df

File tree

171 files changed

+21563
-124
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

171 files changed

+21563
-124
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,4 @@ dist/
111111
dist/*
112112
kafka-manager-web/src/main/resources/templates/
113113
.DS_Store
114+
kafka-manager-console/package-lock.json
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
---
3+
4+
![kafka-manager-logo](../assets/images/common/logo_name.png)
5+
6+
**一站式`Apache Kafka`集群指标监控与运维管控平台**
7+
8+
---
9+
10+
11+
# LogiKM单元测试和集成测试
12+
13+
## 1、单元测试
14+
### 1.1 单元测试介绍
15+
单元测试又称模块测试,是针对软件设计的最小单位——程序模块进行正确性检验的测试工作。
16+
其目的在于检查每个程序单元能否正确实现详细设计说明中的模块功能、性能、接口和设计约束等要求,
17+
发现各模块内部可能存在的各种错误。单元测试需要从程序的内部结构出发设计测试用例。
18+
多个模块可以平行地独立进行单元测试。
19+
20+
### 1.2 LogiKM单元测试思路
21+
LogiKM单元测试思路主要是测试Service层的方法,通过罗列方法的各种参数,
22+
判断方法返回的结果是否符合预期。单元测试的基类加了@SpringBootTest注解,即每次运行单测用例都启动容器
23+
24+
### 1.3 LogiKM单元测试注意事项
25+
1. 单元测试用例在kafka-manager-core以及kafka-manager-extends下的test包中
26+
2. 配置在resources/application.yml,包括运行单元测试用例启用的数据库配置等等
27+
3. 编译打包项目时,加上参数-DskipTests可不执行测试用例,例如使用命令行mvn -DskipTests进行打包
28+
29+
30+
31+
32+
## 2、集成测试
33+
### 2.1 集成测试介绍
34+
集成测试又称组装测试,是一种黑盒测试。通常在单元测试的基础上,将所有的程序模块进行有序的、递增的测试。
35+
集成测试是检验程序单元或部件的接口关系,逐步集成为符合概要设计要求的程序部件或整个系统。
36+
37+
### 2.2 LogiKM集成测试思路
38+
LogiKM集成测试主要思路是对Controller层的接口发送Http请求。
39+
通过罗列测试用例,模拟用户的操作,对接口发送Http请求,判断结果是否达到预期。
40+
本地运行集成测试用例时,无需加@SpringBootTest注解(即无需每次运行测试用例都启动容器)
41+
42+
### 2.3 LogiKM集成测试注意事项
43+
1. 集成测试用例在kafka-manager-web的test包下
44+
2. 因为对某些接口发送Http请求需要先登陆,比较麻烦,可以绕过登陆,方法可见教程见docs -> user_guide -> call_api_bypass_login
45+
3. 集成测试的配置在resources/integrationTest-settings.properties文件下,包括集群地址,zk地址的配置等等
46+
4. 如果需要运行集成测试用例,需要本地先启动LogiKM项目
47+
5. 编译打包项目时,加上参数-DskipTests可不执行测试用例,例如使用命令行mvn -DskipTests进行打包

kafka-manager-common/src/main/java/com/xiaojukeji/kafka/manager/common/entity/Result.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,46 +82,46 @@ public String toString()
8282
return JSON.toJSONString(this);
8383
}
8484

85-
public static Result buildSuc() {
86-
Result result = new Result();
85+
public static <T> Result<T> buildSuc() {
86+
Result<T> result = new Result<>();
8787
result.setCode(ResultStatus.SUCCESS.getCode());
8888
result.setMessage(ResultStatus.SUCCESS.getMessage());
8989
return result;
9090
}
9191

9292
public static <T> Result<T> buildSuc(T data) {
93-
Result<T> result = new Result<T>();
93+
Result<T> result = new Result<>();
9494
result.setCode(ResultStatus.SUCCESS.getCode());
9595
result.setMessage(ResultStatus.SUCCESS.getMessage());
9696
result.setData(data);
9797
return result;
9898
}
9999

100100
public static <T> Result<T> buildGatewayFailure(String message) {
101-
Result<T> result = new Result<T>();
101+
Result<T> result = new Result<>();
102102
result.setCode(ResultStatus.GATEWAY_INVALID_REQUEST.getCode());
103103
result.setMessage(message);
104104
result.setData(null);
105105
return result;
106106
}
107107

108108
public static <T> Result<T> buildFailure(String message) {
109-
Result<T> result = new Result<T>();
109+
Result<T> result = new Result<>();
110110
result.setCode(ResultStatus.FAIL.getCode());
111111
result.setMessage(message);
112112
result.setData(null);
113113
return result;
114114
}
115115

116-
public static Result buildFrom(ResultStatus resultStatus) {
117-
Result result = new Result();
116+
public static <T> Result<T> buildFrom(ResultStatus resultStatus) {
117+
Result<T> result = new Result<>();
118118
result.setCode(resultStatus.getCode());
119119
result.setMessage(resultStatus.getMessage());
120120
return result;
121121
}
122122

123-
public static Result buildFrom(ResultStatus resultStatus, Object data) {
124-
Result result = new Result();
123+
public static <T> Result<T> buildFrom(ResultStatus resultStatus, T data) {
124+
Result<T> result = new Result<>();
125125
result.setCode(resultStatus.getCode());
126126
result.setMessage(resultStatus.getMessage());
127127
result.setData(data);

kafka-manager-common/src/main/java/com/xiaojukeji/kafka/manager/common/entity/dto/rd/LogicalClusterDTO.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,7 @@ public String toString() {
118118
}
119119

120120
public boolean legal() {
121-
if (ValidateUtils.isNull(clusterId)
122-
|| ValidateUtils.isNull(clusterId)
123-
|| ValidateUtils.isEmptyList(regionIdList)
124-
|| ValidateUtils.isNull(mode)) {
121+
if (ValidateUtils.isNull(clusterId) || ValidateUtils.isEmptyList(regionIdList) || ValidateUtils.isNull(mode)) {
125122
return false;
126123
}
127124
if (!ClusterModeEnum.SHARED_MODE.getCode().equals(mode) && ValidateUtils.isNull(appId)) {

kafka-manager-common/src/main/java/com/xiaojukeji/kafka/manager/common/entity/dto/rd/RegionDTO.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,7 @@ public String toString() {
9494
}
9595

9696
public boolean legal() {
97-
if (ValidateUtils.isNull(clusterId)
98-
|| ValidateUtils.isNull(clusterId)
99-
|| ValidateUtils.isEmptyList(brokerIdList)
100-
|| ValidateUtils.isNull(status)) {
97+
if (ValidateUtils.isNull(clusterId) || ValidateUtils.isEmptyList(brokerIdList) || ValidateUtils.isNull(status)) {
10198
return false;
10299
}
103100
description = ValidateUtils.isNull(description)? "": description;

kafka-manager-common/src/main/java/com/xiaojukeji/kafka/manager/common/utils/SpringTool.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.springframework.context.annotation.Lazy;
1414
import org.springframework.core.annotation.Order;
1515
import org.springframework.stereotype.Service;
16+
import org.springframework.web.context.request.RequestAttributes;
1617
import org.springframework.web.context.request.RequestContextHolder;
1718
import org.springframework.web.context.request.ServletRequestAttributes;
1819

@@ -81,16 +82,19 @@ public void destroy() throws Exception {
8182
}
8283

8384
public static String getUserName(){
84-
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
85-
8685
String username = null;
87-
if (TrickLoginConstant.TRICK_LOGIN_SWITCH_ON.equals(request.getHeader(TrickLoginConstant.TRICK_LOGIN_SWITCH))) {
88-
// trick登录方式的获取用户
89-
username = request.getHeader(TrickLoginConstant.TRICK_LOGIN_USER);
90-
} else {
91-
// 走页面登录方式登录的获取用户
92-
HttpSession session = request.getSession();
93-
username = (String) session.getAttribute(LoginConstant.SESSION_USERNAME_KEY);
86+
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
87+
if (!ValidateUtils.isNull(requestAttributes)) {
88+
HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
89+
90+
if (TrickLoginConstant.TRICK_LOGIN_SWITCH_ON.equals(request.getHeader(TrickLoginConstant.TRICK_LOGIN_SWITCH))) {
91+
// trick登录方式的获取用户
92+
username = request.getHeader(TrickLoginConstant.TRICK_LOGIN_USER);
93+
} else {
94+
// 走页面登录方式登录的获取用户
95+
HttpSession session = request.getSession();
96+
username = (String) session.getAttribute(LoginConstant.SESSION_USERNAME_KEY);
97+
}
9498
}
9599

96100
if (ValidateUtils.isNull(username)) {

kafka-manager-common/src/main/java/com/xiaojukeji/kafka/manager/common/zookeeper/znode/config/TopicQuotaData.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ public void setProducer_byte_rate(String producer_byte_rate) {
2929

3030
public static TopicQuotaData getClientData(Long producerByteRate, Long consumerByteRate) {
3131
TopicQuotaData clientData = new TopicQuotaData();
32-
if (!ValidateUtils.isNull(producerByteRate) && consumerByteRate != -1) {
32+
if (!ValidateUtils.isNull(consumerByteRate) && consumerByteRate != -1) {
3333
clientData.setConsumer_byte_rate(consumerByteRate.toString());
3434
}
35-
if (!ValidateUtils.isNull(consumerByteRate) && producerByteRate != -1) {
35+
if (!ValidateUtils.isNull(producerByteRate) && producerByteRate != -1) {
3636
clientData.setProducer_byte_rate(producerByteRate.toString());
3737
}
3838
return clientData;

kafka-manager-core/pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,23 @@
9595
<groupId>com.fasterxml.jackson.core</groupId>
9696
<artifactId>jackson-databind</artifactId>
9797
</dependency>
98+
99+
<!-- testng -->
100+
<dependency>
101+
<groupId>org.springframework.boot</groupId>
102+
<artifactId>spring-boot-starter-test</artifactId>
103+
<scope>test</scope>
104+
</dependency>
105+
<dependency>
106+
<groupId>org.springframework.boot</groupId>
107+
<artifactId>spring-boot-starter-web</artifactId>
108+
</dependency>
109+
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
110+
<dependency>
111+
<groupId>org.testng</groupId>
112+
<artifactId>testng</artifactId>
113+
<version>6.9.10</version>
114+
</dependency>
115+
98116
</dependencies>
99117
</project>

kafka-manager-core/src/main/java/com/xiaojukeji/kafka/manager/service/service/ConsumerService.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ public interface ConsumerService {
4242
*/
4343
List<String> getConsumerGroupConsumedTopicList(Long clusterId, String consumerGroup, String location);
4444

45+
/**
46+
* 获取消费者offset
47+
* @param clusterDO 集群
48+
* @param topicName topic
49+
* @param consumerGroup 消费组
50+
* @return Map<partitionId, offset>
51+
*/
4552
Map<Integer, Long> getConsumerOffset(ClusterDO clusterDO, String topicName, ConsumerGroup consumerGroup);
4653

4754
/**
@@ -52,7 +59,20 @@ List<Result> resetConsumerOffset(ClusterDO clusterDO,
5259
ConsumerGroup consumerGroup,
5360
List<PartitionOffsetDTO> partitionOffsetDTOList);
5461

62+
/**
63+
* 获取每个集群消费组的个数
64+
* @param clusterDOList 物理集群列表
65+
* @return Map<clusterId, consumerGroupNums>
66+
*/
5567
Map<Long, Integer> getConsumerGroupNumMap(List<ClusterDO> clusterDOList);
5668

69+
/**
70+
* 验证消费组是否存在
71+
* @param offsetLocation offset存放位置
72+
* @param id 集群id
73+
* @param topicName topic
74+
* @param consumerGroup 消费组
75+
* @return true:存在,false:不存在
76+
*/
5777
boolean checkConsumerGroupExist(OffsetLocationEnum offsetLocation, Long id, String topicName, String consumerGroup);
5878
}

kafka-manager-core/src/main/java/com/xiaojukeji/kafka/manager/service/service/RegionService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ public interface RegionService {
5454
Map<Integer, RegionDO> convert2BrokerIdRegionMap(List<RegionDO> regionDOList);
5555

5656
/**
57-
* 更新逻辑集群容量
58-
* @param clusterId 集群id
57+
* 根据RegionId更新Region
58+
* @param regionId region的id
5959
* @param newBrokerList 新的broker列表
6060
* @return ResultStatus
6161
*/
62-
ResultStatus updateRegion(Long clusterId, String newBrokerList);
62+
ResultStatus updateRegion(Long regionId, String newBrokerList);
6363

6464
/**
6565
* 获取空闲的region的broker列表

0 commit comments

Comments
 (0)