aboutsummaryrefslogtreecommitdiff
path: root/weed/mq/kafka/schema/integration_test.go
blob: 5677131c124bb08abc5357f5a7dbb63d4713b23d (plain)
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
package schema

import (
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"testing"
	"time"

	"github.com/linkedin/goavro/v2"
)

// TestFullIntegration_AvroWorkflow tests the complete Avro workflow
func TestFullIntegration_AvroWorkflow(t *testing.T) {
	// Create comprehensive mock schema registry
	server := createMockSchemaRegistry(t)
	defer server.Close()

	// Create manager with realistic configuration
	config := ManagerConfig{
		RegistryURL:     server.URL,
		ValidationMode:  ValidationPermissive,
		EnableMirroring: false,
		CacheTTL:        "5m",
	}

	manager, err := NewManager(config)
	if err != nil {
		t.Fatalf("Failed to create manager: %v", err)
	}

	// Test 1: Producer workflow - encode schematized message
	t.Run("Producer_Workflow", func(t *testing.T) {
		// Create realistic user data (with proper Avro union handling)
		userData := map[string]interface{}{
			"id":    int32(12345),
			"name":  "Alice Johnson",
			"email": map[string]interface{}{"string": "alice@example.com"}, // Avro union
			"age":   map[string]interface{}{"int": int32(28)},              // Avro union
			"preferences": map[string]interface{}{
				"Preferences": map[string]interface{}{ // Avro union with record type
					"notifications": true,
					"theme":         "dark",
				},
			},
		}

		// Create Avro message (simulate what a Kafka producer would send)
		avroSchema := getUserAvroSchema()
		codec, err := goavro.NewCodec(avroSchema)
		if err != nil {
			t.Fatalf("Failed to create Avro codec: %v", err)
		}

		avroBinary, err := codec.BinaryFromNative(nil, userData)
		if err != nil {
			t.Fatalf("Failed to encode Avro data: %v", err)
		}

		// Create Confluent envelope (what Kafka Gateway receives)
		confluentMsg := CreateConfluentEnvelope(FormatAvro, 1, nil, avroBinary)

		// Decode message (Produce path processing)
		decodedMsg, err := manager.DecodeMessage(confluentMsg)
		if err != nil {
			t.Fatalf("Failed to decode message: %v", err)
		}

		// Verify decoded data
		if decodedMsg.SchemaID != 1 {
			t.Errorf("Expected schema ID 1, got %d", decodedMsg.SchemaID)
		}

		if decodedMsg.SchemaFormat != FormatAvro {
			t.Errorf("Expected Avro format, got %v", decodedMsg.SchemaFormat)
		}

		// Verify field values
		fields := decodedMsg.RecordValue.Fields
		if fields["id"].GetInt32Value() != 12345 {
			t.Errorf("Expected id=12345, got %v", fields["id"].GetInt32Value())
		}

		if fields["name"].GetStringValue() != "Alice Johnson" {
			t.Errorf("Expected name='Alice Johnson', got %v", fields["name"].GetStringValue())
		}

		t.Logf("Successfully processed producer message with %d fields", len(fields))
	})

	// Test 2: Consumer workflow - reconstruct original message
	t.Run("Consumer_Workflow", func(t *testing.T) {
		// Create test RecordValue (simulate what's stored in SeaweedMQ)
		testData := map[string]interface{}{
			"id":    int32(67890),
			"name":  "Bob Smith",
			"email": map[string]interface{}{"string": "bob@example.com"},
			"age":   map[string]interface{}{"int": int32(35)}, // Avro union
		}
		recordValue := MapToRecordValue(testData)

		// Reconstruct message (Fetch path processing)
		reconstructedMsg, err := manager.EncodeMessage(recordValue, 1, FormatAvro)
		if err != nil {
			t.Fatalf("Failed to reconstruct message: %v", err)
		}

		// Verify reconstructed message can be parsed
		envelope, ok := ParseConfluentEnvelope(reconstructedMsg)
		if !ok {
			t.Fatal("Failed to parse reconstructed envelope")
		}

		if envelope.SchemaID != 1 {
			t.Errorf("Expected schema ID 1, got %d", envelope.SchemaID)
		}

		// Verify the payload can be decoded by Avro
		avroSchema := getUserAvroSchema()
		codec, err := goavro.NewCodec(avroSchema)
		if err != nil {
			t.Fatalf("Failed to create Avro codec: %v", err)
		}

		decodedData, _, err := codec.NativeFromBinary(envelope.Payload)
		if err != nil {
			t.Fatalf("Failed to decode reconstructed Avro data: %v", err)
		}

		// Verify data integrity
		decodedMap := decodedData.(map[string]interface{})
		if decodedMap["id"] != int32(67890) {
			t.Errorf("Expected id=67890, got %v", decodedMap["id"])
		}

		if decodedMap["name"] != "Bob Smith" {
			t.Errorf("Expected name='Bob Smith', got %v", decodedMap["name"])
		}

		t.Logf("Successfully reconstructed consumer message: %d bytes", len(reconstructedMsg))
	})

	// Test 3: Round-trip integrity
	t.Run("Round_Trip_Integrity", func(t *testing.T) {
		originalData := map[string]interface{}{
			"id":    int32(99999),
			"name":  "Charlie Brown",
			"email": map[string]interface{}{"string": "charlie@example.com"},
			"age":   map[string]interface{}{"int": int32(42)}, // Avro union
			"preferences": map[string]interface{}{
				"Preferences": map[string]interface{}{ // Avro union with record type
					"notifications": true,
					"theme":         "dark",
				},
			},
		}

		// Encode -> Decode -> Encode -> Decode
		avroSchema := getUserAvroSchema()
		codec, _ := goavro.NewCodec(avroSchema)

		// Step 1: Original -> Confluent
		avroBinary, _ := codec.BinaryFromNative(nil, originalData)
		confluentMsg := CreateConfluentEnvelope(FormatAvro, 1, nil, avroBinary)

		// Step 2: Confluent -> RecordValue
		decodedMsg, _ := manager.DecodeMessage(confluentMsg)

		// Step 3: RecordValue -> Confluent
		reconstructedMsg, encodeErr := manager.EncodeMessage(decodedMsg.RecordValue, 1, FormatAvro)
		if encodeErr != nil {
			t.Fatalf("Failed to encode message: %v", encodeErr)
		}

		// Verify the reconstructed message is valid
		if len(reconstructedMsg) == 0 {
			t.Fatal("Reconstructed message is empty")
		}

		// Step 4: Confluent -> Verify
		finalDecodedMsg, err := manager.DecodeMessage(reconstructedMsg)
		if err != nil {
			// Debug: Check if the reconstructed message is properly formatted
			envelope, ok := ParseConfluentEnvelope(reconstructedMsg)
			if !ok {
				t.Fatalf("Round-trip failed: reconstructed message is not a valid Confluent envelope")
			}
			t.Logf("Debug: Envelope SchemaID=%d, Format=%v, PayloadLen=%d",
				envelope.SchemaID, envelope.Format, len(envelope.Payload))
			t.Fatalf("Round-trip failed: %v", err)
		}

		// Verify data integrity through complete round-trip
		finalFields := finalDecodedMsg.RecordValue.Fields
		if finalFields["id"].GetInt32Value() != 99999 {
			t.Error("Round-trip failed for id field")
		}

		if finalFields["name"].GetStringValue() != "Charlie Brown" {
			t.Error("Round-trip failed for name field")
		}

		t.Log("Round-trip integrity test passed")
	})
}

// TestFullIntegration_MultiFormatSupport tests all schema formats together
func TestFullIntegration_MultiFormatSupport(t *testing.T) {
	server := createMockSchemaRegistry(t)
	defer server.Close()

	config := ManagerConfig{
		RegistryURL:    server.URL,
		ValidationMode: ValidationPermissive,
	}

	manager, err := NewManager(config)
	if err != nil {
		t.Fatalf("Failed to create manager: %v", err)
	}

	testCases := []struct {
		name     string
		format   Format
		schemaID uint32
		testData interface{}
	}{
		{
			name:     "Avro_Format",
			format:   FormatAvro,
			schemaID: 1,
			testData: map[string]interface{}{
				"id":   int32(123),
				"name": "Avro User",
			},
		},
		{
			name:     "JSON_Schema_Format",
			format:   FormatJSONSchema,
			schemaID: 3,
			testData: map[string]interface{}{
				"id":     float64(456), // JSON numbers are float64
				"name":   "JSON User",
				"active": true,
			},
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			// Create RecordValue from test data
			recordValue := MapToRecordValue(tc.testData.(map[string]interface{}))

			// Test encoding
			encoded, err := manager.EncodeMessage(recordValue, tc.schemaID, tc.format)
			if err != nil {
				if tc.format == FormatProtobuf {
					// Protobuf encoding may fail due to incomplete implementation
					t.Skipf("Protobuf encoding not fully implemented: %v", err)
				} else {
					t.Fatalf("Failed to encode %s message: %v", tc.name, err)
				}
			}

			// Test decoding
			decoded, err := manager.DecodeMessage(encoded)
			if err != nil {
				t.Fatalf("Failed to decode %s message: %v", tc.name, err)
			}

			// Verify format
			if decoded.SchemaFormat != tc.format {
				t.Errorf("Expected format %v, got %v", tc.format, decoded.SchemaFormat)
			}

			// Verify schema ID
			if decoded.SchemaID != tc.schemaID {
				t.Errorf("Expected schema ID %d, got %d", tc.schemaID, decoded.SchemaID)
			}

			t.Logf("Successfully processed %s format", tc.name)
		})
	}
}

// TestIntegration_CachePerformance tests caching behavior under load
func TestIntegration_CachePerformance(t *testing.T) {
	server := createMockSchemaRegistry(t)
	defer server.Close()

	config := ManagerConfig{
		RegistryURL:    server.URL,
		ValidationMode: ValidationPermissive,
	}

	manager, err := NewManager(config)
	if err != nil {
		t.Fatalf("Failed to create manager: %v", err)
	}

	// Create test message
	testData := map[string]interface{}{
		"id":   int32(1),
		"name": "Cache Test",
	}

	avroSchema := getUserAvroSchema()
	codec, _ := goavro.NewCodec(avroSchema)
	avroBinary, _ := codec.BinaryFromNative(nil, testData)
	testMsg := CreateConfluentEnvelope(FormatAvro, 1, nil, avroBinary)

	// First decode (should hit registry)
	start := time.Now()
	_, err = manager.DecodeMessage(testMsg)
	if err != nil {
		t.Fatalf("First decode failed: %v", err)
	}
	firstDuration := time.Since(start)

	// Subsequent decodes (should hit cache)
	start = time.Now()
	for i := 0; i < 100; i++ {
		_, err = manager.DecodeMessage(testMsg)
		if err != nil {
			t.Fatalf("Cached decode failed: %v", err)
		}
	}
	cachedDuration := time.Since(start)

	// Verify cache performance improvement
	avgCachedTime := cachedDuration / 100
	if avgCachedTime >= firstDuration {
		t.Logf("Warning: Cache may not be effective. First: %v, Avg Cached: %v",
			firstDuration, avgCachedTime)
	}

	// Check cache stats
	decoders, schemas, subjects := manager.GetCacheStats()
	if decoders == 0 || schemas == 0 {
		t.Error("Expected non-zero cache stats")
	}

	t.Logf("Cache performance: First decode: %v, Average cached: %v",
		firstDuration, avgCachedTime)
	t.Logf("Cache stats: %d decoders, %d schemas, %d subjects",
		decoders, schemas, subjects)
}

// TestIntegration_ErrorHandling tests error scenarios
func TestIntegration_ErrorHandling(t *testing.T) {
	server := createMockSchemaRegistry(t)
	defer server.Close()

	config := ManagerConfig{
		RegistryURL:    server.URL,
		ValidationMode: ValidationStrict,
	}

	manager, err := NewManager(config)
	if err != nil {
		t.Fatalf("Failed to create manager: %v", err)
	}

	testCases := []struct {
		name        string
		message     []byte
		expectError bool
		errorType   string
	}{
		{
			name:        "Non_Schematized_Message",
			message:     []byte("plain text message"),
			expectError: true,
			errorType:   "not schematized",
		},
		{
			name:        "Invalid_Schema_ID",
			message:     CreateConfluentEnvelope(FormatAvro, 999, nil, []byte("payload")),
			expectError: true,
			errorType:   "schema not found",
		},
		{
			name:        "Empty_Payload",
			message:     CreateConfluentEnvelope(FormatAvro, 1, nil, []byte{}),
			expectError: true,
			errorType:   "empty payload",
		},
		{
			name:        "Corrupted_Avro_Data",
			message:     CreateConfluentEnvelope(FormatAvro, 1, nil, []byte("invalid avro")),
			expectError: true,
			errorType:   "decode failed",
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			_, err := manager.DecodeMessage(tc.message)

			if (err != nil) != tc.expectError {
				t.Errorf("Expected error: %v, got error: %v", tc.expectError, err != nil)
			}

			if tc.expectError && err != nil {
				t.Logf("Expected error occurred: %v", err)
			}
		})
	}
}

// TestIntegration_SchemaEvolution tests schema evolution scenarios
func TestIntegration_SchemaEvolution(t *testing.T) {
	server := createMockSchemaRegistryWithEvolution(t)
	defer server.Close()

	config := ManagerConfig{
		RegistryURL:    server.URL,
		ValidationMode: ValidationPermissive,
	}

	manager, err := NewManager(config)
	if err != nil {
		t.Fatalf("Failed to create manager: %v", err)
	}

	// Test decoding messages with different schema versions
	t.Run("Schema_V1_Message", func(t *testing.T) {
		// Create message with schema v1 (basic user)
		userData := map[string]interface{}{
			"id":   int32(1),
			"name": "User V1",
		}

		avroSchema := getUserAvroSchemaV1()
		codec, _ := goavro.NewCodec(avroSchema)
		avroBinary, _ := codec.BinaryFromNative(nil, userData)
		msg := CreateConfluentEnvelope(FormatAvro, 1, nil, avroBinary)

		decoded, err := manager.DecodeMessage(msg)
		if err != nil {
			t.Fatalf("Failed to decode v1 message: %v", err)
		}

		if decoded.Version != 1 {
			t.Errorf("Expected version 1, got %d", decoded.Version)
		}
	})

	t.Run("Schema_V2_Message", func(t *testing.T) {
		// Create message with schema v2 (user with email)
		userData := map[string]interface{}{
			"id":    int32(2),
			"name":  "User V2",
			"email": map[string]interface{}{"string": "user@example.com"},
		}

		avroSchema := getUserAvroSchemaV2()
		codec, _ := goavro.NewCodec(avroSchema)
		avroBinary, _ := codec.BinaryFromNative(nil, userData)
		msg := CreateConfluentEnvelope(FormatAvro, 2, nil, avroBinary)

		decoded, err := manager.DecodeMessage(msg)
		if err != nil {
			t.Fatalf("Failed to decode v2 message: %v", err)
		}

		if decoded.Version != 2 {
			t.Errorf("Expected version 2, got %d", decoded.Version)
		}
	})
}

// Helper functions for creating mock schema registries

func createMockSchemaRegistry(t *testing.T) *httptest.Server {
	return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		switch r.URL.Path {
		case "/subjects":
			// List subjects
			subjects := []string{"user-value", "product-value", "order-value"}
			json.NewEncoder(w).Encode(subjects)

		case "/schemas/ids/1":
			// Avro user schema
			response := map[string]interface{}{
				"schema":  getUserAvroSchema(),
				"subject": "user-value",
				"version": 1,
			}
			json.NewEncoder(w).Encode(response)

		case "/schemas/ids/2":
			// Protobuf schema (simplified)
			response := map[string]interface{}{
				"schema":  "syntax = \"proto3\"; message User { int32 id = 1; string name = 2; }",
				"subject": "user-value",
				"version": 2,
			}
			json.NewEncoder(w).Encode(response)

		case "/schemas/ids/3":
			// JSON Schema
			response := map[string]interface{}{
				"schema":  getUserJSONSchema(),
				"subject": "user-value",
				"version": 3,
			}
			json.NewEncoder(w).Encode(response)

		default:
			w.WriteHeader(http.StatusNotFound)
		}
	}))
}

func createMockSchemaRegistryWithEvolution(t *testing.T) *httptest.Server {
	return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		switch r.URL.Path {
		case "/schemas/ids/1":
			// Schema v1
			response := map[string]interface{}{
				"schema":  getUserAvroSchemaV1(),
				"subject": "user-value",
				"version": 1,
			}
			json.NewEncoder(w).Encode(response)

		case "/schemas/ids/2":
			// Schema v2 (evolved)
			response := map[string]interface{}{
				"schema":  getUserAvroSchemaV2(),
				"subject": "user-value",
				"version": 2,
			}
			json.NewEncoder(w).Encode(response)

		default:
			w.WriteHeader(http.StatusNotFound)
		}
	}))
}

// Schema definitions for testing

func getUserAvroSchema() string {
	return `{
		"type": "record",
		"name": "User",
		"fields": [
			{"name": "id", "type": "int"},
			{"name": "name", "type": "string"},
			{"name": "email", "type": ["null", "string"], "default": null},
			{"name": "age", "type": ["null", "int"], "default": null},
			{"name": "preferences", "type": ["null", {
				"type": "record",
				"name": "Preferences",
				"fields": [
					{"name": "notifications", "type": "boolean", "default": true},
					{"name": "theme", "type": "string", "default": "light"}
				]
			}], "default": null}
		]
	}`
}

func getUserAvroSchemaV1() string {
	return `{
		"type": "record",
		"name": "User",
		"fields": [
			{"name": "id", "type": "int"},
			{"name": "name", "type": "string"}
		]
	}`
}

func getUserAvroSchemaV2() string {
	return `{
		"type": "record",
		"name": "User",
		"fields": [
			{"name": "id", "type": "int"},
			{"name": "name", "type": "string"},
			{"name": "email", "type": ["null", "string"], "default": null}
		]
	}`
}

func getUserJSONSchema() string {
	return `{
		"$schema": "http://json-schema.org/draft-07/schema#",
		"type": "object",
		"properties": {
			"id": {"type": "integer"},
			"name": {"type": "string"},
			"active": {"type": "boolean"}
		},
		"required": ["id", "name"]
	}`
}

// Benchmark tests for integration scenarios

func BenchmarkIntegration_AvroDecoding(b *testing.B) {
	server := createMockSchemaRegistry(nil)
	defer server.Close()

	config := ManagerConfig{RegistryURL: server.URL}
	manager, _ := NewManager(config)

	// Create test message
	testData := map[string]interface{}{
		"id":   int32(1),
		"name": "Benchmark User",
	}

	avroSchema := getUserAvroSchema()
	codec, _ := goavro.NewCodec(avroSchema)
	avroBinary, _ := codec.BinaryFromNative(nil, testData)
	testMsg := CreateConfluentEnvelope(FormatAvro, 1, nil, avroBinary)

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_, _ = manager.DecodeMessage(testMsg)
	}
}

func BenchmarkIntegration_JSONSchemaDecoding(b *testing.B) {
	server := createMockSchemaRegistry(nil)
	defer server.Close()

	config := ManagerConfig{RegistryURL: server.URL}
	manager, _ := NewManager(config)

	// Create test message
	jsonData := []byte(`{"id": 1, "name": "Benchmark User", "active": true}`)
	testMsg := CreateConfluentEnvelope(FormatJSONSchema, 3, nil, jsonData)

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_, _ = manager.DecodeMessage(testMsg)
	}
}