private void setStartedAt() { when(server.getStartedAt()).thenReturn(new Date()); }
@Test public void allow_client_to_cache_messages() { Date now = new Date(); Date aBitLater = new Date(now.getTime() + 1000); when(server.getStartedAt()).thenReturn(now); TestResponse result = call(null, DateUtils.formatDateTime(aBitLater)); verifyZeroInteractions(i18n); verify(server).getStartedAt(); assertThat(result.getStatus()).isEqualTo(HttpURLConnection.HTTP_NOT_MODIFIED); }
@Override public void handle(Request request, Response response) throws Exception { Date timestamp = request.paramAsDateTime(TS_PARAM); if (timestamp != null && timestamp.after(server.getStartedAt())) { response.stream().setStatus(HTTP_NOT_MODIFIED).output().close(); return; } String localeParam = request.mandatoryParam(LOCALE_PARAM); Locale locale = Locale.forLanguageTag(localeParam); checkArgument(!locale.getISO3Language().isEmpty(), "'%s' cannot be parsed as a BCP47 language tag", localeParam); try (JsonWriter json = response.newJsonWriter()) { json.beginObject(); json.prop("effectiveLocale", i18n.getEffectiveLocale(locale).toLanguageTag()); json.name("messages"); json.beginObject(); i18n.getPropertyKeys().forEach(messageKey -> json.prop(messageKey, i18n.message(locale, messageKey, messageKey))); json.endObject(); json.endObject(); } } }
@Test public void return_all_l10n_messages_using_accept_header_with_cache_expired() { Date now = new Date(); Date aBitEarlier = new Date(now.getTime() - 1000); when(server.getStartedAt()).thenReturn(now); when(i18n.getPropertyKeys()).thenReturn(ImmutableSet.of(KEY_1, KEY_2, KEY_3)); when(i18n.message(PRC, KEY_1, KEY_1)).thenReturn(KEY_1); when(i18n.message(PRC, KEY_2, KEY_2)).thenReturn(KEY_2); when(i18n.message(PRC, KEY_3, KEY_3)).thenReturn(KEY_3); when(i18n.getEffectiveLocale(PRC)).thenReturn(PRC); TestResponse result = call(PRC.toLanguageTag(), DateUtils.formatDateTime(aBitEarlier)); verify(i18n).getPropertyKeys(); verify(i18n).message(PRC, KEY_1, KEY_1); verify(i18n).message(PRC, KEY_2, KEY_2); verify(i18n).message(PRC, KEY_3, KEY_3); assertJson(result.getInput()).isSimilarTo("{\"effectiveLocale\":\"zh-CN\", \"messages\": {\"key1\":\"key1\",\"key2\":\"key2\",\"key3\":\"key3\"}}"); }
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 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_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()); }
@Override public void handle(Request request, Response response) throws Exception { Date timestamp = request.paramAsDateTime(TS_PARAM); if (timestamp != null && timestamp.after(server.getStartedAt())) { response.stream().setStatus(HTTP_NOT_MODIFIED).output().close(); return; } String localeParam = request.mandatoryParam(LOCALE_PARAM); Locale locale = Locale.forLanguageTag(localeParam); checkArgument(!locale.getISO3Language().isEmpty(), "'%s' cannot be parsed as a BCP47 language tag", localeParam); try (JsonWriter json = response.newJsonWriter()) { json.beginObject(); json.prop("effectiveLocale", i18n.getEffectiveLocale(locale).toLanguageTag()); json.name("messages"); json.beginObject(); i18n.getPropertyKeys().forEach(messageKey -> json.prop(messageKey, i18n.message(locale, messageKey, messageKey))); json.endObject(); json.endObject(); } } }
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(); }