diff --git a/kafka-avro/build.gradle.kts b/kafka-avro/build.gradle.kts index 958281f..79c96d3 100644 --- a/kafka-avro/build.gradle.kts +++ b/kafka-avro/build.gradle.kts @@ -1,3 +1,7 @@ +plugins { + id("com.github.davidmc24.gradle.plugin.avro") version "1.9.1" +} + dependencies { implementation("org.apache.kafka:kafka-clients") implementation(libs.avro) diff --git a/kafka-avro/src/test/avro/AvroSchema1.avsc b/kafka-avro/src/test/avro/AvroSchema1.avsc new file mode 100644 index 0000000..608efe7 --- /dev/null +++ b/kafka-avro/src/test/avro/AvroSchema1.avsc @@ -0,0 +1,21 @@ +{ + "type": "record", + "name": "AvroSchema1", + "namespace": "com.alliander.gxf.utilities.kafka.avro", + "fields": [ + { + "name": "field1", + "type": { + "type": "string", + "avro.java.string": "String" + } + }, + { + "name": "field2", + "type": { + "type": "string", + "avro.java.string": "String" + } + } + ] +} diff --git a/kafka-avro/src/test/avro/AvroSchema2.avsc b/kafka-avro/src/test/avro/AvroSchema2.avsc new file mode 100644 index 0000000..baeba73 --- /dev/null +++ b/kafka-avro/src/test/avro/AvroSchema2.avsc @@ -0,0 +1,14 @@ +{ + "type": "record", + "name": "AvroSchema2", + "namespace": "com.alliander.gxf.utilities.kafka.avro", + "fields": [ + { + "name": "message2", + "type": { + "type": "string", + "avro.java.string": "String" + } + } + ] +} diff --git a/kafka-avro/src/test/avro/AvroSchema3.avsc b/kafka-avro/src/test/avro/AvroSchema3.avsc new file mode 100644 index 0000000..9a5bb8c --- /dev/null +++ b/kafka-avro/src/test/avro/AvroSchema3.avsc @@ -0,0 +1,14 @@ +{ + "type": "record", + "name": "AvroSchema3", + "namespace": "com.alliander.gxf.utilities.kafka.avro", + "fields": [ + { + "name": "message3", + "type": { + "type": "string", + "avro.java.string": "String" + } + } + ] +} diff --git a/kafka-avro/src/test/kotlin/com/gxf/utilities/kafka/avro/AvroDeserializerTest.kt b/kafka-avro/src/test/kotlin/com/gxf/utilities/kafka/avro/AvroDeserializerTest.kt new file mode 100644 index 0000000..8141af8 --- /dev/null +++ b/kafka-avro/src/test/kotlin/com/gxf/utilities/kafka/avro/AvroDeserializerTest.kt @@ -0,0 +1,30 @@ +package com.gxf.utilities.kafka.avro + +import com.alliander.gxf.utilities.kafka.avro.AvroSchema1 +import com.alliander.gxf.utilities.kafka.avro.AvroSchema2 +import com.alliander.gxf.utilities.kafka.avro.AvroSchema3 +import org.apache.kafka.common.errors.SerializationException +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test + +class AvroDeserializerTest { + + @Test + fun avroDeserializerTest() { + val message1 = AvroSchema1("field no 1", "field no 2") + val message2 = AvroSchema2("message in a bottle") + val message3 = AvroSchema3("message in a bottle") + val deserializer = AvroDeserializer(listOf(AvroSchema1.getClassSchema(), AvroSchema2.getClassSchema())) + + assertThat(deserializer.deserialize("topic1", message1.toByteBuffer().array())) + .isEqualTo(message1) + assertThat(deserializer.deserialize("topic2", message2.toByteBuffer().array())) + .isEqualTo(message2) + + assertThatThrownBy({deserializer.deserialize("topic3", message3.toByteBuffer().array())}) + .isInstanceOf(SerializationException::class.java) + .hasMessageContaining("Error deserializing Avro message for topic: topic3"); + + } +} diff --git a/kafka-avro/src/test/kotlin/com/gxf/utilities/kafka/avro/AvroEncoderTest.kt b/kafka-avro/src/test/kotlin/com/gxf/utilities/kafka/avro/AvroEncoderTest.kt index 0625991..66177d2 100644 --- a/kafka-avro/src/test/kotlin/com/gxf/utilities/kafka/avro/AvroEncoderTest.kt +++ b/kafka-avro/src/test/kotlin/com/gxf/utilities/kafka/avro/AvroEncoderTest.kt @@ -1,7 +1,8 @@ package com.gxf.utilities.kafka.avro -import org.apache.avro.Schema -import org.apache.avro.specific.SpecificRecordBase +import com.alliander.gxf.utilities.kafka.avro.AvroSchema1 +import com.alliander.gxf.utilities.kafka.avro.AvroSchema2 +import org.assertj.core.api.AbstractIntegerAssert import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test import java.io.ByteArrayOutputStream @@ -24,55 +25,3 @@ class AvroEncoderTest { assertThat(AvroEncoder.encoders.size).isEqualTo(2) } } - -class AvroSchema1(private var field1: String, private var field2: String): SpecificRecordBase() { - override fun getSchema(): Schema = Schema.Parser() - .parse("{\"type\":\"record\",\"name\":\"AvroSchema1\",\"namespace\":\"com.alliander.gxf.utilities.kafka.avro\",\"fields\":[{\"name\":\"field1\",\"type\":{\"type\":\"string\",\"avro.java.string\":\"String\"}}, {\"name\":\"field2\",\"type\":{\"type\":\"string\",\"avro.java.string\":\"String\"}}]}") - - override fun put(field: Int, value: Any?) { - when(field) { - 0 -> { - if(value != null) { - field1 = value.toString() - } - } - 1 -> { - if(value != null) { - field2 = value.toString() - } - } - else -> throw IndexOutOfBoundsException() - } - } - - override fun get(field: Int): Any { - return when(field) { - 0 -> field1 - 1 -> field2 - else -> throw IndexOutOfBoundsException() - } - } -} - -class AvroSchema2(private var message: String): SpecificRecordBase() { - override fun getSchema(): Schema = Schema.Parser() - .parse("{\"type\":\"record\",\"name\":\"AvroSchema2\",\"namespace\":\"com.alliander.gxf.utilities.kafka.avro\",\"fields\":[{\"name\":\"message\",\"type\":{\"type\":\"string\",\"avro.java.string\":\"String\"}}]}") - - override fun put(field: Int, value: Any?) { - when(field) { - 0 -> { - if(value != null) { - message = value.toString() - } - } - else -> throw IndexOutOfBoundsException() - } - } - - override fun get(field: Int): Any { - return when(field) { - 0 -> message - else -> throw IndexOutOfBoundsException() - } - } -} diff --git a/kafka-avro/src/test/kotlin/com/gxf/utilities/kafka/avro/AvroSerializerTest.kt b/kafka-avro/src/test/kotlin/com/gxf/utilities/kafka/avro/AvroSerializerTest.kt new file mode 100644 index 0000000..71ba26e --- /dev/null +++ b/kafka-avro/src/test/kotlin/com/gxf/utilities/kafka/avro/AvroSerializerTest.kt @@ -0,0 +1,22 @@ +package com.gxf.utilities.kafka.avro + +import com.alliander.gxf.utilities.kafka.avro.AvroSchema1 +import com.alliander.gxf.utilities.kafka.avro.AvroSchema2 +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class AvroSerializerTest { + @Test + fun testEncodersCache() { + val message1 = AvroSchema1("field no 1", "field no 2") + val message2 = AvroSchema2("message in a bottle") + val serializer = AvroSerializer() + + serializer.serialize("", message1) + serializer.serialize("", message2) + + assertThat(AvroEncoder.encoders).containsKeys(AvroSchema1::class) + assertThat(AvroEncoder.encoders).containsKeys(AvroSchema2::class) + assertThat(AvroEncoder.encoders.size).isEqualTo(2) + } +}