public NodeHealthProviderImpl(Configuration configuration, HealthChecker healthChecker, Server server, NetworkUtils networkUtils) { this.healthChecker = healthChecker; this.nodeHealthBuilder = newNodeHealthBuilder(); this.nodeDetails = newNodeDetailsBuilder() .setName(computeName(configuration)) .setType(NodeDetails.Type.APPLICATION) .setHost(computeHost(configuration, networkUtils)) .setPort(computePort(configuration)) .setStartedAt(server.getStartedAt().getTime()) .build(); }
private static String computeName(Configuration configuration) { return configuration.get(CLUSTER_NODE_NAME.getKey()) .orElseThrow(missingPropertyISE(CLUSTER_NODE_NAME.getKey())); }
@Test public void get_returns_started_from_server_startedAt_at_constructor_time() { setRequiredPropertiesForConstructor(); when(networkUtils.getHostname()).thenReturn(randomAlphanumeric(4)); Date date = new Date(); when(server.getStartedAt()).thenReturn(date); when(healthChecker.checkNode()).thenReturn(Health.newHealthCheckBuilder() .setStatus(Health.Status.values()[random.nextInt(Health.Status.values().length)]) .build()); NodeHealthProviderImpl underTest = new NodeHealthProviderImpl(mapSettings.asConfig(), healthChecker, server, networkUtils); NodeHealth nodeHealth = underTest.get(); assertThat(nodeHealth.getDetails().getStartedAt()).isEqualTo(date.getTime()); // change startedAt value setStartedAt(); NodeHealth newNodeHealth = underTest.get(); assertThat(newNodeHealth.getDetails().getStartedAt()).isEqualTo(date.getTime()); }
@Test public void constructor_throws_NPE_is_Server_getStartedAt_is_null() { setRequiredPropertiesForConstructor(); expectedException.expect(NullPointerException.class); new NodeHealthProviderImpl(mapSettings.asConfig(), healthChecker, server, networkUtils); }
@Test public void constructor_throws_ISE_if_node_name_property_is_not_set() { expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Property sonar.cluster.node.name is not defined"); new NodeHealthProviderImpl(mapSettings.asConfig(), healthChecker, server, networkUtils); }
public NodeHealthProviderImpl(Configuration configuration, HealthChecker healthChecker, Server server, NetworkUtils networkUtils) { this.healthChecker = healthChecker; this.nodeHealthBuilder = newNodeHealthBuilder(); this.nodeDetails = newNodeDetailsBuilder() .setName(computeName(configuration)) .setType(NodeDetails.Type.APPLICATION) .setHost(computeHost(configuration, networkUtils)) .setPort(computePort(configuration)) .setStartedAt(server.getStartedAt().getTime()) .build(); }
@Test public void get_returns_APPLICATION_type() { setRequiredPropertiesForConstructor(); setStartedAt(); when(networkUtils.getHostname()).thenReturn(randomAlphanumeric(23)); when(healthChecker.checkNode()).thenReturn(Health.newHealthCheckBuilder() .setStatus(Health.Status.values()[random.nextInt(Health.Status.values().length)]) .build()); NodeHealthProviderImpl underTest = new NodeHealthProviderImpl(mapSettings.asConfig(), healthChecker, server, networkUtils); NodeHealth nodeHealth = underTest.get(); assertThat(nodeHealth.getDetails().getType()).isEqualTo(NodeDetails.Type.APPLICATION); }
private static int computePort(Configuration configuration) { return configuration.getInt(CLUSTER_NODE_HZ_PORT.getKey()) .orElseThrow(missingPropertyISE(CLUSTER_NODE_HZ_PORT.getKey())); }
@Test public void constructor_throws_ISE_if_node_port_property_is_not_set() { mapSettings.setProperty(CLUSTER_NODE_NAME.getKey(), randomAlphanumeric(3)); when(networkUtils.getHostname()).thenReturn(randomAlphanumeric(23)); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Property sonar.cluster.node.port is not defined"); new NodeHealthProviderImpl(mapSettings.asConfig(), healthChecker, server, networkUtils); }
@Test public void get_returns_HEALTH_status_and_causes_from_HealthChecker_checkNode() { setRequiredPropertiesForConstructor(); setStartedAt(); when(networkUtils.getHostname()).thenReturn(randomAlphanumeric(4)); Health.Status randomStatus = Health.Status.values()[random.nextInt(Health.Status.values().length)]; String[] expected = IntStream.range(0, random.nextInt(4)).mapToObj(s -> randomAlphabetic(55)).toArray(String[]::new); Health.Builder healthBuilder = Health.newHealthCheckBuilder() .setStatus(randomStatus); Arrays.stream(expected).forEach(healthBuilder::addCause); when(healthChecker.checkNode()).thenReturn(healthBuilder.build()); NodeHealthProviderImpl underTest = new NodeHealthProviderImpl(mapSettings.asConfig(), healthChecker, server, networkUtils); NodeHealth nodeHealth = underTest.get(); assertThat(nodeHealth.getStatus().name()).isEqualTo(randomStatus.name()); assertThat(nodeHealth.getCauses()).containsOnly(expected); }
private static String computeName(Configuration configuration) { return configuration.get(CLUSTER_NODE_NAME.getKey()) .orElseThrow(missingPropertyISE(CLUSTER_NODE_NAME.getKey())); }
@Test public void constructor_thows_NPE_if_NetworkUtils_getHostname_returns_null() { mapSettings.setProperty(CLUSTER_NODE_NAME.getKey(), randomAlphanumeric(3)); expectedException.expect(NullPointerException.class); new NodeHealthProviderImpl(mapSettings.asConfig(), healthChecker, server, networkUtils); }
private void getReturnsHostnameFromNetworkUtils(String hostPropertyValue) { String host = randomAlphanumeric(3); setRequiredPropertiesForConstructor(); if (hostPropertyValue != null) { mapSettings.setProperty(CLUSTER_NODE_HOST.getKey(), hostPropertyValue); } setStartedAt(); when(healthChecker.checkNode()).thenReturn(Health.newHealthCheckBuilder() .setStatus(Health.Status.values()[random.nextInt(Health.Status.values().length)]) .build()); when(networkUtils.getHostname()).thenReturn(host); NodeHealthProviderImpl underTest = new NodeHealthProviderImpl(mapSettings.asConfig(), healthChecker, server, networkUtils); NodeHealth nodeHealth = underTest.get(); assertThat(nodeHealth.getDetails().getHost()).isEqualTo(host); // change hostname when(networkUtils.getHostname()).thenReturn(randomAlphanumeric(4)); NodeHealth newNodeHealth = underTest.get(); assertThat(newNodeHealth.getDetails().getHost()).isEqualTo(host); }
private static int computePort(Configuration configuration) { return configuration.getInt(CLUSTER_NODE_HZ_PORT.getKey()) .orElseThrow(missingPropertyISE(CLUSTER_NODE_HZ_PORT.getKey())); }
@Test public void get_returns_name_and_port_from_properties_at_constructor_time() { String name = randomAlphanumeric(3); int port = 1 + random.nextInt(4); mapSettings.setProperty(CLUSTER_NODE_NAME.getKey(), name); mapSettings.setProperty(CLUSTER_NODE_HZ_PORT.getKey(), port); setStartedAt(); when(healthChecker.checkNode()).thenReturn(Health.newHealthCheckBuilder() .setStatus(Health.Status.values()[random.nextInt(Health.Status.values().length)]) .build()); when(networkUtils.getHostname()).thenReturn(randomAlphanumeric(3)); NodeHealthProviderImpl underTest = new NodeHealthProviderImpl(mapSettings.asConfig(), healthChecker, server, networkUtils); NodeHealth nodeHealth = underTest.get(); assertThat(nodeHealth.getDetails().getName()).isEqualTo(name); assertThat(nodeHealth.getDetails().getPort()).isEqualTo(port); // change values in properties setRequiredPropertiesForConstructor(); NodeHealth newNodeHealth = underTest.get(); assertThat(newNodeHealth.getDetails().getName()).isEqualTo(name); assertThat(newNodeHealth.getDetails().getPort()).isEqualTo(port); }
@Test public void get_returns_host_from_property_if_set_at_constructor_time() { String host = randomAlphanumeric(4); mapSettings.setProperty(CLUSTER_NODE_NAME.getKey(), randomAlphanumeric(3)); mapSettings.setProperty(CLUSTER_NODE_HZ_PORT.getKey(), 1 + random.nextInt(4)); mapSettings.setProperty(CLUSTER_NODE_HOST.getKey(), host); setStartedAt(); when(healthChecker.checkNode()).thenReturn(Health.newHealthCheckBuilder() .setStatus(Health.Status.values()[random.nextInt(Health.Status.values().length)]) .build()); NodeHealthProviderImpl underTest = new NodeHealthProviderImpl(mapSettings.asConfig(), healthChecker, server, networkUtils); NodeHealth nodeHealth = underTest.get(); assertThat(nodeHealth.getDetails().getHost()).isEqualTo(host); // change values in properties mapSettings.setProperty(CLUSTER_NODE_HOST.getKey(), randomAlphanumeric(66)); NodeHealth newNodeHealth = underTest.get(); assertThat(newNodeHealth.getDetails().getHost()).isEqualTo(host); }