Skip to content

Commit 9f75e80

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

File tree

7 files changed

+144
-36
lines changed

7 files changed

+144
-36
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: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,83 +7,80 @@
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
*/
25-
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));
26+
public HealthCheckInfo getLiveness() throws GitLabApiException {
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
4036
* @return HealthCheckInfo instance
4137
* @throws GitLabApiException if any exception occurs
4238
* @deprecated
4339
*/
44-
public HealthCheckInfo getLiveness(String token) throws GitLabApiException, IOException {
45-
URL livenessUrl = getApiClient().getUrlWithBase("-", "liveness");
46-
GitLabApiForm formData = new GitLabApiForm()
47-
.withParam("token", token, false);
48-
Response response = get(Response.Status.OK, formData.asMap(), livenessUrl);
49-
return (response.readEntity(HealthCheckInfo.class));
40+
public HealthCheckInfo getLiveness(String token) throws GitLabApiException {
41+
try {
42+
URL livenessUrl = getApiClient().getUrlWithBase("-", "liveness");
43+
GitLabApiForm formData = new GitLabApiForm().withParam("token", token, false);
44+
Response response = get(Response.Status.OK, formData.asMap(), livenessUrl);
45+
return (response.readEntity(HealthCheckInfo.class));
46+
} catch (IOException ioe) {
47+
throw (new GitLabApiException(ioe));
48+
}
5049
}
5150

5251
/**
5352
* Get Health Checks from the readiness endpoint.
5453
*
55-
* Requires ip_whitelist
56-
* https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html
54+
* Requires ip_whitelist, see the following link for more info:
55+
* See <a href="https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html">https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html</a>
5756
*
5857
* GET /-/readiness
5958
*
6059
* @return HealthCheckInfo instance
6160
* @throws GitLabApiException if any exception occurs
6261
*/
63-
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));
62+
public HealthCheckInfo getReadiness() throws GitLabApiException {
63+
return (getReadiness(null));
6764
}
6865

6966
/**
7067
* Get Health Checks from the readiness endpoint.
7168
*
72-
* Requires ip_whitelist
73-
* https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html
74-
*
7569
* GET /-/readiness
7670
*
7771
* @param token Health Status token
7872
* @return HealthCheckInfo instance
7973
* @throws GitLabApiException if any exception occurs
8074
* @deprecated
8175
*/
82-
public HealthCheckInfo getReadiness(String token) throws GitLabApiException, IOException {
83-
URL readinessUrl = getApiClient().getUrlWithBase("-", "readiness");
84-
GitLabApiForm formData = new GitLabApiForm()
85-
.withParam("token", token, false);
86-
Response response = get(Response.Status.OK, formData.asMap(), readinessUrl);
87-
return (response.readEntity(HealthCheckInfo.class));
76+
public HealthCheckInfo getReadiness(String token) throws GitLabApiException {
77+
try {
78+
URL readinessUrl = getApiClient().getUrlWithBase("-", "readiness");
79+
GitLabApiForm formData = new GitLabApiForm().withParam("token", token, false);
80+
Response response = get(Response.Status.OK, formData.asMap(), readinessUrl);
81+
return (response.readEntity(HealthCheckInfo.class));
82+
} catch (IOException ioe) {
83+
throw (new GitLabApiException(ioe));
84+
}
8885
}
8986
}

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