Skip to content

Commit 98659ce

Browse files
committed
more test coverage
1 parent 99073ba commit 98659ce

File tree

2 files changed

+193
-19
lines changed

2 files changed

+193
-19
lines changed

libraries/src/AWS.Lambda.Powertools.Logging/Serializers/PowertoolsLoggingSerializer.cs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -181,25 +181,6 @@ private IJsonTypeInfoResolver GetCompositeResolver()
181181
return new CompositeJsonTypeInfoResolver(resolvers.ToArray());
182182
}
183183

184-
/// <summary>
185-
/// Handles the TypeInfoResolver from the JsonSerializerOptions.
186-
/// </summary>
187-
private void HandleJsonOptionsTypeResolver(JsonSerializerOptions options)
188-
{
189-
// Check for TypeInfoResolver and ensure it's not lost
190-
if (options?.TypeInfoResolver != null &&
191-
options.TypeInfoResolver != GetCompositeResolver())
192-
{
193-
_customTypeInfoResolver = options.TypeInfoResolver;
194-
195-
// If it's a JsonSerializerContext, also add it to our contexts
196-
if (_customTypeInfoResolver is JsonSerializerContext jsonContext)
197-
{
198-
AddSerializerContext(jsonContext);
199-
}
200-
}
201-
}
202-
203184
/// <summary>
204185
/// Gets the JsonTypeInfo for a given type.
205186
/// </summary>

libraries/tests/AWS.Lambda.Powertools.Logging.Tests/Serializers/PowertoolsLoggingSerializerTests.cs

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Text.Json.Serialization;
77
using System.Text.Json.Serialization.Metadata;
88
using Amazon.Lambda.Serialization.SystemTextJson;
9+
using AWS.Lambda.Powertools.Common;
910
using AWS.Lambda.Powertools.Common.Utils;
1011
using AWS.Lambda.Powertools.Logging.Internal;
1112
using AWS.Lambda.Powertools.Logging.Internal.Converters;
@@ -400,6 +401,198 @@ private class ComplexTestObject
400401
public TimeOnly Time { get; set; }
401402
#endif
402403
}
404+
405+
[Fact]
406+
public void ConfigureNamingPolicy_WhenChanged_RebuildsOptions()
407+
{
408+
// Arrange
409+
var serializer = new PowertoolsLoggingSerializer();
410+
411+
// Force initialization of _jsonOptions
412+
_ = serializer.GetSerializerOptions();
413+
414+
// Act
415+
serializer.ConfigureNamingPolicy(LoggerOutputCase.CamelCase);
416+
var options = serializer.GetSerializerOptions();
417+
418+
// Assert
419+
Assert.Equal(JsonNamingPolicy.CamelCase, options.PropertyNamingPolicy);
420+
Assert.Equal(JsonNamingPolicy.CamelCase, options.DictionaryKeyPolicy);
421+
}
422+
423+
[Fact]
424+
public void ConfigureNamingPolicy_WhenAlreadySet_DoesNothing()
425+
{
426+
// Arrange
427+
var serializer = new PowertoolsLoggingSerializer();
428+
serializer.ConfigureNamingPolicy(LoggerOutputCase.CamelCase);
429+
430+
// Get the initial options
431+
var initialOptions = serializer.GetSerializerOptions();
432+
433+
// Act - set the same case again
434+
serializer.ConfigureNamingPolicy(LoggerOutputCase.CamelCase);
435+
var newOptions = serializer.GetSerializerOptions();
436+
437+
// Assert - should be the same instance
438+
Assert.Same(initialOptions, newOptions);
439+
}
440+
441+
[Fact]
442+
public void Serialize_WithValidObject_ReturnsJsonString()
443+
{
444+
// Arrange
445+
var serializer = new PowertoolsLoggingSerializer();
446+
var testObj = new TestClass { Name = "Test", Value = 123 };
447+
448+
// Act
449+
var json = serializer.Serialize(testObj, typeof(TestClass));
450+
451+
// Assert
452+
Assert.Contains("\"name\"", json);
453+
Assert.Contains("\"value\"", json);
454+
Assert.Contains("123", json);
455+
Assert.Contains("Test", json);
456+
}
457+
458+
#if NET8_0_OR_GREATER
459+
[Fact]
460+
public void AddSerializerContext_AddsContext()
461+
{
462+
// Arrange
463+
var serializer = new PowertoolsLoggingSerializer();
464+
var context = new TestJsonContext(new JsonSerializerOptions());
465+
466+
// Act
467+
serializer.AddSerializerContext(context);
468+
469+
// No immediate assertion - the context is added internally
470+
// We'll verify it works through serialization tests
471+
}
472+
473+
[Fact]
474+
public void SetOptions_WithTypeInfoResolver_SetsCustomResolver()
475+
{
476+
// Arrange
477+
var serializer = new PowertoolsLoggingSerializer();
478+
479+
// Explicitly disable dynamic code - important to set before creating options
480+
RuntimeFeatureWrapper.SetIsDynamicCodeSupported(false);
481+
482+
var context = new TestJsonContext(new JsonSerializerOptions());
483+
var options = new JsonSerializerOptions
484+
{
485+
TypeInfoResolver = context
486+
};
487+
488+
// Act
489+
serializer.SetOptions(options);
490+
var serializerOptions = serializer.GetSerializerOptions();
491+
492+
// Assert - options are properly configured
493+
Assert.NotNull(serializerOptions.TypeInfoResolver);
494+
}
495+
496+
[Fact]
497+
public void SetOptions_WithContextAsResolver_AddsToContexts()
498+
{
499+
// Arrange
500+
var serializer = new PowertoolsLoggingSerializer();
501+
var context = new TestJsonContext(new JsonSerializerOptions());
502+
var options = new JsonSerializerOptions
503+
{
504+
TypeInfoResolver = context
505+
};
506+
507+
// Act - This adds the context automatically
508+
serializer.SetOptions(options);
509+
510+
// No direct assertion possible for internal state, but we can test it works
511+
// through proper serialization
512+
}
513+
#endif
514+
515+
[Fact]
516+
public void SetOutputCase_CamelCase_SetsPoliciesCorrectly()
517+
{
518+
// Arrange
519+
var serializer = new PowertoolsLoggingSerializer();
520+
serializer.ConfigureNamingPolicy(LoggerOutputCase.CamelCase);
521+
522+
// Act
523+
var options = serializer.GetSerializerOptions();
524+
525+
// Assert
526+
Assert.Equal(JsonNamingPolicy.CamelCase, options.PropertyNamingPolicy);
527+
Assert.Equal(JsonNamingPolicy.CamelCase, options.DictionaryKeyPolicy);
528+
}
529+
530+
[Fact]
531+
public void SetOutputCase_PascalCase_SetsPoliciesCorrectly()
532+
{
533+
// Arrange
534+
var serializer = new PowertoolsLoggingSerializer();
535+
serializer.ConfigureNamingPolicy(LoggerOutputCase.PascalCase);
536+
537+
// Act
538+
var options = serializer.GetSerializerOptions();
539+
540+
// Assert
541+
Assert.IsType<PascalCaseNamingPolicy>(options.PropertyNamingPolicy);
542+
Assert.IsType<PascalCaseNamingPolicy>(options.DictionaryKeyPolicy);
543+
}
544+
545+
[Fact]
546+
public void SetOutputCase_SnakeCase_SetsPoliciesCorrectly()
547+
{
548+
// Arrange
549+
var serializer = new PowertoolsLoggingSerializer();
550+
serializer.ConfigureNamingPolicy(LoggerOutputCase.SnakeCase);
551+
552+
// Act
553+
var options = serializer.GetSerializerOptions();
554+
555+
#if NET8_0_OR_GREATER
556+
// Assert - in .NET 8 we use built-in SnakeCaseLower
557+
Assert.Equal(JsonNamingPolicy.SnakeCaseLower, options.PropertyNamingPolicy);
558+
Assert.Equal(JsonNamingPolicy.SnakeCaseLower, options.DictionaryKeyPolicy);
559+
#else
560+
// Assert - in earlier versions, we use custom SnakeCaseNamingPolicy
561+
Assert.IsType<SnakeCaseNamingPolicy>(options.PropertyNamingPolicy);
562+
Assert.IsType<SnakeCaseNamingPolicy>(options.DictionaryKeyPolicy);
563+
#endif
564+
}
565+
566+
[Fact]
567+
public void GetSerializerOptions_AddsAllConverters()
568+
{
569+
// Arrange
570+
var serializer = new PowertoolsLoggingSerializer();
571+
572+
// Act
573+
var options = serializer.GetSerializerOptions();
574+
575+
// Assert
576+
Assert.Contains(options.Converters, c => c is ByteArrayConverter);
577+
Assert.Contains(options.Converters, c => c is ExceptionConverter);
578+
Assert.Contains(options.Converters, c => c is MemoryStreamConverter);
579+
Assert.Contains(options.Converters, c => c is ConstantClassConverter);
580+
Assert.Contains(options.Converters, c => c is DateOnlyConverter);
581+
Assert.Contains(options.Converters, c => c is TimeOnlyConverter);
582+
#if NET8_0_OR_GREATER || NET6_0
583+
Assert.Contains(options.Converters, c => c is LogLevelJsonConverter);
584+
#endif
585+
}
586+
587+
// Test class for serialization
588+
private class TestClass
589+
{
590+
public string Name { get; set; }
591+
public int Value { get; set; }
592+
}
593+
594+
595+
403596

404597
public void Dispose()
405598
{

0 commit comments

Comments
 (0)