// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschränkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using System; using System.Collections.Generic; using System.Linq; using System.Security.Claims; using FakeItEasy; using Squidex.Domain.Apps.Events; using Squidex.Infrastructure; using Squidex.Infrastructure.Commands; using Squidex.Infrastructure.EventSourcing; using Squidex.Infrastructure.Orleans; using Squidex.Infrastructure.States; namespace Squidex.Domain.Apps.Entities.TestHelpers { public abstract class HandlerTestBase { private readonly IStore store = A.Fake>(); private readonly IPersistence persistenceWithState = A.Fake>(); private readonly IPersistence persistence = A.Fake(); protected RefToken Actor { get; } = new RefToken(RefTokenType.Subject, "me"); protected RefToken ActorClient { get; } = new RefToken(RefTokenType.Client, "client"); protected Guid AppId { get; } = Guid.NewGuid(); protected Guid SchemaId { get; } = Guid.NewGuid(); protected string AppName { get; } = "my-app"; protected string SchemaName { get; } = "my-schema"; protected ClaimsPrincipal User { get; } = Mocks.FrontendUser(); protected NamedId AppNamedId { get { return NamedId.Of(AppId, AppName); } } protected NamedId SchemaNamedId { get { return NamedId.Of(SchemaId, SchemaName); } } protected abstract Guid Id { get; } public IStore Store { get { return store; } } public IEnumerable> LastEvents { get; private set; } = Enumerable.Empty>(); protected HandlerTestBase() { A.CallTo(() => store.WithSnapshotsAndEventSourcing(A.Ignored, Id, A>.Ignored, A.Ignored)) .Returns(persistenceWithState); A.CallTo(() => store.WithEventSourcing(A.Ignored, Id, A.Ignored)) .Returns(persistence); A.CallTo(() => persistenceWithState.WriteEventsAsync(A>>.Ignored)) .Invokes((IEnumerable> events) => LastEvents = events); A.CallTo(() => persistence.WriteEventsAsync(A>>.Ignored)) .Invokes((IEnumerable> events) => LastEvents = events); } protected CommandContext CreateContextForCommand(TCommand command) where TCommand : SquidexCommand { return new CommandContext(CreateCommand(command), A.Dummy()); } protected TCommand CreateCommand(TCommand command) where TCommand : SquidexCommand { command.ExpectedVersion = EtagVersion.Any; if (command.Actor == null) { command.Actor = Actor; } if (command.User == null && command.Actor.IsSubject) { command.User = User; } if (command is IAppCommand appCommand && appCommand.AppId == null) { appCommand.AppId = AppNamedId; } if (command is ISchemaCommand schemaCommand && schemaCommand.SchemaId == null) { schemaCommand.SchemaId = SchemaNamedId; } return command; } protected static J J(IAggregateCommand command) { return command.AsJ(); } protected TEvent CreateEvent(TEvent @event, bool fromClient = false) where TEvent : SquidexEvent { @event.Actor = fromClient ? ActorClient : Actor; if (@event is AppEvent appEvent) { appEvent.AppId = AppNamedId; } if (@event is SchemaEvent schemaEvent) { schemaEvent.SchemaId = SchemaNamedId; } return @event; } } }