From bb5a02d8b12c8d0e167f1b5f36797d3de915e94e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=94?= =?UTF-8?q?=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Fri, 9 Feb 2024 09:24:55 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=B8=D0=BD=D1=84=D1=80=D0=B0=D1=81=D1=82=D1=80?= =?UTF-8?q?=D1=83=D0=BA=D1=82=D1=83=D1=80=D1=8B=20=D0=B8=D0=BD=D1=82=D0=B5?= =?UTF-8?q?=D0=B3=D1=80=D0=B0=D1=86=D0=B8=D0=BE=D0=BD=D0=BD=D1=8B=D1=85=20?= =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AssemblyExtensions.cs | 25 +++++++ .../Converters/ValidationResultConverter.cs | 68 +++++++++++++++++++ .../WebAppFactoryFixture.cs | 4 +- 3 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 AsbCloudWebApi.IntegrationTests/AssemblyExtensions.cs create mode 100644 AsbCloudWebApi.IntegrationTests/Converters/ValidationResultConverter.cs diff --git a/AsbCloudWebApi.IntegrationTests/AssemblyExtensions.cs b/AsbCloudWebApi.IntegrationTests/AssemblyExtensions.cs new file mode 100644 index 00000000..d4013aa1 --- /dev/null +++ b/AsbCloudWebApi.IntegrationTests/AssemblyExtensions.cs @@ -0,0 +1,25 @@ +using System.Reflection; + +namespace AsbCloudWebApi.IntegrationTests; + +internal static class AssemblyExtensions +{ + internal static Stream GetFileCopyStream(this Assembly assembly, string templateName) + { + var resourceName = assembly + .GetManifestResourceNames() + .FirstOrDefault(n => n.EndsWith(templateName)); + + if (string.IsNullOrWhiteSpace(resourceName)) + throw new ArgumentNullException(nameof(resourceName)); + + using var stream = Assembly.GetExecutingAssembly() + .GetManifestResourceStream(resourceName); + + var memoryStream = new MemoryStream(); + stream?.CopyTo(memoryStream); + memoryStream.Position = 0; + + return memoryStream; + } +} \ No newline at end of file diff --git a/AsbCloudWebApi.IntegrationTests/Converters/ValidationResultConverter.cs b/AsbCloudWebApi.IntegrationTests/Converters/ValidationResultConverter.cs new file mode 100644 index 00000000..15423bc0 --- /dev/null +++ b/AsbCloudWebApi.IntegrationTests/Converters/ValidationResultConverter.cs @@ -0,0 +1,68 @@ +using System.ComponentModel.DataAnnotations; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace AsbCloudWebApi.IntegrationTests.Converters; + +public class ValidationResultConverter : JsonConverter +{ + public override ValidationResult Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + if (reader.TokenType != JsonTokenType.StartObject) + { + throw new JsonException("Expected the start of an object."); + } + + string? errorMessage = null; + List? memberNames = null; + + while (reader.Read()) + { + if (reader.TokenType == JsonTokenType.EndObject) + { + break; + } + + if (reader.TokenType != JsonTokenType.PropertyName) + { + throw new JsonException($"Unexpected token type: {reader.TokenType}"); + } + + var propertyName = reader.GetString(); + reader.Read(); + + switch (propertyName) + { + case "errorMessage": + errorMessage = reader.GetString(); + break; + case "memberNames": + if (reader.TokenType != JsonTokenType.StartArray) + { + throw new JsonException("Expected the start of an array for 'memberNames'."); + } + memberNames = new List(); + while (reader.Read() && reader.TokenType != JsonTokenType.EndArray) + { + memberNames.Add(reader.GetString() ?? string.Empty); + } + break; + default: + reader.Skip(); + break; + } + } + + if (errorMessage == null) + { + throw new JsonException("Missing 'errorMessage' property."); + } + + return new ValidationResult(errorMessage, memberNames ?? Enumerable.Empty()); + } + + public override void Write(Utf8JsonWriter writer, ValidationResult value, JsonSerializerOptions options) + { + throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/AsbCloudWebApi.IntegrationTests/WebAppFactoryFixture.cs b/AsbCloudWebApi.IntegrationTests/WebAppFactoryFixture.cs index af7303b6..aa8bd27e 100644 --- a/AsbCloudWebApi.IntegrationTests/WebAppFactoryFixture.cs +++ b/AsbCloudWebApi.IntegrationTests/WebAppFactoryFixture.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.DependencyInjection; using Refit; using System.Net.Http.Headers; using System.Text.Json; +using AsbCloudWebApi.IntegrationTests.Converters; using Xunit; namespace AsbCloudWebApi.IntegrationTests; @@ -18,7 +19,8 @@ public class WebAppFactoryFixture : WebApplicationFactory, private static readonly JsonSerializerOptions jsonSerializerOptions = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, - PropertyNameCaseInsensitive = true + PropertyNameCaseInsensitive = true, + Converters = { new ValidationResultConverter() } }; private static readonly RefitSettings refitSettings = new RefitSettings(new SystemTextJsonContentSerializer(jsonSerializerOptions));