[Test] public async Task Balance_Valid_Account_Id_Returns_Balance() { //Arrange var dataContext = new DataContext(_dataContextOptions.Options); IRepository <Account> accountRepository = new AccountRepository(dataContext); IRepository <Transaction> transactionRepository = new TransactionRepository(dataContext); IUnitOfWork unitOfWork = new UnitOfWork(dataContext, accountRepository, transactionRepository); IGenerateId idGenerator = new IdGenerator(); var mockRates = new Mock <IRatesService>(); mockRates.Setup(a => a.GetExchangeRate(It.IsAny <Currency>(), It.IsAny <Currency>(), It.IsAny <CancellationToken>())) .ReturnsAsync(() => new Rate("GBP", "GBP", 0.79m, DateTime.Now)); //Act IAccountService accountService = new AccountService(_mockAccountActionLogger.Object, unitOfWork, idGenerator); var createAccount = new CreateAccount("Martin Fowler", Amount.FromInput(100m)); AccountCreationSummary accountCreationSummary = await accountService.CreateAccount(createAccount); BalanceResult result = await accountService.GetBalance(new AccountInfo(accountCreationSummary.AccountId, Amount.None)); Assert.That(result, Is.Not.Null); Assert.That(result.Amount, Is.EqualTo(accountCreationSummary.Amount)); Assert.That(result.Status, Is.EqualTo(AccountStatus.Success)); }
public async Task Account_Creation_Invokes_Deposit() { //Arrange var dataContext = new DataContext(_dataContextOptions.Options); IRepository <Account> accountRepository = new AccountRepository(dataContext); IRepository <Transaction> transactionRepository = new TransactionRepository(dataContext); IUnitOfWork unitOfWork = new UnitOfWork(dataContext, accountRepository, transactionRepository); IGenerateId idGenerator = new IdGenerator(); //Act IAccountService accountService = new AccountService(_mockAccountActionLogger.Object, unitOfWork, idGenerator); var createAccount = new CreateAccount("Arlef Kaba", Amount.FromInput(100m)); AccountCreationSummary accountCreationSummary = await accountService.CreateAccount(createAccount); Account searchedAccount = await accountRepository.FindByIdAsync(accountCreationSummary.AccountId); //Assert Assert.That(accountCreationSummary, Is.Not.Null); Assert.That(accountCreationSummary.AccountId, Is.Not.Null); Assert.That(searchedAccount, Is.Not.Null); }
public async Task Deposit_RatesApi_Is_Invoked_Only_If_Currency_Is_Not_Base_Currency() { //Arrange var dataContext = new DataContext(_dataContextOptions.Options); IRepository <Account> accountRepository = new AccountRepository(dataContext); IRepository <Transaction> transactionRepository = new TransactionRepository(dataContext); IUnitOfWork unitOfWork = new UnitOfWork(dataContext, accountRepository, transactionRepository); IGenerateId idGenerator = new IdGenerator(); CancellationToken token = new CancellationToken(); var mockRates = new Mock <IRatesService>(); mockRates.Setup(a => a.GetExchangeRate(It.IsAny <Currency>(), It.IsAny <Currency>(), It.IsAny <CancellationToken>())) .ReturnsAsync(() => new Rate("GBP", "GBP", 0.79m, DateTime.Now)); //Act RatesApiOption ratesApiOption = new RatesApiOption("someurl", "USD"); IAccountService accountService = new AccountService(_mockAccountActionLogger.Object, unitOfWork, idGenerator); AccountCreationSummary accountCreationSummary = await accountService.CreateAccount(new CreateAccount("Jared", Amount.FromInput(100m))); AccountController accountController = new AccountController(_mockAccountControllerLogger.Object, accountService, mockRates.Object, ratesApiOption); CreateDepositRequest request = new CreateDepositRequest { AccountId = accountCreationSummary.AccountId, Amount = 30m, Currency = Currencies.EUR }; var result = (await accountController.Deposit(request, token) as ObjectResult); Assert.That(accountCreationSummary, Is.Not.Null); Assert.That(accountCreationSummary.AccountId, Is.Not.Empty); Assert.That(result, Is.Not.Null); mockRates.Verify(a => a.GetExchangeRate(It.IsAny <Currency>(), It.IsAny <Currency>(), It.IsAny <CancellationToken>()), Times.AtLeastOnce); }
public async Task Withdrawal_Amount_Cannot_Leave_Account_In_Negative() { //Arrange var dataContext = new DataContext(_dataContextOptions.Options); IRepository <Account> accountRepository = new AccountRepository(dataContext); IRepository <Transaction> transactionRepository = new TransactionRepository(dataContext); IUnitOfWork unitOfWork = new UnitOfWork(dataContext, accountRepository, transactionRepository); IGenerateId idGenerator = new IdGenerator(); CancellationToken token = new CancellationToken(); var mockRates = new Mock <IRatesService>(); mockRates.Setup(a => a.GetExchangeRate(It.IsAny <Currency>(), It.IsAny <Currency>(), It.IsAny <CancellationToken>())) .ReturnsAsync(() => new Rate("GBP", "GBP", 0.79m, DateTime.Now)); //Act RatesApiOption ratesApiOption = new RatesApiOption("someurl", "USD"); IAccountService accountService = new AccountService(_mockAccountActionLogger.Object, unitOfWork, idGenerator); AccountCreationSummary accountCreationSummary = await accountService.CreateAccount(new CreateAccount("Jared", Amount.FromInput(100m))); AccountController accountController = new AccountController(_mockAccountControllerLogger.Object, accountService, mockRates.Object, ratesApiOption); CreateWithdrawalRequest request = new CreateWithdrawalRequest { AccountId = accountCreationSummary.AccountId, Amount = 101m }; var result = (await accountController.Withdraw(request, token) as ObjectResult); Assert.That(accountCreationSummary, Is.Not.Null); Assert.That(accountCreationSummary.AccountId, Is.Not.Empty); Assert.That(result, Is.Not.Null); Assert.That(result.Value, Contains.Substring("amount is greater than balance")); }
public async Task Transaction_Valid_AccountId_Returns_Transactions() { //Arrange var dataContext = new DataContext(_dataContextOptions.Options); IRepository <Account> accountRepository = new AccountRepository(dataContext); IRepository <Transaction> transactionRepository = new TransactionRepository(dataContext); IUnitOfWork unitOfWork = new UnitOfWork(dataContext, accountRepository, transactionRepository); IGenerateId idGenerator = new IdGenerator(); var mockRates = new Mock <IRatesService>(); mockRates.Setup(a => a.GetExchangeRate(It.IsAny <Currency>(), It.IsAny <Currency>(), It.IsAny <CancellationToken>())) .ReturnsAsync(() => new Rate("GBP", "GBP", 0.79m, DateTime.Now)); //Act IAccountService accountService = new AccountService(_mockAccountActionLogger.Object, unitOfWork, idGenerator); var createAccount = new CreateAccount("Robert C. Martin", Amount.FromInput(100m)); decimal[] depositAmount = new[] { 30m, 50m, 10m }; AccountCreationSummary accountCreationSummary = await accountService.CreateAccount(createAccount); foreach (decimal depositEntry in depositAmount) { await accountService.Deposit(new CreateDeposit(accountCreationSummary.AccountId, Amount.FromInput(depositEntry), Currency.FromInput(Currencies.GBP))); } BalanceResult balance = await accountService.GetBalance(new AccountInfo(accountCreationSummary.AccountId, Amount.None)); TransactionResult result = await accountService.GetTransactions(accountCreationSummary.AccountId); Assert.That(result, Is.Not.Null); Assert.That(result.Status, Is.EqualTo(AccountStatus.Success)); }
public async Task <IActionResult> CreateAccount(CreateAccountRequest request) { _logger.LogInformation($"{nameof(AccountController)}.{nameof(CreateAccount)} - {request}"); if (!ModelState.IsValid) { return(BadRequest($"Problem with CreateAccountRequest")); } if (string.IsNullOrWhiteSpace(request.Name)) { return(BadRequest($"Cannot created with an empty name")); } CreateAccount createAccount = new CreateAccount(request.Name, Amount.FromInput(request.InitialDeposit)); AccountCreationSummary accountCreationSummary = await _accountService.CreateAccount(createAccount); AccountCreateResponse response = new AccountCreateResponse(accountCreationSummary.AccountId, accountCreationSummary.Amount.Value, accountCreationSummary.Name); return(Ok(response)); }