@Override public Optional<String> getLeaderHostName() { return Optional.of(NetworkUtilsImpl.INSTANCE.getHostname()); }
private static String getHost(Props props, NetworkUtils networkUtils) { String host = props.value(CLUSTER_NODE_HOST.getKey()); if (host != null && !host.isEmpty()) { return host; } return networkUtils.getHostname(); }
@Override public Optional<String> getLeaderHostName() { return Optional.of(NetworkUtilsImpl.INSTANCE.getHostname()); }
private long setRequiredPropertiesAndMocks(Properties properties) { properties.setProperty(CLUSTER_NODE_NAME.getKey(), randomAlphanumeric(3)); properties.setProperty(CLUSTER_NODE_HZ_PORT.getKey(), valueOf(1 + random.nextInt(4))); long now = 1L + random.nextInt(87); when(clock.now()).thenReturn(now); when(networkUtils.getHostname()).thenReturn(randomAlphanumeric(34)); return now; } }
private void getReturnsHostFromNetworkUtils(@Nullable String hostPropertyValue) { String host = randomAlphanumeric(34); Properties properties = new Properties(); properties.setProperty(CLUSTER_NODE_NAME.getKey(), randomAlphanumeric(3)); properties.setProperty(CLUSTER_NODE_HZ_PORT.getKey(), valueOf(1 + random.nextInt(4))); if (hostPropertyValue != null) { properties.setProperty(CLUSTER_NODE_HOST.getKey(), hostPropertyValue); } when(clock.now()).thenReturn(1L + random.nextInt(87)); when(networkUtils.getHostname()).thenReturn(host); SearchNodeHealthProvider underTest = new SearchNodeHealthProvider(new Props(properties), clusterAppState, networkUtils, clock); NodeHealth nodeHealth = underTest.get(); assertThat(nodeHealth.getDetails().getHost()).isEqualTo(host); // change now when(networkUtils.getHostname()).thenReturn(randomAlphanumeric(96)); NodeHealth newNodeHealth = underTest.get(); assertThat(newNodeHealth.getDetails().getHost()).isEqualTo(host); }
@Test public void constructor_throws_IAE_if_property_node_port_is_not_set() { Properties properties = new Properties(); properties.put(CLUSTER_NODE_NAME.getKey(), randomAlphanumeric(3)); when(networkUtils.getHostname()).thenReturn(randomAlphanumeric(34)); Props props = new Props(properties); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Missing property: sonar.cluster.node.port"); new SearchNodeHealthProvider(props, clusterAppState, networkUtils, clock); }
@Test public void constructor_throws_FormatException_if_property_node_port_is_not_an_integer() { String port = randomAlphabetic(3); Properties properties = new Properties(); properties.put(CLUSTER_NODE_NAME.getKey(), randomAlphanumeric(3)); properties.put(CLUSTER_NODE_HZ_PORT.getKey(), port); when(networkUtils.getHostname()).thenReturn(randomAlphanumeric(34)); Props props = new Props(properties); expectedException.expect(NumberFormatException.class); expectedException.expectMessage("For input string: \"" + port + "\""); new SearchNodeHealthProvider(props, clusterAppState, networkUtils, clock); }
@Test public void no_broken_dependencies() { ComponentContainer container = new ComponentContainer(); Server server = mock(Server.class); NetworkUtils networkUtils = mock(NetworkUtils.class); // settings required by NodeHealthProvider mapSettings.setProperty("sonar.cluster.node.name", randomAlphanumeric(3)); mapSettings.setProperty("sonar.cluster.node.port", valueOf(1 + random.nextInt(10))); when(server.getStartedAt()).thenReturn(new Date()); when(networkUtils.getHostname()).thenReturn(randomAlphanumeric(12)); // upper level dependencies container.add( mock(System2.class), mapSettings.asConfig(), server, networkUtils, mock(HazelcastMember.class)); // HealthAction dependencies container.add(mock(HealthChecker.class)); underTest.configure(container); container.startComponents(); }
@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); }
@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 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 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); }
@Test public void get_returns_name_and_port_from_properties_at_constructor_time() { String name = randomAlphanumeric(3); int port = 1 + random.nextInt(4); Properties properties = new Properties(); properties.setProperty(CLUSTER_NODE_NAME.getKey(), name); properties.setProperty(CLUSTER_NODE_HZ_PORT.getKey(), valueOf(port)); when(networkUtils.getHostname()).thenReturn(randomAlphanumeric(34)); when(clock.now()).thenReturn(1L + random.nextInt(87)); SearchNodeHealthProvider underTest = new SearchNodeHealthProvider(new Props(properties), clusterAppState, networkUtils, clock); NodeHealth nodeHealth = underTest.get(); assertThat(nodeHealth.getDetails().getName()).isEqualTo(name); assertThat(nodeHealth.getDetails().getPort()).isEqualTo(port); // change values in properties properties.setProperty(CLUSTER_NODE_NAME.getKey(), randomAlphanumeric(6)); properties.setProperty(CLUSTER_NODE_HZ_PORT.getKey(), valueOf(1 + random.nextInt(99))); NodeHealth newNodeHealth = underTest.get(); assertThat(newNodeHealth.getDetails().getName()).isEqualTo(name); assertThat(newNodeHealth.getDetails().getPort()).isEqualTo(port); }
@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_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); }
@Override public Optional<String> getLeaderHostName() { return Optional.of(NetworkUtilsImpl.INSTANCE.getHostname()); }
private static String getHost(Props props, NetworkUtils networkUtils) { String host = props.value(CLUSTER_NODE_HOST); if (host != null && !host.isEmpty()) { return host; } return networkUtils.getHostname(); }