Exemplo n.º 1
0
        public RiskControlSagaTests()
        {
            _logFactory          = LogFactory.Create().AddUnbufferedConsole();
            _chaosKittyMock      = new Mock <IChaosKitty>();
            _eventsPublisherMock = new Mock <IEventPublisher>();
            _commandSender       = new Mock <ICommandSender>();
            _cashoutRiskControlRepositoryMock = new Mock <ICashoutRiskControlRepository>();
            _batchRepositoryMock = new Mock <ICashoutsBatchRepository>();
            _closedBatchedCashoutsRepositoryMock = new Mock <IClosedBatchedCashoutRepository>();
            _activeCashoutsBatchIdRepositoryMock = new Mock <IActiveCashoutsBatchIdRepository>();
            _assetsServiceMock = new Mock <IAssetsServiceWithCache>();
            _walletsClientMock = new Mock <IBlockchainWalletsClient>();
            _cqrsSettings      = new CqrsSettings
            {
                RabbitConnectionString = "fake-connection-string",
                RetryDelay             = TimeSpan.FromSeconds(30)
            };
            _blockchainConfigurationProvider = new BlockchainConfigurationsProvider
                                               (
                _logFactory,
                new Dictionary <string, BlockchainConfiguration>
            {
                { "Bitcoin", new BlockchainConfiguration("HotWallet", false, null) }
            }
                                               );

            _asset = new Asset
            {
                Id = "LykkeBTC",
                BlockchainIntegrationLayerId      = "Bitcoin",
                BlockchainIntegrationLayerAssetId = "BTC"
            };

            _aggregate = null;

            _cashoutRiskControlRepositoryMock
            .Setup(x => x.GetOrAddAsync(
                       It.IsAny <Guid>(),
                       It.IsAny <Func <CashoutRiskControlAggregate> >()))
            .ReturnsAsync((Guid opId, Func <CashoutRiskControlAggregate> factory) => _aggregate ?? (_aggregate = factory()));

            _cashoutRiskControlRepositoryMock
            .Setup(x => x.SaveAsync(It.IsAny <CashoutRiskControlAggregate>()))
            .Callback((CashoutRiskControlAggregate agg) => _aggregate = agg)
            .Returns(Task.CompletedTask);

            _cashoutRiskControlRepositoryMock
            .Setup(x => x.TryGetAsync(It.IsAny <Guid>()))
            .ReturnsAsync((Guid opId) => opId == _aggregate?.OperationId ? _aggregate : null);

            _assetsServiceMock
            .Setup(x => x.TryGetAssetAsync(
                       It.Is <string>(p => p == "LykkeBTC"),
                       It.IsAny <CancellationToken>()))
            .ReturnsAsync(() => _asset);

            _walletsClientMock
            .Setup(x => x.TryGetClientIdAsync(
                       It.Is <string>(p => p == "Bitcoin"),
                       It.IsAny <string>()))
            .ReturnsAsync((Guid?)null);

            _startCashoutCommandsHandler = new StartCashoutCommandsHandler(_logFactory, _blockchainConfigurationProvider, _assetsServiceMock.Object, new Mock <IHttpClientFactory>().Object);

            _notifyCashoutFailedCommandsHandler = new NotifyCashoutFailedCommandsHandler();

            _acceptCashoutCommandsHandler = new AcceptCashoutCommandsHandler(
                _chaosKittyMock.Object,
                _batchRepositoryMock.Object,
                _closedBatchedCashoutsRepositoryMock.Object,
                _activeCashoutsBatchIdRepositoryMock.Object,
                _blockchainConfigurationProvider,
                _walletsClientMock.Object,
                _cqrsSettings,
                false);

            _saga = new RiskControlSaga(_chaosKittyMock.Object, _cashoutRiskControlRepositoryMock.Object);

            _eventsPublisherMock.Setup(x => x.PublishEvent(It.IsAny <ValidationStartedEvent>()))
            .Callback((object evt) => _saga.Handle((ValidationStartedEvent)evt, _commandSender.Object));

            _eventsPublisherMock.Setup(x => x.PublishEvent(It.IsAny <OperationAcceptedEvent>()))
            .Callback((object evt) => _saga.Handle((OperationAcceptedEvent)evt, _commandSender.Object));

            _eventsPublisherMock.Setup(x => x.PublishEvent(It.IsAny <OperationRejectedEvent>()))
            .Callback((object evt) => _saga.Handle((OperationRejectedEvent)evt, _commandSender.Object));

            _commandSender
            .Setup(x => x.SendCommand(
                       It.IsAny <AcceptCashoutCommand>(),
                       It.Is <string>(v => v == BlockchainCashoutProcessorBoundedContext.Name),
                       It.IsAny <uint>()))
            .Callback((AcceptCashoutCommand cmd, string bc, uint _) => _acceptCashoutCommandsHandler.Handle(cmd, _eventsPublisherMock.Object));

            _commandSender
            .Setup(x => x.SendCommand(
                       It.IsAny <NotifyCashoutFailedCommand>(),
                       It.Is <string>(v => v == BlockchainCashoutProcessorBoundedContext.Name),
                       It.IsAny <uint>()))
            .Callback((NotifyCashoutFailedCommand cmd, string bc, uint _) => _notifyCashoutFailedCommandsHandler.Handle(cmd, _eventsPublisherMock.Object));
        }
        public async Task Batch_Filling_Started()
        {
            // Arrange

            var clientId  = Guid.NewGuid();
            var cashoutId = Guid.NewGuid();

            // Act

            var handlingResult = await _handler.Handle
                                 (
                new AcceptCashoutCommand
            {
                BlockchainType    = "Bitcoin",
                BlockchainAssetId = "BTC",
                AssetId           = "LykkeBTC",
                Amount            = 100,
                ClientId          = clientId,
                OperationId       = cashoutId,
                HotWalletAddress  = "HotWallet",
                ToAddress         = "Destination"
            },
                _eventsPublisherMock.Object
                                 );

            // Assert

            Assert.False(handlingResult.Retry);
            Assert.Equal(CashoutsBatchState.FillingUp, _batch.State);
            Assert.Equal(1, _batch.Cashouts.Count);
            Assert.Equal(cashoutId, _batch.Cashouts.First().CashoutId);

            _batchRepositoryMock.Verify(x => x.SaveAsync(It.Is <CashoutsBatchAggregate>(p => p.BatchId == _batch.BatchId)), Times.Once);
            _eventsPublisherMock.Verify(x => x.PublishEvent(It.Is <BatchFillingStartedEvent>(p => p.BatchId == _batch.BatchId)), Times.Once);
            _eventsPublisherMock
            .Verify
            (
                x => x.PublishEvent
                (
                    It.Is <BatchedCashoutStartedEvent>(p =>
                                                       p.BatchId == _batch.BatchId &&
                                                       p.Amount == 100 &&
                                                       p.AssetId == "LykkeBTC" &&
                                                       p.BlockchainAssetId == "BTC" &&
                                                       p.BlockchainType == "Bitcoin" &&
                                                       p.ClientId == clientId &&
                                                       p.HotWalletAddress == "HotWallet" &&
                                                       p.OperationId == cashoutId &&
                                                       p.ToAddress == "Destination")
                ),
                Times.Once
            );
            _eventsPublisherMock.VerifyNoOtherCalls();
        }