test for equality via compareTo() to consider BigDecimal/DateTime etc. => effectively unchanged in update

This commit is contained in:
Michael Hoennig 2019-04-27 20:36:25 +02:00
parent 3abc201a8d
commit acfef3dfbe
4 changed files with 28 additions and 3 deletions

View File

@ -153,6 +153,6 @@ public class JSonDeserializationWithAccessFilter<T> extends JSonAccessFilter<T>
}
private boolean isActuallyUpdated(final Field field, final T dto, T currentDto) {
return ObjectUtils.notEqual(ReflectionUtil.getValue(dto, field), ReflectionUtil.getValue(currentDto, field));
return 0 != ObjectUtils.compare(ReflectionUtil.getValue(dto, field), ReflectionUtil.getValue(currentDto, field));
}
}

View File

@ -92,6 +92,9 @@ public class JSonAccessFilterTestFixture {
@AccessFor(init = ANYBODY, update = ANYBODY, read = ANYBODY)
BigDecimal openBigDecimalField;
@AccessFor(init = SUPPORTER, update = SUPPORTER, read = SUPPORTER)
BigDecimal restrictedBigDecimalField;
@AccessFor(init = ANYBODY, update = ANYBODY, read = ANYBODY)
int[] openArrayField;
}

View File

@ -34,6 +34,9 @@ import static org.mockito.BDDMockito.given;
@SuppressWarnings("ALL")
public class JSonDeserializationWithAccessFilterUnitTest {
public static final String SOME_BIG_DECIMAL_AS_STRING = "5432191234888.1";
public static final BigDecimal SOME_BIG_DECIMAL = new BigDecimal(SOME_BIG_DECIMAL_AS_STRING).setScale(2, BigDecimal.ROUND_HALF_UP);
public static final BigDecimal SOME_BIG_DECIMAL_WITH_ANOTHER_SCALE = new BigDecimal(SOME_BIG_DECIMAL_AS_STRING).setScale(5, BigDecimal.ROUND_HALF_UP);
@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();
@ -77,9 +80,10 @@ public class JSonDeserializationWithAccessFilterUnitTest {
.with(dto -> dto.openPrimitiveLongField = 44444444L)
.with(dto -> dto.openBooleanField = true)
.with(dto -> dto.openPrimitiveBooleanField = false)
.with(dto -> dto.openBigDecimalField = new BigDecimal("9876543.09"))
.with(dto -> dto.openBigDecimalField = SOME_BIG_DECIMAL)
.with(dto -> dto.openStringField = "3333")
.with(dto -> dto.restrictedField = "initial value of restricted field")
.with(dto -> dto.restrictedBigDecimalField = SOME_BIG_DECIMAL)
));
given(autowireCapableBeanFactory.createBean(GivenCustomerService.class)).willReturn(givenCustomerService);
given(givenCustomerService.findOne(888L)).willReturn(Optional.of(new GivenCustomerDto()
@ -134,6 +138,24 @@ public class JSonDeserializationWithAccessFilterUnitTest {
assertThat(actualDto.openIntegerField).isEqualTo(1234);
}
@Test
public void shouldDeserializeRestrictedBigDecimalFieldIfUnchangedByCompareTo() throws IOException {
// given
assertThat(SOME_BIG_DECIMAL_WITH_ANOTHER_SCALE).as("precondition failed").isNotEqualTo(SOME_BIG_DECIMAL);
givenJSonTree(asJSon(
ImmutablePair.of("id", 1234L),
ImmutablePair.of("customerId", 888L),
ImmutablePair.of("restrictedBigDecimalField", SOME_BIG_DECIMAL_WITH_ANOTHER_SCALE)));
// when
GivenDto actualDto = new JSonDeserializationWithAccessFilter<>(ctx, jsonParser, null, GivenDto.class).deserialize();
// then
assertThat(actualDto.restrictedBigDecimalField).isEqualByComparingTo(SOME_BIG_DECIMAL);
assertThat(actualDto.restrictedBigDecimalField).isEqualByComparingTo(SOME_BIG_DECIMAL_WITH_ANOTHER_SCALE);
}
@Test
// TODO: split in separate tests for each type, you see all errors at once (if any) and it's easier to debug when there are problems
public void shouldDeserializeAcessibleFieldOfAnyType() throws IOException {

View File

@ -91,7 +91,7 @@ public class RoleUnitTest {
assertThat(Role.ANY_CUSTOMER_USER.coversAny(Role.CONTRACTUAL_CONTACT, Role.FINANCIAL_CONTACT)).isFalse();
assertThat(catchThrowable(() -> Role.HOSTMASTER.coversAny())).isInstanceOf(VerifyException.class);
assertThat(catchThrowable(() -> Role.HOSTMASTER.coversAny(null))).isInstanceOf(VerifyException.class);
assertThat(catchThrowable(() -> Role.HOSTMASTER.coversAny((Role[]) null))).isInstanceOf(VerifyException.class);
}
@Test