public async Task Get_invoices_handler_should_be_called_the_same_number_of_times_as_get_invoices_query(int count)
        {
            // Arrange
            ServiceCollection services = TestHelper.PrepareServiceCollection();

            services.AddSingleton <Counter>();

            // Replace the registered event class
            ServiceDescriptor serviceDescriptor = services.FirstOrDefault(d => d.ServiceType == typeof(IRequestHandler <GetInvoicesQuery, GetInvoicesQueryResponse>));

            services.Remove(serviceDescriptor);
            services.AddScoped <IRequestHandler <GetInvoicesQuery, GetInvoicesQueryResponse>, FakeGetInvoicesHandler>();

            ServiceProvider serviceProvider = services.BuildServiceProvider();

            using (IServiceScope scope = serviceProvider.CreateScope())
            {
                var scopedServices = scope.ServiceProvider;

                IMessageManager messageManager = scopedServices.GetRequiredService <IMessageManager>();

                Counter counter = scopedServices.GetRequiredService <Counter>();

                // Act
                for (int i = 0; i < count; i++)
                {
                    GetInvoicesQueryResponse queryResponse = await messageManager.SendCommand(new GetInvoicesQuery());
                }

                // Assert
                counter.Get().Should().Be(count);
            }
        }
        public async Task Providing_invalid_invoice_number_when_creating_the_invoice_should_thrown_DomainException(string invalidInvoiceNumber)
        {
            ServiceProvider serviceProvider = TestHelper.PrepareServiceProvider();

            using (IServiceScope scope = serviceProvider.CreateScope())
            {
                var scopedServices = scope.ServiceProvider;

                IMessageManager messageManager = scopedServices.GetRequiredService <IMessageManager>();

                var invoiceRepository = scopedServices.GetRequiredService <IInvoiceRepository>();

                DomainException ex = Assert.ThrowsAsync <DomainException>(async() =>
                {
                    _ = await messageManager.SendCommand(
                        new CreateInvoiceCommand(new Invoice(id: Guid.NewGuid(), number: invalidInvoiceNumber,
                                                             creationDate: DateTime.Now)));
                });

                ex.DomainErrors.Should().HaveCount(1);
                ex.DomainErrors.First().ErrorCode.Should().BeEquivalentTo("LengthValidator");
                ex.DomainErrors.First().PropertyName.Should().BeEquivalentTo("Invoice.Number");

                GetInvoicesQueryResponse result = await messageManager.SendCommand(new GetInvoicesQuery());

                result.Invoices.Should().HaveCount(0);
            }
        }
        public async Task <ActionResult <InvoicesResponse> > GetInvoices()
        {
            GetInvoicesQueryResponse result = await _messageManager.SendCommand(new GetInvoicesQuery());

            if (result.Invoices is null || !result.Invoices.Any())
            {
                return(NoContent());
            }

            var response = new InvoicesResponse(
                result.Invoices.Select(c => new InvoiceDto(c.Id, c.Number, c.CreationDate)),
                StatusCodes.Status200OK);

            return(Ok(response));
        }
        public async Task All_created_invoices_should_be_added_to_the_repository(int count)
        {
            ServiceProvider serviceProvider = TestHelper.PrepareServiceProvider();

            using (IServiceScope scope = serviceProvider.CreateScope())
            {
                var scopedServices = scope.ServiceProvider;

                IMessageManager messageManager = scopedServices.GetRequiredService <IMessageManager>();


                // Arrange

                // empty repository
                var invoiceRepository = scopedServices.GetRequiredService <IInvoiceRepository>();

                List <CreateInvoiceCommandResponse> createInvoiceResponses = new List <CreateInvoiceCommandResponse>();

                // Act
                for (int i = 0; i < count; i++)
                {
                    CreateInvoiceCommandResponse createInvoiceResponse = await messageManager.SendCommand(
                        new CreateInvoiceCommand(new Invoice(id: Guid.NewGuid(), number: "J/01/2019", creationDate: DateTime.Now)));

                    createInvoiceResponses.Add(createInvoiceResponse);
                }

                GetInvoicesQueryResponse queryResponse = await messageManager.SendCommand(new GetInvoicesQuery());

                // Assert
                queryResponse.Invoices.Should().HaveCount(count);

                // checking if the invoices ids match
                for (int i = 0; i < queryResponse.Invoices.Count(); i++)
                {
                    Guid repoInvoiceId    = queryResponse.Invoices.ElementAt(i).Id;
                    Guid createdInvoiceId = createInvoiceResponses[i].Id;

                    repoInvoiceId.ToString().Should().BeEquivalentTo(createdInvoiceId.ToString());
                }
            }
        }
        public async Task Id_should_be_possible_to_delete_all_created_invoices(int count)
        {
            ServiceProvider serviceProvider = TestHelper.PrepareServiceProvider();

            using (IServiceScope scope = serviceProvider.CreateScope())
            {
                var scopedServices = scope.ServiceProvider;

                IMessageManager messageManager = scopedServices.GetRequiredService <IMessageManager>();

                // Arrange

                // empty repository
                var invoiceRepository = scopedServices.GetRequiredService <IInvoiceRepository>();

                List <CreateInvoiceCommandResponse> createInvoiceResponses = new List <CreateInvoiceCommandResponse>();

                // Act
                for (int i = 0; i < count; i++)
                {
                    CreateInvoiceCommandResponse createInvoiceResponse = await messageManager.SendCommand(
                        new CreateInvoiceCommand(new Invoice(id: Guid.NewGuid(), number: "JK/02/2019", creationDate: DateTime.Now)));

                    createInvoiceResponses.Add(createInvoiceResponse);
                }

                // Assert
                foreach (var createdInvoice in createInvoiceResponses)
                {
                    RemoveInvoiceCommandResponse removeResponse = await messageManager.SendCommand(new RemoveInvoiceCommand(createdInvoice.Id));

                    removeResponse.Removed.Should().BeTrue();
                }

                // Repo should be empty
                GetInvoicesQueryResponse queryResponse = await messageManager.SendCommand(new GetInvoicesQuery());

                queryResponse.Invoices.Should().HaveCount(0);
            }
        }