From 765dd895df201966e7dea5851e29638dde115ce2 Mon Sep 17 00:00:00 2001 From: bpcreech <35012922+bpcreech@users.noreply.github.com> Date: Thu, 18 Aug 2022 14:27:33 -0400 Subject: [PATCH] fix: Replace Date with LocalDateTime (#1012) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Switch from Date to LocalDateTime * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Ben Creech Co-authored-by: Owl Bot --- .../cloud/logging/TimestampDefaultFilter.java | 23 +++++------ .../google/cloud/logging/BaseSystemTest.java | 18 +++++---- .../logging/TimestampDefaultFilterTest.java | 39 ++++++++----------- 3 files changed, 38 insertions(+), 42 deletions(-) diff --git a/google-cloud-logging/src/main/java/com/google/cloud/logging/TimestampDefaultFilter.java b/google-cloud-logging/src/main/java/com/google/cloud/logging/TimestampDefaultFilter.java index 5734ddb0a..cea25a74a 100644 --- a/google-cloud-logging/src/main/java/com/google/cloud/logging/TimestampDefaultFilter.java +++ b/google-cloud-logging/src/main/java/com/google/cloud/logging/TimestampDefaultFilter.java @@ -16,25 +16,22 @@ package com.google.cloud.logging; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.Date; -import java.util.TimeZone; +import static java.time.ZoneOffset.UTC; +import static java.util.Locale.US; + +import java.time.Duration; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; public class TimestampDefaultFilter implements ITimestampDefaultFilter { @Override public String createDefaultTimestampFilter() { - DateFormat rfcDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); - rfcDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); + DateTimeFormatter rfcDateFormat = + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", US); return "timestamp>=\"" + rfcDateFormat.format(yesterday()) + "\""; } - private Date yesterday() { - TimeZone timeZone = TimeZone.getTimeZone("UTC"); - Calendar calendar = Calendar.getInstance(timeZone); - calendar.add(Calendar.DATE, -1); - - return calendar.getTime(); + private LocalDateTime yesterday() { + return LocalDateTime.now(UTC).minus(Duration.ofDays(1)); } } diff --git a/google-cloud-logging/src/test/java/com/google/cloud/logging/BaseSystemTest.java b/google-cloud-logging/src/test/java/com/google/cloud/logging/BaseSystemTest.java index 8c90a3e75..1e63dbdb2 100644 --- a/google-cloud-logging/src/test/java/com/google/cloud/logging/BaseSystemTest.java +++ b/google-cloud-logging/src/test/java/com/google/cloud/logging/BaseSystemTest.java @@ -16,13 +16,14 @@ package com.google.cloud.logging; +import static java.time.ZoneOffset.UTC; + import com.google.api.gax.paging.Page; import com.google.cloud.MonitoredResource; import com.google.cloud.logging.testing.RemoteLoggingHelper; -import com.google.common.collect.Iterators; +import com.google.common.collect.Iterables; import com.google.logging.v2.LogName; -import java.text.DateFormat; -import java.text.SimpleDateFormat; +import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.Iterator; import org.junit.AfterClass; @@ -38,7 +39,8 @@ public class BaseSystemTest { @Rule public Timeout globalTimeout = Timeout.seconds(600); - private static DateFormat RFC_3339 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); + private static final DateTimeFormatter RFC_3339 = + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); protected static Logging logging; @@ -60,7 +62,7 @@ public static void afterClass() throws Exception { * Filters Documentation */ protected static String createEqualityFilter(String name, V value) { - return name + "=" + "\"" + value.toString() + "\""; + return name + "=" + "\"" + value + "\""; } protected static boolean cleanupLog(String logName) throws InterruptedException { @@ -84,7 +86,9 @@ protected static boolean cleanupLog(String logName) throws InterruptedException protected static String createTimestampFilter(int hoursAgo) { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.HOUR, -1 * hoursAgo); - return "timestamp>=\"" + RFC_3339.format(calendar.getTime()) + "\""; + return "timestamp>=\"" + + calendar.getTime().toInstant().atZone(UTC).toLocalDateTime().format(RFC_3339) + + "\""; } protected static String appendResourceTypeFilter( @@ -133,7 +137,7 @@ protected static Iterator waitForLogs(LogName logName) throws Interrup protected static Iterator waitForLogs(Logging.EntryListOption[] options, int minLogs) throws InterruptedException { Page page = logging.listLogEntries(options); - while (Iterators.size(page.iterateAll().iterator()) < minLogs) { + while (Iterables.size(page.iterateAll()) < minLogs) { Thread.sleep(500); page = logging.listLogEntries(options); } diff --git a/google-cloud-logging/src/test/java/com/google/cloud/logging/TimestampDefaultFilterTest.java b/google-cloud-logging/src/test/java/com/google/cloud/logging/TimestampDefaultFilterTest.java index 49618c41f..bf821d03e 100644 --- a/google-cloud-logging/src/test/java/com/google/cloud/logging/TimestampDefaultFilterTest.java +++ b/google-cloud-logging/src/test/java/com/google/cloud/logging/TimestampDefaultFilterTest.java @@ -16,15 +16,13 @@ package com.google.cloud.logging; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static com.google.common.truth.Truth.assertThat; +import static java.time.ZoneOffset.UTC; +import static java.util.Locale.US; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.Date; -import java.util.TimeZone; -import javax.management.timer.Timer; +import java.time.Duration; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import org.junit.Test; public class TimestampDefaultFilterTest { @@ -33,23 +31,20 @@ public class TimestampDefaultFilterTest { public void DefaultTimestampFilterTest() { ITimestampDefaultFilter filter = new TimestampDefaultFilter(); - TimeZone timeZone = TimeZone.getTimeZone("UTC"); - Calendar calendar = Calendar.getInstance(timeZone); - calendar.add(Calendar.DATE, -1); - Date expected = calendar.getTime(); - // Timestamp filter exists String defaultFilter = filter.createDefaultTimestampFilter(); - assertTrue(defaultFilter.contains("timestamp>=")); + assertThat(defaultFilter).contains("timestamp>="); // Time is last 24 hours - try { - DateFormat rfcDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); - rfcDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - Date actual = rfcDateFormat.parse(defaultFilter.substring(12, defaultFilter.length() - 1)); - assertTrue(Math.abs(expected.getTime() - actual.getTime()) < Timer.ONE_MINUTE); - } catch (java.text.ParseException ex) { - fail(); // Just fail if exception is thrown - } + DateTimeFormatter rfcDateFormat = + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", US); + LocalDateTime actual = + LocalDateTime.parse(defaultFilter.substring(12, defaultFilter.length() - 1), rfcDateFormat); + assertThat( + Duration.between(actual, LocalDateTime.now(UTC)) + .minus(Duration.ofDays(1)) + .abs() + .compareTo(Duration.ofMinutes(1))) + .isLessThan(0); } }