Add EppResource diffing to AbstractEppResourceSubject.isEqualTo()

Currently, the message you get when using e.g. assertAboutContacts().that(alice).isEqualTo(bob) is not very helpful, because we override the "actual" toString (for alice) to return just an abbreviated version, and dump the entire full ImmutableObject toString for bob:

  java.lang.AssertionError: Not true that <ContactResource with foreign key 'contact_id'> is equal to ContactResource (@2125903542): {
      authInfo=ContactAuthInfo (@2128169374): ...
    // giant blob of contact info here
  }

So you can't even figure out why they aren't equal.  Rather than reverting the "actual" object's toString representation in this case, which would just require you to visually compare two giant blobs of properties, this special-cases to let you compare using the prettyPrintEntityDeepDiff() helper we use in MutatingCommand.

With the new version, you see exactly what differs:

  java.lang.AssertionError: Not true that <ContactResource with foreign key 'contact_id'> is equal to ContactResource (@2125903542): {
      authInfo=ContactAuthInfo (@2128169374): ...
    // giant blob of contact info here
  }

  It differs as follows:
  transferData.pendingTransferExpirationTime -> [2017-03-27T16:01:39.882Z, 2020-05-08T16:01:39.882Z]
  lastTransferTime -> [2017-03-27T16:01:39.882Z, 2020-05-08T16:01:39.882Z]

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=151004823
This commit is contained in:
nickfelt 2017-03-23 08:10:24 -07:00 committed by Ben McIlwain
parent dfce7608e7
commit 30ae6effcb

View file

@ -19,17 +19,20 @@ import static com.google.common.truth.Truth.assertThat;
import static google.registry.model.EppResourceUtils.isActive;
import static google.registry.testing.DatastoreHelper.getHistoryEntriesOfType;
import static google.registry.testing.HistoryEntrySubject.assertAboutHistoryEntries;
import static google.registry.util.DiffUtils.prettyPrintEntityDeepDiff;
import com.google.common.collect.ImmutableSet;
import com.google.common.truth.FailureStrategy;
import com.google.common.truth.Subject;
import google.registry.model.EppResource;
import google.registry.model.ImmutableObject;
import google.registry.model.eppcommon.StatusValue;
import google.registry.model.reporting.HistoryEntry;
import google.registry.testing.TruthChainer.And;
import google.registry.testing.TruthChainer.Which;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nullable;
import org.joda.time.DateTime;
/** Base Truth subject for asserting things about epp resources. */
@ -57,6 +60,18 @@ abstract class AbstractEppResourceSubject
actual().getForeignKey());
}
@Override
public void isEqualTo(@Nullable Object other) {
// If the objects differ and we can show an interesting ImmutableObject diff, do so.
if (actual() != null && other instanceof ImmutableObject && !actual().equals(other)) {
String diffText = prettyPrintEntityDeepDiff(
((ImmutableObject) other).toDiffableFieldMap(), actual().toDiffableFieldMap());
fail(String.format("is equal to %s\n\nIt differs as follows:\n%s", other, diffText));
}
// Otherwise, fall back to regular behavior.
super.isEqualTo(other);
}
public And<S> hasRepoId(long roid) {
return hasValue(roid, actual().getRepoId(), "has repoId");
}