Replace com.google.common.base.Function with java.util.function.Function

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=179249159
This commit is contained in:
guyben 2017-12-15 15:41:05 -08:00 committed by Ben McIlwain
parent d538dca2e0
commit 8157928a35
53 changed files with 424 additions and 399 deletions

View file

@ -16,23 +16,24 @@ package google.registry.flows;
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.Sets.difference;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.flows.EppXmlTransformer.marshal;
import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.testing.DatastoreHelper.BILLING_EVENT_ID_STRIPPER;
import static google.registry.testing.DatastoreHelper.POLL_MESSAGE_ID_STRIPPER;
import static google.registry.testing.DatastoreHelper.getPollMessages;
import static google.registry.xml.XmlTestUtils.assertXmlEquals;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.joda.time.DateTimeZone.UTC;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.collect.ObjectArrays;
import com.google.common.collect.Streams;
import google.registry.config.RegistryConfig.ConfigModule.TmchCaMode;
import google.registry.flows.EppTestComponent.FakesAndMocksModule;
import google.registry.flows.picker.FlowPicker;
@ -58,8 +59,10 @@ import google.registry.tmch.TmchCertificateAuthority;
import google.registry.tmch.TmchXmlSignature;
import google.registry.util.TypeUtils.TypeInstantiator;
import google.registry.xml.ValidationMode;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Rule;
@ -217,6 +220,18 @@ public abstract class FlowTestCase<F extends Flow> extends ShardableTestCase {
return builder.build();
}
private static BillingEvent expandGracePeriod(GracePeriod gracePeriod) {
assertThat(gracePeriod.hasBillingEvent())
.named("Billing event is present for grace period: " + gracePeriod)
.isTrue();
return ofy()
.load()
.key(
firstNonNull(
gracePeriod.getOneTimeBillingEvent(), gracePeriod.getRecurringBillingEvent()))
.now();
}
/**
* Assert that the actual grace periods and the corresponding billing events referenced from
* their keys match the expected map of grace periods to billing events. For the expected map,
@ -225,19 +240,7 @@ public abstract class FlowTestCase<F extends Flow> extends ShardableTestCase {
public void assertGracePeriods(
Iterable<GracePeriod> actual,
ImmutableMap<GracePeriod, ? extends BillingEvent> expected) {
Function<GracePeriod, BillingEvent> gracePeriodExpander =
gracePeriod -> {
assertThat(gracePeriod.hasBillingEvent())
.named("Billing event is present for grace period: " + gracePeriod)
.isTrue();
return ofy()
.load()
.key(
firstNonNull(
gracePeriod.getOneTimeBillingEvent(), gracePeriod.getRecurringBillingEvent()))
.now();
};
assertThat(canonicalizeGracePeriods(Maps.toMap(actual, gracePeriodExpander)))
assertThat(canonicalizeGracePeriods(Maps.toMap(actual, FlowTestCase::expandGracePeriod)))
.isEqualTo(canonicalizeGracePeriods(expected));
}
@ -261,12 +264,11 @@ public abstract class FlowTestCase<F extends Flow> extends ShardableTestCase {
/** Assert that the list matches all the poll messages in the fake Datastore. */
public void assertPollMessagesHelper(Iterable<PollMessage> pollMessages, PollMessage... expected)
throws Exception {
// To facilitate comparison, remove the ids.
Function<PollMessage, PollMessage> idStripper =
pollMessage -> pollMessage.asBuilder().setId(1L).build();
// Ordering is irrelevant but duplicates should be considered independently.
assertThat(FluentIterable.from(pollMessages).transform(idStripper))
.containsExactlyElementsIn(FluentIterable.from(expected).transform(idStripper));
assertThat(
Streams.stream(pollMessages).map(POLL_MESSAGE_ID_STRIPPER).collect(toImmutableList()))
.containsExactlyElementsIn(
Arrays.stream(expected).map(POLL_MESSAGE_ID_STRIPPER).collect(toImmutableList()));
}
private EppOutput runFlowInternal(CommitMode commitMode, UserPrivileges userPrivileges)

View file

@ -37,8 +37,6 @@ import static google.registry.testing.JUnitBackports.expectThrows;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
import static org.joda.money.CurrencyUnit.USD;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedMap;
@ -72,6 +70,8 @@ import google.registry.model.reporting.HistoryEntry;
import google.registry.model.transfer.TransferData;
import google.registry.model.transfer.TransferResponse.DomainTransferResponse;
import google.registry.model.transfer.TransferStatus;
import java.util.Arrays;
import java.util.stream.Stream;
import org.joda.money.Money;
import org.joda.time.DateTime;
import org.joda.time.Duration;
@ -262,22 +262,21 @@ public class DomainTransferApproveFlowTest
.build();
assertBillingEventsForResource(
domain,
FluentIterable.from(expectedCancellationBillingEvents)
.transform(
(Function<BillingEvent.Cancellation.Builder, BillingEvent>)
builder -> builder.setParent(historyEntryTransferApproved).build())
.append(
transferBillingEvent,
getLosingClientAutorenewEvent()
.asBuilder()
.setRecurrenceEndTime(clock.nowUtc())
.build(),
getGainingClientAutorenewEvent()
.asBuilder()
.setEventTime(domain.getRegistrationExpirationTime())
.setParent(historyEntryTransferApproved)
.build())
.toArray(BillingEvent.class));
Stream.concat(
Arrays.stream(expectedCancellationBillingEvents)
.map(builder -> builder.setParent(historyEntryTransferApproved).build()),
Stream.of(
transferBillingEvent,
getLosingClientAutorenewEvent()
.asBuilder()
.setRecurrenceEndTime(clock.nowUtc())
.build(),
getGainingClientAutorenewEvent()
.asBuilder()
.setEventTime(domain.getRegistrationExpirationTime())
.setParent(historyEntryTransferApproved)
.build()))
.toArray(size -> new BillingEvent[size]));
// There should be a grace period for the new transfer billing event.
assertGracePeriods(
domain.getGracePeriods(),
@ -299,21 +298,20 @@ public class DomainTransferApproveFlowTest
// for the gaining client that begins at the new expiration time.
assertBillingEventsForResource(
domain,
FluentIterable.from(expectedCancellationBillingEvents)
.transform(
(Function<BillingEvent.Cancellation.Builder, BillingEvent>)
builder -> builder.setParent(historyEntryTransferApproved).build())
.append(
getLosingClientAutorenewEvent()
.asBuilder()
.setRecurrenceEndTime(clock.nowUtc())
.build(),
getGainingClientAutorenewEvent()
.asBuilder()
.setEventTime(domain.getRegistrationExpirationTime())
.setParent(historyEntryTransferApproved)
.build())
.toArray(BillingEvent.class));
Stream.concat(
Arrays.stream(expectedCancellationBillingEvents)
.map(builder -> builder.setParent(historyEntryTransferApproved).build()),
Stream.of(
getLosingClientAutorenewEvent()
.asBuilder()
.setRecurrenceEndTime(clock.nowUtc())
.build(),
getGainingClientAutorenewEvent()
.asBuilder()
.setEventTime(domain.getRegistrationExpirationTime())
.setParent(historyEntryTransferApproved)
.build()))
.toArray(size -> new BillingEvent[size]));
// There should be no grace period.
assertGracePeriods(domain.getGracePeriods(), ImmutableMap.of());
}

View file

@ -14,6 +14,7 @@
package google.registry.flows.domain;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.MoreCollectors.onlyElement;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.ofy.ObjectifyService.ofy;
@ -37,12 +38,10 @@ import static google.registry.testing.HostResourceSubject.assertAboutHosts;
import static google.registry.testing.JUnitBackports.assertThrows;
import static google.registry.testing.JUnitBackports.expectThrows;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
import static java.util.stream.Collectors.toSet;
import static org.joda.money.CurrencyUnit.EUR;
import static org.joda.money.CurrencyUnit.USD;
import com.google.appengine.repackaged.com.google.common.collect.Sets;
import com.google.common.base.Function;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
@ -91,7 +90,6 @@ import google.registry.model.transfer.TransferResponse;
import google.registry.model.transfer.TransferStatus;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
import org.joda.money.Money;
import org.joda.time.DateTime;
@ -257,6 +255,11 @@ public class DomainTransferRequestFlowTest
expectedExpirationTime, implicitTransferTime, Period.create(1, Unit.YEARS));
}
/** Implements the missing Optional.stream function that is added in Java 9. */
private static <T> Stream<T> optionalToStream(Optional<T> optional) {
return optional.map(Stream::of).orElseGet(Stream::empty);
}
private void assertHistoryEntriesContainBillingEventsAndGracePeriods(
DateTime expectedExpirationTime,
DateTime implicitTransferTime,
@ -304,16 +307,16 @@ public class DomainTransferRequestFlowTest
BillingEvent.Recurring gainingClientAutorenew =
getGainingClientAutorenewEvent().asBuilder().setEventTime(expectedExpirationTime).build();
// Construct extra billing events expected by the specific test.
Set<BillingEvent> extraBillingEvents =
ImmutableSet<BillingEvent> extraBillingEvents =
Stream.of(extraExpectedBillingEvents)
.map(
(Function<BillingEvent.Cancellation.Builder, BillingEvent>)
builder -> builder.setParent(historyEntryTransferRequest).build())
.collect(toSet());
.map(builder -> builder.setParent(historyEntryTransferRequest).build())
.collect(toImmutableSet());
// Assert that the billing events we constructed above actually exist in Datastore.
Set<BillingEvent> expectedBillingEvents =
Sets.newHashSet(losingClientAutorenew, gainingClientAutorenew);
optionalTransferBillingEvent.ifPresent(expectedBillingEvents::add);
ImmutableSet<BillingEvent> expectedBillingEvents =
Stream.concat(
Stream.of(losingClientAutorenew, gainingClientAutorenew),
optionalToStream(optionalTransferBillingEvent))
.collect(toImmutableSet());
assertBillingEvents(Sets.union(expectedBillingEvents, extraBillingEvents));
// Assert that the domain's TransferData server-approve billing events match the above.
if (expectTransferBillingEvent) {
@ -328,8 +331,10 @@ public class DomainTransferRequestFlowTest
gainingClientAutorenew);
// Assert that the full set of server-approve billing events is exactly the extra ones plus
// the transfer billing event (if present) and the gaining client autorenew.
Set<BillingEvent> expectedServeApproveBillingEvents = Sets.newHashSet(gainingClientAutorenew);
optionalTransferBillingEvent.ifPresent(expectedServeApproveBillingEvents::add);
ImmutableSet<BillingEvent> expectedServeApproveBillingEvents =
Stream.concat(
Stream.of(gainingClientAutorenew), optionalToStream(optionalTransferBillingEvent))
.collect(toImmutableSet());
assertBillingEventsEqual(
Iterables.filter(
ofy()