1
1
package io .apicurio .registry .noprofile .serde ;
2
2
3
- import static io .apicurio .registry .utils .tests .TestUtils .waitForSchema ;
4
- import static org .junit .jupiter .api .Assertions .assertEquals ;
5
-
6
- import java .io .IOException ;
7
- import java .util .HashMap ;
8
- import java .util .Map ;
9
-
10
- import org .apache .kafka .common .serialization .Deserializer ;
11
- import org .apache .kafka .common .serialization .Serializer ;
12
- import org .junit .jupiter .api .Assertions ;
13
- import org .junit .jupiter .api .BeforeEach ;
14
- import org .junit .jupiter .api .Test ;
15
-
16
3
import com .google .protobuf .Descriptors ;
17
4
import com .google .protobuf .DynamicMessage ;
18
-
19
5
import io .api .sample .TableNotification ;
20
6
import io .apicurio .registry .AbstractResourceTestBase ;
21
7
import io .apicurio .registry .client .auth .VertXAuthFactory ;
8
+ import io .apicurio .registry .resolver .SchemaResolverConfig ;
22
9
import io .apicurio .registry .rest .client .RegistryClient ;
23
10
import io .apicurio .registry .rest .client .models .VersionMetaData ;
24
11
import io .apicurio .registry .serde .SerdeConfig ;
29
16
import io .apicurio .registry .utils .tests .TestUtils ;
30
17
import io .kiota .http .vertx .VertXRequestAdapter ;
31
18
import io .quarkus .test .junit .QuarkusTest ;
19
+ import org .apache .kafka .common .serialization .Deserializer ;
20
+ import org .apache .kafka .common .serialization .Serializer ;
21
+ import org .junit .jupiter .api .Assertions ;
22
+ import org .junit .jupiter .api .BeforeEach ;
23
+ import org .junit .jupiter .api .Test ;
24
+
25
+ import java .io .IOException ;
26
+ import java .util .HashMap ;
27
+ import java .util .Map ;
28
+
29
+ import static io .apicurio .registry .utils .tests .TestUtils .waitForSchema ;
30
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
32
31
33
32
@ QuarkusTest
34
33
public class ProtobufSerdeTest extends AbstractResourceTestBase {
35
34
36
35
private RegistryClient restClient ;
37
- // Isolating this test in it's own groupId:
38
- // io.apicurio.registry.rest.client.exception.ArtifactAlreadyExistsException: An artifact with ID 'google/protobuf/timestamp.proto' in group 'default' already exists.
39
36
private String groupId = "protobuf-serde-test" ;
40
37
41
38
@ BeforeEach
@@ -45,72 +42,11 @@ public void createIsolatedClient() {
45
42
restClient = new RegistryClient (adapter );
46
43
}
47
44
48
- //FIXME
49
- //test not working because of getArtifactVersionMetaDataByContent does not find the schema for somereason
50
- // @Test
51
- // public void testConfiguration() throws Exception {
52
- //
53
- // TestCmmn.UUID record = TestCmmn.UUID.newBuilder().setLsb(2).setMsb(1).build();
54
- // byte[] schema = toSchemaProto(record.getDescriptorForType().getFile()).toByteArray();
55
- //// String schema = IoUtil.toString(toSchemaProto(record));
56
- //
57
- // String groupId = TestUtils.generateGroupId();
58
- // String topic = generateArtifactId();
59
- //
60
- // createArtifact(groupId, topic, ArtifactType.PROTOBUF_FD, IoUtil.toString(schema));
61
- //
62
- // System.out.println("artifaaact " + clientV2.listArtifactsInGroup(groupId).getArtifacts().get(0).getId());
63
- //
64
- // Map<String, Object> config = new HashMap<>();
65
- // config.put(SerdeConfigKeys.REGISTRY_URL, TestUtils.getRegistryV2ApiUrl());
66
- // config.put(SerdeConfigKeys.ARTIFACT_GROUP_ID, groupId);
67
- // config.put(SerdeConfigKeys.ARTIFACT_ID_STRATEGY, new SimpleTopicIdStrategy<>());
68
- // Serializer<TestCmmn.UUID> serializer = new ProtobufKafkaSerializer<>();
69
- // serializer.configure(config, true);
70
- //
71
- // byte[] bytes = serializer.serialize(topic, record);
72
- //
73
- // Map<String, Object> deserializerConfig = new HashMap<>();
74
- // deserializerConfig.put(SerdeConfigKeys.REGISTRY_URL, TestUtils.getRegistryV2ApiUrl());
75
- // Deserializer<DynamicMessage> deserializer = new ProtobufKafkaDeserializer();
76
- // deserializer.configure(deserializerConfig, true);
77
- //
78
- // DynamicMessage deserializedRecord = deserializer.deserialize(topic, bytes);
79
- // assertProtobufEquals(record, deserializedRecord);
80
- //
81
- // config.put(SerdeConfigKeys.ARTIFACT_ID_STRATEGY, SimpleTopicIdStrategy.class);
82
- // serializer.configure(config, true);
83
- // bytes = serializer.serialize(topic, record);
84
- //
85
- // deserializer.configure(deserializerConfig, true);
86
- // deserializedRecord = deserializer.deserialize(topic, bytes);
87
- // assertProtobufEquals(record, deserializedRecord);
88
- //
89
- // config.put(SerdeConfigKeys.ARTIFACT_ID_STRATEGY, SimpleTopicIdStrategy.class.getName());
90
- // serializer.configure(config, true);
91
- // bytes = serializer.serialize(topic, record);
92
- // deserializer.configure(deserializerConfig, true);
93
- // deserializedRecord = deserializer.deserialize(topic, bytes);
94
- // assertProtobufEquals(record, deserializedRecord);
95
- //
96
- // serializer.close();
97
- // deserializer.close();
98
- // }
99
- //
100
- // private Serde.Schema toSchemaProto(Descriptors.FileDescriptor file) {
101
- // Serde.Schema.Builder b = Serde.Schema.newBuilder();
102
- // b.setFile(file.toProto());
103
- // for (Descriptors.FileDescriptor d : file.getDependencies()) {
104
- // b.addImport(toSchemaProto(d));
105
- // }
106
- // return b.build();
107
- // }
108
-
109
45
@ SuppressWarnings ({ "rawtypes" , "unchecked" })
110
46
@ Test
111
47
public void testProto () throws Exception {
112
48
try (Serializer <TestCmmn .UUID > serializer = new ProtobufKafkaSerializer <>(restClient );
113
- Deserializer <DynamicMessage > deserializer = new ProtobufKafkaDeserializer (restClient )) {
49
+ Deserializer <DynamicMessage > deserializer = new ProtobufKafkaDeserializer (restClient )) {
114
50
115
51
Map <String , Object > config = new HashMap <>();
116
52
config .put (SerdeConfig .ARTIFACT_RESOLVER_STRATEGY , SimpleTopicIdStrategy .class );
@@ -129,11 +65,13 @@ public void testProto() throws Exception {
129
65
waitForSchema (globalId -> {
130
66
try {
131
67
if (restClient .ids ().globalIds ().byGlobalId (globalId ).get ().readAllBytes ().length > 0 ) {
132
- VersionMetaData artifactMetadata = restClient .groups ().byGroupId (groupId ).artifacts ().byArtifactId (topic ).versions ().byVersionExpression ("branch=latest" ).get ();
68
+ VersionMetaData artifactMetadata = restClient .groups ().byGroupId (groupId ).artifacts ().byArtifactId (topic ).versions ()
69
+ .byVersionExpression ("branch=latest" ).get ();
133
70
assertEquals (globalId , artifactMetadata .getGlobalId ());
134
71
return true ;
135
72
}
136
- } catch (IOException e ) {
73
+ }
74
+ catch (IOException e ) {
137
75
throw new RuntimeException (e );
138
76
}
139
77
return false ;
@@ -147,9 +85,8 @@ public void testProto() throws Exception {
147
85
@ SuppressWarnings ({ "rawtypes" , "unchecked" })
148
86
@ Test
149
87
public void testProtobufSchemaWithReferences () {
150
-
151
88
try (Serializer <TableNotification > serializer = new ProtobufKafkaSerializer <>(restClient );
152
- Deserializer <TableNotification > deserializer = new ProtobufKafkaDeserializer (restClient )) {
89
+ Deserializer <TableNotification > deserializer = new ProtobufKafkaDeserializer (restClient )) {
153
90
154
91
Map <String , Object > config = new HashMap <>();
155
92
config .put (SerdeConfig .ARTIFACT_RESOLVER_STRATEGY , SimpleTopicIdStrategy .class );
@@ -159,11 +96,30 @@ public void testProtobufSchemaWithReferences() {
159
96
serializer .configure (config , false );
160
97
deserializer .configure (config , false );
161
98
162
- byte [] data = serializer .serialize ("test" , TableNotification .newBuilder ().build ());
99
+ byte [] data = serializer .serialize ("test" , TableNotification .newBuilder ().build ());
163
100
deserializer .deserialize ("test" , data );
164
101
165
102
}
103
+ }
104
+
105
+ @ SuppressWarnings ({ "rawtypes" , "unchecked" })
106
+ @ Test
107
+ public void testProtobufSchemaWithReferencesDereferenced () {
108
+ try (Serializer <TableNotification > serializer = new ProtobufKafkaSerializer <>(restClient );
109
+ Deserializer <TableNotification > deserializer = new ProtobufKafkaDeserializer (restClient )) {
166
110
111
+ Map <String , Object > config = new HashMap <>();
112
+ config .put (SerdeConfig .ARTIFACT_RESOLVER_STRATEGY , SimpleTopicIdStrategy .class );
113
+ config .put (SerdeConfig .AUTO_REGISTER_ARTIFACT , "true" );
114
+ config .put (SerdeConfig .EXPLICIT_ARTIFACT_GROUP_ID , groupId );
115
+ config .put (SerdeConfig .FALLBACK_ARTIFACT_GROUP_ID , groupId );
116
+ config .put (SchemaResolverConfig .DESERIALIZER_DEREFERENCE_SCHEMA , "true" );
117
+ serializer .configure (config , false );
118
+ deserializer .configure (config , false );
119
+
120
+ byte [] data = serializer .serialize ("test" , TableNotification .newBuilder ().build ());
121
+ deserializer .deserialize ("test" , data );
122
+ }
167
123
}
168
124
169
125
private void assertProtobufEquals (TestCmmn .UUID record , DynamicMessage dm ) {
0 commit comments