public void AccountUnsubscribeFromCommissionFail() { //Arrange CommissionAccount acc = new CommissionAccount("123", 10, Currency.AUD); //Assert AccountManager nothing = null; var ex = Assert.Throws <ArgumentNullException>(() => acc.UnsubscribeFromCommission(nothing)); Assert.AreEqual($"{ExceptionMessages.AccountUnsubscribeNullParam() + Environment.NewLine}Parameter name: accountManager", ex.Message); }
public async Task <TransactionCheckResult> TransactionAsync(string accFromName, string userFromName, string accToName, string userToName, decimal amount) { TransactionCheckResult result = new TransactionCheckResult(); if (accFromName == accToName && userFromName == userToName) { result.IsSameAccs = true; return(result); } AccountEntity accEntityFrom = await GetAccountEntityAsync(accFromName, userFromName); AccountEntity accEntityTo = await GetAccountEntityAsync(accToName, userToName); Account accFrom = new Account(accEntityFrom, AccountCreationOptions.EmptyName); Account accTo = new Account(accEntityTo, AccountCreationOptions.EmptyName); decimal coefficient = await _globalRates.GetRateAsync(accFrom.Money.Curr, accTo.Money.Curr); accFrom.Money.AdjustExchangeRate(accFrom.Money.Curr, accTo.Money.Curr, coefficient); accTo.Money.AdjustExchangeRate(accFrom.Money.Curr, accTo.Money.Curr, coefficient); AccountManager accManager = await GetAccountManagerAsync(accEntityFrom, userFromName); AccountEntity commissionAccEntity = await GetCommissionAccount(); CommissionAccount commissionAcc = new CommissionAccount(commissionAccEntity, AccountCreationOptions.CurrencyOnly); commissionAcc.SubscribeToCommission(accManager); try { accManager.Transaction(accFrom, accTo, amount); } catch (Exception ex) { if (ex.Message == ExceptionMessages.TransactionInsufficientFunds()) { result.IsEnough = false; return(result); } else { throw new Exception(ex.Message); } } commissionAcc.UnsubscribeFromCommission(accManager); result.IsEnough = true; result.SendAmount = accEntityFrom.Amount - accFrom.Money.Amount; result.Commission = result.SendAmount - amount; result.Receive = accTo.Money.Amount - accEntityTo.Amount; result.CommissionOwner = commissionAccEntity.User.UserName; result.CommissionAccName = commissionAccEntity.AccountName; result.CurrFrom = accEntityFrom.Currency; result.CurrTo = accEntityTo.Currency; accEntityFrom.Amount = accFrom.Money.Amount; accEntityTo.Amount = accTo.Money.Amount; commissionAccEntity.Amount += commissionAcc.Money.Amount; DBContext.Accounts.Update(accEntityFrom); DBContext.Accounts.Update(accEntityTo); DBContext.Accounts.Update(commissionAccEntity); await DBContext.SaveChangesAsync(); return(result); }