From c5e04e11df2c3b92a6e06d9e9d5ce31682f76674 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Mon, 11 Oct 2021 11:14:56 -0700 Subject: [PATCH] Remove Joda from remaining xpack tests (#78805) This commit removes uses of Joda classes from rollup and other xpack tests still utilizing them, mostly around time zones. --- .../RollupActionDateHistogramGroupConfig.java | 3 ++- .../index/engine/frozen/FrozenIndexIT.java | 6 ++--- ...lAuthenticationResponseMessageBuilder.java | 6 ----- .../rollup/RollupJobIdentifierUtilTests.java | 13 +++++----- .../rollup/action/SearchActionTests.java | 4 ++-- .../xpack/rollup/config/ConfigTests.java | 8 +++---- .../xpack/rollup/job/IndexerUtilsTests.java | 9 +++---- .../job/RollupIndexerIndexingTests.java | 24 +++++++++---------- 8 files changed, 34 insertions(+), 39 deletions(-) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/RollupActionDateHistogramGroupConfig.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/RollupActionDateHistogramGroupConfig.java index e6c5391f90b29..a537a6574802b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/RollupActionDateHistogramGroupConfig.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/RollupActionDateHistogramGroupConfig.java @@ -52,7 +52,8 @@ public abstract class RollupActionDateHistogramGroupConfig implements Writeable, public static final String CALENDAR_INTERVAL = "calendar_interval"; public static final String TIME_ZONE = "time_zone"; - public static final String DEFAULT_TIMEZONE = "UTC"; + // this should really be ZoneOffset.UTC, but the literal UTC timezone is used because it came from Joda + public static final String DEFAULT_TIMEZONE = ZoneId.of("UTC").getId(); private static final String FIELD = "field"; private static final ConstructingObjectParser PARSER; diff --git a/x-pack/plugin/frozen-indices/src/internalClusterTest/java/org/elasticsearch/index/engine/frozen/FrozenIndexIT.java b/x-pack/plugin/frozen-indices/src/internalClusterTest/java/org/elasticsearch/index/engine/frozen/FrozenIndexIT.java index 063c5dcf1d7ef..2b84734c254f4 100644 --- a/x-pack/plugin/frozen-indices/src/internalClusterTest/java/org/elasticsearch/index/engine/frozen/FrozenIndexIT.java +++ b/x-pack/plugin/frozen-indices/src/internalClusterTest/java/org/elasticsearch/index/engine/frozen/FrozenIndexIT.java @@ -37,9 +37,9 @@ import org.elasticsearch.xpack.core.LocalStateCompositeXPackPlugin; import org.elasticsearch.xpack.core.frozen.action.FreezeIndexAction; import org.elasticsearch.xpack.frozen.FrozenIndices; -import org.joda.time.Instant; import java.io.IOException; +import java.time.Instant; import java.util.Collection; import java.util.List; import java.util.stream.Collectors; @@ -123,8 +123,8 @@ public void testTimestampRangeRecalculatedOnStalePrimaryAllocation() throws IOEx assertThat(timestampFieldRange, not(sameInstance(IndexLongFieldRange.UNKNOWN))); assertThat(timestampFieldRange, not(sameInstance(IndexLongFieldRange.EMPTY))); assertTrue(timestampFieldRange.isComplete()); - assertThat(timestampFieldRange.getMin(), equalTo(Instant.parse("2010-01-06T02:03:04.567Z").getMillis())); - assertThat(timestampFieldRange.getMax(), equalTo(Instant.parse("2010-01-06T02:03:04.567Z").getMillis())); + assertThat(timestampFieldRange.getMin(), equalTo(Instant.parse("2010-01-06T02:03:04.567Z").toEpochMilli())); + assertThat(timestampFieldRange.getMax(), equalTo(Instant.parse("2010-01-06T02:03:04.567Z").toEpochMilli())); } public void testTimestampFieldTypeExposedByAllIndicesServices() throws Exception { diff --git a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/authn/SuccessfulAuthenticationResponseMessageBuilder.java b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/authn/SuccessfulAuthenticationResponseMessageBuilder.java index 0aa72fb6a532d..4ff2bc6d7cd34 100644 --- a/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/authn/SuccessfulAuthenticationResponseMessageBuilder.java +++ b/x-pack/plugin/identity-provider/src/main/java/org/elasticsearch/xpack/idp/saml/authn/SuccessfulAuthenticationResponseMessageBuilder.java @@ -19,8 +19,6 @@ import org.elasticsearch.xpack.idp.saml.support.SamlFactory; import org.elasticsearch.xpack.idp.saml.support.SamlInit; import org.elasticsearch.xpack.idp.saml.support.SamlObjectSigner; -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; import org.opensaml.core.xml.schema.XSString; import org.opensaml.saml.saml2.core.Assertion; import org.opensaml.saml.saml2.core.Attribute; @@ -117,10 +115,6 @@ private Conditions buildConditions(Instant now, SamlServiceProvider serviceProvi return conditions; } - private DateTime now() { - return new DateTime(clock.millis(), DateTimeZone.UTC); - } - private Subject buildSubject(Instant now, UserServiceAuthentication user, SamlAuthenticationState authnState) { final SamlServiceProvider serviceProvider = user.getServiceProvider(); diff --git a/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/RollupJobIdentifierUtilTests.java b/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/RollupJobIdentifierUtilTests.java index aa49b9d9bbbf4..b37edab2ce23f 100644 --- a/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/RollupJobIdentifierUtilTests.java +++ b/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/RollupJobIdentifierUtilTests.java @@ -24,7 +24,6 @@ import org.elasticsearch.xpack.core.rollup.job.MetricConfig; import org.elasticsearch.xpack.core.rollup.job.RollupJobConfig; import org.elasticsearch.xpack.core.rollup.job.TermsGroupConfig; -import org.joda.time.DateTimeZone; import java.time.ZoneId; import java.time.ZoneOffset; @@ -399,7 +398,7 @@ public void testHistoSameNameWrongTypeInCaps() { final GroupConfig group = new GroupConfig( // NOTE same name but wrong type - new DateHistogramGroupConfig.CalendarInterval("foo", new DateHistogramInterval("1d"), null, DateTimeZone.UTC.getID()), + new DateHistogramGroupConfig.CalendarInterval("foo", new DateHistogramInterval("1d"), null, ZoneOffset.UTC.getId()), new HistogramGroupConfig(1L, "baz"), // <-- NOTE right type but wrong name null ); @@ -428,7 +427,7 @@ public void testMissingDateHisto() { .subAggregation(new AvgAggregationBuilder("the_avg").field("avg_field")); final GroupConfig group = new GroupConfig( - new DateHistogramGroupConfig.CalendarInterval("foo", new DateHistogramInterval("1d"), null, DateTimeZone.UTC.getID()) + new DateHistogramGroupConfig.CalendarInterval("foo", new DateHistogramInterval("1d"), null, ZoneOffset.UTC.getId()) ); final List metrics = Arrays.asList( new MetricConfig("max_field", singletonList("max")), @@ -457,7 +456,7 @@ public void testNoMatchingInterval() { final GroupConfig group = new GroupConfig( // interval in job is much higher than agg interval above - new DateHistogramGroupConfig.FixedInterval("foo", new DateHistogramInterval("100d"), null, DateTimeZone.UTC.getID()) + new DateHistogramGroupConfig.FixedInterval("foo", new DateHistogramInterval("100d"), null, ZoneOffset.UTC.getId()) ); final RollupJobConfig job = new RollupJobConfig("foo", "index", "rollup", "*/5 * * * * ?", 10, group, emptyList(), null); Set caps = singletonSet(new RollupJobCaps(job)); @@ -481,7 +480,7 @@ public void testDateHistoMissingFieldInCaps() { final GroupConfig group = new GroupConfig( // NOTE different field from the one in the query - new DateHistogramGroupConfig.CalendarInterval("bar", new DateHistogramInterval("1d"), null, DateTimeZone.UTC.getID()) + new DateHistogramGroupConfig.CalendarInterval("bar", new DateHistogramInterval("1d"), null, ZoneOffset.UTC.getId()) ); final List metrics = Arrays.asList( new MetricConfig("max_field", singletonList("max")), @@ -509,7 +508,7 @@ public void testHistoMissingFieldInCaps() { .subAggregation(new AvgAggregationBuilder("the_avg").field("avg_field")); final GroupConfig group = new GroupConfig( - new DateHistogramGroupConfig.CalendarInterval("bar", new DateHistogramInterval("1d"), null, DateTimeZone.UTC.getID()), + new DateHistogramGroupConfig.CalendarInterval("bar", new DateHistogramInterval("1d"), null, ZoneOffset.UTC.getId()), new HistogramGroupConfig(1L, "baz"), // <-- NOTE right type but wrong name null ); @@ -538,7 +537,7 @@ public void testNoMatchingHistoInterval() { .subAggregation(new AvgAggregationBuilder("the_avg").field("avg_field")); final GroupConfig group = new GroupConfig( - new DateHistogramGroupConfig.CalendarInterval("foo", new DateHistogramInterval("1d"), null, DateTimeZone.UTC.getID()), + new DateHistogramGroupConfig.CalendarInterval("foo", new DateHistogramInterval("1d"), null, ZoneOffset.UTC.getId()), new HistogramGroupConfig(1L, "baz"), // <-- NOTE right type but wrong name null ); diff --git a/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/action/SearchActionTests.java b/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/action/SearchActionTests.java index 8f2de11f197ad..64a11811b7d6f 100644 --- a/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/action/SearchActionTests.java +++ b/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/action/SearchActionTests.java @@ -55,11 +55,11 @@ import org.elasticsearch.xpack.core.rollup.job.TermsGroupConfig; import org.elasticsearch.xpack.rollup.Rollup; import org.hamcrest.core.IsEqual; -import org.joda.time.DateTimeZone; import org.junit.Before; import org.mockito.Mockito; import java.io.IOException; +import java.time.ZoneOffset; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -454,7 +454,7 @@ public void testGoodButNullQuery() { SearchRequest request = new SearchRequest(combinedIndices, source); final GroupConfig groupConfig = new GroupConfig( - new DateHistogramGroupConfig.CalendarInterval("foo", new DateHistogramInterval("1d"), null, DateTimeZone.UTC.getID()) + new DateHistogramGroupConfig.CalendarInterval("foo", new DateHistogramInterval("1d"), null, ZoneOffset.UTC.getId()) ); final RollupJobConfig job = new RollupJobConfig("foo", "index", "rollup", "*/5 * * * * ?", 10, groupConfig, emptyList(), null); Set caps = singleton(new RollupJobCaps(job)); diff --git a/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/config/ConfigTests.java b/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/config/ConfigTests.java index e7d5d7c365f8f..ed8c01d1461b8 100644 --- a/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/config/ConfigTests.java +++ b/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/config/ConfigTests.java @@ -15,8 +15,8 @@ import org.elasticsearch.xpack.core.rollup.job.MetricConfig; import org.elasticsearch.xpack.core.rollup.job.RollupJob; import org.elasticsearch.xpack.core.rollup.job.TermsGroupConfig; -import org.joda.time.DateTimeZone; +import java.time.ZoneId; import java.time.zone.ZoneRulesException; import java.util.HashMap; import java.util.Map; @@ -75,17 +75,17 @@ public void testEmptyDateHistoInterval() { public void testNullTimeZone() { DateHistogramGroupConfig config = new CalendarInterval("foo", DateHistogramInterval.HOUR, null, null); - assertThat(config.getTimeZone(), equalTo(DateTimeZone.UTC.getID())); + assertThat(config.getTimeZone(), equalTo(ZoneId.of("UTC").getId())); } public void testEmptyTimeZone() { DateHistogramGroupConfig config = new CalendarInterval("foo", DateHistogramInterval.HOUR, null, ""); - assertThat(config.getTimeZone(), equalTo(DateTimeZone.UTC.getID())); + assertThat(config.getTimeZone(), equalTo(ZoneId.of("UTC").getId())); } public void testDefaultTimeZone() { DateHistogramGroupConfig config = new CalendarInterval("foo", DateHistogramInterval.HOUR); - assertThat(config.getTimeZone(), equalTo(DateTimeZone.UTC.getID())); + assertThat(config.getTimeZone(), equalTo(ZoneId.of("UTC").getId())); } public void testUnkownTimeZone() { diff --git a/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/job/IndexerUtilsTests.java b/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/job/IndexerUtilsTests.java index f8c0be9e586c2..e14704e2a3b03 100644 --- a/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/job/IndexerUtilsTests.java +++ b/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/job/IndexerUtilsTests.java @@ -44,11 +44,12 @@ import org.elasticsearch.xpack.core.rollup.job.MetricConfig; import org.elasticsearch.xpack.core.rollup.job.RollupIndexerJobStats; import org.elasticsearch.xpack.core.rollup.job.TermsGroupConfig; -import org.joda.time.DateTime; import org.mockito.stubbing.Answer; import java.io.IOException; import java.time.ZoneId; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -80,7 +81,7 @@ public void testMissingFields() throws IOException { int numDocs = randomIntBetween(1, 10); for (int i = 0; i < numDocs; i++) { Document document = new Document(); - long timestamp = new DateTime().minusDays(i).getMillis(); + long timestamp = ZonedDateTime.now(ZoneOffset.UTC).minusDays(i).toInstant().toEpochMilli(); document.add(new SortedNumericDocValuesField(timestampField, timestamp)); document.add(new LongPoint(timestampField, timestamp)); document.add(new SortedNumericDocValuesField(valueField, randomIntBetween(1, 100))); @@ -140,7 +141,7 @@ public void testCorrectFields() throws IOException { int numDocs = randomIntBetween(1, 10); for (int i = 0; i < numDocs; i++) { Document document = new Document(); - long timestamp = new DateTime().minusDays(i).getMillis(); + long timestamp = ZonedDateTime.now(ZoneOffset.UTC).minusDays(i).toInstant().toEpochMilli(); document.add(new SortedNumericDocValuesField(timestampField, timestamp)); document.add(new LongPoint(timestampField, timestamp)); document.add(new SortedNumericDocValuesField(valueField, randomIntBetween(1, 100))); @@ -256,7 +257,7 @@ public void testEmptyCounts() throws IOException { int numDocs = randomIntBetween(1, 10); for (int i = 0; i < numDocs; i++) { Document document = new Document(); - long timestamp = new DateTime().minusDays(i).getMillis(); + long timestamp = ZonedDateTime.now(ZoneOffset.UTC).minusDays(i).toInstant().toEpochMilli(); document.add(new SortedNumericDocValuesField(timestampField, timestamp)); document.add(new LongPoint(timestampField, timestamp)); document.add(new SortedNumericDocValuesField(valueField, randomIntBetween(1, 100))); diff --git a/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/job/RollupIndexerIndexingTests.java b/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/job/RollupIndexerIndexingTests.java index ce6aad81bface..87f8011a68d26 100644 --- a/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/job/RollupIndexerIndexingTests.java +++ b/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/job/RollupIndexerIndexingTests.java @@ -137,7 +137,7 @@ public void testSimpleDateHisto() throws Exception { "the_histo.date_histogram._count", 2, "the_histo.date_histogram.time_zone", - "UTC", // TODO: the default is hardcoded from Joda, we should change this + ZoneId.of("UTC").getId(), "_rollup.id", job.getId() ) @@ -158,7 +158,7 @@ public void testSimpleDateHisto() throws Exception { "the_histo.date_histogram._count", 1, "the_histo.date_histogram.time_zone", - "UTC", // TODO: the default is hardcoded from Joda, we should change this + ZoneId.of("UTC").getId(), "_rollup.id", job.getId() ) @@ -221,7 +221,7 @@ public void testDateHistoAndMetrics() throws Exception { "counter.sum.value", 50.0, "the_histo.date_histogram.time_zone", - "UTC", // TODO: the default is hardcoded from Joda, we should change this + ZoneId.of("UTC").getId(), "_rollup.id", job.getId() ) @@ -252,7 +252,7 @@ public void testDateHistoAndMetrics() throws Exception { "counter.sum.value", 141.0, "the_histo.date_histogram.time_zone", - "UTC", // TODO: the default is hardcoded from Joda, we should change this + ZoneId.of("UTC").getId(), "_rollup.id", job.getId() ) @@ -283,7 +283,7 @@ public void testDateHistoAndMetrics() throws Exception { "counter.sum.value", 275.0, "the_histo.date_histogram.time_zone", - "UTC", // TODO: the default is hardcoded from Joda, we should change this + ZoneId.of("UTC").getId(), "_rollup.id", job.getId() ) @@ -314,7 +314,7 @@ public void testDateHistoAndMetrics() throws Exception { "counter.sum.value", 270.0, "the_histo.date_histogram.time_zone", - "UTC", // TODO: the default is hardcoded from Joda, we should change this + ZoneId.of("UTC").getId(), "_rollup.id", job.getId() ) @@ -345,7 +345,7 @@ public void testDateHistoAndMetrics() throws Exception { "counter.sum.value", 440.0, "the_histo.date_histogram.time_zone", - "UTC", // TODO: the default is hardcoded from Joda, we should change this + ZoneId.of("UTC").getId(), "_rollup.id", job.getId() ) @@ -398,7 +398,7 @@ public void testSimpleDateHistoWithDelay() throws Exception { "the_histo.date_histogram._count", 2, "the_histo.date_histogram.time_zone", - "UTC", // TODO: the default is hardcoded from Joda, we should change this + ZoneId.of("UTC").getId(), "_rollup.id", job.getId() ) @@ -419,7 +419,7 @@ public void testSimpleDateHistoWithDelay() throws Exception { "the_histo.date_histogram._count", 2, "the_histo.date_histogram.time_zone", - "UTC", // TODO: the default is hardcoded from Joda, we should change this + ZoneId.of("UTC").getId(), "_rollup.id", job.getId() ) @@ -440,7 +440,7 @@ public void testSimpleDateHistoWithDelay() throws Exception { "the_histo.date_histogram._count", 1, "the_histo.date_histogram.time_zone", - "UTC", // TODO: the default is hardcoded from Joda, we should change this + ZoneId.of("UTC").getId(), "_rollup.id", job.getId() ) @@ -493,7 +493,7 @@ public void testSimpleDateHistoWithOverlappingDelay() throws Exception { "the_histo.date_histogram._count", 3, "the_histo.date_histogram.time_zone", - "UTC", // TODO: the default is hardcoded from Joda, we should change this + ZoneId.of("UTC").getId(), "_rollup.id", job.getId() ) @@ -514,7 +514,7 @@ public void testSimpleDateHistoWithOverlappingDelay() throws Exception { "the_histo.date_histogram._count", 4, "the_histo.date_histogram.time_zone", - "UTC", // TODO: the default is hardcoded from Joda, we should change this + ZoneId.of("UTC").getId(), "_rollup.id", job.getId() )