From 1a943c704634b62553c95edc409b9a74c18a8b6a Mon Sep 17 00:00:00 2001 From: John Blum Date: Wed, 4 Nov 2020 12:33:21 -0800 Subject: [PATCH] DATAGEODE-302 - Add test case asserting the expected behavior of MappingPdxSerializer when serializing complex types including BigDecimal and BigInteger properties to PDX. --- .../gemfire/mapping/MappingPdxSerializer.java | 6 +- .../java/example/app/model/ComplexType.java | 49 +++++++++ ...eMappingPdxSerializerIntegrationTests.java | 100 ++++++++++++++++++ 3 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 spring-data-geode/src/test/java/example/app/model/ComplexType.java create mode 100644 spring-data-geode/src/test/java/org/springframework/data/gemfire/mapping/TypeMappingPdxSerializerIntegrationTests.java diff --git a/spring-data-geode/src/main/java/org/springframework/data/gemfire/mapping/MappingPdxSerializer.java b/spring-data-geode/src/main/java/org/springframework/data/gemfire/mapping/MappingPdxSerializer.java index e91408b1..3be08a46 100644 --- a/spring-data-geode/src/main/java/org/springframework/data/gemfire/mapping/MappingPdxSerializer.java +++ b/spring-data-geode/src/main/java/org/springframework/data/gemfire/mapping/MappingPdxSerializer.java @@ -622,6 +622,8 @@ public class MappingPdxSerializer implements PdxSerializer, ApplicationContextAw PdxSerializer customPdxSerializer = resolveCustomPdxSerializer(persistentProperty); + String propertyName = persistentProperty.getName(); + Supplier messageSuffix = () -> customPdxSerializer != null ? String.format(" using custom PdxSerializer [%s]", customPdxSerializer) : ""; @@ -632,7 +634,7 @@ public class MappingPdxSerializer implements PdxSerializer, ApplicationContextAw if (getLogger().isDebugEnabled()) { getLogger().debug("Serializing entity [{}] property [{}] value [{}] of type [{}] to PDX{}", - entity.getType().getName(), persistentProperty.getName(), propertyValue, + entity.getType().getName(), propertyName, propertyValue, ObjectUtils.nullSafeClassName(propertyValue), messageSuffix.get()); } @@ -640,7 +642,7 @@ public class MappingPdxSerializer implements PdxSerializer, ApplicationContextAw customPdxSerializer.toData(propertyValue, pdxWriter); } else { - pdxWriter.writeField(persistentProperty.getName(), propertyValue, + pdxWriter.writeField(propertyName, propertyValue, (Class) persistentProperty.getType()); } } diff --git a/spring-data-geode/src/test/java/example/app/model/ComplexType.java b/spring-data-geode/src/test/java/example/app/model/ComplexType.java new file mode 100644 index 00000000..86c12f72 --- /dev/null +++ b/spring-data-geode/src/test/java/example/app/model/ComplexType.java @@ -0,0 +1,49 @@ +/* + * Copyright 2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example.app.model; + +import java.math.BigDecimal; +import java.math.BigInteger; + +import org.springframework.data.annotation.Id; +import org.springframework.data.gemfire.mapping.annotation.Region; + +import lombok.Data; +import lombok.ToString; + +/** + * {@link ComplexType} class used for testing purposes. + * + * @author John Blum + * @see org.springframework.data.annotation.Id + * @see org.springframework.data.gemfire.mapping.annotation.Region + * @since 2.5.0 + */ +@Data +@ToString +@Region("Examples") +public class ComplexType { + + private BigDecimal decimalValue; + + private BigInteger integerValue; + + @Id + private Long id; + + private String name; + +} diff --git a/spring-data-geode/src/test/java/org/springframework/data/gemfire/mapping/TypeMappingPdxSerializerIntegrationTests.java b/spring-data-geode/src/test/java/org/springframework/data/gemfire/mapping/TypeMappingPdxSerializerIntegrationTests.java new file mode 100644 index 00000000..7dfb56fb --- /dev/null +++ b/spring-data-geode/src/test/java/org/springframework/data/gemfire/mapping/TypeMappingPdxSerializerIntegrationTests.java @@ -0,0 +1,100 @@ +/* + * Copyright 2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.gemfire.mapping; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; + +import java.math.BigDecimal; +import java.math.BigInteger; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.apache.geode.pdx.PdxWriter; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +import example.app.model.ComplexType; + +/** + * Integration Tests for {@link MappingPdxSerializer} with {@literal complex} and {@literal simple} {@link Class types}. + * + * @author John Blum + * @see org.junit.Test + * @see org.mockito.Mockito + * @see org.apache.geode.pdx.PdxSerializer + * @see org.apache.geode.pdx.PdxWriter + * @see org.springframework.data.gemfire.mapping.MappingPdxSerializer + * @since 2.5.0 + */ +@RunWith(SpringRunner.class) +@ContextConfiguration +public class TypeMappingPdxSerializerIntegrationTests { + + @Autowired + private MappingPdxSerializer pdxSerializer; + + @Before + public void setup() { + assertThat(this.pdxSerializer).isNotNull(); + } + + @Test + public void mapsComplexTypeSuccessfully() { + + PdxWriter mockPdxWriter = mock(PdxWriter.class); + + ComplexType complexType = new ComplexType(); + + complexType.setId(2L); + complexType.setDecimalValue(new BigDecimal(123)); + complexType.setIntegerValue(new BigInteger("987")); + complexType.setName("TEST"); + + this.pdxSerializer.toData(complexType, mockPdxWriter); + + verify(mockPdxWriter, times(1)).writeField(eq("id"), eq(2L), eq(Long.class)); + verify(mockPdxWriter, times(1)) + .writeField(eq("decimalValue"), eq(new BigDecimal(123)), eq(BigDecimal.class)); + verify(mockPdxWriter, times(1)) + .writeField(eq("integerValue"), eq(new BigInteger("987")), eq(BigInteger.class)); + verify(mockPdxWriter, times(1)) + .writeField(eq("name"), eq("TEST"), eq(String.class)); + verify(mockPdxWriter, times(1)).markIdentityField(eq("id")); + verifyNoMoreInteractions(mockPdxWriter); + } + + @Configuration + @SuppressWarnings("unused") + static class TestConfiguration { + + @Bean + MappingPdxSerializer testMappingPdxSerializer(ConfigurableApplicationContext applicationContext) { + return MappingPdxSerializer.create(applicationContext.getBeanFactory().getConversionService()); + } + } +}