@Override public ClusterHealth checkCluster() { checkState(!webServer.isStandalone(), "Clustering is not enabled"); checkState(sharedHealthState != null, "HealthState instance can't be null when clustering is enabled"); Set<NodeHealth> nodeHealths = sharedHealthState.readAll(); Health health = clusterHealthChecks.stream() .map(clusterHealthCheck -> clusterHealthCheck.check(nodeHealths)) .reduce(Health.GREEN, HealthReducer.INSTANCE); return new ClusterHealth(health, nodeHealths); }
@Before public void before() throws InterruptedException { when(globalInfoLoader.load()).thenReturn(Collections.singletonList(createSection("globalInfo"))); when(appNodesInfoLoader.load()).thenReturn(Collections.singletonList(createNodeInfo("appNodes"))); when(searchNodesInfoLoader.load()).thenReturn(Collections.singletonList(createNodeInfo("searchNodes"))); Health health = Health.newHealthCheckBuilder().setStatus(Health.Status.GREEN).build(); when(healthChecker.checkCluster()).thenReturn(new ClusterHealth(health, Collections.emptySet())); }
@Test public void constructor_fails_with_NPE_if_Health_is_null() { expectedException.expect(NullPointerException.class); expectedException.expectMessage("health can't be null"); new ClusterHealth(null, Collections.emptySet()); }
@Test public void verify_toString() { Health health = randomHealth(); Set<NodeHealth> nodeHealths = randomNodeHealths(); ClusterHealth underTest = new ClusterHealth(health, nodeHealths); assertThat(underTest.toString()).isEqualTo("ClusterHealth{health=" + health + ", nodes=" + nodeHealths + "}"); }
@Test public void hashcode_is_based_on_content() { Health health = randomHealth(); Set<NodeHealth> nodeHealths = randomNodeHealths(); ClusterHealth underTest = new ClusterHealth(health, nodeHealths); assertThat(underTest.hashCode()).isEqualTo(underTest.hashCode()); }
@Test public void equals_is_based_on_content() { Health health = randomHealth(); Set<NodeHealth> nodeHealths = randomNodeHealths(); ClusterHealth underTest = new ClusterHealth(health, nodeHealths); assertThat(underTest) .isEqualTo(underTest) .isEqualTo(new ClusterHealth(health, nodeHealths)) .isNotEqualTo(new Object()) .isNotEqualTo(null) .isNotEqualTo(new ClusterHealth( newHealthCheckBuilder() .setStatus(health.getStatus()) .addCause("foo_bar") .build(), randomNodeHealths())) .isNotEqualTo(new ClusterHealth( health, concat(nodeHealths.stream(), Stream.of(randomNodeHealth())).collect(toSet()))); }
private ClusterHealth randomStatusMinimalClusterHealth() { return new ClusterHealth(newHealthCheckBuilder() .setStatus(Health.Status.values()[random.nextInt(Health.Status.values().length)]) .build(), emptySet()); }
@Test public void constructor_fails_with_NPE_if_NodeHealth_is_null() { expectedException.expect(NullPointerException.class); expectedException.expectMessage("nodes can't be null"); new ClusterHealth(Health.GREEN, null); }
@Test public void test_getNodeHealth() { Health health = randomHealth(); Set<NodeHealth> nodeHealths = new HashSet<>(Arrays.asList(newNodeHealth("foo"), newNodeHealth("bar"))); ClusterHealth underTest = new ClusterHealth(health, nodeHealths); assertThat(underTest.getNodeHealth("does_not_exist")).isEmpty(); assertThat(underTest.getNodeHealth("bar")).isPresent(); }
@Test public void response_contains_status_and_causes_from_HealthChecker_checkCluster_when_standalone() { authenticateWithRandomMethod(); Health.Status randomStatus = Health.Status.values()[random.nextInt(Health.Status.values().length)]; String[] causes = IntStream.range(0, random.nextInt(33)).mapToObj(i -> randomAlphanumeric(4)).toArray(String[]::new); Health.Builder healthBuilder = newHealthCheckBuilder() .setStatus(randomStatus); Arrays.stream(causes).forEach(healthBuilder::addCause); when(webServer.isStandalone()).thenReturn(false); when(healthChecker.checkCluster()).thenReturn(new ClusterHealth(healthBuilder.build(), emptySet())); System.HealthResponse clusterHealthResponse = underTest.newRequest().executeProtobuf(System.HealthResponse.class); assertThat(clusterHealthResponse.getHealth().name()).isEqualTo(randomStatus.name()); assertThat(clusterHealthResponse.getCausesList()) .extracting(System.Cause::getMessage) .containsOnly(causes); }
@Test public void verify_getters() { Health health = randomHealth(); Set<NodeHealth> nodeHealths = randomNodeHealths(); ClusterHealth underTest = new ClusterHealth(health, nodeHealths); assertThat(underTest.getHealth()).isSameAs(health); assertThat(underTest.getNodes()).isEqualTo(nodeHealths); }
@Test public void response_contains_information_of_nodes_when_clustered() { authenticateWithRandomMethod(); NodeHealth nodeHealth = randomNodeHealth(); when(webServer.isStandalone()).thenReturn(false); when(healthChecker.checkCluster()).thenReturn(new ClusterHealth(GREEN, singleton(nodeHealth))); System.HealthResponse response = underTest.newRequest().executeProtobuf(System.HealthResponse.class); assertThat(response.getNodes().getNodesList()) .hasSize(1); System.Node node = response.getNodes().getNodesList().iterator().next(); assertThat(node.getHealth().name()).isEqualTo(nodeHealth.getStatus().name()); assertThat(node.getCausesList()) .extracting(System.Cause::getMessage) .containsOnly(nodeHealth.getCauses().stream().toArray(String[]::new)); assertThat(node.getName()).isEqualTo(nodeHealth.getDetails().getName()); assertThat(node.getHost()).isEqualTo(nodeHealth.getDetails().getHost()); assertThat(node.getPort()).isEqualTo(nodeHealth.getDetails().getPort()); assertThat(node.getStartedAt()).isEqualTo(formatDateTime(nodeHealth.getDetails().getStartedAt())); assertThat(node.getType().name()).isEqualTo(nodeHealth.getDetails().getType().name()); }
@Test public void response_sort_nodes_by_type_name_host_then_port_when_clustered() { authenticateWithRandomMethod(); // using created field as a unique identifier. pseudo random value to ensure sorting is not based on created field List<NodeHealth> nodeHealths = new ArrayList<>(Arrays.asList( randomNodeHealth(NodeDetails.Type.APPLICATION, "1_name", "1_host", 1, 99), randomNodeHealth(NodeDetails.Type.APPLICATION, "1_name", "2_host", 1, 85), randomNodeHealth(NodeDetails.Type.APPLICATION, "1_name", "2_host", 2, 12), randomNodeHealth(NodeDetails.Type.APPLICATION, "2_name", "1_host", 1, 6), randomNodeHealth(NodeDetails.Type.APPLICATION, "2_name", "1_host", 2, 30), randomNodeHealth(NodeDetails.Type.APPLICATION, "2_name", "2_host", 1, 75), randomNodeHealth(NodeDetails.Type.APPLICATION, "2_name", "2_host", 2, 258), randomNodeHealth(NodeDetails.Type.SEARCH, "1_name", "1_host", 1, 963), randomNodeHealth(NodeDetails.Type.SEARCH, "1_name", "1_host", 2, 1), randomNodeHealth(NodeDetails.Type.SEARCH, "1_name", "2_host", 1, 35), randomNodeHealth(NodeDetails.Type.SEARCH, "1_name", "2_host", 2, 45), randomNodeHealth(NodeDetails.Type.SEARCH, "2_name", "1_host", 1, 39), randomNodeHealth(NodeDetails.Type.SEARCH, "2_name", "1_host", 2, 28), randomNodeHealth(NodeDetails.Type.SEARCH, "2_name", "2_host", 1, 66), randomNodeHealth(NodeDetails.Type.SEARCH, "2_name", "2_host", 2, 77))); String[] expected = nodeHealths.stream().map(s -> formatDateTime(new Date(s.getDetails().getStartedAt()))).toArray(String[]::new); Collections.shuffle(nodeHealths); when(webServer.isStandalone()).thenReturn(false); when(healthChecker.checkCluster()).thenReturn(new ClusterHealth(GREEN, new HashSet<>(nodeHealths))); System.HealthResponse response = underTest.newRequest().executeProtobuf(System.HealthResponse.class); assertThat(response.getNodes().getNodesList()) .extracting(System.Node::getStartedAt) .containsExactly(expected); }
when(healthChecker.checkCluster()) .thenReturn( new ClusterHealth(newHealthCheckBuilder() .setStatus(Health.Status.RED) .addCause("Application node app-1 is RED")
@Override public ClusterHealth checkCluster() { checkState(!webServer.isStandalone(), "Clustering is not enabled"); checkState(sharedHealthState != null, "HealthState instance can't be null when clustering is enabled"); Set<NodeHealth> nodeHealths = sharedHealthState.readAll(); Health health = clusterHealthChecks.stream() .map(clusterHealthCheck -> clusterHealthCheck.check(nodeHealths)) .reduce(Health.GREEN, HealthReducer.INSTANCE); return new ClusterHealth(health, nodeHealths); }