Skip to content

Commit 5fe3b7d

Browse files
committed
feat: Add HealthOutcome record for health responses
1 parent 52f5b56 commit 5fe3b7d

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.retailsvc.http;
2+
3+
import java.util.List;
4+
import java.util.Objects;
5+
6+
/**
7+
* Wire-shape carrier for the {@link Handlers#healthHandler health handler} response.
8+
*
9+
* <p>The record owns the JSON shape on the wire — {@code {"outcome": "...", "dependencies": [
10+
* {"id": "...", "status": "..."} ]}}. Construct it from whatever check-running mechanism the caller
11+
* prefers; this library has no opinion.
12+
*
13+
* @param outcome overall outcome; {@code "Up"} (case-insensitive) means healthy
14+
* @param dependencies per-dependency statuses; {@code null} is normalised to an empty list
15+
*/
16+
public record HealthOutcome(String outcome, List<Dependency> dependencies) {
17+
18+
public HealthOutcome {
19+
Objects.requireNonNull(outcome, "outcome");
20+
dependencies = List.copyOf(Objects.requireNonNullElse(dependencies, List.of()));
21+
}
22+
23+
/** Returns {@code true} when {@link #outcome()} equals {@code "Up"} ignoring case. */
24+
public boolean isUp() {
25+
return "Up".equalsIgnoreCase(outcome);
26+
}
27+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.retailsvc.http;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import org.junit.jupiter.api.Test;
9+
10+
class HealthOutcomeTest {
11+
12+
@Test
13+
void exposesOutcomeAndDependencies() {
14+
HealthOutcome o = new HealthOutcome("Up", List.of(new Dependency("jdbc", "Up")));
15+
assertThat(o.outcome()).isEqualTo("Up");
16+
assertThat(o.dependencies()).containsExactly(new Dependency("jdbc", "Up"));
17+
}
18+
19+
@Test
20+
void rejectsNullOutcome() {
21+
assertThatNullPointerException()
22+
.isThrownBy(() -> new HealthOutcome(null, List.of()))
23+
.withMessageContaining("outcome");
24+
}
25+
26+
@Test
27+
void coercesNullDependenciesToEmpty() {
28+
HealthOutcome o = new HealthOutcome("Up", null);
29+
assertThat(o.dependencies()).isEmpty();
30+
}
31+
32+
@Test
33+
void copiesDependencyListDefensively() {
34+
List<Dependency> mutable = new ArrayList<>();
35+
mutable.add(new Dependency("jdbc", "Up"));
36+
HealthOutcome o = new HealthOutcome("Up", mutable);
37+
mutable.clear();
38+
assertThat(o.dependencies()).hasSize(1);
39+
}
40+
41+
@Test
42+
void isUpReturnsTrueForUpCaseInsensitive() {
43+
assertThat(new HealthOutcome("Up", List.of()).isUp()).isTrue();
44+
assertThat(new HealthOutcome("UP", List.of()).isUp()).isTrue();
45+
assertThat(new HealthOutcome("up", List.of()).isUp()).isTrue();
46+
}
47+
48+
@Test
49+
void isUpReturnsFalseForAnythingElse() {
50+
assertThat(new HealthOutcome("Down", List.of()).isUp()).isFalse();
51+
assertThat(new HealthOutcome("", List.of()).isUp()).isFalse();
52+
assertThat(new HealthOutcome("Degraded", List.of()).isUp()).isFalse();
53+
}
54+
}

0 commit comments

Comments
 (0)