Skip to content

Commit e9fcbf2

Browse files
committed
improve tests and examples
1 parent 5a59f86 commit e9fcbf2

2 files changed

Lines changed: 71 additions & 1 deletion

File tree

examples/Kafka/JsonClassLibrary/src/Function.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,16 @@ public string FunctionHandler(ConsumerRecords<string, CustomerProfile> records,
2121
{
2222
foreach (var record in records)
2323
{
24-
Logger.LogInformation("Record Value: {@record}", record.Value);
24+
Logger.LogInformation("Processing messagem from topic: {topic}", record.Topic);
25+
Logger.LogInformation("Partition: {partition}, Offset: {offset}", record.Partition, record.Offset);
26+
Logger.LogInformation("Produced at: {timestamp}", record.Timestamp);
27+
28+
foreach (var header in record.Headers.DecodedValues())
29+
{
30+
Logger.LogInformation($"{header.Key}: {header.Value}");
31+
}
32+
33+
Logger.LogInformation("Processing order for: {fullName}", record.Value.FullName);
2534
}
2635

2736
return "Processed " + records.Count() + " records";

libraries/tests/AWS.Lambda.Powertools.Kafka.Tests/Protobuf/HandlerTests.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,4 +298,65 @@ private async Task<string> HandlerWithProtobufKeys(ConsumerRecords<ProtobufKey,
298298

299299
return "Successfully processed Protobuf Kafka events with complex keys";
300300
}
301+
302+
[Fact]
303+
public void SimpleHandlerTest()
304+
{
305+
string Handler(ConsumerRecords<int, ProtobufProduct> records, ILambdaContext context)
306+
{
307+
foreach (var record in records)
308+
{
309+
var product = record.Value;
310+
context.Logger.LogInformation($"Processing {product.Name} at ${product.Price}");
311+
}
312+
313+
return "Successfully processed Protobuf Kafka events";
314+
}
315+
// Simulate the handler execution
316+
var mockLogger = new TestLambdaLogger();
317+
var mockContext = new TestLambdaContext
318+
{
319+
Logger = mockLogger
320+
};
321+
322+
var records = new ConsumerRecords<int, ProtobufProduct>
323+
{
324+
Records = new Dictionary<string, List<ConsumerRecord<int, ProtobufProduct>>>
325+
{
326+
{ "mytopic-0", new List<ConsumerRecord<int, ProtobufProduct>>
327+
{
328+
new()
329+
{
330+
Topic = "mytopic",
331+
Partition = 0,
332+
Offset = 15,
333+
Key = 42,
334+
Value = new ProtobufProduct { Name = "Test Product", Id = 1, Price = 99.99 }
335+
}
336+
}
337+
}
338+
}
339+
};
340+
341+
// Call the handler
342+
var result = Handler(records, mockContext);
343+
344+
// Assert the result
345+
Assert.Equal("Successfully processed Protobuf Kafka events", result);
346+
347+
// Verify the context logger output
348+
Assert.Contains("Processing Test Product at $99.99", mockLogger.Buffer.ToString());
349+
350+
// Verify the records were processed
351+
Assert.Single(records.Records);
352+
Assert.Contains("mytopic-0", records.Records.Keys);
353+
Assert.Single(records.Records["mytopic-0"]);
354+
Assert.Equal("mytopic", records.Records["mytopic-0"][0].Topic);
355+
Assert.Equal(0, records.Records["mytopic-0"][0].Partition);
356+
Assert.Equal(15, records.Records["mytopic-0"][0].Offset);
357+
Assert.Equal(42, records.Records["mytopic-0"][0].Key);
358+
Assert.Equal("Test Product", records.Records["mytopic-0"][0].Value.Name);
359+
Assert.Equal(1, records.Records["mytopic-0"][0].Value.Id);
360+
Assert.Equal(99.99, records.Records["mytopic-0"][0].Value.Price);
361+
}
301362
}

0 commit comments

Comments
 (0)