1
+ package io .kurrent .dbclient .v2 ;
2
+
3
+ import org .junit .jupiter .api .Assertions ;
4
+ import org .junit .jupiter .api .Test ;
5
+
6
+ public class SchemaInfoTests {
7
+
8
+ @ Test
9
+ public void testNoneSchemaInfo () {
10
+ SchemaInfo none = SchemaInfo .NONE ;
11
+
12
+ Assertions .assertEquals ("" , none .getSchemaName ());
13
+ Assertions .assertEquals (SchemaDataFormat .UNSPECIFIED , none .getDataFormat ());
14
+ Assertions .assertEquals ("application/octet-stream" , none .getContentType ());
15
+ Assertions .assertTrue (none .isSchemaNameMissing ());
16
+ }
17
+
18
+ @ Test
19
+ public void testSchemaInfoConstructor () {
20
+ SchemaInfo jsonSchema = new SchemaInfo ("test-schema" , SchemaDataFormat .JSON );
21
+
22
+ Assertions .assertEquals ("test-schema" , jsonSchema .getSchemaName ());
23
+ Assertions .assertEquals (SchemaDataFormat .JSON , jsonSchema .getDataFormat ());
24
+ Assertions .assertEquals ("application/json" , jsonSchema .getContentType ());
25
+ Assertions .assertFalse (jsonSchema .isSchemaNameMissing ());
26
+
27
+ SchemaInfo protobufSchema = new SchemaInfo ("proto-schema" , SchemaDataFormat .PROTOBUF );
28
+
29
+ Assertions .assertEquals ("proto-schema" , protobufSchema .getSchemaName ());
30
+ Assertions .assertEquals (SchemaDataFormat .PROTOBUF , protobufSchema .getDataFormat ());
31
+ Assertions .assertEquals ("application/vnd.google.protobuf" , protobufSchema .getContentType ());
32
+ Assertions .assertFalse (protobufSchema .isSchemaNameMissing ());
33
+
34
+ SchemaInfo avroSchema = new SchemaInfo ("avro-schema" , SchemaDataFormat .AVRO );
35
+
36
+ Assertions .assertEquals ("avro-schema" , avroSchema .getSchemaName ());
37
+ Assertions .assertEquals (SchemaDataFormat .AVRO , avroSchema .getDataFormat ());
38
+ Assertions .assertEquals ("application/vnd.apache.avro+json" , avroSchema .getContentType ());
39
+ Assertions .assertFalse (avroSchema .isSchemaNameMissing ());
40
+
41
+ SchemaInfo bytesSchema = new SchemaInfo ("bytes-schema" , SchemaDataFormat .BYTES );
42
+
43
+ Assertions .assertEquals ("bytes-schema" , bytesSchema .getSchemaName ());
44
+ Assertions .assertEquals (SchemaDataFormat .BYTES , bytesSchema .getDataFormat ());
45
+ Assertions .assertEquals ("application/octet-stream" , bytesSchema .getContentType ());
46
+ Assertions .assertFalse (bytesSchema .isSchemaNameMissing ());
47
+ }
48
+
49
+ @ Test
50
+ public void testSchemaNameMissing () {
51
+ SchemaInfo emptyName = new SchemaInfo ("" , SchemaDataFormat .JSON );
52
+ Assertions .assertTrue (emptyName .isSchemaNameMissing ());
53
+
54
+ SchemaInfo whitespace = new SchemaInfo (" " , SchemaDataFormat .JSON );
55
+ Assertions .assertTrue (whitespace .isSchemaNameMissing ());
56
+
57
+ SchemaInfo nullName = new SchemaInfo (null , SchemaDataFormat .JSON );
58
+ Assertions .assertTrue (nullName .isSchemaNameMissing ());
59
+
60
+ SchemaInfo validName = new SchemaInfo ("valid-name" , SchemaDataFormat .JSON );
61
+ Assertions .assertFalse (validName .isSchemaNameMissing ());
62
+ }
63
+
64
+ @ Test
65
+ public void testInjectIntoMetadata () {
66
+ SchemaInfo schema = new SchemaInfo ("test-schema" , SchemaDataFormat .JSON );
67
+ Metadata metadata = new Metadata ();
68
+
69
+ schema .injectIntoMetadata (metadata );
70
+
71
+ Assertions .assertEquals (2 , metadata .size ());
72
+ Assertions .assertEquals ("test-schema" , metadata .get ("schema-name" , String .class ));
73
+ Assertions .assertEquals ("json" , metadata .get ("schema-data-format" , String .class ));
74
+ }
75
+
76
+ @ Test
77
+ public void testInjectSchemaNameIntoMetadata () {
78
+ SchemaInfo schema = new SchemaInfo ("test-schema" , SchemaDataFormat .JSON );
79
+ Metadata metadata = new Metadata ();
80
+
81
+ schema .injectSchemaNameIntoMetadata (metadata );
82
+
83
+ Assertions .assertEquals (1 , metadata .size ());
84
+ Assertions .assertEquals ("test-schema" , metadata .get ("schema-name" , String .class ));
85
+ Assertions .assertNull (metadata .get ("schema-data-format" , String .class ));
86
+ }
87
+
88
+ @ Test
89
+ public void testFromMetadata () {
90
+ Metadata metadata = new Metadata ();
91
+ metadata .set ("schema-name" , "test-schema" );
92
+ metadata .set ("schema-data-format" , "json" );
93
+
94
+ SchemaInfo schema = SchemaInfo .fromMetadata (metadata );
95
+
96
+ Assertions .assertEquals ("test-schema" , schema .getSchemaName ());
97
+ Assertions .assertEquals (SchemaDataFormat .JSON , schema .getDataFormat ());
98
+ Assertions .assertEquals ("application/json" , schema .getContentType ());
99
+ }
100
+
101
+ @ Test
102
+ public void testFromMetadataWithMissingValues () {
103
+ Metadata emptyMetadata = new Metadata ();
104
+ SchemaInfo schema = SchemaInfo .fromMetadata (emptyMetadata );
105
+
106
+ Assertions .assertEquals ("" , schema .getSchemaName ());
107
+ Assertions .assertEquals (SchemaDataFormat .UNSPECIFIED , schema .getDataFormat ());
108
+
109
+ Metadata nameOnlyMetadata = new Metadata ();
110
+ nameOnlyMetadata .set ("schema-name" , "test-schema" );
111
+
112
+ SchemaInfo nameOnlySchema = SchemaInfo .fromMetadata (nameOnlyMetadata );
113
+
114
+ Assertions .assertEquals ("test-schema" , nameOnlySchema .getSchemaName ());
115
+ Assertions .assertEquals (SchemaDataFormat .UNSPECIFIED , nameOnlySchema .getDataFormat ());
116
+
117
+ Metadata formatOnlyMetadata = new Metadata ();
118
+ formatOnlyMetadata .set ("schema-data-format" , "protobuf" );
119
+
120
+ SchemaInfo formatOnlySchema = SchemaInfo .fromMetadata (formatOnlyMetadata );
121
+
122
+ Assertions .assertEquals ("" , formatOnlySchema .getSchemaName ());
123
+ Assertions .assertEquals (SchemaDataFormat .PROTOBUF , formatOnlySchema .getDataFormat ());
124
+ }
125
+
126
+ @ Test
127
+ public void testFromContentType () {
128
+ SchemaInfo jsonSchema = SchemaInfo .fromContentType ("test-schema" , "application/json" );
129
+
130
+ Assertions .assertEquals ("test-schema" , jsonSchema .getSchemaName ());
131
+ Assertions .assertEquals (SchemaDataFormat .JSON , jsonSchema .getDataFormat ());
132
+
133
+ SchemaInfo protobufSchema = SchemaInfo .fromContentType ("proto-schema" , "application/vnd.google.protobuf" );
134
+
135
+ Assertions .assertEquals ("proto-schema" , protobufSchema .getSchemaName ());
136
+ Assertions .assertEquals (SchemaDataFormat .PROTOBUF , protobufSchema .getDataFormat ());
137
+
138
+ SchemaInfo bytesSchema = SchemaInfo .fromContentType ("bytes-schema" , "application/octet-stream" );
139
+
140
+ Assertions .assertEquals ("bytes-schema" , bytesSchema .getSchemaName ());
141
+ Assertions .assertEquals (SchemaDataFormat .BYTES , bytesSchema .getDataFormat ());
142
+
143
+ SchemaInfo unknownSchema = SchemaInfo .fromContentType ("unknown-schema" , "unknown/content-type" );
144
+
145
+ Assertions .assertEquals ("unknown-schema" , unknownSchema .getSchemaName ());
146
+ Assertions .assertEquals (SchemaDataFormat .UNSPECIFIED , unknownSchema .getDataFormat ());
147
+ }
148
+
149
+ @ Test
150
+ public void testFromContentTypeWithInvalidArguments () {
151
+ Assertions .assertThrows (IllegalArgumentException .class , () -> {
152
+ SchemaInfo .fromContentType (null , "application/json" );
153
+ });
154
+
155
+ Assertions .assertThrows (IllegalArgumentException .class , () -> {
156
+ SchemaInfo .fromContentType ("" , "application/json" );
157
+ });
158
+
159
+ Assertions .assertThrows (IllegalArgumentException .class , () -> {
160
+ SchemaInfo .fromContentType ("test-schema" , null );
161
+ });
162
+
163
+ Assertions .assertThrows (IllegalArgumentException .class , () -> {
164
+ SchemaInfo .fromContentType ("test-schema" , "" );
165
+ });
166
+ }
167
+ }
0 commit comments