|
12 | 12 | from aws_lambda_powertools.utilities.kafka.kafka_consumer import kafka_consumer |
13 | 13 | from aws_lambda_powertools.utilities.kafka.schema_config import SchemaConfig |
14 | 14 |
|
| 15 | +# Import confluent complex schema |
| 16 | +from .confluent_protobuf_pb2 import ProtobufProduct |
| 17 | + |
15 | 18 | # Import the generated protobuf classes |
16 | 19 | from .user_pb2 import Key, User |
17 | 20 |
|
@@ -335,3 +338,87 @@ def test_kafka_consumer_without_protobuf_key_schema(): |
335 | 338 | # Verify the error message mentions the missing key schema |
336 | 339 | assert "key_schema" in str(excinfo.value) |
337 | 340 | assert "PROTOBUF" in str(excinfo.value) |
| 341 | + |
| 342 | + |
| 343 | +def test_confluent_complex_schema(lambda_context): |
| 344 | + # GIVEN |
| 345 | + # A scenario where a complex schema is used with the PROTOBUF schema type |
| 346 | + complex_event = { |
| 347 | + "eventSource": "aws:kafka", |
| 348 | + "eventSourceArn": "arn:aws:kafka:us-east-1:0123456789019:cluster/SalesCluster/abcd1234", |
| 349 | + "bootstrapServers": "b-2.demo-cluster-1.a1bcde.c1.kafka.us-east-1.amazonaws.com:9092", |
| 350 | + "records": { |
| 351 | + "mytopic-0": [ |
| 352 | + { |
| 353 | + "topic": "mytopic", |
| 354 | + "partition": 0, |
| 355 | + "offset": 15, |
| 356 | + "timestamp": 1545084650987, |
| 357 | + "timestampType": "CREATE_TIME", |
| 358 | + "key": "NDI=", |
| 359 | + "value": "COkHEgZMYXB0b3AZUrgehes/j0A=", |
| 360 | + "headers": [{"headerKey": [104, 101, 97, 100, 101, 114, 86, 97, 108, 117, 101]}], |
| 361 | + }, |
| 362 | + { |
| 363 | + "topic": "mytopic", |
| 364 | + "partition": 0, |
| 365 | + "offset": 16, |
| 366 | + "timestamp": 1545084650988, |
| 367 | + "timestampType": "CREATE_TIME", |
| 368 | + "key": "NDI=", |
| 369 | + "value": "AAjpBxIGTGFwdG9wGVK4HoXrP49A", |
| 370 | + "headers": [{"headerKey": [104, 101, 97, 100, 101, 114, 86, 97, 108, 117, 101]}], |
| 371 | + }, |
| 372 | + { |
| 373 | + "topic": "mytopic", |
| 374 | + "partition": 0, |
| 375 | + "offset": 17, |
| 376 | + "timestamp": 1545084650989, |
| 377 | + "timestampType": "CREATE_TIME", |
| 378 | + "key": "NDI=", |
| 379 | + "value": "AgEACOkHEgZMYXB0b3AZUrgehes/j0A=", |
| 380 | + "headers": [{"headerKey": [104, 101, 97, 100, 101, 114, 86, 97, 108, 117, 101]}], |
| 381 | + }, |
| 382 | + ], |
| 383 | + }, |
| 384 | + } |
| 385 | + |
| 386 | + # GIVEN A Kafka consumer configured to deserialize Protobuf data |
| 387 | + # using the User protobuf message type as the schema |
| 388 | + schema_config = SchemaConfig( |
| 389 | + value_schema_type="PROTOBUF", |
| 390 | + value_schema=ProtobufProduct, |
| 391 | + ) |
| 392 | + |
| 393 | + processed_records = [] |
| 394 | + |
| 395 | + @kafka_consumer(schema_config=schema_config) |
| 396 | + def handler(event: ConsumerRecords, context): |
| 397 | + for record in event.records: |
| 398 | + processed_records.append( |
| 399 | + {"id": record.value["id"], "name": record.value["name"], "price": record.value["price"]}, |
| 400 | + ) |
| 401 | + return {"processed": len(processed_records)} |
| 402 | + |
| 403 | + # WHEN The handler processes a Kafka event containing Protobuf-encoded data |
| 404 | + result = handler(complex_event, lambda_context) |
| 405 | + |
| 406 | + # THEN |
| 407 | + # The handler should successfully process both records |
| 408 | + # and return the correct count |
| 409 | + assert result == {"processed": 3} |
| 410 | + |
| 411 | + # All records should be correctly deserialized with proper values |
| 412 | + assert len(processed_records) == 3 |
| 413 | + |
| 414 | + # First record should contain decoded values |
| 415 | + assert processed_records[0]["id"] == 1001 |
| 416 | + assert processed_records[0]["name"] == "Laptop" |
| 417 | + |
| 418 | + # Second record should contain decoded values |
| 419 | + assert processed_records[1]["id"] == 1001 |
| 420 | + assert processed_records[1]["name"] == "Laptop" |
| 421 | + |
| 422 | + # Third record should contain decoded values |
| 423 | + assert processed_records[2]["id"] == 1001 |
| 424 | + assert processed_records[2]["name"] == "Laptop" |
0 commit comments