public void CannotWithdrawCashWithNonPositiveAmount(decimal amount) { CmdHandler.Handle(new CreateAccountCommand { AccountId = AccountId, HolderName = "John Doe" }); CmdHandler.Handle(new DepositCashCommand { AccountId = AccountId, Amount = 1000m }); CmdHandler.Handle(new SetDailyWireTransferLimitCommand { AccountId = AccountId, DailyWireTransferLimit = 1000m }); var cmd = new WithdrawCashCommand() { AccountId = AccountId, Amount = amount }; dynamic msg = CmdHandler.Handle(cmd); Assert.IsType <Fail>(msg); Assert.Equal("Cannot withdraw a negative amount", msg.Exception.Message); }
public void CannotExceedOverDraftLimit() { CmdHandler.Handle(new CreateAccountCommand { AccountId = AccountId, HolderName = "John Doe" }); CmdHandler.Handle(new SetDailyWireTransferLimitCommand { AccountId = AccountId, DailyWireTransferLimit = 1000m }); CmdHandler.Handle(new SetOverDraftLimitCommand { AccountId = AccountId, OverDraftLimit = 100m }); var cmd = new WithdrawCashCommand() { AccountId = AccountId, Amount = 101m }; dynamic msg = CmdHandler.Handle(cmd); Assert.IsType <Fail>(msg); Assert.Equal("Account is blocked, you exceeded your overdraft limit", msg.Exception.Message); }
public async Task WithdrawCash_command_need_update_debt_ammountAsync() { #region Arrange var accountId = Guid.Parse("1805FB93-2A90-4C9C-B286-EE9A62A94212"); Account account = GetAccount(); var repo = new Mock <Myrepo.IRepository>(); repo.Setup(x => x.GetById <Account>(It.IsAny <Guid>())).Returns(Task.FromResult(account)); CommandHandler commandHandler = new CommandHandler(repo.Object); await commandHandler.Handle(new DepositeCashCommand(accountId, 200)); #endregion #region Act WithdrawCashCommand withdrawCash = new WithdrawCashCommand(accountId, 200); await commandHandler.Handle(withdrawCash); #endregion #region Assert Assert.Equal(0, account.Debt); #endregion }
public void CannotWithdrawFromBlockedAccount() { CmdHandler.Handle(new CreateAccountCommand { AccountId = AccountId, HolderName = "John Doe" }); CmdHandler.Handle(new DepositCashCommand { AccountId = AccountId, Amount = 1000m }); var cmd = new WithdrawCashCommand() { AccountId = AccountId, Amount = 10m }; CmdHandler.Handle(cmd); dynamic msg = CmdHandler.Handle(cmd); Assert.IsType <Fail>(msg); Assert.Equal("Account is blocked", msg.Exception.Message); }
public void CanWithdrawCash() { CmdHandler.Handle(new CreateAccountCommand { AccountId = AccountId, HolderName = "John Doe" }); CmdHandler.Handle(new DepositCashCommand { AccountId = AccountId, Amount = 1000m }); CmdHandler.Handle(new SetDailyWireTransferLimitCommand { AccountId = AccountId, DailyWireTransferLimit = 1000m }); var cmd = new WithdrawCashCommand() { AccountId = AccountId, Amount = 100m }; Assert.IsType <Success>(CmdHandler.Handle(cmd)); }
public void CannotExceedDailyWireTransferLimit() { CmdHandler.Handle(new CreateAccountCommand { AccountId = AccountId, HolderName = "John Doe" }); CmdHandler.Handle(new DepositCashCommand { AccountId = AccountId, Amount = 1000m }); CmdHandler.Handle(new SetDailyWireTransferLimitCommand { AccountId = AccountId, DailyWireTransferLimit = 100m }); var cmd = new WithdrawCashCommand() { AccountId = AccountId, Amount = 101m }; dynamic msg = CmdHandler.Handle(cmd); Assert.IsType <Fail>(msg); Assert.StartsWith("Account is blocked, you only have", msg.Exception.Message); }
public CommandResponse Withdraw(Guid accountId, decimal amount) { var account = _repo.GetById <AccountAggregate>(accountId); var withdrawCommand = new WithdrawCashCommand { AccountId = account.Id, Amount = amount }; return(_cmdHandler.Handle(withdrawCommand)); }
public void CannotWithdrawCashFromInvalidAccount() { var cmd = new WithdrawCashCommand() { AccountId = AccountId, Amount = 200m }; dynamic msg = CmdHandler.Handle(cmd); Assert.IsType <Fail>(msg); Assert.Equal("No account with this ID exists", msg.Exception.Message); }
public async Task WithdrawCash_command_thorw_exception_when_amount_is_negative_or0(decimal amount) { #region Arrange var accountId = Guid.Parse("1805FB93-2A90-4C9C-B286-EE9A62A94212"); Account account = GetAccount(); var repo = new Mock <Myrepo.IRepository>(); repo.Setup(x => x.GetById <Account>(It.IsAny <Guid>())).Returns(Task.FromResult(account)); CommandHandler commandHandler = new CommandHandler(repo.Object); await commandHandler.Handle(new DepositeCashCommand(accountId, 200)); WithdrawCashCommand withdrawCash = new WithdrawCashCommand(accountId, amount); #endregion #region Act and Assert cause call async await Assert.ThrowsAsync <InvalidOperationException>(() => commandHandler.Handle(withdrawCash)); #endregion }
public CommandResponse Handle(WithdrawCashCommand command) { try { if (!_repository.TryGetById <AccountAggregate>(command.AccountId, out var account)) { throw new ValidationException("No account with this ID exists"); } var msg = account.Debit(command.Amount, command); _repository.Save(account); if (!string.IsNullOrEmpty(msg)) { throw new ValidationException(msg); } return(command.Succeed()); } catch (Exception e) { return(command.Fail(e)); } }
public void Run() { var cmd = new[] { "" }; AccountAggregate acct; do { cmd = Console.ReadLine().Split(' '); switch (cmd[0].ToLower()) { case "conn": _accountId = Guid.Parse(cmd[1]); acct = repo.GetById <AccountAggregate>(_accountId); _holderName = acct.HolderName; _readModel.redraw(acct); break; case "list": foreach (var account in _readModel.Accounts) { Console.WriteLine(account.Id + "\t" + account.HolderName); } break; case "new": _accountId = Guid.NewGuid(); if (cmd.Length > 1) { _holderName = cmd[1]; } var createCommand = new CreateAccountCommand() { AccountId = _accountId, HolderName = _holderName }; cmdHandler.Handle(createCommand); break; case "deposit": acct = repo.GetById <AccountAggregate>(_accountId); try { if (cmd[1].ToLower() == "cash") { var depositCashCommand = new DepositCashCommand() { AccountId = acct.Id, Amount = decimal.Parse(cmd[2]) }; cmdHandler.Handle(depositCashCommand); } else if (cmd[1].ToLower() == "check") { var depositCheckCommand = new DepositCheckCommand() { AccountId = acct.Id, Amount = decimal.Parse(cmd[2]) }; cmdHandler.Handle(depositCheckCommand); } Console.WriteLine($"Deposit {cmd[2]}"); } catch (Exception e) { Console.WriteLine("Command should be:\n\tdeposit {type} {amount}\n\ttype : 'cash' or 'check'\n\tamount : number"); } break; case "withdraw": try { acct = repo.GetById <AccountAggregate>(_accountId); var withdrawCommand = new WithdrawCashCommand { AccountId = acct.Id, Amount = decimal.Parse(cmd[1]) }; cmdHandler.Handle(withdrawCommand); Console.WriteLine($"Withdraw {cmd[1]}"); } catch (Exception e) { Console.WriteLine(e); } break; case "overdraft": acct = repo.GetById <AccountAggregate>(_accountId); try { var setOverDraftLimit = new SetOverDraftLimitCommand() { AccountId = acct.Id, OverDraftLimit = decimal.Parse(cmd[1]) }; cmdHandler.Handle(setOverDraftLimit); Console.WriteLine($"setting overdraftLimit to {cmd[1]}"); } catch (Exception e) { Console.WriteLine(e.Message); } break; case "daily": acct = repo.GetById <AccountAggregate>(_accountId); try { var setDailyWireTransferLimit = new SetDailyWireTransferLimitCommand() { AccountId = acct.Id, DailyWireTransferLimit = decimal.Parse(cmd[1]) }; cmdHandler.Handle(setDailyWireTransferLimit); Console.WriteLine($"setting dailyWireTransferLimit to {cmd[1]}"); } catch (Exception e) { Console.WriteLine(e.Message); } break; } } while (cmd[0].ToLower() != "exit"); }
public async Task Handle(WithdrawCashCommand cmd) { await Execute(cmd.AccountId, (account) => account.WithdrawCash(cmd.Amount)); }