public static async Task TransferMoney(
            [EventHubTrigger(@"%TransferEventHubName%", Connection = @"EventHubsNamespaceConnection")] EventData[] eventsData,
            [EventHub(@"%ReplyEventHubName%", Connection = @"EventHubsNamespaceConnection")] IAsyncCollector <EventData> eventCollector,
            [CosmosDB(
                 databaseName: @"%CosmosDbDatabaseName%",
                 collectionName: @"%CosmosDbTransferCollectionName%",
                 ConnectionStringSetting = @"CosmosDbConnectionString")]
            IAsyncCollector <CheckingAccountLine> stateCollector,
            ILogger logger)
        {
            IMessageProducer eventProducer = new EventHubMessageProducer(eventCollector);
            IRepository <CheckingAccountLine> repository = new CosmosDbRepository <CheckingAccountLine>(stateCollector);
            var processors = TransferServiceCommandProcessorFactory.BuildProcessorMap(eventProducer, repository);
            var dispatcher = new CommandProcessorDispatcher(processors);

            foreach (EventData eventData in eventsData)
            {
                try
                {
                    var commandContainer = CommandContainerFactory.BuildCommandContainer(eventData);
                    await dispatcher.ProcessCommandAsync(commandContainer);
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, ex.Message);
                }
            }
        }
        public void Command_should_not_be_processed_by_invalid_container()
        {
            var commandProcessorMock = new Mock <ICommandProcessor>();

            commandProcessorMock
            .Setup(x => x.ProcessAsync(It.IsAny <ICommandContainer>()))
            .Returns(Task.CompletedTask);

            var processors = new Dictionary <string, ICommandProcessor>
            {
                { nameof(DefaultCommand), commandProcessorMock.Object }
            };

            var dispatcher = new CommandProcessorDispatcher(processors);
            var exception  = Assert.ThrowsAsync <ArgumentException>(async() => await dispatcher.ProcessCommandAsync(null));

            Assert.NotNull(exception);
        }
        public async Task Command_should_be_processed_by_valid_dispatcher()
        {
            var messageProducerMock  = new Mock <IMessageProducer>();
            var repository           = new Mock <IRepository <object> >();
            var commandProcessorMock = new Mock <ICommandProcessor>();
            var commandContainerMock = new Mock <ICommandContainer>();

            messageProducerMock
            .Setup(x => x.ProduceAsync(It.IsAny <DefaultEvent>()))
            .Returns(Task.CompletedTask);

            repository
            .Setup(x => x.AddAsync(It.IsAny <object>()))
            .Returns(Task.CompletedTask);

            commandProcessorMock
            .Setup(x => x.ProcessAsync(It.IsAny <ICommandContainer>()))
            .Returns(Task.CompletedTask);

            commandContainerMock
            .Setup(x => x.ParseCommand <DefaultCommand>())
            .Returns(new DefaultCommand
            {
                Header = new MessageHeader(Guid.NewGuid().ToString(), nameof(DefaultCommand), nameof(Sources.Orchestrator)),
            });

            var processors = new Dictionary <string, ICommandProcessor>
            {
                { nameof(DefaultCommand), commandProcessorMock.Object }
            };

            var dispatcher = new CommandProcessorDispatcher(processors);
            await dispatcher.ProcessCommandAsync(commandContainerMock.Object);

            Assert.NotNull(dispatcher);
        }