Skip to content

Commit 0b26a1a

Browse files
committed
enhance tests: add deserialization and serialization tests for non-ConsumerRecord types and primitive values
1 parent 146837f commit 0b26a1a

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

libraries/tests/AWS.Lambda.Powertools.Kafka.Tests/PowertoolsKafkaSerializerBaseTests.cs

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,10 +445,140 @@ public void Serialize_NonRegisteredType_FallsBackToRegularSerialization()
445445
Assert.Contains("\"Id\":", result);
446446
Assert.Contains("\"Message\":\"Not in context\"", result);
447447
}
448+
449+
[Fact]
450+
public void Deserialize_NonConsumerRecordWithSerializerContext_UsesTypeInfo()
451+
{
452+
// Arrange
453+
var options = new JsonSerializerOptions();
454+
var context = new TestSerializerContext(options);
455+
var serializer = new TestKafkaSerializer(options, context);
456+
457+
var testModel = new TestModel { Name = "DirectDeserialization", Value = 42 };
458+
var json = JsonSerializer.Serialize(testModel);
459+
460+
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(json));
461+
462+
// Act
463+
var result = serializer.Deserialize<TestModel>(stream);
464+
465+
// Assert
466+
Assert.NotNull(result);
467+
Assert.Equal("DirectDeserialization", result.Name);
468+
Assert.Equal(42, result.Value);
469+
}
470+
471+
[Fact]
472+
public void Deserialize_NonConsumerRecordWithoutTypeInfo_UsesRegularDeserialize()
473+
{
474+
// Arrange
475+
var options = new JsonSerializerOptions();
476+
var context = new TestSerializerContext(options);
477+
var serializer = new TestKafkaSerializer(options, context);
478+
479+
// Dictionary<string,int> is not registered in TestSerializerContext
480+
var dict = new Dictionary<string, int> { ["test"] = 123 };
481+
var json = JsonSerializer.Serialize(dict);
482+
483+
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(json));
484+
485+
// Act
486+
var result = serializer.Deserialize<Dictionary<string, int>>(stream);
487+
488+
// Assert
489+
Assert.NotNull(result);
490+
Assert.Equal(123, result["test"]);
491+
}
492+
493+
[Fact]
494+
public void Deserialize_NonConsumerRecordFailed_ThrowsException()
495+
{
496+
// Arrange
497+
var serializer = new TestKafkaSerializer();
498+
var invalidJson = "{ invalid json";
499+
500+
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(invalidJson));
501+
502+
// Act & Assert
503+
// With invalid JSON input, JsonSerializer throws JsonException directly
504+
var ex = Assert.Throws<JsonException>(() =>
505+
serializer.Deserialize<TestModel>(stream));
506+
507+
// Check that we're getting a JSON parsing error
508+
Assert.Contains("invalid", ex.Message.ToLower());
509+
}
510+
511+
[Theory]
512+
[InlineData(new byte[] { 42 }, 42)] // Single byte
513+
[InlineData(new byte[] { 0x2A, 0x00, 0x00, 0x00 }, 42)] // Four bytes
514+
public void DeserializePrimitiveValue_IntWithDifferentByteFormats_DeserializesCorrectly(byte[] bytes,
515+
int expected)
516+
{
517+
// Arrange
518+
var serializer = new TestKafkaSerializer();
519+
520+
// Act
521+
var result = serializer.TestDeserializePrimitiveValue(bytes, typeof(int));
522+
523+
// Assert
524+
Assert.Equal(expected, result);
525+
}
526+
527+
[Theory]
528+
[InlineData(new byte[] { 0x2A, 0x00, 0x00, 0x00 }, 42L)] // Four bytes as int
529+
[InlineData(new byte[] { 0x2A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 42L)] // Eight bytes as long
530+
public void DeserializePrimitiveValue_LongWithDifferentByteFormats_DeserializesCorrectly(byte[] bytes,
531+
long expected)
532+
{
533+
// Arrange
534+
var serializer = new TestKafkaSerializer();
535+
536+
// Act
537+
var result = serializer.TestDeserializePrimitiveValue(bytes, typeof(long));
538+
539+
// Assert
540+
Assert.Equal(expected, result);
541+
}
542+
543+
[Fact]
544+
public void DeserializePrimitiveValue_DoubleWithShortBytes_ReturnsZero()
545+
{
546+
// Arrange
547+
var serializer = new TestKafkaSerializer();
548+
var shortBytes = new byte[] { 0x00, 0x00, 0x00, 0x00 }; // Less than 8 bytes
549+
550+
// Act
551+
var result = serializer.TestDeserializePrimitiveValue(shortBytes, typeof(double));
552+
553+
// Assert
554+
Assert.Equal(0.0, result);
555+
}
556+
557+
[Fact]
558+
public void Serialize_WithTypeInfoFromContext_WritesToStream()
559+
{
560+
// Arrange
561+
var options = new JsonSerializerOptions();
562+
var context = new TestSerializerContext(options);
563+
var serializer = new TestKafkaSerializer(options, context);
564+
565+
var testModel = new TestModel { Name = "ContextSerialization", Value = 555 };
566+
using var responseStream = new MemoryStream();
567+
568+
// Act
569+
serializer.Serialize(testModel, responseStream);
570+
responseStream.Position = 0;
571+
string result = Encoding.UTF8.GetString(responseStream.ToArray());
572+
573+
// Assert
574+
Assert.Contains("\"Name\":\"ContextSerialization\"", result);
575+
Assert.Contains("\"Value\":555", result);
576+
}
448577
}
449578

450579
[JsonSerializable(typeof(TestModel))]
451580
[JsonSerializable(typeof(ConsumerRecords<string, TestModel>))]
581+
[JsonSerializable(typeof(Dictionary<string, int>))]
452582
public partial class TestSerializerContext : JsonSerializerContext
453583
{
454584
}

0 commit comments

Comments
 (0)