// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschraenkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using System.Runtime.Serialization.Formatters.Binary; using System.Security.Claims; using System.Text.Json; using System.Text.Json.Serialization; using MongoDB.Bson.IO; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Attributes; using NetTopologySuite.IO.Converters; using NodaTime; using NodaTime.Serialization.SystemTextJson; using Squidex.Domain.Apps.Core.Apps; using Squidex.Domain.Apps.Core.Apps.Json; using Squidex.Domain.Apps.Core.Contents; using Squidex.Domain.Apps.Core.Contents.Json; using Squidex.Domain.Apps.Core.HandleRules; using Squidex.Domain.Apps.Core.Rules; using Squidex.Domain.Apps.Core.Rules.Json; using Squidex.Domain.Apps.Core.Schemas; using Squidex.Domain.Apps.Core.Schemas.Json; using Squidex.Infrastructure; using Squidex.Infrastructure.EventSourcing; using Squidex.Infrastructure.Json; using Squidex.Infrastructure.Json.Objects; using Squidex.Infrastructure.Json.System; using Squidex.Infrastructure.MongoDb; using Squidex.Infrastructure.Queries; using Squidex.Infrastructure.Queries.Json; using Squidex.Infrastructure.Reflection; #pragma warning disable SYSLIB0011 // Type or member is obsolete namespace Squidex.Domain.Apps.Core.TestHelpers { public static class TestUtils { public static readonly IJsonSerializer DefaultSerializer = CreateSerializer(); public sealed class ObjectHolder { [BsonRequired] public T Value1 { get; set; } [BsonRequired] public T Value2 { get; set; } } static TestUtils() { SetupBson(); } public static void SetupBson() { BsonDomainIdSerializer.Register(); BsonInstantSerializer.Register(); BsonJsonConvention.Register(DefaultOptions()); BsonJsonValueSerializer.Register(); } public static IJsonSerializer CreateSerializer(Action? configure = null) { var serializerSettings = DefaultOptions(configure); return new SystemJsonSerializer(serializerSettings); } public static JsonSerializerOptions DefaultOptions(Action? configure = null) { var typeNameRegistry = new TypeNameRegistry() .Map(new FieldTypeProvider()) .Map(new RuleTypeProvider()) .MapUnmapped(typeof(TestUtils).Assembly); var options = new JsonSerializerOptions(JsonSerializerDefaults.Web); options.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb); // It is also a readonly list, so we have to register it first, so that other converters do not pick this up. options.Converters.Add(new StringConverter(x => x)); options.Converters.Add(new GeoJsonConverterFactory()); options.Converters.Add(new InheritanceConverter(typeNameRegistry)); options.Converters.Add(new InheritanceConverter(typeNameRegistry)); options.Converters.Add(new InheritanceConverter(typeNameRegistry)); options.Converters.Add(new InheritanceConverter(typeNameRegistry)); options.Converters.Add(new JsonValueConverter()); options.Converters.Add(new ReadonlyDictionaryConverterFactory()); options.Converters.Add(new ReadonlyListConverterFactory()); options.Converters.Add(new SurrogateJsonConverter()); options.Converters.Add(new SurrogateJsonConverter, JsonFilterSurrogate>()); options.Converters.Add(new SurrogateJsonConverter()); options.Converters.Add(new SurrogateJsonConverter()); options.Converters.Add(new SurrogateJsonConverter()); options.Converters.Add(new SurrogateJsonConverter()); options.Converters.Add(new SurrogateJsonConverter()); options.Converters.Add(new SurrogateJsonConverter()); options.Converters.Add(new SurrogateJsonConverter()); options.Converters.Add(new StringConverter()); options.Converters.Add(new StringConverter()); options.Converters.Add(new StringConverter>()); options.Converters.Add(new StringConverter>()); options.Converters.Add(new StringConverter>()); options.Converters.Add(new StringConverter>()); options.Converters.Add(new StringConverter()); options.Converters.Add(new StringConverter()); options.Converters.Add(new StringConverter()); options.Converters.Add(new JsonStringEnumConverter()); configure?.Invoke(options); return options; } public static T SerializeAndDeserializeBinary(this T source) { using (var stream = new MemoryStream()) { var formatter = new BinaryFormatter(); formatter.Serialize(stream, source!); stream.Position = 0; return (T)formatter.Deserialize(stream); } } public static T SerializeAndDeserializeBson(this T value) { using var stream = new MemoryStream(); using (var writer = new BsonBinaryWriter(stream)) { BsonSerializer.Serialize(writer, new ObjectHolder { Value1 = value, Value2 = value }); } stream.Position = 0; using (var reader = new BsonBinaryReader(stream)) { return BsonSerializer.Deserialize>(reader).Value1; } } public static T SerializeAndDeserialize(this object value) { var json = DefaultSerializer.Serialize(value); return DefaultSerializer.Deserialize(json); } public static T SerializeAndDeserialize(this T value) { var json = DefaultSerializer.Serialize(new ObjectHolder { Value1 = value, Value2 = value }); return DefaultSerializer.Deserialize>(json).Value1; } public static T Deserialize(string value) { var json = DefaultSerializer.Serialize(new ObjectHolder { Value1 = value, Value2 = value }); return DefaultSerializer.Deserialize>(json).Value1; } public static T Deserialize(object value) { var json = DefaultSerializer.Serialize(new ObjectHolder { Value1 = value, Value2 = value }); return DefaultSerializer.Deserialize>(json).Value1; } public static string CleanJson(this string json) { using var document = JsonDocument.Parse(json); return DefaultSerializer.Serialize(document, true); } } }