using System; using System.Collections.Generic; using Moq; using Xunit; using LCU.API.Collections.CollectionsApi.Services; using LCU.API.Collections.CollectionsApi.Exceptions; using System.Threading.Tasks; using LCU.API.Collections.CollectionsApi.Dao; using LCU.API.Collections.CollectionsApiLib.Models; using LCU.API.Collections.TemenosApiWrapper.Services; using LCU.API.Collections.CollectionsApi.Models.TemenosApiWrapper; namespace LCU.API.Collections.CollectionsApiTests.ServiceTests { public class AccountServiceTest { private Mock _mockTemenosAccountDao; private AccountService _accountService; private Mock _mockTemenosApiWrapperService; public AccountServiceTest() { _mockTemenosAccountDao = new Mock(); _mockTemenosApiWrapperService = new Mock(); _accountService = new AccountService(_mockTemenosAccountDao.Object, _mockTemenosApiWrapperService.Object); } [Fact] public async void ListByAccountNo_Ok() { //setup _mockTemenosAccountDao.Setup(dao => dao.ListByAccountNoAsync(It.IsAny(), It.IsAny())).ReturnsAsync( new List() { new CollectionsApiLib.Models.TemenosAccount() { AccountId = 1234, AccountNumber = "123" } }); _mockTemenosApiWrapperService.Setup(service => service.AccountSearch(It.IsAny())).ReturnsAsync( new List() { new AccountSearchResponse() { InternalAccountNumber = "232354254425443", AccountAccountNumber = "123" } }); var result = await _accountService.ListByAccountNoAsync("123"); Assert.NotNull(result); Assert.Single(result); } [Fact] public async void ListByAccountNoTemenosApiWrapperService_Exception() { //setup _mockTemenosApiWrapperService.Setup(service => service.AccountSearch(It.IsAny())) .Throws(new Exception("Oops")); var exc = await Assert.ThrowsAsync(() => _accountService.ListByAccountNoAsync("123")); Assert.NotNull(exc); } [Fact] public async void ListByAccountNo_Exception() { //setup _mockTemenosAccountDao.Setup(dao => dao.ListByAccountNoAsync(It.IsAny(), It.IsAny())) .Throws(new Exception("Oops")); var exc = await Assert.ThrowsAsync(() => _accountService.ListByAccountNoAsync("123")); Assert.NotNull(exc); } [Fact] public async void Get_OK() { //setup _mockTemenosAccountDao.Setup(dao => dao.FindByIdAsync(It.IsAny())).ReturnsAsync( new CollectionsApiLib.Models.TemenosAccount() { AccountId = 1234, AccountNumber = "123" } ); var result = await _accountService.FindByIdAsync(123); Assert.NotNull(result); Assert.Equal(1234, result.AccountId); } [Fact] public async void Get_Server_Exception() { //setup _mockTemenosAccountDao.Setup(dao => dao.FindByIdAsync(It.IsAny())).ThrowsAsync(new Exception("Oops")); var result = await Assert.ThrowsAsync(() => _accountService.FindByIdAsync(123)); Assert.NotNull(result); } [Fact] public async void Get_NotFound() { //setup _mockTemenosAccountDao.Setup(dao => dao.FindByIdAsync(It.IsAny())) .Returns(Task.FromResult(null)); var result = await Assert.ThrowsAsync(() => _accountService.FindByIdAsync(123)); Assert.NotNull(result); Assert.Equal("Account with id 123 not found.", result.Message); } [Fact] public async void GetHistory_OK() { const int accountId = 111; //setup _mockTemenosAccountDao.Setup(dao => dao.FindCommentsAsync(accountId)).ReturnsAsync( new List() { new TemenosHistory() { Created = DateTime.Now, Message = "Comment", Type = HistoryType.Comment, UserId = 10 } } ); _mockTemenosAccountDao.Setup(dao => dao.FindHistoryAsync(accountId)).ReturnsAsync( new List() { new TemenosHistory() { Created = DateTime.Now, Message = "Workflow", Type = HistoryType.Workflow, UserId = 10 } } ); var result = await _accountService.GetHistoryByAccountIdAsync(accountId); Assert.NotNull(result); Assert.Equal(2, result.Count); } } }