Browse Source

No static event type registry anymore

pull/1/head
Sebastian 9 years ago
parent
commit
d9746d5b53
  1. 15
      src/Squidex.Core/Schemas/FieldRegistry.cs
  2. 11
      src/Squidex.Infrastructure/CQRS/Events/EventDataFormatter.cs
  3. 13
      src/Squidex.Infrastructure/Json/TypeNameSerializationBinder.cs
  4. 28
      src/Squidex.Infrastructure/TypeNameRegistry.cs
  5. 10
      src/Squidex.Read.MongoDb/History/MongoHistoryEventEntity.cs
  6. 85
      src/Squidex.Read/Apps/AppHistoryEventsCreator.cs
  7. 11
      src/Squidex.Read/History/HistoryEventToStore.cs
  8. 50
      src/Squidex.Read/History/HistoryEventsCreatorBase.cs
  9. 54
      src/Squidex.Read/Schemas/SchemaHistoryEventsCreator.cs
  10. 13
      src/Squidex/Config/Domain/Serializers.cs
  11. 2
      src/Squidex/app/theme/_bootstrap.scss
  12. 11
      tests/Squidex.Core.Tests/Schemas/FieldRegistryTests.cs
  13. 17
      tests/Squidex.Core.Tests/Schemas/Json/JsonSerializerTests.cs
  14. 2
      tests/Squidex.Infrastructure.Tests/CQRS/Commands/DefaultDomainObjectRepositoryTests.cs
  15. 12
      tests/Squidex.Infrastructure.Tests/CQRS/Events/EventDataFormatterTests.cs
  16. 3
      tests/Squidex.Infrastructure.Tests/CQRS/Events/EventReceiverTests.cs
  17. 50
      tests/Squidex.Infrastructure.Tests/TypeNameRegistryTests.cs
  18. 3
      tests/Squidex.Write.Tests/EnrichWithSchemaIdProcessorTests.cs
  19. 8
      tests/Squidex.Write.Tests/Schemas/SchemaCommandHandlerTests.cs
  20. 2
      tests/Squidex.Write.Tests/Schemas/SchemaDomainObjectTests.cs
  21. 28
      tests/Squidex.Write.Tests/Utils/SchemaFixture.cs

15
src/Squidex.Core/Schemas/FieldRegistry.cs

@ -16,6 +16,7 @@ namespace Squidex.Core.Schemas
public sealed class FieldRegistry
{
private readonly TypeNameRegistry typeNameRegistry;
private readonly Dictionary<string, IRegisteredField> fieldsByTypeName = new Dictionary<string, IRegisteredField>();
private readonly Dictionary<Type, IRegisteredField> fieldsByPropertyType = new Dictionary<Type, IRegisteredField>();
@ -35,9 +36,9 @@ namespace Squidex.Core.Schemas
get { return typeName; }
}
public Registered(FactoryFunction fieldFactory, Type propertiesType)
public Registered(FactoryFunction fieldFactory, Type propertiesType, TypeNameRegistry typeNameRegistry)
{
typeName = TypeNameRegistry.GetName(propertiesType);
typeName = typeNameRegistry.GetName(propertiesType);
this.fieldFactory = fieldFactory;
this.propertiesType = propertiesType;
@ -49,8 +50,12 @@ namespace Squidex.Core.Schemas
}
}
public FieldRegistry()
public FieldRegistry(TypeNameRegistry typeNameRegistry)
{
Guard.NotNull(typeNameRegistry, nameof(typeNameRegistry));
this.typeNameRegistry = typeNameRegistry;
Add<BooleanFieldProperties>(
(id, name, p) => new BooleanField(id, name, (BooleanFieldProperties)p));
@ -64,8 +69,10 @@ namespace Squidex.Core.Schemas
public void Add<TFieldProperties>(FactoryFunction fieldFactory)
{
Guard.NotNull(fieldFactory, nameof(fieldFactory));
typeNameRegistry.Map(typeof(TFieldProperties));
var registered = new Registered(fieldFactory, typeof(TFieldProperties));
var registered = new Registered(fieldFactory, typeof(TFieldProperties), typeNameRegistry);
fieldsByTypeName[registered.TypeName] = registered;
fieldsByPropertyType[registered.PropertiesType] = registered;

11
src/Squidex.Infrastructure/CQRS/Events/EventDataFormatter.cs

@ -15,9 +15,14 @@ namespace Squidex.Infrastructure.CQRS.Events
public class EventDataFormatter
{
private readonly JsonSerializerSettings serializerSettings;
private readonly TypeNameRegistry typeNameRegistry;
public EventDataFormatter(JsonSerializerSettings serializerSettings = null)
public EventDataFormatter(TypeNameRegistry typeNameRegistry, JsonSerializerSettings serializerSettings = null)
{
Guard.NotNull(typeNameRegistry, nameof(typeNameRegistry));
this.typeNameRegistry = typeNameRegistry;
this.serializerSettings = serializerSettings ?? new JsonSerializerSettings();
}
@ -25,7 +30,7 @@ namespace Squidex.Infrastructure.CQRS.Events
{
var headers = ReadJson<PropertiesBag>(eventData.Metadata);
var eventType = TypeNameRegistry.GetType(eventData.Type);
var eventType = typeNameRegistry.GetType(eventData.Type);
var eventContent = ReadJson<IEvent>(eventData.Payload, eventType);
var envelope = new Envelope<IEvent>(eventContent, headers);
@ -35,7 +40,7 @@ namespace Squidex.Infrastructure.CQRS.Events
public virtual EventData ToEventData(Envelope<IEvent> envelope, Guid commitId)
{
var eventType = TypeNameRegistry.GetName(envelope.Payload.GetType());
var eventType = typeNameRegistry.GetName(envelope.Payload.GetType());
envelope.SetCommitId(commitId);

13
src/Squidex.Infrastructure/Json/TypeNameSerializationBinder.cs

@ -13,9 +13,18 @@ namespace Squidex.Infrastructure.Json
{
public class TypeNameSerializationBinder : DefaultSerializationBinder
{
private readonly TypeNameRegistry typeNameRegistry;
public TypeNameSerializationBinder(TypeNameRegistry typeNameRegistry)
{
Guard.NotNull(typeNameRegistry, nameof(typeNameRegistry));
this.typeNameRegistry = typeNameRegistry;
}
public override Type BindToType(string assemblyName, string typeName)
{
var type = TypeNameRegistry.GetType(typeName);
var type = typeNameRegistry.GetType(typeName);
return type ?? base.BindToType(assemblyName, typeName);
}
@ -24,7 +33,7 @@ namespace Squidex.Infrastructure.Json
{
assemblyName = null;
var name = TypeNameRegistry.GetName(serializedType);
var name = typeNameRegistry.GetName(serializedType);
if (name != null)
{

28
src/Squidex.Infrastructure/TypeNameRegistry.cs

@ -12,12 +12,24 @@ using System.Reflection;
namespace Squidex.Infrastructure
{
public class TypeNameRegistry
public sealed class TypeNameRegistry
{
private static readonly Dictionary<Type, string> namesByType = new Dictionary<Type, string>();
private static readonly Dictionary<string, Type> typesByName = new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<Type, string> namesByType = new Dictionary<Type, string>();
private readonly Dictionary<string, Type> typesByName = new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase);
public static void Map(Type type, string name)
public void Map(Type type)
{
Guard.NotNull(type, nameof(type));
var typeNameAttribute = type.GetTypeInfo().GetCustomAttribute<TypeNameAttribute>();
if (typeNameAttribute != null)
{
Map(type, typeNameAttribute.TypeName);
}
}
public void Map(Type type, string name)
{
Guard.NotNull(type, nameof(type));
Guard.NotNull(name, nameof(name));
@ -54,7 +66,7 @@ namespace Squidex.Infrastructure
}
}
public static void Map(Assembly assembly)
public void Map(Assembly assembly)
{
foreach (var type in assembly.GetTypes())
{
@ -67,12 +79,12 @@ namespace Squidex.Infrastructure
}
}
public static string GetName<T>()
public string GetName<T>()
{
return GetName(typeof(T));
}
public static string GetName(Type type)
public string GetName(Type type)
{
var result = namesByType.GetOrDefault(type);
@ -84,7 +96,7 @@ namespace Squidex.Infrastructure
return result;
}
public static Type GetType(string name)
public Type GetType(string name)
{
var result = typesByName.GetOrDefault(name);

10
src/Squidex.Read.MongoDb/History/MongoHistoryEventEntity.cs

@ -10,7 +10,6 @@ using System;
using System.Collections.Generic;
using MongoDB.Bson.Serialization.Attributes;
using Squidex.Infrastructure;
using Squidex.Infrastructure.CQRS;
using Squidex.Infrastructure.MongoDb;
namespace Squidex.Read.MongoDb.History
@ -58,15 +57,6 @@ namespace Squidex.Read.MongoDb.History
Parameters = new Dictionary<string, string>();
}
public MongoHistoryEventEntity Setup<T>(EnvelopeHeaders headers, string channel)
{
Channel = channel;
Message = TypeNameRegistry.GetName<T>();
return this;
}
public MongoHistoryEventEntity AddParameter(string key, string value)
{
Parameters.Add(key, value);

85
src/Squidex.Read/Apps/AppHistoryEventsCreator.cs

@ -6,7 +6,6 @@
// All rights reserved.
// ==========================================================================
using System.Collections.Generic;
using System.Threading.Tasks;
using Squidex.Events.Apps;
using Squidex.Infrastructure;
@ -17,48 +16,34 @@ using Squidex.Read.History;
namespace Squidex.Read.Apps
{
public class AppHistoryEventsCreator : IHistoryEventsCreator
public class AppHistoryEventsCreator : HistoryEventsCreatorBase
{
private static readonly IReadOnlyDictionary<string, string> TextsEN =
new Dictionary<string, string>
{
{
TypeNameRegistry.GetName<AppContributorAssigned>(),
"assigned {user:[Contributor]} as [Permission]"
},
{
TypeNameRegistry.GetName<AppContributorRemoved>(),
"removed {user:[Contributor]} from app"
},
{
TypeNameRegistry.GetName<AppClientAttached>(),
"added client {[Id]} to app"
},
{
TypeNameRegistry.GetName<AppClientRevoked>(),
"revoked client {[Id]}"
},
{
TypeNameRegistry.GetName<AppClientRenamed>(),
"named client {[Id]} as {[Name]}"
},
{
TypeNameRegistry.GetName<AppLanguageAdded>(),
"added language {[Language]}"
},
{
TypeNameRegistry.GetName<AppLanguageRemoved>(),
"removed language {[Language]}"
},
{
TypeNameRegistry.GetName<AppMasterLanguageSet>(),
"changed master language to {[Language]}"
}
};
public IReadOnlyDictionary<string, string> Texts
public AppHistoryEventsCreator(TypeNameRegistry typeNameRegistry)
: base(typeNameRegistry)
{
get { return TextsEN; }
AddEventMessage<AppContributorAssigned>(
"assigned {user:[Contributor]} as [Permission]");
AddEventMessage<AppContributorRemoved>(
"removed {user:[Contributor]} from app");
AddEventMessage<AppClientAttached>(
"added client {[Id]} to app");
AddEventMessage<AppClientRevoked>(
"revoked client {[Id]}");
AddEventMessage<AppClientRenamed>(
"named client {[Id]} as {[Name]}");
AddEventMessage<AppLanguageAdded>(
"added language {[Language]}");
AddEventMessage<AppLanguageRemoved>(
"removed language {[Language]}");
AddEventMessage<AppMasterLanguageSet>(
"changed master language to {[Language]}");
}
protected Task<HistoryEventToStore> On(AppContributorAssigned @event, EnvelopeHeaders headers)
@ -66,7 +51,7 @@ namespace Squidex.Read.Apps
const string channel = "settings.contributors";
return Task.FromResult(
HistoryEventToStore.Create(@event, channel)
ForEvent(@event, channel)
.AddParameter("Contributor", @event.ContributorId)
.AddParameter("Permission", @event.Permission.ToString()));
}
@ -76,7 +61,7 @@ namespace Squidex.Read.Apps
const string channel = "settings.contributors";
return Task.FromResult(
HistoryEventToStore.Create(@event, channel)
ForEvent(@event, channel)
.AddParameter("Contributor", @event.ContributorId));
}
@ -85,7 +70,7 @@ namespace Squidex.Read.Apps
const string channel = "settings.clients";
return Task.FromResult(
HistoryEventToStore.Create(@event, channel)
ForEvent(@event, channel)
.AddParameter("Id", @event.Id)
.AddParameter("Name", !string.IsNullOrWhiteSpace(@event.Name) ? @event.Name : @event.Id));
}
@ -95,7 +80,7 @@ namespace Squidex.Read.Apps
const string channel = "settings.clients";
return Task.FromResult(
HistoryEventToStore.Create(@event, channel)
ForEvent(@event, channel)
.AddParameter("Id", @event.Id));
}
@ -104,7 +89,7 @@ namespace Squidex.Read.Apps
const string channel = "settings.clients";
return Task.FromResult(
HistoryEventToStore.Create(@event, channel)
ForEvent(@event, channel)
.AddParameter("Id", @event.Id));
}
@ -113,7 +98,7 @@ namespace Squidex.Read.Apps
const string channel = "settings.languages";
return Task.FromResult(
HistoryEventToStore.Create(@event, channel)
ForEvent(@event, channel)
.AddParameter("Language", @event.Language.EnglishName));
}
@ -122,7 +107,7 @@ namespace Squidex.Read.Apps
const string channel = "settings.languages";
return Task.FromResult(
HistoryEventToStore.Create(@event, channel)
ForEvent(@event, channel)
.AddParameter("Language", @event.Language.EnglishName));
}
@ -131,11 +116,11 @@ namespace Squidex.Read.Apps
const string channel = "settings.languages";
return Task.FromResult(
HistoryEventToStore.Create(@event, channel)
ForEvent(@event, channel)
.AddParameter("Language", @event.Language.EnglishName));
}
public Task<HistoryEventToStore> CreateEventAsync(Envelope<IEvent> @event)
public override Task<HistoryEventToStore> CreateEventAsync(Envelope<IEvent> @event)
{
return this.DispatchFuncAsync(@event.Payload, @event.Headers, (HistoryEventToStore)null);
}

11
src/Squidex.Read/History/HistoryEventToStore.cs

@ -8,11 +8,10 @@
using System.Collections.Generic;
using Squidex.Infrastructure;
using Squidex.Infrastructure.CQRS.Events;
namespace Squidex.Read.History
{
public class HistoryEventToStore
public sealed class HistoryEventToStore
{
private readonly Dictionary<string, string> parameters = new Dictionary<string, string>();
@ -25,19 +24,13 @@ namespace Squidex.Read.History
get { return parameters; }
}
public static HistoryEventToStore Create(IEvent @event, string channel)
{
Guard.NotNull(@event, nameof(@event));
return new HistoryEventToStore(channel, TypeNameRegistry.GetName(@event.GetType()));
}
public HistoryEventToStore(string channel, string message)
{
Guard.NotNullOrEmpty(channel, nameof(channel));
Guard.NotNullOrEmpty(message, nameof(message));
Channel = channel;
Message = message;
}

50
src/Squidex.Read/History/HistoryEventsCreatorBase.cs

@ -0,0 +1,50 @@
// ==========================================================================
// HistoryEventCreatorBase.cs
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex Group
// All rights reserved.
// ==========================================================================
using System.Collections.Generic;
using System.Threading.Tasks;
using Squidex.Infrastructure;
using Squidex.Infrastructure.CQRS;
using Squidex.Infrastructure.CQRS.Events;
namespace Squidex.Read.History
{
public abstract class HistoryEventsCreatorBase : IHistoryEventsCreator
{
private readonly Dictionary<string, string> texts = new Dictionary<string, string>();
private readonly TypeNameRegistry typeNameRegistry;
public IReadOnlyDictionary<string, string> Texts
{
get { return texts; }
}
protected HistoryEventsCreatorBase(TypeNameRegistry typeNameRegistry)
{
Guard.NotNull(typeNameRegistry, nameof(typeNameRegistry));
this.typeNameRegistry = typeNameRegistry;
}
public void AddEventMessage<TEvent>(string message) where TEvent : IEvent
{
Guard.NotNullOrEmpty(message, nameof(message));
texts[typeNameRegistry.GetName<TEvent>()] = message;
}
protected HistoryEventToStore ForEvent(IEvent @event, string channel)
{
var message = typeNameRegistry.GetName(@event.GetType());
return new HistoryEventToStore(channel, message);
}
public abstract Task<HistoryEventToStore> CreateEventAsync(Envelope<IEvent> @event);
}
}

54
src/Squidex.Read/Schemas/SchemaHistoryEventsCreator.cs

@ -6,7 +6,6 @@
// All rights reserved.
// ==========================================================================
using System.Collections.Generic;
using System.Threading.Tasks;
using Squidex.Events.Schemas;
using Squidex.Infrastructure;
@ -18,39 +17,28 @@ using Squidex.Read.Schemas.Services;
namespace Squidex.Read.Schemas
{
public class SchemaHistoryEventsCreator : IHistoryEventsCreator
public class SchemaHistoryEventsCreator : HistoryEventsCreatorBase
{
private readonly ISchemaProvider schemaProvider;
private static readonly IReadOnlyDictionary<string, string> TextsEN =
new Dictionary<string, string>
{
{
TypeNameRegistry.GetName<SchemaCreated>(),
"created schema {[Name]}"
},
{
TypeNameRegistry.GetName<SchemaUpdated>(),
"updated schema {[Name]}"
},
{
TypeNameRegistry.GetName<SchemaPublished>(),
"published schema {[Name]}"
},
{
TypeNameRegistry.GetName<SchemaUnpublished>(),
"unpublished schema {[Name]}"
}
};
public SchemaHistoryEventsCreator(ISchemaProvider schemaProvider)
public SchemaHistoryEventsCreator(TypeNameRegistry typeNameRegistry, ISchemaProvider schemaProvider)
: base(typeNameRegistry)
{
Guard.NotNull(schemaProvider, nameof(schemaProvider));
this.schemaProvider = schemaProvider;
}
public IReadOnlyDictionary<string, string> Texts
{
get { return TextsEN; }
AddEventMessage<SchemaCreated>(
"created schema {[Name]}");
AddEventMessage<SchemaUpdated>(
"updated schema {[Name]}");
AddEventMessage<SchemaPublished>(
"published schema {[Name]}");
AddEventMessage<SchemaUnpublished>(
"unpublished schema {[Name]}");
}
protected Task<HistoryEventToStore> On(SchemaCreated @event, EnvelopeHeaders headers)
@ -60,7 +48,7 @@ namespace Squidex.Read.Schemas
string channel = $"schemas.{name}";
return Task.FromResult(
HistoryEventToStore.Create(@event, channel)
ForEvent(@event, channel)
.AddParameter("Name", name));
}
@ -71,7 +59,7 @@ namespace Squidex.Read.Schemas
string channel = $"schemas.{name}";
return
HistoryEventToStore.Create(@event, channel)
ForEvent(@event, channel)
.AddParameter("Name", name);
}
@ -82,7 +70,7 @@ namespace Squidex.Read.Schemas
string channel = $"schemas.{name}";
return
HistoryEventToStore.Create(@event, channel)
ForEvent(@event, channel)
.AddParameter("Name", name);
}
@ -93,11 +81,11 @@ namespace Squidex.Read.Schemas
string channel = $"schemas.{name}";
return
HistoryEventToStore.Create(@event, channel)
ForEvent(@event, channel)
.AddParameter("Name", name);
}
public Task<HistoryEventToStore> CreateEventAsync(Envelope<IEvent> @event)
public override Task<HistoryEventToStore> CreateEventAsync(Envelope<IEvent> @event)
{
return this.DispatchFuncAsync(@event.Payload, @event.Headers, (HistoryEventToStore)null);
}

13
src/Squidex/Config/Domain/Serializers.cs

@ -10,7 +10,6 @@ using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Squidex.Core.Schemas;
using Squidex.Events;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Json;
@ -19,9 +18,10 @@ namespace Squidex.Config.Domain
{
public static class Serializers
{
private static readonly TypeNameRegistry typeNameRegistry = new TypeNameRegistry();
private static JsonSerializerSettings ConfigureJson(JsonSerializerSettings settings)
{
settings.SerializationBinder = new TypeNameSerializationBinder();
settings.SerializationBinder = new TypeNameSerializationBinder(typeNameRegistry);
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
settings.Converters.Add(new LanguageConverter());
settings.Converters.Add(new PropertiesBagConverter());
@ -34,6 +34,11 @@ namespace Squidex.Config.Domain
return settings;
}
static Serializers()
{
typeNameRegistry.Map(typeof(EventExtensions).GetTypeInfo().Assembly);
}
private static JsonSerializerSettings CreateSettings()
{
return ConfigureJson(new JsonSerializerSettings());
@ -46,9 +51,7 @@ namespace Squidex.Config.Domain
public static IServiceCollection AddMyEventFormatter(this IServiceCollection services)
{
TypeNameRegistry.Map(typeof(Schema).GetTypeInfo().Assembly);
TypeNameRegistry.Map(typeof(EventExtensions).GetTypeInfo().Assembly);
services.AddSingleton(t => typeNameRegistry);
services.AddSingleton(t => CreateSettings());
services.AddSingleton(t => CreateSerializer(t.GetRequiredService<JsonSerializerSettings>()));

2
src/Squidex/app/theme/_bootstrap.scss

@ -126,7 +126,7 @@ body {
&:disabled,
&.disabled {
cursor: not-allowed;
border: 0;
border-color: transparent;
}
&:focus,

11
tests/Squidex.Core.Tests/Schemas/FieldRegistryTests.cs

@ -15,7 +15,7 @@ namespace Squidex.Core.Schemas
{
public class FieldRegistryTests
{
private readonly FieldRegistry sut = new FieldRegistry();
private readonly FieldRegistry sut = new FieldRegistry(new TypeNameRegistry());
private sealed class InvalidProperties : FieldProperties
{
@ -24,14 +24,7 @@ namespace Squidex.Core.Schemas
yield break;
}
}
static FieldRegistryTests()
{
TypeNameRegistry.Map(typeof(BooleanFieldProperties), "BooleanField");
TypeNameRegistry.Map(typeof(NumberFieldProperties), "NumberField");
TypeNameRegistry.Map(typeof(StringFieldProperties), "StringField");
TypeNameRegistry.Map(typeof(InvalidProperties), "invalid");
}
[Fact]
public void Should_throw_if_creating_field_and_field_is_not_registered()

17
tests/Squidex.Core.Tests/Schemas/Json/JsonSerializerTests.cs

@ -6,7 +6,6 @@
// All rights reserved.
// ==========================================================================
using System.Reflection;
using FluentAssertions;
using Newtonsoft.Json;
using Squidex.Infrastructure;
@ -17,20 +16,16 @@ namespace Squidex.Core.Schemas.Json
{
public class JsonSerializerTests
{
private static readonly JsonSerializerSettings serializerSettings = new JsonSerializerSettings();
private readonly JsonSerializerSettings serializerSettings = new JsonSerializerSettings();
private readonly TypeNameRegistry typeNameRegistry = new TypeNameRegistry();
private readonly SchemaJsonSerializer sut;
static JsonSerializerTests()
{
TypeNameRegistry.Map(typeof(FieldRegistry).GetTypeInfo().Assembly);
serializerSettings.TypeNameHandling = TypeNameHandling.Auto;
serializerSettings.SerializationBinder = new TypeNameSerializationBinder();
}
public JsonSerializerTests()
{
sut = new SchemaJsonSerializer(new FieldRegistry(), serializerSettings);
serializerSettings.TypeNameHandling = TypeNameHandling.Auto;
serializerSettings.SerializationBinder = new TypeNameSerializationBinder(typeNameRegistry);
sut = new SchemaJsonSerializer(new FieldRegistry(typeNameRegistry), serializerSettings);
}
[Fact]

2
tests/Squidex.Infrastructure.Tests/CQRS/Commands/DefaultDomainObjectRepositoryTests.cs

@ -25,7 +25,7 @@ namespace Squidex.Infrastructure.CQRS.Commands
private readonly Mock<IEventStore> eventStore = new Mock<IEventStore>();
private readonly Mock<IEventPublisher> eventPublisher = new Mock<IEventPublisher>();
private readonly Mock<IStreamNameResolver> streamNameResolver = new Mock<IStreamNameResolver>();
private readonly Mock<EventDataFormatter> eventDataFormatter = new Mock<EventDataFormatter>(null);
private readonly Mock<EventDataFormatter> eventDataFormatter = new Mock<EventDataFormatter>(new TypeNameRegistry(), null);
private readonly string streamName = Guid.NewGuid().ToString();
private readonly Guid aggregateId = Guid.NewGuid();
private readonly MyDomainObject domainObject;

12
tests/Squidex.Infrastructure.Tests/CQRS/Events/EventDataFormatterTests.cs

@ -22,16 +22,14 @@ namespace Squidex.Infrastructure.CQRS.Events
public string MyProperty { get; set; }
}
private static readonly JsonSerializerSettings serializerSettings = new JsonSerializerSettings();
private readonly JsonSerializerSettings serializerSettings = new JsonSerializerSettings();
private readonly TypeNameRegistry typeNameRegistry = new TypeNameRegistry();
static EventStoreFormatterTests()
public EventStoreFormatterTests()
{
serializerSettings.Converters.Add(new PropertiesBagConverter());
}
public EventStoreFormatterTests()
{
TypeNameRegistry.Map(typeof(MyEvent), "Event");
typeNameRegistry.Map(typeof(MyEvent), "Event");
}
[Fact]
@ -46,7 +44,7 @@ namespace Squidex.Infrastructure.CQRS.Events
inputEvent.SetEventNumber(1);
inputEvent.SetTimestamp(SystemClock.Instance.GetCurrentInstant());
var sut = new EventDataFormatter(serializerSettings);
var sut = new EventDataFormatter(typeNameRegistry, serializerSettings);
var eventData = sut.ToEventData(inputEvent.To<IEvent>(), commitId);

3
tests/Squidex.Infrastructure.Tests/CQRS/Events/EventReceiverTests.cs

@ -9,7 +9,6 @@
using System;
using Microsoft.Extensions.Logging;
using Moq;
using Newtonsoft.Json;
using NodaTime;
using Xunit;
@ -46,7 +45,7 @@ namespace Squidex.Infrastructure.CQRS.Events
private readonly Mock<ICatchEventConsumer> catchConsumer1 = new Mock<ICatchEventConsumer>();
private readonly Mock<ICatchEventConsumer> catchConsumer2 = new Mock<ICatchEventConsumer>();
private readonly Mock<IEventStream> eventStream = new Mock<IEventStream>();
private readonly Mock<EventDataFormatter> formatter = new Mock<EventDataFormatter>(new JsonSerializerSettings());
private readonly Mock<EventDataFormatter> formatter = new Mock<EventDataFormatter>(new TypeNameRegistry(), null);
private readonly EventData eventDataPast = new EventData();
private readonly EventData eventDataFuture = new EventData();
private readonly Envelope<IEvent> eventPast = new Envelope<IEvent>(new MyEvent());

50
tests/Squidex.Infrastructure.Tests/TypeNameRegistryTests.cs

@ -14,6 +14,8 @@ namespace Squidex.Infrastructure
{
public class TypeNameRegistryTests
{
private readonly TypeNameRegistry sut = new TypeNameRegistry();
[TypeName("my")]
public sealed class MyType
{
@ -22,60 +24,72 @@ namespace Squidex.Infrastructure
[Fact]
public void Should_register_and_retrieve_types()
{
TypeNameRegistry.Map(typeof(int), "NumberField");
sut.Map(typeof(int), "NumberField");
Assert.Equal("NumberField", sut.GetName<int>());
Assert.Equal("NumberField", sut.GetName(typeof(int)));
Assert.Equal(typeof(int), sut.GetType("NumberField"));
Assert.Equal(typeof(int), sut.GetType("NumberField"));
}
[Fact]
public void Should_register_from_attribute()
{
sut.Map(typeof(MyType));
Assert.Equal("NumberField", TypeNameRegistry.GetName<int>());
Assert.Equal("NumberField", TypeNameRegistry.GetName(typeof(int)));
Assert.Equal("my", sut.GetName<MyType>());
Assert.Equal("my", sut.GetName(typeof(MyType)));
Assert.Equal(typeof(int), TypeNameRegistry.GetType("NumberField"));
Assert.Equal(typeof(int), TypeNameRegistry.GetType("NumberField"));
Assert.Equal(typeof(MyType), sut.GetType("my"));
Assert.Equal(typeof(MyType), sut.GetType("My"));
}
[Fact]
public void Should_register_from_assembly()
{
TypeNameRegistry.Map(typeof(TypeNameRegistryTests).GetTypeInfo().Assembly);
sut.Map(typeof(TypeNameRegistryTests).GetTypeInfo().Assembly);
Assert.Equal("my", TypeNameRegistry.GetName<MyType>());
Assert.Equal("my", TypeNameRegistry.GetName(typeof(MyType)));
Assert.Equal("my", sut.GetName<MyType>());
Assert.Equal("my", sut.GetName(typeof(MyType)));
Assert.Equal(typeof(MyType), TypeNameRegistry.GetType("my"));
Assert.Equal(typeof(MyType), TypeNameRegistry.GetType("My"));
Assert.Equal(typeof(MyType), sut.GetType("my"));
Assert.Equal(typeof(MyType), sut.GetType("My"));
}
[Fact]
public void Should_not_throw_if_type_is_already_registered_with_same_name()
{
TypeNameRegistry.Map(typeof(long), "long");
TypeNameRegistry.Map(typeof(long), "long");
sut.Map(typeof(long), "long");
sut.Map(typeof(long), "long");
}
[Fact]
public void Should_throw_if_type_is_already_registered()
{
TypeNameRegistry.Map(typeof(long), "long");
sut.Map(typeof(long), "long");
Assert.Throws<ArgumentException>(() => TypeNameRegistry.Map(typeof(long), "longer"));
Assert.Throws<ArgumentException>(() => sut.Map(typeof(long), "longer"));
}
[Fact]
public void Should_throw_if_name_is_already_registered()
{
TypeNameRegistry.Map(typeof(short), "short");
sut.Map(typeof(short), "short");
Assert.Throws<ArgumentException>(() => TypeNameRegistry.Map(typeof(byte), "short"));
Assert.Throws<ArgumentException>(() => sut.Map(typeof(byte), "short"));
}
[Fact]
public void Should_throw_if_name_is_not_supported()
{
Assert.Throws<ArgumentException>(() => TypeNameRegistry.GetType("unsupported"));
Assert.Throws<ArgumentException>(() => sut.GetType("unsupported"));
}
[Fact]
public void Should_throw_if_type_is_not_supported()
{
Assert.Throws<ArgumentException>(() => TypeNameRegistry.GetName<Guid>());
Assert.Throws<ArgumentException>(() => sut.GetName<Guid>());
}
}
}

3
tests/Squidex.Write.Tests/EnrichWithSchemaIdProcessorTests.cs

@ -10,6 +10,7 @@ using System;
using System.Threading.Tasks;
using Squidex.Core.Schemas;
using Squidex.Events;
using Squidex.Infrastructure;
using Squidex.Infrastructure.CQRS;
using Squidex.Infrastructure.CQRS.Commands;
using Squidex.Infrastructure.CQRS.Events;
@ -51,7 +52,7 @@ namespace Squidex.Write
var envelope = new Envelope<IEvent>(new MyEvent());
await sut.ProcessEventAsync(envelope, new SchemaDomainObject(appId, 1, new FieldRegistry()), new MyNormalCommand());
await sut.ProcessEventAsync(envelope, new SchemaDomainObject(appId, 1, new FieldRegistry(new TypeNameRegistry())), new MyNormalCommand());
Assert.Equal(appId, envelope.Headers.SchemaId());
}

8
tests/Squidex.Write.Tests/Schemas/SchemaCommandHandlerTests.cs

@ -7,7 +7,6 @@
// ==========================================================================
using System;
using System.Reflection;
using System.Threading.Tasks;
using Moq;
using Squidex.Core.Schemas;
@ -27,16 +26,11 @@ namespace Squidex.Write.Schemas
private readonly Mock<ISchemaProvider> schemaProvider = new Mock<ISchemaProvider>();
private readonly SchemaCommandHandler sut;
private readonly SchemaDomainObject schema;
private readonly FieldRegistry registry = new FieldRegistry();
private readonly FieldRegistry registry = new FieldRegistry(new TypeNameRegistry());
private readonly Guid appId = Guid.NewGuid();
private readonly string fieldName = "age";
private readonly string schemaName = "users";
static SchemaCommandHandlerTests()
{
TypeNameRegistry.Map(typeof(Schema).GetTypeInfo().Assembly);
}
public SchemaCommandHandlerTests()
{
schema = new SchemaDomainObject(Id, 0, registry);

2
tests/Squidex.Write.Tests/Schemas/SchemaDomainObjectTests.cs

@ -26,7 +26,7 @@ namespace Squidex.Write.Schemas
private readonly Guid appId = Guid.NewGuid();
private readonly string fieldName = "age";
private readonly string appName = "schema";
private readonly FieldRegistry registry = new FieldRegistry();
private readonly FieldRegistry registry = new FieldRegistry(new TypeNameRegistry());
private readonly SchemaDomainObject sut;
public SchemaDomainObjectTests()

28
tests/Squidex.Write.Tests/Utils/SchemaFixture.cs

@ -1,28 +0,0 @@
// ==========================================================================
// SchemaFixture.cs
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex Group
// All rights reserved.
// ==========================================================================
using System.Reflection;
using Squidex.Core.Schemas;
using Squidex.Infrastructure;
using Xunit;
namespace Squidex.Write.Utils
{
public class SchemaFixture
{
public SchemaFixture()
{
TypeNameRegistry.Map(typeof(Schema).GetTypeInfo().Assembly);
}
}
[CollectionDefinition("Schema")]
public class DatabaseCollection : ICollectionFixture<SchemaFixture>
{
}
}
Loading…
Cancel
Save