|
| 1 | +package com.provectus.kafka.ui.serdes.builtin; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
| 6 | +import com.provectus.kafka.ui.serde.api.DeserializeResult; |
| 7 | +import com.provectus.kafka.ui.serde.api.Serde; |
| 8 | +import com.provectus.kafka.ui.serdes.PropertyResolverImpl; |
| 9 | +import io.confluent.kafka.schemaregistry.avro.AvroSchemaUtils; |
| 10 | +import java.io.ByteArrayOutputStream; |
| 11 | +import java.io.IOException; |
| 12 | +import org.apache.avro.Schema; |
| 13 | +import org.apache.avro.file.DataFileWriter; |
| 14 | +import org.apache.avro.generic.GenericData; |
| 15 | +import org.apache.avro.generic.GenericDatumWriter; |
| 16 | +import org.apache.avro.generic.GenericRecord; |
| 17 | +import org.junit.jupiter.api.BeforeEach; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.junit.jupiter.params.ParameterizedTest; |
| 20 | +import org.junit.jupiter.params.provider.EnumSource; |
| 21 | + |
| 22 | +class AvroEmbeddedSerdeTest { |
| 23 | + |
| 24 | + private AvroEmbeddedSerde avroEmbeddedSerde; |
| 25 | + |
| 26 | + @BeforeEach |
| 27 | + void init() { |
| 28 | + avroEmbeddedSerde = new AvroEmbeddedSerde(); |
| 29 | + avroEmbeddedSerde.configure( |
| 30 | + PropertyResolverImpl.empty(), |
| 31 | + PropertyResolverImpl.empty(), |
| 32 | + PropertyResolverImpl.empty() |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + @ParameterizedTest |
| 37 | + @EnumSource |
| 38 | + void canDeserializeReturnsTrueForAllTargets(Serde.Target target) { |
| 39 | + assertThat(avroEmbeddedSerde.canDeserialize("anyTopic", target)) |
| 40 | + .isTrue(); |
| 41 | + } |
| 42 | + |
| 43 | + @ParameterizedTest |
| 44 | + @EnumSource |
| 45 | + void canSerializeReturnsFalseForAllTargets(Serde.Target target) { |
| 46 | + assertThat(avroEmbeddedSerde.canSerialize("anyTopic", target)) |
| 47 | + .isFalse(); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void deserializerParsesAvroDataWithEmbeddedSchema() throws Exception { |
| 52 | + Schema schema = new Schema.Parser().parse(""" |
| 53 | + { |
| 54 | + "type": "record", |
| 55 | + "name": "TestAvroRecord", |
| 56 | + "fields": [ |
| 57 | + { "name": "field1", "type": "string" }, |
| 58 | + { "name": "field2", "type": "int" } |
| 59 | + ] |
| 60 | + } |
| 61 | + """ |
| 62 | + ); |
| 63 | + GenericRecord record = new GenericData.Record(schema); |
| 64 | + record.put("field1", "this is test msg"); |
| 65 | + record.put("field2", 100500); |
| 66 | + |
| 67 | + String jsonRecord = new String(AvroSchemaUtils.toJson(record)); |
| 68 | + byte[] serializedRecordBytes = serializeAvroWithEmbeddedSchema(record); |
| 69 | + |
| 70 | + var deserializer = avroEmbeddedSerde.deserializer("anyTopic", Serde.Target.KEY); |
| 71 | + DeserializeResult result = deserializer.deserialize(null, serializedRecordBytes); |
| 72 | + assertThat(result.getType()).isEqualTo(DeserializeResult.Type.JSON); |
| 73 | + assertThat(result.getAdditionalProperties()).isEmpty(); |
| 74 | + assertJsonEquals(jsonRecord, result.getResult()); |
| 75 | + } |
| 76 | + |
| 77 | + private void assertJsonEquals(String expected, String actual) throws IOException { |
| 78 | + var mapper = new JsonMapper(); |
| 79 | + assertThat(mapper.readTree(actual)).isEqualTo(mapper.readTree(expected)); |
| 80 | + } |
| 81 | + |
| 82 | + private byte[] serializeAvroWithEmbeddedSchema(GenericRecord record) throws IOException { |
| 83 | + try (DataFileWriter<GenericRecord> writer = new DataFileWriter<>(new GenericDatumWriter<>()); |
| 84 | + ByteArrayOutputStream baos = new ByteArrayOutputStream()) { |
| 85 | + writer.create(record.getSchema(), baos); |
| 86 | + writer.append(record); |
| 87 | + writer.flush(); |
| 88 | + return baos.toByteArray(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments