|
6 | 6 | using System.Text.Json.Serialization;
|
7 | 7 | using System.Text.Json.Serialization.Metadata;
|
8 | 8 | using Amazon.Lambda.Serialization.SystemTextJson;
|
| 9 | +using AWS.Lambda.Powertools.Common; |
9 | 10 | using AWS.Lambda.Powertools.Common.Utils;
|
10 | 11 | using AWS.Lambda.Powertools.Logging.Internal;
|
11 | 12 | using AWS.Lambda.Powertools.Logging.Internal.Converters;
|
@@ -400,6 +401,198 @@ private class ComplexTestObject
|
400 | 401 | public TimeOnly Time { get; set; }
|
401 | 402 | #endif
|
402 | 403 | }
|
| 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 | + |
403 | 596 |
|
404 | 597 | public void Dispose()
|
405 | 598 | {
|
|
0 commit comments