public async Task DepositWalletLockReleasedEventSent__AggregateIsFailedCashin__NotifyCashinFailedCommandSent() { var operationId = Guid.NewGuid(); var cashinAggregate = CashinAggregate.Restore(operationId, "ETC", 6, 6, 10, 250, "ETC", "EthereumClassic", 0, DateTime.UtcNow, "0x...", 150, 250, null, null, null, 0.05m, "0x...", DateTime.UtcNow, 10, 10, DateTime.UtcNow, null, operationId, CashinResult.Failure, null, null, 10, 250, "0xHASH", CashinState.OutdatedBalance, true, "1.0.0", CashinErrorCode.Unknown, null); var cashinRepoMock = new Mock <ICashinRepository>(); cashinRepoMock.Setup(x => x.GetAsync(It.IsAny <Guid>())).ReturnsAsync(cashinAggregate); var repoModule = new RepoMockModule((builder) => { var depositWalletLockRepository = new Mock <IDepositWalletLockRepository>(); var matchingEngineCallsDeduplicationRepository = new Mock <IMatchingEngineCallsDeduplicationRepository>(); var enrolledBalanceRepository = new Mock <IEnrolledBalanceRepository>(); builder.RegisterInstance(cashinRepoMock.Object) .As <ICashinRepository>(); builder.RegisterInstance(matchingEngineCallsDeduplicationRepository.Object) .As <IMatchingEngineCallsDeduplicationRepository>(); builder.RegisterInstance(enrolledBalanceRepository.Object) .As <IEnrolledBalanceRepository>(); builder.RegisterInstance(depositWalletLockRepository.Object) .As <IDepositWalletLockRepository>(); }); var dependencies = GetIntegrationDependencies(); dependencies.Add(repoModule); var testContainer = ContainerCreator.CreateContainer( dependencies.ToArray() ); var cashinRepo = testContainer.Resolve <ICashinRepository>(); var cqrsEngine = testContainer.Resolve <ICqrsEngine>(); var @event = new DepositWalletLockReleasedEvent() { OperationId = operationId }; cqrsEngine.StartSubscribers(); cqrsEngine.PublishEvent(@event, CqrsTestModule.Self); await CqrsTestModule.EventsInterceptor.WaitForEventToBeHandledWithTimeoutAsync( typeof(DepositWalletLockReleasedEvent), TimeSpan.FromMinutes(4)); await CqrsTestModule.CommandsInterceptor.WaitForCommandToBeHandledWithTimeoutAsync( typeof(NotifyCashinFailedCommand), TimeSpan.FromMinutes(4)); }
private async Task Handle(DepositWalletLockReleasedEvent evt, ICommandSender sender) { var aggregate = await _cashinRepository.GetAsync(evt.OperationId); var transitionResult = aggregate.OnDepositWalletLockReleased(); if (transitionResult.ShouldSaveAggregate()) { await _cashinRepository.SaveAsync(aggregate); } if (aggregate.Result == CashinResult.OutdatedBalance) { return; } // Sending the "off-blockchain operation" event, if needed. if (!aggregate.IsDustCashin.HasValue) { throw new InvalidOperationException("IsDustCashin should be not null here"); } if (!aggregate.ClientId.HasValue) { throw new InvalidOperationException("Client ID should be not null here"); } if (!aggregate.OperationAmount.HasValue) { throw new InvalidOperationException("Operation amount should be not null here"); } if (aggregate.OperationAmount.Value == 0M) { throw new InvalidOperationException("Operation amount should be not 0 here"); } if (transitionResult.ShouldSendCommands()) { if (aggregate.ErrorCode.HasValue) { sender.SendCommand ( new NotifyCashinFailedCommand { OperationId = aggregate.OperationId, Amount = aggregate.OperationAmount.Value, ClientId = aggregate.ClientId.Value, AssetId = aggregate.AssetId, Error = aggregate.Error, ErrorCode = aggregate.ErrorCode.Value.MapToCashinErrorCode() }, Self ); } else if (aggregate.IsDustCashin.Value) { sender.SendCommand ( new NotifyCashinCompletedCommand { OperationAmount = aggregate.OperationAmount.Value, TransactionnAmount = 0M, TransactionFee = 0M, AssetId = aggregate.AssetId, ClientId = aggregate.ClientId.Value, OperationType = CashinOperationType.OffBlockchain, OperationId = aggregate.OperationId, TransactionHash = "0x" }, Self ); } else { if (!aggregate.TransactionAmount.HasValue) { throw new InvalidOperationException("Transaction amount should be not null here"); } if (aggregate.TransactionAmount.Value == 0M) { throw new InvalidOperationException("Transaction amount should be not 0 here"); } if (!aggregate.Fee.HasValue) { throw new InvalidOperationException("TransactionFee should be not null here"); } sender.SendCommand ( new NotifyCashinCompletedCommand { OperationAmount = aggregate.OperationAmount.Value, TransactionnAmount = aggregate.TransactionAmount.Value, TransactionFee = aggregate.Fee.Value, AssetId = aggregate.AssetId, ClientId = aggregate.ClientId.Value, OperationType = CashinOperationType.OnBlockchain, OperationId = aggregate.OperationId, TransactionHash = aggregate.TransactionHash }, Self ); } _chaosKitty.Meow(aggregate.OperationId); } }