forked from kafkajs/confluent-schema-registry
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SchemaRegistry.avro.spec.ts
558 lines (473 loc) · 16.3 KB
/
SchemaRegistry.avro.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
import SchemaRegistry, { RegisteredSchema } from './SchemaRegistry'
import API from './api'
import { AvroConfluentSchema, SchemaType } from './@types'
import avro from 'avsc'
const REGISTRY_HOST = 'http://localhost:8982'
const schemaRegistryAPIClientArgs = { host: REGISTRY_HOST }
const schemaRegistryArgs = { host: REGISTRY_HOST }
enum Color {
RED = 1,
GREEN = 2,
BLUE = 3,
}
enum Direction {
UP = 1,
DOWN = 2,
}
const TestSchemas = {
FirstLevelSchema: {
type: SchemaType.AVRO,
schema: `
{
"type" : "record",
"namespace" : "test",
"name" : "FirstLevel",
"fields" : [
{ "name" : "id1" , "type" : "int" },
{ "name" : "level1a" , "type" : "test.SecondLevelA" },
{ "name" : "level1b" , "type" : "test.SecondLevelB" }
]
}`,
references: [
{
name: 'test.SecondLevelA',
subject: 'Avro:SecondLevelA',
version: undefined,
},
{
name: 'test.SecondLevelB',
subject: 'Avro:SecondLevelB',
version: undefined,
},
],
} as AvroConfluentSchema,
SecondLevelASchema: {
type: SchemaType.AVRO,
schema: `
{
"type" : "record",
"namespace" : "test",
"name" : "SecondLevelA",
"fields" : [
{ "name" : "id2a" , "type" : "int" },
{ "name" : "level2a" , "type" : "test.ThirdLevel" }
]
}`,
references: [
{
name: 'test.ThirdLevel',
subject: 'Avro:ThirdLevel',
version: undefined,
},
],
} as AvroConfluentSchema,
SecondLevelBSchema: {
type: SchemaType.AVRO,
schema: `
{
"type" : "record",
"namespace" : "test",
"name" : "SecondLevelB",
"fields" : [
{ "name" : "id2b" , "type" : "int" },
{ "name" : "level2b" , "type" : "test.ThirdLevel" }
]
}`,
references: [
{
name: 'test.ThirdLevel',
subject: 'Avro:ThirdLevel',
version: undefined,
},
],
} as AvroConfluentSchema,
ThirdLevelSchema: {
type: SchemaType.AVRO,
schema: `
{
"type" : "record",
"namespace" : "test",
"name" : "ThirdLevel",
"fields" : [
{ "name" : "id3" , "type" : "int" }
]
}`,
} as AvroConfluentSchema,
EnumSchema: {
type: SchemaType.AVRO,
schema: `
{
"type" : "record",
"namespace" : "test",
"name" : "EnumSchema",
"fields" : [
{
"name": "color",
"type": ["null", {
"type": "enum",
"name": "Color",
"symbols": ["RED", "GREEN", "BLUE"]
}
]
}
]
}`,
} as AvroConfluentSchema,
EnumWithReferencesSchema: {
type: SchemaType.AVRO,
schema: `
{
"type" : "record",
"namespace" : "test",
"name" : "EnumWithReferences",
"fields" : [
{
"name": "direction",
"type": ["null", {
"type": "enum",
"name": "Direction",
"symbols": ["UP", "DOWN"]
}
]
},
{ "name" : "attributes" , "type" : "test.EnumSchema" }
]
}`,
references: [
{
name: 'test.EnumSchema',
subject: 'Avro:EnumSchema',
version: undefined,
},
],
} as AvroConfluentSchema,
}
function apiResponse(result) {
return JSON.parse(result.responseData)
}
describe('SchemaRegistry', () => {
let schemaRegistry: SchemaRegistry
let registeredSchema: RegisteredSchema
let api
beforeEach(async () => {
api = API(schemaRegistryAPIClientArgs)
schemaRegistry = new SchemaRegistry(schemaRegistryArgs)
})
describe('when register', () => {
describe('when no reference', () => {
beforeEach(async () => {
registeredSchema = await schemaRegistry.register(TestSchemas.ThirdLevelSchema, {
subject: 'Avro:ThirdLevel',
})
})
it('should return schema id', async () => {
expect(registeredSchema.id).toEqual(expect.any(Number))
})
it('should be able to encode/decode', async () => {
const obj = { id3: 3 }
const buffer = await schemaRegistry.encode(registeredSchema.id, obj)
const resultObj = await schemaRegistry.decode(buffer)
expect(resultObj).toEqual(obj)
})
})
describe('with reference', () => {
let schemaId
let referenceSchema
beforeEach(async () => {
await schemaRegistry.register(TestSchemas.ThirdLevelSchema, {
subject: 'Avro:ThirdLevel',
})
const latest = apiResponse(await api.Subject.latestVersion({ subject: 'Avro:ThirdLevel' }))
TestSchemas.SecondLevelASchema.references[0].version = latest.version
registeredSchema = await schemaRegistry.register(TestSchemas.SecondLevelASchema, {
subject: 'Avro:SecondLevelA',
})
schemaId = registeredSchema.id
const schemaRaw = apiResponse(await api.Schema.find({ id: schemaId }))
referenceSchema = schemaRaw.references[0].subject
})
it('should return schema id', async () => {
expect(schemaId).toEqual(expect.any(Number))
})
it('should create a schema with reference', async () => {
expect(referenceSchema).toEqual('Avro:ThirdLevel')
})
it('should be able to encode/decode', async () => {
const obj = { id2a: 2, level2a: { id3: 3 } }
const buffer = await schemaRegistry.encode(registeredSchema.id, obj)
const resultObj = await schemaRegistry.decode(buffer)
expect(resultObj).toEqual(obj)
})
})
describe('with multiple reference', () => {
beforeEach(async () => {
let latest
registeredSchema = await schemaRegistry.register(TestSchemas.ThirdLevelSchema, {
subject: 'Avro:ThirdLevel',
})
latest = apiResponse(await api.Subject.latestVersion({ subject: 'Avro:ThirdLevel' }))
TestSchemas.SecondLevelASchema.references[0].version = latest.version
registeredSchema = await schemaRegistry.register(TestSchemas.SecondLevelASchema, {
subject: 'Avro:SecondLevelA',
})
latest = apiResponse(await api.Subject.latestVersion({ subject: 'Avro:ThirdLevel' }))
TestSchemas.SecondLevelBSchema.references[0].version = latest.version
registeredSchema = await schemaRegistry.register(TestSchemas.SecondLevelBSchema, {
subject: 'Avro:SecondLevelB',
})
latest = apiResponse(await api.Subject.latestVersion({ subject: 'Avro:SecondLevelA' }))
TestSchemas.FirstLevelSchema.references[0].version = latest.version
latest = apiResponse(await api.Subject.latestVersion({ subject: 'Avro:SecondLevelB' }))
TestSchemas.FirstLevelSchema.references[1].version = latest.version
registeredSchema = await schemaRegistry.register(TestSchemas.FirstLevelSchema, {
subject: 'Avro:FirstLevel',
})
})
it('should be able to encode/decode', async () => {
const obj = {
id1: 1,
level1a: { id2a: 2, level2a: { id3: 3 } },
level1b: { id2b: 4, level2b: { id3: 5 } },
}
const buffer = await schemaRegistry.encode(registeredSchema.id, obj)
const resultObj = await schemaRegistry.decode(buffer)
expect(resultObj).toEqual(obj)
})
it('should be able to encode/decode independent', async () => {
const obj = {
id1: 1,
level1a: { id2a: 2, level2a: { id3: 3 } },
level1b: { id2b: 4, level2b: { id3: 5 } },
}
schemaRegistry = new SchemaRegistry(schemaRegistryArgs)
const buffer = await schemaRegistry.encode(registeredSchema.id, obj)
schemaRegistry = new SchemaRegistry(schemaRegistryArgs)
const resultObj = await schemaRegistry.decode(buffer)
expect(resultObj).toEqual(obj)
})
})
})
describe('_getSchema', () => {
let schema
describe('no references', () => {
beforeEach(async () => {
registeredSchema = await schemaRegistry.register(TestSchemas.ThirdLevelSchema, {
subject: 'Avro:ThirdLevel',
})
;({ schema } = await schemaRegistry['_getSchema'](registeredSchema.id))
})
it('should return schema that match name', async () => {
expect(schema.name).toEqual('test.ThirdLevel')
})
it('should be able to encode/decode', async () => {
const obj = { id3: 3 }
const buffer = await schema.toBuffer(obj)
const resultObj = await schema.fromBuffer(buffer)
expect(resultObj).toEqual(obj)
})
})
describe('with references', () => {
beforeEach(async () => {
await schemaRegistry.register(TestSchemas.ThirdLevelSchema, { subject: 'Avro:ThirdLevel' })
const latest = apiResponse(await api.Subject.latestVersion({ subject: 'Avro:ThirdLevel' }))
TestSchemas.SecondLevelASchema.references[0].version = latest.version
registeredSchema = await schemaRegistry.register(TestSchemas.SecondLevelASchema, {
subject: 'Avro:SecondLevelA',
})
;({ schema } = await schemaRegistry['_getSchema'](registeredSchema.id))
})
it('should return schema that match name', async () => {
expect(schema.name).toEqual('test.SecondLevelA')
})
it('should be able to encode/decode', async () => {
const obj = { id2a: 2, level2a: { id3: 3 } }
const buffer = await schema.toBuffer(obj)
const resultObj = await schema.fromBuffer(buffer)
expect(resultObj).toEqual(obj)
})
})
describe('with multi references', () => {
beforeEach(async () => {
let latest
await schemaRegistry.register(TestSchemas.ThirdLevelSchema, {
subject: 'Avro:ThirdLevel',
})
latest = apiResponse(await api.Subject.latestVersion({ subject: 'Avro:ThirdLevel' }))
TestSchemas.SecondLevelASchema.references[0].version = latest.version
registeredSchema = await schemaRegistry.register(TestSchemas.SecondLevelASchema, {
subject: 'Avro:SecondLevelA',
})
latest = apiResponse(await api.Subject.latestVersion({ subject: 'Avro:ThirdLevel' }))
TestSchemas.SecondLevelBSchema.references[0].version = latest.version
registeredSchema = await schemaRegistry.register(TestSchemas.SecondLevelBSchema, {
subject: 'Avro:SecondLevelB',
})
latest = apiResponse(await api.Subject.latestVersion({ subject: 'Avro:SecondLevelA' }))
TestSchemas.FirstLevelSchema.references[0].version = latest.version
latest = apiResponse(await api.Subject.latestVersion({ subject: 'Avro:SecondLevelB' }))
TestSchemas.FirstLevelSchema.references[1].version = latest.version
registeredSchema = await schemaRegistry.register(TestSchemas.FirstLevelSchema, {
subject: 'Avro:FirstLevel',
})
;({ schema } = await schemaRegistry['_getSchema'](registeredSchema.id))
})
it('should return schema that match name', async () => {
expect(schema.name).toEqual('test.FirstLevel')
})
it('should be able to encode/decode', async () => {
const obj = {
id1: 1,
level1a: { id2a: 2, level2a: { id3: 3 } },
level1b: { id2b: 4, level2b: { id3: 5 } },
}
const buffer = await schema.toBuffer(obj)
const resultObj = await schema.fromBuffer(buffer)
expect(resultObj).toEqual(obj)
})
})
})
describe('when document example', () => {
it('should encode/decode', async () => {
const schemaA = {
type: 'record',
namespace: 'test',
name: 'A',
fields: [
{ name: 'id', type: 'int' },
{ name: 'b', type: 'test.B' },
],
}
const schemaB = {
type: 'record',
namespace: 'test',
name: 'B',
fields: [{ name: 'id', type: 'int' }],
}
await schemaRegistry.register(
{ type: SchemaType.AVRO, schema: JSON.stringify(schemaB) },
{ subject: 'Avro:B' },
)
const response = await schemaRegistry.api.Subject.latestVersion({ subject: 'Avro:B' })
const { version } = JSON.parse(response.responseData)
const { id } = await schemaRegistry.register(
{
type: SchemaType.AVRO,
schema: JSON.stringify(schemaA),
references: [
{
name: 'test.B',
subject: 'Avro:B',
version,
},
],
},
{ subject: 'Avro:A' },
)
const obj = { id: 1, b: { id: 2 } }
const buffer = await schemaRegistry.encode(id, obj)
const decodedObj = await schemaRegistry.decode(buffer)
expect(decodedObj).toEqual(obj)
})
})
describe('with EnumType types and nested schemas', () => {
/**
* Hook which will decode/encode enums to/from integers.
*
* The default `EnumType` implementation represents enum values as strings
* (consistent with the JSON representation). This hook can be used to provide
* an alternate representation (which is for example compatible with TypeScript
* enums).
*
* For simplicity, we don't do any bound checking here but we could by
* implementing a "bounded long" logical type and returning that instead.
*
* https://gist.github.com/mtth/c0088c745de048c4e466#file-long-enum-js
*/
function typeHook(attrs, opts) {
if (attrs.type === 'enum') {
return avro.parse('long', opts)
}
}
let schema
describe('with no enum typeHook defined', () => {
beforeEach(async () => {
const schemaRegistry = new SchemaRegistry(schemaRegistryArgs)
await schemaRegistry.register(TestSchemas.EnumSchema, {
subject: 'Avro:EnumSchema',
})
const latest = apiResponse(await api.Subject.latestVersion({ subject: 'Avro:EnumSchema' }))
TestSchemas.EnumWithReferencesSchema.references[0].version = latest.version
const registeredSchema = await schemaRegistry.register(
TestSchemas.EnumWithReferencesSchema,
{
subject: 'Avro:EnumWithReferences',
},
)
;({ schema } = await schemaRegistry['_getSchema'](registeredSchema.id))
})
it('should not be able to encode/decode enums schemas', async () => {
const obj = {
direction: Direction.UP,
attributes: { color: Color.BLUE },
}
expect(() => schema.toBuffer(obj)).toThrow(Error)
})
})
describe('with enum typeHook defined', () => {
beforeEach(async () => {
const schemaRegistry = new SchemaRegistry(schemaRegistryArgs, {
[SchemaType.AVRO]: { typeHook },
})
await schemaRegistry.register(TestSchemas.EnumSchema, {
subject: 'Avro:EnumSchema',
})
const latest = apiResponse(await api.Subject.latestVersion({ subject: 'Avro:EnumSchema' }))
TestSchemas.EnumWithReferencesSchema.references[0].version = latest.version
const registeredSchema = await schemaRegistry.register(
TestSchemas.EnumWithReferencesSchema,
{
subject: 'Avro:EnumWithReferences',
},
)
;({ schema } = await schemaRegistry['_getSchema'](registeredSchema.id))
})
it('should be able to encode/decode enums schemas', async () => {
const obj = {
direction: Direction.UP,
attributes: { color: Color.BLUE },
}
const buffer = await schema.toBuffer(obj)
const resultObj = await schema.fromBuffer(buffer)
expect(resultObj).toEqual(obj)
})
})
describe('with enum typeHook defined as LegacyOptions', () => {
beforeEach(async () => {
const schemaRegistry = new SchemaRegistry(schemaRegistryArgs, {
forSchemaOptions: { typeHook },
})
await schemaRegistry.register(TestSchemas.EnumSchema, {
subject: 'Avro:EnumSchema',
})
const latest = apiResponse(await api.Subject.latestVersion({ subject: 'Avro:EnumSchema' }))
TestSchemas.EnumWithReferencesSchema.references[0].version = latest.version
const registeredSchema = await schemaRegistry.register(
TestSchemas.EnumWithReferencesSchema,
{
subject: 'Avro:EnumWithReferences',
},
)
;({ schema } = await schemaRegistry['_getSchema'](registeredSchema.id))
})
it('should be able to encode/decode enums schemas', async () => {
const obj = {
direction: Direction.UP,
attributes: { color: Color.BLUE },
}
const buffer = await schema.toBuffer(obj)
const resultObj = await schema.fromBuffer(buffer)
expect(resultObj).toEqual(obj)
})
})
})
})