// ========================================================================== // AppCommandHandlerTests.cs // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex Group // All rights reserved. // ========================================================================== using System; using System.Threading.Tasks; using Moq; using Squidex.Infrastructure; using Squidex.Infrastructure.CQRS.Commands; using Squidex.Read.Apps; using Squidex.Read.Apps.Repositories; using Squidex.Write.Apps; using Squidex.Write.Apps.Commands; using Xunit; namespace Squidex.Write.Tests.Apps { public class AppCommandHandlerTests { private readonly Mock domainObjectFactory = new Mock(); private readonly Mock domainObjectRepository = new Mock(); private readonly Mock appRepository = new Mock(); private readonly AppCommandHandler sut; public AppCommandHandlerTests() { sut = new AppCommandHandler( domainObjectFactory.Object, domainObjectRepository.Object, appRepository.Object); } [Fact] public async Task Create_should_throw_if_a_name_with_same_name_already_exists() { appRepository.Setup(x => x.FindAppByNameAsync("my-app")).Returns(Task.FromResult(new Mock().Object)).Verifiable(); await Assert.ThrowsAsync(async () => await sut.On(new CreateApp { Name = "my-app" })); appRepository.VerifyAll(); } [Fact] public async Task Create_should_create_app_if_name_is_free() { var id = Guid.NewGuid(); var app = new AppDomainObject(id, 0); appRepository.Setup(x => x.FindAppByNameAsync("my-app")).Returns(Task.FromResult(null)).Verifiable(); domainObjectFactory.Setup(x => x.CreateNew(typeof(AppDomainObject), id)).Returns(app).Verifiable(); domainObjectRepository.Setup(x => x.SaveAsync(app, It.IsAny())).Returns(Task.FromResult(true)).Verifiable(); await sut.On(new CreateApp { Name = "my-app", AggregateId = id }); appRepository.VerifyAll(); domainObjectFactory.VerifyAll(); domainObjectRepository.VerifyAll(); } } }