From 8d9ae82f7e5d2d0d8d6f35f02a343e40450d8157 Mon Sep 17 00:00:00 2001 From: Marcelo Boveto Shima Date: Fri, 9 Feb 2024 22:46:29 -0300 Subject: [PATCH] fix partialUpdate test code smells --- .../_package_/domain/AssertUtils.java.ejs | 9 ++++---- .../java/_package_/web/rest/TestUtil.java.ejs | 22 +++++++++++++++++++ .../web/rest/_entityClass_ResourceIT.java.ejs | 20 +++++------------ 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/generators/java/templates/src/test/java/_package_/domain/AssertUtils.java.ejs b/generators/java/templates/src/test/java/_package_/domain/AssertUtils.java.ejs index c1eea5d2e077..12d537f22a0a 100644 --- a/generators/java/templates/src/test/java/_package_/domain/AssertUtils.java.ejs +++ b/generators/java/templates/src/test/java/_package_/domain/AssertUtils.java.ejs @@ -19,15 +19,16 @@ package <%= packageName %>.domain; import java.math.BigDecimal; -import java.time.ZonedDateTime; import java.time.ZoneOffset; +import java.time.ZonedDateTime; import java.util.Comparator; -import java.util.Objects; public class AssertUtils { - public static Comparator zonedDataTimeSameInstant = (ZonedDateTime e, ZonedDateTime a) -> Objects.compare(e, a, (e1, a2) -> e1.withZoneSameInstant(ZoneOffset.UTC).compareTo(a2.withZoneSameInstant(ZoneOffset.UTC))); + public static Comparator zonedDataTimeSameInstant = Comparator.nullsFirst((e1, a2) -> + e1.withZoneSameInstant(ZoneOffset.UTC).compareTo(a2.withZoneSameInstant(ZoneOffset.UTC)) + ); - public static Comparator bigDecimalCompareTo = (BigDecimal e, BigDecimal a) -> Objects.compare(e, a, (e1, a2) -> e1.compareTo(a2)); + public static Comparator bigDecimalCompareTo = Comparator.nullsFirst((e1, a2) -> e1.compareTo(a2)); } diff --git a/generators/server/templates/src/test/java/_package_/web/rest/TestUtil.java.ejs b/generators/server/templates/src/test/java/_package_/web/rest/TestUtil.java.ejs index caa3b03b206c..d43a452ef06e 100644 --- a/generators/server/templates/src/test/java/_package_/web/rest/TestUtil.java.ejs +++ b/generators/server/templates/src/test/java/_package_/web/rest/TestUtil.java.ejs @@ -18,9 +18,13 @@ -%> package <%= packageName %>.web.rest; +import java.lang.reflect.Method; import org.hamcrest.Description; import org.hamcrest.TypeSafeDiagnosingMatcher; import org.hamcrest.TypeSafeMatcher; +import org.springframework.cglib.proxy.Enhancer; +import org.springframework.cglib.proxy.MethodInterceptor; +import org.springframework.cglib.proxy.MethodProxy; <%_ if (!reactive) { _%> import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar; import org.springframework.format.support.DefaultFormattingConversionService; @@ -246,5 +250,23 @@ public final class TestUtil { } <%_ } _%> + @SuppressWarnings("unchecked") + public static T createUpdateProxyForBean(T update, T original) { + Enhancer e = new Enhancer(); + e.setSuperclass(original.getClass()); + e.setCallback( + new MethodInterceptor() { + public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { + Object val = update.getClass().getMethod(method.getName(), method.getParameterTypes()).invoke(update, args); + if (val == null) { + return original.getClass().getMethod(method.getName(), method.getParameterTypes()).invoke(original, args); + } + return val; + } + } + ); + return (T) e.create(); + } + private TestUtil() {} } diff --git a/generators/spring-boot/templates/src/test/java/_package_/_entityPackage_/web/rest/_entityClass_ResourceIT.java.ejs b/generators/spring-boot/templates/src/test/java/_package_/_entityPackage_/web/rest/_entityClass_ResourceIT.java.ejs index ed3aeef5aeae..813cdba718cc 100644 --- a/generators/spring-boot/templates/src/test/java/_package_/_entityPackage_/web/rest/_entityClass_ResourceIT.java.ejs +++ b/generators/spring-boot/templates/src/test/java/_package_/_entityPackage_/web/rest/_entityClass_ResourceIT.java.ejs @@ -19,7 +19,7 @@ package <%= entityAbsolutePackage %>.web.rest; import static <%= entityAbsolutePackage %>.domain.<%= persistClass %>Asserts.*; - +import static <%= packageName %>.web.rest.TestUtil.createUpdateProxyForBean; <%_ var filterTestableRelationships = (reactive ? reactiveEagerRelations : relationships).filter(rel => rel.persistableRelationship && !rel.otherEntity.hasCyclicRequiredRelationship); const fieldsToTest = fields.filter(field => !field.id && !field.autoGenerate && !field.transient); @@ -1624,20 +1624,10 @@ _%> <%- include('/_global_partials_entity_/it_patch_update.partial.java.ejs', {fields: fieldsToIncludeInPartialPatchTest, saveMethod, callBlock, callListBlock}); -%> 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 %>UpdatableFieldsEquals( + createUpdateProxyForBean(partialUpdated<%= persistClass %>, <%= persistInstance %>), + getPersisted<%- persistClass %>(<%= persistInstance %>) + ); } @Test<%= transactionalAnnotation %>