From 291b906a6cc3d1a840150dbb615f1d3050d522e2 Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Mon, 11 Feb 2019 08:54:03 +0100 Subject: [PATCH] Move testToUtc test to DateFormattersTests (#38610) The test was relying on toString in ZonedDateTime which is different to what is formatted by strict_date_time when milliseconds are 0 The method is just delegating to dateFormatter, so that scenario should be covered there. closes #38359 --- .../common/time/DateFormattersTests.java | 12 ++++++++++++ .../core/monitoring/exporter/MonitoringDoc.java | 8 ++------ .../exporter/BaseMonitoringDocTestCase.java | 9 --------- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/server/src/test/java/org/elasticsearch/common/time/DateFormattersTests.java b/server/src/test/java/org/elasticsearch/common/time/DateFormattersTests.java index acb202f80fc55..3c97d27fe7874 100644 --- a/server/src/test/java/org/elasticsearch/common/time/DateFormattersTests.java +++ b/server/src/test/java/org/elasticsearch/common/time/DateFormattersTests.java @@ -21,8 +21,10 @@ import org.elasticsearch.test.ESTestCase; +import java.time.Clock; import java.time.Instant; import java.time.ZoneId; +import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoField; @@ -96,6 +98,8 @@ public void testEpochSecondParserWithFraction() { assertThat(e.getMessage(), is("failed to parse date field [1234.1234567890] with format [epoch_second]")); } + + public void testEpochMilliParsersWithDifferentFormatters() { DateFormatter formatter = DateFormatter.forPattern("strict_date_optional_time||epoch_millis"); TemporalAccessor accessor = formatter.parse("123"); @@ -248,4 +252,12 @@ public void testRoundupFormatterLocale() { assertThat(roundupParser.getLocale(), is(locale)); assertThat(formatter.locale(), is(locale)); } + + public void test0MillisAreFormatted() { + DateFormatter formatter = DateFormatter.forPattern("strict_date_time"); + Clock clock = Clock.fixed(ZonedDateTime.of(2019, 02, 8, 11, 43, 00, 0, + ZoneOffset.UTC).toInstant(), ZoneOffset.UTC); + String formatted = formatter.formatMillis(clock.millis()); + assertThat(formatted, is("2019-02-08T11:43:00.000Z")); + } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/exporter/MonitoringDoc.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/exporter/MonitoringDoc.java index 0642ed5955f77..21ceb6097ae3a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/exporter/MonitoringDoc.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/monitoring/exporter/MonitoringDoc.java @@ -16,9 +16,7 @@ import org.elasticsearch.xpack.core.monitoring.MonitoredSystem; import java.io.IOException; -import java.time.Instant; import java.time.ZoneOffset; -import java.time.ZonedDateTime; import java.util.Objects; /** @@ -26,7 +24,7 @@ */ public abstract class MonitoringDoc implements ToXContentObject { - private static final DateFormatter dateTimeFormatter = DateFormatter.forPattern("strict_date_time"); + private static final DateFormatter dateTimeFormatter = DateFormatter.forPattern("strict_date_time").withZone(ZoneOffset.UTC); private final String cluster; private final long timestamp; private final long intervalMillis; @@ -126,9 +124,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws * @return a string representing the timestamp */ public static String toUTC(final long timestamp) { - ZonedDateTime zonedDateTime = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.UTC); - return dateTimeFormatter.format(zonedDateTime); - + return dateTimeFormatter.formatMillis(timestamp); } /** diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BaseMonitoringDocTestCase.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BaseMonitoringDocTestCase.java index a3b5f5d018da3..849b7e0a1059d 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BaseMonitoringDocTestCase.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BaseMonitoringDocTestCase.java @@ -26,8 +26,6 @@ import org.junit.Before; import java.io.IOException; -import java.time.Instant; -import java.time.ZoneOffset; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -166,13 +164,6 @@ public final void testToXContentContainsCommonFields() throws IOException { } } - public void testToUTC() { - final long timestamp = System.currentTimeMillis(); - final String expected = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.UTC).toString(); - - assertEquals(expected, MonitoringDoc.toUTC(timestamp)); - } - public void testMonitoringNodeConstructor() { final String id = randomAlphaOfLength(5); final String name = randomAlphaOfLengthBetween(3, 10);