Skip to content

Commit a63321f

Browse files
committed
refactor: Simplify health-probe null handling
1 parent cbc58bd commit a63321f

2 files changed

Lines changed: 4 additions & 9 deletions

File tree

src/main/java/com/retailsvc/http/Handlers.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,7 @@ public static HttpHandler healthHandler(Supplier<HealthOutcome> probe) {
8989
try (exchange) {
9090
HealthOutcome outcome;
9191
try {
92-
HealthOutcome result = probe.get();
93-
if (result == null) {
94-
throw new IllegalStateException("Health probe returned null");
95-
}
96-
outcome = result;
92+
outcome = Objects.requireNonNull(probe.get(), "Health probe returned null");
9793
} catch (RuntimeException e) {
9894
LOG.warn("Health probe failed", e);
9995
outcome = new HealthOutcome("Down", List.of());

src/test/java/com/retailsvc/http/HealthHandlerTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,12 @@ void headIsAccepted() throws IOException {
7171
HttpExchange ex = newExchange("HEAD");
7272
Headers headers = new Headers();
7373
when(ex.getResponseHeaders()).thenReturn(headers);
74-
when(ex.getResponseBody()).thenReturn(new ByteArrayOutputStream());
74+
ByteArrayOutputStream body = new ByteArrayOutputStream();
75+
when(ex.getResponseBody()).thenReturn(body);
7576

7677
Handlers.healthHandler(() -> new HealthOutcome("Up", List.of())).handle(ex);
7778

78-
verify(ex)
79-
.sendResponseHeaders(
80-
eq(HTTP_OK), eq((long) "{\"outcome\":\"Up\",\"dependencies\":[]}".length()));
79+
verify(ex).sendResponseHeaders(eq(HTTP_OK), eq((long) body.size()));
8180
}
8281

8382
@Test

0 commit comments

Comments
 (0)