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 8d9ae82
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ZonedDateTime> zonedDataTimeSameInstant = (ZonedDateTime e, ZonedDateTime a) -> Objects.compare(e, a, (e1, a2) -> e1.withZoneSameInstant(ZoneOffset.UTC).compareTo(a2.withZoneSameInstant(ZoneOffset.UTC)));
public static Comparator<ZonedDateTime> zonedDataTimeSameInstant = Comparator.nullsFirst((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<BigDecimal> bigDecimalCompareTo = Comparator.nullsFirst((e1, a2) -> e1.compareTo(a2));

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -246,5 +250,23 @@ public final class TestUtil {
}
<%_ } _%>
@SuppressWarnings("unchecked")
public static <T> 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() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 %>
Expand Down

0 comments on commit 8d9ae82

Please sign in to comment.