forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request quarkusio#19543 from loicmathieu/kafka-serializer-…
…null-handling Kafka serializer null handling
- Loading branch information
Showing
8 changed files
with
289 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...nt/runtime/src/test/java/io/quarkus/kafka/client/serialization/JsonbDeserializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package io.quarkus.kafka.client.serialization; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
import java.lang.reflect.Type; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class JsonbDeserializerTest { | ||
@Test | ||
void shouldDeserializeEntity() { | ||
MyEntity expected = new MyEntity(1, "entity1"); | ||
JsonbDeserializer<MyEntity> deserializer = new JsonbDeserializer<>(MyEntity.class); | ||
MyEntity actual = deserializer.deserialize("topic", "{\"id\":1,\"name\":\"entity1\"}".getBytes()); | ||
assertNotNull(actual); | ||
assertEquals(expected, actual); | ||
} | ||
|
||
@Test | ||
void shouldDeserializeListOfEntities() { | ||
Type listType = new ArrayList<MyEntity>() { | ||
}.getClass().getGenericSuperclass(); | ||
JsonbDeserializer<List<MyEntity>> deserializer = new JsonbDeserializer<>(listType); | ||
List<MyEntity> actuals = deserializer.deserialize("topic", | ||
"[{\"id\":1,\"name\":\"entity1\"},{\"id\":2,\"name\":\"entity2\"}]".getBytes()); | ||
assertNotNull(actuals); | ||
assertEquals(2, actuals.size()); | ||
} | ||
|
||
@Test | ||
void shouldDeserializeNullAsNullString() { | ||
JsonbDeserializer<MyEntity> deserializer = new JsonbDeserializer<>(MyEntity.class); | ||
MyEntity results = deserializer.deserialize("topic", "null".getBytes()); | ||
assertNull(results); | ||
} | ||
|
||
@Test | ||
void shouldDeserializeNullAsNull() { | ||
JsonbDeserializer<MyEntity> deserializer = new JsonbDeserializer<>(MyEntity.class); | ||
MyEntity results = deserializer.deserialize("topic", null); | ||
assertNull(results); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...ient/runtime/src/test/java/io/quarkus/kafka/client/serialization/JsonbSerializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package io.quarkus.kafka.client.serialization; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class JsonbSerializerTest { | ||
|
||
@Test | ||
void shouldSerializeEntity() { | ||
JsonbSerializer<MyEntity> serializer = new JsonbSerializer<>(); | ||
MyEntity entity = new MyEntity(1, "entity1"); | ||
byte[] result = serializer.serialize("topic", entity); | ||
assertNotNull(result); | ||
assertEquals("{\"id\":1,\"name\":\"entity1\"}", new String(result)); | ||
} | ||
|
||
@Test | ||
void shouldSerializeListOfEntities() { | ||
JsonbSerializer<List<MyEntity>> serializer = new JsonbSerializer<>(); | ||
MyEntity entity1 = new MyEntity(1, "entity1"); | ||
MyEntity entity2 = new MyEntity(2, "entity2"); | ||
byte[] result = serializer.serialize("topic", List.of(entity1, entity2)); | ||
assertNotNull(result); | ||
assertEquals("[{\"id\":1,\"name\":\"entity1\"},{\"id\":2,\"name\":\"entity2\"}]", new String(result)); | ||
} | ||
|
||
@Test | ||
void shouldSerializeNullAsNullString() { | ||
JsonbSerializer<MyEntity> serializer = new JsonbSerializer<>(); | ||
byte[] results = serializer.serialize("topic", null); | ||
assertNotNull(results); | ||
assertEquals("null", new String(results)); | ||
} | ||
|
||
@Test | ||
void shouldSerializeNullAsNull() { | ||
JsonbSerializer<MyEntity> serializer = new JsonbSerializer<>(); | ||
serializer.configure(Map.of(JsonbSerializer.NULL_AS_NULL_CONFIG, "true"), false); | ||
byte[] results = serializer.serialize("topic", null); | ||
assertNull(results); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
...ns/kafka-client/runtime/src/test/java/io/quarkus/kafka/client/serialization/MyEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.quarkus.kafka.client.serialization; | ||
|
||
import java.util.Objects; | ||
|
||
public class MyEntity { | ||
public long id; | ||
public String name; | ||
|
||
// used by deserializers | ||
public MyEntity() { | ||
} | ||
|
||
public MyEntity(long id, String name) { | ||
this.id = id; | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) | ||
return true; | ||
if (o == null || getClass() != o.getClass()) | ||
return false; | ||
MyEntity myEntity = (MyEntity) o; | ||
return id == myEntity.id && Objects.equals(name, myEntity.name); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(id, name); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...ime/src/test/java/io/quarkus/kafka/client/serialization/ObjectMapperDeserializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package io.quarkus.kafka.client.serialization; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
|
||
class ObjectMapperDeserializerTest { | ||
@Test | ||
void shouldDeserializeEntity() { | ||
MyEntity expected = new MyEntity(1, "entity1"); | ||
ObjectMapperDeserializer<MyEntity> deserializer = new ObjectMapperDeserializer<>(MyEntity.class); | ||
MyEntity actual = deserializer.deserialize("topic", "{\"id\":1,\"name\":\"entity1\"}".getBytes()); | ||
assertNotNull(actual); | ||
assertEquals(expected, actual); | ||
} | ||
|
||
@Test | ||
void shouldDeserializeListOfEntities() { | ||
TypeReference<List<MyEntity>> listType = new TypeReference<>() { | ||
}; | ||
ObjectMapperDeserializer<List<MyEntity>> deserializer = new ObjectMapperDeserializer<>(listType); | ||
List<MyEntity> actuals = deserializer.deserialize("topic", | ||
"[{\"id\":1,\"name\":\"entity1\"},{\"id\":2,\"name\":\"entity2\"}]".getBytes()); | ||
assertNotNull(actuals); | ||
assertEquals(2, actuals.size()); | ||
} | ||
|
||
@Test | ||
void shouldDeserializeNullAsNullString() { | ||
ObjectMapperDeserializer<MyEntity> deserializer = new ObjectMapperDeserializer<>(MyEntity.class); | ||
MyEntity results = deserializer.deserialize("topic", "null".getBytes()); | ||
assertNull(results); | ||
} | ||
|
||
@Test | ||
void shouldDeserializeNullAsNull() { | ||
ObjectMapperDeserializer<MyEntity> deserializer = new ObjectMapperDeserializer<>(MyEntity.class); | ||
MyEntity results = deserializer.deserialize("topic", null); | ||
assertNull(results); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...ntime/src/test/java/io/quarkus/kafka/client/serialization/ObjectMapperSerializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package io.quarkus.kafka.client.serialization; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class ObjectMapperSerializerTest { | ||
|
||
@Test | ||
void shouldSerializeEntity() { | ||
ObjectMapperSerializer<MyEntity> serializer = new ObjectMapperSerializer<>(); | ||
MyEntity entity = new MyEntity(1, "entity1"); | ||
byte[] result = serializer.serialize("topic", entity); | ||
assertNotNull(result); | ||
assertEquals("{\"id\":1,\"name\":\"entity1\"}", new String(result)); | ||
} | ||
|
||
@Test | ||
void shouldSerializeListOfEntities() { | ||
ObjectMapperSerializer<List<MyEntity>> serializer = new ObjectMapperSerializer<>(); | ||
MyEntity entity1 = new MyEntity(1, "entity1"); | ||
MyEntity entity2 = new MyEntity(2, "entity2"); | ||
byte[] result = serializer.serialize("topic", List.of(entity1, entity2)); | ||
assertNotNull(result); | ||
assertEquals("[{\"id\":1,\"name\":\"entity1\"},{\"id\":2,\"name\":\"entity2\"}]", new String(result)); | ||
} | ||
|
||
@Test | ||
void shouldSerializeNullAsNullString() { | ||
ObjectMapperSerializer<MyEntity> serializer = new ObjectMapperSerializer<>(); | ||
byte[] results = serializer.serialize("topic", null); | ||
assertNotNull(results); | ||
assertEquals("null", new String(results)); | ||
} | ||
|
||
@Test | ||
void shouldSerializeNullAsNull() { | ||
ObjectMapperSerializer<MyEntity> serializer = new ObjectMapperSerializer<>(); | ||
serializer.configure(Map.of(ObjectMapperSerializer.NULL_AS_NULL_CONFIG, "true"), false); | ||
byte[] results = serializer.serialize("topic", null); | ||
assertNull(results); | ||
} | ||
|
||
} |