22
33import com .fasterxml .jackson .annotation .JsonCreator ;
44import 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 ;
511
612import java .math .BigDecimal ;
713
14+ import static org .assertj .core .api .Assertions .assertThat ;
15+
816public class BigDecimalTest extends AvroTestBase
917{
18+ private static final AvroMapper MAPPER = new AvroMapper ();
19+
20+ static class BigDecimalWithDecimalAnnotationToBytesWrapper {
21+ @ JsonProperty (required = true ) // field is made required only to have simpler avro schema
22+ @ Decimal (precision = 10 , scale = 2 )
23+ public BigDecimal bigDecimalValue ;
24+
25+ public BigDecimalWithDecimalAnnotationToBytesWrapper (BigDecimal bigDecimalValue ) {
26+ this .bigDecimalValue = bigDecimalValue ;
27+ }
28+ }
29+
30+ @ Test
31+ public void testSchemaCreationOnBigDecimalWithDecimalAnnotationToBytes () throws JsonMappingException {
32+ // GIVEN
33+ AvroSchemaGenerator gen = new AvroSchemaGenerator ()
34+ .enableLogicalTypes ();
35+
36+ // WHEN
37+ MAPPER .acceptJsonFormatVisitor (BigDecimalWithDecimalAnnotationToBytesWrapper .class , gen );
38+ final Schema actualSchema = gen .getGeneratedSchema ().getAvroSchema ();
39+
40+ System .out .println (BigDecimalWithDecimalAnnotationToBytesWrapper .class .getSimpleName () + " schema:\n " + actualSchema .toString (true ));
41+
42+ // THEN
43+ assertThat (actualSchema .getField ("bigDecimalValue" )).isNotNull ();
44+
45+ Schema bigDecimalValue = actualSchema .getField ("bigDecimalValue" ).schema ();
46+ assertThat (bigDecimalValue .getType ()).isEqualTo (Schema .Type .BYTES );
47+ assertThat (bigDecimalValue .getLogicalType ()).isEqualTo (LogicalTypes .decimal (10 , 2 ));
48+ assertThat (bigDecimalValue .getProp ("java-class" )).isNull ();
49+ }
50+
51+ static class BigDecimalWithDecimalAnnotationToFixedWrapper {
52+ @ JsonProperty (required = true ) // field is made required only to have simpler avro schema
53+ @ AvroFixedSize (typeName = "BigDecimalWithDecimalAnnotationToFixedWrapper" , size = 10 )
54+ @ Decimal (precision = 6 , scale = 3 )
55+ public BigDecimal bigDecimalValue ;
56+
57+ public BigDecimalWithDecimalAnnotationToFixedWrapper (BigDecimal bigDecimalValue ) {
58+ this .bigDecimalValue = bigDecimalValue ;
59+ }
60+ }
61+
62+ @ Test
63+ public void testSchemaCreationOnBigDecimalWithDecimalAnnotationToFixed () throws JsonMappingException {
64+ // GIVEN
65+ AvroSchemaGenerator gen = new AvroSchemaGenerator ()
66+ .enableLogicalTypes ();
67+
68+ // WHEN
69+ MAPPER .acceptJsonFormatVisitor (BigDecimalWithDecimalAnnotationToFixedWrapper .class , gen );
70+ final Schema actualSchema = gen .getGeneratedSchema ().getAvroSchema ();
71+
72+ System .out .println (BigDecimalWithDecimalAnnotationToFixedWrapper .class .getSimpleName () + " schema:\n " + actualSchema .toString (true ));
73+
74+ // THEN
75+ assertThat (actualSchema .getField ("bigDecimalValue" )).isNotNull ();
76+
77+ Schema bigDecimalValue = actualSchema .getField ("bigDecimalValue" ).schema ();
78+ assertThat (bigDecimalValue .getType ()).isEqualTo (Schema .Type .FIXED );
79+ assertThat (bigDecimalValue .getFixedSize ()).isEqualTo (10 );
80+ assertThat (bigDecimalValue .getLogicalType ()).isEqualTo (LogicalTypes .decimal (6 , 3 ));
81+ assertThat (bigDecimalValue .getProp ("java-class" )).isNull ();
82+ }
83+
1084 public static class NamedAmount {
1185 public final String name ;
1286 public final BigDecimal amount ;
@@ -20,13 +94,12 @@ public NamedAmount(@JsonProperty("name") String name,
2094 }
2195
2296 public void testSerializeBigDecimal () throws Exception {
23- AvroMapper mapper = newMapper ();
24- AvroSchema schema = mapper .schemaFor (NamedAmount .class );
97+ AvroSchema schema = MAPPER .schemaFor (NamedAmount .class );
2598
26- byte [] bytes = mapper .writer (schema )
99+ byte [] bytes = MAPPER .writer (schema )
27100 .writeValueAsBytes (new NamedAmount ("peter" , 42.0 ));
28101
29- NamedAmount result = mapper .reader (schema ).forType (NamedAmount .class ).readValue (bytes );
102+ NamedAmount result = MAPPER .reader (schema ).forType (NamedAmount .class ).readValue (bytes );
30103
31104 assertEquals ("peter" , result .name );
32105 assertEquals (BigDecimal .valueOf (42.0 ), result .amount );
0 commit comments