forked from provectus/kafka-ui
-
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.
ISSUE-849: Use a map between topics and message-names when using Prot…
…obufFile (provectus#854) * Use a map between topics and message-names when using ProtobufFile * Validate the given message names for the topics in ProtobufFileRecordSerDe
- Loading branch information
Showing
7 changed files
with
192 additions
and
12 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
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
102 changes: 102 additions & 0 deletions
102
kafka-ui-api/src/test/java/com/provectus/kafka/ui/serde/ProtobufFileRecordSerDeTest.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,102 @@ | ||
package com.provectus.kafka.ui.serde; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.protobuf.DynamicMessage; | ||
import com.google.protobuf.util.JsonFormat; | ||
import com.provectus.kafka.ui.serde.schemaregistry.MessageFormat; | ||
import io.confluent.kafka.schemaregistry.protobuf.ProtobufSchema; | ||
import java.io.IOException; | ||
import java.net.URISyntaxException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.apache.kafka.common.utils.Bytes; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ProtobufFileRecordSerDeTest { | ||
|
||
// Sample message of type `test.Person` | ||
private static byte[] personMessage; | ||
// Sample message of type `test.AddressBook` | ||
private static byte[] addressBookMessage; | ||
private static Path protobufSchemaPath; | ||
|
||
@BeforeAll | ||
static void setUp() throws URISyntaxException, IOException { | ||
protobufSchemaPath = Paths.get(ProtobufFileRecordSerDeTest.class.getClassLoader() | ||
.getResource("address-book.proto").toURI()); | ||
ProtobufSchema protobufSchema = new ProtobufSchema(Files.readString(protobufSchemaPath)); | ||
|
||
DynamicMessage.Builder builder = protobufSchema.newMessageBuilder("test.Person"); | ||
JsonFormat.parser().merge( | ||
"{ \"name\": \"My Name\",\"id\": 101, \"email\": \"user1@example.com\" }", builder); | ||
personMessage = builder.build().toByteArray(); | ||
|
||
builder = protobufSchema.newMessageBuilder("test.AddressBook"); | ||
JsonFormat.parser().merge( | ||
"{\"version\": 1, \"people\": [" | ||
+ "{ \"name\": \"My Name\",\"id\": 102, \"email\": \"user2@example.com\" }]}", builder); | ||
addressBookMessage = builder.build().toByteArray(); | ||
} | ||
|
||
@Test | ||
void testDeserialize() throws IOException { | ||
var messageNameMap = Map.of( | ||
"topic1", "test.Person", | ||
"topic2", "test.AddressBook"); | ||
var deserializer = | ||
new ProtobufFileRecordSerDe(protobufSchemaPath, messageNameMap, null, new ObjectMapper()); | ||
var msg1 = deserializer | ||
.deserialize(new ConsumerRecord<>("topic1", 1, 0, Bytes.wrap("key".getBytes()), | ||
Bytes.wrap(personMessage))); | ||
assertEquals(MessageFormat.PROTOBUF, msg1.getValueFormat()); | ||
assertTrue(msg1.getValue().contains("user1@example.com")); | ||
|
||
var msg2 = deserializer | ||
.deserialize(new ConsumerRecord<>("topic2", 1, 1, Bytes.wrap("key".getBytes()), | ||
Bytes.wrap(addressBookMessage))); | ||
assertTrue(msg2.getValue().contains("user2@example.com")); | ||
} | ||
|
||
@Test | ||
void testNoDefaultMessageName() throws IOException { | ||
// by default the first message type defined in proto definition is used | ||
var deserializer = | ||
new ProtobufFileRecordSerDe(protobufSchemaPath, Collections.emptyMap(), null, | ||
new ObjectMapper()); | ||
var msg = deserializer | ||
.deserialize(new ConsumerRecord<>("topic", 1, 0, Bytes.wrap("key".getBytes()), | ||
Bytes.wrap(personMessage))); | ||
assertTrue(msg.getValue().contains("user1@example.com")); | ||
} | ||
|
||
@Test | ||
void testDefaultMessageName() throws IOException { | ||
var messageNameMap = Map.of("topic1", "test.Person"); | ||
var deserializer = | ||
new ProtobufFileRecordSerDe(protobufSchemaPath, messageNameMap, "test.AddressBook", | ||
new ObjectMapper()); | ||
var msg = deserializer | ||
.deserialize(new ConsumerRecord<>("a_random_topic", 1, 0, Bytes.wrap("key".getBytes()), | ||
Bytes.wrap(addressBookMessage))); | ||
assertTrue(msg.getValue().contains("user2@example.com")); | ||
} | ||
|
||
@Test | ||
void testSerialize() throws IOException { | ||
var messageNameMap = Map.of("topic1", "test.Person"); | ||
var serializer = | ||
new ProtobufFileRecordSerDe(protobufSchemaPath, messageNameMap, "test.AddressBook", | ||
new ObjectMapper()); | ||
var serialized = serializer.serialize("topic1", "key1", "{\"name\":\"MyName\"}", 0); | ||
assertNotNull(serialized.value()); | ||
} | ||
} |
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,39 @@ | ||
// [START declaration] | ||
syntax = "proto3"; | ||
package test; | ||
|
||
// [END declaration] | ||
|
||
// [START java_declaration] | ||
option java_multiple_files = true; | ||
option java_package = "com.example.tutorial.protos"; | ||
option java_outer_classname = "AddressBookProtos"; | ||
// [END java_declaration] | ||
|
||
// [START messages] | ||
message Person { | ||
string name = 1; | ||
int32 id = 2; // Unique ID number for this person. | ||
string email = 3; | ||
|
||
enum PhoneType { | ||
MOBILE = 0; | ||
HOME = 1; | ||
WORK = 2; | ||
} | ||
|
||
message PhoneNumber { | ||
string number = 1; | ||
PhoneType type = 2; | ||
} | ||
|
||
repeated PhoneNumber phones = 4; | ||
|
||
} | ||
|
||
// Our address book file is just one of these. | ||
message AddressBook { | ||
int32 version = 1; | ||
repeated Person people = 2; | ||
} | ||
// [END messages] |