From 4fff4ba11deb4d3395497917a6d8ccf6b671f7a5 Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Fri, 13 Nov 2026 14:40:03 +0100 Subject: [PATCH] SerializationDemo: move to .NET 11, fix the cache client, one test framework Retarget all four projects to net11.0. Common references protobuf-net 3.4.40 directly; the API references WebApiContrib.Core.Formatter.Protobuf 3.0.0 and Microsoft.AspNetCore.OpenApi 11.0.0 in place of Swashbuckle. CachedJsonClient.RefreshCache no longer serializes the JSON response a second time. Serializers return T?, the client awaits instead of .Result, and the tests run on MSTest 4.5.3 only (the four NUnit tests are converted, the JSON byte test now tests JsonByteSerializer, a Protobuf GET test is added; 12 tests). --- .../Clients/CachedJsonClient.cs | 38 +-- .../Clients/IClient.cs | 6 +- .../Clients/JsonClient.cs | 12 +- .../Clients/ProtobufClient.cs | 14 +- .../Clients/XmlClient.cs | 14 +- .../SerializationDemo.Client/Program.cs | 40 ++- .../Serialization/IByteSerializer.cs | 2 +- .../Serialization/IStringSerializer.cs | 4 +- .../Serialization/Json/JsonByteSerializer.cs | 12 +- .../Json/JsonStringSerializer.cs | 4 +- .../Protobuf/ProtobufSerializer.cs | 18 +- .../Serialization/Xml/XmlSerializer.cs | 17 +- .../SerializationDemo.Client.csproj | 5 +- .../Models/Address.cs | 6 +- .../Models/Employee.cs | 2 +- .../SerializationDemo.Common.csproj | 4 +- .../SerializationDemo.EmployeeApi/Program.cs | 12 +- .../Properties/launchSettings.json | 21 +- .../Repositories/EmployeeRepository.cs | 5 +- .../SerializationDemo.EmployeeApi.csproj | 5 +- .../Tests/ApiIntegrationTests.cs | 289 +++++++++--------- .../Tests/JsonSerializationTests.cs | 95 +++--- .../Tests/ProtobufSerializerTests.cs | 66 ++-- .../SerializationDemo/Tests/Tests.csproj | 17 +- .../Tests/XmlSerializerTests.cs | 49 ++- 25 files changed, 348 insertions(+), 409 deletions(-) diff --git a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Clients/CachedJsonClient.cs b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Clients/CachedJsonClient.cs index cc4eba1f9f..354e1d4e9c 100644 --- a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Clients/CachedJsonClient.cs +++ b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Clients/CachedJsonClient.cs @@ -1,5 +1,4 @@ using Microsoft.Extensions.Caching.Distributed; -using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.DependencyInjection; using SerializationDemo.Client.Serialization; using SerializationDemo.Client.Serialization.Json; @@ -9,30 +8,30 @@ namespace SerializationDemo.Client.Clients { internal class CachedJsonClient : ClientBase, IClient - { + { private readonly IByteSerializer _serializer = new JsonByteSerializer(); private readonly HttpClient _client = new HttpClient(); private readonly IDistributedCache _distributedCache; private const string AllEmployeesKey = "AllEmployees"; public CachedJsonClient() - { + { var collection = new ServiceCollection(); collection.AddDistributedMemoryCache(); IServiceProvider serviceProvider = collection.BuildServiceProvider(); - _distributedCache = serviceProvider.GetService(); + _distributedCache = serviceProvider.GetRequiredService(); } public async Task> GetAllEmployees() { // Find cached item - byte[] objectFromCache = await _distributedCache.GetAsync(AllEmployeesKey); + byte[]? objectFromCache = await _distributedCache.GetAsync(AllEmployeesKey); if (objectFromCache != null) - { + { var employees = _serializer.Deserialize>(objectFromCache); - return employees; + return employees ?? []; } return await RefreshCache(); @@ -43,25 +42,25 @@ private async Task GetResponseFromApi() var url = $"{BaseUrl}/api/employees"; var request = new HttpRequestMessage(HttpMethod.Get, url); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); - + var response = await _client.SendAsync(request); - - return response.Content; + + return response.Content; } - public async Task CreateEmployee(Employee employee) + public async Task CreateEmployee(Employee employee) { var url = $"{BaseUrl}/api/employees"; var request = new HttpRequestMessage(HttpMethod.Post, url); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); request.Content = new ByteArrayContent(_serializer.Serialize(employee)); - request.Content.Headers.Add("Content-Type", "application/json"); - + request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); + var response = await _client.SendAsync(request); if (response.IsSuccessStatusCode) { - await RefreshCache(); + await RefreshCache(); } return employee; @@ -71,16 +70,17 @@ private async Task> RefreshCache() { // If not found, then recalculate response var content = await GetResponseFromApi(); + var employees = _serializer.Deserialize>(await content.ReadAsByteArrayAsync()) ?? []; var cacheEntryOptions = new DistributedCacheEntryOptions() - .SetSlidingExpiration(TimeSpan.FromSeconds(10)) - .SetAbsoluteExpiration(TimeSpan.FromSeconds(30)); + .SetSlidingExpiration(TimeSpan.FromSeconds(10)) + .SetAbsoluteExpiration(TimeSpan.FromSeconds(30)); // Cache it - var resultAsByteArray = _serializer.Serialize(await content.ReadAsStringAsync()); + var resultAsByteArray = _serializer.Serialize(employees); await _distributedCache.SetAsync(AllEmployeesKey, resultAsByteArray, cacheEntryOptions); - return _serializer.Deserialize>(resultAsByteArray); + return employees; } } -} \ No newline at end of file +} diff --git a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Clients/IClient.cs b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Clients/IClient.cs index 5541c98144..85064d6926 100644 --- a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Clients/IClient.cs +++ b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Clients/IClient.cs @@ -3,8 +3,8 @@ namespace SerializationDemo.Client.Clients { internal interface IClient - { + { Task> GetAllEmployees(); - Task CreateEmployee(Employee employee); + Task CreateEmployee(Employee employee); } -} \ No newline at end of file +} diff --git a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Clients/JsonClient.cs b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Clients/JsonClient.cs index c15fccdfb1..5d565e72cc 100644 --- a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Clients/JsonClient.cs +++ b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Clients/JsonClient.cs @@ -7,16 +7,16 @@ namespace SerializationDemo.Client.Clients { internal class JsonClient : ClientBase, IClient - { + { private readonly IStringSerializer _serializer = new JsonStringSerializer(); private readonly HttpClient _client = new HttpClient(); - public async Task CreateEmployee(Employee employee) + public async Task CreateEmployee(Employee employee) { var url = $"{BaseUrl}/api/employees"; var request = new HttpRequestMessage(HttpMethod.Post, url); - request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); - request.Content = new StringContent(_serializer.Serialize(employee), Encoding.UTF8,"application/json"); + request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + request.Content = new StringContent(_serializer.Serialize(employee), Encoding.UTF8, "application/json"); var response = await _client.SendAsync(request); @@ -37,8 +37,8 @@ public async Task> GetAllEmployees() var response = await _client.SendAsync(request); var content = await response.Content.ReadAsStringAsync(); - - return _serializer.Deserialize>(content); + + return _serializer.Deserialize>(content) ?? []; } } } diff --git a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Clients/ProtobufClient.cs b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Clients/ProtobufClient.cs index 4b78be43db..c8e61d969c 100644 --- a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Clients/ProtobufClient.cs +++ b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Clients/ProtobufClient.cs @@ -6,17 +6,17 @@ namespace SerializationDemo.Client.Clients { internal class ProtobufClient : ClientBase, IClient - { + { private readonly IByteSerializer _serializer = new ProtobufSerializer(); private readonly HttpClient _client = new HttpClient(); - public async Task CreateEmployee(Employee employee) + public async Task CreateEmployee(Employee employee) { var url = $"{BaseUrl}/api/employees"; var request = new HttpRequestMessage(HttpMethod.Post, url); - request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-protobuf")); + request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-protobuf")); request.Content = new ByteArrayContent(_serializer.Serialize(employee)); - request.Content.Headers.Add("Content-Type", "application/x-protobuf"); + request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-protobuf"); var response = await _client.SendAsync(request); @@ -34,11 +34,11 @@ public async Task> GetAllEmployees() var url = $"{BaseUrl}/api/employees"; var request = new HttpRequestMessage(HttpMethod.Get, url); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-protobuf")); - + var response = await _client.SendAsync(request); var content = await response.Content.ReadAsByteArrayAsync(); - - return _serializer.Deserialize>(content); + + return _serializer.Deserialize>(content) ?? []; } } } diff --git a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Clients/XmlClient.cs b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Clients/XmlClient.cs index 73421b8876..599667ab94 100644 --- a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Clients/XmlClient.cs +++ b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Clients/XmlClient.cs @@ -7,17 +7,17 @@ namespace SerializationDemo.Client.Clients { internal class XmlClient : ClientBase, IClient - { + { private readonly IStringSerializer _serializer = new XmlSerializer(); private readonly HttpClient _client = new HttpClient(); - public async Task CreateEmployee(Employee employee) + public async Task CreateEmployee(Employee employee) { var url = $"{BaseUrl}/api/employees"; var request = new HttpRequestMessage(HttpMethod.Post, url); - request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml")); + request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml")); request.Content = new StringContent(_serializer.Serialize(employee), Encoding.UTF8, "application/xml"); - + var response = await _client.SendAsync(request); if (response.IsSuccessStatusCode) @@ -34,11 +34,11 @@ public async Task> GetAllEmployees() var url = $"{BaseUrl}/api/employees"; var request = new HttpRequestMessage(HttpMethod.Get, url); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml")); - + var response = await _client.SendAsync(request); var content = await response.Content.ReadAsStringAsync(); - - return _serializer.Deserialize>(content); + + return _serializer.Deserialize>(content) ?? []; } } } diff --git a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Program.cs b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Program.cs index 8990914901..c9ce0e041b 100644 --- a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Program.cs +++ b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Program.cs @@ -1,8 +1,6 @@ -// See https://aka.ms/new-console-template for more information -using SerializationDemo.Client.Clients; +using SerializationDemo.Client.Clients; using SerializationDemo.Common.Models; - Console.Write("Enter a format name(json, cache, xml, proto): "); string? format = Console.ReadLine(); format = format?.Trim().ToLower(); @@ -16,42 +14,42 @@ _ => new JsonClient() }; -Console.WriteLine("Employee list before adding employee."); +Console.WriteLine(); +Console.WriteLine("Employee list before adding an employee."); Console.WriteLine("--------------------------------------"); -PrintAllEmployees(client); -CreateEmployee(client); +await PrintAllEmployees(client); +await CreateEmployee(client); Console.WriteLine(); -Console.WriteLine("Employee list after adding employee."); +Console.WriteLine("Employee list after adding an employee."); Console.WriteLine("--------------------------------------"); -PrintAllEmployees(client); +await PrintAllEmployees(client); -static void PrintAllEmployees(IClient client) +static async Task PrintAllEmployees(IClient client) { - var employees = client.GetAllEmployees().Result; + var employees = await client.GetAllEmployees(); foreach (var employee in employees) { Console.WriteLine($"Name:{employee.Name}"); - Console.WriteLine($"Address:{employee?.Address?.AddressLine1}, {employee?.Address?.AddressLine2}, {employee?.Address?.City}, {employee?.Address?.Country}"); - Console.WriteLine(); + Console.WriteLine($"Address:{employee.Address?.AddressLine1}, {employee.Address?.AddressLine2}, {employee.Address?.City}, {employee.Address?.Country}"); Console.WriteLine(); } } -static void CreateEmployee(IClient client) +static async Task CreateEmployee(IClient client) { var employee = new Employee { Id = Guid.NewGuid(), - Name = $"TestEmployee6", + Name = "TestEmployee6", Address = new Address { - AddressLine1 = $"Street #6", - AddressLine2 = $"District #6", - City = $"Sample City30", - Country = $"Sample Country22", + AddressLine1 = "Street #6", + AddressLine2 = "District #6", + City = "Sample City30", + Country = "Sample Country22", } }; - var neweEmployee = client.CreateEmployee(employee).Result; - Console.WriteLine($"New employee {neweEmployee?.Name} has been added"); -} \ No newline at end of file + var newEmployee = await client.CreateEmployee(employee); + Console.WriteLine($"New employee {newEmployee?.Name} has been added"); +} diff --git a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Serialization/IByteSerializer.cs b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Serialization/IByteSerializer.cs index 7df4191d0f..9335dc0074 100644 --- a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Serialization/IByteSerializer.cs +++ b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Serialization/IByteSerializer.cs @@ -2,7 +2,7 @@ { internal interface IByteSerializer { - T Deserialize(byte[] buffer); + T? Deserialize(byte[] buffer); byte[] Serialize(object data); } diff --git a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Serialization/IStringSerializer.cs b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Serialization/IStringSerializer.cs index 8ff1aed5d9..7d82b2a44c 100644 --- a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Serialization/IStringSerializer.cs +++ b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Serialization/IStringSerializer.cs @@ -1,8 +1,8 @@ namespace SerializationDemo.Client.Serialization { - internal interface IStringSerializer + internal interface IStringSerializer { - T Deserialize(string text); + T? Deserialize(string text); string Serialize(object data); } diff --git a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Serialization/Json/JsonByteSerializer.cs b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Serialization/Json/JsonByteSerializer.cs index 614b250b3f..08f02b50fd 100644 --- a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Serialization/Json/JsonByteSerializer.cs +++ b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Serialization/Json/JsonByteSerializer.cs @@ -5,6 +5,7 @@ namespace SerializationDemo.Client.Serialization.Json public class JsonByteSerializer : IByteSerializer { private readonly JsonSerializerOptions _options; + public JsonByteSerializer() : this(GetDefaultJsonSerializerOptions()) { @@ -13,16 +14,11 @@ public JsonByteSerializer() public JsonByteSerializer(JsonSerializerOptions options) => _options = options; - public T Deserialize(byte[] buffer) - { - using var stream = new MemoryStream(buffer); - return JsonSerializer.Deserialize(stream, _options); - } + public T? Deserialize(byte[] buffer) + => JsonSerializer.Deserialize(buffer, _options); public byte[] Serialize(object data) - { - return JsonSerializer.SerializeToUtf8Bytes(data, _options); - } + => JsonSerializer.SerializeToUtf8Bytes(data, _options); private static JsonSerializerOptions GetDefaultJsonSerializerOptions() => new JsonSerializerOptions(JsonSerializerDefaults.Web); diff --git a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Serialization/Json/JsonStringSerializer.cs b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Serialization/Json/JsonStringSerializer.cs index e34a774fa7..7a9f626ca6 100644 --- a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Serialization/Json/JsonStringSerializer.cs +++ b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Serialization/Json/JsonStringSerializer.cs @@ -14,7 +14,7 @@ public JsonStringSerializer() public JsonStringSerializer(JsonSerializerOptions options) => _options = options; - public T Deserialize(string text) + public T? Deserialize(string text) => JsonSerializer.Deserialize(text, _options); public string Serialize(object data) @@ -23,4 +23,4 @@ public string Serialize(object data) private static JsonSerializerOptions GetDefaultJsonSerializerOptions() => new JsonSerializerOptions(JsonSerializerDefaults.Web); } -} \ No newline at end of file +} diff --git a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Serialization/Protobuf/ProtobufSerializer.cs b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Serialization/Protobuf/ProtobufSerializer.cs index 0178b030f4..659715f285 100644 --- a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Serialization/Protobuf/ProtobufSerializer.cs +++ b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Serialization/Protobuf/ProtobufSerializer.cs @@ -4,23 +4,19 @@ namespace SerializationDemo.Client.Serialization.Protobuf { public class ProtobufSerializer : IByteSerializer { - public T Deserialize(byte[] buffer) + public T? Deserialize(byte[] buffer) { - using (var stream = new MemoryStream(buffer)) - { - return Serializer.Deserialize(stream); - } + using var stream = new MemoryStream(buffer); + + return Serializer.Deserialize(stream); } public byte[] Serialize(object data) { - using (var stream = new MemoryStream()) - { - Serializer.Serialize(stream, data); - stream.Flush(); + using var stream = new MemoryStream(); + Serializer.Serialize(stream, data); - return stream.ToArray(); - } + return stream.ToArray(); } } } diff --git a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Serialization/Xml/XmlSerializer.cs b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Serialization/Xml/XmlSerializer.cs index 206b61833d..3ac348211b 100644 --- a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Serialization/Xml/XmlSerializer.cs +++ b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/Serialization/Xml/XmlSerializer.cs @@ -4,26 +4,23 @@ namespace SerializationDemo.Client.Serialization.Xml { public class XmlSerializer : IStringSerializer { - public T Deserialize(string text) + public T? Deserialize(string text) { var serializer = new System.Xml.Serialization.XmlSerializer(typeof(T)); - using (var stream = new StringReader(text)) - { - return (T)serializer.Deserialize(stream); - } + using var reader = new StringReader(text); + + return (T?)serializer.Deserialize(reader); } public string Serialize(object data) { var serializer = new System.Xml.Serialization.XmlSerializer(data.GetType()); - using (var stream = new Utf8StringWriter()) - { - serializer.Serialize(stream, data); + using var writer = new Utf8StringWriter(); + serializer.Serialize(writer, data); - return stream.ToString(); - } + return writer.ToString(); } } diff --git a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/SerializationDemo.Client.csproj b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/SerializationDemo.Client.csproj index c4e8714db8..042fd74e4c 100644 --- a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/SerializationDemo.Client.csproj +++ b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Client/SerializationDemo.Client.csproj @@ -2,13 +2,14 @@ Exe - net6.0 + net11.0 enable enable - + + diff --git a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Common/Models/Address.cs b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Common/Models/Address.cs index fd479b5d02..79ddd21d11 100644 --- a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Common/Models/Address.cs +++ b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Common/Models/Address.cs @@ -6,15 +6,15 @@ namespace SerializationDemo.Common.Models public class Address { [ProtoMember(1)] - public string AddressLine1 { get; set; } + public string AddressLine1 { get; set; } = string.Empty; [ProtoMember(2)] public string? AddressLine2 { get; set; } [ProtoMember(3)] - public string City { get; set; } + public string City { get; set; } = string.Empty; [ProtoMember(4)] - public string Country { get; set; } + public string Country { get; set; } = string.Empty; } } diff --git a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Common/Models/Employee.cs b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Common/Models/Employee.cs index f2cdc882cd..a9cf0868be 100644 --- a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Common/Models/Employee.cs +++ b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Common/Models/Employee.cs @@ -9,7 +9,7 @@ public class Employee public Guid Id { get; set; } [ProtoMember(2)] - public string Name { get; set; } + public string Name { get; set; } = string.Empty; [ProtoMember(3)] public Address? Address { get; set; } diff --git a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Common/SerializationDemo.Common.csproj b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Common/SerializationDemo.Common.csproj index 0032ba3902..c815e134d0 100644 --- a/aspnetcore-webapi/SerializationDemo/SerializationDemo.Common/SerializationDemo.Common.csproj +++ b/aspnetcore-webapi/SerializationDemo/SerializationDemo.Common/SerializationDemo.Common.csproj @@ -1,13 +1,13 @@ - net6.0 + net11.0 enable enable - + diff --git a/aspnetcore-webapi/SerializationDemo/SerializationDemo.EmployeeApi/Program.cs b/aspnetcore-webapi/SerializationDemo/SerializationDemo.EmployeeApi/Program.cs index d32b9fda1b..4c429e0993 100644 --- a/aspnetcore-webapi/SerializationDemo/SerializationDemo.EmployeeApi/Program.cs +++ b/aspnetcore-webapi/SerializationDemo/SerializationDemo.EmployeeApi/Program.cs @@ -3,25 +3,19 @@ var builder = WebApplication.CreateBuilder(args); -// Add services to the container. - builder.Services.AddControllers() .AddProtobufFormatters() .AddXmlSerializerFormatters(); -// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle -builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); +builder.Services.AddOpenApi(); builder.Services.AddSingleton(); var app = builder.Build(); -// Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { - app.UseSwagger(); - app.UseSwaggerUI(); + app.MapOpenApi(); } app.UseHttpsRedirection(); @@ -31,5 +25,3 @@ app.MapControllers(); app.Run(); - -public partial class Program { } \ No newline at end of file diff --git a/aspnetcore-webapi/SerializationDemo/SerializationDemo.EmployeeApi/Properties/launchSettings.json b/aspnetcore-webapi/SerializationDemo/SerializationDemo.EmployeeApi/Properties/launchSettings.json index c6c937f4bc..814ba16086 100644 --- a/aspnetcore-webapi/SerializationDemo/SerializationDemo.EmployeeApi/Properties/launchSettings.json +++ b/aspnetcore-webapi/SerializationDemo/SerializationDemo.EmployeeApi/Properties/launchSettings.json @@ -1,31 +1,14 @@ { "$schema": "https://json.schemastore.org/launchsettings.json", - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:39967", - "sslPort": 44359 - } - }, "profiles": { - "SerializationDemo.EmployeeApi": { + "https": { "commandName": "Project", "dotnetRunMessages": true, - "launchBrowser": true, - "launchUrl": "swagger", + "launchBrowser": false, "applicationUrl": "https://localhost:7181;http://localhost:5181", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } - }, - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, - "launchUrl": "swagger", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } } } } diff --git a/aspnetcore-webapi/SerializationDemo/SerializationDemo.EmployeeApi/Repositories/EmployeeRepository.cs b/aspnetcore-webapi/SerializationDemo/SerializationDemo.EmployeeApi/Repositories/EmployeeRepository.cs index 6a8b67ab27..0fb78dc47e 100644 --- a/aspnetcore-webapi/SerializationDemo/SerializationDemo.EmployeeApi/Repositories/EmployeeRepository.cs +++ b/aspnetcore-webapi/SerializationDemo/SerializationDemo.EmployeeApi/Repositories/EmployeeRepository.cs @@ -5,7 +5,6 @@ namespace SerializationDemo.EmployeeApi.Repositories public class EmployeeRepository : IEmployeeRepository { private readonly Dictionary _employees = new(); - private Random _rnd = new Random(); public EmployeeRepository() { InitializeEmployeeStore(); @@ -39,8 +38,8 @@ private void InitializeEmployeeStore() { AddressLine1 = $"Street #{i + 1}", AddressLine2 = $"District #{i + 1}", - City = $"Sample City{_rnd.Next(1, 5)}", - Country = $"Sample Country{_rnd.Next(1, 5)}", + City = $"Sample City{Random.Shared.Next(1, 5)}", + Country = $"Sample Country{Random.Shared.Next(1, 5)}", } }); } diff --git a/aspnetcore-webapi/SerializationDemo/SerializationDemo.EmployeeApi/SerializationDemo.EmployeeApi.csproj b/aspnetcore-webapi/SerializationDemo/SerializationDemo.EmployeeApi/SerializationDemo.EmployeeApi.csproj index c7a671aa3a..5bed500502 100644 --- a/aspnetcore-webapi/SerializationDemo/SerializationDemo.EmployeeApi/SerializationDemo.EmployeeApi.csproj +++ b/aspnetcore-webapi/SerializationDemo/SerializationDemo.EmployeeApi/SerializationDemo.EmployeeApi.csproj @@ -1,13 +1,14 @@ - net6.0 + net11.0 enable enable - + + diff --git a/aspnetcore-webapi/SerializationDemo/Tests/ApiIntegrationTests.cs b/aspnetcore-webapi/SerializationDemo/Tests/ApiIntegrationTests.cs index 16f2770ddc..2e0f315912 100644 --- a/aspnetcore-webapi/SerializationDemo/Tests/ApiIntegrationTests.cs +++ b/aspnetcore-webapi/SerializationDemo/Tests/ApiIntegrationTests.cs @@ -1,184 +1,171 @@ using Microsoft.AspNetCore.Mvc.Testing; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using Newtonsoft.Json; using SerializationDemo.Common.Models; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; +using SerializationDemo.EmployeeApi.Controllers; using System.Net; -using System.Net.Http; using System.Net.Http.Headers; using System.Text; -using System.Threading.Tasks; +using System.Text.Json; using System.Xml.Serialization; -namespace Tests +namespace Tests; + +[TestClass] +public class ApiIntegrationTests { - [TestClass] - public class ApiIntegrationTests - { - [TestMethod] - public async Task WhenGetEndPointIsInvoked_Status200IsReturnedWithListOfEmployees() - { - using var factory = new WebApplicationFactory(); - var httpClient = factory.CreateClient(); + private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web); - var response = await httpClient.GetAsync("/api/employees"); - response.EnsureSuccessStatusCode(); - var result = await response.Content.ReadAsStringAsync(); - var employees = JsonConvert.DeserializeObject>(result); + [TestMethod] + public async Task WhenGetEndPointIsInvoked_Status200IsReturnedWithListOfEmployees() + { + using var factory = new WebApplicationFactory(); + var httpClient = factory.CreateClient(); - Assert.AreEqual(5, employees?.ToList().Count); - } + var response = await httpClient.GetAsync("/api/employees"); + response.EnsureSuccessStatusCode(); + var result = await response.Content.ReadAsStringAsync(); + var employees = JsonSerializer.Deserialize>(result, JsonOptions); - [TestMethod] - public async Task WhenPostEndPointIsInvoked_StatusCode201IsReturnedWithNewEmployee() - { - var newEmployee = GetNewEmployee(); - using var factory = new WebApplicationFactory(); - var httpClient = factory.CreateClient(); - var request = new HttpRequestMessage(HttpMethod.Post, "/api/employees"); - request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); - request.Content = new StringContent(JsonConvert.SerializeObject(newEmployee), Encoding.UTF8, "application/json"); - - var response = await httpClient.SendAsync(request); - var receivedEmployee = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); - - Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); - Assert.AreEqual(newEmployee.Id, receivedEmployee.Id); - } + Assert.AreEqual(5, employees?.Count); + } - [TestMethod] - public async Task WhenGetEndPointIsInvokedWithAcceptHeaderSetAsJson_ResponseIsReturnedInJsonFormat() - { - using var factory = new WebApplicationFactory(); - var httpClient = factory.CreateClient(); - var contentFormat = "application/json"; - var request = CreateRequest(HttpMethod.Get, "/api/employees", contentFormat); - - var response = await httpClient.SendAsync(request); - - Assert.AreEqual(contentFormat, response?.Content?.Headers?.ContentType?.MediaType); - } + [TestMethod] + public async Task WhenPostEndPointIsInvoked_StatusCode201IsReturnedWithNewEmployee() + { + var newEmployee = GetNewEmployee(); + using var factory = new WebApplicationFactory(); + var httpClient = factory.CreateClient(); + var request = CreateRequest(HttpMethod.Post, "/api/employees", "application/json"); + request.Content = new StringContent(JsonSerializer.Serialize(newEmployee, JsonOptions), Encoding.UTF8, "application/json"); - [TestMethod] - public async Task WhenGetEndPointIsInvokedWithAcceptHeaderSetAsXml_ResponseIsReturnedInXmlFormat() - { - using var factory = new WebApplicationFactory(); - var httpClient = factory.CreateClient(); - var contentFormat = "application/xml"; - var request = CreateRequest(HttpMethod.Get, "/api/employees", contentFormat); + var response = await httpClient.SendAsync(request); + var receivedEmployee = JsonSerializer.Deserialize(await response.Content.ReadAsStringAsync(), JsonOptions); - var response = await httpClient.SendAsync(request); + Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); + Assert.AreEqual(newEmployee.Id, receivedEmployee?.Id); + } - Assert.AreEqual(contentFormat, response?.Content?.Headers?.ContentType?.MediaType); - } + [TestMethod] + public async Task WhenGetEndPointIsInvokedWithAcceptHeaderSetAsJson_ResponseIsReturnedInJsonFormat() + { + using var factory = new WebApplicationFactory(); + var httpClient = factory.CreateClient(); + var contentFormat = "application/json"; + var request = CreateRequest(HttpMethod.Get, "/api/employees", contentFormat); - [TestMethod] - public async Task WhenGetEndPointIsInvokedWithAcceptHeaderSetAsProtobuf_ResponseIsReturnedInProtoFormat() - { - using var factory = new WebApplicationFactory(); - var httpClient = factory.CreateClient(); - var contentFormat = "application/x-protobuf"; - var request = CreateRequest(HttpMethod.Get, "/api/employees", contentFormat); + var response = await httpClient.SendAsync(request); - var response = await httpClient.SendAsync(request); + Assert.AreEqual(contentFormat, response.Content.Headers.ContentType?.MediaType); + } - Assert.AreEqual(contentFormat, response?.Content?.Headers?.ContentType?.MediaType); - } + [TestMethod] + public async Task WhenGetEndPointIsInvokedWithAcceptHeaderSetAsXml_ResponseIsReturnedInXmlFormat() + { + using var factory = new WebApplicationFactory(); + var httpClient = factory.CreateClient(); + var contentFormat = "application/xml"; + var request = CreateRequest(HttpMethod.Get, "/api/employees", contentFormat); - [TestMethod] - public async Task WhenPostEndPointIsInvokedWithContentTypeHeaderSetAsJson_StatusCode201IsReturnedWithNewEmployee() - { - using var factory = new WebApplicationFactory(); - var httpClient = factory.CreateClient(); - var newEmployee = GetNewEmployee(); - var request = CreateRequest(HttpMethod.Post, "api/employees", "application/json"); - request.Content = new StringContent(JsonConvert.SerializeObject(newEmployee), Encoding.UTF8, "application/json"); - - var response = await httpClient.SendAsync(request); - var receivedEmployee = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); - - Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); - Assert.AreEqual(newEmployee.Id, receivedEmployee.Id); - } + var response = await httpClient.SendAsync(request); - [TestMethod] - public async Task WhenPostEndPointIsInvokedWithContentTypeHeaderSetAsXml_StatusCode201IsReturnedWithNewEmployee() - { - using var factory = new WebApplicationFactory(); - var httpClient = factory.CreateClient(); - var newEmployee = GetNewEmployee(); - var request = CreateRequest(HttpMethod.Post, "api/employees", "application/json"); - request.Content = new StringContent(XmlSerialize(newEmployee), Encoding.UTF8, "application/xml"); + Assert.AreEqual(contentFormat, response.Content.Headers.ContentType?.MediaType); + } - var response = await httpClient.SendAsync(request); - var receivedEmployee = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); + [TestMethod] + public async Task WhenGetEndPointIsInvokedWithAcceptHeaderSetAsProtobuf_ResponseIsReturnedInProtoFormat() + { + using var factory = new WebApplicationFactory(); + var httpClient = factory.CreateClient(); + var contentFormat = "application/x-protobuf"; + var request = CreateRequest(HttpMethod.Get, "/api/employees", contentFormat); - Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); - Assert.AreEqual(newEmployee.Id, receivedEmployee.Id); - } + var response = await httpClient.SendAsync(request); - [TestMethod] - public async Task WhenPostEndPointIsInvokedWithContentTypeHeaderSetAsProtobuf_StatusCode201IsReturnedWithNewEmployee() - { - using var factory = new WebApplicationFactory(); - var httpClient = factory.CreateClient(); - var newEmployee = GetNewEmployee(); - var request = CreateRequest(HttpMethod.Post, "api/employees", "application/json"); - using (MemoryStream stream = new MemoryStream()) - { - ProtoBuf.Serializer.Serialize(stream, newEmployee); - request.Content = new ByteArrayContent(stream.ToArray()); - request.Content.Headers.Add("Content-Type", "application/x-protobuf"); - var response = await httpClient.SendAsync(request); - var receivedEmployee = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); - - Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); - Assert.AreEqual(newEmployee.Id, receivedEmployee.Id); - } - - } + Assert.AreEqual(contentFormat, response.Content.Headers.ContentType?.MediaType); + } - private Employee GetNewEmployee() - { - return new Employee - { - Id = Guid.NewGuid(), - Name = $"TestEmployee", - Address = new Address - { - AddressLine1 = $"Street", - AddressLine2 = $"District", - City = $"Sample City30", - Country = $"Sample Country22", - } - }; - } + [TestMethod] + public async Task WhenGetEndPointIsInvokedWithAcceptHeaderSetAsProtobuf_ResponseDeserializesToEmployees() + { + using var factory = new WebApplicationFactory(); + var httpClient = factory.CreateClient(); + var request = CreateRequest(HttpMethod.Get, "/api/employees", "application/x-protobuf"); - private HttpRequestMessage CreateRequest(HttpMethod verb, string url, string format) - { - var request = new HttpRequestMessage(verb, url); - request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(format)); + var response = await httpClient.SendAsync(request); + using var stream = await response.Content.ReadAsStreamAsync(); + var employees = ProtoBuf.Serializer.Deserialize>(stream); - return request; - } - private string XmlSerialize(object data) - { - var serializer = new XmlSerializer(data.GetType()); + Assert.AreEqual(5, employees.Count); + Assert.AreEqual("TestEmployee1", employees[0].Name); + } - using (var stream = new Utf8StringWriter()) - { - serializer.Serialize(stream, data); + [TestMethod] + public async Task WhenPostEndPointIsInvokedWithContentTypeHeaderSetAsXml_StatusCode201IsReturnedWithNewEmployee() + { + using var factory = new WebApplicationFactory(); + var httpClient = factory.CreateClient(); + var newEmployee = GetNewEmployee(); + var request = CreateRequest(HttpMethod.Post, "api/employees", "application/json"); + request.Content = new StringContent(XmlSerialize(newEmployee), Encoding.UTF8, "application/xml"); - return stream.ToString(); - } - } + var response = await httpClient.SendAsync(request); + var receivedEmployee = JsonSerializer.Deserialize(await response.Content.ReadAsStringAsync(), JsonOptions); + + Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); + Assert.AreEqual(newEmployee.Id, receivedEmployee?.Id); + } - private class Utf8StringWriter : StringWriter + [TestMethod] + public async Task WhenPostEndPointIsInvokedWithContentTypeHeaderSetAsProtobuf_StatusCode201IsReturnedWithNewEmployee() + { + using var factory = new WebApplicationFactory(); + var httpClient = factory.CreateClient(); + var newEmployee = GetNewEmployee(); + var request = CreateRequest(HttpMethod.Post, "api/employees", "application/json"); + using var stream = new MemoryStream(); + ProtoBuf.Serializer.Serialize(stream, newEmployee); + request.Content = new ByteArrayContent(stream.ToArray()); + request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-protobuf"); + + var response = await httpClient.SendAsync(request); + var receivedEmployee = JsonSerializer.Deserialize(await response.Content.ReadAsStringAsync(), JsonOptions); + + Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); + Assert.AreEqual(newEmployee.Id, receivedEmployee?.Id); + } + + private static Employee GetNewEmployee() => new() + { + Id = Guid.NewGuid(), + Name = "TestEmployee", + Address = new Address { - public override Encoding Encoding => Encoding.UTF8; + AddressLine1 = "Street", + AddressLine2 = "District", + City = "Sample City30", + Country = "Sample Country22", } + }; + + private static HttpRequestMessage CreateRequest(HttpMethod verb, string url, string format) + { + var request = new HttpRequestMessage(verb, url); + request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(format)); + + return request; + } + + private static string XmlSerialize(object data) + { + var serializer = new XmlSerializer(data.GetType()); + + using var writer = new Utf8StringWriter(); + serializer.Serialize(writer, data); + + return writer.ToString(); + } + + private class Utf8StringWriter : StringWriter + { + public override Encoding Encoding => Encoding.UTF8; } } diff --git a/aspnetcore-webapi/SerializationDemo/Tests/JsonSerializationTests.cs b/aspnetcore-webapi/SerializationDemo/Tests/JsonSerializationTests.cs index c4ba229a74..a4a3bab7bb 100644 --- a/aspnetcore-webapi/SerializationDemo/Tests/JsonSerializationTests.cs +++ b/aspnetcore-webapi/SerializationDemo/Tests/JsonSerializationTests.cs @@ -1,55 +1,54 @@ -using NUnit.Framework; -using SerializationDemo.Client.Serialization.Json; -using System; +using SerializationDemo.Client.Serialization.Json; -namespace Tests +namespace Tests; + +[TestClass] +public class JsonSerializationTests { - [TestFixture] - public class JsonSerializationTests + [TestMethod] + public void JsonStringSerializationFollowedByDeserialization_ReturnsSameObject() { - [Test] - public void JsonStringSerializationFollowedByDeserialization_ReturnsSameObject() - { - var expected = new TestClass - { - Flag = true, - Text = "Test String", - Timestamp = DateTime.UtcNow - }; - var serializer = new JsonStringSerializer(); - - var obj = serializer.Deserialize(serializer.Serialize(expected)); - - Assert.AreEqual(expected.Text, obj.Text); - Assert.AreEqual(expected.Flag, obj.Flag); - Assert.AreEqual(expected.Timestamp, obj.Timestamp); - } - - [Test] - public void JsonByteSerializationFollowedByDeserialization_ReturnsSameObject() + var expected = new TestClass { - var expected = new TestClass - { - Flag = true, - Text = "Test String", - Timestamp = DateTime.UtcNow - }; - var serializer = new JsonStringSerializer(); - - var obj = serializer.Deserialize(serializer.Serialize(expected)); - - Assert.AreEqual(expected.Text, obj.Text); - Assert.AreEqual(expected.Flag, obj.Flag); - Assert.AreEqual(expected.Timestamp, obj.Timestamp); - } - - private class TestClass + Flag = true, + Text = "Test String", + Timestamp = DateTime.UtcNow + }; + var serializer = new JsonStringSerializer(); + + var obj = serializer.Deserialize(serializer.Serialize(expected)); + + Assert.IsNotNull(obj); + Assert.AreEqual(expected.Text, obj.Text); + Assert.AreEqual(expected.Flag, obj.Flag); + Assert.AreEqual(expected.Timestamp, obj.Timestamp); + } + + [TestMethod] + public void JsonByteSerializationFollowedByDeserialization_ReturnsSameObject() + { + var expected = new TestClass { - public DateTime Timestamp { get; set; } + Flag = true, + Text = "Test String", + Timestamp = DateTime.UtcNow + }; + var serializer = new JsonByteSerializer(); + + var obj = serializer.Deserialize(serializer.Serialize(expected)); + + Assert.IsNotNull(obj); + Assert.AreEqual(expected.Text, obj.Text); + Assert.AreEqual(expected.Flag, obj.Flag); + Assert.AreEqual(expected.Timestamp, obj.Timestamp); + } + + private class TestClass + { + public DateTime Timestamp { get; set; } - public string Text { get; set; } + public string Text { get; set; } = string.Empty; - public bool Flag { get; set; } - } - } -} \ No newline at end of file + public bool Flag { get; set; } + } +} diff --git a/aspnetcore-webapi/SerializationDemo/Tests/ProtobufSerializerTests.cs b/aspnetcore-webapi/SerializationDemo/Tests/ProtobufSerializerTests.cs index 6828c4e194..8159751143 100644 --- a/aspnetcore-webapi/SerializationDemo/Tests/ProtobufSerializerTests.cs +++ b/aspnetcore-webapi/SerializationDemo/Tests/ProtobufSerializerTests.cs @@ -1,42 +1,40 @@ -using NUnit.Framework; -using ProtoBuf; -using System; +using ProtoBuf; using SerializationDemo.Client.Serialization.Protobuf; -namespace Tests +namespace Tests; + +[TestClass] +public class ProtobufSerializerTests { - [TestFixture] - public class ProtobufSerializerTests + [TestMethod] + public void ProtobufSerializationFollowedByDeserialization_ReturnsSameObject() { - [Test] - public void ProtobufSerializationFollowedByDeserialization_ReturnsSameObject() - { - var expected = new TestClass - { - Flag = true, - Text = "Test String", - Timestamp = DateTime.UtcNow - }; - var serializer = new ProtobufSerializer(); - - var obj = serializer.Deserialize(serializer.Serialize(expected)); - - Assert.AreEqual(expected.Text, obj.Text); - Assert.AreEqual(expected.Flag, obj.Flag); - Assert.AreEqual(expected.Timestamp, obj.Timestamp); - } - - [ProtoContract] - private class TestClass + var expected = new TestClass { - [ProtoMember(1)] - public DateTime Timestamp { get; set; } + Flag = true, + Text = "Test String", + Timestamp = DateTime.UtcNow + }; + var serializer = new ProtobufSerializer(); + + var obj = serializer.Deserialize(serializer.Serialize(expected)); + + Assert.IsNotNull(obj); + Assert.AreEqual(expected.Text, obj.Text); + Assert.AreEqual(expected.Flag, obj.Flag); + Assert.AreEqual(expected.Timestamp, obj.Timestamp); + } + + [ProtoContract] + private class TestClass + { + [ProtoMember(1)] + public DateTime Timestamp { get; set; } - [ProtoMember(2)] - public string Text { get; set; } + [ProtoMember(2)] + public string Text { get; set; } = string.Empty; - [ProtoMember(3)] - public bool Flag { get; set; } - } - } + [ProtoMember(3)] + public bool Flag { get; set; } + } } diff --git a/aspnetcore-webapi/SerializationDemo/Tests/Tests.csproj b/aspnetcore-webapi/SerializationDemo/Tests/Tests.csproj index 8a0197b102..0b07a2ad09 100644 --- a/aspnetcore-webapi/SerializationDemo/Tests/Tests.csproj +++ b/aspnetcore-webapi/SerializationDemo/Tests/Tests.csproj @@ -1,22 +1,17 @@ - net6.0 + net11.0 enable - + enable false - - - - - - - - - + + + + diff --git a/aspnetcore-webapi/SerializationDemo/Tests/XmlSerializerTests.cs b/aspnetcore-webapi/SerializationDemo/Tests/XmlSerializerTests.cs index 954ececbaa..5c93a5209f 100644 --- a/aspnetcore-webapi/SerializationDemo/Tests/XmlSerializerTests.cs +++ b/aspnetcore-webapi/SerializationDemo/Tests/XmlSerializerTests.cs @@ -1,38 +1,35 @@ -using NUnit.Framework; -using SerializationDemo.Client.Serialization.Xml; -using System; +using SerializationDemo.Client.Serialization.Xml; +namespace Tests; -namespace Tests +[TestClass] +public class XmlSerializerTests { - [TestFixture] - public class XmlSerializerTests + [TestMethod] + public void XmlSerializationFollowedByDeserialization_ReturnsSameObject() { - [Test] - public void XmlSerializationFollowedByDeserialization_ReturnsSameObject() + var expected = new TestClass { - var expected = new TestClass - { - Flag = true, - Text = "Test String", - Timestamp = DateTime.UtcNow - }; - var serializer = new XmlSerializer(); + Flag = true, + Text = "Test String", + Timestamp = DateTime.UtcNow + }; + var serializer = new XmlSerializer(); - var obj = serializer.Deserialize(serializer.Serialize(expected)); + var obj = serializer.Deserialize(serializer.Serialize(expected)); - Assert.AreEqual(expected.Text, obj.Text); - Assert.AreEqual(expected.Flag, obj.Flag); - Assert.AreEqual(expected.Timestamp, obj.Timestamp); - } + Assert.IsNotNull(obj); + Assert.AreEqual(expected.Text, obj.Text); + Assert.AreEqual(expected.Flag, obj.Flag); + Assert.AreEqual(expected.Timestamp, obj.Timestamp); + } - public class TestClass - { - public DateTime Timestamp { get; set; } + public class TestClass + { + public DateTime Timestamp { get; set; } - public string Text { get; set; } + public string Text { get; set; } = string.Empty; - public bool Flag { get; set; } - } + public bool Flag { get; set; } } }