org.eclipse.microprofile.health.HealthCheck Java Examples

The following examples show how to use org.eclipse.microprofile.health.HealthCheck. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HealthCheckManager.java    From hammock with Apache License 2.0 6 votes vote down vote up
public HealthCheckModel performHealthChecks() {
    Instance<HealthCheck> healthChecks = this.healthChecks;
    if (requireAnnotation) {
        healthChecks = this.healthChecks.select(HealthLiteral.INSTANCE);
    }
    List<HealthCheck> healthCheckBeans = healthChecks.stream()
            .collect(toList());

    List<HealthResultModel> results = healthCheckBeans.stream()
            .map(HealthCheck::call)
            .map(r -> new HealthResultModel(r.getName(), r.getState().name(), r.getData().orElse(emptyMap())))
            .collect(toList());
    boolean anyDown = results.stream().anyMatch(r -> r.getState().equalsIgnoreCase(HealthCheckResponse.State.DOWN.name()));
    try {
        if (anyDown) {
            return toModel(HealthCheckResponse.State.DOWN, results);
        } else {
            return toModel(HealthCheckResponse.State.UP, results);
        }
    }
    finally {
        healthCheckBeans.forEach(healthChecks::destroy);
    }
}
 
Example #2
Source File: ExpectedBeansUnitTest.java    From quarkus with Apache License 2.0 6 votes vote down vote up
/**
 * Test metadata on HealthCheck procedure beans
 */
@Test
public void testHealthCheckMetadata() {
    Instance<HealthCheck> selects;

    selects = checks.select(Liveness.Literal.INSTANCE);
    Assertions.assertTrue(isUnique(selects));

    selects = checks.select(Readiness.Literal.INSTANCE);
    Assertions.assertTrue(isUnique(selects));

    selects = checks.select(HealthGroup.Literal.of("group1"));
    Assertions.assertTrue(isUnique(selects));

    selects = checks.select(HealthGroup.Literal.of("group2"));
    Assertions.assertTrue(isUnique(selects));

    selects = checks.select(Liveness.Literal.INSTANCE,
            Readiness.Literal.INSTANCE,
            HealthGroup.Literal.of("group1"),
            HealthGroup.Literal.of("group2"));
    Assertions.assertTrue(isUnique(selects));

    Assertions.assertTrue(checks.select(HealthGroup.Literal.of("group3")).isUnsatisfied());

}
 
Example #3
Source File: UrlHealthCheckTest.java    From smallrye-health with Apache License 2.0 5 votes vote down vote up
@Test
public void testUrlCheckIncorrectStatusCode() {
    final HealthCheck urlHealthCheck = new UrlHealthCheck("http://www.google.com")
            .statusCode(HttpURLConnection.HTTP_CREATED);
    final HealthCheckResponse healthCheckResponse = urlHealthCheck.call();

    assertEquals(HealthCheckResponse.State.DOWN, healthCheckResponse.getState());
    assertEquals("Expected response code 201 but actual is 200", healthCheckResponse.getData().get().get("error"));
}
 
Example #4
Source File: FailingUnitTest.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Test
public void testHealthBeans() {
    List<HealthCheck> check = new ArrayList<>();
    for (HealthCheck i : checks) {
        check.add(i);
    }
    assertEquals(1, check.size());
    assertEquals(HealthCheckResponse.State.DOWN, check.get(0).call().getState());
}
 
Example #5
Source File: DBHealthCheckTest.java    From trellis with Apache License 2.0 5 votes vote down vote up
@Test
void testHealthy() throws SQLException {
    final DataSource mockDataSource = mock(DataSource.class);
    final Connection mockConnection = mock(Connection.class);
    when(mockDataSource.getConnection()).thenReturn(mockConnection);
    when(mockConnection.isClosed()).thenReturn(false);

    final HealthCheck check = new DBHealthCheck(mockDataSource);
    assertEquals(HealthCheckResponse.State.UP, check.call().getState(), "Database connection isn't healthy!");
}
 
Example #6
Source File: DBHealthCheckTest.java    From trellis with Apache License 2.0 5 votes vote down vote up
@Test
void testThrowsUnhealthy() throws SQLException {
    final DataSource mockDataSource = mock(DataSource.class);
    doThrow(SQLException.class).when(mockDataSource).getConnection();

    final HealthCheck check = new DBHealthCheck(mockDataSource);
    assertEquals(HealthCheckResponse.State.DOWN, check.call().getState(), "Database connection isn't healthy!");
}
 
Example #7
Source File: SmallRyeHealthReporter.java    From smallrye-health with Apache License 2.0 5 votes vote down vote up
@Experimental("Asynchronous Health Check procedures and Health Groups")
public Uni<SmallRyeHealth> getHealthGroupsAsync() {
    List<Uni<HealthCheckResponse>> checks = new ArrayList<>();
    initUnis(checks, getHealthGroupsChecks(HealthCheck.class), getHealthGroupsChecks(AsyncHealthCheck.class));

    return getHealthAsync(checks);
}
 
Example #8
Source File: SmallRyeHealthReporter.java    From smallrye-health with Apache License 2.0 5 votes vote down vote up
private void initUnis(List<Uni<HealthCheckResponse>> list, Iterable<HealthCheck> checks,
        Iterable<AsyncHealthCheck> asyncChecks) {
    for (HealthCheck check : checks) {
        list.add(callSync(check));
    }

    for (AsyncHealthCheck asyncCheck : asyncChecks) {
        list.add(callAsync(asyncCheck));
    }
}
 
Example #9
Source File: DBHealthCheckTest.java    From trellis with Apache License 2.0 5 votes vote down vote up
@Test
void testUnhealthy() throws SQLException {
    final DataSource mockDataSource = mock(DataSource.class);
    final Connection mockConnection = mock(Connection.class);
    when(mockDataSource.getConnection()).thenReturn(mockConnection);
    when(mockConnection.isClosed()).thenReturn(true);

    final HealthCheck check = new DBHealthCheck(mockDataSource);
    assertEquals(HealthCheckResponse.State.DOWN, check.call().getState(),
            "Database connection doesn't report as unhealthy!");
}
 
Example #10
Source File: TriplestoreHealthCheckTest.java    From trellis with Apache License 2.0 5 votes vote down vote up
@Test
void testHealthy() {
    final Dataset dataset = rdf.createDataset();
    final RDFConnection rdfConnection = connect(wrap(toJena(dataset)));
    final HealthCheck check = new TriplestoreHealthCheck(rdfConnection);
    assertEquals(HealthCheckResponse.State.UP, check.call().getState(), "RDFConnection isn't healthy!");
}
 
Example #11
Source File: TriplestoreHealthCheckTest.java    From trellis with Apache License 2.0 5 votes vote down vote up
@Test
void testUnhealthy() {
    final Dataset dataset = rdf.createDataset();
    final RDFConnection rdfConnection = connect(wrap(toJena(dataset)));
    rdfConnection.close();
    final HealthCheck check = new TriplestoreHealthCheck(rdfConnection);
    assertEquals(HealthCheckResponse.State.DOWN, check.call().getState(),
            "Closed RDFConnection doesn't report as unhealthy!");
}
 
Example #12
Source File: SmallRyeHealthReporter.java    From smallrye-health with Apache License 2.0 4 votes vote down vote up
private Uni<HealthCheckResponse> callSync(HealthCheck healthCheck) {
    return withRecovery(healthCheck.getClass().getName(), Uni.createFrom().item(healthCheck::call));
}
 
Example #13
Source File: SmallRyeHealthReporter.java    From smallrye-health with Apache License 2.0 4 votes vote down vote up
public void addHealthCheck(HealthCheck check) {
    if (check != null) {
        additionalChecks.put(check.getClass().getName(), callSync(check));
        additionalListsChanged = true;
    }
}
 
Example #14
Source File: SmallRyeHealthReporter.java    From smallrye-health with Apache License 2.0 4 votes vote down vote up
public void removeHealthCheck(HealthCheck check) {
    additionalChecks.remove(check.getClass().getName());
    additionalListsChanged = true;
}
 
Example #15
Source File: TriplestoreHealthCheckTest.java    From trellis with Apache License 2.0 4 votes vote down vote up
@Test
void testUnhealthyDefault() {
    final HealthCheck check = new TriplestoreHealthCheck();
    assertEquals(HealthCheckResponse.State.DOWN, check.call().getState(), "RDFConnection isn't healthy!");
}
 
Example #16
Source File: DBHealthCheckTest.java    From trellis with Apache License 2.0 4 votes vote down vote up
@Test
void testUnhealthyDefault() {
    final HealthCheck check = new DBHealthCheck();
    assertEquals(HealthCheckResponse.State.DOWN, check.call().getState(), "Database connection isn't healthy!");
}
 
Example #17
Source File: KeycloakHealthChecks.java    From keycloak-extension-playground with Apache License 2.0 4 votes vote down vote up
@Produces
@Liveness
HealthCheck serverCheck() {
    return () -> KEYCLOAK_SERVER_HEALTH_CHECK.up().build();
}
 
Example #18
Source File: CDIProducedProcedureCheck.java    From microprofile-health with Apache License 2.0 4 votes vote down vote up
@Produces
@Readiness
HealthCheck cdiProducedReadinessCheck() {
    return () -> HealthCheckResponse.down("cdi-produced-failed-check");
}
 
Example #19
Source File: HealthCheckProducerDefaultScopeTest.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Readiness
HealthCheck alpha() {
    int idx = ALPHA_COUNTER.incrementAndGet();
    return () -> HealthCheckResponse.builder().up().name("alpha" + idx).build();
}
 
Example #20
Source File: HealthCheckProducerDefaultScopeTest.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Dependent
@Readiness
HealthCheck bravo() {
    int idx = BRAVO_COUNTER.incrementAndGet();
    return () -> HealthCheckResponse.builder().up().name("bravo" + idx).build();
}
 
Example #21
Source File: CDIProducedProcedureCheck.java    From microprofile-health with Apache License 2.0 4 votes vote down vote up
@Produces
@Liveness
HealthCheck cdiProducedLivenessCheck() {
    return () -> HealthCheckResponse.up("cdi-produced-successful-check");
}
 
Example #22
Source File: HealthStatusTest.java    From smallrye-health with Apache License 2.0 3 votes vote down vote up
@Test
public void testBooleanSupplierHealthStatus() {

    final HealthCheck myHealth = HealthStatus.state("myHealth", () -> true);

    final HealthCheckResponse healthCheckResponse = myHealth.call();

    assertThat(healthCheckResponse.getName(), is("myHealth"));
    assertThat(healthCheckResponse.getState(), is(HealthCheckResponse.State.UP));

}
 
Example #23
Source File: HealthStatusTest.java    From smallrye-health with Apache License 2.0 3 votes vote down vote up
@Test
public void testBooleanHealthStatus() {

    final HealthCheck myHealth = HealthStatus.state("myHealth", true);

    final HealthCheckResponse healthCheckResponse = myHealth.call();

    assertThat(healthCheckResponse.getName(), is("myHealth"));
    assertThat(healthCheckResponse.getState(), is(HealthCheckResponse.State.UP));

}
 
Example #24
Source File: HealthStatusTest.java    From smallrye-health with Apache License 2.0 3 votes vote down vote up
@Test
public void testRandomNameHealthStatus() {

    final HealthCheck healthCheck = HealthStatus.state(true);

    final HealthCheckResponse healthCheckResponse = healthCheck.call();

    assertThat(healthCheckResponse.getName(), startsWith("unnamed-health-check-"));
}
 
Example #25
Source File: HealthStatusTest.java    From smallrye-health with Apache License 2.0 3 votes vote down vote up
@Test
public void testHealthStatusDown() {

    final HealthCheck myHealth = HealthStatus.down("myHealth");

    final HealthCheckResponse healthCheckResponse = myHealth.call();

    assertThat(healthCheckResponse.getName(), is("myHealth"));
    assertThat(healthCheckResponse.getState(), is(HealthCheckResponse.State.DOWN));
}
 
Example #26
Source File: HealthStatusTest.java    From smallrye-health with Apache License 2.0 3 votes vote down vote up
@Test
public void testHealthStatusUp() {

    final HealthCheck myHealth = HealthStatus.up("myHealth");

    final HealthCheckResponse healthCheckResponse = myHealth.call();

    assertThat(healthCheckResponse.getName(), is("myHealth"));
    assertThat(healthCheckResponse.getState(), is(HealthCheckResponse.State.UP));
}
 
Example #27
Source File: KeycloakHealthChecks.java    From keycloak-extension-playground with Apache License 2.0 3 votes vote down vote up
@Produces
@Readiness
HealthCheck databaseCheck() {

    HealthCheckResponseBuilder databaseHealth = HealthCheckResponse.named("keycloak:database");

    boolean databaseReady = isDatabaseReady();

    return () -> (databaseReady ? databaseHealth.up() : databaseHealth.down()).build();
}
 
Example #28
Source File: HealthStatus.java    From smallrye-health with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a health check with given state and health check name.
 * 
 * @param name of the state.
 * @param supplier to get state.
 * @return Health check with given state and name.
 */
public static HealthCheck state(String name, BooleanSupplier supplier) {
    return state(name, supplier.getAsBoolean());
}
 
Example #29
Source File: HealthStatus.java    From smallrye-health with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a health check with state set from supplier and default health check name (health-check).
 * 
 * @param supplier to get state.
 * @return Health check with given state and default name.
 */
public static HealthCheck state(BooleanSupplier supplier) {
    return state(supplier.getAsBoolean());
}
 
Example #30
Source File: HealthStatus.java    From smallrye-health with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a health check with state down.
 * 
 * @param name of the health check
 * @return Health check with state down and given name.
 */
public static HealthCheck down(String name) {
    return state(name, false);
}