From 30ae6effcbed553f1629f4d87c89a9687b50ac5f Mon Sep 17 00:00:00 2001 From: nickfelt Date: Thu, 23 Mar 2017 08:10:24 -0700 Subject: [PATCH] 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 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 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 --- .../testing/AbstractEppResourceSubject.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/javatests/google/registry/testing/AbstractEppResourceSubject.java b/javatests/google/registry/testing/AbstractEppResourceSubject.java index 0b514708f..93c9477a9 100644 --- a/javatests/google/registry/testing/AbstractEppResourceSubject.java +++ b/javatests/google/registry/testing/AbstractEppResourceSubject.java @@ -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 hasRepoId(long roid) { return hasValue(roid, actual().getRepoId(), "has repoId"); }