Repository: smallrye/smallrye-health (java)

Issue:
Create a HealthStatus helper class
Checking MicroProfile spec, in CDI section (https://github.com/eclipse/microprofile-health/blob/master/spec/src/main/asciidoc/java-api.adoc#integration-with-cdi) you'll see that they are using a class called HealthStatus. This class is not a spec class but a helper class created for the sake of simplifying the example.

But after using the spec a bit, I have arrived at the conclusion that a simple helper class for basic usage is more beneficial than having to deal with the builder all the time, for this reason, I implemented this simple class in Health extension to improve the developer experience.

PR is coming.

Pull request:
feat(#50): Adds HealthStatus helper class
Fixes #50 

Patch:
diff --git a/implementation/src/main/java/io/smallrye/health/HealthStatus.java b/implementation/src/main/java/io/smallrye/health/HealthStatus.java
new file mode 100644
index 0000000..236edad
--- /dev/null
+++ b/implementation/src/main/java/io/smallrye/health/HealthStatus.java
@@ -0,0 +1,72 @@
+package io.smallrye.health;
+
+import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.function.BooleanSupplier;
+import org.eclipse.microprofile.health.HealthCheck;
+import org.eclipse.microprofile.health.HealthCheckResponse;
+
+public class HealthStatus {
+
+    /**
+     * Creates a health check with state up.
+     * @param name of the health check
+     * @return Health check with state up and given name.
+     */
+    public static HealthCheck up(String name) {
+        return state(name, true);
+    }
+
+    /**
+     * 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);
+    }
+
+    /**
+     * 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());
+    }
+
+    /**
+     * Creates a health check with given state and default health check name (health-check).
+     * @param state
+     * @return Health check with given state and default name.
+     */
+    public static HealthCheck state(boolean state) {
+        return state(generateRandomHealthCheckName(), state);
+    }
+
+    /**
+     * 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());
+    }
+
+    /**
+     * Creates a health check with given state and health check name.
+     * @param name of the state.
+     * @param state
+     * @return Health check with given state and name.
+     */
+    public static HealthCheck state(String name, boolean state) {
+        return () -> HealthCheckResponse.named(name).state(state).build();
+    }
+
+    private static final String generateRandomHealthCheckName() {
+        int suffix = ThreadLocalRandom.current().nextInt(99999);
+        return String.format("unnamed-health-check-%05d", suffix);
+    }
+
+}


Test patch:
diff --git a/implementation/src/test/java/io/smallrye/health/HealthStatusTest.java b/implementation/src/test/java/io/smallrye/health/HealthStatusTest.java
new file mode 100644
index 0000000..67eb48f
--- /dev/null
+++ b/implementation/src/test/java/io/smallrye/health/HealthStatusTest.java
@@ -0,0 +1,70 @@
+package io.smallrye.health;
+
+import org.eclipse.microprofile.health.HealthCheck;
+import org.eclipse.microprofile.health.HealthCheckResponse;
+import org.hamcrest.CoreMatchers;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.startsWith;
+import static org.junit.Assert.assertThat;
+
+public class HealthStatusTest {
+
+    @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));
+    }
+
+    @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));
+    }
+
+    @Test
+    public void testRandomNameHealthStatus() {
+
+        final HealthCheck healthCheck = HealthStatus.state(true);
+
+        final HealthCheckResponse healthCheckResponse = healthCheck.call();
+
+        assertThat(healthCheckResponse.getName(), startsWith("unnamed-health-check-"));
+    }
+
+    @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));
+
+    }
+
+    @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));
+
+    }
+
+}