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

            services.AddSingleton <Counter>();

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

            services.Remove(serviceDescriptor);
            services.AddScoped <IRequestHandler <CreateInvoiceCommand, CreateInvoiceCommandResponse>, FakeCreateInvoiceHandler>();

            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++)
                {
                    CreateInvoiceCommandResponse createInvoiceResponse = await messageManager.SendCommand(
                        new CreateInvoiceCommand(new Invoice(id: Guid.NewGuid(), number: "J/01/2019", creationDate: DateTime.Now)));
                }

                // Assert
                counter.Get().Should().Be(count);
            }
        }
        public async Task <IActionResult> CreateInvoice([FromBody] CreateInvoiceRequest request)
        {
            CreateInvoiceCommandResponse result = await _messageManager.SendCommand(
                new CreateInvoiceCommand(new Invoice(id: Guid.NewGuid(), number: request.Number, creationDate: request.CreationDate)));

            return(CreatedAtAction(nameof(GetInvoice), new GetInvoiceRequest {
                Id = result.Id
            },
                                   new CreateInvoiceResponse(result.Id, StatusCodes.Status201Created)));
        }
        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);
            }
        }
        public async Task All_created_invoices_should_be_able_to_get_by_passing_their_id(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)
                {
                    GetInvoiceQueryResponse queryResponse = await messageManager.SendCommand(new GetInvoiceQuery(createdInvoice.Id));

                    queryResponse.Invoice.Should().NotBeNull();
                    queryResponse.Invoice.Id.ToString().Should().BeEquivalentTo(createdInvoice.Id.ToString());
                }
            }
        }