Skip to content

Commit 779b0dd

Browse files
committed
Add api health test
1 parent 8b2bc82 commit 779b0dd

File tree

4 files changed

+97
-11
lines changed

4 files changed

+97
-11
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>io.github.json031</groupId>
66
<artifactId>mcunittests</artifactId>
77
<packaging>jar</packaging>
8-
<version>1.0.8</version>
8+
<version>1.0.9</version>
99
<name>mcunittests</name>
1010
<description>mcunittests是SpringBoot项目的一个单元测试库,是一个在MIT许可下分发的开源项目</description>
1111
<url>https://github.com/Json031/mcunittests</url>

src/main/java/io/github/json031/apitests/MCApiTests.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,33 @@ public void testApiReturnsValidJson(String url,
7373
Assertions.fail("API did not respond valid json, url: " + url);
7474
} else {
7575
ResponseEntity<String> response = result.response;
76-
DataUnitTests.isValidJSON(response.getBody());
76+
DataUnitTests.isValidJSON(response);
7777
}
7878
}
7979

80+
81+
/**
82+
* 通用测试方法,验证 API 是否正常运行。
83+
*
84+
* @param url 完整的 API 地址(包括 http/https)
85+
* @param method 请求方式(GET / POST)
86+
* @param params 请求参数(POST body 或 GET 查询参数)
87+
* @param headers 请求头(可选)
88+
* @param verbose 是否打印响应
89+
*/
90+
public void testApiHealth(String url,
91+
HttpMethod method,
92+
Map<String, Object> params,
93+
Map<String, String> headers,
94+
long timeoutMillis,
95+
boolean verbose) {
96+
RequestUnitTestsResult result = RequestUnitTests.requestWitRestTemplate(url, method, params, headers, verbose);
97+
if (result == null) {
98+
Assertions.fail("API did not respond : " + url);
99+
} else {
100+
ResponseEntity<String> response = result.response;
101+
DataUnitTests.isValidJSON(response);
102+
DataUnitTests.withinTimeOut(result, timeoutMillis);
103+
}
104+
}
80105
}

src/main/java/io/github/json031/apitests/MCHighConcurrencyTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,13 @@ public HighConcurrencyResult highConcurrencyTestWithTimeoutMillis(String url,
5656
Map<String, String> headers,
5757
long timeoutMillis,
5858
boolean verbose) {
59+
//create an executor to manager api request thread
5960
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
61+
//the api request thread results
6062
List<Future<Long>> futures = new ArrayList<>();
63+
//thread safe for logging success count
6164
AtomicInteger successCount = new AtomicInteger();
65+
//thread safe for logging fail count
6266
AtomicInteger failCount = new AtomicInteger();
6367

6468
for (int i = 0; i < threadCount; i++) {

src/main/java/io/github/json031/unittests/DataUnitTests.java

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import io.github.json031.JavaBean.RequestUnitTestsResult;
55
import org.junit.jupiter.api.Assertions;
6+
import org.springframework.http.HttpHeaders;
7+
import org.springframework.http.MediaType;
8+
import org.springframework.http.ResponseEntity;
69

710
import java.net.URL;
811

@@ -13,17 +16,22 @@ public class DataUnitTests {
1316

1417
/**
1518
* 验证是否为有效json格式数据。
16-
* @param json 数据
19+
* @param response api请求结果
1720
* @return 是否为有效json格式数据。
1821
*/
19-
public static Boolean isValidJSON(String json) {
20-
// 用 Jackson 尝试解析 JSON
21-
ObjectMapper mapper = new ObjectMapper();
22-
try {
23-
mapper.readTree(json); // 验证数据是否为合法JSON格式数据
24-
return true;
25-
} catch (Exception e) {
26-
Assertions.fail("Invalid JSON data: " + json);
22+
public static <T> Boolean isValidJSON(ResponseEntity<T> response) {
23+
if (DataUnitTests.isJSONContentType(response)) {
24+
String json = (String) response.getBody();
25+
// 用 Jackson 尝试解析 JSON
26+
ObjectMapper mapper = new ObjectMapper();
27+
try {
28+
mapper.readTree(json); // 验证数据是否为合法JSON格式数据
29+
return true;
30+
} catch (Exception e) {
31+
Assertions.fail("Invalid JSON data: " + json);
32+
return false;
33+
}
34+
} else {
2735
return false;
2836
}
2937
}
@@ -69,4 +77,53 @@ public static Boolean isValidUrl(String url) {
6977
}
7078
}
7179

80+
/**
81+
* 获取getMediaType
82+
* @param result api请求结果
83+
* @return MediaType
84+
*/
85+
public static MediaType getMediaType(RequestUnitTestsResult result) {
86+
return DataUnitTests.getMediaType(result.response);
87+
}
88+
/**
89+
* 获取getMediaType
90+
* @param response api请求结果
91+
* @return MediaType
92+
*/
93+
public static <T> MediaType getMediaType(ResponseEntity<T> response) {
94+
HttpHeaders responseHeaders = response.getHeaders();
95+
MediaType contentType = responseHeaders.getContentType();
96+
return contentType;
97+
}
98+
/**
99+
* ContentType是否为application/json
100+
* @param response api请求结果
101+
* @return Boolean 是否为application/json
102+
*/
103+
public static <T> Boolean isJSONContentType(ResponseEntity<T> response) {
104+
MediaType contentType = DataUnitTests.getMediaType(response);
105+
return DataUnitTests.isJSONContentType(contentType);
106+
}
107+
/**
108+
* ContentType是否为application/json
109+
* @param result api请求结果
110+
* @return Boolean 是否为application/json
111+
*/
112+
public static Boolean isJSONContentType(RequestUnitTestsResult result) {
113+
MediaType contentType = DataUnitTests.getMediaType(result.response);
114+
return DataUnitTests.isJSONContentType(contentType);
115+
}
116+
/**
117+
* ContentType是否为application/json
118+
* @param contentType api请求结果response.headers.contentType
119+
* @return Boolean 是否为application/json
120+
*/
121+
public static Boolean isJSONContentType(MediaType contentType) {
122+
if (contentType != null) {
123+
if (MediaType.APPLICATION_JSON.includes(contentType)) {
124+
return true;
125+
}
126+
}
127+
return false;
128+
}
72129
}

0 commit comments

Comments
 (0)