Use nullness parity helper (#944)

* Use nullness parity helper
This commit is contained in:
Ben McIlwain 2021-01-26 13:20:48 -05:00 committed by GitHub
parent 7578211c47
commit c3b3b39b50

View file

@ -16,6 +16,7 @@ package google.registry.model;
import static com.google.common.truth.Truth.assertAbout; import static com.google.common.truth.Truth.assertAbout;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static google.registry.testing.truth.TruthUtils.assertNullnessParity;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.truth.Correspondence; import com.google.common.truth.Correspondence;
@ -62,11 +63,7 @@ public final class ImmutableObjectSubject extends Subject {
* <p>This is used to verify that entities stored in both cloud SQL and Datastore are identical. * <p>This is used to verify that entities stored in both cloud SQL and Datastore are identical.
*/ */
public void isEqualAcrossDatabases(@Nullable ImmutableObject expected) { public void isEqualAcrossDatabases(@Nullable ImmutableObject expected) {
if (actual == null) { assertNullnessParity(actual, expected);
assertThat(expected).isNull();
} else {
assertThat(expected).isNotNull();
}
if (actual != null) { if (actual != null) {
Map<Field, Object> actualFields = filterFields(actual, ImmutableObject.DoNotCompare.class); Map<Field, Object> actualFields = filterFields(actual, ImmutableObject.DoNotCompare.class);
Map<Field, Object> expectedFields = Map<Field, Object> expectedFields =
@ -109,7 +106,8 @@ public final class ImmutableObjectSubject extends Subject {
} }
} }
public static Map<Field, Object> filterFields(ImmutableObject original, String... ignoredFields) { private static Map<Field, Object> filterFields(
ImmutableObject original, String... ignoredFields) {
ImmutableSet<String> ignoredFieldSet = ImmutableSet.copyOf(ignoredFields); ImmutableSet<String> ignoredFieldSet = ImmutableSet.copyOf(ignoredFields);
Map<Field, Object> originalFields = ModelUtils.getFieldValues(original); Map<Field, Object> originalFields = ModelUtils.getFieldValues(original);
// don't use ImmutableMap or a stream->collect model since we can have nulls // don't use ImmutableMap or a stream->collect model since we can have nulls
@ -123,7 +121,7 @@ public final class ImmutableObjectSubject extends Subject {
} }
/** Filter out fields with the given annotation. */ /** Filter out fields with the given annotation. */
public static Map<Field, Object> filterFields( private static Map<Field, Object> filterFields(
ImmutableObject original, Class<? extends Annotation> annotation) { ImmutableObject original, Class<? extends Annotation> annotation) {
Map<Field, Object> originalFields = ModelUtils.getFieldValues(original); Map<Field, Object> originalFields = ModelUtils.getFieldValues(original);
// don't use ImmutableMap or a stream->collect model since we can have nulls // don't use ImmutableMap or a stream->collect model since we can have nulls