Skip to content

Commit

Permalink
Move testToUtc test to DateFormattersTests (#38610)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
pgomulka authored Feb 11, 2019
1 parent 8ede155 commit 067e648
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@
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;

/**
* Base class for all monitoring documents.
*/
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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 067e648

Please sign in to comment.