Skip to content

Commit

Permalink
FDP-2470: Add serializer and deserializer tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jasper Kamerling <[email protected]>
  • Loading branch information
jasperkamerling committed Jul 17, 2024
1 parent 3f05a57 commit 91aa15c
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 54 deletions.
4 changes: 4 additions & 0 deletions kafka-avro/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
21 changes: 21 additions & 0 deletions kafka-avro/src/test/avro/AvroSchema1.avsc
Original file line number Diff line number Diff line change
@@ -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"
}
}
]
}
14 changes: 14 additions & 0 deletions kafka-avro/src/test/avro/AvroSchema2.avsc
Original file line number Diff line number Diff line change
@@ -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"
}
}
]
}
14 changes: 14 additions & 0 deletions kafka-avro/src/test/avro/AvroSchema3.avsc
Original file line number Diff line number Diff line change
@@ -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"
}
}
]
}
Original file line number Diff line number Diff line change
@@ -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");

}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
}
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 91aa15c

Please sign in to comment.