ClusterAwarenessHealthIndicator implements HealthIndicator { @Override public Health health() { final var optClusterState = clusterStateSupplier.get(); if (optClusterState.isEmpty()) { return Health.down().build(); } else { if (optClusterState.get().getBrokers().isEmpty()) { return Health.down().build(); } else { return Health.up().build(); } } } ClusterAwarenessHealthIndicator(
      final Supplier<Optional<BrokerClusterState>> clusterStateSupplier); @Override Health health();  }
@Test public void shouldReportDownIfSupplierReturnsEmpty() { final Supplier<Optional<BrokerClusterState>> stateSupplier = () -> Optional.empty(); final var sutHealthIndicator = new ClusterAwarenessHealthIndicator(stateSupplier); final var actualHealth = sutHealthIndicator.health(); assertThat(actualHealth).isNotNull(); assertThat(actualHealth.getStatus()).isEqualTo(Status.DOWN); }
@Test public void shouldReportUpIfListOfBrokersIsNotEmpty() { final BrokerClusterState mockClusterState = mock(BrokerClusterState.class); when(mockClusterState.getBrokers()).thenReturn(List.of(1)); final Supplier<Optional<BrokerClusterState>> stateSupplier = () -> Optional.of(mockClusterState); final var sutHealthIndicator = new ClusterAwarenessHealthIndicator(stateSupplier); final var actualHealth = sutHealthIndicator.health(); assertThat(actualHealth).isNotNull(); assertThat(actualHealth.getStatus()).isEqualTo(Status.UP); }
@Test public void shouldReportDownIfListOfBrokersIsEmpty() { final BrokerClusterState mockClusterState = mock(BrokerClusterState.class); when(mockClusterState.getBrokers()).thenReturn(emptyList()); final Supplier<Optional<BrokerClusterState>> stateSupplier = () -> Optional.of(mockClusterState); final var sutHealthIndicator = new ClusterAwarenessHealthIndicator(stateSupplier); final var actualHealth = sutHealthIndicator.health(); assertThat(actualHealth).isNotNull(); assertThat(actualHealth.getStatus()).isEqualTo(Status.DOWN); }