You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@causeway.apache.org by da...@apache.org on 2024/09/25 11:26:11 UTC
(causeway) branch CAUSEWAY-3813 created (now a7b2380ef2)
This is an automated email from the ASF dual-hosted git repository.
danhaywood pushed a change to branch CAUSEWAY-3813
in repository https://gitbox.apache.org/repos/asf/causeway.git
at a7b2380ef2 CAUSEWAY-3813: reworks HealthIndicatorUsingHealthCheckService
This branch includes the following new commits:
new a7b2380ef2 CAUSEWAY-3813: reworks HealthIndicatorUsingHealthCheckService
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(causeway) 01/01: CAUSEWAY-3813: reworks HealthIndicatorUsingHealthCheckService
Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
danhaywood pushed a commit to branch CAUSEWAY-3813
in repository https://gitbox.apache.org/repos/asf/causeway.git
commit a7b2380ef21672d598b8116569918afb116aab46
Author: Dan Haywood <da...@haywood-associates.co.uk>
AuthorDate: Wed Sep 25 12:26:03 2024 +0100
CAUSEWAY-3813: reworks HealthIndicatorUsingHealthCheckService
to allow there to be multiple implementations of HealthCheckService
---
.../HealthIndicatorUsingHealthCheckService.java | 41 ++++----
...HealthIndicatorUsingHealthCheckServiceTest.java | 106 +++++++++++++++++++++
2 files changed, 124 insertions(+), 23 deletions(-)
diff --git a/core/webapp/src/main/java/org/apache/causeway/core/webapp/health/HealthIndicatorUsingHealthCheckService.java b/core/webapp/src/main/java/org/apache/causeway/core/webapp/health/HealthIndicatorUsingHealthCheckService.java
index 4fd86b1779..3630368e6a 100644
--- a/core/webapp/src/main/java/org/apache/causeway/core/webapp/health/HealthIndicatorUsingHealthCheckService.java
+++ b/core/webapp/src/main/java/org/apache/causeway/core/webapp/health/HealthIndicatorUsingHealthCheckService.java
@@ -18,6 +18,10 @@
*/
package org.apache.causeway.core.webapp.health;
+import lombok.Builder;
+import lombok.val;
+
+import java.util.List;
import java.util.Optional;
import javax.inject.Inject;
@@ -31,44 +35,35 @@ import org.apache.causeway.applib.services.health.HealthCheckService;
import org.apache.causeway.applib.services.iactnlayer.InteractionService;
import org.apache.causeway.core.security.authentication.InteractionContextFactory;
-import lombok.val;
-
@Component
@Named("causeway.webapp.HealthCheckService") // logical name appears in the endpoint
public class HealthIndicatorUsingHealthCheckService extends AbstractHealthIndicator {
private final InteractionService interactionService;
- private final Optional<HealthCheckService> healthCheckServiceIfAny;
+ private final List<HealthCheckService> healthCheckServices;
+ @Builder
@Inject
public HealthIndicatorUsingHealthCheckService(
final InteractionService interactionService,
- final Optional<HealthCheckService> healthCheckServiceIfAny) {
+ final List<HealthCheckService> healthCheckServices) {
this.interactionService = interactionService;
- this.healthCheckServiceIfAny = healthCheckServiceIfAny;
+ this.healthCheckServices = healthCheckServices;
}
@Override
- protected void doHealthCheck(Health.Builder builder) throws Exception {
- val health = healthCheckServiceIfAny
- .map(healthCheckService ->
- interactionService
- .call(InteractionContextFactory.health(), healthCheckService::check))
- .orElse(null);
- if(health != null) {
- final boolean result = health.getResult();
- if(result) {
- builder.up();
- } else {
- final Throwable cause = health.getCause();
- if(cause != null) {
- builder.down(cause);
- } else {
- builder.down();
+ protected void doHealthCheck(Health.Builder builder) {
+ for (HealthCheckService healthCheckService : healthCheckServices) {
+ org.apache.causeway.applib.services.health.Health health = interactionService.call(InteractionContextFactory.health(), healthCheckService::check);
+ if (health != null) {
+ val success = health.getResult();
+ if(! success) {
+ Optional.ofNullable(health.getCause())
+ .ifPresentOrElse(ex -> builder.down(ex), () -> builder.down());
+ return;
}
}
- } else {
- builder.unknown();
}
+ builder.up();
}
}
diff --git a/core/webapp/src/test/java/org/apache/causeway/core/webapp/health/HealthIndicatorUsingHealthCheckServiceTest.java b/core/webapp/src/test/java/org/apache/causeway/core/webapp/health/HealthIndicatorUsingHealthCheckServiceTest.java
new file mode 100644
index 0000000000..d3a067bc3d
--- /dev/null
+++ b/core/webapp/src/test/java/org/apache/causeway/core/webapp/health/HealthIndicatorUsingHealthCheckServiceTest.java
@@ -0,0 +1,106 @@
+package org.apache.causeway.core.webapp.health;
+
+import org.apache.causeway.applib.services.health.HealthCheckService;
+import org.apache.causeway.applib.services.iactnlayer.InteractionService;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import org.springframework.boot.actuate.health.Health;
+
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.*;
+
+@ExtendWith(MockitoExtension.class)
+class HealthIndicatorUsingHealthCheckServiceTest {
+
+ @Mock private InteractionService mockInteractionService;
+ @Mock private HealthCheckService mockHealthCheckService1;
+ @Mock private HealthCheckService mockHealthCheckService2;
+
+ private org.apache.causeway.applib.services.health.Health causewayHealth;
+
+ @InjectMocks
+ private HealthIndicatorUsingHealthCheckService healthIndicator;
+
+ @BeforeEach
+ void setUp() {
+ healthIndicator = HealthIndicatorUsingHealthCheckService.builder()
+ .interactionService(mockInteractionService)
+ .healthCheckServices(List.of(mockHealthCheckService1, mockHealthCheckService2))
+ .build();
+ }
+
+ @Test
+ void shouldReturnUpWhenAllHealthChecksPass() {
+ // Given
+ causewayHealth = org.apache.causeway.applib.services.health.Health.ok();
+ when(mockInteractionService.call(any(), any())).thenReturn(causewayHealth);
+
+ Health.Builder builder = new Health.Builder();
+
+ // When
+ healthIndicator.doHealthCheck(builder);
+
+ // Then
+ Health health = builder.build();
+ assertEquals(Health.up().build(), health);
+ verify(mockInteractionService, times(2)).call(any(), any());
+ }
+
+ @Test
+ void shouldReturnDownWhenAnyHealthCheckFailsWithoutException() {
+ // Given
+ causewayHealth = org.apache.causeway.applib.services.health.Health.error("no cause");
+ when(mockInteractionService.call(any(), any())).thenReturn(causewayHealth);
+
+ Health.Builder builder = new Health.Builder();
+
+ // When
+ healthIndicator.doHealthCheck(builder);
+
+ // Then
+ Health health = builder.build();
+ assertEquals(Health.down().build(), health);
+ verify(mockInteractionService, times(1)).call(any(), any());
+ }
+
+ @Test
+ void shouldReturnDownWhenAnyHealthCheckFailsWithException() {
+ // Given
+ Exception exception = new RuntimeException("Database connection failed");
+ causewayHealth = org.apache.causeway.applib.services.health.Health.error(exception);
+ when(mockInteractionService.call(any(), any())).thenReturn(causewayHealth);
+
+ Health.Builder builder = new Health.Builder();
+
+ // When
+ healthIndicator.doHealthCheck(builder);
+
+ // Then
+ Health health = builder.build();
+ assertEquals(Health.down(exception).build(), health);
+ verify(mockInteractionService, times(1)).call(any(), any());
+ }
+
+ @Test
+ void shouldReturnUpWhenHealthCheckServiceIsEmpty() {
+ // Given
+ healthIndicator = new HealthIndicatorUsingHealthCheckService(mockInteractionService, List.of());
+ Health.Builder builder = new Health.Builder();
+
+ // When
+ healthIndicator.doHealthCheck(builder);
+
+ // Then
+ Health health = builder.build();
+ assertEquals(Health.up().build(), health);
+ verifyNoInteractions(mockInteractionService);
+ }
+}