From 2629f087d0f01004c0ac6a78bb7cb92f948ae50c Mon Sep 17 00:00:00 2001 From: David Zane Date: Mon, 23 Dec 2024 13:19:01 -1000 Subject: [PATCH] Migrate from Joda-Time to java.time API Signed-off-by: David Zane --- .../core/exporter/LocalIndexExporter.java | 8 +++--- .../QueryInsightsExporterFactory.java | 6 ++--- .../core/reader/LocalIndexReader.java | 27 ++++++++++--------- .../reader/QueryInsightsReaderFactory.java | 11 +++++--- .../core/service/TopQueriesService.java | 18 ++++++------- .../top_queries/RestTopQueriesAction.java | 4 +-- .../exporter/LocalIndexExporterTests.java | 8 +++--- .../QueryInsightsExporterFactoryTests.java | 7 ++--- .../core/reader/LocalIndexReaderTests.java | 16 +++++------ .../QueryInsightsReaderFactoryTests.java | 7 ++--- .../service/QueryInsightsServiceTests.java | 11 ++++++++ .../RestTopQueriesActionTests.java | 17 ++++++------ 12 files changed, 81 insertions(+), 59 deletions(-) diff --git a/src/main/java/org/opensearch/plugin/insights/core/exporter/LocalIndexExporter.java b/src/main/java/org/opensearch/plugin/insights/core/exporter/LocalIndexExporter.java index a93bd88b..ef396bcc 100644 --- a/src/main/java/org/opensearch/plugin/insights/core/exporter/LocalIndexExporter.java +++ b/src/main/java/org/opensearch/plugin/insights/core/exporter/LocalIndexExporter.java @@ -8,12 +8,12 @@ package org.opensearch.plugin.insights.core.exporter; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.util.List; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; -import org.joda.time.format.DateTimeFormatter; import org.opensearch.action.bulk.BulkRequestBuilder; import org.opensearch.action.bulk.BulkResponse; import org.opensearch.action.index.IndexRequest; @@ -111,6 +111,6 @@ public void close() { } private String getDateTimeFromFormat() { - return indexPattern.print(DateTime.now(DateTimeZone.UTC)); + return indexPattern.format(ZonedDateTime.now(ZoneOffset.UTC)); } } diff --git a/src/main/java/org/opensearch/plugin/insights/core/exporter/QueryInsightsExporterFactory.java b/src/main/java/org/opensearch/plugin/insights/core/exporter/QueryInsightsExporterFactory.java index 1ba82a22..3a2c2fa7 100644 --- a/src/main/java/org/opensearch/plugin/insights/core/exporter/QueryInsightsExporterFactory.java +++ b/src/main/java/org/opensearch/plugin/insights/core/exporter/QueryInsightsExporterFactory.java @@ -12,12 +12,12 @@ import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.EXPORTER_TYPE; import java.io.IOException; +import java.time.format.DateTimeFormatter; import java.util.HashSet; import java.util.Locale; import java.util.Set; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.joda.time.format.DateTimeFormat; import org.opensearch.client.Client; import org.opensearch.common.settings.Settings; import org.opensearch.plugin.insights.core.metrics.OperationalMetric; @@ -80,7 +80,7 @@ public void validateExporterConfig(final Settings settings) throws IllegalArgume */ public QueryInsightsExporter createExporter(SinkType type, String indexPattern) { if (SinkType.LOCAL_INDEX.equals(type)) { - QueryInsightsExporter exporter = new LocalIndexExporter(client, DateTimeFormat.forPattern(indexPattern)); + QueryInsightsExporter exporter = new LocalIndexExporter(client, DateTimeFormatter.ofPattern(indexPattern, Locale.ROOT)); this.exporters.add(exporter); return exporter; } @@ -96,7 +96,7 @@ public QueryInsightsExporter createExporter(SinkType type, String indexPattern) */ public QueryInsightsExporter updateExporter(QueryInsightsExporter exporter, String indexPattern) { if (exporter.getClass() == LocalIndexExporter.class) { - ((LocalIndexExporter) exporter).setIndexPattern(DateTimeFormat.forPattern(indexPattern)); + ((LocalIndexExporter) exporter).setIndexPattern(DateTimeFormatter.ofPattern(indexPattern, Locale.ROOT)); } return exporter; } diff --git a/src/main/java/org/opensearch/plugin/insights/core/reader/LocalIndexReader.java b/src/main/java/org/opensearch/plugin/insights/core/reader/LocalIndexReader.java index 9c549b67..df8ba7a6 100644 --- a/src/main/java/org/opensearch/plugin/insights/core/reader/LocalIndexReader.java +++ b/src/main/java/org/opensearch/plugin/insights/core/reader/LocalIndexReader.java @@ -8,13 +8,13 @@ package org.opensearch.plugin.insights.core.reader; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; -import org.joda.time.format.DateTimeFormatter; import org.opensearch.action.search.SearchRequest; import org.opensearch.action.search.SearchResponse; import org.opensearch.client.Client; @@ -91,18 +91,21 @@ public List read(final String from, final String to) { if (from == null || to == null) { return records; } - final DateTime start = DateTime.parse(from); - DateTime end = DateTime.parse(to); - if (end.compareTo(DateTime.now(DateTimeZone.UTC)) > 0) { - end = DateTime.now(DateTimeZone.UTC); + final ZonedDateTime start = ZonedDateTime.parse(from); + ZonedDateTime end = ZonedDateTime.parse(to); + ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC); + if (end.isAfter(now)) { + end = now; } - DateTime curr = start; - while (curr.compareTo(end.plusDays(1).withTimeAtStartOfDay()) < 0) { + ZonedDateTime curr = start; + while (curr.isBefore(end.plusDays(1).toLocalDate().atStartOfDay(end.getZone()))) { String index = getDateTimeFromFormat(curr); SearchRequest searchRequest = new SearchRequest(index); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); MatchQueryBuilder excludeQuery = QueryBuilders.matchQuery("indices", "top_queries*"); - RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("timestamp").from(start.getMillis()).to(end.getMillis()); + RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("timestamp") + .from(start.toInstant().toEpochMilli()) + .to(end.toInstant().toEpochMilli()); QueryBuilder query = QueryBuilders.boolQuery().must(rangeQuery).mustNot(excludeQuery); searchSourceBuilder.query(query); searchRequest.source(searchSourceBuilder); @@ -132,7 +135,7 @@ public void close() { logger.debug("Closing the LocalIndexReader.."); } - private String getDateTimeFromFormat(DateTime current) { - return indexPattern.print(current); + private String getDateTimeFromFormat(ZonedDateTime current) { + return current.format(indexPattern); } } diff --git a/src/main/java/org/opensearch/plugin/insights/core/reader/QueryInsightsReaderFactory.java b/src/main/java/org/opensearch/plugin/insights/core/reader/QueryInsightsReaderFactory.java index 8d765438..0f3c2701 100644 --- a/src/main/java/org/opensearch/plugin/insights/core/reader/QueryInsightsReaderFactory.java +++ b/src/main/java/org/opensearch/plugin/insights/core/reader/QueryInsightsReaderFactory.java @@ -9,11 +9,12 @@ package org.opensearch.plugin.insights.core.reader; import java.io.IOException; +import java.time.format.DateTimeFormatter; import java.util.HashSet; +import java.util.Locale; import java.util.Set; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.joda.time.format.DateTimeFormat; import org.opensearch.client.Client; import org.opensearch.core.xcontent.NamedXContentRegistry; @@ -46,7 +47,11 @@ public QueryInsightsReaderFactory(final Client client) { * @return QueryInsightsReader the created Reader */ public QueryInsightsReader createReader(String indexPattern, NamedXContentRegistry namedXContentRegistry) { - QueryInsightsReader Reader = new LocalIndexReader(client, DateTimeFormat.forPattern(indexPattern), namedXContentRegistry); + QueryInsightsReader Reader = new LocalIndexReader( + client, + DateTimeFormatter.ofPattern(indexPattern, Locale.ROOT), + namedXContentRegistry + ); this.Readers.add(Reader); return Reader; } @@ -60,7 +65,7 @@ public QueryInsightsReader createReader(String indexPattern, NamedXContentRegist */ public QueryInsightsReader updateReader(QueryInsightsReader Reader, String indexPattern) { if (Reader.getClass() == LocalIndexReader.class) { - ((LocalIndexReader) Reader).setIndexPattern(DateTimeFormat.forPattern(indexPattern)); + ((LocalIndexReader) Reader).setIndexPattern(DateTimeFormatter.ofPattern(indexPattern, Locale.ROOT)); } return Reader; } diff --git a/src/main/java/org/opensearch/plugin/insights/core/service/TopQueriesService.java b/src/main/java/org/opensearch/plugin/insights/core/service/TopQueriesService.java index 9e3e5d35..3b533f4c 100644 --- a/src/main/java/org/opensearch/plugin/insights/core/service/TopQueriesService.java +++ b/src/main/java/org/opensearch/plugin/insights/core/service/TopQueriesService.java @@ -18,6 +18,7 @@ import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZoneOffset; +import java.time.ZonedDateTime; import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.Arrays; @@ -32,7 +33,6 @@ import java.util.stream.Stream; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.joda.time.DateTime; import org.opensearch.common.settings.Settings; import org.opensearch.common.unit.TimeValue; import org.opensearch.core.xcontent.NamedXContentRegistry; @@ -362,10 +362,10 @@ public List getTopQueriesRecords(final boolean includeLastWin } List filterQueries = queries; if (from != null && to != null) { - final DateTime start = DateTime.parse(from); - final DateTime end = DateTime.parse(to); - Predicate timeFilter = element -> start.getMillis() <= element.getTimestamp() - && element.getTimestamp() <= end.getMillis(); + final ZonedDateTime start = ZonedDateTime.parse(from); + final ZonedDateTime end = ZonedDateTime.parse(to); + Predicate timeFilter = element -> start.toInstant().toEpochMilli() <= element.getTimestamp() + && element.getTimestamp() <= end.toInstant().toEpochMilli(); filterQueries = queries.stream().filter(checkIfInternal.and(timeFilter)).collect(Collectors.toList()); } return Stream.of(filterQueries) @@ -394,11 +394,11 @@ public List getTopQueriesRecordsFromIndex(final String from, final List queries = new ArrayList<>(); if (reader != null) { try { - final DateTime start = DateTime.parse(from); - final DateTime end = DateTime.parse(to); + final ZonedDateTime start = ZonedDateTime.parse(from); + final ZonedDateTime end = ZonedDateTime.parse(to); List records = reader.read(from, to); - Predicate timeFilter = element -> start.getMillis() <= element.getTimestamp() - && element.getTimestamp() <= end.getMillis(); + Predicate timeFilter = element -> start.toInstant().toEpochMilli() <= element.getTimestamp() + && element.getTimestamp() <= end.toInstant().toEpochMilli(); List filteredRecords = records.stream() .filter(checkIfInternal.and(timeFilter)) .collect(Collectors.toList()); diff --git a/src/main/java/org/opensearch/plugin/insights/rules/resthandler/top_queries/RestTopQueriesAction.java b/src/main/java/org/opensearch/plugin/insights/rules/resthandler/top_queries/RestTopQueriesAction.java index 270dd5f5..a7538faf 100644 --- a/src/main/java/org/opensearch/plugin/insights/rules/resthandler/top_queries/RestTopQueriesAction.java +++ b/src/main/java/org/opensearch/plugin/insights/rules/resthandler/top_queries/RestTopQueriesAction.java @@ -11,11 +11,11 @@ import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.TOP_QUERIES_BASE_URI; import static org.opensearch.rest.RestRequest.Method.GET; +import java.time.ZonedDateTime; import java.util.List; import java.util.Locale; import java.util.Set; import java.util.stream.Collectors; -import org.joda.time.DateTime; import org.opensearch.client.node.NodeClient; import org.opensearch.common.settings.Settings; import org.opensearch.core.common.Strings; @@ -67,7 +67,7 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC private static boolean isNotISODate(final String dateTime) { try { - DateTime.parse(dateTime); + ZonedDateTime.parse(dateTime); return false; } catch (Exception e) { return true; diff --git a/src/test/java/org/opensearch/plugin/insights/core/exporter/LocalIndexExporterTests.java b/src/test/java/org/opensearch/plugin/insights/core/exporter/LocalIndexExporterTests.java index ebd9b03e..a7c934a9 100644 --- a/src/test/java/org/opensearch/plugin/insights/core/exporter/LocalIndexExporterTests.java +++ b/src/test/java/org/opensearch/plugin/insights/core/exporter/LocalIndexExporterTests.java @@ -14,9 +14,9 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; +import java.time.format.DateTimeFormatter; import java.util.List; -import org.joda.time.format.DateTimeFormat; -import org.joda.time.format.DateTimeFormatter; +import java.util.Locale; import org.junit.Before; import org.opensearch.action.bulk.BulkAction; import org.opensearch.action.bulk.BulkRequestBuilder; @@ -31,7 +31,7 @@ * Granular tests for the {@link LocalIndexExporterTests} class. */ public class LocalIndexExporterTests extends OpenSearchTestCase { - private final DateTimeFormatter format = DateTimeFormat.forPattern("YYYY.MM.dd"); + private final DateTimeFormatter format = DateTimeFormatter.ofPattern("YYYY.MM.dd", Locale.ROOT); private final Client client = mock(Client.class); private LocalIndexExporter localIndexExporter; @@ -91,7 +91,7 @@ public void testClose() { } public void testGetAndSetIndexPattern() { - DateTimeFormatter newFormatter = mock(DateTimeFormatter.class); + final DateTimeFormatter newFormatter = DateTimeFormatter.ofPattern("YYYY-MM-dd", Locale.ROOT); localIndexExporter.setIndexPattern(newFormatter); assert (localIndexExporter.getIndexPattern() == newFormatter); } diff --git a/src/test/java/org/opensearch/plugin/insights/core/exporter/QueryInsightsExporterFactoryTests.java b/src/test/java/org/opensearch/plugin/insights/core/exporter/QueryInsightsExporterFactoryTests.java index 9a52ab00..99c96280 100644 --- a/src/test/java/org/opensearch/plugin/insights/core/exporter/QueryInsightsExporterFactoryTests.java +++ b/src/test/java/org/opensearch/plugin/insights/core/exporter/QueryInsightsExporterFactoryTests.java @@ -13,7 +13,8 @@ import static org.mockito.Mockito.when; import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.EXPORTER_TYPE; -import org.joda.time.format.DateTimeFormat; +import java.time.format.DateTimeFormatter; +import java.util.Locale; import org.junit.Before; import org.opensearch.client.Client; import org.opensearch.common.settings.Settings; @@ -76,9 +77,9 @@ public void testCreateAndCloseExporter() { } public void testUpdateExporter() { - LocalIndexExporter exporter = new LocalIndexExporter(client, DateTimeFormat.forPattern("yyyy-MM-dd")); + LocalIndexExporter exporter = new LocalIndexExporter(client, DateTimeFormatter.ofPattern(format, Locale.ROOT)); queryInsightsExporterFactory.updateExporter(exporter, "yyyy-MM-dd-HH"); - assertEquals(DateTimeFormat.forPattern("yyyy-MM-dd-HH"), exporter.getIndexPattern()); + assertEquals(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH", Locale.ROOT).toString(), exporter.getIndexPattern().toString()); } } diff --git a/src/test/java/org/opensearch/plugin/insights/core/reader/LocalIndexReaderTests.java b/src/test/java/org/opensearch/plugin/insights/core/reader/LocalIndexReaderTests.java index f6f28486..5d607f67 100644 --- a/src/test/java/org/opensearch/plugin/insights/core/reader/LocalIndexReaderTests.java +++ b/src/test/java/org/opensearch/plugin/insights/core/reader/LocalIndexReaderTests.java @@ -13,15 +13,15 @@ import static org.mockito.Mockito.when; import java.io.IOException; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.util.Collections; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import org.apache.lucene.search.TotalHits; -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; -import org.joda.time.format.DateTimeFormat; -import org.joda.time.format.DateTimeFormatter; import org.junit.Before; import org.opensearch.action.search.SearchRequest; import org.opensearch.action.search.SearchResponse; @@ -41,7 +41,7 @@ * Granular tests for the {@link LocalIndexReaderTests} class. */ public class LocalIndexReaderTests extends OpenSearchTestCase { - private final DateTimeFormatter format = DateTimeFormat.forPattern("YYYY.MM.dd"); + private final DateTimeFormatter format = DateTimeFormatter.ofPattern("YYYY.MM.dd", Locale.ROOT); private final Client client = mock(Client.class); private final NamedXContentRegistry namedXContentRegistry = mock(NamedXContentRegistry.class); private LocalIndexReader localIndexReader; @@ -55,7 +55,7 @@ public void setup() { public void testReadRecords() { ActionFuture responseActionFuture = mock(ActionFuture.class); Map sourceMap = new HashMap<>(); - sourceMap.put("timestamp", DateTime.now(DateTimeZone.UTC).getMillis()); + sourceMap.put("timestamp", ZonedDateTime.now(ZoneOffset.UTC).toInstant().toEpochMilli()); sourceMap.put("indices", Collections.singletonList("my-index-0")); sourceMap.put("source", Map.of()); sourceMap.put("labels", Map.of()); @@ -83,7 +83,7 @@ public void testReadRecords() { when(searchResponse.getHits()).thenReturn(searchHits); when(responseActionFuture.actionGet()).thenReturn(searchResponse); when(client.search(any(SearchRequest.class))).thenReturn(responseActionFuture); - String time = DateTime.now(DateTimeZone.UTC).toString(); + String time = ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_DATE_TIME); List records = List.of(); try { records = localIndexReader.read(time, time); @@ -103,7 +103,7 @@ public void testClose() { } public void testGetAndSetIndexPattern() { - DateTimeFormatter newFormatter = mock(DateTimeFormatter.class); + final DateTimeFormatter newFormatter = DateTimeFormatter.ofPattern("YYYY-MM-dd", Locale.ROOT); localIndexReader.setIndexPattern(newFormatter); assert (localIndexReader.getIndexPattern() == newFormatter); } diff --git a/src/test/java/org/opensearch/plugin/insights/core/reader/QueryInsightsReaderFactoryTests.java b/src/test/java/org/opensearch/plugin/insights/core/reader/QueryInsightsReaderFactoryTests.java index 0a6e8159..74adcac2 100644 --- a/src/test/java/org/opensearch/plugin/insights/core/reader/QueryInsightsReaderFactoryTests.java +++ b/src/test/java/org/opensearch/plugin/insights/core/reader/QueryInsightsReaderFactoryTests.java @@ -13,7 +13,8 @@ import static org.mockito.Mockito.when; import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.DEFAULT_TOP_N_QUERIES_INDEX_PATTERN; -import org.joda.time.format.DateTimeFormat; +import java.time.format.DateTimeFormatter; +import java.util.Locale; import org.junit.Before; import org.opensearch.client.Client; import org.opensearch.core.xcontent.NamedXContentRegistry; @@ -55,9 +56,9 @@ public void testCreateAndCloseReader() { } public void testUpdateReader() { - LocalIndexReader reader = new LocalIndexReader(client, DateTimeFormat.forPattern(format), namedXContentRegistry); + LocalIndexReader reader = new LocalIndexReader(client, DateTimeFormatter.ofPattern(format, Locale.ROOT), namedXContentRegistry); queryInsightsReaderFactory.updateReader(reader, "yyyy-MM-dd-HH"); - assertEquals(DateTimeFormat.forPattern("yyyy-MM-dd-HH"), reader.getIndexPattern()); + assertEquals(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH", Locale.ROOT).toString(), reader.getIndexPattern().toString()); } } diff --git a/src/test/java/org/opensearch/plugin/insights/core/service/QueryInsightsServiceTests.java b/src/test/java/org/opensearch/plugin/insights/core/service/QueryInsightsServiceTests.java index 274257a2..279fee2a 100644 --- a/src/test/java/org/opensearch/plugin/insights/core/service/QueryInsightsServiceTests.java +++ b/src/test/java/org/opensearch/plugin/insights/core/service/QueryInsightsServiceTests.java @@ -8,8 +8,10 @@ package org.opensearch.plugin.insights.core.service; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; import java.util.List; import java.util.Map; @@ -21,12 +23,15 @@ import org.opensearch.common.unit.TimeValue; import org.opensearch.core.xcontent.NamedXContentRegistry; import org.opensearch.plugin.insights.QueryInsightsTestUtils; +import org.opensearch.plugin.insights.core.metrics.OperationalMetricsCounter; import org.opensearch.plugin.insights.rules.model.GroupingType; import org.opensearch.plugin.insights.rules.model.MetricType; import org.opensearch.plugin.insights.rules.model.SearchQueryRecord; import org.opensearch.plugin.insights.rules.model.healthStats.QueryInsightsHealthStats; import org.opensearch.plugin.insights.rules.model.healthStats.TopQueriesHealthStats; import org.opensearch.plugin.insights.settings.QueryInsightsSettings; +import org.opensearch.telemetry.metrics.Counter; +import org.opensearch.telemetry.metrics.MetricsRegistry; import org.opensearch.telemetry.metrics.noop.NoopMetricsRegistry; import org.opensearch.test.OpenSearchTestCase; import org.opensearch.threadpool.ScalingExecutorBuilder; @@ -64,6 +69,12 @@ public void setup() { queryInsightsService.enableCollection(MetricType.CPU, true); queryInsightsService.enableCollection(MetricType.MEMORY, true); queryInsightsServiceSpy = spy(queryInsightsService); + + MetricsRegistry metricsRegistry = mock(MetricsRegistry.class); + when(metricsRegistry.createCounter(any(String.class), any(String.class), any(String.class))).thenAnswer( + invocation -> mock(Counter.class) + ); + OperationalMetricsCounter.initialize("cluster", metricsRegistry); } @Override diff --git a/src/test/java/org/opensearch/plugin/insights/rules/resthandler/top_queries/RestTopQueriesActionTests.java b/src/test/java/org/opensearch/plugin/insights/rules/resthandler/top_queries/RestTopQueriesActionTests.java index 4028d4f9..8043ee52 100644 --- a/src/test/java/org/opensearch/plugin/insights/rules/resthandler/top_queries/RestTopQueriesActionTests.java +++ b/src/test/java/org/opensearch/plugin/insights/rules/resthandler/top_queries/RestTopQueriesActionTests.java @@ -10,12 +10,13 @@ import static org.opensearch.plugin.insights.rules.resthandler.top_queries.RestTopQueriesAction.ALLOWED_METRICS; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; import org.opensearch.plugin.insights.rules.action.top_queries.TopQueriesRequest; import org.opensearch.rest.RestHandler; import org.opensearch.rest.RestRequest; @@ -27,8 +28,8 @@ public class RestTopQueriesActionTests extends OpenSearchTestCase { public void testEmptyNodeIdsValidType() { Map params = new HashMap<>(); params.put("type", randomFrom(ALLOWED_METRICS)); - params.put("from", DateTime.now(DateTimeZone.UTC).toString()); - params.put("to", DateTime.now(DateTimeZone.UTC).toString()); + params.put("from", ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_DATE_TIME)); + params.put("to", ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_DATE_TIME)); RestRequest restRequest = buildRestRequest(params); TopQueriesRequest actual = RestTopQueriesAction.prepareRequest(restRequest); assertEquals(0, actual.nodesIds().length); @@ -60,7 +61,7 @@ public void testInValidType() { public void testInValidFrom() { Map params = new HashMap<>(); params.put("from", "not valid timestamp"); - params.put("to", DateTime.now(DateTimeZone.UTC).toString()); + params.put("to", ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_DATE_TIME)); RestRequest restRequest = buildRestRequest(params); Exception exception = assertThrows(IllegalArgumentException.class, () -> { RestTopQueriesAction.prepareRequest(restRequest); }); assertEquals( @@ -75,7 +76,7 @@ public void testInValidFrom() { public void testInValidTo() { Map params = new HashMap<>(); - params.put("from", DateTime.now(DateTimeZone.UTC).toString()); + params.put("from", ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_DATE_TIME)); params.put("to", "not valid timestamp"); RestRequest restRequest = buildRestRequest(params); Exception exception = assertThrows(IllegalArgumentException.class, () -> { RestTopQueriesAction.prepareRequest(restRequest); }); @@ -91,7 +92,7 @@ public void testInValidTo() { public void testMissingOneTimeParam() { Map params = new HashMap<>(); - params.put("from", DateTime.now(DateTimeZone.UTC).toString()); + params.put("from", ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_DATE_TIME)); RestRequest restRequest = buildRestRequest(params); Exception exception = assertThrows(IllegalArgumentException.class, () -> { RestTopQueriesAction.prepareRequest(restRequest); }); assertEquals( @@ -99,7 +100,7 @@ public void testMissingOneTimeParam() { exception.getMessage() ); Map params2 = new HashMap<>(); - params2.put("to", DateTime.now(DateTimeZone.UTC).toString()); + params2.put("to", ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_DATE_TIME)); RestRequest restRequest2 = buildRestRequest(params2); Exception exception2 = assertThrows(IllegalArgumentException.class, () -> { RestTopQueriesAction.prepareRequest(restRequest2); }); assertEquals(