// ========================================================================== // 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.Schemas.Commands; using Squidex.Infrastructure; using Squidex.Infrastructure.Commands; using Squidex.Infrastructure.Orleans; using Xunit; namespace Squidex.Domain.Apps.Entities.Schemas.Indexes { public class SchemasByAppIndexCommandMiddlewareTests { private readonly IGrainFactory grainFactory = A.Fake(); private readonly ICommandBus commandBus = A.Fake(); private readonly ISchemasByAppIndex index = A.Fake(); private readonly Guid appId = Guid.NewGuid(); private readonly Guid schemaId = Guid.NewGuid(); private readonly SchemasByAppIndexCommandMiddleware sut; public SchemasByAppIndexCommandMiddlewareTests() { A.CallTo(() => grainFactory.GetGrain(appId, null)) .Returns(index); sut = new SchemasByAppIndexCommandMiddleware(grainFactory); } [Fact] public async Task Should_add_schema_to_index_on_create() { var context = new CommandContext(new CreateSchema { SchemaId = appId, Name = "my-schema", AppId = BuildAppId() }, commandBus) .Complete(); await sut.HandleAsync(context); A.CallTo(() => index.AddSchemaAsync(appId, "my-schema")) .MustHaveHappened(); } [Fact] public async Task Should_remove_schema_from_index_on_delete() { var schemaGrain = A.Fake(); var schemaState = A.Fake(); A.CallTo(() => grainFactory.GetGrain(appId, null)) .Returns(schemaGrain); A.CallTo(() => schemaGrain.GetStateAsync()) .Returns(J.AsTask(schemaState)); A.CallTo(() => schemaState.AppId) .Returns(BuildAppId()); var context = new CommandContext(new DeleteSchema { SchemaId = appId }, commandBus) .Complete(); await sut.HandleAsync(context); A.CallTo(() => index.RemoveSchemaAsync(appId)) .MustHaveHappened(); } private NamedId BuildAppId() { return NamedId.Of(appId, "my-app"); } } }