// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschraenkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using System; using System.Threading.Tasks; using FakeItEasy; using Orleans; using Squidex.Domain.Apps.Entities.Rules.Commands; using Squidex.Infrastructure; using Squidex.Infrastructure.Commands; using Squidex.Infrastructure.Orleans; using Xunit; namespace Squidex.Domain.Apps.Entities.Rules.Indexes { public class RulesByAppIndexCommandMiddlewareTests { private readonly IGrainFactory grainFactory = A.Fake(); private readonly ICommandBus commandBus = A.Fake(); private readonly IRulesByAppIndex index = A.Fake(); private readonly Guid appId = Guid.NewGuid(); private readonly Guid ruleId = Guid.NewGuid(); private readonly RulesByAppIndexCommandMiddleware sut; public RulesByAppIndexCommandMiddlewareTests() { A.CallTo(() => grainFactory.GetGrain(appId, null)) .Returns(index); sut = new RulesByAppIndexCommandMiddleware(grainFactory); } [Fact] public async Task Should_add_rule_to_index_on_create() { var context = new CommandContext(new CreateRule { RuleId = appId, AppId = BuildAppId() }, commandBus) .Complete(); await sut.HandleAsync(context); A.CallTo(() => index.AddRuleAsync(appId)) .MustHaveHappened(); } [Fact] public async Task Should_remove_rule_from_index_on_delete() { var ruleGrain = A.Fake(); var ruleState = A.Fake(); A.CallTo(() => grainFactory.GetGrain(appId, null)) .Returns(ruleGrain); A.CallTo(() => ruleGrain.GetStateAsync()) .Returns(J.AsTask(ruleState)); A.CallTo(() => ruleState.AppId) .Returns(BuildAppId()); var context = new CommandContext(new DeleteRule { RuleId = appId }, commandBus) .Complete(); await sut.HandleAsync(context); A.CallTo(() => index.RemoveRuleAsync(appId)) .MustHaveHappened(); } private NamedId BuildAppId() { return NamedId.Of(appId, "my-app"); } } }