Skip to content

Commit 1a943c7

Browse files
committed
DATAGEODE-302 - Add test case asserting the expected behavior of MappingPdxSerializer when serializing complex types including BigDecimal and BigInteger properties to PDX.
1 parent 5a6a8c3 commit 1a943c7

File tree

3 files changed

+153
-2
lines changed

3 files changed

+153
-2
lines changed

spring-data-geode/src/main/java/org/springframework/data/gemfire/mapping/MappingPdxSerializer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,8 @@ boolean doToData(Object value, @NonNull PdxWriter pdxWriter) {
622622

623623
PdxSerializer customPdxSerializer = resolveCustomPdxSerializer(persistentProperty);
624624

625+
String propertyName = persistentProperty.getName();
626+
625627
Supplier<String> messageSuffix = () -> customPdxSerializer != null
626628
? String.format(" using custom PdxSerializer [%s]", customPdxSerializer)
627629
: "";
@@ -632,15 +634,15 @@ boolean doToData(Object value, @NonNull PdxWriter pdxWriter) {
632634

633635
if (getLogger().isDebugEnabled()) {
634636
getLogger().debug("Serializing entity [{}] property [{}] value [{}] of type [{}] to PDX{}",
635-
entity.getType().getName(), persistentProperty.getName(), propertyValue,
637+
entity.getType().getName(), propertyName, propertyValue,
636638
ObjectUtils.nullSafeClassName(propertyValue), messageSuffix.get());
637639
}
638640

639641
if (customPdxSerializer != null) {
640642
customPdxSerializer.toData(propertyValue, pdxWriter);
641643
}
642644
else {
643-
pdxWriter.writeField(persistentProperty.getName(), propertyValue,
645+
pdxWriter.writeField(propertyName, propertyValue,
644646
(Class<Object>) persistentProperty.getType());
645647
}
646648
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.app.model;
17+
18+
import java.math.BigDecimal;
19+
import java.math.BigInteger;
20+
21+
import org.springframework.data.annotation.Id;
22+
import org.springframework.data.gemfire.mapping.annotation.Region;
23+
24+
import lombok.Data;
25+
import lombok.ToString;
26+
27+
/**
28+
* {@link ComplexType} class used for testing purposes.
29+
*
30+
* @author John Blum
31+
* @see org.springframework.data.annotation.Id
32+
* @see org.springframework.data.gemfire.mapping.annotation.Region
33+
* @since 2.5.0
34+
*/
35+
@Data
36+
@ToString
37+
@Region("Examples")
38+
public class ComplexType {
39+
40+
private BigDecimal decimalValue;
41+
42+
private BigInteger integerValue;
43+
44+
@Id
45+
private Long id;
46+
47+
private String name;
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright 2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.gemfire.mapping;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.mockito.ArgumentMatchers.eq;
20+
import static org.mockito.Mockito.mock;
21+
import static org.mockito.Mockito.times;
22+
import static org.mockito.Mockito.verify;
23+
import static org.mockito.Mockito.verifyNoMoreInteractions;
24+
25+
import java.math.BigDecimal;
26+
import java.math.BigInteger;
27+
28+
import org.junit.Before;
29+
import org.junit.Test;
30+
import org.junit.runner.RunWith;
31+
32+
import org.apache.geode.pdx.PdxWriter;
33+
34+
import org.springframework.beans.factory.annotation.Autowired;
35+
import org.springframework.context.ConfigurableApplicationContext;
36+
import org.springframework.context.annotation.Bean;
37+
import org.springframework.context.annotation.Configuration;
38+
import org.springframework.test.context.ContextConfiguration;
39+
import org.springframework.test.context.junit4.SpringRunner;
40+
41+
import example.app.model.ComplexType;
42+
43+
/**
44+
* Integration Tests for {@link MappingPdxSerializer} with {@literal complex} and {@literal simple} {@link Class types}.
45+
*
46+
* @author John Blum
47+
* @see org.junit.Test
48+
* @see org.mockito.Mockito
49+
* @see org.apache.geode.pdx.PdxSerializer
50+
* @see org.apache.geode.pdx.PdxWriter
51+
* @see org.springframework.data.gemfire.mapping.MappingPdxSerializer
52+
* @since 2.5.0
53+
*/
54+
@RunWith(SpringRunner.class)
55+
@ContextConfiguration
56+
public class TypeMappingPdxSerializerIntegrationTests {
57+
58+
@Autowired
59+
private MappingPdxSerializer pdxSerializer;
60+
61+
@Before
62+
public void setup() {
63+
assertThat(this.pdxSerializer).isNotNull();
64+
}
65+
66+
@Test
67+
public void mapsComplexTypeSuccessfully() {
68+
69+
PdxWriter mockPdxWriter = mock(PdxWriter.class);
70+
71+
ComplexType complexType = new ComplexType();
72+
73+
complexType.setId(2L);
74+
complexType.setDecimalValue(new BigDecimal(123));
75+
complexType.setIntegerValue(new BigInteger("987"));
76+
complexType.setName("TEST");
77+
78+
this.pdxSerializer.toData(complexType, mockPdxWriter);
79+
80+
verify(mockPdxWriter, times(1)).writeField(eq("id"), eq(2L), eq(Long.class));
81+
verify(mockPdxWriter, times(1))
82+
.writeField(eq("decimalValue"), eq(new BigDecimal(123)), eq(BigDecimal.class));
83+
verify(mockPdxWriter, times(1))
84+
.writeField(eq("integerValue"), eq(new BigInteger("987")), eq(BigInteger.class));
85+
verify(mockPdxWriter, times(1))
86+
.writeField(eq("name"), eq("TEST"), eq(String.class));
87+
verify(mockPdxWriter, times(1)).markIdentityField(eq("id"));
88+
verifyNoMoreInteractions(mockPdxWriter);
89+
}
90+
91+
@Configuration
92+
@SuppressWarnings("unused")
93+
static class TestConfiguration {
94+
95+
@Bean
96+
MappingPdxSerializer testMappingPdxSerializer(ConfigurableApplicationContext applicationContext) {
97+
return MappingPdxSerializer.create(applicationContext.getBeanFactory().getConversionService());
98+
}
99+
}
100+
}

0 commit comments

Comments
 (0)