From c874da44bf6a9a0dadac59be10de51ca9970d09f Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Thu, 7 Oct 2021 13:20:06 -0700 Subject: [PATCH] Remove uses of Joda from xpack tests (#78660) (#78800) * Remove uses of Joda from xpack tests (#78660) Joda was deprecated internally many years ago, but there still exist some tests that use it to format dates. This commit converts tests in xpack that need date parsing or printing to use Java time. * fix compile * checkstyle Co-authored-by: Elastic Machine --- .../job/RollupIndexerIndexingTests.java | 36 ++++++++++--------- .../audit/index/IndexNameResolver.java | 14 ++++---- .../authz/IndicesAndAliasesResolverTests.java | 15 ++++---- .../xpack/security/user/XPackUserTests.java | 5 +-- .../integration/TransformIntegTestCase.java | 2 +- .../integration/TransformRestTestCase.java | 2 +- 6 files changed, 40 insertions(+), 34 deletions(-) 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 987963f5dacf3..c6be98852f63c 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 @@ -59,12 +59,12 @@ import org.elasticsearch.xpack.core.rollup.job.MetricConfig; import org.elasticsearch.xpack.core.rollup.job.RollupJob; import org.elasticsearch.xpack.core.rollup.job.RollupJobConfig; -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; import org.junit.Before; 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.Collection; @@ -140,7 +140,7 @@ public void testSimpleDateHisto() throws Exception { "the_histo.date_histogram._count", 2, "the_histo.date_histogram.time_zone", - DateTimeZone.UTC.toString(), + "UTC", // TODO: the default is hardcoded from Joda, we should change this "_rollup.id", job.getId() ) @@ -162,7 +162,7 @@ public void testSimpleDateHisto() throws Exception { "the_histo.date_histogram._count", 1, "the_histo.date_histogram.time_zone", - DateTimeZone.UTC.toString(), + "UTC", // TODO: the default is hardcoded from Joda, we should change this "_rollup.id", job.getId() ) @@ -226,7 +226,7 @@ public void testDateHistoAndMetrics() throws Exception { "counter.sum.value", 50.0, "the_histo.date_histogram.time_zone", - DateTimeZone.UTC.toString(), + "UTC", // TODO: the default is hardcoded from Joda, we should change this "_rollup.id", job.getId() ) @@ -258,7 +258,7 @@ public void testDateHistoAndMetrics() throws Exception { "counter.sum.value", 141.0, "the_histo.date_histogram.time_zone", - DateTimeZone.UTC.toString(), + "UTC", // TODO: the default is hardcoded from Joda, we should change this "_rollup.id", job.getId() ) @@ -290,7 +290,7 @@ public void testDateHistoAndMetrics() throws Exception { "counter.sum.value", 275.0, "the_histo.date_histogram.time_zone", - DateTimeZone.UTC.toString(), + "UTC", // TODO: the default is hardcoded from Joda, we should change this "_rollup.id", job.getId() ) @@ -322,7 +322,7 @@ public void testDateHistoAndMetrics() throws Exception { "counter.sum.value", 270.0, "the_histo.date_histogram.time_zone", - DateTimeZone.UTC.toString(), + "UTC", // TODO: the default is hardcoded from Joda, we should change this "_rollup.id", job.getId() ) @@ -354,7 +354,7 @@ public void testDateHistoAndMetrics() throws Exception { "counter.sum.value", 440.0, "the_histo.date_histogram.time_zone", - DateTimeZone.UTC.toString(), + "UTC", // TODO: the default is hardcoded from Joda, we should change this "_rollup.id", job.getId() ) @@ -408,7 +408,7 @@ public void testSimpleDateHistoWithDelay() throws Exception { "the_histo.date_histogram._count", 2, "the_histo.date_histogram.time_zone", - DateTimeZone.UTC.toString(), + "UTC", // TODO: the default is hardcoded from Joda, we should change this "_rollup.id", job.getId() ) @@ -430,7 +430,7 @@ public void testSimpleDateHistoWithDelay() throws Exception { "the_histo.date_histogram._count", 2, "the_histo.date_histogram.time_zone", - DateTimeZone.UTC.toString(), + "UTC", // TODO: the default is hardcoded from Joda, we should change this "_rollup.id", job.getId() ) @@ -452,7 +452,7 @@ public void testSimpleDateHistoWithDelay() throws Exception { "the_histo.date_histogram._count", 1, "the_histo.date_histogram.time_zone", - DateTimeZone.UTC.toString(), + "UTC", // TODO: the default is hardcoded from Joda, we should change this "_rollup.id", job.getId() ) @@ -505,7 +505,7 @@ public void testSimpleDateHistoWithOverlappingDelay() throws Exception { "the_histo.date_histogram._count", 3, "the_histo.date_histogram.time_zone", - DateTimeZone.UTC.toString(), + "UTC", // TODO: the default is hardcoded from Joda, we should change this "_rollup.id", job.getId() ) @@ -526,7 +526,7 @@ public void testSimpleDateHistoWithOverlappingDelay() throws Exception { "the_histo.date_histogram._count", 4, "the_histo.date_histogram.time_zone", - DateTimeZone.UTC.toString(), + "UTC", // TODO: the default is hardcoded from Joda, we should change this "_rollup.id", job.getId() ) @@ -550,7 +550,7 @@ public void testSimpleDateHistoWithTimeZone() throws Exception { ) ); - String timeZone = DateTimeZone.forOffsetHours(-3).getID(); + String timeZone = ZoneOffset.ofHours(-3).getId(); String rollupIndex = randomAlphaOfLengthBetween(5, 10); String field = "the_histo"; DateHistogramGroupConfig dateHistoConfig = new CalendarInterval(field, new DateHistogramInterval("1d"), null, timeZone); @@ -648,7 +648,11 @@ public void testRandomizedDateHisto() throws Exception { for (int i = 0; i < numDocs; i++) { // Make sure the timestamp is sufficiently in the past that we don't get bitten // by internal rounding, causing no docs to match - long timestamp = new DateTime().minusDays(2).minusHours(randomIntBetween(11, 100)).getMillis(); + long timestamp = ZonedDateTime.now(ZoneOffset.UTC) + .minusDays(2) + .minusHours(randomIntBetween(11, 100)) + .toInstant() + .toEpochMilli(); dataset.add(asMap(timestampField, timestamp, valueField, randomLongBetween(1, 100))); } executeTestCase(dataset, job, System.currentTimeMillis(), (resp) -> { diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/audit/index/IndexNameResolver.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/audit/index/IndexNameResolver.java index b4899197c340f..f9d5498ee5066 100644 --- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/audit/index/IndexNameResolver.java +++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/audit/index/IndexNameResolver.java @@ -6,9 +6,9 @@ */ package org.elasticsearch.xpack.security.audit.index; -import org.joda.time.DateTime; -import org.joda.time.format.DateTimeFormat; -import org.joda.time.format.DateTimeFormatter; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Locale; public class IndexNameResolver { @@ -21,7 +21,7 @@ public enum Rollover { private final DateTimeFormatter formatter; Rollover(String format) { - this.formatter = DateTimeFormat.forPattern(format); + this.formatter = DateTimeFormatter.ofPattern(format, Locale.ROOT); } DateTimeFormatter formatter() { @@ -31,11 +31,11 @@ DateTimeFormatter formatter() { private IndexNameResolver() {} - public static String resolve(DateTime timestamp, Rollover rollover) { - return rollover.formatter().print(timestamp); + public static String resolve(ZonedDateTime timestamp, Rollover rollover) { + return rollover.formatter().format(timestamp); } - public static String resolve(String indexNamePrefix, DateTime timestamp, Rollover rollover) { + public static String resolve(String indexNamePrefix, ZonedDateTime timestamp, Rollover rollover) { return indexNamePrefix + resolve(timestamp, rollover); } } diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/IndicesAndAliasesResolverTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/IndicesAndAliasesResolverTests.java index b1e6e28df5edc..cbb6b4b9100ad 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/IndicesAndAliasesResolverTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authz/IndicesAndAliasesResolverTests.java @@ -71,19 +71,20 @@ import org.elasticsearch.xpack.core.security.user.XPackUser; import org.elasticsearch.xpack.security.authz.store.CompositeRolesStore; import org.elasticsearch.xpack.security.test.SecurityTestUtils; -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; -import org.joda.time.format.DateTimeFormat; import org.junit.Before; import java.time.Clock; import java.time.Duration; import java.time.Instant; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Set; @@ -1417,8 +1418,8 @@ public void testUnauthorizedDateMathExpressionIgnoreUnavailableDisallowNoIndices } public void testUnauthorizedDateMathExpressionStrict() { - String expectedIndex = "datetime-" + DateTimeFormat.forPattern("YYYY.MM.dd").print( - new DateTime(DateTimeZone.UTC).monthOfYear().roundFloorCopy()); + String expectedIndex = "datetime-" + DateTimeFormatter.ofPattern("uuuu.MM.dd", Locale.ROOT).format( + ZonedDateTime.now(ZoneOffset.UTC).withDayOfMonth(1)); SearchRequest request = new SearchRequest(""); request.indicesOptions(IndicesOptions.fromOptions(false, randomBoolean(), randomBoolean(), randomBoolean())); IndexNotFoundException e = expectThrows(IndexNotFoundException.class, @@ -1459,8 +1460,8 @@ public void testMissingDateMathExpressionIgnoreUnavailableDisallowNoIndices() { } public void testMissingDateMathExpressionStrict() { - String expectedIndex = "foobar-" + DateTimeFormat.forPattern("YYYY.MM.dd").print( - new DateTime(DateTimeZone.UTC).monthOfYear().roundFloorCopy()); + String expectedIndex = "foobar-" + DateTimeFormatter.ofPattern("uuuu.MM.dd", Locale.ROOT).format( + ZonedDateTime.now(ZoneOffset.UTC).withDayOfMonth(1)); SearchRequest request = new SearchRequest(""); request.indicesOptions(IndicesOptions.fromOptions(false, randomBoolean(), randomBoolean(), randomBoolean())); IndexNotFoundException e = expectThrows(IndexNotFoundException.class, diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/user/XPackUserTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/user/XPackUserTests.java index e6d1516840f21..d3779d7dbc9c5 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/user/XPackUserTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/user/XPackUserTests.java @@ -18,8 +18,9 @@ import org.elasticsearch.xpack.core.security.user.XPackUser; import org.elasticsearch.xpack.security.audit.index.IndexNameResolver; import org.hamcrest.Matchers; -import org.joda.time.DateTime; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; import java.util.Arrays; import java.util.function.Predicate; @@ -66,7 +67,7 @@ public void testXPackUserCannotWriteToAuditTrail() { } private String getAuditLogName() { - final DateTime date = new DateTime().plusDays(randomIntBetween(1, 360)); + final ZonedDateTime date = ZonedDateTime.now(ZoneOffset.UTC).plusDays(randomIntBetween(1, 360)); final IndexNameResolver.Rollover rollover = randomFrom(IndexNameResolver.Rollover.values()); return IndexNameResolver.resolve(IndexAuditTrailField.INDEX_NAME_PREFIX, date, rollover); } diff --git a/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformIntegTestCase.java b/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformIntegTestCase.java index 70351131054e2..fd42c4d72ed6c 100644 --- a/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformIntegTestCase.java +++ b/x-pack/plugin/transform/qa/multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformIntegTestCase.java @@ -65,10 +65,10 @@ import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.test.rest.ESRestTestCase; -import org.joda.time.Instant; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.time.Instant; import java.time.ZoneId; import java.util.Base64; import java.util.Collections; diff --git a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformRestTestCase.java b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformRestTestCase.java index f9e9eb18273be..46a22135a6b06 100644 --- a/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformRestTestCase.java +++ b/x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformRestTestCase.java @@ -26,12 +26,12 @@ import org.elasticsearch.test.rest.ESRestTestCase; import org.elasticsearch.xpack.core.transform.TransformField; import org.elasticsearch.xpack.core.transform.transforms.persistence.TransformInternalIndexConstants; -import org.joda.time.Instant; import org.junit.After; import org.junit.AfterClass; import org.junit.BeforeClass; import java.io.IOException; +import java.time.Instant; import java.util.Arrays; import java.util.Collections; import java.util.List;