From 1a562da78232eb8cd71403956bee45bdc8ea671b Mon Sep 17 00:00:00 2001 From: Sebastian Stehle Date: Mon, 12 Feb 2018 23:55:37 +0100 Subject: [PATCH] RuleGrainTests --- .../Rules/RuleGrain.cs | 13 +- .../Apps/AppCommandMiddlewareTests.cs | 580 +++---- .../Apps/AppDomainObjectTests.cs | 798 +++++----- .../Assets/AssetCommandMiddlewareTests.cs | 290 ++-- .../Assets/AssetDomainObjectTests.cs | 440 +++--- .../Contents/ContentCommandMiddlewareTests.cs | 464 +++--- .../Contents/ContentDomainObjectTests.cs | 608 ++++---- .../Rules/RuleCommandMiddlewareTests.cs | 118 -- .../Rules/RuleDequeuerTests.cs | 2 - .../Rules/RuleDomainObjectTests.cs | 256 ---- .../Rules/RuleGrainTests.cs | 227 +++ .../Schemas/SchemaCommandMiddlewareTests.cs | 560 +++---- .../Schemas/SchemaDomainObjectTests.cs | 1330 ++++++++--------- .../TestHelpers/HandlerTestBase.cs | 136 +- 14 files changed, 2827 insertions(+), 2995 deletions(-) delete mode 100644 tests/Squidex.Domain.Apps.Entities.Tests/Rules/RuleCommandMiddlewareTests.cs delete mode 100644 tests/Squidex.Domain.Apps.Entities.Tests/Rules/RuleDomainObjectTests.cs create mode 100644 tests/Squidex.Domain.Apps.Entities.Tests/Rules/RuleGrainTests.cs diff --git a/src/Squidex.Domain.Apps.Entities/Rules/RuleGrain.cs b/src/Squidex.Domain.Apps.Entities/Rules/RuleGrain.cs index d61214ace..d99c8086a 100644 --- a/src/Squidex.Domain.Apps.Entities/Rules/RuleGrain.cs +++ b/src/Squidex.Domain.Apps.Entities/Rules/RuleGrain.cs @@ -7,6 +7,8 @@ using System; using System.Threading.Tasks; +using Orleans.Core; +using Orleans.Runtime; using Squidex.Domain.Apps.Entities.Rules.Commands; using Squidex.Domain.Apps.Entities.Rules.Guards; using Squidex.Domain.Apps.Entities.Rules.State; @@ -21,12 +23,17 @@ using Squidex.Infrastructure.States; namespace Squidex.Domain.Apps.Entities.Rules { - public sealed class RuleGrain : DomainObjectGrain, IRuleGrain + public class RuleGrain : DomainObjectGrain, IRuleGrain { private readonly IAppProvider appProvider; public RuleGrain(IStore store, IAppProvider appProvider) - : base(store) + : this(store, appProvider, null, null) + { + } + + protected RuleGrain(IStore store, IAppProvider appProvider, IGrainIdentity identity, IGrainRuntime runtime) + : base(store, identity, runtime) { Guard.NotNull(appProvider, nameof(appProvider)); @@ -116,7 +123,7 @@ namespace Squidex.Domain.Apps.Entities.Rules private void VerifyNotDeleted() { - if (Snapshot.IsDeleted || Snapshot.RuleDef == null) + if (Snapshot.IsDeleted) { throw new DomainException("Webhook has already been deleted."); } diff --git a/tests/Squidex.Domain.Apps.Entities.Tests/Apps/AppCommandMiddlewareTests.cs b/tests/Squidex.Domain.Apps.Entities.Tests/Apps/AppCommandMiddlewareTests.cs index 18ab306fa..655be3528 100644 --- a/tests/Squidex.Domain.Apps.Entities.Tests/Apps/AppCommandMiddlewareTests.cs +++ b/tests/Squidex.Domain.Apps.Entities.Tests/Apps/AppCommandMiddlewareTests.cs @@ -1,291 +1,291 @@ -// ========================================================================== -// Squidex Headless CMS -// ========================================================================== -// Copyright (c) Squidex UG (haftungsbeschränkt) -// All rights reserved. Licensed under the MIT license. -// ========================================================================== - -using System; -using System.Threading.Tasks; -using FakeItEasy; -using Squidex.Domain.Apps.Entities.Apps.Commands; -using Squidex.Domain.Apps.Entities.Apps.Services; -using Squidex.Domain.Apps.Entities.Apps.Services.Implementations; -using Squidex.Domain.Apps.Entities.TestHelpers; -using Squidex.Infrastructure; -using Squidex.Infrastructure.Commands; -using Squidex.Infrastructure.States; -using Squidex.Shared.Users; -using Xunit; - -namespace Squidex.Domain.Apps.Entities.Apps -{ - public class AppCommandMiddlewareTests : HandlerTestBase - { - private readonly IAppProvider appProvider = A.Fake(); - private readonly IAppPlansProvider appPlansProvider = A.Fake(); - private readonly IAppPlanBillingManager appPlansBillingManager = A.Fake(); - private readonly IUserResolver userResolver = A.Fake(); - private readonly Language language = Language.DE; - private readonly string contributorId = Guid.NewGuid().ToString(); - private readonly string clientName = "client"; - private readonly Guid patternId = Guid.NewGuid(); - private readonly AppDomainObject app = new AppDomainObject(new InitialPatterns()); - private readonly AppCommandMiddleware sut; - - protected override Guid Id - { - get { return AppId; } - } - - public AppCommandMiddlewareTests() - { - A.CallTo(() => appProvider.GetAppAsync(AppName)) - .Returns((IAppEntity)null); - - A.CallTo(() => userResolver.FindByIdAsync(contributorId)) - .Returns(A.Fake()); - - sut = new AppCommandMiddleware(Handler, appProvider, appPlansProvider, appPlansBillingManager, userResolver); - - app.ActivateAsync(Id, A.Fake>()); - } - - [Fact] - public async Task Create_should_create_domain_object() - { - var context = CreateContextForCommand(new CreateApp { Name = AppName, AppId = AppId }); - - await TestCreate(app, async _ => - { - await sut.HandleAsync(context); - }); - - Assert.Equal(AppId, context.Result>().IdOrValue); - } - - [Fact] - public async Task AssignContributor_should_update_domain_object_if_user_found() - { - A.CallTo(() => appPlansProvider.GetPlan(null)) - .Returns(new ConfigAppLimitsPlan { MaxContributors = -1 }); - - CreateApp(); - - var context = CreateContextForCommand(new AssignContributor { ContributorId = contributorId }); - - await TestUpdate(app, async _ => - { - await sut.HandleAsync(context); - }); - } - - [Fact] - public async Task RemoveContributor_should_update_domain_object() - { - CreateApp() - .AssignContributor(CreateCommand(new AssignContributor { ContributorId = contributorId })); - - var context = CreateContextForCommand(new RemoveContributor { ContributorId = contributorId }); - - await TestUpdate(app, async _ => - { - await sut.HandleAsync(context); - }); - } - - [Fact] - public async Task AttachClient_should_update_domain_object() - { - CreateApp(); - - var context = CreateContextForCommand(new AttachClient { Id = clientName }); - - await TestUpdate(app, async _ => - { - await sut.HandleAsync(context); - }); - } - - [Fact] - public async Task RenameClient_should_update_domain_object() - { - CreateApp() - .AttachClient(CreateCommand(new AttachClient { Id = clientName })); - - var context = CreateContextForCommand(new UpdateClient { Id = clientName, Name = "New Name" }); - - await TestUpdate(app, async _ => - { - await sut.HandleAsync(context); - }); - } - - [Fact] - public async Task RevokeClient_should_update_domain_object() - { - CreateApp() - .AttachClient(CreateCommand(new AttachClient { Id = clientName })); - - var context = CreateContextForCommand(new RevokeClient { Id = clientName }); - - await TestUpdate(app, async _ => - { - await sut.HandleAsync(context); - }); - } - - [Fact] - public async Task ChangePlan_should_update_domain_object() - { - A.CallTo(() => appPlansProvider.IsConfiguredPlan("my-plan")) - .Returns(true); - - CreateApp(); +//// ========================================================================== +//// Squidex Headless CMS +//// ========================================================================== +//// Copyright (c) Squidex UG (haftungsbeschränkt) +//// All rights reserved. Licensed under the MIT license. +//// ========================================================================== + +//using System; +//using System.Threading.Tasks; +//using FakeItEasy; +//using Squidex.Domain.Apps.Entities.Apps.Commands; +//using Squidex.Domain.Apps.Entities.Apps.Services; +//using Squidex.Domain.Apps.Entities.Apps.Services.Implementations; +//using Squidex.Domain.Apps.Entities.TestHelpers; +//using Squidex.Infrastructure; +//using Squidex.Infrastructure.Commands; +//using Squidex.Infrastructure.States; +//using Squidex.Shared.Users; +//using Xunit; + +//namespace Squidex.Domain.Apps.Entities.Apps +//{ +// public class AppCommandMiddlewareTests : HandlerTestBase +// { +// private readonly IAppProvider appProvider = A.Fake(); +// private readonly IAppPlansProvider appPlansProvider = A.Fake(); +// private readonly IAppPlanBillingManager appPlansBillingManager = A.Fake(); +// private readonly IUserResolver userResolver = A.Fake(); +// private readonly Language language = Language.DE; +// private readonly string contributorId = Guid.NewGuid().ToString(); +// private readonly string clientName = "client"; +// private readonly Guid patternId = Guid.NewGuid(); +// private readonly AppDomainObject app = new AppDomainObject(new InitialPatterns()); +// private readonly AppCommandMiddleware sut; + +// protected override Guid Id +// { +// get { return AppId; } +// } + +// public AppCommandMiddlewareTests() +// { +// A.CallTo(() => appProvider.GetAppAsync(AppName)) +// .Returns((IAppEntity)null); + +// A.CallTo(() => userResolver.FindByIdAsync(contributorId)) +// .Returns(A.Fake()); + +// sut = new AppCommandMiddleware(Handler, appProvider, appPlansProvider, appPlansBillingManager, userResolver); + +// app.ActivateAsync(Id, A.Fake>()); +// } + +// [Fact] +// public async Task Create_should_create_domain_object() +// { +// var context = CreateContextForCommand(new CreateApp { Name = AppName, AppId = AppId }); + +// await TestCreate(app, async _ => +// { +// await sut.HandleAsync(context); +// }); + +// Assert.Equal(AppId, context.Result>().IdOrValue); +// } + +// [Fact] +// public async Task AssignContributor_should_update_domain_object_if_user_found() +// { +// A.CallTo(() => appPlansProvider.GetPlan(null)) +// .Returns(new ConfigAppLimitsPlan { MaxContributors = -1 }); + +// CreateApp(); + +// var context = CreateContextForCommand(new AssignContributor { ContributorId = contributorId }); + +// await TestUpdate(app, async _ => +// { +// await sut.HandleAsync(context); +// }); +// } + +// [Fact] +// public async Task RemoveContributor_should_update_domain_object() +// { +// CreateApp() +// .AssignContributor(CreateCommand(new AssignContributor { ContributorId = contributorId })); + +// var context = CreateContextForCommand(new RemoveContributor { ContributorId = contributorId }); + +// await TestUpdate(app, async _ => +// { +// await sut.HandleAsync(context); +// }); +// } + +// [Fact] +// public async Task AttachClient_should_update_domain_object() +// { +// CreateApp(); + +// var context = CreateContextForCommand(new AttachClient { Id = clientName }); + +// await TestUpdate(app, async _ => +// { +// await sut.HandleAsync(context); +// }); +// } + +// [Fact] +// public async Task RenameClient_should_update_domain_object() +// { +// CreateApp() +// .AttachClient(CreateCommand(new AttachClient { Id = clientName })); + +// var context = CreateContextForCommand(new UpdateClient { Id = clientName, Name = "New Name" }); + +// await TestUpdate(app, async _ => +// { +// await sut.HandleAsync(context); +// }); +// } + +// [Fact] +// public async Task RevokeClient_should_update_domain_object() +// { +// CreateApp() +// .AttachClient(CreateCommand(new AttachClient { Id = clientName })); + +// var context = CreateContextForCommand(new RevokeClient { Id = clientName }); + +// await TestUpdate(app, async _ => +// { +// await sut.HandleAsync(context); +// }); +// } + +// [Fact] +// public async Task ChangePlan_should_update_domain_object() +// { +// A.CallTo(() => appPlansProvider.IsConfiguredPlan("my-plan")) +// .Returns(true); + +// CreateApp(); - var context = CreateContextForCommand(new ChangePlan { PlanId = "my-plan" }); - - await TestUpdate(app, async _ => - { - await sut.HandleAsync(context); - }); - - A.CallTo(() => appPlansBillingManager.ChangePlanAsync(User.Identifier, AppId, AppName, "my-plan")) - .MustHaveHappened(); - } - - [Fact] - public async Task ChangePlan_should_not_make_update_for_redirect_result() - { - A.CallTo(() => appPlansProvider.IsConfiguredPlan("my-plan")) - .Returns(true); - - A.CallTo(() => appPlansBillingManager.ChangePlanAsync(User.Identifier, AppId, AppName, "my-plan")) - .Returns(CreateRedirectResult()); - - CreateApp(); - - var context = CreateContextForCommand(new ChangePlan { PlanId = "my-plan" }); - - await TestUpdate(app, async _ => - { - await sut.HandleAsync(context); - }); - - Assert.Null(app.Snapshot.Plan); - } - - [Fact] - public async Task ChangePlan_should_not_call_billing_manager_for_callback() - { - A.CallTo(() => appPlansProvider.IsConfiguredPlan("my-plan")) - .Returns(true); - - CreateApp(); - - var context = CreateContextForCommand(new ChangePlan { PlanId = "my-plan", FromCallback = true }); - - await TestUpdate(app, async _ => - { - await sut.HandleAsync(context); - }); - - A.CallTo(() => appPlansBillingManager.ChangePlanAsync(User.Identifier, AppId, AppName, "my-plan")) - .MustNotHaveHappened(); - } - - [Fact] - public async Task AddLanguage_should_update_domain_object() - { - CreateApp(); - - var context = CreateContextForCommand(new AddLanguage { Language = language }); - - await TestUpdate(app, async _ => - { - await sut.HandleAsync(context); - }); - } - - [Fact] - public async Task RemoveLanguage_should_update_domain_object() - { - CreateApp() - .AddLanguage(CreateCommand(new AddLanguage { Language = language })); - - var context = CreateContextForCommand(new RemoveLanguage { Language = language }); - - await TestUpdate(app, async _ => - { - await sut.HandleAsync(context); - }); - } - - [Fact] - public async Task UpdateLanguage_should_update_domain_object() - { - CreateApp() - .AddLanguage(CreateCommand(new AddLanguage { Language = language })); - - var context = CreateContextForCommand(new UpdateLanguage { Language = language }); - - await TestUpdate(app, async _ => - { - await sut.HandleAsync(context); - }); - } - - [Fact] - public async Task AddPattern_should_update_domain_object() - { - CreateApp(); - - var context = CreateContextForCommand(new AddPattern { Name = "Any", Pattern = ".*" }); - - await TestUpdate(app, async _ => - { - await sut.HandleAsync(context); - }); - } - - [Fact] - public async Task UpdatePattern_should_update_domain() - { - CreateApp() - .AddPattern(CreateCommand(new AddPattern { PatternId = patternId, Name = "Any", Pattern = "." })); - - var context = CreateContextForCommand(new UpdatePattern { PatternId = patternId, Name = "Number", Pattern = "[0-9]" }); - - await TestUpdate(app, async _ => - { - await sut.HandleAsync(context); - }); - } - - [Fact] - public async Task DeletePattern_should_update_domain_object() - { - CreateApp() - .AddPattern(CreateCommand(new AddPattern { PatternId = patternId, Name = "Any", Pattern = "." })); - - var context = CreateContextForCommand(new DeletePattern { PatternId = patternId }); - - await TestUpdate(app, async _ => - { - await sut.HandleAsync(context); - }); - } - - private AppDomainObject CreateApp() - { - app.Create(CreateCommand(new CreateApp { AppId = AppId, Name = AppName })); - - return app; - } - - private static Task CreateRedirectResult() - { - return Task.FromResult(new RedirectToCheckoutResult(new Uri("http://squidex.io"))); - } - } -} +// var context = CreateContextForCommand(new ChangePlan { PlanId = "my-plan" }); + +// await TestUpdate(app, async _ => +// { +// await sut.HandleAsync(context); +// }); + +// A.CallTo(() => appPlansBillingManager.ChangePlanAsync(User.Identifier, AppId, AppName, "my-plan")) +// .MustHaveHappened(); +// } + +// [Fact] +// public async Task ChangePlan_should_not_make_update_for_redirect_result() +// { +// A.CallTo(() => appPlansProvider.IsConfiguredPlan("my-plan")) +// .Returns(true); + +// A.CallTo(() => appPlansBillingManager.ChangePlanAsync(User.Identifier, AppId, AppName, "my-plan")) +// .Returns(CreateRedirectResult()); + +// CreateApp(); + +// var context = CreateContextForCommand(new ChangePlan { PlanId = "my-plan" }); + +// await TestUpdate(app, async _ => +// { +// await sut.HandleAsync(context); +// }); + +// Assert.Null(app.Snapshot.Plan); +// } + +// [Fact] +// public async Task ChangePlan_should_not_call_billing_manager_for_callback() +// { +// A.CallTo(() => appPlansProvider.IsConfiguredPlan("my-plan")) +// .Returns(true); + +// CreateApp(); + +// var context = CreateContextForCommand(new ChangePlan { PlanId = "my-plan", FromCallback = true }); + +// await TestUpdate(app, async _ => +// { +// await sut.HandleAsync(context); +// }); + +// A.CallTo(() => appPlansBillingManager.ChangePlanAsync(User.Identifier, AppId, AppName, "my-plan")) +// .MustNotHaveHappened(); +// } + +// [Fact] +// public async Task AddLanguage_should_update_domain_object() +// { +// CreateApp(); + +// var context = CreateContextForCommand(new AddLanguage { Language = language }); + +// await TestUpdate(app, async _ => +// { +// await sut.HandleAsync(context); +// }); +// } + +// [Fact] +// public async Task RemoveLanguage_should_update_domain_object() +// { +// CreateApp() +// .AddLanguage(CreateCommand(new AddLanguage { Language = language })); + +// var context = CreateContextForCommand(new RemoveLanguage { Language = language }); + +// await TestUpdate(app, async _ => +// { +// await sut.HandleAsync(context); +// }); +// } + +// [Fact] +// public async Task UpdateLanguage_should_update_domain_object() +// { +// CreateApp() +// .AddLanguage(CreateCommand(new AddLanguage { Language = language })); + +// var context = CreateContextForCommand(new UpdateLanguage { Language = language }); + +// await TestUpdate(app, async _ => +// { +// await sut.HandleAsync(context); +// }); +// } + +// [Fact] +// public async Task AddPattern_should_update_domain_object() +// { +// CreateApp(); + +// var context = CreateContextForCommand(new AddPattern { Name = "Any", Pattern = ".*" }); + +// await TestUpdate(app, async _ => +// { +// await sut.HandleAsync(context); +// }); +// } + +// [Fact] +// public async Task UpdatePattern_should_update_domain() +// { +// CreateApp() +// .AddPattern(CreateCommand(new AddPattern { PatternId = patternId, Name = "Any", Pattern = "." })); + +// var context = CreateContextForCommand(new UpdatePattern { PatternId = patternId, Name = "Number", Pattern = "[0-9]" }); + +// await TestUpdate(app, async _ => +// { +// await sut.HandleAsync(context); +// }); +// } + +// [Fact] +// public async Task DeletePattern_should_update_domain_object() +// { +// CreateApp() +// .AddPattern(CreateCommand(new AddPattern { PatternId = patternId, Name = "Any", Pattern = "." })); + +// var context = CreateContextForCommand(new DeletePattern { PatternId = patternId }); + +// await TestUpdate(app, async _ => +// { +// await sut.HandleAsync(context); +// }); +// } + +// private AppDomainObject CreateApp() +// { +// app.Create(CreateCommand(new CreateApp { AppId = AppId, Name = AppName })); + +// return app; +// } + +// private static Task CreateRedirectResult() +// { +// return Task.FromResult(new RedirectToCheckoutResult(new Uri("http://squidex.io"))); +// } +// } +//} diff --git a/tests/Squidex.Domain.Apps.Entities.Tests/Apps/AppDomainObjectTests.cs b/tests/Squidex.Domain.Apps.Entities.Tests/Apps/AppDomainObjectTests.cs index e49cf171e..7c93b7fe4 100644 --- a/tests/Squidex.Domain.Apps.Entities.Tests/Apps/AppDomainObjectTests.cs +++ b/tests/Squidex.Domain.Apps.Entities.Tests/Apps/AppDomainObjectTests.cs @@ -1,399 +1,399 @@ -// ========================================================================== -// 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 FakeItEasy; -using Squidex.Domain.Apps.Core.Apps; -using Squidex.Domain.Apps.Entities.Apps.Commands; -using Squidex.Domain.Apps.Entities.TestHelpers; -using Squidex.Domain.Apps.Events.Apps; -using Squidex.Infrastructure; -using Squidex.Infrastructure.States; -using Xunit; - -namespace Squidex.Domain.Apps.Entities.Apps -{ - public class AppDomainObjectTests : HandlerTestBase - { - private readonly string contributorId = Guid.NewGuid().ToString(); - private readonly string clientId = "client"; - private readonly string clientNewName = "My Client"; - private readonly string planId = "premium"; - private readonly Guid patternId = Guid.NewGuid(); - private readonly AppDomainObject sut = new AppDomainObject(new InitialPatterns()); - - protected override Guid Id - { - get { return AppId; } - } - - public AppDomainObjectTests() - { - sut.ActivateAsync(Id, A.Fake>()); - } - - [Fact] - public void Create_should_throw_exception_if_created() - { - CreateApp(); - - Assert.Throws(() => - { - sut.Create(CreateCommand(new CreateApp { Name = AppName })); - }); - } - - [Fact] - public void Create_should_specify_name_and_owner() - { - var id1 = Guid.NewGuid(); - var id2 = Guid.NewGuid(); - - var initialPatterns = new InitialPatterns - { - { id1, new AppPattern("Number", "[0-9]") }, - { id2, new AppPattern("Numbers", "[0-9]*") } - }; - - var app = new AppDomainObject(initialPatterns); - - app.Create(CreateCommand(new CreateApp { Name = AppName, Actor = User, AppId = AppId })); - - Assert.Equal(AppName, app.Snapshot.Name); - - app.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateEvent(new AppCreated { Name = AppName }), - CreateEvent(new AppContributorAssigned { ContributorId = User.Identifier, Permission = AppContributorPermission.Owner }), - CreateEvent(new AppLanguageAdded { Language = Language.EN }), - CreateEvent(new AppPatternAdded { PatternId = id1, Name = "Number", Pattern = "[0-9]" }), - CreateEvent(new AppPatternAdded { PatternId = id2, Name = "Numbers", Pattern = "[0-9]*" }) - ); - } - - [Fact] - public void ChangePlan_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.ChangePlan(CreateCommand(new ChangePlan { PlanId = planId })); - }); - } - - [Fact] - public void ChangePlan_should_create_events() - { - CreateApp(); - - sut.ChangePlan(CreateCommand(new ChangePlan { PlanId = planId })); - - Assert.Equal(planId, sut.Snapshot.Plan.PlanId); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateEvent(new AppPlanChanged { PlanId = planId }) - ); - } - - [Fact] - public void AssignContributor_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.AssignContributor(CreateCommand(new AssignContributor { ContributorId = contributorId })); - }); - } - - [Fact] - public void AssignContributor_should_create_events() - { - CreateApp(); - - sut.AssignContributor(CreateCommand(new AssignContributor { ContributorId = contributorId, Permission = AppContributorPermission.Editor })); - - Assert.Equal(AppContributorPermission.Editor, sut.Snapshot.Contributors[contributorId]); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateEvent(new AppContributorAssigned { ContributorId = contributorId, Permission = AppContributorPermission.Editor }) - ); - } - - [Fact] - public void RemoveContributor_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.RemoveContributor(CreateCommand(new RemoveContributor { ContributorId = contributorId })); - }); - } - - [Fact] - public void RemoveContributor_should_create_events_and_remove_contributor() - { - CreateApp(); - - sut.AssignContributor(CreateCommand(new AssignContributor { ContributorId = contributorId, Permission = AppContributorPermission.Editor })); - sut.RemoveContributor(CreateCommand(new RemoveContributor { ContributorId = contributorId })); - - Assert.False(sut.Snapshot.Contributors.ContainsKey(contributorId)); - - sut.GetUncomittedEvents().Skip(1) - .ShouldHaveSameEvents( - CreateEvent(new AppContributorRemoved { ContributorId = contributorId }) - ); - } - - [Fact] - public void AttachClient_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.AttachClient(CreateCommand(new AttachClient { Id = clientId })); - }); - } - - [Fact] - public void AttachClient_should_create_events() - { - var command = new AttachClient { Id = clientId }; - - CreateApp(); - - sut.AttachClient(CreateCommand(command)); - - Assert.True(sut.Snapshot.Clients.ContainsKey(clientId)); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateEvent(new AppClientAttached { Id = clientId, Secret = command.Secret }) - ); - } - - [Fact] - public void RevokeClient_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.RevokeClient(CreateCommand(new RevokeClient { Id = "not-found" })); - }); - } - - [Fact] - public void RevokeClient_should_create_events() - { - CreateApp(); - CreateClient(); - - sut.RevokeClient(CreateCommand(new RevokeClient { Id = clientId })); - - Assert.False(sut.Snapshot.Clients.ContainsKey(clientId)); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateEvent(new AppClientRevoked { Id = clientId }) - ); - } - - [Fact] - public void UpdateClient_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.UpdateClient(CreateCommand(new UpdateClient { Id = "not-found", Name = clientNewName })); - }); - } - - [Fact] - public void UpdateClient_should_create_events() - { - CreateApp(); - CreateClient(); - - sut.UpdateClient(CreateCommand(new UpdateClient { Id = clientId, Name = clientNewName, Permission = AppClientPermission.Developer })); - - Assert.Equal(clientNewName, sut.Snapshot.Clients[clientId].Name); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateEvent(new AppClientRenamed { Id = clientId, Name = clientNewName }), - CreateEvent(new AppClientUpdated { Id = clientId, Permission = AppClientPermission.Developer }) - ); - } - - [Fact] - public void AddLanguage_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.AddLanguage(CreateCommand(new AddLanguage { Language = Language.DE })); - }); - } - - [Fact] - public void AddLanguage_should_create_events() - { - CreateApp(); - - sut.AddLanguage(CreateCommand(new AddLanguage { Language = Language.DE })); - - Assert.True(sut.Snapshot.LanguagesConfig.Contains(Language.DE)); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateEvent(new AppLanguageAdded { Language = Language.DE }) - ); - } - - [Fact] - public void RemoveLanguage_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.RemoveLanguage(CreateCommand(new RemoveLanguage { Language = Language.EN })); - }); - } - - [Fact] - public void RemoveLanguage_should_create_events() - { - CreateApp(); - CreateLanguage(Language.DE); - - sut.RemoveLanguage(CreateCommand(new RemoveLanguage { Language = Language.DE })); - - Assert.False(sut.Snapshot.LanguagesConfig.Contains(Language.DE)); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateEvent(new AppLanguageRemoved { Language = Language.DE }) - ); - } - - [Fact] - public void UpdateLanguage_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.UpdateLanguage(CreateCommand(new UpdateLanguage { Language = Language.EN })); - }); - } - - [Fact] - public void UpdateLanguage_should_create_events() - { - CreateApp(); - CreateLanguage(Language.DE); - - sut.UpdateLanguage(CreateCommand(new UpdateLanguage { Language = Language.DE, Fallback = new List { Language.EN } })); - - Assert.True(sut.Snapshot.LanguagesConfig.Contains(Language.DE)); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateEvent(new AppLanguageUpdated { Language = Language.DE, Fallback = new List { Language.EN } }) - ); - } - - [Fact] - public void AddPattern_should_throw_exception_if_app_not_created() - { - Assert.Throws(() => sut.AddPattern(CreateCommand(new AddPattern { PatternId = patternId, Name = "Any", Pattern = ".*" }))); - } - - [Fact] - public void AddPattern_should_create_events() - { - CreateApp(); - - sut.AddPattern(CreateCommand(new AddPattern { PatternId = patternId, Name = "Any", Pattern = ".*", Message = "Msg" })); - - Assert.Single(sut.Snapshot.Patterns); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateEvent(new AppPatternAdded { PatternId = patternId, Name = "Any", Pattern = ".*", Message = "Msg" }) - ); - } - - [Fact] - public void DeletePattern_should_throw_exception_if_app_not_created() - { - Assert.Throws(() => - { - sut.DeletePattern(CreateCommand(new DeletePattern - { - PatternId = Guid.NewGuid() - })); - }); - } - - [Fact] - public void DeletePattern_should_create_events() - { - CreateApp(); - CreatePattern(); - - sut.DeletePattern(CreateCommand(new DeletePattern { PatternId = patternId })); - - Assert.Empty(sut.Snapshot.Patterns); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateEvent(new AppPatternDeleted { PatternId = patternId }) - ); - } - - [Fact] - public void UpdatePattern_should_throw_exception_if_app_not_created() - { - Assert.Throws(() => sut.UpdatePattern(CreateCommand(new UpdatePattern { PatternId = patternId, Name = "Any", Pattern = ".*" }))); - } - - [Fact] - public void UpdatePattern_should_create_events() - { - CreateApp(); - CreatePattern(); - - sut.UpdatePattern(CreateCommand(new UpdatePattern { PatternId = patternId, Name = "Any", Pattern = ".*", Message = "Msg" })); - - Assert.Single(sut.Snapshot.Patterns); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateEvent(new AppPatternUpdated { PatternId = patternId, Name = "Any", Pattern = ".*", Message = "Msg" }) - ); - } - - private void CreatePattern() - { - sut.AddPattern(CreateCommand(new AddPattern { PatternId = patternId, Name = "Name", Pattern = ".*" })); - sut.ClearUncommittedEvents(); - } - - private void CreateApp() - { - sut.Create(CreateCommand(new CreateApp { Name = AppName })); - sut.ClearUncommittedEvents(); - } - - private void CreateClient() - { - sut.AttachClient(CreateCommand(new AttachClient { Id = clientId })); - sut.ClearUncommittedEvents(); - } - - private void CreateLanguage(Language language) - { - sut.AddLanguage(CreateCommand(new AddLanguage { Language = language })); - sut.ClearUncommittedEvents(); - } - } -} +//// ========================================================================== +//// 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 FakeItEasy; +//using Squidex.Domain.Apps.Core.Apps; +//using Squidex.Domain.Apps.Entities.Apps.Commands; +//using Squidex.Domain.Apps.Entities.TestHelpers; +//using Squidex.Domain.Apps.Events.Apps; +//using Squidex.Infrastructure; +//using Squidex.Infrastructure.States; +//using Xunit; + +//namespace Squidex.Domain.Apps.Entities.Apps +//{ +// public class AppDomainObjectTests : HandlerTestBase +// { +// private readonly string contributorId = Guid.NewGuid().ToString(); +// private readonly string clientId = "client"; +// private readonly string clientNewName = "My Client"; +// private readonly string planId = "premium"; +// private readonly Guid patternId = Guid.NewGuid(); +// private readonly AppDomainObject sut = new AppDomainObject(new InitialPatterns()); + +// protected override Guid Id +// { +// get { return AppId; } +// } + +// public AppDomainObjectTests() +// { +// sut.ActivateAsync(Id, A.Fake>()); +// } + +// [Fact] +// public void Create_should_throw_exception_if_created() +// { +// CreateApp(); + +// Assert.Throws(() => +// { +// sut.Create(CreateCommand(new CreateApp { Name = AppName })); +// }); +// } + +// [Fact] +// public void Create_should_specify_name_and_owner() +// { +// var id1 = Guid.NewGuid(); +// var id2 = Guid.NewGuid(); + +// var initialPatterns = new InitialPatterns +// { +// { id1, new AppPattern("Number", "[0-9]") }, +// { id2, new AppPattern("Numbers", "[0-9]*") } +// }; + +// var app = new AppDomainObject(initialPatterns); + +// app.Create(CreateCommand(new CreateApp { Name = AppName, Actor = User, AppId = AppId })); + +// Assert.Equal(AppName, app.Snapshot.Name); + +// app.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateEvent(new AppCreated { Name = AppName }), +// CreateEvent(new AppContributorAssigned { ContributorId = User.Identifier, Permission = AppContributorPermission.Owner }), +// CreateEvent(new AppLanguageAdded { Language = Language.EN }), +// CreateEvent(new AppPatternAdded { PatternId = id1, Name = "Number", Pattern = "[0-9]" }), +// CreateEvent(new AppPatternAdded { PatternId = id2, Name = "Numbers", Pattern = "[0-9]*" }) +// ); +// } + +// [Fact] +// public void ChangePlan_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.ChangePlan(CreateCommand(new ChangePlan { PlanId = planId })); +// }); +// } + +// [Fact] +// public void ChangePlan_should_create_events() +// { +// CreateApp(); + +// sut.ChangePlan(CreateCommand(new ChangePlan { PlanId = planId })); + +// Assert.Equal(planId, sut.Snapshot.Plan.PlanId); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateEvent(new AppPlanChanged { PlanId = planId }) +// ); +// } + +// [Fact] +// public void AssignContributor_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.AssignContributor(CreateCommand(new AssignContributor { ContributorId = contributorId })); +// }); +// } + +// [Fact] +// public void AssignContributor_should_create_events() +// { +// CreateApp(); + +// sut.AssignContributor(CreateCommand(new AssignContributor { ContributorId = contributorId, Permission = AppContributorPermission.Editor })); + +// Assert.Equal(AppContributorPermission.Editor, sut.Snapshot.Contributors[contributorId]); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateEvent(new AppContributorAssigned { ContributorId = contributorId, Permission = AppContributorPermission.Editor }) +// ); +// } + +// [Fact] +// public void RemoveContributor_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.RemoveContributor(CreateCommand(new RemoveContributor { ContributorId = contributorId })); +// }); +// } + +// [Fact] +// public void RemoveContributor_should_create_events_and_remove_contributor() +// { +// CreateApp(); + +// sut.AssignContributor(CreateCommand(new AssignContributor { ContributorId = contributorId, Permission = AppContributorPermission.Editor })); +// sut.RemoveContributor(CreateCommand(new RemoveContributor { ContributorId = contributorId })); + +// Assert.False(sut.Snapshot.Contributors.ContainsKey(contributorId)); + +// sut.GetUncomittedEvents().Skip(1) +// .ShouldHaveSameEvents( +// CreateEvent(new AppContributorRemoved { ContributorId = contributorId }) +// ); +// } + +// [Fact] +// public void AttachClient_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.AttachClient(CreateCommand(new AttachClient { Id = clientId })); +// }); +// } + +// [Fact] +// public void AttachClient_should_create_events() +// { +// var command = new AttachClient { Id = clientId }; + +// CreateApp(); + +// sut.AttachClient(CreateCommand(command)); + +// Assert.True(sut.Snapshot.Clients.ContainsKey(clientId)); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateEvent(new AppClientAttached { Id = clientId, Secret = command.Secret }) +// ); +// } + +// [Fact] +// public void RevokeClient_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.RevokeClient(CreateCommand(new RevokeClient { Id = "not-found" })); +// }); +// } + +// [Fact] +// public void RevokeClient_should_create_events() +// { +// CreateApp(); +// CreateClient(); + +// sut.RevokeClient(CreateCommand(new RevokeClient { Id = clientId })); + +// Assert.False(sut.Snapshot.Clients.ContainsKey(clientId)); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateEvent(new AppClientRevoked { Id = clientId }) +// ); +// } + +// [Fact] +// public void UpdateClient_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.UpdateClient(CreateCommand(new UpdateClient { Id = "not-found", Name = clientNewName })); +// }); +// } + +// [Fact] +// public void UpdateClient_should_create_events() +// { +// CreateApp(); +// CreateClient(); + +// sut.UpdateClient(CreateCommand(new UpdateClient { Id = clientId, Name = clientNewName, Permission = AppClientPermission.Developer })); + +// Assert.Equal(clientNewName, sut.Snapshot.Clients[clientId].Name); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateEvent(new AppClientRenamed { Id = clientId, Name = clientNewName }), +// CreateEvent(new AppClientUpdated { Id = clientId, Permission = AppClientPermission.Developer }) +// ); +// } + +// [Fact] +// public void AddLanguage_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.AddLanguage(CreateCommand(new AddLanguage { Language = Language.DE })); +// }); +// } + +// [Fact] +// public void AddLanguage_should_create_events() +// { +// CreateApp(); + +// sut.AddLanguage(CreateCommand(new AddLanguage { Language = Language.DE })); + +// Assert.True(sut.Snapshot.LanguagesConfig.Contains(Language.DE)); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateEvent(new AppLanguageAdded { Language = Language.DE }) +// ); +// } + +// [Fact] +// public void RemoveLanguage_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.RemoveLanguage(CreateCommand(new RemoveLanguage { Language = Language.EN })); +// }); +// } + +// [Fact] +// public void RemoveLanguage_should_create_events() +// { +// CreateApp(); +// CreateLanguage(Language.DE); + +// sut.RemoveLanguage(CreateCommand(new RemoveLanguage { Language = Language.DE })); + +// Assert.False(sut.Snapshot.LanguagesConfig.Contains(Language.DE)); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateEvent(new AppLanguageRemoved { Language = Language.DE }) +// ); +// } + +// [Fact] +// public void UpdateLanguage_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.UpdateLanguage(CreateCommand(new UpdateLanguage { Language = Language.EN })); +// }); +// } + +// [Fact] +// public void UpdateLanguage_should_create_events() +// { +// CreateApp(); +// CreateLanguage(Language.DE); + +// sut.UpdateLanguage(CreateCommand(new UpdateLanguage { Language = Language.DE, Fallback = new List { Language.EN } })); + +// Assert.True(sut.Snapshot.LanguagesConfig.Contains(Language.DE)); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateEvent(new AppLanguageUpdated { Language = Language.DE, Fallback = new List { Language.EN } }) +// ); +// } + +// [Fact] +// public void AddPattern_should_throw_exception_if_app_not_created() +// { +// Assert.Throws(() => sut.AddPattern(CreateCommand(new AddPattern { PatternId = patternId, Name = "Any", Pattern = ".*" }))); +// } + +// [Fact] +// public void AddPattern_should_create_events() +// { +// CreateApp(); + +// sut.AddPattern(CreateCommand(new AddPattern { PatternId = patternId, Name = "Any", Pattern = ".*", Message = "Msg" })); + +// Assert.Single(sut.Snapshot.Patterns); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateEvent(new AppPatternAdded { PatternId = patternId, Name = "Any", Pattern = ".*", Message = "Msg" }) +// ); +// } + +// [Fact] +// public void DeletePattern_should_throw_exception_if_app_not_created() +// { +// Assert.Throws(() => +// { +// sut.DeletePattern(CreateCommand(new DeletePattern +// { +// PatternId = Guid.NewGuid() +// })); +// }); +// } + +// [Fact] +// public void DeletePattern_should_create_events() +// { +// CreateApp(); +// CreatePattern(); + +// sut.DeletePattern(CreateCommand(new DeletePattern { PatternId = patternId })); + +// Assert.Empty(sut.Snapshot.Patterns); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateEvent(new AppPatternDeleted { PatternId = patternId }) +// ); +// } + +// [Fact] +// public void UpdatePattern_should_throw_exception_if_app_not_created() +// { +// Assert.Throws(() => sut.UpdatePattern(CreateCommand(new UpdatePattern { PatternId = patternId, Name = "Any", Pattern = ".*" }))); +// } + +// [Fact] +// public void UpdatePattern_should_create_events() +// { +// CreateApp(); +// CreatePattern(); + +// sut.UpdatePattern(CreateCommand(new UpdatePattern { PatternId = patternId, Name = "Any", Pattern = ".*", Message = "Msg" })); + +// Assert.Single(sut.Snapshot.Patterns); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateEvent(new AppPatternUpdated { PatternId = patternId, Name = "Any", Pattern = ".*", Message = "Msg" }) +// ); +// } + +// private void CreatePattern() +// { +// sut.AddPattern(CreateCommand(new AddPattern { PatternId = patternId, Name = "Name", Pattern = ".*" })); +// sut.ClearUncommittedEvents(); +// } + +// private void CreateApp() +// { +// sut.Create(CreateCommand(new CreateApp { Name = AppName })); +// sut.ClearUncommittedEvents(); +// } + +// private void CreateClient() +// { +// sut.AttachClient(CreateCommand(new AttachClient { Id = clientId })); +// sut.ClearUncommittedEvents(); +// } + +// private void CreateLanguage(Language language) +// { +// sut.AddLanguage(CreateCommand(new AddLanguage { Language = language })); +// sut.ClearUncommittedEvents(); +// } +// } +//} diff --git a/tests/Squidex.Domain.Apps.Entities.Tests/Assets/AssetCommandMiddlewareTests.cs b/tests/Squidex.Domain.Apps.Entities.Tests/Assets/AssetCommandMiddlewareTests.cs index 599eaa005..44a1a36ca 100644 --- a/tests/Squidex.Domain.Apps.Entities.Tests/Assets/AssetCommandMiddlewareTests.cs +++ b/tests/Squidex.Domain.Apps.Entities.Tests/Assets/AssetCommandMiddlewareTests.cs @@ -1,145 +1,145 @@ -// ========================================================================== -// Squidex Headless CMS -// ========================================================================== -// Copyright (c) Squidex UG (haftungsbeschränkt) -// All rights reserved. Licensed under the MIT license. -// ========================================================================== - -using System; -using System.IO; -using System.Threading.Tasks; -using FakeItEasy; -using Squidex.Domain.Apps.Entities.Assets.Commands; -using Squidex.Domain.Apps.Entities.TestHelpers; -using Squidex.Infrastructure.Assets; -using Squidex.Infrastructure.Commands; -using Squidex.Infrastructure.Tasks; -using Xunit; - -namespace Squidex.Domain.Apps.Entities.Assets -{ - public class AssetCommandMiddlewareTests : HandlerTestBase - { - private readonly IAssetThumbnailGenerator assetThumbnailGenerator = A.Fake(); - private readonly IAssetStore assetStore = A.Fake(); - private readonly Guid assetId = Guid.NewGuid(); - private readonly Stream stream = new MemoryStream(); - private readonly ImageInfo image = new ImageInfo(2048, 2048); - private readonly AssetDomainObject asset = new AssetDomainObject(); - private readonly AssetFile file; - private readonly AssetCommandMiddleware sut; - - protected override Guid Id - { - get { return assetId; } - } - - public AssetCommandMiddlewareTests() - { - file = new AssetFile("my-image.png", "image/png", 1024, () => stream); - - sut = new AssetCommandMiddleware(Handler, assetStore, assetThumbnailGenerator); - } - - [Fact] - public async Task Create_should_create_domain_object() - { - var context = CreateContextForCommand(new CreateAsset { AssetId = assetId, File = file }); - - SetupStore(0, context.ContextId); - SetupImageInfo(); - - await TestCreate(asset, async _ => - { - await sut.HandleAsync(context); - }); - - Assert.Equal(assetId, context.Result>().IdOrValue); - - AssertAssetHasBeenUploaded(0, context.ContextId); - AssertAssetImageChecked(); - } - - [Fact] - public async Task Update_should_update_domain_object() - { - var context = CreateContextForCommand(new UpdateAsset { AssetId = assetId, File = file }); - - SetupStore(1, context.ContextId); - SetupImageInfo(); - - CreateAsset(); - - await TestUpdate(asset, async _ => - { - await sut.HandleAsync(context); - }); - - AssertAssetHasBeenUploaded(1, context.ContextId); - AssertAssetImageChecked(); - } - - [Fact] - public async Task Rename_should_update_domain_object() - { - CreateAsset(); - - var context = CreateContextForCommand(new RenameAsset { AssetId = assetId, FileName = "my-new-image.png" }); - - await TestUpdate(asset, async _ => - { - await sut.HandleAsync(context); - }); - } - - [Fact] - public async Task Delete_should_update_domain_object() - { - CreateAsset(); - - var command = CreateContextForCommand(new DeleteAsset { AssetId = assetId }); - - await TestUpdate(asset, async _ => - { - await sut.HandleAsync(command); - }); - } - - private void CreateAsset() - { - asset.Create(CreateCommand(new CreateAsset { File = file })); - } - - private void SetupImageInfo() - { - A.CallTo(() => assetThumbnailGenerator.GetImageInfoAsync(stream)) - .Returns(image); - } - - private void SetupStore(long version, Guid commitId) - { - A.CallTo(() => assetStore.UploadTemporaryAsync(commitId.ToString(), stream)) - .Returns(TaskHelper.Done); - A.CallTo(() => assetStore.CopyTemporaryAsync(commitId.ToString(), assetId.ToString(), version, null)) - .Returns(TaskHelper.Done); - A.CallTo(() => assetStore.DeleteTemporaryAsync(commitId.ToString())) - .Returns(TaskHelper.Done); - } - - private void AssertAssetImageChecked() - { - A.CallTo(() => assetThumbnailGenerator.GetImageInfoAsync(stream)) - .MustHaveHappened(); - } - - private void AssertAssetHasBeenUploaded(long version, Guid commitId) - { - A.CallTo(() => assetStore.UploadTemporaryAsync(commitId.ToString(), stream)) - .MustHaveHappened(); - A.CallTo(() => assetStore.CopyTemporaryAsync(commitId.ToString(), assetId.ToString(), version, null)) - .MustHaveHappened(); - A.CallTo(() => assetStore.DeleteTemporaryAsync(commitId.ToString())) - .MustHaveHappened(); - } - } -} +//// ========================================================================== +//// Squidex Headless CMS +//// ========================================================================== +//// Copyright (c) Squidex UG (haftungsbeschränkt) +//// All rights reserved. Licensed under the MIT license. +//// ========================================================================== + +//using System; +//using System.IO; +//using System.Threading.Tasks; +//using FakeItEasy; +//using Squidex.Domain.Apps.Entities.Assets.Commands; +//using Squidex.Domain.Apps.Entities.TestHelpers; +//using Squidex.Infrastructure.Assets; +//using Squidex.Infrastructure.Commands; +//using Squidex.Infrastructure.Tasks; +//using Xunit; + +//namespace Squidex.Domain.Apps.Entities.Assets +//{ +// public class AssetCommandMiddlewareTests : HandlerTestBase +// { +// private readonly IAssetThumbnailGenerator assetThumbnailGenerator = A.Fake(); +// private readonly IAssetStore assetStore = A.Fake(); +// private readonly Guid assetId = Guid.NewGuid(); +// private readonly Stream stream = new MemoryStream(); +// private readonly ImageInfo image = new ImageInfo(2048, 2048); +// private readonly AssetDomainObject asset = new AssetDomainObject(); +// private readonly AssetFile file; +// private readonly AssetCommandMiddleware sut; + +// protected override Guid Id +// { +// get { return assetId; } +// } + +// public AssetCommandMiddlewareTests() +// { +// file = new AssetFile("my-image.png", "image/png", 1024, () => stream); + +// sut = new AssetCommandMiddleware(Handler, assetStore, assetThumbnailGenerator); +// } + +// [Fact] +// public async Task Create_should_create_domain_object() +// { +// var context = CreateContextForCommand(new CreateAsset { AssetId = assetId, File = file }); + +// SetupStore(0, context.ContextId); +// SetupImageInfo(); + +// await TestCreate(asset, async _ => +// { +// await sut.HandleAsync(context); +// }); + +// Assert.Equal(assetId, context.Result>().IdOrValue); + +// AssertAssetHasBeenUploaded(0, context.ContextId); +// AssertAssetImageChecked(); +// } + +// [Fact] +// public async Task Update_should_update_domain_object() +// { +// var context = CreateContextForCommand(new UpdateAsset { AssetId = assetId, File = file }); + +// SetupStore(1, context.ContextId); +// SetupImageInfo(); + +// CreateAsset(); + +// await TestUpdate(asset, async _ => +// { +// await sut.HandleAsync(context); +// }); + +// AssertAssetHasBeenUploaded(1, context.ContextId); +// AssertAssetImageChecked(); +// } + +// [Fact] +// public async Task Rename_should_update_domain_object() +// { +// CreateAsset(); + +// var context = CreateContextForCommand(new RenameAsset { AssetId = assetId, FileName = "my-new-image.png" }); + +// await TestUpdate(asset, async _ => +// { +// await sut.HandleAsync(context); +// }); +// } + +// [Fact] +// public async Task Delete_should_update_domain_object() +// { +// CreateAsset(); + +// var command = CreateContextForCommand(new DeleteAsset { AssetId = assetId }); + +// await TestUpdate(asset, async _ => +// { +// await sut.HandleAsync(command); +// }); +// } + +// private void CreateAsset() +// { +// asset.Create(CreateCommand(new CreateAsset { File = file })); +// } + +// private void SetupImageInfo() +// { +// A.CallTo(() => assetThumbnailGenerator.GetImageInfoAsync(stream)) +// .Returns(image); +// } + +// private void SetupStore(long version, Guid commitId) +// { +// A.CallTo(() => assetStore.UploadTemporaryAsync(commitId.ToString(), stream)) +// .Returns(TaskHelper.Done); +// A.CallTo(() => assetStore.CopyTemporaryAsync(commitId.ToString(), assetId.ToString(), version, null)) +// .Returns(TaskHelper.Done); +// A.CallTo(() => assetStore.DeleteTemporaryAsync(commitId.ToString())) +// .Returns(TaskHelper.Done); +// } + +// private void AssertAssetImageChecked() +// { +// A.CallTo(() => assetThumbnailGenerator.GetImageInfoAsync(stream)) +// .MustHaveHappened(); +// } + +// private void AssertAssetHasBeenUploaded(long version, Guid commitId) +// { +// A.CallTo(() => assetStore.UploadTemporaryAsync(commitId.ToString(), stream)) +// .MustHaveHappened(); +// A.CallTo(() => assetStore.CopyTemporaryAsync(commitId.ToString(), assetId.ToString(), version, null)) +// .MustHaveHappened(); +// A.CallTo(() => assetStore.DeleteTemporaryAsync(commitId.ToString())) +// .MustHaveHappened(); +// } +// } +//} diff --git a/tests/Squidex.Domain.Apps.Entities.Tests/Assets/AssetDomainObjectTests.cs b/tests/Squidex.Domain.Apps.Entities.Tests/Assets/AssetDomainObjectTests.cs index b1c057749..d1c8aeae4 100644 --- a/tests/Squidex.Domain.Apps.Entities.Tests/Assets/AssetDomainObjectTests.cs +++ b/tests/Squidex.Domain.Apps.Entities.Tests/Assets/AssetDomainObjectTests.cs @@ -1,220 +1,220 @@ -// ========================================================================== -// Squidex Headless CMS -// ========================================================================== -// Copyright (c) Squidex UG (haftungsbeschränkt) -// All rights reserved. Licensed under the MIT license. -// ========================================================================== - -using System; -using System.IO; -using FakeItEasy; -using Squidex.Domain.Apps.Entities.Assets.Commands; -using Squidex.Domain.Apps.Entities.TestHelpers; -using Squidex.Domain.Apps.Events.Assets; -using Squidex.Infrastructure; -using Squidex.Infrastructure.Assets; -using Squidex.Infrastructure.States; -using Xunit; - -namespace Squidex.Domain.Apps.Entities.Assets -{ - public class AssetDomainObjectTests : HandlerTestBase - { - private readonly ImageInfo image = new ImageInfo(2048, 2048); - private readonly Guid assetId = Guid.NewGuid(); - private readonly AssetFile file = new AssetFile("my-image.png", "image/png", 1024, () => new MemoryStream()); - private readonly AssetDomainObject sut = new AssetDomainObject(); - - protected override Guid Id - { - get { return assetId; } - } - - public AssetDomainObjectTests() - { - sut.ActivateAsync(Id, A.Fake>()); - } - - [Fact] - public void Create_should_throw_exception_if_created() - { - CreateAsset(); - - Assert.Throws(() => - { - sut.Create(CreateAssetCommand(new CreateAsset { File = file })); - }); - } - - [Fact] - public void Create_should_create_events() - { - sut.Create(CreateAssetCommand(new CreateAsset { File = file, ImageInfo = image })); - - Assert.Equal(0, sut.Snapshot.FileVersion); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateAssetEvent(new AssetCreated - { - IsImage = true, - FileName = file.FileName, - FileSize = file.FileSize, - FileVersion = 0, - MimeType = file.MimeType, - PixelWidth = image.PixelWidth, - PixelHeight = image.PixelHeight - }) - ); - } - - [Fact] - public void Update_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.Update(CreateAssetCommand(new UpdateAsset { File = file })); - }); - } - - [Fact] - public void Update_should_throw_exception_if_asset_is_deleted() - { - CreateAsset(); - DeleteAsset(); - - Assert.Throws(() => - { - sut.Update(CreateAssetCommand(new UpdateAsset())); - }); - } - - [Fact] - public void Update_should_create_events() - { - CreateAsset(); - - sut.Update(CreateAssetCommand(new UpdateAsset { File = file, ImageInfo = image })); - - Assert.Equal(1, sut.Snapshot.FileVersion); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateAssetEvent(new AssetUpdated - { - IsImage = true, - FileSize = file.FileSize, - FileVersion = 1, - MimeType = file.MimeType, - PixelWidth = image.PixelWidth, - PixelHeight = image.PixelHeight - }) - ); - } - - [Fact] - public void Rename_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.Rename(CreateAssetCommand(new RenameAsset { FileName = "new-file.png" })); - }); - } - - [Fact] - public void Rename_should_throw_exception_if_asset_is_deleted() - { - CreateAsset(); - DeleteAsset(); - - Assert.Throws(() => - { - sut.Update(CreateAssetCommand(new UpdateAsset())); - }); - } - - [Fact] - public void Rename_should_create_events() - { - CreateAsset(); - - sut.Rename(CreateAssetCommand(new RenameAsset { FileName = "my-new-image.png" })); - - Assert.Equal("my-new-image.png", sut.Snapshot.FileName); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateAssetEvent(new AssetRenamed { FileName = "my-new-image.png" }) - ); - } - - [Fact] - public void Delete_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.Delete(CreateAssetCommand(new DeleteAsset())); - }); - } - - [Fact] - public void Delete_should_throw_exception_if_already_deleted() - { - CreateAsset(); - DeleteAsset(); - - Assert.Throws(() => - { - sut.Delete(CreateAssetCommand(new DeleteAsset())); - }); - } - - [Fact] - public void Delete_should_create_events_with_total_file_size() - { - CreateAsset(); - UpdateAsset(); - - sut.Delete(CreateAssetCommand(new DeleteAsset())); - - Assert.True(sut.Snapshot.IsDeleted); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateAssetEvent(new AssetDeleted { DeletedSize = 2048 }) - ); - } - - private void CreateAsset() - { - sut.Create(CreateAssetCommand(new CreateAsset { File = file })); - sut.ClearUncommittedEvents(); - } - - private void UpdateAsset() - { - sut.Update(CreateAssetCommand(new UpdateAsset { File = file })); - sut.ClearUncommittedEvents(); - } - - private void DeleteAsset() - { - sut.Delete(CreateAssetCommand(new DeleteAsset())); - sut.ClearUncommittedEvents(); - } - - protected T CreateAssetEvent(T @event) where T : AssetEvent - { - @event.AssetId = assetId; - - return CreateEvent(@event); - } - - protected T CreateAssetCommand(T command) where T : AssetCommand - { - command.AssetId = assetId; - - return CreateCommand(command); - } - } -} +//// ========================================================================== +//// Squidex Headless CMS +//// ========================================================================== +//// Copyright (c) Squidex UG (haftungsbeschränkt) +//// All rights reserved. Licensed under the MIT license. +//// ========================================================================== + +//using System; +//using System.IO; +//using FakeItEasy; +//using Squidex.Domain.Apps.Entities.Assets.Commands; +//using Squidex.Domain.Apps.Entities.TestHelpers; +//using Squidex.Domain.Apps.Events.Assets; +//using Squidex.Infrastructure; +//using Squidex.Infrastructure.Assets; +//using Squidex.Infrastructure.States; +//using Xunit; + +//namespace Squidex.Domain.Apps.Entities.Assets +//{ +// public class AssetDomainObjectTests : HandlerTestBase +// { +// private readonly ImageInfo image = new ImageInfo(2048, 2048); +// private readonly Guid assetId = Guid.NewGuid(); +// private readonly AssetFile file = new AssetFile("my-image.png", "image/png", 1024, () => new MemoryStream()); +// private readonly AssetDomainObject sut = new AssetDomainObject(); + +// protected override Guid Id +// { +// get { return assetId; } +// } + +// public AssetDomainObjectTests() +// { +// sut.ActivateAsync(Id, A.Fake>()); +// } + +// [Fact] +// public void Create_should_throw_exception_if_created() +// { +// CreateAsset(); + +// Assert.Throws(() => +// { +// sut.Create(CreateAssetCommand(new CreateAsset { File = file })); +// }); +// } + +// [Fact] +// public void Create_should_create_events() +// { +// sut.Create(CreateAssetCommand(new CreateAsset { File = file, ImageInfo = image })); + +// Assert.Equal(0, sut.Snapshot.FileVersion); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateAssetEvent(new AssetCreated +// { +// IsImage = true, +// FileName = file.FileName, +// FileSize = file.FileSize, +// FileVersion = 0, +// MimeType = file.MimeType, +// PixelWidth = image.PixelWidth, +// PixelHeight = image.PixelHeight +// }) +// ); +// } + +// [Fact] +// public void Update_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.Update(CreateAssetCommand(new UpdateAsset { File = file })); +// }); +// } + +// [Fact] +// public void Update_should_throw_exception_if_asset_is_deleted() +// { +// CreateAsset(); +// DeleteAsset(); + +// Assert.Throws(() => +// { +// sut.Update(CreateAssetCommand(new UpdateAsset())); +// }); +// } + +// [Fact] +// public void Update_should_create_events() +// { +// CreateAsset(); + +// sut.Update(CreateAssetCommand(new UpdateAsset { File = file, ImageInfo = image })); + +// Assert.Equal(1, sut.Snapshot.FileVersion); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateAssetEvent(new AssetUpdated +// { +// IsImage = true, +// FileSize = file.FileSize, +// FileVersion = 1, +// MimeType = file.MimeType, +// PixelWidth = image.PixelWidth, +// PixelHeight = image.PixelHeight +// }) +// ); +// } + +// [Fact] +// public void Rename_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.Rename(CreateAssetCommand(new RenameAsset { FileName = "new-file.png" })); +// }); +// } + +// [Fact] +// public void Rename_should_throw_exception_if_asset_is_deleted() +// { +// CreateAsset(); +// DeleteAsset(); + +// Assert.Throws(() => +// { +// sut.Update(CreateAssetCommand(new UpdateAsset())); +// }); +// } + +// [Fact] +// public void Rename_should_create_events() +// { +// CreateAsset(); + +// sut.Rename(CreateAssetCommand(new RenameAsset { FileName = "my-new-image.png" })); + +// Assert.Equal("my-new-image.png", sut.Snapshot.FileName); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateAssetEvent(new AssetRenamed { FileName = "my-new-image.png" }) +// ); +// } + +// [Fact] +// public void Delete_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.Delete(CreateAssetCommand(new DeleteAsset())); +// }); +// } + +// [Fact] +// public void Delete_should_throw_exception_if_already_deleted() +// { +// CreateAsset(); +// DeleteAsset(); + +// Assert.Throws(() => +// { +// sut.Delete(CreateAssetCommand(new DeleteAsset())); +// }); +// } + +// [Fact] +// public void Delete_should_create_events_with_total_file_size() +// { +// CreateAsset(); +// UpdateAsset(); + +// sut.Delete(CreateAssetCommand(new DeleteAsset())); + +// Assert.True(sut.Snapshot.IsDeleted); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateAssetEvent(new AssetDeleted { DeletedSize = 2048 }) +// ); +// } + +// private void CreateAsset() +// { +// sut.Create(CreateAssetCommand(new CreateAsset { File = file })); +// sut.ClearUncommittedEvents(); +// } + +// private void UpdateAsset() +// { +// sut.Update(CreateAssetCommand(new UpdateAsset { File = file })); +// sut.ClearUncommittedEvents(); +// } + +// private void DeleteAsset() +// { +// sut.Delete(CreateAssetCommand(new DeleteAsset())); +// sut.ClearUncommittedEvents(); +// } + +// protected T CreateAssetEvent(T @event) where T : AssetEvent +// { +// @event.AssetId = assetId; + +// return CreateEvent(@event); +// } + +// protected T CreateAssetCommand(T command) where T : AssetCommand +// { +// command.AssetId = assetId; + +// return CreateCommand(command); +// } +// } +//} diff --git a/tests/Squidex.Domain.Apps.Entities.Tests/Contents/ContentCommandMiddlewareTests.cs b/tests/Squidex.Domain.Apps.Entities.Tests/Contents/ContentCommandMiddlewareTests.cs index 113b574da..1ef316155 100644 --- a/tests/Squidex.Domain.Apps.Entities.Tests/Contents/ContentCommandMiddlewareTests.cs +++ b/tests/Squidex.Domain.Apps.Entities.Tests/Contents/ContentCommandMiddlewareTests.cs @@ -1,264 +1,264 @@ -// ========================================================================== -// Squidex Headless CMS -// ========================================================================== -// Copyright (c) Squidex UG (haftungsbeschränkt) -// All rights reserved. Licensed under the MIT license. -// ========================================================================== - -using System; -using System.Security.Claims; -using System.Threading.Tasks; -using FakeItEasy; -using NodaTime; -using Squidex.Domain.Apps.Core; -using Squidex.Domain.Apps.Core.Apps; -using Squidex.Domain.Apps.Core.Contents; -using Squidex.Domain.Apps.Core.Schemas; -using Squidex.Domain.Apps.Core.Scripting; -using Squidex.Domain.Apps.Entities.Apps; -using Squidex.Domain.Apps.Entities.Assets.Repositories; -using Squidex.Domain.Apps.Entities.Contents.Commands; -using Squidex.Domain.Apps.Entities.Contents.Repositories; -using Squidex.Domain.Apps.Entities.Schemas; -using Squidex.Domain.Apps.Entities.TestHelpers; -using Squidex.Infrastructure; -using Squidex.Infrastructure.Commands; -using Xunit; - -namespace Squidex.Domain.Apps.Entities.Contents -{ - public class ContentCommandMiddlewareTests : HandlerTestBase - { - private readonly ISchemaEntity schema = A.Fake(); - private readonly IScriptEngine scriptEngine = A.Fake(); - private readonly IAppProvider appProvider = A.Fake(); - private readonly IAppEntity app = A.Fake(); - private readonly ClaimsPrincipal user = new ClaimsPrincipal(); - private readonly LanguagesConfig languagesConfig = LanguagesConfig.Build(Language.DE); - private readonly Guid contentId = Guid.NewGuid(); - private readonly ContentDomainObject content = new ContentDomainObject(); - private readonly ContentCommandMiddleware sut; - - protected override Guid Id - { - get { return contentId; } - } - - private readonly NamedContentData invalidData = - new NamedContentData() - .AddField("my-field1", new ContentFieldData() - .AddValue(null)) - .AddField("my-field2", new ContentFieldData() - .AddValue(1)); - private readonly NamedContentData data = - new NamedContentData() - .AddField("my-field1", new ContentFieldData() - .AddValue(1)) - .AddField("my-field2", new ContentFieldData() - .AddValue(1)); - private readonly NamedContentData patch = - new NamedContentData() - .AddField("my-field1", new ContentFieldData() - .AddValue(1)); - - public ContentCommandMiddlewareTests() - { - var schemaDef = - new Schema("my-schema") - .AddField(new NumberField(1, "my-field1", Partitioning.Invariant, - new NumberFieldProperties { IsRequired = true })) - .AddField(new NumberField(2, "my-field2", Partitioning.Invariant, - new NumberFieldProperties { IsRequired = false })); - - sut = new ContentCommandMiddleware(Handler, appProvider, A.Dummy(), scriptEngine, A.Dummy()); - - A.CallTo(() => app.LanguagesConfig).Returns(languagesConfig); - - A.CallTo(() => appProvider.GetAppAsync(AppName)).Returns(app); - - A.CallTo(() => schema.SchemaDef).Returns(schemaDef); - A.CallTo(() => schema.ScriptCreate).Returns(""); - A.CallTo(() => schema.ScriptChange).Returns(""); - A.CallTo(() => schema.ScriptUpdate).Returns(""); - A.CallTo(() => schema.ScriptDelete).Returns(""); - - A.CallTo(() => appProvider.GetAppWithSchemaAsync(AppId, SchemaId)).Returns((app, schema)); - } - - [Fact] - public async Task Create_should_throw_exception_if_data_is_not_valid() - { - A.CallTo(() => scriptEngine.ExecuteAndTransform(A.Ignored, A.Ignored)) - .Returns(invalidData); - - var context = CreateContextForCommand(new CreateContent { ContentId = contentId, Data = invalidData, User = user }); - - await TestCreate(content, async _ => - { - await Assert.ThrowsAsync(() => sut.HandleAsync(context)); - }, false); - } - - [Fact] - public async Task Create_should_create_content() - { - A.CallTo(() => scriptEngine.ExecuteAndTransform(A.Ignored, A.Ignored)) - .Returns(data); - - var context = CreateContextForCommand(new CreateContent { ContentId = contentId, Data = data, User = user }); - - await TestCreate(content, async _ => - { - await sut.HandleAsync(context); - }); - - Assert.Equal(data, context.Result>().IdOrValue); - - A.CallTo(() => scriptEngine.ExecuteAndTransform(A.Ignored, "")).MustHaveHappened(); - A.CallTo(() => scriptEngine.Execute(A.Ignored, "")).MustNotHaveHappened(); - } - - [Fact] - public async Task Create_should_also_invoke_publish_script_when_publishing() - { - A.CallTo(() => scriptEngine.ExecuteAndTransform(A.Ignored, A.Ignored)) - .Returns(data); - - var context = CreateContextForCommand(new CreateContent { ContentId = contentId, Data = data, User = user, Publish = true }); - - await TestCreate(content, async _ => - { - await sut.HandleAsync(context); - }); - - Assert.Equal(data, context.Result>().IdOrValue); - - A.CallTo(() => scriptEngine.ExecuteAndTransform(A.Ignored, "")).MustHaveHappened(); - A.CallTo(() => scriptEngine.Execute(A.Ignored, "")).MustHaveHappened(); - } - - [Fact] - public async Task Update_should_throw_exception_if_data_is_not_valid() - { - A.CallTo(() => scriptEngine.ExecuteAndTransform(A.Ignored, A.Ignored)) - .Returns(invalidData); +//// ========================================================================== +//// Squidex Headless CMS +//// ========================================================================== +//// Copyright (c) Squidex UG (haftungsbeschränkt) +//// All rights reserved. Licensed under the MIT license. +//// ========================================================================== + +//using System; +//using System.Security.Claims; +//using System.Threading.Tasks; +//using FakeItEasy; +//using NodaTime; +//using Squidex.Domain.Apps.Core; +//using Squidex.Domain.Apps.Core.Apps; +//using Squidex.Domain.Apps.Core.Contents; +//using Squidex.Domain.Apps.Core.Schemas; +//using Squidex.Domain.Apps.Core.Scripting; +//using Squidex.Domain.Apps.Entities.Apps; +//using Squidex.Domain.Apps.Entities.Assets.Repositories; +//using Squidex.Domain.Apps.Entities.Contents.Commands; +//using Squidex.Domain.Apps.Entities.Contents.Repositories; +//using Squidex.Domain.Apps.Entities.Schemas; +//using Squidex.Domain.Apps.Entities.TestHelpers; +//using Squidex.Infrastructure; +//using Squidex.Infrastructure.Commands; +//using Xunit; + +//namespace Squidex.Domain.Apps.Entities.Contents +//{ +// public class ContentCommandMiddlewareTests : HandlerTestBase +// { +// private readonly ISchemaEntity schema = A.Fake(); +// private readonly IScriptEngine scriptEngine = A.Fake(); +// private readonly IAppProvider appProvider = A.Fake(); +// private readonly IAppEntity app = A.Fake(); +// private readonly ClaimsPrincipal user = new ClaimsPrincipal(); +// private readonly LanguagesConfig languagesConfig = LanguagesConfig.Build(Language.DE); +// private readonly Guid contentId = Guid.NewGuid(); +// private readonly ContentDomainObject content = new ContentDomainObject(); +// private readonly ContentCommandMiddleware sut; + +// protected override Guid Id +// { +// get { return contentId; } +// } + +// private readonly NamedContentData invalidData = +// new NamedContentData() +// .AddField("my-field1", new ContentFieldData() +// .AddValue(null)) +// .AddField("my-field2", new ContentFieldData() +// .AddValue(1)); +// private readonly NamedContentData data = +// new NamedContentData() +// .AddField("my-field1", new ContentFieldData() +// .AddValue(1)) +// .AddField("my-field2", new ContentFieldData() +// .AddValue(1)); +// private readonly NamedContentData patch = +// new NamedContentData() +// .AddField("my-field1", new ContentFieldData() +// .AddValue(1)); + +// public ContentCommandMiddlewareTests() +// { +// var schemaDef = +// new Schema("my-schema") +// .AddField(new NumberField(1, "my-field1", Partitioning.Invariant, +// new NumberFieldProperties { IsRequired = true })) +// .AddField(new NumberField(2, "my-field2", Partitioning.Invariant, +// new NumberFieldProperties { IsRequired = false })); + +// sut = new ContentCommandMiddleware(Handler, appProvider, A.Dummy(), scriptEngine, A.Dummy()); + +// A.CallTo(() => app.LanguagesConfig).Returns(languagesConfig); + +// A.CallTo(() => appProvider.GetAppAsync(AppName)).Returns(app); + +// A.CallTo(() => schema.SchemaDef).Returns(schemaDef); +// A.CallTo(() => schema.ScriptCreate).Returns(""); +// A.CallTo(() => schema.ScriptChange).Returns(""); +// A.CallTo(() => schema.ScriptUpdate).Returns(""); +// A.CallTo(() => schema.ScriptDelete).Returns(""); + +// A.CallTo(() => appProvider.GetAppWithSchemaAsync(AppId, SchemaId)).Returns((app, schema)); +// } + +// [Fact] +// public async Task Create_should_throw_exception_if_data_is_not_valid() +// { +// A.CallTo(() => scriptEngine.ExecuteAndTransform(A.Ignored, A.Ignored)) +// .Returns(invalidData); + +// var context = CreateContextForCommand(new CreateContent { ContentId = contentId, Data = invalidData, User = user }); + +// await TestCreate(content, async _ => +// { +// await Assert.ThrowsAsync(() => sut.HandleAsync(context)); +// }, false); +// } + +// [Fact] +// public async Task Create_should_create_content() +// { +// A.CallTo(() => scriptEngine.ExecuteAndTransform(A.Ignored, A.Ignored)) +// .Returns(data); + +// var context = CreateContextForCommand(new CreateContent { ContentId = contentId, Data = data, User = user }); + +// await TestCreate(content, async _ => +// { +// await sut.HandleAsync(context); +// }); + +// Assert.Equal(data, context.Result>().IdOrValue); + +// A.CallTo(() => scriptEngine.ExecuteAndTransform(A.Ignored, "")).MustHaveHappened(); +// A.CallTo(() => scriptEngine.Execute(A.Ignored, "")).MustNotHaveHappened(); +// } + +// [Fact] +// public async Task Create_should_also_invoke_publish_script_when_publishing() +// { +// A.CallTo(() => scriptEngine.ExecuteAndTransform(A.Ignored, A.Ignored)) +// .Returns(data); + +// var context = CreateContextForCommand(new CreateContent { ContentId = contentId, Data = data, User = user, Publish = true }); + +// await TestCreate(content, async _ => +// { +// await sut.HandleAsync(context); +// }); + +// Assert.Equal(data, context.Result>().IdOrValue); + +// A.CallTo(() => scriptEngine.ExecuteAndTransform(A.Ignored, "")).MustHaveHappened(); +// A.CallTo(() => scriptEngine.Execute(A.Ignored, "")).MustHaveHappened(); +// } + +// [Fact] +// public async Task Update_should_throw_exception_if_data_is_not_valid() +// { +// A.CallTo(() => scriptEngine.ExecuteAndTransform(A.Ignored, A.Ignored)) +// .Returns(invalidData); - CreateContent(); - - var context = CreateContextForCommand(new UpdateContent { ContentId = contentId, Data = invalidData, User = user }); +// CreateContent(); + +// var context = CreateContextForCommand(new UpdateContent { ContentId = contentId, Data = invalidData, User = user }); - await TestUpdate(content, async _ => - { - await Assert.ThrowsAsync(() => sut.HandleAsync(context)); - }, false); - } +// await TestUpdate(content, async _ => +// { +// await Assert.ThrowsAsync(() => sut.HandleAsync(context)); +// }, false); +// } - [Fact] - public async Task Update_should_update_domain_object() - { - A.CallTo(() => scriptEngine.ExecuteAndTransform(A.Ignored, A.Ignored)) - .Returns(data); +// [Fact] +// public async Task Update_should_update_domain_object() +// { +// A.CallTo(() => scriptEngine.ExecuteAndTransform(A.Ignored, A.Ignored)) +// .Returns(data); - CreateContent(); +// CreateContent(); - var context = CreateContextForCommand(new UpdateContent { ContentId = contentId, Data = data, User = user }); +// var context = CreateContextForCommand(new UpdateContent { ContentId = contentId, Data = data, User = user }); - await TestUpdate(content, async _ => - { - await sut.HandleAsync(context); - }); +// await TestUpdate(content, async _ => +// { +// await sut.HandleAsync(context); +// }); - Assert.Equal(data, context.Result().Data); +// Assert.Equal(data, context.Result().Data); - A.CallTo(() => scriptEngine.ExecuteAndTransform(A.Ignored, "")).MustHaveHappened(); - } +// A.CallTo(() => scriptEngine.ExecuteAndTransform(A.Ignored, "")).MustHaveHappened(); +// } - [Fact] - public async Task Patch_should_throw_exception_if_data_is_not_valid() - { - A.CallTo(() => scriptEngine.ExecuteAndTransform(A.Ignored, A.Ignored)) - .Returns(invalidData); +// [Fact] +// public async Task Patch_should_throw_exception_if_data_is_not_valid() +// { +// A.CallTo(() => scriptEngine.ExecuteAndTransform(A.Ignored, A.Ignored)) +// .Returns(invalidData); - CreateContent(); +// CreateContent(); - var context = CreateContextForCommand(new PatchContent { ContentId = contentId, Data = invalidData, User = user }); +// var context = CreateContextForCommand(new PatchContent { ContentId = contentId, Data = invalidData, User = user }); - await TestUpdate(content, async _ => - { - await Assert.ThrowsAsync(() => sut.HandleAsync(context)); - }, false); - } +// await TestUpdate(content, async _ => +// { +// await Assert.ThrowsAsync(() => sut.HandleAsync(context)); +// }, false); +// } - [Fact] - public async Task Patch_should_update_domain_object() - { - A.CallTo(() => scriptEngine.ExecuteAndTransform(A.Ignored, A.Ignored)) - .Returns(data); +// [Fact] +// public async Task Patch_should_update_domain_object() +// { +// A.CallTo(() => scriptEngine.ExecuteAndTransform(A.Ignored, A.Ignored)) +// .Returns(data); - A.CallTo(() => scriptEngine.ExecuteAndTransform(A.Ignored, A.Ignored)).Returns(patch); +// A.CallTo(() => scriptEngine.ExecuteAndTransform(A.Ignored, A.Ignored)).Returns(patch); - CreateContent(); +// CreateContent(); - var context = CreateContextForCommand(new PatchContent { ContentId = contentId, Data = patch, User = user }); +// var context = CreateContextForCommand(new PatchContent { ContentId = contentId, Data = patch, User = user }); - await TestUpdate(content, async _ => - { - await sut.HandleAsync(context); - }); +// await TestUpdate(content, async _ => +// { +// await sut.HandleAsync(context); +// }); - Assert.NotNull(context.Result().Data); +// Assert.NotNull(context.Result().Data); - A.CallTo(() => scriptEngine.ExecuteAndTransform(A.Ignored, "")).MustHaveHappened(); - } +// A.CallTo(() => scriptEngine.ExecuteAndTransform(A.Ignored, "")).MustHaveHappened(); +// } - [Fact] - public async Task ChangeStatus_should_publish_domain_object() - { - CreateContent(); +// [Fact] +// public async Task ChangeStatus_should_publish_domain_object() +// { +// CreateContent(); - var context = CreateContextForCommand(new ChangeContentStatus { ContentId = contentId, User = user, Status = Status.Published }); +// var context = CreateContextForCommand(new ChangeContentStatus { ContentId = contentId, User = user, Status = Status.Published }); - await TestUpdate(content, async _ => - { - await sut.HandleAsync(context); - }); +// await TestUpdate(content, async _ => +// { +// await sut.HandleAsync(context); +// }); - A.CallTo(() => scriptEngine.Execute(A.Ignored, "")).MustHaveHappened(); - } +// A.CallTo(() => scriptEngine.Execute(A.Ignored, "")).MustHaveHappened(); +// } - [Fact] - public async Task ChangeStatus_should_not_invoke_scripts_when_scheduled() - { - CreateContent(); +// [Fact] +// public async Task ChangeStatus_should_not_invoke_scripts_when_scheduled() +// { +// CreateContent(); - var context = CreateContextForCommand(new ChangeContentStatus { ContentId = contentId, User = user, Status = Status.Published, DueTime = Instant.MaxValue }); +// var context = CreateContextForCommand(new ChangeContentStatus { ContentId = contentId, User = user, Status = Status.Published, DueTime = Instant.MaxValue }); - await TestUpdate(content, async _ => - { - await sut.HandleAsync(context); - }); +// await TestUpdate(content, async _ => +// { +// await sut.HandleAsync(context); +// }); - A.CallTo(() => scriptEngine.Execute(A.Ignored, "")).MustNotHaveHappened(); - } +// A.CallTo(() => scriptEngine.Execute(A.Ignored, "")).MustNotHaveHappened(); +// } - [Fact] - public async Task Delete_should_update_domain_object() - { - CreateContent(); +// [Fact] +// public async Task Delete_should_update_domain_object() +// { +// CreateContent(); - var command = CreateContextForCommand(new DeleteContent { ContentId = contentId, User = user }); +// var command = CreateContextForCommand(new DeleteContent { ContentId = contentId, User = user }); - await TestUpdate(content, async _ => - { - await sut.HandleAsync(command); - }); +// await TestUpdate(content, async _ => +// { +// await sut.HandleAsync(command); +// }); - A.CallTo(() => scriptEngine.Execute(A.Ignored, "")).MustHaveHappened(); - } +// A.CallTo(() => scriptEngine.Execute(A.Ignored, "")).MustHaveHappened(); +// } - private void CreateContent() - { - content.Create(CreateCommand(new CreateContent { Data = data })); - } - } -} +// private void CreateContent() +// { +// content.Create(CreateCommand(new CreateContent { Data = data })); +// } +// } +//} diff --git a/tests/Squidex.Domain.Apps.Entities.Tests/Contents/ContentDomainObjectTests.cs b/tests/Squidex.Domain.Apps.Entities.Tests/Contents/ContentDomainObjectTests.cs index 59e7e0213..b6d428fdf 100644 --- a/tests/Squidex.Domain.Apps.Entities.Tests/Contents/ContentDomainObjectTests.cs +++ b/tests/Squidex.Domain.Apps.Entities.Tests/Contents/ContentDomainObjectTests.cs @@ -1,304 +1,304 @@ -// ========================================================================== -// Squidex Headless CMS -// ========================================================================== -// Copyright (c) Squidex UG (haftungsbeschränkt) -// All rights reserved. Licensed under the MIT license. -// ========================================================================== - -using System; -using FakeItEasy; -using FluentAssertions; -using NodaTime; -using Squidex.Domain.Apps.Core.Contents; -using Squidex.Domain.Apps.Entities.Contents.Commands; -using Squidex.Domain.Apps.Entities.TestHelpers; -using Squidex.Domain.Apps.Events.Contents; -using Squidex.Infrastructure; -using Squidex.Infrastructure.States; -using Xunit; - -namespace Squidex.Domain.Apps.Entities.Contents -{ - public class ContentDomainObjectTests : HandlerTestBase - { - private readonly NamedContentData data = - new NamedContentData() - .AddField("field1", - new ContentFieldData() - .AddValue("iv", 1)); - private readonly NamedContentData otherData = - new NamedContentData() - .AddField("field2", - new ContentFieldData() - .AddValue("iv", 2)); - private readonly NamedContentData patched; - private readonly Guid contentId = Guid.NewGuid(); - private readonly ContentDomainObject sut = new ContentDomainObject(); - - protected override Guid Id - { - get { return contentId; } - } - - public ContentDomainObjectTests() - { - patched = otherData.MergeInto(data); - - sut.ActivateAsync(Id, A.Fake>()); - } - - [Fact] - public void Create_should_throw_exception_if_created() - { - sut.Create(CreateCommand(new CreateContent { Data = data })); - - Assert.Throws(() => - { - sut.Create(CreateContentCommand(new CreateContent { Data = data })); - }); - } - - [Fact] - public void Create_should_create_events() - { - sut.Create(CreateContentCommand(new CreateContent { Data = data })); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateContentEvent(new ContentCreated { Data = data }) - ); - } - - [Fact] - public void Create_should_also_publish_if_set_to_true() - { - sut.Create(CreateContentCommand(new CreateContent { Data = data, Publish = true })); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateContentEvent(new ContentCreated { Data = data }), - CreateContentEvent(new ContentStatusChanged { Status = Status.Published }) - ); - } - - [Fact] - public void Update_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.Update(CreateContentCommand(new UpdateContent { Data = data })); - }); - } - - [Fact] - public void Update_should_throw_exception_if_content_is_deleted() - { - CreateContent(); - DeleteContent(); - - Assert.Throws(() => - { - sut.Update(CreateContentCommand(new UpdateContent())); - }); - } - - [Fact] - public void Update_should_create_events() - { - CreateContent(); - - sut.Update(CreateContentCommand(new UpdateContent { Data = otherData })); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateContentEvent(new ContentUpdated { Data = otherData }) - ); - } - - [Fact] - public void Update_should_not_create_event_for_same_data() - { - CreateContent(); - UpdateContent(); - - sut.Update(CreateContentCommand(new UpdateContent { Data = data })); - - sut.GetUncomittedEvents().Should().BeEmpty(); - } - - [Fact] - public void Patch_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.Patch(CreateContentCommand(new PatchContent { Data = data })); - }); - } - - [Fact] - public void Patch_should_throw_exception_if_content_is_deleted() - { - CreateContent(); - DeleteContent(); - - Assert.Throws(() => - { - sut.Patch(CreateContentCommand(new PatchContent())); - }); - } - - [Fact] - public void Patch_should_create_events() - { - CreateContent(); - UpdateContent(); - - sut.Patch(CreateContentCommand(new PatchContent { Data = otherData })); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateContentEvent(new ContentUpdated { Data = patched }) - ); - } - - [Fact] - public void Patch_should_not_create_event_for_same_data() - { - CreateContent(); - UpdateContent(); - - sut.Patch(CreateContentCommand(new PatchContent { Data = data })); - - sut.GetUncomittedEvents().Should().BeEmpty(); - } - - [Fact] - public void ChangeStatus_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.ChangeStatus(CreateContentCommand(new ChangeContentStatus())); - }); - } - - [Fact] - public void ChangeStatus_should_throw_exception_if_content_is_deleted() - { - CreateContent(); - DeleteContent(); - - Assert.Throws(() => - { - sut.ChangeStatus(CreateContentCommand(new ChangeContentStatus())); - }); - } - - [Fact] - public void ChangeStatus_should_refresh_properties_and_create_events() - { - CreateContent(); - - sut.ChangeStatus(CreateContentCommand(new ChangeContentStatus { Status = Status.Published })); - - Assert.Equal(Status.Published, sut.Snapshot.Status); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateContentEvent(new ContentStatusChanged { Status = Status.Published }) - ); - } - - [Fact] - public void ChangeStatus_should_refresh_properties_and_create_scheduled_events_when_command_has_due_time() - { - CreateContent(); - - var dueTime = Instant.MaxValue; - - sut.ChangeStatus(CreateContentCommand(new ChangeContentStatus { Status = Status.Published, DueTime = dueTime })); - - Assert.Equal(Status.Draft, sut.Snapshot.Status); - Assert.Equal(Status.Published, sut.Snapshot.ScheduledTo); - Assert.Equal(dueTime, sut.Snapshot.ScheduledAt); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateContentEvent(new ContentStatusScheduled { Status = Status.Published, DueTime = dueTime }) - ); - } - - [Fact] - public void Delete_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.Delete(CreateContentCommand(new DeleteContent())); - }); - } - - [Fact] - public void Delete_should_throw_exception_if_already_deleted() - { - CreateContent(); - DeleteContent(); - - Assert.Throws(() => - { - sut.Delete(CreateContentCommand(new DeleteContent())); - }); - } - - [Fact] - public void Delete_should_update_properties_and_create_events() - { - CreateContent(); - - sut.Delete(CreateContentCommand(new DeleteContent())); - - Assert.True(sut.Snapshot.IsDeleted); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateContentEvent(new ContentDeleted()) - ); - } - - private void CreateContent() - { - sut.Create(CreateContentCommand(new CreateContent { Data = data })); - sut.ClearUncommittedEvents(); - } - - private void UpdateContent() - { - sut.Update(CreateContentCommand(new UpdateContent { Data = data })); - sut.ClearUncommittedEvents(); - } - - private void ChangeStatus(Status status) - { - sut.ChangeStatus(CreateContentCommand(new ChangeContentStatus { Status = status })); - sut.ClearUncommittedEvents(); - } - - private void DeleteContent() - { - sut.Delete(CreateContentCommand(new DeleteContent())); - sut.ClearUncommittedEvents(); - } - - protected T CreateContentEvent(T @event) where T : ContentEvent - { - @event.ContentId = contentId; - - return CreateEvent(@event); - } - - protected T CreateContentCommand(T command) where T : ContentCommand - { - command.ContentId = contentId; - - return CreateCommand(command); - } - } -} +//// ========================================================================== +//// Squidex Headless CMS +//// ========================================================================== +//// Copyright (c) Squidex UG (haftungsbeschränkt) +//// All rights reserved. Licensed under the MIT license. +//// ========================================================================== + +//using System; +//using FakeItEasy; +//using FluentAssertions; +//using NodaTime; +//using Squidex.Domain.Apps.Core.Contents; +//using Squidex.Domain.Apps.Entities.Contents.Commands; +//using Squidex.Domain.Apps.Entities.TestHelpers; +//using Squidex.Domain.Apps.Events.Contents; +//using Squidex.Infrastructure; +//using Squidex.Infrastructure.States; +//using Xunit; + +//namespace Squidex.Domain.Apps.Entities.Contents +//{ +// public class ContentDomainObjectTests : HandlerTestBase +// { +// private readonly NamedContentData data = +// new NamedContentData() +// .AddField("field1", +// new ContentFieldData() +// .AddValue("iv", 1)); +// private readonly NamedContentData otherData = +// new NamedContentData() +// .AddField("field2", +// new ContentFieldData() +// .AddValue("iv", 2)); +// private readonly NamedContentData patched; +// private readonly Guid contentId = Guid.NewGuid(); +// private readonly ContentDomainObject sut = new ContentDomainObject(); + +// protected override Guid Id +// { +// get { return contentId; } +// } + +// public ContentDomainObjectTests() +// { +// patched = otherData.MergeInto(data); + +// sut.ActivateAsync(Id, A.Fake>()); +// } + +// [Fact] +// public void Create_should_throw_exception_if_created() +// { +// sut.Create(CreateCommand(new CreateContent { Data = data })); + +// Assert.Throws(() => +// { +// sut.Create(CreateContentCommand(new CreateContent { Data = data })); +// }); +// } + +// [Fact] +// public void Create_should_create_events() +// { +// sut.Create(CreateContentCommand(new CreateContent { Data = data })); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateContentEvent(new ContentCreated { Data = data }) +// ); +// } + +// [Fact] +// public void Create_should_also_publish_if_set_to_true() +// { +// sut.Create(CreateContentCommand(new CreateContent { Data = data, Publish = true })); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateContentEvent(new ContentCreated { Data = data }), +// CreateContentEvent(new ContentStatusChanged { Status = Status.Published }) +// ); +// } + +// [Fact] +// public void Update_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.Update(CreateContentCommand(new UpdateContent { Data = data })); +// }); +// } + +// [Fact] +// public void Update_should_throw_exception_if_content_is_deleted() +// { +// CreateContent(); +// DeleteContent(); + +// Assert.Throws(() => +// { +// sut.Update(CreateContentCommand(new UpdateContent())); +// }); +// } + +// [Fact] +// public void Update_should_create_events() +// { +// CreateContent(); + +// sut.Update(CreateContentCommand(new UpdateContent { Data = otherData })); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateContentEvent(new ContentUpdated { Data = otherData }) +// ); +// } + +// [Fact] +// public void Update_should_not_create_event_for_same_data() +// { +// CreateContent(); +// UpdateContent(); + +// sut.Update(CreateContentCommand(new UpdateContent { Data = data })); + +// sut.GetUncomittedEvents().Should().BeEmpty(); +// } + +// [Fact] +// public void Patch_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.Patch(CreateContentCommand(new PatchContent { Data = data })); +// }); +// } + +// [Fact] +// public void Patch_should_throw_exception_if_content_is_deleted() +// { +// CreateContent(); +// DeleteContent(); + +// Assert.Throws(() => +// { +// sut.Patch(CreateContentCommand(new PatchContent())); +// }); +// } + +// [Fact] +// public void Patch_should_create_events() +// { +// CreateContent(); +// UpdateContent(); + +// sut.Patch(CreateContentCommand(new PatchContent { Data = otherData })); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateContentEvent(new ContentUpdated { Data = patched }) +// ); +// } + +// [Fact] +// public void Patch_should_not_create_event_for_same_data() +// { +// CreateContent(); +// UpdateContent(); + +// sut.Patch(CreateContentCommand(new PatchContent { Data = data })); + +// sut.GetUncomittedEvents().Should().BeEmpty(); +// } + +// [Fact] +// public void ChangeStatus_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.ChangeStatus(CreateContentCommand(new ChangeContentStatus())); +// }); +// } + +// [Fact] +// public void ChangeStatus_should_throw_exception_if_content_is_deleted() +// { +// CreateContent(); +// DeleteContent(); + +// Assert.Throws(() => +// { +// sut.ChangeStatus(CreateContentCommand(new ChangeContentStatus())); +// }); +// } + +// [Fact] +// public void ChangeStatus_should_refresh_properties_and_create_events() +// { +// CreateContent(); + +// sut.ChangeStatus(CreateContentCommand(new ChangeContentStatus { Status = Status.Published })); + +// Assert.Equal(Status.Published, sut.Snapshot.Status); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateContentEvent(new ContentStatusChanged { Status = Status.Published }) +// ); +// } + +// [Fact] +// public void ChangeStatus_should_refresh_properties_and_create_scheduled_events_when_command_has_due_time() +// { +// CreateContent(); + +// var dueTime = Instant.MaxValue; + +// sut.ChangeStatus(CreateContentCommand(new ChangeContentStatus { Status = Status.Published, DueTime = dueTime })); + +// Assert.Equal(Status.Draft, sut.Snapshot.Status); +// Assert.Equal(Status.Published, sut.Snapshot.ScheduledTo); +// Assert.Equal(dueTime, sut.Snapshot.ScheduledAt); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateContentEvent(new ContentStatusScheduled { Status = Status.Published, DueTime = dueTime }) +// ); +// } + +// [Fact] +// public void Delete_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.Delete(CreateContentCommand(new DeleteContent())); +// }); +// } + +// [Fact] +// public void Delete_should_throw_exception_if_already_deleted() +// { +// CreateContent(); +// DeleteContent(); + +// Assert.Throws(() => +// { +// sut.Delete(CreateContentCommand(new DeleteContent())); +// }); +// } + +// [Fact] +// public void Delete_should_update_properties_and_create_events() +// { +// CreateContent(); + +// sut.Delete(CreateContentCommand(new DeleteContent())); + +// Assert.True(sut.Snapshot.IsDeleted); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateContentEvent(new ContentDeleted()) +// ); +// } + +// private void CreateContent() +// { +// sut.Create(CreateContentCommand(new CreateContent { Data = data })); +// sut.ClearUncommittedEvents(); +// } + +// private void UpdateContent() +// { +// sut.Update(CreateContentCommand(new UpdateContent { Data = data })); +// sut.ClearUncommittedEvents(); +// } + +// private void ChangeStatus(Status status) +// { +// sut.ChangeStatus(CreateContentCommand(new ChangeContentStatus { Status = status })); +// sut.ClearUncommittedEvents(); +// } + +// private void DeleteContent() +// { +// sut.Delete(CreateContentCommand(new DeleteContent())); +// sut.ClearUncommittedEvents(); +// } + +// protected T CreateContentEvent(T @event) where T : ContentEvent +// { +// @event.ContentId = contentId; + +// return CreateEvent(@event); +// } + +// protected T CreateContentCommand(T command) where T : ContentCommand +// { +// command.ContentId = contentId; + +// return CreateCommand(command); +// } +// } +//} diff --git a/tests/Squidex.Domain.Apps.Entities.Tests/Rules/RuleCommandMiddlewareTests.cs b/tests/Squidex.Domain.Apps.Entities.Tests/Rules/RuleCommandMiddlewareTests.cs deleted file mode 100644 index 863bd6d97..000000000 --- a/tests/Squidex.Domain.Apps.Entities.Tests/Rules/RuleCommandMiddlewareTests.cs +++ /dev/null @@ -1,118 +0,0 @@ -// ========================================================================== -// Squidex Headless CMS -// ========================================================================== -// Copyright (c) Squidex UG (haftungsbeschränkt) -// All rights reserved. Licensed under the MIT license. -// ========================================================================== - -using System; -using System.Threading.Tasks; -using FakeItEasy; -using Squidex.Domain.Apps.Core.Rules; -using Squidex.Domain.Apps.Core.Rules.Actions; -using Squidex.Domain.Apps.Core.Rules.Triggers; -using Squidex.Domain.Apps.Entities.Rules.Commands; -using Squidex.Domain.Apps.Entities.Schemas; -using Squidex.Domain.Apps.Entities.TestHelpers; -using Squidex.Infrastructure.Commands; -using Xunit; - -namespace Squidex.Domain.Apps.Entities.Rules -{ - public class RuleCommandMiddlewareTests : HandlerTestBase - { - private readonly IAppProvider appProvider = A.Fake(); - private readonly RuleDomainObject rule = new RuleDomainObject(); - private readonly RuleTrigger ruleTrigger = new ContentChangedTrigger(); - private readonly RuleAction ruleAction = new WebhookAction { Url = new Uri("https://squidex.io") }; - private readonly Guid ruleId = Guid.NewGuid(); - private readonly RuleCommandMiddleware sut; - - protected override Guid Id - { - get { return ruleId; } - } - - public RuleCommandMiddlewareTests() - { - A.CallTo(() => appProvider.GetSchemaAsync(A.Ignored, A.Ignored, false)) - .Returns(A.Fake()); - - sut = new RuleCommandMiddleware(Handler, appProvider); - } - - [Fact] - public async Task Create_should_create_domain_object() - { - var context = CreateContextForCommand(new CreateRule { Trigger = ruleTrigger, Action = ruleAction }); - - await TestCreate(rule, async _ => - { - await sut.HandleAsync(context); - }); - } - - [Fact] - public async Task Update_should_update_domain_object() - { - var context = CreateContextForCommand(new UpdateRule { Trigger = ruleTrigger, Action = ruleAction }); - - CreateRule(); - - await TestUpdate(rule, async _ => - { - await sut.HandleAsync(context); - }); - } - - [Fact] - public async Task Enable_should_update_domain_object() - { - CreateRule(); - DisableRule(); - - var command = CreateContextForCommand(new EnableRule { RuleId = ruleId }); - - await TestUpdate(rule, async _ => - { - await sut.HandleAsync(command); - }); - } - - [Fact] - public async Task Disable_should_update_domain_object() - { - CreateRule(); - - var command = CreateContextForCommand(new DisableRule { RuleId = ruleId }); - - await TestUpdate(rule, async _ => - { - await sut.HandleAsync(command); - }); - } - - [Fact] - public async Task Delete_should_update_domain_object() - { - CreateRule(); - - var command = CreateContextForCommand(new DeleteRule { RuleId = ruleId }); - - await TestUpdate(rule, async _ => - { - await sut.HandleAsync(command); - }); - } - - private void DisableRule() - { - rule.Disable(CreateCommand(new DisableRule())); - } - - private void CreateRule() - { - rule.Create(CreateCommand(new CreateRule { Trigger = ruleTrigger, Action = ruleAction })); - } - } -} \ No newline at end of file diff --git a/tests/Squidex.Domain.Apps.Entities.Tests/Rules/RuleDequeuerTests.cs b/tests/Squidex.Domain.Apps.Entities.Tests/Rules/RuleDequeuerTests.cs index 6fa74a242..c7303a074 100644 --- a/tests/Squidex.Domain.Apps.Entities.Tests/Rules/RuleDequeuerTests.cs +++ b/tests/Squidex.Domain.Apps.Entities.Tests/Rules/RuleDequeuerTests.cs @@ -69,8 +69,6 @@ namespace Squidex.Domain.Apps.Entities.Rules await sut.HandleAsync(@event); - sut.Dispose(); - A.CallTo(() => ruleEventRepository.MarkSentAsync(@event.Id, requestDump, result, jobResult, requestElapsed, nextCall)) .MustHaveHappened(); } diff --git a/tests/Squidex.Domain.Apps.Entities.Tests/Rules/RuleDomainObjectTests.cs b/tests/Squidex.Domain.Apps.Entities.Tests/Rules/RuleDomainObjectTests.cs deleted file mode 100644 index 0afbaab17..000000000 --- a/tests/Squidex.Domain.Apps.Entities.Tests/Rules/RuleDomainObjectTests.cs +++ /dev/null @@ -1,256 +0,0 @@ -// ========================================================================== -// Squidex Headless CMS -// ========================================================================== -// Copyright (c) Squidex UG (haftungsbeschränkt) -// All rights reserved. Licensed under the MIT license. -// ========================================================================== - -using System; -using System.Collections.Immutable; -using FakeItEasy; -using Squidex.Domain.Apps.Core.Rules; -using Squidex.Domain.Apps.Core.Rules.Actions; -using Squidex.Domain.Apps.Core.Rules.Triggers; -using Squidex.Domain.Apps.Entities.Rules.Commands; -using Squidex.Domain.Apps.Entities.TestHelpers; -using Squidex.Domain.Apps.Events.Rules; -using Squidex.Infrastructure; -using Squidex.Infrastructure.States; -using Xunit; - -namespace Squidex.Domain.Apps.Entities.Rules -{ - public class RuleDomainObjectTests : HandlerTestBase - { - private readonly Guid ruleId = Guid.NewGuid(); - private readonly RuleTrigger ruleTrigger = new ContentChangedTrigger(); - private readonly RuleAction ruleAction = new WebhookAction { Url = new Uri("https://squidex.io") }; - private readonly RuleDomainObject sut = new RuleDomainObject(); - - protected override Guid Id - { - get { return ruleId; } - } - - public RuleDomainObjectTests() - { - sut.ActivateAsync(Id, A.Fake>()); - } - - [Fact] - public void Create_should_throw_exception_if_created() - { - sut.Create(CreateRuleCommand(new CreateRule { Trigger = ruleTrigger, Action = ruleAction })); - - Assert.Throws(() => - { - sut.Create(CreateRuleCommand(new CreateRule { Trigger = ruleTrigger, Action = ruleAction })); - }); - } - - [Fact] - public void Create_should_create_events() - { - var command = new CreateRule { Trigger = ruleTrigger, Action = ruleAction }; - - sut.Create(CreateRuleCommand(command)); - - Assert.Equal(AppId, sut.Snapshot.AppId.Id); - - Assert.Same(ruleTrigger, sut.Snapshot.RuleDef.Trigger); - Assert.Same(ruleAction, sut.Snapshot.RuleDef.Action); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateRuleEvent(new RuleCreated { Trigger = ruleTrigger, Action = ruleAction }) - ); - } - - [Fact] - public void Update_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.Update(CreateRuleCommand(new UpdateRule { Trigger = ruleTrigger, Action = ruleAction })); - }); - } - - [Fact] - public void Update_should_throw_exception_if_rule_is_deleted() - { - CreateRule(); - DeleteRule(); - - Assert.Throws(() => - { - sut.Update(CreateRuleCommand(new UpdateRule { Trigger = ruleTrigger, Action = ruleAction })); - }); - } - - [Fact] - public void Update_should_create_events() - { - var newTrigger = new ContentChangedTrigger - { - Schemas = ImmutableList.Empty - }; - - var newAction = new WebhookAction - { - Url = new Uri("https://squidex.io/v2") - }; - - CreateRule(); - - var command = new UpdateRule { Trigger = newTrigger, Action = newAction }; - - sut.Update(CreateRuleCommand(command)); - - Assert.Same(newTrigger, sut.Snapshot.RuleDef.Trigger); - Assert.Same(newAction, sut.Snapshot.RuleDef.Action); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateRuleEvent(new RuleUpdated { Trigger = newTrigger, Action = newAction }) - ); - } - - [Fact] - public void Enable_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.Enable(CreateRuleCommand(new EnableRule())); - }); - } - - [Fact] - public void Enable_should_throw_exception_if_rule_is_deleted() - { - CreateRule(); - DeleteRule(); - - Assert.Throws(() => - { - sut.Enable(CreateRuleCommand(new EnableRule())); - }); - } - - [Fact] - public void Enable_should_create_events() - { - CreateRule(); - - var command = new EnableRule(); - - sut.Enable(CreateRuleCommand(command)); - - Assert.True(sut.Snapshot.RuleDef.IsEnabled); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateRuleEvent(new RuleEnabled()) - ); - } - - [Fact] - public void Disable_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.Disable(CreateRuleCommand(new DisableRule())); - }); - } - - [Fact] - public void Disable_should_throw_exception_if_rule_is_deleted() - { - CreateRule(); - DeleteRule(); - - Assert.Throws(() => - { - sut.Disable(CreateRuleCommand(new DisableRule())); - }); - } - - [Fact] - public void Disable_should_create_events() - { - CreateRule(); - - var command = new DisableRule(); - - sut.Disable(CreateRuleCommand(command)); - - Assert.False(sut.Snapshot.RuleDef.IsEnabled); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateRuleEvent(new RuleDisabled()) - ); - } - - [Fact] - public void Delete_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.Delete(CreateRuleCommand(new DeleteRule())); - }); - } - - [Fact] - public void Delete_should_throw_exception_if_already_deleted() - { - CreateRule(); - DeleteRule(); - - Assert.Throws(() => - { - sut.Delete(CreateRuleCommand(new DeleteRule())); - }); - } - - [Fact] - public void Delete_should_update_create_events() - { - CreateRule(); - - sut.Delete(CreateRuleCommand(new DeleteRule())); - - Assert.True(sut.Snapshot.IsDeleted); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateRuleEvent(new RuleDeleted()) - ); - } - - private void CreateRule() - { - sut.Create(CreateRuleCommand(new CreateRule { Trigger = ruleTrigger, Action = ruleAction })); - sut.ClearUncommittedEvents(); - } - - private void DeleteRule() - { - sut.Delete(CreateRuleCommand(new DeleteRule())); - sut.ClearUncommittedEvents(); - } - - protected T CreateRuleEvent(T @event) where T : RuleEvent - { - @event.RuleId = ruleId; - - return CreateEvent(@event); - } - - protected T CreateRuleCommand(T command) where T : RuleCommand - { - command.RuleId = ruleId; - - return CreateCommand(command); - } - } -} \ No newline at end of file diff --git a/tests/Squidex.Domain.Apps.Entities.Tests/Rules/RuleGrainTests.cs b/tests/Squidex.Domain.Apps.Entities.Tests/Rules/RuleGrainTests.cs new file mode 100644 index 000000000..aa1c9be31 --- /dev/null +++ b/tests/Squidex.Domain.Apps.Entities.Tests/Rules/RuleGrainTests.cs @@ -0,0 +1,227 @@ +// ========================================================================== +// Squidex Headless CMS +// ========================================================================== +// Copyright (c) Squidex UG (haftungsbeschränkt) +// All rights reserved. Licensed under the MIT license. +// ========================================================================== + +using System; +using System.Collections.Immutable; +using System.Threading.Tasks; +using FakeItEasy; +using Orleans.Core; +using Orleans.Runtime; +using Squidex.Domain.Apps.Core.Rules.Actions; +using Squidex.Domain.Apps.Core.Rules.Triggers; +using Squidex.Domain.Apps.Entities.Rules.Commands; +using Squidex.Domain.Apps.Entities.Rules.State; +using Squidex.Domain.Apps.Entities.TestHelpers; +using Squidex.Domain.Apps.Events.Rules; +using Squidex.Infrastructure; +using Squidex.Infrastructure.Commands; +using Squidex.Infrastructure.States; +using Xunit; + +namespace Squidex.Domain.Apps.Entities.Rules +{ + public class RuleDomainObjectTests : HandlerTestBase + { + private readonly IAppProvider appProvider = A.Fake(); + private readonly Guid ruleId = Guid.NewGuid(); + private readonly RuleGrain sut; + + public sealed class MyRuleGrain : RuleGrain + { + public MyRuleGrain(IStore store, IAppProvider appProvider, IGrainIdentity identity, IGrainRuntime runtime) + : base(store, appProvider, identity, runtime) + { + } + } + + protected override Guid Id + { + get { return ruleId; } + } + + public RuleDomainObjectTests() + { + sut = new MyRuleGrain(Store, appProvider, Identity, Runtime); + sut.OnActivateAsync().Wait(); + } + + [Fact] + public async Task Command_should_throw_exception_if_rule_is_deleted() + { + await CreateAsync(); + await DeleteAsync(); + + await Assert.ThrowsAsync(() => + { + return sut.ExecuteAsync(J(CreateRuleCommand(MakeUpdateCommand()))); + }); + } + + [Fact] + public async Task Create_should_create_events() + { + var command = MakeCreateCommand(); + + var result = await sut.ExecuteAsync(J(CreateRuleCommand(command))); + + Assert.True(result.Value is EntityCreatedResult); + + Assert.Equal(AppId, sut.Snapshot.AppId.Id); + + Assert.Same(command.Trigger, sut.Snapshot.RuleDef.Trigger); + Assert.Same(command.Action, sut.Snapshot.RuleDef.Action); + + LastEvents + .ShouldHaveSameEvents( + CreateRuleEvent(new RuleCreated { Trigger = command.Trigger, Action = command.Action }) + ); + } + + [Fact] + public async Task Update_should_handle_command() + { + await sut.ExecuteAsync(J(CreateRuleCommand(MakeCreateCommand()))); + + var result = await sut.ExecuteAsync(J(CreateRuleCommand(MakeUpdateCommand()))); + + Assert.True(result.Value is EntitySavedResult); + } + + [Fact] + public async Task Update_should_create_events() + { + var command = MakeUpdateCommand(); + + await CreateAsync(); + + var result = await sut.ExecuteAsync(J(CreateRuleCommand(command))); + + Assert.True(result.Value is EntitySavedResult); + + Assert.Same(command.Trigger, sut.Snapshot.RuleDef.Trigger); + Assert.Same(command.Action, sut.Snapshot.RuleDef.Action); + + LastEvents + .ShouldHaveSameEvents( + CreateRuleEvent(new RuleUpdated { Trigger = command.Trigger, Action = command.Action }) + ); + } + + [Fact] + public async Task Enable_should_handle_command() + { + await sut.ExecuteAsync(J(CreateRuleCommand(MakeCreateCommand()))); + await sut.ExecuteAsync(J(CreateRuleCommand(new DisableRule()))); + } + + [Fact] + public async Task Enable_should_create_events() + { + await CreateAsync(); + await sut.ExecuteAsync(J(CreateRuleCommand(new DisableRule()))); + + var result = await sut.ExecuteAsync(J(CreateRuleCommand(new EnableRule()))); + + Assert.True(result.Value is EntitySavedResult); + + Assert.True(sut.Snapshot.RuleDef.IsEnabled); + + LastEvents + .ShouldHaveSameEvents( + CreateRuleEvent(new RuleEnabled()) + ); + } + + [Fact] + public async Task Disable_should_create_events() + { + await CreateAsync(); + + var result = await sut.ExecuteAsync(J(CreateRuleCommand(new DisableRule()))); + + Assert.True(result.Value is EntitySavedResult); + + Assert.False(sut.Snapshot.RuleDef.IsEnabled); + + LastEvents + .ShouldHaveSameEvents( + CreateRuleEvent(new RuleDisabled()) + ); + } + + [Fact] + public async Task Delete_should_update_create_events() + { + await CreateAsync(); + + var result = await sut.ExecuteAsync(J(CreateRuleCommand(new DeleteRule()))); + + Assert.True(result.Value is EntitySavedResult); + + Assert.True(sut.Snapshot.IsDeleted); + + LastEvents + .ShouldHaveSameEvents( + CreateRuleEvent(new RuleDeleted()) + ); + } + + private Task CreateAsync() + { + return sut.ExecuteAsync(J(CreateRuleCommand(MakeCreateCommand()))); + } + + private Task DeleteAsync() + { + return sut.ExecuteAsync(J(CreateRuleCommand(new DeleteRule()))); + } + + protected T CreateRuleEvent(T @event) where T : RuleEvent + { + @event.RuleId = ruleId; + + return CreateEvent(@event); + } + + protected T CreateRuleCommand(T command) where T : RuleCommand + { + command.RuleId = ruleId; + + return CreateCommand(command); + } + + private CreateRule MakeCreateCommand() + { + var newTrigger = new ContentChangedTrigger + { + Schemas = ImmutableList.Empty + }; + + var newAction = new WebhookAction + { + Url = new Uri("https://squidex.io/v2") + }; + + return new CreateRule { Trigger = newTrigger, Action = newAction }; + } + + private static UpdateRule MakeUpdateCommand() + { + var newTrigger = new ContentChangedTrigger + { + Schemas = ImmutableList.Empty + }; + + var newAction = new WebhookAction + { + Url = new Uri("https://squidex.io/v2") + }; + + return new UpdateRule { Trigger = newTrigger, Action = newAction }; + } + } +} \ No newline at end of file diff --git a/tests/Squidex.Domain.Apps.Entities.Tests/Schemas/SchemaCommandMiddlewareTests.cs b/tests/Squidex.Domain.Apps.Entities.Tests/Schemas/SchemaCommandMiddlewareTests.cs index f6e5c4c43..54a4dfcb5 100644 --- a/tests/Squidex.Domain.Apps.Entities.Tests/Schemas/SchemaCommandMiddlewareTests.cs +++ b/tests/Squidex.Domain.Apps.Entities.Tests/Schemas/SchemaCommandMiddlewareTests.cs @@ -1,280 +1,280 @@ -// ========================================================================== -// 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.Threading.Tasks; -using FakeItEasy; -using Squidex.Domain.Apps.Core.Schemas; -using Squidex.Domain.Apps.Entities.Schemas.Commands; -using Squidex.Domain.Apps.Entities.TestHelpers; -using Squidex.Infrastructure; -using Squidex.Infrastructure.Commands; -using Xunit; - -namespace Squidex.Domain.Apps.Entities.Schemas -{ - public class SchemaCommandMiddlewareTests : HandlerTestBase - { - private readonly IAppProvider appProvider = A.Fake(); - private readonly SchemaCommandMiddleware sut; - private readonly SchemaDomainObject schema; - private readonly FieldRegistry registry = new FieldRegistry(new TypeNameRegistry()); - private readonly string fieldName = "age"; - - protected override Guid Id - { - get { return SchemaId; } - } - - public SchemaCommandMiddlewareTests() - { - schema = new SchemaDomainObject(registry); - - sut = new SchemaCommandMiddleware(Handler, appProvider); - - A.CallTo(() => appProvider.GetSchemaAsync(AppId, SchemaName)) - .Returns((ISchemaEntity)null); - } - - [Fact] - public async Task Create_should_create_schema_domain_object() - { - var context = CreateContextForCommand(new CreateSchema { Name = SchemaName, SchemaId = SchemaId }); - - await TestCreate(schema, async _ => - { - await sut.HandleAsync(context); - }); - - Assert.Equal(SchemaId, context.Result>().IdOrValue); - - A.CallTo(() => appProvider.GetSchemaAsync(AppId, SchemaName)).MustHaveHappened(); - } - - [Fact] - public async Task UpdateSchema_should_update_domain_object() - { - CreateSchema(); - - var context = CreateContextForCommand(new UpdateSchema { Properties = new SchemaProperties() }); - - await TestUpdate(schema, async _ => - { - await sut.HandleAsync(context); - }); - } - - [Fact] - public async Task ReorderSchema_should_update_domain_object() - { - CreateSchema(); - - var context = CreateContextForCommand(new ReorderFields { FieldIds = new List() }); - - await TestUpdate(schema, async _ => - { - await sut.HandleAsync(context); - }); - } - - [Fact] - public async Task PublishSchema_should_update_domain_object() - { - CreateSchema(); - - var context = CreateContextForCommand(new PublishSchema()); - - await TestUpdate(schema, async _ => - { - await sut.HandleAsync(context); - }); - } - - [Fact] - public async Task UnpublishSchema_should_update_domain_object() - { - CreateSchema(); - PublishSchema(); - - var context = CreateContextForCommand(new UnpublishSchema()); - - await TestUpdate(schema, async _ => - { - await sut.HandleAsync(context); - }); - } - - [Fact] - public async Task ConfigureScripts_should_update_domain_object() - { - CreateSchema(); - - var context = CreateContextForCommand(new ConfigureScripts()); - - await TestUpdate(schema, async _ => - { - await sut.HandleAsync(context); - }); - } - - [Fact] - public async Task DeleteSchema_should_update_domain_object() - { - CreateSchema(); - - var context = CreateContextForCommand(new DeleteSchema()); - - await TestUpdate(schema, async _ => - { - await sut.HandleAsync(context); - }); - } - - [Fact] - public async Task Add_should_update_domain_object() - { - CreateSchema(); - - var context = CreateContextForCommand(new AddField { Name = fieldName, Properties = new NumberFieldProperties() }); - - await TestUpdate(schema, async _ => - { - await sut.HandleAsync(context); - }); - - Assert.Equal(1, context.Result>().IdOrValue); - } - - [Fact] - public async Task UpdateField_should_update_domain_object() - { - CreateSchema(); - CreateField(); - - var context = CreateContextForCommand(new UpdateField { FieldId = 1, Properties = new NumberFieldProperties() }); - - await TestUpdate(schema, async _ => - { - await sut.HandleAsync(context); - }); - } - - [Fact] - public async Task LockField_should_update_domain_object() - { - CreateSchema(); - CreateField(); - - var context = CreateContextForCommand(new LockField { FieldId = 1 }); - - await TestUpdate(schema, async _ => - { - await sut.HandleAsync(context); - }); - } - - [Fact] - public async Task HideField_should_update_domain_object() - { - CreateSchema(); - CreateField(); - - var context = CreateContextForCommand(new HideField { FieldId = 1 }); - - await TestUpdate(schema, async _ => - { - await sut.HandleAsync(context); - }); - } - - [Fact] - public async Task ShowField_should_update_domain_object() - { - CreateSchema(); - CreateField(); - - HideField(); - - var context = CreateContextForCommand(new ShowField { FieldId = 1 }); - - await TestUpdate(schema, async _ => - { - await sut.HandleAsync(context); - }); - } - - [Fact] - public async Task DisableField_should_update_domain_object() - { - CreateSchema(); - CreateField(); - - var context = CreateContextForCommand(new DisableField { FieldId = 1 }); - - await TestUpdate(schema, async _ => - { - await sut.HandleAsync(context); - }); - } - - [Fact] - public async Task EnableField_should_update_domain_object() - { - CreateSchema(); - CreateField(); - - DisableField(); - - var context = CreateContextForCommand(new EnableField { FieldId = 1 }); - - await TestUpdate(schema, async _ => - { - await sut.HandleAsync(context); - }); - } - - [Fact] - public async Task DeleteField_should_update_domain_object() - { - CreateSchema(); - CreateField(); - - var context = CreateContextForCommand(new DeleteField { FieldId = 1 }); - - await TestUpdate(schema, async _ => - { - await sut.HandleAsync(context); - }); - } - - private void CreateSchema() - { - schema.Create(CreateCommand(new CreateSchema { Name = SchemaName })); - } - - private void PublishSchema() - { - schema.Publish(CreateCommand(new PublishSchema())); - } - - private void CreateField() - { - schema.Add(CreateCommand(new AddField { Name = fieldName, Properties = new NumberFieldProperties() })); - } - - private void HideField() - { - schema.HideField(CreateCommand(new HideField { FieldId = 1 })); - } - - private void DisableField() - { - schema.DisableField(CreateCommand(new DisableField { FieldId = 1 })); - } - } -} \ No newline at end of file +//// ========================================================================== +//// 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.Threading.Tasks; +//using FakeItEasy; +//using Squidex.Domain.Apps.Core.Schemas; +//using Squidex.Domain.Apps.Entities.Schemas.Commands; +//using Squidex.Domain.Apps.Entities.TestHelpers; +//using Squidex.Infrastructure; +//using Squidex.Infrastructure.Commands; +//using Xunit; + +//namespace Squidex.Domain.Apps.Entities.Schemas +//{ +// public class SchemaCommandMiddlewareTests : HandlerTestBase +// { +// private readonly IAppProvider appProvider = A.Fake(); +// private readonly SchemaCommandMiddleware sut; +// private readonly SchemaDomainObject schema; +// private readonly FieldRegistry registry = new FieldRegistry(new TypeNameRegistry()); +// private readonly string fieldName = "age"; + +// protected override Guid Id +// { +// get { return SchemaId; } +// } + +// public SchemaCommandMiddlewareTests() +// { +// schema = new SchemaDomainObject(registry); + +// sut = new SchemaCommandMiddleware(Handler, appProvider); + +// A.CallTo(() => appProvider.GetSchemaAsync(AppId, SchemaName)) +// .Returns((ISchemaEntity)null); +// } + +// [Fact] +// public async Task Create_should_create_schema_domain_object() +// { +// var context = CreateContextForCommand(new CreateSchema { Name = SchemaName, SchemaId = SchemaId }); + +// await TestCreate(schema, async _ => +// { +// await sut.HandleAsync(context); +// }); + +// Assert.Equal(SchemaId, context.Result>().IdOrValue); + +// A.CallTo(() => appProvider.GetSchemaAsync(AppId, SchemaName)).MustHaveHappened(); +// } + +// [Fact] +// public async Task UpdateSchema_should_update_domain_object() +// { +// CreateSchema(); + +// var context = CreateContextForCommand(new UpdateSchema { Properties = new SchemaProperties() }); + +// await TestUpdate(schema, async _ => +// { +// await sut.HandleAsync(context); +// }); +// } + +// [Fact] +// public async Task ReorderSchema_should_update_domain_object() +// { +// CreateSchema(); + +// var context = CreateContextForCommand(new ReorderFields { FieldIds = new List() }); + +// await TestUpdate(schema, async _ => +// { +// await sut.HandleAsync(context); +// }); +// } + +// [Fact] +// public async Task PublishSchema_should_update_domain_object() +// { +// CreateSchema(); + +// var context = CreateContextForCommand(new PublishSchema()); + +// await TestUpdate(schema, async _ => +// { +// await sut.HandleAsync(context); +// }); +// } + +// [Fact] +// public async Task UnpublishSchema_should_update_domain_object() +// { +// CreateSchema(); +// PublishSchema(); + +// var context = CreateContextForCommand(new UnpublishSchema()); + +// await TestUpdate(schema, async _ => +// { +// await sut.HandleAsync(context); +// }); +// } + +// [Fact] +// public async Task ConfigureScripts_should_update_domain_object() +// { +// CreateSchema(); + +// var context = CreateContextForCommand(new ConfigureScripts()); + +// await TestUpdate(schema, async _ => +// { +// await sut.HandleAsync(context); +// }); +// } + +// [Fact] +// public async Task DeleteSchema_should_update_domain_object() +// { +// CreateSchema(); + +// var context = CreateContextForCommand(new DeleteSchema()); + +// await TestUpdate(schema, async _ => +// { +// await sut.HandleAsync(context); +// }); +// } + +// [Fact] +// public async Task Add_should_update_domain_object() +// { +// CreateSchema(); + +// var context = CreateContextForCommand(new AddField { Name = fieldName, Properties = new NumberFieldProperties() }); + +// await TestUpdate(schema, async _ => +// { +// await sut.HandleAsync(context); +// }); + +// Assert.Equal(1, context.Result>().IdOrValue); +// } + +// [Fact] +// public async Task UpdateField_should_update_domain_object() +// { +// CreateSchema(); +// CreateField(); + +// var context = CreateContextForCommand(new UpdateField { FieldId = 1, Properties = new NumberFieldProperties() }); + +// await TestUpdate(schema, async _ => +// { +// await sut.HandleAsync(context); +// }); +// } + +// [Fact] +// public async Task LockField_should_update_domain_object() +// { +// CreateSchema(); +// CreateField(); + +// var context = CreateContextForCommand(new LockField { FieldId = 1 }); + +// await TestUpdate(schema, async _ => +// { +// await sut.HandleAsync(context); +// }); +// } + +// [Fact] +// public async Task HideField_should_update_domain_object() +// { +// CreateSchema(); +// CreateField(); + +// var context = CreateContextForCommand(new HideField { FieldId = 1 }); + +// await TestUpdate(schema, async _ => +// { +// await sut.HandleAsync(context); +// }); +// } + +// [Fact] +// public async Task ShowField_should_update_domain_object() +// { +// CreateSchema(); +// CreateField(); + +// HideField(); + +// var context = CreateContextForCommand(new ShowField { FieldId = 1 }); + +// await TestUpdate(schema, async _ => +// { +// await sut.HandleAsync(context); +// }); +// } + +// [Fact] +// public async Task DisableField_should_update_domain_object() +// { +// CreateSchema(); +// CreateField(); + +// var context = CreateContextForCommand(new DisableField { FieldId = 1 }); + +// await TestUpdate(schema, async _ => +// { +// await sut.HandleAsync(context); +// }); +// } + +// [Fact] +// public async Task EnableField_should_update_domain_object() +// { +// CreateSchema(); +// CreateField(); + +// DisableField(); + +// var context = CreateContextForCommand(new EnableField { FieldId = 1 }); + +// await TestUpdate(schema, async _ => +// { +// await sut.HandleAsync(context); +// }); +// } + +// [Fact] +// public async Task DeleteField_should_update_domain_object() +// { +// CreateSchema(); +// CreateField(); + +// var context = CreateContextForCommand(new DeleteField { FieldId = 1 }); + +// await TestUpdate(schema, async _ => +// { +// await sut.HandleAsync(context); +// }); +// } + +// private void CreateSchema() +// { +// schema.Create(CreateCommand(new CreateSchema { Name = SchemaName })); +// } + +// private void PublishSchema() +// { +// schema.Publish(CreateCommand(new PublishSchema())); +// } + +// private void CreateField() +// { +// schema.Add(CreateCommand(new AddField { Name = fieldName, Properties = new NumberFieldProperties() })); +// } + +// private void HideField() +// { +// schema.HideField(CreateCommand(new HideField { FieldId = 1 })); +// } + +// private void DisableField() +// { +// schema.DisableField(CreateCommand(new DisableField { FieldId = 1 })); +// } +// } +//} \ No newline at end of file diff --git a/tests/Squidex.Domain.Apps.Entities.Tests/Schemas/SchemaDomainObjectTests.cs b/tests/Squidex.Domain.Apps.Entities.Tests/Schemas/SchemaDomainObjectTests.cs index 71922b093..d4ffa9186 100644 --- a/tests/Squidex.Domain.Apps.Entities.Tests/Schemas/SchemaDomainObjectTests.cs +++ b/tests/Squidex.Domain.Apps.Entities.Tests/Schemas/SchemaDomainObjectTests.cs @@ -1,665 +1,665 @@ -// ========================================================================== -// 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 FakeItEasy; -using Squidex.Domain.Apps.Core.Schemas; -using Squidex.Domain.Apps.Entities.Schemas.Commands; -using Squidex.Domain.Apps.Entities.TestHelpers; -using Squidex.Domain.Apps.Events.Schemas; -using Squidex.Infrastructure; -using Squidex.Infrastructure.States; -using Xunit; - -namespace Squidex.Domain.Apps.Entities.Schemas -{ - public class SchemaDomainObjectTests : HandlerTestBase - { - private readonly string fieldName = "age"; - private readonly NamedId fieldId; - private readonly SchemaDomainObject sut; - - protected override Guid Id - { - get { return SchemaId; } - } - - public SchemaDomainObjectTests() - { - fieldId = new NamedId(1, fieldName); - - var fieldRegistry = new FieldRegistry(new TypeNameRegistry()); - - sut = new SchemaDomainObject(fieldRegistry); - sut.ActivateAsync(Id, A.Fake>()); - } - - [Fact] - public void Create_should_throw_exception_if_created() - { - sut.Create(CreateCommand(new CreateSchema { Name = SchemaName })); - - Assert.Throws(() => - { - sut.Create(CreateCommand(new CreateSchema { Name = SchemaName })); - }); - } - - [Fact] - public void Create_should_create_schema_and_create_events() - { - var properties = new SchemaProperties(); - - sut.Create(CreateCommand(new CreateSchema { Name = SchemaName, SchemaId = SchemaId, Properties = properties })); - - Assert.Equal(AppId, sut.Snapshot.AppId.Id); - - Assert.Equal(SchemaName, sut.Snapshot.Name); - Assert.Equal(SchemaName, sut.Snapshot.SchemaDef.Name); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateEvent(new SchemaCreated { Name = SchemaName, Properties = properties }) - ); - } - - [Fact] - public void Create_should_create_schema_with_initial_fields() - { - var properties = new SchemaProperties(); - - var fields = new List - { - new CreateSchemaField { Name = "field1", Properties = ValidProperties() }, - new CreateSchemaField { Name = "field2", Properties = ValidProperties() } - }; - - sut.Create(CreateCommand(new CreateSchema { Name = SchemaName, Properties = properties, Fields = fields })); - - var @event = (SchemaCreated)sut.GetUncomittedEvents().Single().Payload; - - Assert.Equal(AppId, sut.Snapshot.AppId.Id); - Assert.Equal(SchemaName, sut.Snapshot.Name); - Assert.Equal(SchemaName, sut.Snapshot.SchemaDef.Name); - - Assert.Equal(2, @event.Fields.Count); - } - - [Fact] - public void Update_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.Update(CreateCommand(new UpdateSchema { Properties = new SchemaProperties() })); - }); - } - - [Fact] - public void Update_should_throw_exception_if_schema_is_deleted() - { - CreateSchema(); - DeleteSchema(); - - Assert.Throws(() => - { - sut.Update(CreateCommand(new UpdateSchema { Properties = new SchemaProperties() })); - }); - } - - [Fact] - public void Update_should_refresh_properties_and_create_events() - { - var properties = new SchemaProperties(); - - CreateSchema(); - - sut.Update(CreateCommand(new UpdateSchema { Properties = properties })); - - Assert.Equal(properties, sut.Snapshot.SchemaDef.Properties); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateEvent(new SchemaUpdated { Properties = properties }) - ); - } - - [Fact] - public void ConfigureScripts_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.ConfigureScripts(CreateCommand(new ConfigureScripts())); - }); - } - - [Fact] - public void ConfigureScripts_should_throw_exception_if_schema_is_deleted() - { - CreateSchema(); - DeleteSchema(); - - Assert.Throws(() => - { - sut.ConfigureScripts(CreateCommand(new ConfigureScripts())); - }); - } - - [Fact] - public void ConfigureScripts_should_create_events() - { - CreateSchema(); - - sut.ConfigureScripts(CreateCommand(new ConfigureScripts - { - ScriptQuery = "", - ScriptCreate = "", - ScriptUpdate = "", - ScriptDelete = "", - ScriptChange = "" - })); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateEvent(new ScriptsConfigured - { - ScriptQuery = "", - ScriptCreate = "", - ScriptUpdate = "", - ScriptDelete = "", - ScriptChange = "" - }) - ); - } - - [Fact] - public void Reorder_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.Reorder(CreateCommand(new ReorderFields { FieldIds = new List() })); - }); - } - - [Fact] - public void Reorder_should_throw_exception_if_schema_is_deleted() - { - CreateSchema(); - DeleteSchema(); - - Assert.Throws(() => - { - sut.Reorder(CreateCommand(new ReorderFields { FieldIds = new List() })); - }); - } - - [Fact] - public void Reorder_should_refresh_properties_and_create_events() - { - var fieldIds = new List { 1, 2 }; - - CreateSchema(); - - sut.Add(CreateCommand(new AddField { Name = "field1", Properties = ValidProperties() })); - sut.Add(CreateCommand(new AddField { Name = "field2", Properties = ValidProperties() })); - - sut.ClearUncommittedEvents(); - - sut.Reorder(CreateCommand(new ReorderFields { FieldIds = fieldIds })); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateEvent(new SchemaFieldsReordered { FieldIds = fieldIds }) - ); - } - - [Fact] - public void Publish_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.Publish(CreateCommand(new PublishSchema())); - }); - } - - [Fact] - public void Publish_should_throw_exception_if_schema_is_deleted() - { - CreateSchema(); - DeleteSchema(); - - Assert.Throws(() => - { - sut.Publish(CreateCommand(new PublishSchema())); - }); - } - - [Fact] - public void Publish_should_refresh_properties_and_create_events() - { - CreateSchema(); - - sut.Publish(CreateCommand(new PublishSchema())); - - Assert.True(sut.Snapshot.SchemaDef.IsPublished); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateEvent(new SchemaPublished()) - ); - } - - [Fact] - public void Unpublish_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.Unpublish(CreateCommand(new UnpublishSchema())); - }); - } - - [Fact] - public void Unpublish_should_throw_exception_if_schema_is_deleted() - { - CreateSchema(); - DeleteSchema(); - - Assert.Throws(() => - { - sut.Unpublish(CreateCommand(new UnpublishSchema())); - }); - } - - [Fact] - public void Unpublish_should_refresh_properties_and_create_events() - { - CreateSchema(); - PublishSchema(); - - sut.Unpublish(CreateCommand(new UnpublishSchema())); - - Assert.False(sut.Snapshot.SchemaDef.IsPublished); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateEvent(new SchemaUnpublished()) - ); - } - - [Fact] - public void Delete_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.Delete(CreateCommand(new DeleteSchema())); - }); - } - - [Fact] - public void Delete_should_throw_exception_if_already_deleted() - { - CreateSchema(); - DeleteSchema(); - - Assert.Throws(() => - { - sut.Delete(CreateCommand(new DeleteSchema())); - }); - } - - [Fact] - public void Delete_should_refresh_properties_and_create_events() - { - CreateSchema(); - - sut.Delete(CreateCommand(new DeleteSchema())); - - Assert.True(sut.Snapshot.IsDeleted); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateEvent(new SchemaDeleted()) - ); - } - - [Fact] - public void AddField_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.Add(CreateCommand(new AddField { Name = fieldName, Properties = ValidProperties() })); - }); - } - - [Fact] - public void AddField_should_throw_exception_if_schema_is_deleted() - { - CreateSchema(); - DeleteSchema(); - - Assert.Throws(() => - { - sut.Add(CreateCommand(new AddField { Name = fieldName, Properties = new NumberFieldProperties() })); - }); - } - - [Fact] - public void Add_should_update_schema_and_create_events() - { - var properties = new NumberFieldProperties(); - - CreateSchema(); - - sut.Add(CreateCommand(new AddField { Name = fieldName, Properties = properties })); - - Assert.Equal(properties, sut.Snapshot.SchemaDef.FieldsById[1].RawProperties); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateEvent(new FieldAdded { Name = fieldName, FieldId = fieldId, Properties = properties }) - ); - } - - [Fact] - public void UpdateField_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.UpdateField(CreateCommand(new UpdateField { FieldId = 1, Properties = new NumberFieldProperties() })); - }); - } - - [Fact] - public void UpdateField_should_throw_exception_if_schema_is_deleted() - { - CreateSchema(); - DeleteSchema(); - - Assert.Throws(() => - { - sut.UpdateField(CreateCommand(new UpdateField { FieldId = 1, Properties = new NumberFieldProperties() })); - }); - } - - [Fact] - public void UpdateField_should_update_schema_and_create_events() - { - var properties = new NumberFieldProperties(); - - CreateSchema(); - CreateField(); - - sut.UpdateField(CreateCommand(new UpdateField { FieldId = 1, Properties = properties })); - - Assert.Equal(properties, sut.Snapshot.SchemaDef.FieldsById[1].RawProperties); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateEvent(new FieldUpdated { FieldId = fieldId, Properties = properties }) - ); - } - - [Fact] - public void LockField_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.LockField(CreateCommand(new LockField { FieldId = 1 })); - }); - } - - [Fact] - public void LockField_should_throw_exception_if_schema_is_deleted() - { - CreateSchema(); - DeleteSchema(); - - Assert.Throws(() => - { - sut.LockField(CreateCommand(new LockField { FieldId = 1 })); - }); - } - - [Fact] - public void LockField_should_update_schema_and_create_events() - { - CreateSchema(); - CreateField(); - - sut.LockField(CreateCommand(new LockField { FieldId = 1 })); - - Assert.False(sut.Snapshot.SchemaDef.FieldsById[1].IsDisabled); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateEvent(new FieldLocked { FieldId = fieldId }) - ); - } - - [Fact] - public void HideField_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.HideField(CreateCommand(new HideField { FieldId = 1 })); - }); - } - - [Fact] - public void HideField_should_throw_exception_if_schema_is_deleted() - { - CreateSchema(); - DeleteSchema(); - - Assert.Throws(() => - { - sut.HideField(CreateCommand(new HideField { FieldId = 1 })); - }); - } - - [Fact] - public void HideField_should_update_schema_and_create_events() - { - CreateSchema(); - CreateField(); - - sut.HideField(CreateCommand(new HideField { FieldId = 1 })); - - Assert.True(sut.Snapshot.SchemaDef.FieldsById[1].IsHidden); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateEvent(new FieldHidden { FieldId = fieldId }) - ); - } - - [Fact] - public void ShowField_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.ShowField(CreateCommand(new ShowField { FieldId = 1 })); - }); - } - - [Fact] - public void ShowField_should_throw_exception_if_schema_is_deleted() - { - CreateSchema(); - DeleteSchema(); - - Assert.Throws(() => - { - sut.ShowField(CreateCommand(new ShowField { FieldId = 1 })); - }); - } - - [Fact] - public void ShowField_should_update_schema_and_create_events() - { - CreateSchema(); - CreateField(); - - sut.HideField(CreateCommand(new HideField { FieldId = 1 })); - sut.ShowField(CreateCommand(new ShowField { FieldId = 1 })); - - Assert.False(sut.Snapshot.SchemaDef.FieldsById[1].IsHidden); - - sut.GetUncomittedEvents().Skip(1) - .ShouldHaveSameEvents( - CreateEvent(new FieldShown { FieldId = fieldId }) - ); - } - - [Fact] - public void DisableField_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.DisableField(CreateCommand(new DisableField { FieldId = 1 })); - }); - } - - [Fact] - public void DisableField_should_throw_exception_if_schema_is_deleted() - { - CreateSchema(); - DeleteSchema(); - - Assert.Throws(() => - { - sut.DisableField(CreateCommand(new DisableField { FieldId = 1 })); - }); - } - - [Fact] - public void DisableField_should_update_schema_and_create_events() - { - CreateSchema(); - CreateField(); - - sut.DisableField(CreateCommand(new DisableField { FieldId = 1 })); - - Assert.True(sut.Snapshot.SchemaDef.FieldsById[1].IsDisabled); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateEvent(new FieldDisabled { FieldId = fieldId }) - ); - } - - [Fact] - public void EnableField_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.EnableField(CreateCommand(new EnableField { FieldId = 1 })); - }); - } - - [Fact] - public void EnableField_should_throw_exception_if_schema_is_deleted() - { - CreateSchema(); - DeleteSchema(); - - Assert.Throws(() => - { - sut.EnableField(CreateCommand(new EnableField { FieldId = 1 })); - }); - } - - [Fact] - public void EnableField_should_update_schema_and_create_events() - { - CreateSchema(); - CreateField(); - - sut.DisableField(CreateCommand(new DisableField { FieldId = 1 })); - sut.EnableField(CreateCommand(new EnableField { FieldId = 1 })); - - Assert.False(sut.Snapshot.SchemaDef.FieldsById[1].IsDisabled); - - sut.GetUncomittedEvents().Skip(1) - .ShouldHaveSameEvents( - CreateEvent(new FieldEnabled { FieldId = fieldId }) - ); - } - - [Fact] - public void DeleteField_should_throw_exception_if_not_created() - { - Assert.Throws(() => - { - sut.DeleteField(CreateCommand(new DeleteField { FieldId = 1 })); - }); - } - - [Fact] - public void DeleteField_should_throw_exception_if_schema_is_deleted() - { - CreateSchema(); - DeleteSchema(); - - Assert.Throws(() => - { - sut.DeleteField(CreateCommand(new DeleteField { FieldId = 1 })); - }); - } - - [Fact] - public void DeleteField_should_update_schema_and_create_events() - { - CreateSchema(); - CreateField(); - - sut.DeleteField(CreateCommand(new DeleteField { FieldId = 1 })); - - Assert.False(sut.Snapshot.SchemaDef.FieldsById.ContainsKey(1)); - - sut.GetUncomittedEvents() - .ShouldHaveSameEvents( - CreateEvent(new FieldDeleted { FieldId = fieldId }) - ); - } - - private void CreateField() - { - sut.Add(CreateCommand(new AddField { Name = fieldName, Properties = new NumberFieldProperties() })); - sut.ClearUncommittedEvents(); - } - - private void CreateSchema() - { - sut.Create(CreateCommand(new CreateSchema { Name = SchemaName })); - sut.ClearUncommittedEvents(); - } - - private void PublishSchema() - { - sut.Publish(CreateCommand(new PublishSchema())); - sut.ClearUncommittedEvents(); - } - - private void DeleteSchema() - { - sut.Delete(CreateCommand(new DeleteSchema())); - sut.ClearUncommittedEvents(); - } - - private static StringFieldProperties ValidProperties() - { - return new StringFieldProperties { MinLength = 10, MaxLength = 20 }; - } - - private static StringFieldProperties InvalidProperties() - { - return new StringFieldProperties { MinLength = 20, MaxLength = 10 }; - } - } -} +//// ========================================================================== +//// 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 FakeItEasy; +//using Squidex.Domain.Apps.Core.Schemas; +//using Squidex.Domain.Apps.Entities.Schemas.Commands; +//using Squidex.Domain.Apps.Entities.TestHelpers; +//using Squidex.Domain.Apps.Events.Schemas; +//using Squidex.Infrastructure; +//using Squidex.Infrastructure.States; +//using Xunit; + +//namespace Squidex.Domain.Apps.Entities.Schemas +//{ +// public class SchemaDomainObjectTests : HandlerTestBase +// { +// private readonly string fieldName = "age"; +// private readonly NamedId fieldId; +// private readonly SchemaDomainObject sut; + +// protected override Guid Id +// { +// get { return SchemaId; } +// } + +// public SchemaDomainObjectTests() +// { +// fieldId = new NamedId(1, fieldName); + +// var fieldRegistry = new FieldRegistry(new TypeNameRegistry()); + +// sut = new SchemaDomainObject(fieldRegistry); +// sut.ActivateAsync(Id, A.Fake>()); +// } + +// [Fact] +// public void Create_should_throw_exception_if_created() +// { +// sut.Create(CreateCommand(new CreateSchema { Name = SchemaName })); + +// Assert.Throws(() => +// { +// sut.Create(CreateCommand(new CreateSchema { Name = SchemaName })); +// }); +// } + +// [Fact] +// public void Create_should_create_schema_and_create_events() +// { +// var properties = new SchemaProperties(); + +// sut.Create(CreateCommand(new CreateSchema { Name = SchemaName, SchemaId = SchemaId, Properties = properties })); + +// Assert.Equal(AppId, sut.Snapshot.AppId.Id); + +// Assert.Equal(SchemaName, sut.Snapshot.Name); +// Assert.Equal(SchemaName, sut.Snapshot.SchemaDef.Name); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateEvent(new SchemaCreated { Name = SchemaName, Properties = properties }) +// ); +// } + +// [Fact] +// public void Create_should_create_schema_with_initial_fields() +// { +// var properties = new SchemaProperties(); + +// var fields = new List +// { +// new CreateSchemaField { Name = "field1", Properties = ValidProperties() }, +// new CreateSchemaField { Name = "field2", Properties = ValidProperties() } +// }; + +// sut.Create(CreateCommand(new CreateSchema { Name = SchemaName, Properties = properties, Fields = fields })); + +// var @event = (SchemaCreated)sut.GetUncomittedEvents().Single().Payload; + +// Assert.Equal(AppId, sut.Snapshot.AppId.Id); +// Assert.Equal(SchemaName, sut.Snapshot.Name); +// Assert.Equal(SchemaName, sut.Snapshot.SchemaDef.Name); + +// Assert.Equal(2, @event.Fields.Count); +// } + +// [Fact] +// public void Update_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.Update(CreateCommand(new UpdateSchema { Properties = new SchemaProperties() })); +// }); +// } + +// [Fact] +// public void Update_should_throw_exception_if_schema_is_deleted() +// { +// CreateSchema(); +// DeleteSchema(); + +// Assert.Throws(() => +// { +// sut.Update(CreateCommand(new UpdateSchema { Properties = new SchemaProperties() })); +// }); +// } + +// [Fact] +// public void Update_should_refresh_properties_and_create_events() +// { +// var properties = new SchemaProperties(); + +// CreateSchema(); + +// sut.Update(CreateCommand(new UpdateSchema { Properties = properties })); + +// Assert.Equal(properties, sut.Snapshot.SchemaDef.Properties); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateEvent(new SchemaUpdated { Properties = properties }) +// ); +// } + +// [Fact] +// public void ConfigureScripts_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.ConfigureScripts(CreateCommand(new ConfigureScripts())); +// }); +// } + +// [Fact] +// public void ConfigureScripts_should_throw_exception_if_schema_is_deleted() +// { +// CreateSchema(); +// DeleteSchema(); + +// Assert.Throws(() => +// { +// sut.ConfigureScripts(CreateCommand(new ConfigureScripts())); +// }); +// } + +// [Fact] +// public void ConfigureScripts_should_create_events() +// { +// CreateSchema(); + +// sut.ConfigureScripts(CreateCommand(new ConfigureScripts +// { +// ScriptQuery = "", +// ScriptCreate = "", +// ScriptUpdate = "", +// ScriptDelete = "", +// ScriptChange = "" +// })); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateEvent(new ScriptsConfigured +// { +// ScriptQuery = "", +// ScriptCreate = "", +// ScriptUpdate = "", +// ScriptDelete = "", +// ScriptChange = "" +// }) +// ); +// } + +// [Fact] +// public void Reorder_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.Reorder(CreateCommand(new ReorderFields { FieldIds = new List() })); +// }); +// } + +// [Fact] +// public void Reorder_should_throw_exception_if_schema_is_deleted() +// { +// CreateSchema(); +// DeleteSchema(); + +// Assert.Throws(() => +// { +// sut.Reorder(CreateCommand(new ReorderFields { FieldIds = new List() })); +// }); +// } + +// [Fact] +// public void Reorder_should_refresh_properties_and_create_events() +// { +// var fieldIds = new List { 1, 2 }; + +// CreateSchema(); + +// sut.Add(CreateCommand(new AddField { Name = "field1", Properties = ValidProperties() })); +// sut.Add(CreateCommand(new AddField { Name = "field2", Properties = ValidProperties() })); + +// sut.ClearUncommittedEvents(); + +// sut.Reorder(CreateCommand(new ReorderFields { FieldIds = fieldIds })); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateEvent(new SchemaFieldsReordered { FieldIds = fieldIds }) +// ); +// } + +// [Fact] +// public void Publish_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.Publish(CreateCommand(new PublishSchema())); +// }); +// } + +// [Fact] +// public void Publish_should_throw_exception_if_schema_is_deleted() +// { +// CreateSchema(); +// DeleteSchema(); + +// Assert.Throws(() => +// { +// sut.Publish(CreateCommand(new PublishSchema())); +// }); +// } + +// [Fact] +// public void Publish_should_refresh_properties_and_create_events() +// { +// CreateSchema(); + +// sut.Publish(CreateCommand(new PublishSchema())); + +// Assert.True(sut.Snapshot.SchemaDef.IsPublished); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateEvent(new SchemaPublished()) +// ); +// } + +// [Fact] +// public void Unpublish_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.Unpublish(CreateCommand(new UnpublishSchema())); +// }); +// } + +// [Fact] +// public void Unpublish_should_throw_exception_if_schema_is_deleted() +// { +// CreateSchema(); +// DeleteSchema(); + +// Assert.Throws(() => +// { +// sut.Unpublish(CreateCommand(new UnpublishSchema())); +// }); +// } + +// [Fact] +// public void Unpublish_should_refresh_properties_and_create_events() +// { +// CreateSchema(); +// PublishSchema(); + +// sut.Unpublish(CreateCommand(new UnpublishSchema())); + +// Assert.False(sut.Snapshot.SchemaDef.IsPublished); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateEvent(new SchemaUnpublished()) +// ); +// } + +// [Fact] +// public void Delete_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.Delete(CreateCommand(new DeleteSchema())); +// }); +// } + +// [Fact] +// public void Delete_should_throw_exception_if_already_deleted() +// { +// CreateSchema(); +// DeleteSchema(); + +// Assert.Throws(() => +// { +// sut.Delete(CreateCommand(new DeleteSchema())); +// }); +// } + +// [Fact] +// public void Delete_should_refresh_properties_and_create_events() +// { +// CreateSchema(); + +// sut.Delete(CreateCommand(new DeleteSchema())); + +// Assert.True(sut.Snapshot.IsDeleted); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateEvent(new SchemaDeleted()) +// ); +// } + +// [Fact] +// public void AddField_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.Add(CreateCommand(new AddField { Name = fieldName, Properties = ValidProperties() })); +// }); +// } + +// [Fact] +// public void AddField_should_throw_exception_if_schema_is_deleted() +// { +// CreateSchema(); +// DeleteSchema(); + +// Assert.Throws(() => +// { +// sut.Add(CreateCommand(new AddField { Name = fieldName, Properties = new NumberFieldProperties() })); +// }); +// } + +// [Fact] +// public void Add_should_update_schema_and_create_events() +// { +// var properties = new NumberFieldProperties(); + +// CreateSchema(); + +// sut.Add(CreateCommand(new AddField { Name = fieldName, Properties = properties })); + +// Assert.Equal(properties, sut.Snapshot.SchemaDef.FieldsById[1].RawProperties); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateEvent(new FieldAdded { Name = fieldName, FieldId = fieldId, Properties = properties }) +// ); +// } + +// [Fact] +// public void UpdateField_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.UpdateField(CreateCommand(new UpdateField { FieldId = 1, Properties = new NumberFieldProperties() })); +// }); +// } + +// [Fact] +// public void UpdateField_should_throw_exception_if_schema_is_deleted() +// { +// CreateSchema(); +// DeleteSchema(); + +// Assert.Throws(() => +// { +// sut.UpdateField(CreateCommand(new UpdateField { FieldId = 1, Properties = new NumberFieldProperties() })); +// }); +// } + +// [Fact] +// public void UpdateField_should_update_schema_and_create_events() +// { +// var properties = new NumberFieldProperties(); + +// CreateSchema(); +// CreateField(); + +// sut.UpdateField(CreateCommand(new UpdateField { FieldId = 1, Properties = properties })); + +// Assert.Equal(properties, sut.Snapshot.SchemaDef.FieldsById[1].RawProperties); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateEvent(new FieldUpdated { FieldId = fieldId, Properties = properties }) +// ); +// } + +// [Fact] +// public void LockField_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.LockField(CreateCommand(new LockField { FieldId = 1 })); +// }); +// } + +// [Fact] +// public void LockField_should_throw_exception_if_schema_is_deleted() +// { +// CreateSchema(); +// DeleteSchema(); + +// Assert.Throws(() => +// { +// sut.LockField(CreateCommand(new LockField { FieldId = 1 })); +// }); +// } + +// [Fact] +// public void LockField_should_update_schema_and_create_events() +// { +// CreateSchema(); +// CreateField(); + +// sut.LockField(CreateCommand(new LockField { FieldId = 1 })); + +// Assert.False(sut.Snapshot.SchemaDef.FieldsById[1].IsDisabled); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateEvent(new FieldLocked { FieldId = fieldId }) +// ); +// } + +// [Fact] +// public void HideField_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.HideField(CreateCommand(new HideField { FieldId = 1 })); +// }); +// } + +// [Fact] +// public void HideField_should_throw_exception_if_schema_is_deleted() +// { +// CreateSchema(); +// DeleteSchema(); + +// Assert.Throws(() => +// { +// sut.HideField(CreateCommand(new HideField { FieldId = 1 })); +// }); +// } + +// [Fact] +// public void HideField_should_update_schema_and_create_events() +// { +// CreateSchema(); +// CreateField(); + +// sut.HideField(CreateCommand(new HideField { FieldId = 1 })); + +// Assert.True(sut.Snapshot.SchemaDef.FieldsById[1].IsHidden); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateEvent(new FieldHidden { FieldId = fieldId }) +// ); +// } + +// [Fact] +// public void ShowField_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.ShowField(CreateCommand(new ShowField { FieldId = 1 })); +// }); +// } + +// [Fact] +// public void ShowField_should_throw_exception_if_schema_is_deleted() +// { +// CreateSchema(); +// DeleteSchema(); + +// Assert.Throws(() => +// { +// sut.ShowField(CreateCommand(new ShowField { FieldId = 1 })); +// }); +// } + +// [Fact] +// public void ShowField_should_update_schema_and_create_events() +// { +// CreateSchema(); +// CreateField(); + +// sut.HideField(CreateCommand(new HideField { FieldId = 1 })); +// sut.ShowField(CreateCommand(new ShowField { FieldId = 1 })); + +// Assert.False(sut.Snapshot.SchemaDef.FieldsById[1].IsHidden); + +// sut.GetUncomittedEvents().Skip(1) +// .ShouldHaveSameEvents( +// CreateEvent(new FieldShown { FieldId = fieldId }) +// ); +// } + +// [Fact] +// public void DisableField_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.DisableField(CreateCommand(new DisableField { FieldId = 1 })); +// }); +// } + +// [Fact] +// public void DisableField_should_throw_exception_if_schema_is_deleted() +// { +// CreateSchema(); +// DeleteSchema(); + +// Assert.Throws(() => +// { +// sut.DisableField(CreateCommand(new DisableField { FieldId = 1 })); +// }); +// } + +// [Fact] +// public void DisableField_should_update_schema_and_create_events() +// { +// CreateSchema(); +// CreateField(); + +// sut.DisableField(CreateCommand(new DisableField { FieldId = 1 })); + +// Assert.True(sut.Snapshot.SchemaDef.FieldsById[1].IsDisabled); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateEvent(new FieldDisabled { FieldId = fieldId }) +// ); +// } + +// [Fact] +// public void EnableField_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.EnableField(CreateCommand(new EnableField { FieldId = 1 })); +// }); +// } + +// [Fact] +// public void EnableField_should_throw_exception_if_schema_is_deleted() +// { +// CreateSchema(); +// DeleteSchema(); + +// Assert.Throws(() => +// { +// sut.EnableField(CreateCommand(new EnableField { FieldId = 1 })); +// }); +// } + +// [Fact] +// public void EnableField_should_update_schema_and_create_events() +// { +// CreateSchema(); +// CreateField(); + +// sut.DisableField(CreateCommand(new DisableField { FieldId = 1 })); +// sut.EnableField(CreateCommand(new EnableField { FieldId = 1 })); + +// Assert.False(sut.Snapshot.SchemaDef.FieldsById[1].IsDisabled); + +// sut.GetUncomittedEvents().Skip(1) +// .ShouldHaveSameEvents( +// CreateEvent(new FieldEnabled { FieldId = fieldId }) +// ); +// } + +// [Fact] +// public void DeleteField_should_throw_exception_if_not_created() +// { +// Assert.Throws(() => +// { +// sut.DeleteField(CreateCommand(new DeleteField { FieldId = 1 })); +// }); +// } + +// [Fact] +// public void DeleteField_should_throw_exception_if_schema_is_deleted() +// { +// CreateSchema(); +// DeleteSchema(); + +// Assert.Throws(() => +// { +// sut.DeleteField(CreateCommand(new DeleteField { FieldId = 1 })); +// }); +// } + +// [Fact] +// public void DeleteField_should_update_schema_and_create_events() +// { +// CreateSchema(); +// CreateField(); + +// sut.DeleteField(CreateCommand(new DeleteField { FieldId = 1 })); + +// Assert.False(sut.Snapshot.SchemaDef.FieldsById.ContainsKey(1)); + +// sut.GetUncomittedEvents() +// .ShouldHaveSameEvents( +// CreateEvent(new FieldDeleted { FieldId = fieldId }) +// ); +// } + +// private void CreateField() +// { +// sut.Add(CreateCommand(new AddField { Name = fieldName, Properties = new NumberFieldProperties() })); +// sut.ClearUncommittedEvents(); +// } + +// private void CreateSchema() +// { +// sut.Create(CreateCommand(new CreateSchema { Name = SchemaName })); +// sut.ClearUncommittedEvents(); +// } + +// private void PublishSchema() +// { +// sut.Publish(CreateCommand(new PublishSchema())); +// sut.ClearUncommittedEvents(); +// } + +// private void DeleteSchema() +// { +// sut.Delete(CreateCommand(new DeleteSchema())); +// sut.ClearUncommittedEvents(); +// } + +// private static StringFieldProperties ValidProperties() +// { +// return new StringFieldProperties { MinLength = 10, MaxLength = 20 }; +// } + +// private static StringFieldProperties InvalidProperties() +// { +// return new StringFieldProperties { MinLength = 20, MaxLength = 10 }; +// } +// } +//} diff --git a/tests/Squidex.Domain.Apps.Entities.Tests/TestHelpers/HandlerTestBase.cs b/tests/Squidex.Domain.Apps.Entities.Tests/TestHelpers/HandlerTestBase.cs index 0822c2f2c..82adf32de 100644 --- a/tests/Squidex.Domain.Apps.Entities.Tests/TestHelpers/HandlerTestBase.cs +++ b/tests/Squidex.Domain.Apps.Entities.Tests/TestHelpers/HandlerTestBase.cs @@ -6,68 +6,29 @@ // ========================================================================== using System; +using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using FakeItEasy; +using Orleans.Core; +using Orleans.Runtime; using Squidex.Domain.Apps.Events; using Squidex.Infrastructure; using Squidex.Infrastructure.Commands; +using Squidex.Infrastructure.EventSourcing; +using Squidex.Infrastructure.Orleans; using Squidex.Infrastructure.States; #pragma warning disable IDE0019 // Use pattern matching namespace Squidex.Domain.Apps.Entities.TestHelpers { - public abstract class HandlerTestBase where T : IDomainObject + public abstract class HandlerTestBase where T : IDomainObjectGrain { - private sealed class MockupHandler : IAggregateHandler - { - private T domainObject; - - public bool IsCreated { get; private set; } - public bool IsUpdated { get; private set; } - - public void Init(T newDomainObject) - { - domainObject = newDomainObject; - - IsCreated = false; - IsUpdated = false; - } - - public Task CreateSyncedAsync(CommandContext context, Func creator) where V : class, IDomainObject - { - return CreateAsync(context, creator); - } - - public Task UpdateSyncedAsync(CommandContext context, Func creator) where V : class, IDomainObject - { - return UpdateAsync(context, creator); - } - - public async Task CreateAsync(CommandContext context, Func creator) where V : class, IDomainObject - { - IsCreated = true; - - var @do = domainObject as V; - - await creator(domainObject as V); - - return @do; - } - - public async Task UpdateAsync(CommandContext context, Func updater) where V : class, IDomainObject - { - IsUpdated = true; - - var @do = domainObject as V; - - await updater(domainObject as V); - - return @do; - } - } - - private readonly MockupHandler handler = new MockupHandler(); + private readonly IGrainIdentity identity = A.Fake(); + private readonly IGrainRuntime runtime = A.Fake(); + private readonly IStore store = A.Fake>(); + private readonly IPersistence persistence = A.Fake>(); protected RefToken User { get; } = new RefToken("subject", Guid.NewGuid().ToString()); @@ -75,8 +36,6 @@ namespace Squidex.Domain.Apps.Entities.TestHelpers protected Guid SchemaId { get; } = Guid.NewGuid(); - protected abstract Guid Id { get; } - protected string AppName { get; } = "my-app"; protected string SchemaName { get; } = "my-schema"; @@ -91,44 +50,49 @@ namespace Squidex.Domain.Apps.Entities.TestHelpers get { return new NamedId(SchemaId, SchemaName); } } - protected IAggregateHandler Handler + protected abstract Guid Id { get; } + + public IGrainIdentity Identity { - get { return handler; } + get { return identity; } } - protected CommandContext CreateContextForCommand(TCommand command) where TCommand : SquidexCommand + public IGrainRuntime Runtime { - return new CommandContext(CreateCommand(command), A.Dummy()); + get { return runtime; } } - protected async Task TestCreate(T domainObject, Func action, bool shouldCreate = true) + public IStore Store { - handler.Init(domainObject); - - await domainObject.ActivateAsync(Id, A.Fake>()); - await action(domainObject); - - if (!handler.IsCreated && shouldCreate) - { - throw new InvalidOperationException("Create not called."); - } + get { return store; } } - protected async Task TestUpdate(T domainObject, Func action, bool shouldUpdate = true) + public IEnumerable> LastEvents { get; private set; } = Enumerable.Empty>(); + + protected HandlerTestBase() { - handler.Init(domainObject); + A.CallTo(() => identity.PrimaryKey) + .Returns(Id); - await domainObject.ActivateAsync(Id, A.Fake>()); - await action(domainObject); + A.CallTo(() => store.WithSnapshotsAndEventSourcing(A.Ignored, Id, A>.Ignored, A, Task>>.Ignored)) + .Returns(persistence); - if (!handler.IsUpdated && shouldUpdate) - { - throw new InvalidOperationException("Update not called."); - } + A.CallTo(() => persistence.WriteEventsAsync(A>>.Ignored)) + .Invokes(new Action>>(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 = User; @@ -147,25 +111,35 @@ namespace Squidex.Domain.Apps.Entities.TestHelpers return command; } + protected static J J(IAggregateCommand command) + { + return command.AsJ(); + } + protected TEvent CreateEvent(TEvent @event) where TEvent : SquidexEvent { @event.Actor = User; - var appEvent = @event as AppEvent; + EnrichAppInfo(@event); + EnrichSchemaInfo(@event); + + return @event; + } - if (appEvent != null) + private void EnrichAppInfo(IEvent @event) + { + if (@event is AppEvent appEvent) { appEvent.AppId = AppNamedId; } + } - var schemaEvent = @event as SchemaEvent; - - if (schemaEvent != null) + private void EnrichSchemaInfo(IEvent @event) + { + if (@event is SchemaEvent schemaEvent) { schemaEvent.SchemaId = SchemaNamedId; } - - return @event; } } }