mirror of
https://github.com/google/nomulus.git
synced 2025-04-30 12:07:51 +02:00
Use injected times in URSC and CommandTestCase (#1805)
We started getting failures because some of the tests used October. In general we should freeze the clock for testing as much as possible. Same thing with the Get*Commands
This commit is contained in:
parent
fd635628ee
commit
72119d92a1
8 changed files with 36 additions and 23 deletions
|
@ -15,30 +15,31 @@
|
|||
package google.registry.tools;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static org.joda.time.DateTimeZone.UTC;
|
||||
|
||||
import com.beust.jcommander.Parameter;
|
||||
import com.beust.jcommander.Parameters;
|
||||
import google.registry.model.EppResource;
|
||||
import google.registry.util.Clock;
|
||||
import java.util.Optional;
|
||||
import javax.inject.Inject;
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
/** Abstract command to print one or more resources to stdout. */
|
||||
@Parameters(separators = " =")
|
||||
abstract class GetEppResourceCommand implements CommandWithRemoteApi {
|
||||
|
||||
private final DateTime now = DateTime.now(UTC);
|
||||
|
||||
@Parameter(
|
||||
names = "--read_timestamp",
|
||||
description = "Timestamp to use when reading. May not be in the past.")
|
||||
protected DateTime readTimestamp = now;
|
||||
protected DateTime readTimestamp;
|
||||
|
||||
@Parameter(
|
||||
names = "--expand",
|
||||
description = "Fully expand the requested resource. NOTE: Output may be lengthy.")
|
||||
boolean expand;
|
||||
|
||||
@Inject Clock clock;
|
||||
|
||||
/** Runs the command's own logic that calls {@link #printResource}. */
|
||||
abstract void runAndPrint();
|
||||
|
||||
|
@ -59,7 +60,11 @@ abstract class GetEppResourceCommand implements CommandWithRemoteApi {
|
|||
|
||||
@Override
|
||||
public void run() {
|
||||
checkArgument(!readTimestamp.isBefore(now), "--read_timestamp may not be in the past");
|
||||
if (readTimestamp == null) {
|
||||
readTimestamp = clock.nowUtc();
|
||||
}
|
||||
checkArgument(
|
||||
!readTimestamp.isBefore(clock.nowUtc()), "--read_timestamp may not be in the past");
|
||||
runAndPrint();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,7 +21,6 @@ import static google.registry.model.EppResourceUtils.checkResourcesExist;
|
|||
import static google.registry.model.EppResourceUtils.loadByForeignKey;
|
||||
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
|
||||
import static google.registry.util.PreconditionsUtils.checkArgumentPresent;
|
||||
import static org.joda.time.DateTimeZone.UTC;
|
||||
|
||||
import com.beust.jcommander.Parameter;
|
||||
import com.beust.jcommander.Parameters;
|
||||
|
@ -38,11 +37,13 @@ import google.registry.model.eppcommon.StatusValue;
|
|||
import google.registry.model.host.Host;
|
||||
import google.registry.tools.soy.DomainRenewSoyInfo;
|
||||
import google.registry.tools.soy.UniformRapidSuspensionSoyInfo;
|
||||
import google.registry.util.Clock;
|
||||
import google.registry.util.DomainNameUtils;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import javax.inject.Inject;
|
||||
import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.format.DateTimeFormat;
|
||||
|
@ -119,10 +120,12 @@ final class UniformRapidSuspensionCommand extends MutatingEppToolCommand {
|
|||
/** Set of status values to remove. */
|
||||
ImmutableSet<String> removeStatuses;
|
||||
|
||||
@Inject Clock clock;
|
||||
|
||||
@Override
|
||||
protected void initMutatingEppToolCommand() {
|
||||
superuser = true;
|
||||
DateTime now = DateTime.now(UTC);
|
||||
DateTime now = clock.nowUtc();
|
||||
ImmutableList<String> newCanonicalHosts =
|
||||
newHosts.stream().map(DomainNameUtils::canonicalizeHostname).collect(toImmutableList());
|
||||
ImmutableSet<String> newHostsSet = ImmutableSet.copyOf(newCanonicalHosts);
|
||||
|
|
|
@ -19,7 +19,6 @@ import static com.google.common.collect.Iterables.toArray;
|
|||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
import static org.joda.time.DateTimeZone.UTC;
|
||||
|
||||
import com.beust.jcommander.JCommander;
|
||||
import com.google.common.base.Joiner;
|
||||
|
@ -63,7 +62,7 @@ public abstract class CommandTestCase<C extends Command> {
|
|||
|
||||
protected C command;
|
||||
|
||||
protected final FakeClock fakeClock = new FakeClock(DateTime.now(UTC));
|
||||
protected final FakeClock fakeClock = new FakeClock(DateTime.parse("2022-09-01T00:00:00.000Z"));
|
||||
|
||||
@RegisterExtension
|
||||
public final AppEngineExtension appEngine =
|
||||
|
|
|
@ -19,22 +19,19 @@ import static google.registry.testing.DatabaseHelper.newContact;
|
|||
import static google.registry.testing.DatabaseHelper.persistActiveContact;
|
||||
import static google.registry.testing.DatabaseHelper.persistDeletedContact;
|
||||
import static google.registry.testing.DatabaseHelper.persistResource;
|
||||
import static org.joda.time.DateTimeZone.UTC;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import com.beust.jcommander.ParameterException;
|
||||
import org.joda.time.DateTime;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Unit tests for {@link GetContactCommand}. */
|
||||
class GetContactCommandTest extends CommandTestCase<GetContactCommand> {
|
||||
|
||||
private DateTime now = DateTime.now(UTC);
|
||||
|
||||
@BeforeEach
|
||||
void beforeEach() {
|
||||
createTld("tld");
|
||||
command.clock = fakeClock;
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -67,7 +64,7 @@ class GetContactCommandTest extends CommandTestCase<GetContactCommand> {
|
|||
|
||||
@Test
|
||||
void testSuccess_deletedContact() throws Exception {
|
||||
persistDeletedContact("sh8013", now.minusDays(1));
|
||||
persistDeletedContact("sh8013", fakeClock.nowUtc().minusDays(1));
|
||||
runCommand("sh8013");
|
||||
assertInStdout("Contact 'sh8013' does not exist or is deleted");
|
||||
}
|
||||
|
@ -85,8 +82,9 @@ class GetContactCommandTest extends CommandTestCase<GetContactCommand> {
|
|||
|
||||
@Test
|
||||
void testSuccess_contactDeletedInFuture() throws Exception {
|
||||
persistResource(newContact("sh8013").asBuilder().setDeletionTime(now.plusDays(1)).build());
|
||||
runCommand("sh8013", "--read_timestamp=" + now.plusMonths(1));
|
||||
persistResource(
|
||||
newContact("sh8013").asBuilder().setDeletionTime(fakeClock.nowUtc().plusDays(1)).build());
|
||||
runCommand("sh8013", "--read_timestamp=" + fakeClock.nowUtc().plusMonths(1));
|
||||
assertInStdout("Contact 'sh8013' does not exist or is deleted");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,6 +31,7 @@ class GetDomainCommandTest extends CommandTestCase<GetDomainCommand> {
|
|||
@BeforeEach
|
||||
void beforeEach() {
|
||||
createTld("tld");
|
||||
command.clock = fakeClock;
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -19,22 +19,19 @@ import static google.registry.testing.DatabaseHelper.newHost;
|
|||
import static google.registry.testing.DatabaseHelper.persistActiveHost;
|
||||
import static google.registry.testing.DatabaseHelper.persistDeletedHost;
|
||||
import static google.registry.testing.DatabaseHelper.persistResource;
|
||||
import static org.joda.time.DateTimeZone.UTC;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import com.beust.jcommander.ParameterException;
|
||||
import org.joda.time.DateTime;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Unit tests for {@link GetHostCommand}. */
|
||||
class GetHostCommandTest extends CommandTestCase<GetHostCommand> {
|
||||
|
||||
private DateTime now = DateTime.now(UTC);
|
||||
|
||||
@BeforeEach
|
||||
void beforeEach() {
|
||||
createTld("tld");
|
||||
command.clock = fakeClock;
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -77,7 +74,7 @@ class GetHostCommandTest extends CommandTestCase<GetHostCommand> {
|
|||
|
||||
@Test
|
||||
void testSuccess_deletedHost() throws Exception {
|
||||
persistDeletedHost("ns1.example.tld", now.minusDays(1));
|
||||
persistDeletedHost("ns1.example.tld", fakeClock.nowUtc().minusDays(1));
|
||||
runCommand("ns1.example.tld");
|
||||
assertInStdout("Host 'ns1.example.tld' does not exist or is deleted");
|
||||
}
|
||||
|
@ -91,8 +88,11 @@ class GetHostCommandTest extends CommandTestCase<GetHostCommand> {
|
|||
@Test
|
||||
void testSuccess_hostDeletedInFuture() throws Exception {
|
||||
persistResource(
|
||||
newHost("ns1.example.tld").asBuilder().setDeletionTime(now.plusDays(1)).build());
|
||||
runCommand("ns1.example.tld", "--read_timestamp=" + now.plusMonths(1));
|
||||
newHost("ns1.example.tld")
|
||||
.asBuilder()
|
||||
.setDeletionTime(fakeClock.nowUtc().plusDays(1))
|
||||
.build());
|
||||
runCommand("ns1.example.tld", "--read_timestamp=" + fakeClock.nowUtc().plusMonths(1));
|
||||
assertInStdout("Host 'ns1.example.tld' does not exist or is deleted");
|
||||
}
|
||||
|
||||
|
|
|
@ -28,11 +28,17 @@ import google.registry.model.domain.token.PackagePromotion;
|
|||
import org.joda.money.CurrencyUnit;
|
||||
import org.joda.money.Money;
|
||||
import org.joda.time.DateTime;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Unit tests for {@link GetPackagePromotionCommand}. */
|
||||
public class GetPackagePromotionCommandTest extends CommandTestCase<GetPackagePromotionCommand> {
|
||||
|
||||
@BeforeEach
|
||||
void beforeEach() {
|
||||
command.clock = fakeClock;
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSuccess() throws Exception {
|
||||
AllocationToken token =
|
||||
|
|
|
@ -48,6 +48,7 @@ class UniformRapidSuspensionCommandTest
|
|||
|
||||
@BeforeEach
|
||||
void beforeEach() {
|
||||
command.clock = fakeClock;
|
||||
// Since the command's history client ID must be CharlestonRoad, resave TheRegistrar that way.
|
||||
persistResource(
|
||||
loadRegistrar("TheRegistrar").asBuilder().setRegistrarId("CharlestonRoad").build());
|
||||
|
|
Loading…
Add table
Reference in a new issue