|
| 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