DATAGEODE-302 - Add test case asserting the expected behavior of MappingPdxSerializer when serializing complex types including BigDecimal and BigInteger properties to PDX.

This commit is contained in:
John Blum
2020-11-04 12:33:21 -08:00
parent 5a6a8c384d
commit 1a943c7046
3 changed files with 153 additions and 2 deletions

View File

@@ -622,6 +622,8 @@ public class MappingPdxSerializer implements PdxSerializer, ApplicationContextAw
PdxSerializer customPdxSerializer = resolveCustomPdxSerializer(persistentProperty);
String propertyName = persistentProperty.getName();
Supplier<String> 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<Object>) persistentProperty.getType());
}
}

View File

@@ -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;
}

View File

@@ -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());
}
}
}