Ejemplo n.º 1
0
        public async Task Expire_Batch_If_Batch_Is_Have_To_Be_Expired()
        {
            // Arrange

            _batch.AddCashout(new BatchedCashoutValueType(Guid.NewGuid(), Guid.NewGuid(), "Destination", 100, 0, DateTime.UtcNow));

            var batchEntity = CashoutsBatchEntity.FromDomain(_batch);

            batchEntity.StartMoment = DateTime.UtcNow - _batch.AgeThreshold - TimeSpan.FromSeconds(1);

            _batch = batchEntity.ToDomain();

            // Act

            var handlingResult = await _handler.Handle
                                 (
                new WaitForBatchExpirationCommand
            {
                BatchId = _batch.BatchId
            },
                _eventsPublisherMock.Object
                                 );

            // Assert

            Assert.False(handlingResult.Retry);
            Assert.Equal(CashoutsBatchState.Expired, _batch.State);

            _batchRepositoryMock.Verify(x => x.SaveAsync(It.Is <CashoutsBatchAggregate>(p => p.BatchId == _batch.BatchId)), Times.Once);
            _eventsPublisherMock.Verify(x => x.PublishEvent(It.Is <BatchExpiredEvent>(p => p.BatchId == _batch.BatchId)), Times.Once);
            _eventsPublisherMock.VerifyNoOtherCalls();
        }
        public async Task Wait_For_The_Next_Batch_If_Current_Batch_Is_Already_Not_Filling()
        {
            // Arrange

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

            var batchEntity = CashoutsBatchEntity.FromDomain(_batch);

            batchEntity.State = CashoutsBatchState.Filled;

            _batch = batchEntity.ToDomain();

            // 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.True(handlingResult.Retry);
            Assert.Equal(_cqrsSettings.RetryDelay, TimeSpan.FromMilliseconds(handlingResult.RetryDelay));

            _batchRepositoryMock.Verify(x => x.SaveAsync(It.IsAny <CashoutsBatchAggregate>()), Times.Never);
            _eventsPublisherMock.VerifyNoOtherCalls();
        }
Ejemplo n.º 3
0
        public async Task Expire_Batch_If_Batch_Is_Have_To_Be_Expired_With_Batch_Saving_And_Event_Publishing_Failures()
        {
            // == Step 1 - Batch saving failure ==

            // Arrange 1

            _batch.AddCashout(new BatchedCashoutValueType(Guid.NewGuid(), Guid.NewGuid(), "Destination", 100, 0, DateTime.UtcNow));

            var batchEntity = CashoutsBatchEntity.FromDomain(_batch);

            batchEntity.StartMoment = DateTime.UtcNow - _batch.AgeThreshold - TimeSpan.FromSeconds(1);

            _batch = batchEntity.ToDomain();

            _batchRepositoryMock
            .Setup(x => x.SaveAsync(It.Is <CashoutsBatchAggregate>(p => p.BatchId == _batch.BatchId)))
            .Throws <InvalidOperationException>();

            // Act/Assert 1

            await Assert.ThrowsAsync <InvalidOperationException>(() => _handler.Handle
                                                                 (
                                                                     new WaitForBatchExpirationCommand
            {
                BatchId = _batch.BatchId
            },
                                                                     _eventsPublisherMock.Object
                                                                 ));

            // Assert 1

            Assert.Equal(CashoutsBatchState.Expired, _batch.State);

            _batchRepositoryMock.Verify(x => x.SaveAsync(It.Is <CashoutsBatchAggregate>(p => p.BatchId == _batch.BatchId)), Times.Once);
            _eventsPublisherMock.Verify(x => x.PublishEvent(It.IsAny <BatchExpiredEvent>()), Times.Never);
            _eventsPublisherMock.VerifyNoOtherCalls();

            // == Step 2 - Event publishing failure ==

            _batchRepositoryMock.Invocations.Clear();
            _eventsPublisherMock.Invocations.Clear();

            _batchRepositoryMock
            .Setup(x => x.SaveAsync(It.Is <CashoutsBatchAggregate>(p => p.BatchId == _batch.BatchId)))
            .Returns(Task.CompletedTask);

            _eventsPublisherMock
            .Setup(x => x.PublishEvent(It.Is <BatchExpiredEvent>(p => p.BatchId == _batch.BatchId)))
            .Throws <InvalidOperationException>();

            // Act/Assert 2

            await Assert.ThrowsAsync <InvalidOperationException>(() => _handler.Handle
                                                                 (
                                                                     new WaitForBatchExpirationCommand
            {
                BatchId = _batch.BatchId
            },
                                                                     _eventsPublisherMock.Object
                                                                 ));

            // Assert 2

            Assert.Equal(CashoutsBatchState.Expired, _batch.State);

            _batchRepositoryMock.Verify(x => x.SaveAsync(It.IsAny <CashoutsBatchAggregate>()), Times.Never);
            _eventsPublisherMock.Verify(x => x.PublishEvent(It.Is <BatchExpiredEvent>(p => p.BatchId == _batch.BatchId)), Times.Once);
            _eventsPublisherMock.VerifyNoOtherCalls();

            // == Step 3 - Success ==

            // Arrange 3

            _batchRepositoryMock.Invocations.Clear();
            _eventsPublisherMock.Invocations.Clear();

            _eventsPublisherMock
            .Setup(x => x.PublishEvent(It.Is <BatchExpiredEvent>(p => p.BatchId == _batch.BatchId)))
            .Callback(() => { });

            // Act 3

            var handlingResult = await _handler.Handle
                                 (
                new WaitForBatchExpirationCommand
            {
                BatchId = _batch.BatchId
            },
                _eventsPublisherMock.Object
                                 );

            // Assert 3

            Assert.False(handlingResult.Retry);
            Assert.Equal(CashoutsBatchState.Expired, _batch.State);

            _batchRepositoryMock.Verify(x => x.SaveAsync(It.IsAny <CashoutsBatchAggregate>()), Times.Never);
            _eventsPublisherMock.Verify(x => x.PublishEvent(It.Is <BatchExpiredEvent>(p => p.BatchId == _batch.BatchId)), Times.Once);
            _eventsPublisherMock.VerifyNoOtherCalls();
        }