Skip to content

Commit

Permalink
Remove uses of Joda from xpack tests (elastic#78660) (elastic#78800)
Browse files Browse the repository at this point in the history
* Remove uses of Joda from xpack tests (elastic#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 <[email protected]>
  • Loading branch information
rjernst and elasticmachine authored Oct 7, 2021
1 parent 5c7cf55 commit c874da4
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
)
Expand All @@ -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()
)
Expand Down Expand Up @@ -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()
)
Expand Down Expand Up @@ -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()
)
Expand Down Expand Up @@ -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()
)
Expand Down Expand Up @@ -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()
)
Expand Down Expand Up @@ -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()
)
Expand Down Expand Up @@ -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()
)
Expand All @@ -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()
)
Expand All @@ -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()
)
Expand Down Expand Up @@ -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()
)
Expand All @@ -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()
)
Expand All @@ -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);
Expand Down Expand Up @@ -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) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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() {
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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("<datetime-{now/M}>");
request.indicesOptions(IndicesOptions.fromOptions(false, randomBoolean(), randomBoolean(), randomBoolean()));
IndexNotFoundException e = expectThrows(IndexNotFoundException.class,
Expand Down Expand Up @@ -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("<foobar-{now/M}>");
request.indicesOptions(IndicesOptions.fromOptions(false, randomBoolean(), randomBoolean(), randomBoolean()));
IndexNotFoundException e = expectThrows(IndexNotFoundException.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

0 comments on commit c874da4

Please sign in to comment.