Switch all added usages of java.time to be joda.time

This is for consistency, mostly the LocalDate fields added in []

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=224525451
This commit is contained in:
jianglai 2018-12-07 09:13:23 -08:00
parent 68320ebad8
commit 305b1edc85
10 changed files with 30 additions and 40 deletions

View file

@ -21,8 +21,6 @@ import google.registry.config.RegistryConfig.Config;
import google.registry.util.Retrier; import google.registry.util.Retrier;
import google.registry.util.SqlTemplate; import google.registry.util.SqlTemplate;
import java.io.Serializable; import java.io.Serializable;
import java.time.LocalDate;
import java.time.YearMonth;
import javax.inject.Inject; import javax.inject.Inject;
import org.apache.beam.runners.dataflow.DataflowRunner; import org.apache.beam.runners.dataflow.DataflowRunner;
import org.apache.beam.runners.dataflow.options.DataflowPipelineOptions; import org.apache.beam.runners.dataflow.options.DataflowPipelineOptions;
@ -41,6 +39,8 @@ import org.apache.beam.sdk.values.KV;
import org.apache.beam.sdk.values.PCollection; import org.apache.beam.sdk.values.PCollection;
import org.apache.beam.sdk.values.TypeDescriptor; import org.apache.beam.sdk.values.TypeDescriptor;
import org.apache.beam.sdk.values.TypeDescriptors; import org.apache.beam.sdk.values.TypeDescriptors;
import org.joda.time.LocalDate;
import org.joda.time.YearMonth;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -64,7 +64,7 @@ public class Spec11Pipeline implements Serializable {
* @see google.registry.reporting.spec11.Spec11EmailUtils * @see google.registry.reporting.spec11.Spec11EmailUtils
*/ */
public static String getSpec11ReportFilePath(LocalDate localDate) { public static String getSpec11ReportFilePath(LocalDate localDate) {
YearMonth yearMonth = YearMonth.of(localDate.getYear(), localDate.getMonth()); YearMonth yearMonth = new YearMonth(localDate);
return String.format("icann/spec11/%s/SPEC11_MONTHLY_REPORT_%s", yearMonth, localDate); return String.format("icann/spec11/%s/SPEC11_MONTHLY_REPORT_%s", yearMonth, localDate);
} }

View file

@ -26,14 +26,12 @@ import google.registry.config.RegistryConfig.Config;
import google.registry.request.HttpException.BadRequestException; import google.registry.request.HttpException.BadRequestException;
import google.registry.request.Parameter; import google.registry.request.Parameter;
import google.registry.util.Clock; import google.registry.util.Clock;
import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import java.util.Optional; import java.util.Optional;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.joda.time.DateTime; import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;
import org.joda.time.YearMonth; import org.joda.time.YearMonth;
import org.joda.time.format.DateTimeFormat; import org.joda.time.format.ISODateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
/** Dagger module for injecting common settings for all reporting tasks. */ /** Dagger module for injecting common settings for all reporting tasks. */
@Module @Module
@ -68,10 +66,9 @@ public class ReportingModule {
@Provides @Provides
@Parameter(PARAM_YEAR_MONTH) @Parameter(PARAM_YEAR_MONTH)
static Optional<YearMonth> provideYearMonthOptional(HttpServletRequest req) { static Optional<YearMonth> provideYearMonthOptional(HttpServletRequest req) {
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM");
Optional<String> optionalYearMonthStr = extractOptionalParameter(req, PARAM_YEAR_MONTH); Optional<String> optionalYearMonthStr = extractOptionalParameter(req, PARAM_YEAR_MONTH);
try { try {
return optionalYearMonthStr.map(s -> YearMonth.parse(s, formatter)); return optionalYearMonthStr.map(s -> YearMonth.parse(s, ISODateTimeFormat.yearMonth()));
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
throw new BadRequestException( throw new BadRequestException(
String.format( String.format(
@ -87,20 +84,17 @@ public class ReportingModule {
@Provides @Provides
static YearMonth provideYearMonth( static YearMonth provideYearMonth(
@Parameter(PARAM_YEAR_MONTH) Optional<YearMonth> yearMonthOptional, LocalDate date) { @Parameter(PARAM_YEAR_MONTH) Optional<YearMonth> yearMonthOptional, LocalDate date) {
return yearMonthOptional.orElseGet( return yearMonthOptional.orElseGet(() -> new YearMonth(date.minusMonths(1)));
() -> new YearMonth(date.getYear(), date.getMonthValue() - 1));
} }
/** Extracts an optional date in yyyy-MM-dd format from the request. */ /** Extracts an optional date in yyyy-MM-dd format from the request. */
@Provides @Provides
@Parameter(PARAM_DATE) @Parameter(PARAM_DATE)
static Optional<LocalDate> provideDateOptional(HttpServletRequest req) { static Optional<LocalDate> provideDateOptional(HttpServletRequest req) {
java.time.format.DateTimeFormatter formatter =
java.time.format.DateTimeFormatter.ISO_LOCAL_DATE;
Optional<String> optionalDateString = extractOptionalParameter(req, PARAM_DATE); Optional<String> optionalDateString = extractOptionalParameter(req, PARAM_DATE);
try { try {
return optionalDateString.map(s -> LocalDate.parse(s, formatter)); return optionalDateString.map(s -> LocalDate.parse(s, ISODateTimeFormat.yearMonthDay()));
} catch (DateTimeParseException e) { } catch (IllegalArgumentException e) {
throw new BadRequestException( throw new BadRequestException(
String.format( String.format(
"date must be in yyyy-MM-dd format, got %s instead", "date must be in yyyy-MM-dd format, got %s instead",
@ -116,11 +110,7 @@ public class ReportingModule {
@Provides @Provides
static LocalDate provideDate( static LocalDate provideDate(
@Parameter(PARAM_DATE) Optional<LocalDate> dateOptional, Clock clock) { @Parameter(PARAM_DATE) Optional<LocalDate> dateOptional, Clock clock) {
return dateOptional.orElseGet( return dateOptional.orElseGet(() -> new LocalDate(clock.nowUtc(), DateTimeZone.UTC));
() -> {
DateTime now = clock.nowUtc();
return LocalDate.of(now.getYear(), now.getMonthOfYear(), now.getDayOfMonth());
});
} }
/** Constructs a {@link Dataflow} API client with default settings. */ /** Constructs a {@link Dataflow} API client with default settings. */

View file

@ -33,9 +33,9 @@ import google.registry.request.Action;
import google.registry.request.Response; import google.registry.request.Response;
import google.registry.request.auth.Auth; import google.registry.request.auth.Auth;
import java.io.IOException; import java.io.IOException;
import java.time.LocalDate;
import java.util.Map; import java.util.Map;
import javax.inject.Inject; import javax.inject.Inject;
import org.joda.time.LocalDate;
/** /**
* Invokes the {@code Spec11Pipeline} Beam template via the REST api. * Invokes the {@code Spec11Pipeline} Beam template via the REST api.

View file

@ -31,8 +31,8 @@ import google.registry.request.Parameter;
import google.registry.request.Response; import google.registry.request.Response;
import google.registry.request.auth.Auth; import google.registry.request.auth.Auth;
import java.io.IOException; import java.io.IOException;
import java.time.LocalDate;
import javax.inject.Inject; import javax.inject.Inject;
import org.joda.time.LocalDate;
/** /**
* Retries until a {@code Dataflow} job with a given {@code jobId} completes, continuing the Spec11 * Retries until a {@code Dataflow} job with a given {@code jobId} completes, continuing the Spec11

View file

@ -21,8 +21,8 @@ import dagger.Provides;
import google.registry.beam.spec11.Spec11Pipeline; import google.registry.beam.spec11.Spec11Pipeline;
import java.lang.annotation.Documented; import java.lang.annotation.Documented;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.time.LocalDate;
import javax.inject.Qualifier; import javax.inject.Qualifier;
import org.joda.time.LocalDate;
/** Module for dependencies required by Spec11 reporting. */ /** Module for dependencies required by Spec11 reporting. */
@Module @Module

View file

@ -26,8 +26,8 @@ import google.registry.gcs.GcsUtils;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.time.LocalDate;
import javax.inject.Inject; import javax.inject.Inject;
import org.joda.time.LocalDate;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;

View file

@ -23,10 +23,10 @@ import com.google.common.truth.Truth8;
import google.registry.request.HttpException.BadRequestException; import google.registry.request.HttpException.BadRequestException;
import google.registry.testing.FakeClock; import google.registry.testing.FakeClock;
import google.registry.util.Clock; import google.registry.util.Clock;
import java.time.LocalDate;
import java.util.Optional; import java.util.Optional;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.YearMonth; import org.joda.time.YearMonth;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -71,15 +71,15 @@ public class ReportingModuleTest {
@Test @Test
public void testEmptyYearMonth_returnsLastMonth() { public void testEmptyYearMonth_returnsLastMonth() {
assertThat(ReportingModule.provideYearMonth(Optional.empty(), LocalDate.of(2017, 7, 6))) assertThat(ReportingModule.provideYearMonth(Optional.empty(), new LocalDate(2017, 1, 6)))
.isEqualTo(new YearMonth(2017, 6)); .isEqualTo(new YearMonth(2016, 12));
} }
@Test @Test
public void testGivenYearMonth_returnsThatMonth() { public void testGivenYearMonth_returnsThatMonth() {
assertThat( assertThat(
ReportingModule.provideYearMonth( ReportingModule.provideYearMonth(
Optional.of(new YearMonth(2017, 5)), LocalDate.of(2017, 7, 6))) Optional.of(new YearMonth(2017, 5)), new LocalDate(2017, 7, 6)))
.isEqualTo(new YearMonth(2017, 5)); .isEqualTo(new YearMonth(2017, 5));
} }
@ -93,7 +93,7 @@ public class ReportingModuleTest {
public void testValidDateParameter_returnsThatDate() { public void testValidDateParameter_returnsThatDate() {
when(req.getParameter("date")).thenReturn("2017-05-13"); when(req.getParameter("date")).thenReturn("2017-05-13");
Truth8.assertThat(ReportingModule.provideDateOptional(req)) Truth8.assertThat(ReportingModule.provideDateOptional(req))
.hasValue(LocalDate.of(2017, 5, 13)); .hasValue(new LocalDate(2017, 5, 13));
} }
@Test @Test
@ -109,12 +109,12 @@ public class ReportingModuleTest {
@Test @Test
public void testEmptyDate_returnsToday() { public void testEmptyDate_returnsToday() {
assertThat(ReportingModule.provideDate(Optional.empty(), clock)) assertThat(ReportingModule.provideDate(Optional.empty(), clock))
.isEqualTo(LocalDate.of(2017, 7, 1)); .isEqualTo(new LocalDate(2017, 7, 1));
} }
@Test @Test
public void testGivenDate_returnsThatDate() { public void testGivenDate_returnsThatDate() {
assertThat(ReportingModule.provideDate(Optional.of(LocalDate.of(2017, 7, 2)), clock)) assertThat(ReportingModule.provideDate(Optional.of(new LocalDate(2017, 7, 2)), clock))
.isEqualTo(LocalDate.of(2017, 7, 2)); .isEqualTo(new LocalDate(2017, 7, 2));
} }
} }

View file

@ -35,7 +35,7 @@ import google.registry.testing.AppEngineRule;
import google.registry.testing.FakeResponse; import google.registry.testing.FakeResponse;
import google.registry.testing.TaskQueueHelper.TaskMatcher; import google.registry.testing.TaskQueueHelper.TaskMatcher;
import java.io.IOException; import java.io.IOException;
import java.time.LocalDate; import org.joda.time.LocalDate;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@ -87,7 +87,7 @@ public class GenerateSpec11ReportActionTest {
"gs://template", "gs://template",
"us-east1-c", "us-east1-c",
"api_key/a", "api_key/a",
LocalDate.parse("2018-06-11"), new LocalDate(2018, 6, 11),
response, response,
dataflow); dataflow);
action.run(); action.run();

View file

@ -32,7 +32,7 @@ import com.google.api.services.dataflow.model.Job;
import com.google.common.net.MediaType; import com.google.common.net.MediaType;
import google.registry.testing.FakeResponse; import google.registry.testing.FakeResponse;
import java.io.IOException; import java.io.IOException;
import java.time.LocalDate; import org.joda.time.LocalDate;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -67,7 +67,7 @@ public class PublishSpec11ReportActionTest {
response = new FakeResponse(); response = new FakeResponse();
publishAction = publishAction =
new PublishSpec11ReportAction( new PublishSpec11ReportAction(
"test-project", "12345", emailUtils, dataflow, response, LocalDate.of(2018, 6, 5)); "test-project", "12345", emailUtils, dataflow, response, new LocalDate(2018, 6, 5));
} }
@Test @Test
@ -75,7 +75,7 @@ public class PublishSpec11ReportActionTest {
expectedJob.setCurrentState("JOB_STATE_DONE"); expectedJob.setCurrentState("JOB_STATE_DONE");
publishAction = publishAction =
new PublishSpec11ReportAction( new PublishSpec11ReportAction(
"test-project", "12345", emailUtils, dataflow, response, LocalDate.of(2018, 6, 2)); "test-project", "12345", emailUtils, dataflow, response, new LocalDate(2018, 6, 2));
publishAction.run(); publishAction.run();
assertThat(response.getStatus()).isEqualTo(SC_OK); assertThat(response.getStatus()).isEqualTo(SC_OK);
verify(emailUtils).emailSpec11Reports(); verify(emailUtils).emailSpec11Reports();

View file

@ -26,8 +26,8 @@ import google.registry.gcs.GcsUtils;
import google.registry.testing.TestDataHelper; import google.registry.testing.TestDataHelper;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.util.List; import java.util.List;
import org.joda.time.LocalDate;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.junit.Before; import org.junit.Before;
@ -41,7 +41,7 @@ public class Spec11RegistrarThreatMatchesParserTest {
private final GcsUtils gcsUtils = mock(GcsUtils.class); private final GcsUtils gcsUtils = mock(GcsUtils.class);
private final Spec11RegistrarThreatMatchesParser parser = private final Spec11RegistrarThreatMatchesParser parser =
new Spec11RegistrarThreatMatchesParser(LocalDate.of(2018, 7, 21), gcsUtils, "test-bucket"); new Spec11RegistrarThreatMatchesParser(new LocalDate(2018, 7, 21), gcsUtils, "test-bucket");
@Before @Before
public void setUp() { public void setUp() {