From acfef3dfbe3abaf1cdc2a761796b7c6bc92a631b Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Sat, 27 Apr 2019 20:36:25 +0200 Subject: [PATCH] test for equality via compareTo() to consider BigDecimal/DateTime etc. => effectively unchanged in update --- .../JSonDeserializationWithAccessFilter.java | 2 +- .../JSonAccessFilterTestFixture.java | 3 +++ ...serializationWithAccessFilterUnitTest.java | 24 ++++++++++++++++++- .../service/accessfilter/RoleUnitTest.java | 2 +- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/hostsharing/hsadminng/service/accessfilter/JSonDeserializationWithAccessFilter.java b/src/main/java/org/hostsharing/hsadminng/service/accessfilter/JSonDeserializationWithAccessFilter.java index 44ab3dfc..6e714b3c 100644 --- a/src/main/java/org/hostsharing/hsadminng/service/accessfilter/JSonDeserializationWithAccessFilter.java +++ b/src/main/java/org/hostsharing/hsadminng/service/accessfilter/JSonDeserializationWithAccessFilter.java @@ -153,6 +153,6 @@ public class JSonDeserializationWithAccessFilter extends JSonAccessFilter } 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)); } } diff --git a/src/test/java/org/hostsharing/hsadminng/service/accessfilter/JSonAccessFilterTestFixture.java b/src/test/java/org/hostsharing/hsadminng/service/accessfilter/JSonAccessFilterTestFixture.java index 51686cf5..439f2990 100644 --- a/src/test/java/org/hostsharing/hsadminng/service/accessfilter/JSonAccessFilterTestFixture.java +++ b/src/test/java/org/hostsharing/hsadminng/service/accessfilter/JSonAccessFilterTestFixture.java @@ -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; } diff --git a/src/test/java/org/hostsharing/hsadminng/service/accessfilter/JSonDeserializationWithAccessFilterUnitTest.java b/src/test/java/org/hostsharing/hsadminng/service/accessfilter/JSonDeserializationWithAccessFilterUnitTest.java index 0db7844a..604aba3c 100644 --- a/src/test/java/org/hostsharing/hsadminng/service/accessfilter/JSonDeserializationWithAccessFilterUnitTest.java +++ b/src/test/java/org/hostsharing/hsadminng/service/accessfilter/JSonDeserializationWithAccessFilterUnitTest.java @@ -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 { diff --git a/src/test/java/org/hostsharing/hsadminng/service/accessfilter/RoleUnitTest.java b/src/test/java/org/hostsharing/hsadminng/service/accessfilter/RoleUnitTest.java index ec1fd98f..ce6a16f8 100644 --- a/src/test/java/org/hostsharing/hsadminng/service/accessfilter/RoleUnitTest.java +++ b/src/test/java/org/hostsharing/hsadminng/service/accessfilter/RoleUnitTest.java @@ -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