Skip to content

Commit 997ad53

Browse files
committed
Assert content of serialized bytes array.
1 parent 213fbe3 commit 997ad53

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

avro/src/test/java/com/fasterxml/jackson/dataformat/avro/BigDecimalTest.java

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,21 @@ public void testSerialization_toString() throws Exception {
135135

136136
AvroSchema schema = MAPPER.schemaFrom(schemaString);
137137

138-
// WHEN
139-
// serialize
138+
// WHEN - serialize
140139
byte[] bytes = MAPPER.writer(schema)
141140
.writeValueAsBytes(new BigDecimalAndName(BigDecimal.valueOf(42.2), "peter"));
142141

143-
// deserialize
142+
// THEN
143+
assertThat(bytes).isEqualTo(new byte[]{
144+
// bigDecimalValue
145+
0x08, // -> 4 dec - bigDecimalValue property string value length
146+
0x34, 0x32, 0x2E, 0x32, // -> "42.2" in ASCII
147+
// name
148+
0x0A, // -> 5 dec - name property string length
149+
0x70, 0x65, 0x74, 0x65, 0x72 // -> "peter" in ASCII
150+
});
151+
152+
// WHEN - deserialize
144153
BigDecimalAndName result = MAPPER.reader(schema)
145154
.forType(BigDecimalAndName.class)
146155
.readValue(bytes);
@@ -172,14 +181,24 @@ public void testSerialization_toBytesWithLogicalTypeDecimal() throws Exception {
172181

173182
AvroSchema schema = MAPPER.schemaFrom(schemaString);
174183

175-
// WHEN
176-
// serialize
184+
// WHEN - serialize
177185
byte[] bytes = MAPPER.writer(schema)
178186
.writeValueAsBytes(new BigDecimalAndName(
179187
new BigDecimal("42.2"),
180188
"peter"));
181-
182-
// deserialize
189+
// THEN
190+
assertThat(bytes).isEqualTo(new byte[]{
191+
// bigDecimalValue
192+
0x02, // -> 1 dec - second bigDecimalValue property type (bytes)
193+
0x04, // -> 2 dec - bigDecimalValue property bytes length
194+
0x10, 0x7C, // -> 0x107C -> 4220 dec - it is 42.2 value in scale 2.
195+
// name
196+
0x02, // 1 dec - second name property type (string)
197+
0x0A, // -> 5 dec - name property string length
198+
0x70, 0x65, 0x74, 0x65, 0x72 // -> "peter" in ASCII
199+
});
200+
201+
// WHEN - deserialize
183202
BigDecimalAndName result = MAPPER.reader(schema)
184203
.forType(BigDecimalAndName.class)
185204
.readValue(bytes);
@@ -215,14 +234,25 @@ public void testSerialization_toFixedWithLogicalTypeDecimal() throws Exception {
215234

216235
AvroSchema schema = MAPPER.schemaFrom(schemaString);
217236

218-
// WHEN
219-
// serialize
237+
// WHEN - serialize
220238
byte[] bytes = MAPPER.writer(schema)
221239
.writeValueAsBytes(new BigDecimalAndName(
222240
new BigDecimal("42.2"),
223241
"peter"));
224242

225-
// deserialize
243+
// THEN
244+
assertThat(bytes).isEqualTo(new byte[]{
245+
// bigDecimalValue
246+
0x02, // -> 1 dec - second bigDecimalValue property type (bytes)
247+
// 10 bytes long fixed value
248+
0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x10 ,0x7C, // -> 0x107C -> 4220 dec - it is 42.2 value in scale 2.
249+
// name
250+
0x02, // 1 dec - second name property type (string)
251+
0x0A, // -> 5 dec - name property string length
252+
0x70, 0x65, 0x74, 0x65, 0x72 // -> "peter" in ASCII
253+
});
254+
255+
// WHEN - deserialize
226256
BigDecimalAndName result = MAPPER.reader(schema)
227257
.forType(BigDecimalAndName.class)
228258
.readValue(bytes);

0 commit comments

Comments
 (0)