Skip to content

Commit 9d4e117

Browse files
committed
Added unit tests for HealthCheckApi (#159).
1 parent 900c1a9 commit 9d4e117

File tree

7 files changed

+128
-22
lines changed

7 files changed

+128
-22
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ To utilize the GitLab API for Java in your project, simply add the following dep
1111
```java
1212
dependencies {
1313
...
14-
compile group: 'org.gitlab4j', name: 'gitlab4j-api', version: '4.8.11'
14+
compile group: 'org.gitlab4j', name: 'gitlab4j-api', version: '4.8.12'
1515
}
1616
```
1717

@@ -20,7 +20,7 @@ dependencies {
2020
<dependency>
2121
<groupId>org.gitlab4j</groupId>
2222
<artifactId>gitlab4j-api</artifactId>
23-
<version>4.8.11</version>
23+
<version>4.8.12</version>
2424
</dependency>
2525
```
2626

@@ -194,9 +194,9 @@ List<Group> groups = gitLabApi.getGroupApi().getGroups();
194194

195195
#### HealthCheckApi
196196
```java
197-
// Get the liveness endpoint health check results.
198-
// Assumes ip_whitelisted
199-
LivenessHealthCheck healthCheck = gitLabApi.getHealthCheckApi().getLiveness();
197+
// Get the liveness endpoint health check results. Assumes ip_whitelisted per:
198+
// https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html
199+
HealthCheckInfo healthCheck = gitLabApi.getHealthCheckApi().getLiveness();
200200
```
201201

202202
#### IssuesApi

example-test-gitlab4j.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@ TEST_BLOCK_USERNAME=
3434
TEST_PROXY_URI=
3535
TEST_PROXY_USERNAME=
3636
TEST_PROXY_PASSWORD=
37+
38+
# OPTIONAL: To thest the HealthCheckApi provide the health check token from the GitLab server
39+
TEST_HEALTH_CHECK_TOKEN=

src/main/java/org/gitlab4j/api/HealthCheckApi.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,29 @@
77
import java.net.URL;
88

99
public class HealthCheckApi extends AbstractApi {
10+
1011
public HealthCheckApi(GitLabApi gitLabApi) {
1112
super(gitLabApi);
1213
}
1314

1415
/**
1516
* Get Health Checks from the liveness endpoint.
1617
*
17-
* Requires ip_whitelist
18-
* https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html
18+
* Requires ip_whitelist, see the following link for more info:
19+
* See <a href="https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html">https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html</a>
1920
*
2021
* GET /-/liveness
2122
*
2223
* @return HealthCheckInfo instance
2324
* @throws GitLabApiException if any exception occurs
2425
*/
2526
public HealthCheckInfo getLiveness() throws GitLabApiException, IOException {
26-
URL livenessUrl = getApiClient().getUrlWithBase("-", "liveness");
27-
Response response = get(Response.Status.OK, null, livenessUrl);
28-
return (response.readEntity(HealthCheckInfo.class));
27+
return (getLiveness(null));
2928
}
3029

3130
/**
3231
* Get Health Checks from the liveness endpoint.
3332
*
34-
* Requires ip_whitelist
35-
* https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html
36-
*
3733
* GET /-/liveness
3834
*
3935
* @param token Health Status token
@@ -52,26 +48,21 @@ public HealthCheckInfo getLiveness(String token) throws GitLabApiException, IOEx
5248
/**
5349
* Get Health Checks from the readiness endpoint.
5450
*
55-
* Requires ip_whitelist
56-
* https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html
51+
* Requires ip_whitelist, see the following link for more info:
52+
* See <a href="https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html">https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html</a>
5753
*
5854
* GET /-/readiness
5955
*
6056
* @return HealthCheckInfo instance
6157
* @throws GitLabApiException if any exception occurs
6258
*/
6359
public HealthCheckInfo getReadiness() throws GitLabApiException, IOException {
64-
URL readinessUrl = getApiClient().getUrlWithBase("-", "readiness");
65-
Response response = get(Response.Status.OK, null, readinessUrl);
66-
return (response.readEntity(HealthCheckInfo.class));
60+
return (getReadiness(null));
6761
}
6862

6963
/**
7064
* Get Health Checks from the readiness endpoint.
7165
*
72-
* Requires ip_whitelist
73-
* https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html
74-
*
7566
* GET /-/readiness
7667
*
7768
* @param token Health Status token

src/main/java/org/gitlab4j/api/models/HealthCheckItem.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,4 @@ public String getMessage() {
3333
public void setMessage(String message) {
3434
this.message = message;
3535
}
36-
3736
}

src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.gitlab4j.api.models.Event;
4141
import org.gitlab4j.api.models.FileUpload;
4242
import org.gitlab4j.api.models.Group;
43+
import org.gitlab4j.api.models.HealthCheckInfo;
4344
import org.gitlab4j.api.models.ImpersonationToken;
4445
import org.gitlab4j.api.models.Issue;
4546
import org.gitlab4j.api.models.Job;
@@ -198,6 +199,17 @@ public void testGroup() {
198199
}
199200
}
200201

202+
@Test
203+
public void testHealthCheckInfo() {
204+
205+
try {
206+
HealthCheckInfo healthCheck = makeFakeApiCall(HealthCheckInfo.class, "health-check");
207+
assertTrue(compareJson(healthCheck, "health-check"));
208+
} catch (Exception e) {
209+
e.printStackTrace();
210+
}
211+
}
212+
201213
@Test
202214
public void testIssue() {
203215

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.gitlab4j.api;
2+
3+
import static org.junit.Assert.assertNotNull;
4+
import static org.junit.Assume.assumeTrue;
5+
6+
import java.io.IOException;
7+
import java.util.Map;
8+
9+
import org.gitlab4j.api.models.HealthCheckInfo;
10+
import org.gitlab4j.api.models.Version;
11+
import org.junit.Before;
12+
import org.junit.BeforeClass;
13+
import org.junit.Test;
14+
15+
/**
16+
* In order for these tests to run you must set the following properties in test-gitlab4j.properties
17+
*
18+
* TEST_HOST_URL
19+
* TEST_HEALTH_CHECK_TOKEN
20+
*
21+
* If any of the above are NULL, all tests in this class will be skipped.
22+
*
23+
*/
24+
public class TestHealthCheckApi {
25+
26+
// The following needs to be set to your test repository
27+
private static final String TEST_HOST_URL;
28+
private static final String TEST_HEALTH_CHECK_TOKEN;
29+
static {
30+
TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
31+
TEST_HEALTH_CHECK_TOKEN = TestUtils.getProperty("TEST_HEALTH_CHECK_TOKEN");
32+
}
33+
34+
private static GitLabApi gitLabApi;
35+
36+
public TestHealthCheckApi() {
37+
super();
38+
}
39+
40+
@BeforeClass
41+
public static void setup() {
42+
43+
String problems = "";
44+
if (TEST_HOST_URL == null || TEST_HOST_URL.trim().isEmpty()) {
45+
problems += "TEST_HOST_URL cannot be empty\n";
46+
}
47+
48+
if (TEST_HEALTH_CHECK_TOKEN == null || TEST_HEALTH_CHECK_TOKEN.trim().isEmpty()) {
49+
problems += "TEST_HEALTH_CHECK_TOKEN cannot be empty\n";
50+
}
51+
52+
if (problems.isEmpty()) {
53+
gitLabApi = new GitLabApi(TEST_HOST_URL, (String)null);
54+
} else {
55+
System.err.print(problems);
56+
}
57+
}
58+
59+
@Before
60+
public void beforeMethod() {
61+
assumeTrue(gitLabApi != null);
62+
}
63+
64+
@Test
65+
public void testLivelinessHealthCheck() throws GitLabApiException, IOException {
66+
@SuppressWarnings("deprecation")
67+
HealthCheckInfo liveness = gitLabApi.getHealthCheckApi().getLiveness(TEST_HEALTH_CHECK_TOKEN);
68+
assertNotNull(liveness);
69+
}
70+
71+
@Test
72+
public void testReadinessHealthCheck() throws GitLabApiException, IOException {
73+
@SuppressWarnings("deprecation")
74+
HealthCheckInfo liveliness = gitLabApi.getHealthCheckApi().getReadiness(TEST_HEALTH_CHECK_TOKEN);
75+
assertNotNull(liveliness);
76+
}
77+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"queues_check" : {
3+
"status" : "ok"
4+
},
5+
"redis_check" : {
6+
"status" : "ok"
7+
},
8+
"shared_state_check" : {
9+
"status" : "ok"
10+
},
11+
"fs_shards_check" : {
12+
"labels" : {
13+
"shard" : "default"
14+
},
15+
"status" : "ok"
16+
},
17+
"db_check" : {
18+
"status" : "failed",
19+
"message": "Problem with database."
20+
},
21+
"cache_check" : {
22+
"status" : "ok"
23+
}
24+
}

0 commit comments

Comments
 (0)