Доработка инфраструктуры интеграционных тестов

This commit is contained in:
Степанов Дмитрий 2024-02-09 09:24:55 +03:00
parent 1b3c06c927
commit bb5a02d8b1
3 changed files with 96 additions and 1 deletions

View File

@ -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;
}
}

View File

@ -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<ValidationResult>
{
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<string>? 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<string>();
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<string>());
}
public override void Write(Utf8JsonWriter writer, ValidationResult value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}

View File

@ -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<Startup>,
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));