AvroSerializer { public byte[] serialize(GenericContainer record) throws IOException { if (record != null) { DatumWriter<GenericContainer> datumWriter = new GenericDatumWriter<>(record.getSchema()); ByteArrayOutputStream out = new ByteArrayOutputStream(); DataFileWriter<GenericContainer> writer = new DataFileWriter<>(datumWriter); writer.create(record.getSchema(), out); writer.append(record); writer.close(); out.close(); return out.toByteArray(); } else { return null; } }  byte[] serialize(GenericContainer record);  }
@Test public void testSerializedDataIncludesAvroSchema() throws IOException { GenericContainer anyValidRecord = new User("anyName"); AvroDeserializer decoder = new AvroDeserializer(); AvroSerializer encoder = new AvroSerializer(); byte[] serializedRecord = encoder.serialize(anyValidRecord); GenericContainer[] decodedRecords = decoder.deserialize(anyValidRecord.getSchema(), serializedRecord); assertThat(decodedRecords.length).isEqualTo(1); assertThat(anyValidRecord.getSchema()).isEqualTo(decodedRecords[0].getSchema()); }