Commit 9f75e802 authored by Greg Messner's avatar Greg Messner
Browse files

Added unit tests for HealthCheckApi (#159).

parent 900c1a91
......@@ -11,7 +11,7 @@ To utilize the GitLab API for Java in your project, simply add the following dep
```java
dependencies {
...
compile group: 'org.gitlab4j', name: 'gitlab4j-api', version: '4.8.11'
compile group: 'org.gitlab4j', name: 'gitlab4j-api', version: '4.8.12'
}
```
......@@ -20,7 +20,7 @@ dependencies {
<dependency>
<groupId>org.gitlab4j</groupId>
<artifactId>gitlab4j-api</artifactId>
<version>4.8.11</version>
<version>4.8.12</version>
</dependency>
```
......@@ -194,9 +194,9 @@ List<Group> groups = gitLabApi.getGroupApi().getGroups();
#### HealthCheckApi
```java
// Get the liveness endpoint health check results.
// Assumes ip_whitelisted
LivenessHealthCheck healthCheck = gitLabApi.getHealthCheckApi().getLiveness();
// Get the liveness endpoint health check results. Assumes ip_whitelisted per:
// https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html
HealthCheckInfo healthCheck = gitLabApi.getHealthCheckApi().getLiveness();
```
#### IssuesApi
......
......@@ -34,3 +34,6 @@ TEST_BLOCK_USERNAME=
TEST_PROXY_URI=
TEST_PROXY_USERNAME=
TEST_PROXY_PASSWORD=
# OPTIONAL: To thest the HealthCheckApi provide the health check token from the GitLab server
TEST_HEALTH_CHECK_TOKEN=
......@@ -7,6 +7,7 @@ import java.io.IOException;
import java.net.URL;
public class HealthCheckApi extends AbstractApi {
public HealthCheckApi(GitLabApi gitLabApi) {
super(gitLabApi);
}
......@@ -14,26 +15,21 @@ public class HealthCheckApi extends AbstractApi {
/**
* Get Health Checks from the liveness endpoint.
*
* Requires ip_whitelist
* https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html
* Requires ip_whitelist, see the following link for more info:
* See <a href="https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html">https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html</a>
*
* GET /-/liveness
*
* @return HealthCheckInfo instance
* @throws GitLabApiException if any exception occurs
*/
public HealthCheckInfo getLiveness() throws GitLabApiException, IOException {
URL livenessUrl = getApiClient().getUrlWithBase("-", "liveness");
Response response = get(Response.Status.OK, null, livenessUrl);
return (response.readEntity(HealthCheckInfo.class));
public HealthCheckInfo getLiveness() throws GitLabApiException {
return (getLiveness(null));
}
/**
* Get Health Checks from the liveness endpoint.
*
* Requires ip_whitelist
* https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html
*
* GET /-/liveness
*
* @param token Health Status token
......@@ -41,37 +37,35 @@ public class HealthCheckApi extends AbstractApi {
* @throws GitLabApiException if any exception occurs
* @deprecated
*/
public HealthCheckInfo getLiveness(String token) throws GitLabApiException, IOException {
URL livenessUrl = getApiClient().getUrlWithBase("-", "liveness");
GitLabApiForm formData = new GitLabApiForm()
.withParam("token", token, false);
Response response = get(Response.Status.OK, formData.asMap(), livenessUrl);
return (response.readEntity(HealthCheckInfo.class));
public HealthCheckInfo getLiveness(String token) throws GitLabApiException {
try {
URL livenessUrl = getApiClient().getUrlWithBase("-", "liveness");
GitLabApiForm formData = new GitLabApiForm().withParam("token", token, false);
Response response = get(Response.Status.OK, formData.asMap(), livenessUrl);
return (response.readEntity(HealthCheckInfo.class));
} catch (IOException ioe) {
throw (new GitLabApiException(ioe));
}
}
/**
* Get Health Checks from the readiness endpoint.
*
* Requires ip_whitelist
* https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html
* Requires ip_whitelist, see the following link for more info:
* See <a href="https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html">https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html</a>
*
* GET /-/readiness
*
* @return HealthCheckInfo instance
* @throws GitLabApiException if any exception occurs
*/
public HealthCheckInfo getReadiness() throws GitLabApiException, IOException {
URL readinessUrl = getApiClient().getUrlWithBase("-", "readiness");
Response response = get(Response.Status.OK, null, readinessUrl);
return (response.readEntity(HealthCheckInfo.class));
public HealthCheckInfo getReadiness() throws GitLabApiException {
return (getReadiness(null));
}
/**
* Get Health Checks from the readiness endpoint.
*
* Requires ip_whitelist
* https://docs.gitlab.com/ee/administration/monitoring/ip_whitelist.html
*
* GET /-/readiness
*
* @param token Health Status token
......@@ -79,11 +73,14 @@ public class HealthCheckApi extends AbstractApi {
* @throws GitLabApiException if any exception occurs
* @deprecated
*/
public HealthCheckInfo getReadiness(String token) throws GitLabApiException, IOException {
URL readinessUrl = getApiClient().getUrlWithBase("-", "readiness");
GitLabApiForm formData = new GitLabApiForm()
.withParam("token", token, false);
Response response = get(Response.Status.OK, formData.asMap(), readinessUrl);
return (response.readEntity(HealthCheckInfo.class));
public HealthCheckInfo getReadiness(String token) throws GitLabApiException {
try {
URL readinessUrl = getApiClient().getUrlWithBase("-", "readiness");
GitLabApiForm formData = new GitLabApiForm().withParam("token", token, false);
Response response = get(Response.Status.OK, formData.asMap(), readinessUrl);
return (response.readEntity(HealthCheckInfo.class));
} catch (IOException ioe) {
throw (new GitLabApiException(ioe));
}
}
}
......@@ -33,5 +33,4 @@ public class HealthCheckItem {
public void setMessage(String message) {
this.message = message;
}
}
......@@ -40,6 +40,7 @@ import org.gitlab4j.api.models.Diff;
import org.gitlab4j.api.models.Event;
import org.gitlab4j.api.models.FileUpload;
import org.gitlab4j.api.models.Group;
import org.gitlab4j.api.models.HealthCheckInfo;
import org.gitlab4j.api.models.ImpersonationToken;
import org.gitlab4j.api.models.Issue;
import org.gitlab4j.api.models.Job;
......@@ -198,6 +199,17 @@ public class TestGitLabApiBeans {
}
}
@Test
public void testHealthCheckInfo() {
try {
HealthCheckInfo healthCheck = makeFakeApiCall(HealthCheckInfo.class, "health-check");
assertTrue(compareJson(healthCheck, "health-check"));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testIssue() {
......
package org.gitlab4j.api;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assume.assumeTrue;
import org.gitlab4j.api.models.HealthCheckInfo;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* In order for these tests to run you must set the following properties in test-gitlab4j.properties
*
* TEST_HOST_URL
* TEST_HEALTH_CHECK_TOKEN
*
* If any of the above are NULL, all tests in this class will be skipped.
*
*/
public class TestHealthCheckApi {
// The following needs to be set to your test repository
private static final String TEST_HOST_URL;
private static final String TEST_HEALTH_CHECK_TOKEN;
static {
TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
TEST_HEALTH_CHECK_TOKEN = TestUtils.getProperty("TEST_HEALTH_CHECK_TOKEN");
}
private static GitLabApi gitLabApi;
public TestHealthCheckApi() {
super();
}
@BeforeClass
public static void setup() {
String problems = "";
if (TEST_HOST_URL == null || TEST_HOST_URL.trim().isEmpty()) {
problems += "TEST_HOST_URL cannot be empty\n";
}
if (TEST_HEALTH_CHECK_TOKEN == null || TEST_HEALTH_CHECK_TOKEN.trim().isEmpty()) {
problems += "TEST_HEALTH_CHECK_TOKEN cannot be empty\n";
}
if (problems.isEmpty()) {
gitLabApi = new GitLabApi(TEST_HOST_URL, (String)null);
} else {
System.err.print(problems);
}
}
@Before
public void beforeMethod() {
assumeTrue(gitLabApi != null);
}
@Test
public void testLivelinessHealthCheck() throws GitLabApiException {
@SuppressWarnings("deprecation")
HealthCheckInfo liveness = gitLabApi.getHealthCheckApi().getLiveness(TEST_HEALTH_CHECK_TOKEN);
assertNotNull(liveness);
}
@Test
public void testReadinessHealthCheck() throws GitLabApiException {
@SuppressWarnings("deprecation")
HealthCheckInfo readiness = gitLabApi.getHealthCheckApi().getReadiness(TEST_HEALTH_CHECK_TOKEN);
assertNotNull(readiness);
}
}
{
"queues_check" : {
"status" : "ok"
},
"redis_check" : {
"status" : "ok"
},
"shared_state_check" : {
"status" : "ok"
},
"fs_shards_check" : {
"labels" : {
"shard" : "default"
},
"status" : "ok"
},
"db_check" : {
"status" : "failed",
"message": "Problem with database."
},
"cache_check" : {
"status" : "ok"
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment