Headless CMS and Content Managment Hub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

79 lines
2.6 KiB

// ==========================================================================
// 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<IGrainFactory>();
private readonly ICommandBus commandBus = A.Fake<ICommandBus>();
private readonly IRulesByAppIndex index = A.Fake<IRulesByAppIndex>();
private readonly Guid appId = Guid.NewGuid();
private readonly RulesByAppIndexCommandMiddleware sut;
public RulesByAppIndexCommandMiddlewareTests()
{
A.CallTo(() => grainFactory.GetGrain<IRulesByAppIndex>(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<IRuleGrain>();
var ruleState = A.Fake<IRuleEntity>();
A.CallTo(() => grainFactory.GetGrain<IRuleGrain>(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<Guid> BuildAppId()
{
return NamedId.Of(appId, "my-app");
}
}
}