|
2 | 2 |
|
3 | 3 | import com.fasterxml.jackson.annotation.JsonCreator; |
4 | 4 | import com.fasterxml.jackson.annotation.JsonProperty; |
5 | | -import com.fasterxml.jackson.databind.JsonMappingException; |
6 | | -import com.fasterxml.jackson.dataformat.avro.annotation.Decimal; |
7 | | -import com.fasterxml.jackson.dataformat.avro.schema.AvroSchemaGenerator; |
8 | | -import org.apache.avro.LogicalTypes; |
9 | | -import org.apache.avro.Schema; |
10 | | -import org.junit.Test; |
11 | 5 |
|
12 | 6 | import java.math.BigDecimal; |
13 | 7 |
|
14 | 8 | import static org.assertj.core.api.Assertions.assertThat; |
15 | 9 |
|
16 | | -public class BigDecimalTest extends AvroTestBase { |
| 10 | +public class BigDecimal_serialization_and_deserializationTest extends AvroTestBase { |
17 | 11 | private static final AvroMapper MAPPER = new AvroMapper(); |
18 | 12 |
|
19 | | - static class BigDecimalWithDecimalAnnotationWrapper { |
20 | | - @JsonProperty(required = true) // field is required to have simpler avro schema |
21 | | - @Decimal(precision = 10, scale = 2) |
22 | | - public final BigDecimal bigDecimalValue; |
23 | | - |
24 | | - public BigDecimalWithDecimalAnnotationWrapper(BigDecimal bigDecimalValue) { |
25 | | - this.bigDecimalValue = bigDecimalValue; |
26 | | - } |
27 | | - } |
28 | | - |
29 | | - @Test |
30 | | - public void testSchemaCreation_withLogicalTypesDisabled_onBigDecimalWithDecimalAnnotation() throws JsonMappingException { |
31 | | - // GIVEN |
32 | | - AvroSchemaGenerator gen = new AvroSchemaGenerator() |
33 | | - .disableLogicalTypes(); |
34 | | - |
35 | | - // WHEN |
36 | | - MAPPER.acceptJsonFormatVisitor(BigDecimalWithDecimalAnnotationWrapper.class, gen); |
37 | | - // actualSchema = MAPPER.schemaFor(BigDecimalWithDecimalAnnotationWrapper.class) would be enough in this case |
38 | | - // because logical types are disabled by default. |
39 | | - final Schema actualSchema = gen.getGeneratedSchema().getAvroSchema(); |
40 | | - |
41 | | - System.out.println(BigDecimalWithDecimalAnnotationWrapper.class.getSimpleName() + " schema:" + actualSchema.toString(true)); |
42 | | - |
43 | | - // THEN |
44 | | - assertThat(actualSchema.getField("bigDecimalValue")).isNotNull(); |
45 | | - Schema bigDecimalValue = actualSchema.getField("bigDecimalValue").schema(); |
46 | | - assertThat(bigDecimalValue.getType()).isEqualTo(Schema.Type.STRING); |
47 | | - assertThat(bigDecimalValue.getLogicalType()).isNull(); |
48 | | - assertThat(bigDecimalValue.getProp("java-class")).isEqualTo("java.math.BigDecimal"); |
49 | | - } |
50 | | - |
51 | | - @Test |
52 | | - public void testSchemaCreation_withLogicalTypesEnabled_onBigDecimalWithDecimalAnnotation() throws JsonMappingException { |
53 | | - // GIVEN |
54 | | - AvroSchemaGenerator gen = new AvroSchemaGenerator() |
55 | | - .enableLogicalTypes(); |
56 | | - |
57 | | - // WHEN |
58 | | - MAPPER.acceptJsonFormatVisitor(BigDecimalWithDecimalAnnotationWrapper.class, gen); |
59 | | - final Schema actualSchema = gen.getGeneratedSchema().getAvroSchema(); |
60 | | - |
61 | | - System.out.println(BigDecimalWithDecimalAnnotationWrapper.class.getSimpleName() + " schema:" + actualSchema.toString(true)); |
62 | | - |
63 | | - // THEN |
64 | | - assertThat(actualSchema.getField("bigDecimalValue")).isNotNull(); |
65 | | - Schema bigDecimalValue = actualSchema.getField("bigDecimalValue").schema(); |
66 | | - assertThat(bigDecimalValue.getType()).isEqualTo(Schema.Type.BYTES); |
67 | | - assertThat(bigDecimalValue.getLogicalType()).isEqualTo(LogicalTypes.decimal(10, 2)); |
68 | | - assertThat(bigDecimalValue.getProp("java-class")).isNull(); |
69 | | - } |
70 | | - |
71 | | - static class BigDecimalWithDecimalAnnotationToFixedWrapper { |
72 | | - @JsonProperty(required = true) // field is required to have simpler avro schema |
73 | | - @AvroFixedSize(typeName = "BigDecimalWithDecimalAnnotationToFixedWrapper", size = 10) |
74 | | - @Decimal(precision = 6, scale = 3) |
75 | | - public final BigDecimal bigDecimalValue; |
76 | | - |
77 | | - public BigDecimalWithDecimalAnnotationToFixedWrapper(BigDecimal bigDecimalValue) { |
78 | | - this.bigDecimalValue = bigDecimalValue; |
79 | | - } |
80 | | - } |
81 | | - |
82 | | - @Test |
83 | | - public void testSchemaCreation_withLogicalTypesEnabled_onBigDecimalWithDecimalAnnotationToFixed() throws JsonMappingException { |
84 | | - // GIVEN |
85 | | - AvroSchemaGenerator gen = new AvroSchemaGenerator() |
86 | | - .enableLogicalTypes(); |
87 | | - |
88 | | - // WHEN |
89 | | - MAPPER.acceptJsonFormatVisitor(BigDecimalWithDecimalAnnotationToFixedWrapper.class, gen); |
90 | | - final Schema actualSchema = gen.getGeneratedSchema().getAvroSchema(); |
91 | | - |
92 | | - System.out.println(BigDecimalWithDecimalAnnotationToFixedWrapper.class.getSimpleName() + " schema:" + actualSchema.toString(true)); |
93 | | - |
94 | | - // THEN |
95 | | - assertThat(actualSchema.getField("bigDecimalValue")).isNotNull(); |
96 | | - |
97 | | - Schema bigDecimalValue = actualSchema.getField("bigDecimalValue").schema(); |
98 | | - assertThat(bigDecimalValue.getType()).isEqualTo(Schema.Type.FIXED); |
99 | | - assertThat(bigDecimalValue.getFixedSize()).isEqualTo(10); |
100 | | - assertThat(bigDecimalValue.getLogicalType()).isEqualTo(LogicalTypes.decimal(6, 3)); |
101 | | - assertThat(bigDecimalValue.getProp("java-class")).isNull(); |
102 | | - } |
103 | | - |
104 | 13 | static class BigDecimalAndName { |
105 | 14 | public final BigDecimal bigDecimalValue; |
106 | 15 | public final String name; |
|
0 commit comments