Skip to content

Commit

Permalink
fix partialUpdate test code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Feb 10, 2024
1 parent 1d0fadb commit 408e80c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import static <%- packageName %>.domain.AssertUtils.bigDecimalCompareTo;
<%_ if (anyFieldIsZonedDateTime) { _%>
import static <%- packageName %>.domain.AssertUtils.zonedDataTimeSameInstant;
<%_ } _%>
import static <%- packageName %>.domain.AssertUtils.matchUpdateOrOriginal;
import static <%- packageName %>.domain.AssertUtils.matchUpdateOrOriginalUsingComparator;
public class <%= persistClass %>Asserts {
Expand Down Expand Up @@ -127,4 +129,32 @@ public class <%= persistClass %>Asserts {
;
<%_ } _%>
}
}
/**
* Asserts that the entity has all the fields with partialUpdate or original if partialUpdate is null.
*
* @param actual the actual persisted entity
* @param partialUpdate the partial update entity
* @param original the originally persisted entity
*/
public static void assert<%- persistClass %>PartialUpdate(<%- persistClass %> persisted, <%- persistClass %> partialUpdate, <%- persistClass %> original) {
<%_ if (fields.some(field => !field.transient && !field.autoGenerate)) { _%>
assertThat(persisted)
.as("Verify <%- persistClass %> properties")
<%_ for (const field of fields.filter(field => !field.transient && !field.autoGenerate)) { _%>
<%_ if (field.fieldTypeZonedDateTime) { _%>
.satisfies(e -> assertThat(e.get<%- field.fieldInJavaBeanMethod %>()).as("check <%- field.propertyName %>").satisfies(matchUpdateOrOriginalUsingComparator(zonedDataTimeSameInstant, partialUpdate.get<%- field.fieldInJavaBeanMethod %>(), original.get<%- field.fieldInJavaBeanMethod %>())))
<%_ } else if (field.fieldTypeBigDecimal) { _%>
.satisfies(e -> assertThat(e.get<%- field.fieldInJavaBeanMethod %>()).as("check <%- field.propertyName %>").satisfies(matchUpdateOrOriginalUsingComparator(bigDecimalCompareTo, partialUpdate.get<%- field.fieldInJavaBeanMethod %>(), original.get<%- field.fieldInJavaBeanMethod %>())))
<%_ } else { _%>
.satisfies(e -> assertThat(e.get<%- field.fieldInJavaBeanMethod %>()).as("check <%- field.propertyName %>").satisfies(matchUpdateOrOriginal(partialUpdate.get<%- field.fieldInJavaBeanMethod %>(), original.get<%- field.fieldInJavaBeanMethod %>())))
<%_ if (field.fieldTypeBinary && !field.blobContentTypeText) { _%>
.satisfies(e -> assertThat(e.get<%- field.fieldInJavaBeanMethod %>ContentType()).as("check <%- field.propertyName %> contenty type").satisfies(matchUpdateOrOriginal(partialUpdate.get<%- field.fieldInJavaBeanMethod %>ContentType(), original.get<%- field.fieldInJavaBeanMethod %>ContentType())))
<%_ } -%>
<%_ } -%>
<%_ } -%>
;
<%_ } _%>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,42 @@
-%>
package <%= packageName %>.domain;

import static org.assertj.core.api.Assertions.assertThat;

import java.math.BigDecimal;
import java.time.ZonedDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Comparator;
import java.util.Objects;
import java.util.function.Consumer;

public class AssertUtils {

public static Comparator<ZonedDateTime> zonedDataTimeSameInstant = (ZonedDateTime e, ZonedDateTime a) -> Objects.compare(e, a, (e1, a2) -> e1.withZoneSameInstant(ZoneOffset.UTC).compareTo(a2.withZoneSameInstant(ZoneOffset.UTC)));

public static Comparator<BigDecimal> bigDecimalCompareTo = (BigDecimal e, BigDecimal a) -> Objects.compare(e, a, (e1, a2) -> e1.compareTo(a2));

public static Comparator<ZonedDateTime> zonedDataTimeSameInstant = Comparator.nullsFirst((e1, a2) ->
e1.withZoneSameInstant(ZoneOffset.UTC).compareTo(a2.withZoneSameInstant(ZoneOffset.UTC))
);

public static Comparator<BigDecimal> bigDecimalCompareTo = Comparator.nullsFirst((e1, a2) -> e1.compareTo(a2));

public static <T> Consumer<T> matchUpdateOrOriginal(T update, T current) {
return s ->
assertThat(s)
.satisfiesAnyOf(
s1 -> assertThat(s1).isEqualTo(update),
s1 -> assertThat(s1).satisfies(s2 -> assertThat(update).isNull(), s2 -> assertThat(s2).isEqualTo(current))
);
}

public static <T> Consumer<T> matchUpdateOrOriginalUsingComparator(Comparator<T> comparator, T update, T current) {
return s ->
assertThat(s)
.satisfiesAnyOf(
s1 -> assertThat(s1).usingComparator(null).usingComparator(comparator).isEqualTo(update),
s1 ->
assertThat(s1)
.satisfies(
s2 -> assertThat(update).isNull(),
s2 -> assertThat(s2).usingComparator(comparator).isEqualTo(current)
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1626,18 +1626,7 @@ _%>
assertSameRepositoryCount(databaseSizeBeforeUpdate);
<%= persistClass %> test<%= entityClass %> = getPersisted<%- persistClass %>(<%= persistInstance %>);

<%_ for (field of fieldsToIncludeInPartialPatchTest) { _%>
<%_ if (field.fieldTypeZonedDateTime) { _%>
assertThat(test<%= entityClass %>.get<%= field.fieldInJavaBeanMethod %>()).isEqualTo(<%= field.testWithConstant %>);
<%_ } else if (field.fieldTypeBinary && !field.blobContentTypeText) { _%>
assertThat(test<%= entityClass %>.get<%= field.fieldInJavaBeanMethod %>()).isEqualTo(<%= field.testWithConstant %>);
assertThat(test<%= entityClass %>.get<%= field.fieldInJavaBeanMethod %>ContentType()).isEqualTo(<%= field.testWithConstant %>_CONTENT_TYPE);
<%_ } else if (field.fieldTypeBigDecimal) { _%>
assertThat(test<%= entityClass %>.get<%= field.fieldInJavaBeanMethod %>()).isEqualByComparingTo(<%= field.testWithConstant %>);
<%_ } else { _%>
assertThat(test<%= entityClass %>.get<%= field.fieldInJavaBeanMethod %>()).isEqualTo(<%= field.testWithConstant %>);
<%_ } _%>
<%_ } _%>
assert<%- persistClass %>PartialUpdate(test<%= entityClass %>, partialUpdated<%= persistClass %>, <%= persistInstance %>);
}

@Test<%= transactionalAnnotation %>
Expand Down

0 comments on commit 408e80c

Please sign in to comment.