public async Task <IActionResult> CreatePoll(CreatePollRequest request)
        {
            var createPollInput = new CreatePollInput(request.Title, request.Note, request.SingleOptionLimitation, request.DueDate, request.Options, request.ParticipantEmailAddresses);

            await this.createPollInputPort.HandleAsync(createPollInput, this.createPollPresenter);

            return(this.createPollPresenter.ViewModel);
        }
        public async Task HandleAsync_WithInvalidPoll_ShouldCallError()
        {
            // Arrange
            ILoggerService <CreatePollUseCase> loggerServiceStub = A.Fake <ILoggerService <CreatePollUseCase> >();

            ICreatePollOutputPort OutputPortMock = A.Fake <ICreatePollOutputPort>();

            var             useCase = new CreatePollUseCase(loggerServiceStub, null, null);
            CreatePollInput input   = GetInvalidCreatePollInput();

            // Act
            await useCase.HandleAsync(input, OutputPortMock);

            // Assert
            A.CallTo(() => OutputPortMock.Error(
                         A <string> .That.IsNotNull()))
            .MustHaveHappenedOnceExactly();
        }
        public async Task HandleAsync_WithValidPoll_ShouldCallSuccess()
        {
            // Arrange
            IEmailSender emailSenderStub = A.Fake <IEmailSender>();
            IPollGateway pollGatewayStub = A.Fake <IPollGateway>();

            ICreatePollOutputPort OutputPortMock = A.Fake <ICreatePollOutputPort>();

            var             useCase = new CreatePollUseCase(null, emailSenderStub, pollGatewayStub);
            CreatePollInput input   = GetValidCreatePollInput();

            // Act
            await useCase.HandleAsync(input, OutputPortMock);

            // Assert
            A.CallTo(() => OutputPortMock.Success(
                         A <CreatePollOutput> .That.Matches(
                             p => p.Id == 0)))
            .MustHaveHappenedOnceExactly();
        }
        public async Task HandleAsync_WithInvalidPoll_ShouldLogInformation()
        {
            // Arrange
            const string          expectedLogMessage = "{@excepton} occured when trying to create a poll with {@input}";
            ICreatePollOutputPort OutputPortStub     = A.Fake <ICreatePollOutputPort>();

            ILoggerService <CreatePollUseCase> loggerServiceMock = A.Fake <ILoggerService <CreatePollUseCase> >();

            var             useCase = new CreatePollUseCase(loggerServiceMock, null, null);
            CreatePollInput input   = GetInvalidCreatePollInput();

            // Act
            await useCase.HandleAsync(input, OutputPortStub);

            // Assert
            A.CallTo(
                () => loggerServiceMock.LogInformation(
                    A <string> .That.Contains(expectedLogMessage),
                    A <DomainException> ._,
                    A <CreatePollInput> ._))
            .MustHaveHappenedOnceExactly();
        }
        public async Task HandleAsync_WithValidPoll_ShouldSendEmail()
        {
            // Arrange
            IPollGateway          pollGatewayStub = A.Fake <IPollGateway>();
            ICreatePollOutputPort OutputPortStub  = A.Fake <ICreatePollOutputPort>();

            IEmailSender emailSenderMock = A.Fake <IEmailSender>();

            var             useCase = new CreatePollUseCase(null, emailSenderMock, pollGatewayStub);
            CreatePollInput input   = GetValidCreatePollInput();

            // Act
            await useCase.HandleAsync(input, OutputPortStub);

            // Assert
            A.CallTo(() => emailSenderMock.SendAsync(
                         A <string> .That.Contains("SUBJECT"),
                         A <string> .That.Contains("PLAIN_TEXT_CONTENT"),
                         //A<List<string>>.Ignored))
                         A <List <string> > .That.IsSameSequenceAs(input.ParticipantEmailAddresses)))
            .MustHaveHappenedOnceExactly();
        }
        public async Task HandleAsync(CreatePollInput input, ICreatePollOutputPort output)
        {
            try
            {
                var poll = new Poll(input.Title, input.Note, input.SingleOptionLimitation, input.DueDate);

                foreach (string option in input.Options)
                {
                    poll.AddOption(option);
                }

                await this.pollGateway.CreateAsync(poll);

                await this.emailSender.SendAsync("SUBJECT", "PLAIN_TEXT_CONTENT", input.ParticipantEmailAddresses);

                output.Success(new CreatePollOutput(poll.Id));
            }
            catch (DomainException ex)
            {
                this.loggerService.LogInformation("{@excepton} occured when trying to create a poll with {@input}", ex, input);
                output.Error(ex.Message);
            }
        }
        public async Task HandleAsync_WithValidPoll_ShouldCreateNewPoll()
        {
            // Arrange
            IEmailSender          emailSenderStub = A.Fake <IEmailSender>();
            ICreatePollOutputPort OutputPortStub  = A.Fake <ICreatePollOutputPort>();

            IPollGateway pollGatewayMock = A.Fake <IPollGateway>();

            var             useCase = new CreatePollUseCase(null, emailSenderStub, pollGatewayMock);
            CreatePollInput input   = GetValidCreatePollInput();

            // Act
            await useCase.HandleAsync(input, OutputPortStub);

            // Assert
            A.CallTo(() => pollGatewayMock.CreateAsync(
                         A <Poll> .That.Matches(
                             p => p.Title == input.Title &&
                             p.Note == input.Note &&
                             p.SingleOptionLimitation == input.SingleOptionLimitation &&
                             p.DueDate == input.DueDate &&
                             p.Options.Count == input.Options.Count)))
            .MustHaveHappenedOnceExactly();
        }